Skip to content

script execute command #682

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 41 commits into from
May 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
f76ef0e
added function dictionary
DvirDukhan Apr 13, 2021
737e005
added script execute parsing. schema validation.
DvirDukhan Apr 18, 2021
0310e2b
legacy tests pass
DvirDukhan Apr 19, 2021
0f5e542
make format
DvirDukhan Apr 19, 2021
57816a0
fixed memory leak
DvirDukhan Apr 19, 2021
91d2dee
torch redis pass with new API
DvirDukhan Apr 19, 2021
664c637
wip
DvirDukhan Apr 21, 2021
9145971
wip
DvirDukhan Apr 21, 2021
62347e1
wip
DvirDukhan Apr 25, 2021
01420ef
wip
DvirDukhan Apr 25, 2021
03e9281
wip
DvirDukhan Apr 26, 2021
a53d6ec
torch-redis tests pass
DvirDukhan Apr 27, 2021
a4ee7a3
wip
DvirDukhan Apr 28, 2021
ae4b0bd
starting valgrind
DvirDukhan Apr 28, 2021
eb9802c
valgrind pass
DvirDukhan Apr 28, 2021
cf13c59
make format
DvirDukhan Apr 28, 2021
2e447c7
added LITE check for getting script from keyspace
DvirDukhan Apr 28, 2021
2dc8f46
fixed typo
DvirDukhan May 2, 2021
22997c9
added torch_c docs
DvirDukhan May 2, 2021
9fffa16
fixed script_struct.h
DvirDukhan May 2, 2021
a802039
renamed otherInputs to nonTensorInputs
DvirDukhan May 2, 2021
9e819a5
removed log message from VerifyKeyInThisShard
DvirDukhan May 2, 2021
85debbb
fixed dictionaries comments
DvirDukhan May 2, 2021
5545bf0
fixed number of args on script keys validation
DvirDukhan May 2, 2021
86bf299
removed empty lines
DvirDukhan May 2, 2021
12bcbce
docs for RAI_DagOpSetRunKey
DvirDukhan May 2, 2021
7dcb8bf
fixed script_command_parser comments
DvirDukhan May 2, 2021
bd9911a
refactor RAI_HoldString
DvirDukhan May 2, 2021
c4150d6
fixed scriptRun_ctx comments
DvirDukhan May 2, 2021
742f8fb
fixed python tests comments
DvirDukhan May 2, 2021
fef615c
added multiple lists test
DvirDukhan May 2, 2021
7399de8
llapi
DvirDukhan May 3, 2021
2daf94d
docs
DvirDukhan May 3, 2021
65a083a
fixed commands.md
DvirDukhan May 4, 2021
2b827f1
fixed script.c and scriptRun_Ctx.h
DvirDukhan May 4, 2021
a2a93ad
added comment about KEYS scope
DvirDukhan May 4, 2021
8ac0e71
fixed LLAPI tests
DvirDukhan May 4, 2021
7143e89
make format
DvirDukhan May 4, 2021
5f5af5d
Merge branch 'master' into script_execute
alonre24 May 4, 2021
7b57f13
Merge branch 'master' into script_execute
DvirDukhan May 4, 2021
f7b870e
fixed valgrind
DvirDukhan May 4, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,119 @@ OK
The `AI.SCRIPTDEL` is equivalent to the [Redis `DEL` command](https://redis.io/commands/del) and should be used in its stead. This ensures compatibility with all deployment options (i.e., stand-alone vs. cluster, OSS vs. Enterprise).


## AI.SCRIPTEXECUTE

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.

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.

A `TIMEOUT t` argument can be specified to cause a request to be removed from the queue after it sits there `t` milliseconds, meaning that the client won't be interested in the result being computed after that time (`TIMEDOUT` is returned in that case).

!!! warning "Intermediate memory overhead"
The execution of models will generate intermediate tensors that are not allocated by the Redis allocator, but by whatever allocator is used in the TORCH backend (which may act on main memory or GPU memory, depending on the device), thus not being limited by `maxmemory` configuration settings of Redis.

**Redis API**

```
AI.SCRIPTEXECUTE <key> <function>
KEYS n <key> [keys...]
[INPUTS m <input> [input ...] | [LIST_INPUTS l <input> [input ...]]*]
[OUTPUTS k <output> [output ...] [TIMEOUT t]]+
```

_Arguments_

* **key**: the script's key name
* **function**: the name of the function to run
* **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.
* **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.
* **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.

* **OUTPUTS**: denotes the beginning of the output tensors keys' list, followed by its length and one or more key names.
* **TIMEOUT**: the time (in ms) after which the client is unblocked and a `TIMEDOUT` string is returned

_Return_

A simple 'OK' string, a simple `TIMEDOUT` string, or an error.

**Examples**

The following is an example of running the previously-created 'myscript' on two input tensors:

```
redis> AI.TENSORSET mytensor1 FLOAT 1 VALUES 40
OK
redis> AI.TENSORSET mytensor2 FLOAT 1 VALUES 2
OK
redis> AI.SCRIPTEXECUTE myscript addtwo KEYS 3 mytensor1 mytensor2 result INPUTS 2 mytensor1 mytensor2 OUTPUTS 1 result
OK
redis> AI.TENSORGET result VALUES
1) FLOAT
2) 1) (integer) 1
3) 1) "42"
```

Note: The above command could be executed with a shorter version, given all the keys are tagged with the same tag:

```
redis> AI.TENSORSET mytensor1{tag} FLOAT 1 VALUES 40
OK
redis> AI.TENSORSET mytensor2{tag} FLOAT 1 VALUES 2
OK
redis> AI.SCRIPTEXECUTE myscript{tag} addtwo KEYS 1 {tag} INPUTS 2 mytensor1{tag} mytensor2{tag} OUTPUTS 1 result{tag}
OK
redis> AI.TENSORGET result{tag} VALUES
1) FLOAT
2) 1) (integer) 1
3) 1) "42"
```

If 'myscript' supports `List[Tensor]` arguments:
```python
def addn(a, args : List[Tensor]):
return a + torch.stack(args).sum()
```

```
redis> AI.TENSORSET mytensor1{tag} FLOAT 1 VALUES 40
OK
redis> AI.TENSORSET mytensor2{tag} FLOAT 1 VALUES 1
OK
redis> AI.TENSORSET mytensor3{tag} FLOAT 1 VALUES 1
OK
redis> AI.SCRIPTEXECUTE myscript{tag} addn keys 1 {tag} INPUTS 1 mytensor1{tag} LIST_INPUTS 2 mytensor2{tag} mytensor3{tag} OUTPUTS 1 result{tag}
OK
redis> AI.TENSORGET result{tag} VALUES
1) FLOAT
2) 1) (integer) 1
3) 1) "42"
```

### Redis Commands support.
In RedisAI TorchScript now supports simple (non-blocking) Redis commnands via the `redis.execute` API. The following (usless) 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.

```
def redis_int_to_tensor(redis_value: int):
return torch.tensor(redis_value)

def int_set_get(key:str, value:int):
redis.execute("SET", key, str(value))
res = redis.execute("GET", key)
return redis_string_int_to_tensor(res)
```
```
redis> AI.SCRIPTEXECUTE redis_scripts{1} int_set_get KEYS 1 {1} INPUTS 2 x{1} 3 OUTPUTS 1 y{1}
OK
redis> AI.TENSORGET y{1} VALUES
1) (integer) 3
```

!!! warning "Intermediate memory overhead"
The execution of scripts may generate intermediate tensors that are not allocated by the Redis allocator, but by whatever allocator is used in the backends (which may act on main memory or GPU memory, depending on the device), thus not being limited by `maxmemory` configuration settings of Redis.

## AI.SCRIPTRUN
_This command is deprecated and will not be available in future versions. consider using AI.MODELEXECUTE command instead._

The **`AI.SCRIPTRUN`** command runs a script stored as a key's value on its specified device. It accepts one or more input tensors and store output tensors.

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.
Expand Down
13 changes: 10 additions & 3 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,34 @@ file (GLOB BACKEND_COMMON_SRC
backends/util.c
redis_ai_objects/err.c
util/dict.c
util/dictionaries.c
redis_ai_objects/tensor.c
util/string_utils.c
execution/utils.c
serialization/ai_datatypes.c)

ADD_LIBRARY(redisai_obj OBJECT
util/dict.c
util/dictionaries.c
util/queue.c
util/string_utils.c
redisai.c
execution/command_parser.c
execution/deprecated.c
execution/parsing/deprecated.c
execution/parsing/dag_parser.c
execution/parsing/model_commands_parser.c
execution/parsing/script_commands_parser.c
execution/parsing/parse_utils.c
execution/run_info.c
execution/background_workers.c
execution/utils.c
config/config.c
execution/DAG/dag.c
execution/DAG/dag_parser.c
execution/DAG/dag_builder.c
execution/DAG/dag_execute.c
execution/modelRun_ctx.c
execution/DAG/dag_op.c
execution/execution_contexts/modelRun_ctx.c
execution/execution_contexts/scriptRun_ctx.c
backends/backends.c
backends/util.c
redis_ai_objects/model.c
Expand Down
Loading