Skip to content

Commit b569fce

Browse files
authored
AI.INFO command (#259)
* Add AI.INFO command * Add tests * Add docs for AI.INFO * Added output to AI.INFO example
1 parent b9a13fe commit b569fce

File tree

8 files changed

+381
-89
lines changed

8 files changed

+381
-89
lines changed

docs/commands.md

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
## AI.CONFIG LOADBACKEND
44

5-
### AI.CONFIG LOADBACKEND Example
6-
75
Load a DL/ML backend.
86

97
By default, RedisAI starts with the ability to set and get tensor data, but setting and running models and scritps requires a computing backend to be loaded. This command allows to dynamically load a backend by specifying the backend identifier and the path to the backend library. Currently, once loaded, a backend cannot be unloaded, and there can be at most one backend per identifier loaded.
@@ -16,6 +14,8 @@ AI.CONFIG LOADBACKEND <backend_identifier> <location_of_backend_library>
1614

1715
It is possible to specify backends at the command-line when starting `redis-server`, see example below.
1816

17+
### AI.CONFIG LOADBACKEND Example
18+
1919
> Load the TORCH backend
2020
2121
```sql
@@ -234,3 +234,80 @@ If needed, input tensors are copied to the device specified in `AI.SCRIPTSET` be
234234
```sql
235235
AI.SCRIPTRUN addscript addtwo INPUTS a b OUTPUTS c
236236
```
237+
238+
## AI.INFO
239+
240+
Return information about runs of a `MODEL` or a `SCRIPT`.
241+
242+
At each `MODELRUN` or `SCRIPTRUN`, RedisAI will collect statistcs specific for each `MODEL` or `SCRIPT`,
243+
specific for the node (hence nodes in a cluster will have to be queried individually for their info).
244+
The following information is collected:
245+
246+
- `KEY`: the key being run
247+
- `TYPE`: either `MODEL` or `SCRIPT`
248+
- `BACKEND`: the type of backend (always `TORCH` for `SCRIPT`)
249+
- `DEVICE`: the device where the run has been executed
250+
- `DURATION`: cumulative duration in microseconds
251+
- `SAMPLES`: cumulative number of samples obtained from the 0-th (batch) dimension (for `MODEL` only)
252+
- `CALLS`: number of calls
253+
- `ERRORS`: number of errors generated after the run has been submitted (i.e. excluding errors generated during parsing of the command)
254+
255+
```sql
256+
AI.INFO <model_or_script_key>
257+
```
258+
259+
Statistcs are accumulated until the same command with an extra `RESETSTAT` argument is called. This resets the statistics relative to the model or script.
260+
261+
```sql
262+
AI.INFO <model_or_script_key> RESETSTAT
263+
```
264+
265+
The command can be called on a key until that key is removed using `MODELDEL` or `SCRIPTDEL`.
266+
267+
### AI.INFO Example
268+
269+
```sql
270+
AI.INFO amodel
271+
272+
> 1) KEY
273+
> 2) "amodel"
274+
> 3) TYPE
275+
> 4) MODEL
276+
> 5) BACKEND
277+
> 6) TORCH
278+
> 7) DEVICE
279+
> 8) CPU
280+
> 9) DURATION
281+
> 10) (integer) 6511
282+
> 11) SAMPLES
283+
> 12) (integer) 2
284+
> 13) CALLS
285+
> 14) (integer) 1
286+
> 15) ERRORS
287+
> 16) (integer) 0
288+
```
289+
290+
```sql
291+
AI.INFO amodel RESETSTAT
292+
293+
> OK
294+
295+
AI.INFO amodel
296+
297+
> 1) KEY
298+
> 2) "amodel"
299+
> 3) TYPE
300+
> 4) MODEL
301+
> 5) BACKEND
302+
> 6) TORCH
303+
> 7) DEVICE
304+
> 8) CPU
305+
> 9) DURATION
306+
> 10) (integer) 0
307+
> 11) SAMPLES
308+
> 12) (integer) 0
309+
> 13) CALLS
310+
> 14) (integer) 0
311+
> 15) ERRORS
312+
> 16) (integer) 0
313+
```

src/backends.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ RedisModuleString* RAI_GetModulePath(RedisModuleCtx *ctx) {
2121
return module_path;
2222
}
2323

24-
2524
RedisModuleString* RAI_GetBackendsPath(RedisModuleCtx *ctx) {
2625
Dl_info info;
2726
RedisModuleString* backends_path = NULL;
@@ -36,6 +35,20 @@ RedisModuleString* RAI_GetBackendsPath(RedisModuleCtx *ctx) {
3635
return backends_path;
3736
}
3837

38+
const char* RAI_BackendName(int backend) {
39+
switch (backend) {
40+
case RAI_BACKEND_TENSORFLOW:
41+
return "TF";
42+
case RAI_BACKEND_TFLITE:
43+
return "TFLITE";
44+
case RAI_BACKEND_TORCH:
45+
return "TORCH";
46+
case RAI_BACKEND_ONNXRUNTIME:
47+
return "ONNX";
48+
}
49+
return NULL;
50+
}
51+
3952
int RAI_LoadBackend_TensorFlow(RedisModuleCtx *ctx, const char *path) {
4053
if (RAI_backends.tf.model_run != NULL) {
4154
RedisModule_Log(ctx, "warning", "Could not load TF backend: backend already loaded");

src/backends.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,6 @@ char* RAI_BackendsPath;
3535
int RAI_LoadBackend(RedisModuleCtx *ctx, int backend, const char *path);
3636
int RAI_LoadDefaultBackend(RedisModuleCtx *ctx, int backend);
3737

38-
#endif
38+
const char* RAI_BackendName(int backend);
39+
40+
#endif

src/model.c

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -135,25 +135,11 @@ static void RAI_Model_AofRewrite(RedisModuleIO *aof, RedisModuleString *key, voi
135135
array_append(outputs_, RedisModule_CreateString(ctx, model->outputs[i], strlen(model->outputs[i])));
136136
}
137137

138-
char backend[256] = "";
139-
switch (model->backend) {
140-
case RAI_BACKEND_TENSORFLOW:
141-
strcpy(backend, "TF");
142-
break;
143-
case RAI_BACKEND_TFLITE:
144-
strcpy(backend, "TFLITE");
145-
break;
146-
case RAI_BACKEND_TORCH:
147-
strcpy(backend, "TORCH");
148-
break;
149-
case RAI_BACKEND_ONNXRUNTIME:
150-
strcpy(backend, "ONNX");
151-
break;
152-
}
138+
const char* backendstr = RAI_BackendName(model->backend);
153139

154140
RedisModule_EmitAOF(aof, "AI.MODELSET", "slccvcvb",
155141
key,
156-
backend, model->devicestr,
142+
backendstr, model->devicestr,
157143
"INPUTS", inputs_, model->ninputs,
158144
"OUTPUTS", outputs_, model->noutputs,
159145
buffer, len);

src/model_struct.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ typedef struct RAI_Model {
1717
char **outputs;
1818
size_t noutputs;
1919
long long refCount;
20-
long long backend_calls;
21-
long long backend_us;
2220
void* data;
2321
} RAI_Model;
2422

0 commit comments

Comments
 (0)