Skip to content

Commit 322890a

Browse files
committed
- save index by casting void* to size_t in TensorsNamesToIndices.
- Save the indices of the tensors to persist as values in persistTensors dict, and release TensorsNamesToIndices before DAG run.
1 parent 7c2736e commit 322890a

File tree

6 files changed

+20
-33
lines changed

6 files changed

+20
-33
lines changed

src/DAG/dag.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,8 @@ static int _DAG_PersistTensors(RedisModuleCtx *ctx, RedisAI_RunInfo *rinfo) {
118118

119119
while ((persist_entry = AI_dictNext(persist_iter))) {
120120
RedisModuleString *persist_key_name = AI_dictGetKey(persist_entry);
121-
AI_dictEntry *tensor_entry = AI_dictFind(rinfo->tensorsNamesToIndices, persist_key_name);
122-
RedisModule_Assert(tensor_entry);
123-
int *index = AI_dictGetVal(tensor_entry);
124-
RAI_Tensor *tensor = Dag_GetInternalTensor(rinfo, *index);
121+
size_t index = (size_t)AI_dictGetVal(persist_entry);
122+
RAI_Tensor *tensor = Dag_GetInternalTensor(rinfo, index);
125123
tensor = RAI_TensorGetShallowCopy(tensor);
126124

127125
if (_StoreTensorInKeySpace(ctx, tensor, persist_key_name, rinfo->err) == REDISMODULE_ERR) {

src/DAG/dag_builder.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@ int RAI_DAGLoadTensor(RAI_DAGRunCtx *run_info, const char *t_name, RAI_Tensor *t
3131
RedisModuleString *key_name = RedisModule_CreateString(NULL, t_name, strlen(t_name));
3232

3333
// Add the tensor to the DAG shared tensors and map its name to the relevant index.
34-
int *instance = RedisModule_Alloc(sizeof(int));
35-
*instance = array_len(rinfo->dagSharedTensors);
36-
AI_dictAdd(rinfo->tensorsNamesToIndices, (void *)key_name, (void *)instance);
34+
size_t index = array_len(rinfo->dagSharedTensors);
35+
AI_dictAdd(rinfo->tensorsNamesToIndices, (void *)key_name, (void *)index);
3736
RAI_TensorGetShallowCopy(tensor);
3837
rinfo->dagSharedTensors = array_append(rinfo->dagSharedTensors, (void *)tensor);
3938
RedisModule_FreeString(NULL, key_name);

src/DAG/dag_execute.c

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ int ValidatePersistKeys(RedisAI_RunInfo *rinfo, AI_dict *tensorsNamesToInd,
1717
AI_dictReleaseIterator(iter);
1818
return REDISMODULE_ERR;
1919
}
20+
size_t index = (size_t)AI_dictGetVal(entry);
21+
AI_dictReplace(persistTensorsNames, (void *)persist_key, (void *)index);
2022
}
2123
AI_dictReleaseIterator(iter);
2224
}
@@ -35,29 +37,23 @@ int MapTensorsKeysToIndices(RedisAI_RunInfo *rinfo, AI_dict *tensorsNamesToInd)
3537
RAI_SetError(rinfo->err, RAI_EDAGRUN, "ERR INPUT key cannot be found in DAG");
3638
return REDISMODULE_ERR;
3739
}
38-
int *ind = AI_dictGetVal(entry);
39-
currentOp->inkeys_indices = array_append(currentOp->inkeys_indices, *ind);
40+
size_t ind = (size_t)AI_dictGetVal(entry);
41+
currentOp->inkeys_indices = array_append(currentOp->inkeys_indices, ind);
4042
}
4143

