Skip to content

Commit 92084f9

Browse files
author
DvirDukhan
committed
docs
1 parent c3e6473 commit 92084f9

File tree

1 file changed

+68
-14
lines changed

1 file changed

+68
-14
lines changed

docs/commands.md

Lines changed: 68 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,55 @@ redis> > AI._MODELSCAN
408408
2) imagenet:5.0
409409
```
410410

411+
412+
## AI.SCRIPTSTORE
413+
The **`AI.SCRIPTSTORE`** command stores a [TorchScript](https://pytorch.org/docs/stable/jit.html) as the value of a key.
414+
415+
**Redis API**
416+
417+
```
418+
AI.SCRIPTSTORE <key> <device> [TAG tag] ENTRY_POINTS <entry_point_amoint> <entry_point> [<entry_point>...] SOURCE "<script>"
419+
```
420+
421+
_Arguments_
422+
423+
424+
* **key**: the script's key name
425+
* **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':
443+
444+
```python
445+
def addtwo(tensors: List[Tensor], keys: List[str], args: List[str]):
446+
a = tensors[0]
447+
b = tensors[1]
448+
return a + b
449+
```
450+
451+
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+
411458
## AI.SCRIPTSET
459+
_This command is deprecated and will not be available in future versions. consider using AI.MODELSTORE command instead._
412460
The **`AI.SCRIPTSET`** command stores a [TorchScript](https://pytorch.org/docs/stable/jit.html) as the value of a key.
413461

414462
**Redis API**
@@ -471,8 +519,9 @@ An array with alternating entries that represent the following key-value pairs:
471519
!!!!The command returns a list of key-value strings, namely `DEVICE device TAG tag [SOURCE source]`.
472520

473521
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+
* **ENTRY_POINTS** will return an array containing the script entry points
476525

477526
**Examples**
478527

@@ -487,6 +536,8 @@ redis> AI.SCRIPTGET myscript
487536
5) "source"
488537
6) def addtwo(a, b):
489538
return a + b
539+
7) "Entry Points"
540+
8) 1) addtwo
490541
```
491542

492543
## AI.SCRIPTDEL
@@ -519,7 +570,7 @@ OK
519570

520571
## AI.SCRIPTEXECUTE
521572

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 keys, input tensors and addtional script args.
523574

524575
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.
525576

@@ -533,7 +584,8 @@ A `TIMEOUT t` argument can be specified to cause a request to be removed from th
533584
```
534585
AI.SCRIPTEXECUTE <key> <function>
535586
KEYS n <key> [keys...]
536-
[INPUTS m <input> [input ...] | [LIST_INPUTS l <input> [input ...]]*]
587+
[INPUTS m <input> [input ...]
588+
[ARGS k <arg> [arg...]]
537589
[OUTPUTS k <output> [output ...] [TIMEOUT t]]+
538590
```
539591

@@ -542,8 +594,8 @@ _Arguments_
542594
* **key**: the script's key name
543595
* **function**: the name of the function to run
544596
* **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.
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`.
547599

548600
* **OUTPUTS**: denotes the beginning of the output tensors keys' list, followed by its length and one or more key names.
549601
* **TIMEOUT**: the time (in ms) after which the client is unblocked and a `TIMEDOUT` string is returned
@@ -584,10 +636,10 @@ redis> AI.TENSORGET result{tag} VALUES
584636
3) 1) "42"
585637
```
586638

587-
If 'myscript' supports `List[Tensor]` arguments:
639+
An example that supports `List[Tensor]` arguments:
588640
```python
589-
def addn(a, args : List[Tensor]):
590-
return a + torch.stack(args).sum()
641+
def addn(tensors: List[Tensor], keys: List[str], args: List[str]):
642+
return torch.stack(tensors).sum()
591643
```
592644

593645
```
@@ -597,7 +649,7 @@ redis> AI.TENSORSET mytensor2{tag} FLOAT 1 VALUES 1
597649
OK
598650
redis> AI.TENSORSET mytensor3{tag} FLOAT 1 VALUES 1
599651
OK
600-
redis> AI.SCRIPTEXECUTE myscript{tag} addn keys 1 {tag} INPUTS 1 mytensor1{tag} LIST_INPUTS 2 mytensor2{tag} mytensor3{tag} OUTPUTS 1 result{tag}
652+
redis> AI.SCRIPTEXECUTE myscript{tag} addn keys 1 {tag} INPUTS 3 mytensor1{tag} mytensor2{tag} mytensor3{tag} OUTPUTS 1 result{tag}
601653
OK
602654
redis> AI.TENSORGET result{tag} VALUES
603655
1) FLOAT
@@ -606,19 +658,21 @@ redis> AI.TENSORGET result{tag} VALUES
606658
```
607659

608660
### 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.
661+
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.
610662

611663
```
612664
def redis_int_to_tensor(redis_value: int):
613665
return torch.tensor(redis_value)
614666
615-
def int_set_get(key:str, value:int):
616-
redis.execute("SET", key, str(value))
667+
def int_set_get(tensors: List[Tensor], keys: List[str], args: List[str]):
668+
key = keys[0]
669+
value = args[0]
670+
redis.execute("SET", key, value)
617671
res = redis.execute("GET", key)
618672
return redis_string_int_to_tensor(res)
619673
```
620674
```
621-
redis> AI.SCRIPTEXECUTE redis_scripts{1} int_set_get KEYS 1 {1} INPUTS 2 x{1} 3 OUTPUTS 1 y{1}
675+
redis> AI.SCRIPTEXECUTE redis_scripts{1} int_set_get KEYS 1 x{1} ARGS 1 3 OUTPUTS 1 y{1}
622676
OK
623677
redis> AI.TENSORGET y{1} VALUES
624678
1) (integer) 3

0 commit comments

Comments
 (0)