You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
***TAG**: an optional string for tagging the script such as a version number or any arbitrary identifier
426
+
***device**: the device that will execute the model can be of:
427
+
***CPU**: a CPU device
428
+
***GPU**: a GPU device
429
+
***GPU:0**, ..., **GPU:n**: a specific GPU device on a multi-GPU system
430
+
***ENTRY_POINTS** A list of entry points to be used in the script. Each entry point should have the signature of `def entry_point(tensors: List[Tensor], keys: List[str], args: List[str])`. The purpose of each list is as follows:
431
+
*`tensors`: A list holding the input tensors to the function.
432
+
*`keys`: A list of keys that the torch script is about to preform read/write operations on.
433
+
*`args`: A list of additional arguments to the function. If the desired argument is not from type string, it is up to the caller to cast it to the right type, within the script.
434
+
***script**: a string containing [TorchScript](https://pytorch.org/docs/stable/jit.html) source code
435
+
436
+
_Return_
437
+
438
+
A simple 'OK' string or an error.
439
+
440
+
**Examples**
441
+
442
+
Given the following contents of the file 'addtwo.py':
It can be stored as a RedisAI script using the CPU device with [`redis-cli`](https://redis.io/topics/rediscli) as follows:
452
+
453
+
```
454
+
$ cat addtwo.py | redis-cli -x AI.SCRIPTSET myscript CPU TAG myscript:v0.1 ENTRY_POINTS 1 addtwo SOURCE
455
+
OK
456
+
```
457
+
411
458
## AI.SCRIPTSET
459
+
_This command is deprecated and will not be available in future versions. consider using AI.SCRIPTSTORE command instead._
412
460
The **`AI.SCRIPTSET`** command stores a [TorchScript](https://pytorch.org/docs/stable/jit.html) as the value of a key.
413
461
414
462
**Redis API**
@@ -471,8 +519,9 @@ An array with alternating entries that represent the following key-value pairs:
471
519
!!!!The command returns a list of key-value strings, namely `DEVICE device TAG tag [SOURCE source]`.
472
520
473
521
1.**DEVICE**: the script's device as a String
474
-
1.**TAG**: the scripts's tag as a String
475
-
1.**SOURCE**: the script's source code as a String
522
+
2.**TAG**: the scripts's tag as a String
523
+
3.**SOURCE**: the script's source code as a String
524
+
4.**ENTRY_POINTS** will return an array containing the script entry points
476
525
477
526
**Examples**
478
527
@@ -487,6 +536,8 @@ redis> AI.SCRIPTGET myscript
487
536
5) "source"
488
537
6) def addtwo(a, b):
489
538
return a + b
539
+
7) "Entry Points"
540
+
8) 1) addtwo
490
541
```
491
542
492
543
## AI.SCRIPTDEL
@@ -519,7 +570,7 @@ OK
519
570
520
571
## AI.SCRIPTEXECUTE
521
572
522
-
The **`AI.SCRIPTEXECUTE`** command runs a script stored as a key's value on its specified device. It accepts one or more inputs, where the inputs could be tensors stored in RedisAI, int, float, or strings and stores the script outputs as RedisAI tensors if required.
573
+
The **`AI.SCRIPTEXECUTE`** command runs a script stored as a key's value on its specified device. It a list of keys, input tensors and addtional script args.
523
574
524
575
The run request is put in a queue and is executed asynchronously by a worker thread. The client that had issued the run request is blocked until the script run is completed. When needed, tensors data is automatically copied to the device prior to execution.
525
576
@@ -532,22 +583,26 @@ A `TIMEOUT t` argument can be specified to cause a request to be removed from th
532
583
533
584
```
534
585
AI.SCRIPTEXECUTE <key> <function>
535
-
KEYS n <key> [keys...]
536
-
[INPUTS m <input> [input ...] | [LIST_INPUTS l <input> [input ...]]*]
586
+
[KEYS n <key> [keys...]]
587
+
[INPUTS m <input> [input ...]]
588
+
[ARGS k <arg> [arg...]]
537
589
[OUTPUTS k <output> [output ...] [TIMEOUT t]]+
538
590
```
539
591
540
592
_Arguments_
541
593
542
594
***key**: the script's key name
543
595
***function**: the name of the function to run
544
-
***KEYS**: Either a squence of key names that the script will access before, during and after its execution, or a tag which all those keys share. `KEYS` is a mandatory scope in this command. Redis will verify that all potional key accesses are done to the right shard.
545
-
***INPUTS**: Denotes the beginning of the input parameters list, followed by its length and one or more inputs; The inputs can be tensor key name, `string`, `int` or `float`. The order of the input should be aligned with the order of their respected parameter at the function signature. Note that list inputs are treated in the **LIST_INPUTS** scope.
546
-
***LIST_INPUTS** Denotes the beginning of a list, followed by its length and one or more inputs; The inputs can be tensor key name, `string`, `int` or `float`. The order of the input should be aligned with the order of their respected parameter at the function signature. Note that if more than one list is provided, their order should be aligned with the order of their respected paramter at the function signature.
596
+
***KEYS**: Either a squence of key names that the script will access before, during and after its execution, or a tag which all those keys share.
597
+
***INPUTS**: Denotes the beginning of the input parameters list, followed by its length and one or more input tensors.
598
+
***ARGS**: A list additional arguments that a user can send to the script. All args are sent as strings, but can be casted to other types supported by torch script, such as `int`, or `float`.
547
599
548
600
***OUTPUTS**: denotes the beginning of the output tensors keys' list, followed by its length and one or more key names.
549
601
***TIMEOUT**: the time (in ms) after which the client is unblocked and a `TIMEDOUT` string is returned
550
602
603
+
Note:
604
+
Either `KEYS` or `INPUTS` scopes should be provided this command (one or both scopes are acceptable). Those scopes indicate keyspace access and such, the right shard to execute the command at. Redis will verify that all potional key accesses are done to the right shard.
605
+
551
606
_Return_
552
607
553
608
A simple 'OK' string, a simple `TIMEDOUT` string, or an error.
Note: for the time being, as `AI.SCRIPTSET` is still avialable to use, `AI.SCRIPTEXECUTE` still supports running functions that are part of scripts stored with `AI.SCRIPTSET` or imported from old RDB/AOF files. Meaning calling `AI.SCRIPTEXECUTE` over a function without the dedicated signature of `(tensors: List[Tensor], keys: List[str], args: List[str]` will yield a "best effort" execution to match the deprecated API `AI.SCRIPTRUN` function execution. This will map `INPUTS` tensors only, to their counterpart input arguments in the function, according to the order which they apear.
664
+
608
665
### Redis Commands support.
609
-
RedisAI TorchScript now supports simple (non-blocking) Redis commands via the `redis.execute` API. The following (useless) script gets a key name (`x{1}`), and an `int` value (3). First, the script `SET`s the value in the key. Next, the script `GET`s the value back from the key, and sets it in a tensor which is eventually stored under the key 'y{1}'. Note that the inputs are `str` and `int`. The script sets and gets the value and set it into a tensor.
666
+
In RedisAI TorchScript now supports simple (non-blocking) Redis commnands via the `redis.execute` API. The following script gets a key name (`x{1}`), and an `int` value (3). First, the script `SET`s the value in the key. Next, the script `GET`s the value back from the key, and sets it in a tensor which is eventually stored under the key 'y{1}'. Note that the inputs are `str` and `int`. The script sets and gets the value and set it into a tensor.
@@ -764,7 +823,7 @@ It accepts one or more operations, split by the pipe-forward operator (`|>`).
764
823
765
824
By default, the DAG execution context is local, meaning that tensor keys appearing in the DAG only live in the scope of the command. That is, setting a tensor with `TENSORSET` will store it local memory and not set it to an actual database key. One can refer to that key in subsequent commands within the DAG, but that key won't be visible outside the DAG or to other clients - no keys are open at the database level.
766
825
767
-
Loading and persisting tensors from/to keyspace should be done explicitly. The user should specify which key tensors to load from keyspace using the `LOAD` keyword, and which command outputs to persist to the keyspace using the `PERSIST` keyspace. The user can also specify keys in Redis that are going to be accessed for read/write operations (for example, from within `AI.SCRIPTEXECUTE` command), by using the keyword `KEYS`.
826
+
Loading and persisting tensors from/to keyspace should be done explicitly. The user should specify which key tensors to load from keyspace using the `LOAD` keyword, and which command outputs to persist to the keyspace using the `PERSIST` keyspace. The user can also specify a tag or key which will assist for the routing of the DAG execution on the right shard in Redis that are going to be accessed for read/write operations (for example, from within `AI.SCRIPTEXECUTE` command), by using the keyword `ROUTING`.
768
827
769
828
As an example, if `command 1` sets a tensor, it can be referenced by any further command on the chaining.
770
829
@@ -776,7 +835,7 @@ A `TIMEOUT t` argument can be specified to cause a request to be removed from th
***LOAD**: denotes the beginning of the input tensors keys' list, followed by the number of keys, and one or more key names
787
846
***PERSIST**: denotes the beginning of the output tensors keys' list, followed by the number of keys, and one or more key names
788
-
***KEYS**: denotes the beginning of keys' list which are used within this command, followed by the number of keys, and one or more key names. Alternately, the keys names list can be replaced with a tag which all of those keys share. Redis will verify that all potential key accesses are done to the right shard.
847
+
***ROUTING**: denotes the a key name or a tag that will assist in routing the dag execution command to the right shard. Redis will verify that all potential key accesses are done to within the target shard.
789
848
790
-
_While each of the LOAD, PERSIST and KEYS sections are optional (and may appear at most once in the command), the command must contain **at least one** of these 3 keywords._
849
+
_While each of the LOAD, PERSIST and ROUTING sections are optional (and may appear at most once in the command), the command must contain **at least one** of these 3 keywords._
791
850
***TIMEOUT**: an optional argument, denotes the time (in ms) after which the client is unblocked and a `TIMEDOUT` string is returned
792
851
***|> command**: the chaining operator, that denotes the beginning of a RedisAI command, followed by one of RedisAI's commands. Command splitting is done by the presence of another `|>`. The supported commands are:
A common pattern is enqueuing multiple SCRIPTEXECUTE and MODELEXECUTE commands within a DAG. The following example uses ResNet-50,to classify images into 1000 object categories. Given that our input tensor contains each color represented as a 8-bit integer and that neural networks usually work with floating-point tensors as their input we need to cast a tensor to floating-point and normalize the values of the pixels - for that we will use `pre_process_3ch` function.
826
885
827
886
To optimize the classification process we can use a post process script to return only the category position with the maximum classification - for that we will use `post_process` script. Using the DAG capabilities we've removed the necessity of storing the intermediate tensors in the keyspace. You can even run the entire process without storing the output tensor, as follows:
0 commit comments