4244
for (long long j = 0; j < array_len(currentOp->outkeys); j++) {
4345
RedisModuleString *key = currentOp->outkeys[j];
44-
int *ind = RedisModule_Alloc(sizeof(int));
45-
*ind = array_len(rinfo->dagSharedTensors);
46+
size_t ind = array_len(rinfo->dagSharedTensors);
4647

4748
// Add a new empty place holder in the array for an output tensor.
48-
// If this is MODELSET op, the tensor is already realized.
49+
// If this is a TENSORSET op, the tensor is already realized.
4950
if (currentOp->commandType == REDISAI_DAG_CMD_TENSORSET) {
5051
RAI_Tensor *t = RAI_TensorGetShallowCopy(currentOp->outTensor);
5152
rinfo->dagSharedTensors = array_append(rinfo->dagSharedTensors, t);
5253
} else {
5354
rinfo->dagSharedTensors = array_append(rinfo->dagSharedTensors, NULL);
5455
}
55-
currentOp->outkeys_indices = array_append(currentOp->outkeys_indices, *ind);
56-
AI_dictEntry *entry = AI_dictFind(tensorsNamesToInd, (void *)key);
57-
// If this key was already in the dict, remove and free the previous index
58-
if (entry) {
59-
RedisModule_Free(AI_dictGetVal(entry));
60-
}
56+
currentOp->outkeys_indices = array_append(currentOp->outkeys_indices, ind);
6157
AI_dictReplace(tensorsNamesToInd, (void *)key, (void *)ind);
6258
}
6359
}

src/DAG/dag_parser.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,8 @@ static int _ParseDAGLoadArgs(RedisModuleCtx *ctx, RedisModuleString **argv, int
5959
}
6060

6161
// Add the tensor to the DAG shared tensors and map its name to the relevant index.
62-
int *instance = RedisModule_Alloc(sizeof(int));
63-
*instance = array_len(*sharedTensors);
64-
AI_dictAdd(tensorsToInd, (void *)key_name, (void *)instance);
62+
size_t index = array_len(*sharedTensors);
63+
AI_dictAdd(tensorsToInd, (void *)key_name, (void *)index);
6564
*sharedTensors = array_append(*sharedTensors, (void *)RAI_TensorGetShallowCopy(t));
6665
number_loaded_keys++;
6766
}
@@ -113,7 +112,7 @@ static int _ParseDAGPersistArgs(RedisModuleString **argv, int argc, AI_dict *per
113112
RAI_SetError(err, RAI_EDAGRUN, "ERR PERSIST keys must be unique");
114113
return -1;
115114
}
116-
AI_dictAdd(persistTensorsNames, (void *)argv[argpos], (void *)1);
115+
AI_dictAdd(persistTensorsNames, (void *)argv[argpos], NULL);
117116
number_keys_to_persist++;
118117
}
119118
if (number_keys_to_persist != n_keys) {
@@ -313,6 +312,8 @@ int ParseDAGRunCommand(RedisAI_RunInfo *rinfo, RedisModuleCtx *ctx, RedisModuleS
313312
REDISMODULE_OK) {
314313
goto cleanup;
315314
}
315+
AI_dictRelease(rinfo->tensorsNamesToIndices);
316+
rinfo->tensorsNamesToIndices = NULL;
316317
res = REDISMODULE_OK;
317318

318319
cleanup:

src/run_info.c

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -161,17 +161,10 @@ void RAI_FreeRunInfo(struct RedisAI_RunInfo *rinfo) {
161161
RAI_TensorFree(rinfo->dagSharedTensors[i]);
162162
}
163163
array_free(rinfo->dagSharedTensors);
164-
{
165-
AI_dictIterator *iter = AI_dictGetSafeIterator(rinfo->tensorsNamesToIndices);
166-
AI_dictEntry *entry;
167-
while ((entry = AI_dictNext(iter))) {
168-
int *val = (int *)AI_dictGetVal(entry);
169-
RedisModule_Free(val);
170-
}
171-
AI_dictReleaseIterator(iter);
172-
}
173-
AI_dictRelease(rinfo->tensorsNamesToIndices);
174164
AI_dictRelease(rinfo->persistTensors);
165+
if (rinfo->tensorsNamesToIndices) {
166+
AI_dictRelease(rinfo->tensorsNamesToIndices);
167+
}
175168

176169
if (rinfo->dagOps) {
177170
for (size_t i = 0; i < array_len(rinfo->dagOps); i++) {

src/run_info.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ struct RedisAI_RunInfo {
9494
int single_op_dag;
9595
int single_device_dag;
9696
RAI_Tensor **dagSharedTensors; // Shared array of tensors that dag ops use.
97-
AI_dict *persistTensors; // The keys of the tensors that will be persisted.
97+
AI_dict *persistTensors; // Associates the tensors to persist with their indices .
9898
AI_dict *tensorsNamesToIndices; // Maps tensor key name to its (maximal) index.
9999
RAI_DagOp **dagOps; // all ops in DAG
100100
RAI_DagOp **dagDeviceOps; // all ops in DAG for device

0 commit comments

Comments
 (0)