Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
ADD_LIBRARY(redisai_obj OBJECT
util/dict.c
util/queue.c
redisai.c
backends.c
model.c
Expand Down
18 changes: 9 additions & 9 deletions src/backends/onnxruntime.c
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ RAI_Model *RAI_ModelCreateORT(RAI_Backend backend, const char* devicestr, RAI_Mo
#else
// TODO: Do dynamic device/provider check with GetExecutionProviderType or something on these lines
if (device == RAI_DEVICE_GPU) {
RAI_SetError(error, RAI_EMODELCREATE, "GPU requested but ONNX couldn't find CUDA");
RAI_SetError(error, RAI_EMODELCREATE, "ERR GPU requested but ONNX couldn't find CUDA");
return NULL;
}
#endif
Expand Down Expand Up @@ -408,13 +408,13 @@ int RAI_ModelRunORT(RAI_ModelRunCtx *mctx, RAI_Error *error)
OrtSession *session = mctx->model->session;

if (session == NULL) {
RAI_SetError(error, RAI_EMODELRUN, "ONNXRuntime session was not allocated\n");
RAI_SetError(error, RAI_EMODELRUN, "ERR ONNXRuntime session was not allocated");
return 1;
}

const size_t nbatches = array_len(mctx->batches);
if (nbatches == 0) {
RAI_SetError(error, RAI_EMODELRUN, "No batches to run\n");
RAI_SetError(error, RAI_EMODELRUN, "ERR No batches to run");
return 1;
}

Expand Down Expand Up @@ -462,14 +462,14 @@ int RAI_ModelRunORT(RAI_ModelRunCtx *mctx, RAI_Error *error)

if (ninputs != n_input_nodes) {
char msg[70];
sprintf(msg, "Expected %li inputs but got %li", n_input_nodes, ninputs);
sprintf(msg, "ERR Expected %li inputs but got %li", n_input_nodes, ninputs);
RAI_SetError(error, RAI_EMODELRUN, msg);
return 1;
}

if (noutputs != n_output_nodes) {
char msg[70];
sprintf(msg, "Expected %li outputs but got %li", n_output_nodes, noutputs);
sprintf(msg, "ERR Expected %li outputs but got %li", n_output_nodes, noutputs);
RAI_SetError(error, RAI_EMODELRUN, msg);
return 1;
}
Expand Down Expand Up @@ -500,14 +500,14 @@ int RAI_ModelRunORT(RAI_ModelRunCtx *mctx, RAI_Error *error)
status = OrtSessionGetInputTypeInfo(session, i, &typeinfo);
const OrtTensorTypeAndShapeInfo* tensor_info = OrtCastTypeInfoToTensorInfo(typeinfo);
ONNXTensorElementDataType type = OrtGetTensorElementType(tensor_info);
// printf("Input %zu : type=%d\n", i, type);
// printf("Input %zu : type=%d", i, type);

size_t num_dims = OrtGetDimensionsCount(tensor_info);
// printf("Input %zu : num_dims=%zu\n", i, num_dims);
// printf("Input %zu : num_dims=%zu", i, num_dims);
input_node_dims.resize(num_dims);
OrtGetDimensions(tensor_info, (int64_t*)input_node_dims.data(), num_dims);
for (size_t j = 0; j < num_dims; j++) {
// printf("Input %zu : dim %zu=%jd\n", i, j, input_node_dims[j]);
// printf("Input %zu : dim %zu=%jd", i, j, input_node_dims[j]);
}

OrtReleaseTypeInfo(typeinfo);
Expand Down Expand Up @@ -549,7 +549,7 @@ int RAI_ModelRunORT(RAI_ModelRunCtx *mctx, RAI_Error *error)
RAI_TensorFree(output_tensor);
}
else {
printf("ERR: non-tensor output from ONNX models, ignoring (currently unsupported).\n");
printf("ERR: non-tensor output from ONNX models, ignoring (currently unsupported)");
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/backends/tensorflow.c
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,8 @@ RAI_Model *RAI_ModelCreateTF(RAI_Backend backend, const char* devicestr, RAI_Mod
TF_Operation* oper = TF_GraphOperationByName(model, inputs[i]);
if (oper == NULL) {
size_t len = strlen(inputs[i]);
char* msg = RedisModule_Calloc(40 + len, sizeof(*msg));
sprintf(msg, "Input node named \"%s\" not found in TF graph.", inputs[i]);
char* msg = RedisModule_Calloc(60 + len, sizeof(*msg));
sprintf(msg, "ERR Input node named \"%s\" not found in TF graph.", inputs[i]);
RAI_SetError(error, RAI_EMODELIMPORT, msg);
return NULL;
}
Expand All @@ -267,8 +267,8 @@ RAI_Model *RAI_ModelCreateTF(RAI_Backend backend, const char* devicestr, RAI_Mod
TF_Operation* oper = TF_GraphOperationByName(model, outputs[i]);
if (oper == NULL) {
size_t len = strlen(outputs[i]);
char* msg = RedisModule_Calloc(40 + len, sizeof(*msg));
sprintf(msg, "Output node named \"%s\" not found in TF graph.", outputs[i]);
char* msg = RedisModule_Calloc(60 + len, sizeof(*msg));
sprintf(msg, "ERR Output node named \"%s\" not found in TF graph", outputs[i]);
RAI_SetError(error, RAI_EMODELIMPORT, msg);
return NULL;
}
Expand Down Expand Up @@ -336,7 +336,7 @@ RAI_Model *RAI_ModelCreateTF(RAI_Backend backend, const char* devicestr, RAI_Mod
}
}
if (foundNoGPU == 1 && device == RAI_DEVICE_GPU) {
RAI_SetError(error, RAI_EMODELCREATE, "GPU requested but TF couldn't find CUDA");
RAI_SetError(error, RAI_EMODELCREATE, "ERR GPU requested but TF couldn't find CUDA");
TF_DeleteDeviceList(deviceList);
TF_DeleteStatus(deviceListStatus);
// TODO: free other memory allocations
Expand Down Expand Up @@ -424,7 +424,7 @@ int RAI_ModelRunTF(RAI_ModelRunCtx* mctx, RAI_Error *error) {

const size_t nbatches = array_len(mctx->batches);
if (nbatches == 0) {
RAI_SetError(error, RAI_EMODELRUN, "No batches to run\n");
RAI_SetError(error, RAI_EMODELRUN, "ERR No batches to run");
return 1;
}

Expand Down Expand Up @@ -522,7 +522,7 @@ int RAI_ModelSerializeTF(RAI_Model *model, char **buffer, size_t *len, RAI_Error
TF_GraphToGraphDef(model->model, tf_buffer, status);

if (TF_GetCode(status) != TF_OK) {
RAI_SetError(error, RAI_EMODELSERIALIZE, "Error serializing TF model");
RAI_SetError(error, RAI_EMODELSERIALIZE, "ERR Error serializing TF model");
TF_DeleteBuffer(tf_buffer);
TF_DeleteStatus(status);
return 1;
Expand Down
10 changes: 5 additions & 5 deletions src/backends/tflite.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ RAI_Model *RAI_ModelCreateTFLite(RAI_Backend backend, const char* devicestr, RAI
RAI_Device device;
int64_t deviceid;
if (!parseDeviceStr(devicestr, &device, &deviceid)) {
RAI_SetError(error, RAI_EMODELCONFIGURE, "Unsupported device");
RAI_SetError(error, RAI_EMODELCONFIGURE, "ERR Unsupported device");
return NULL;
}

Expand All @@ -39,7 +39,7 @@ RAI_Model *RAI_ModelCreateTFLite(RAI_Backend backend, const char* devicestr, RAI
dl_device = kDLGPU;
break;
default:
RAI_SetError(error, RAI_EMODELCONFIGURE, "Error configuring model: unsupported device\n");
RAI_SetError(error, RAI_EMODELCONFIGURE, "ERR Error configuring model: unsupported device");
return NULL;
}

Expand Down Expand Up @@ -86,7 +86,7 @@ int RAI_ModelRunTFLite(RAI_ModelRunCtx* mctx, RAI_Error *error) {

const size_t nbatches = array_len(mctx->batches);
if (nbatches == 0) {
RAI_SetError(error, RAI_EMODELRUN, "No batches to run\n");
RAI_SetError(error, RAI_EMODELRUN, "ERR No batches to run");
return 1;
}

Expand Down Expand Up @@ -149,13 +149,13 @@ int RAI_ModelRunTFLite(RAI_ModelRunCtx* mctx, RAI_Error *error) {

for(size_t i=0 ; i<noutputs; ++i) {
if (outputs_dl[i] == NULL) {
RAI_SetError(error, RAI_EMODELRUN, "Model did not generate the expected number of outputs.");
RAI_SetError(error, RAI_EMODELRUN, "ERR Model did not generate the expected number of outputs");
return 1;
}
RAI_Tensor* output_tensor = RAI_TensorCreateFromDLTensor(outputs_dl[i]);
if (nbatches > 1 && RAI_TensorDim(output_tensor, 0) != total_batch_size) {
RAI_TensorFree(output_tensor);
RAI_SetError(error, RAI_EMODELRUN, "Model did not generate the expected batch size.");
RAI_SetError(error, RAI_EMODELRUN, "ERR Model did not generate the expected batch size");
return 1;
}
if (nbatches > 1) {
Expand Down
8 changes: 4 additions & 4 deletions src/backends/torch.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ RAI_Model *RAI_ModelCreateTorch(RAI_Backend backend, const char* devicestr, RAI_
dl_device = kDLGPU;
break;
default:
RAI_SetError(error, RAI_EMODELCONFIGURE, "Error configuring model: unsupported device\n");
RAI_SetError(error, RAI_EMODELCONFIGURE, "ERR Error configuring model: unsupported device");
return NULL;
}

Expand Down Expand Up @@ -71,7 +71,7 @@ void RAI_ModelFreeTorch(RAI_Model* model, RAI_Error *error) {
int RAI_ModelRunTorch(RAI_ModelRunCtx* mctx, RAI_Error *error) {
const size_t nbatches = array_len(mctx->batches);
if (nbatches == 0) {
RAI_SetError(error, RAI_EMODELRUN, "No batches to run\n");
RAI_SetError(error, RAI_EMODELRUN, "ERR No batches to run");
return 1;
}

Expand Down Expand Up @@ -134,7 +134,7 @@ int RAI_ModelRunTorch(RAI_ModelRunCtx* mctx, RAI_Error *error) {

for(size_t i=0 ; i<noutputs; ++i) {
if (outputs_dl[i] == NULL) {
RAI_SetError(error, RAI_EMODELRUN, "Model did not generate the expected number of outputs.");
RAI_SetError(error, RAI_EMODELRUN, "ERR Model did not generate the expected number of outputs");
return 1;
}
RAI_Tensor* output_tensor = RAI_TensorCreateFromDLTensor(outputs_dl[i]);
Expand Down Expand Up @@ -188,7 +188,7 @@ RAI_Script *RAI_ScriptCreateTorch(const char* devicestr, const char *scriptdef,
dl_device = kDLGPU;
break;
default:
RAI_SetError(error, RAI_ESCRIPTCONFIGURE, "Error configuring script: unsupported device\n");
RAI_SetError(error, RAI_ESCRIPTCONFIGURE, "ERR Error configuring script: unsupported device");
break;
}

Expand Down
2 changes: 1 addition & 1 deletion src/err.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ void RAI_SetError(RAI_Error *err, RAI_ErrorCode code, const char *detail) {
if (detail) {
err->detail = RedisModule_Strdup(detail);
} else {
err->detail = RedisModule_Strdup("Generic error");
err->detail = RedisModule_Strdup("ERR Generic error");
}

err->detail_oneline = RAI_Chomp(err->detail);
Expand Down
38 changes: 19 additions & 19 deletions src/model.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,34 +220,34 @@ RAI_Model *RAI_ModelCreate(RAI_Backend backend, const char* devicestr, const cha
RAI_Model *model;
if (backend == RAI_BACKEND_TENSORFLOW) {
if (!RAI_backends.tf.model_create_with_nodes) {
RAI_SetError(err, RAI_EBACKENDNOTLOADED, "Backend not loaded: TF.\n");
RAI_SetError(err, RAI_EBACKENDNOTLOADED, "ERR Backend not loaded: TF");
return NULL;
}
model = RAI_backends.tf.model_create_with_nodes(backend, devicestr, opts, ninputs, inputs, noutputs, outputs, modeldef, modellen, err);
}
else if (backend == RAI_BACKEND_TFLITE) {
if (!RAI_backends.tflite.model_create) {
RAI_SetError(err, RAI_EBACKENDNOTLOADED, "Backend not loaded: TFLITE.\n");
RAI_SetError(err, RAI_EBACKENDNOTLOADED, "ERR Backend not loaded: TFLITE");
return NULL;
}
model = RAI_backends.tflite.model_create(backend, devicestr, opts, modeldef, modellen, err);
}
else if (backend == RAI_BACKEND_TORCH) {
if (!RAI_backends.torch.model_create) {
RAI_SetError(err, RAI_EBACKENDNOTLOADED, "Backend not loaded: TORCH.\n");
RAI_SetError(err, RAI_EBACKENDNOTLOADED, "ERR Backend not loaded: TORCH");
return NULL;
}
model = RAI_backends.torch.model_create(backend, devicestr, opts, modeldef, modellen, err);
}
else if (backend == RAI_BACKEND_ONNXRUNTIME) {
if (!RAI_backends.onnx.model_create) {
RAI_SetError(err, RAI_EBACKENDNOTLOADED, "Backend not loaded: ONNX.\n");
RAI_SetError(err, RAI_EBACKENDNOTLOADED, "ERR Backend not loaded: ONNX");
return NULL;
}
model = RAI_backends.onnx.model_create(backend, devicestr, opts, modeldef, modellen, err);
}
else {
RAI_SetError(err, RAI_EUNSUPPORTEDBACKEND, "Unsupported backend.\n");
RAI_SetError(err, RAI_EUNSUPPORTEDBACKEND, "ERR Unsupported backend\n");
return NULL;
}

Expand All @@ -265,28 +265,28 @@ void RAI_ModelFree(RAI_Model* model, RAI_Error* err) {

if (model->backend == RAI_BACKEND_TENSORFLOW) {
if (!RAI_backends.tf.model_free) {
RAI_SetError(err, RAI_EBACKENDNOTLOADED, "Backend not loaded: TF.\n");
RAI_SetError(err, RAI_EBACKENDNOTLOADED, "ERR Backend not loaded: TF\n");
return;
}
RAI_backends.tf.model_free(model, err);
}
else if (model->backend == RAI_BACKEND_TFLITE) {
if (!RAI_backends.tflite.model_free) {
RAI_SetError(err, RAI_EBACKENDNOTLOADED, "Backend not loaded: TFLITE.\n");
RAI_SetError(err, RAI_EBACKENDNOTLOADED, "ERR Backend not loaded: TFLITE");
return;
}
RAI_backends.tflite.model_free(model, err);
}
else if (model->backend == RAI_BACKEND_TORCH) {
if (!RAI_backends.torch.model_free) {
RAI_SetError(err, RAI_EBACKENDNOTLOADED, "Backend not loaded: TORCH.\n");
RAI_SetError(err, RAI_EBACKENDNOTLOADED, "ERR Backend not loaded: TORCH");
return;
}
RAI_backends.torch.model_free(model, err);
}
else if (model->backend == RAI_BACKEND_ONNXRUNTIME) {
if (!RAI_backends.onnx.model_free) {
RAI_SetError(err, RAI_EBACKENDNOTLOADED, "Backend not loaded: ONNX.\n");
RAI_SetError(err, RAI_EBACKENDNOTLOADED, "ERR Backend not loaded: ONNX");
return;
}
RAI_backends.onnx.model_free(model, err);
Expand Down Expand Up @@ -428,34 +428,34 @@ int RAI_ModelRun(RAI_ModelRunCtx* mctx, RAI_Error* err) {
switch (mctx->model->backend) {
case RAI_BACKEND_TENSORFLOW:
if (!RAI_backends.tf.model_run) {
RAI_SetError(err, RAI_EBACKENDNOTLOADED, "Backend not loaded: TF.\n");
RAI_SetError(err, RAI_EBACKENDNOTLOADED, "ERR Backend not loaded: TF");
return REDISMODULE_ERR;
}
ret = RAI_backends.tf.model_run(mctx, err);
break;
case RAI_BACKEND_TFLITE:
if (!RAI_backends.tflite.model_run) {
RAI_SetError(err, RAI_EBACKENDNOTLOADED, "Backend not loaded: TFLITE.\n");
RAI_SetError(err, RAI_EBACKENDNOTLOADED, "ERR Backend not loaded: TFLITE");
return REDISMODULE_ERR;
}
ret = RAI_backends.tflite.model_run(mctx, err);
break;
case RAI_BACKEND_TORCH:
if (!RAI_backends.torch.model_run) {
RAI_SetError(err, RAI_EBACKENDNOTLOADED, "Backend not loaded: TORCH.\n");
RAI_SetError(err, RAI_EBACKENDNOTLOADED, "ERR Backend not loaded: TORCH");
return REDISMODULE_ERR;
}
ret = RAI_backends.torch.model_run(mctx, err);
break;
case RAI_BACKEND_ONNXRUNTIME:
if (!RAI_backends.onnx.model_run) {
RAI_SetError(err, RAI_EBACKENDNOTLOADED, "Backend not loaded: ONNX.\n");
RAI_SetError(err, RAI_EBACKENDNOTLOADED, "ERR Backend not loaded: ONNX");
return REDISMODULE_ERR;
}
ret = RAI_backends.onnx.model_run(mctx, err);
break;
default:
RAI_SetError(err, RAI_EUNSUPPORTEDBACKEND, "Unsupported backend.\n");
RAI_SetError(err, RAI_EUNSUPPORTEDBACKEND, "ERR Unsupported backend");
return REDISMODULE_ERR;
}

Expand All @@ -473,34 +473,34 @@ int RAI_ModelSerialize(RAI_Model *model, char **buffer, size_t *len, RAI_Error *
switch (model->backend) {
case RAI_BACKEND_TENSORFLOW:
if (!RAI_backends.tf.model_serialize) {
RAI_SetError(err, RAI_EBACKENDNOTLOADED, "Backend not loaded: TF.\n");
RAI_SetError(err, RAI_EBACKENDNOTLOADED, "ERR Backend not loaded: TF");
return REDISMODULE_ERR;
}
ret = RAI_backends.tf.model_serialize(model, buffer, len, err);
break;
case RAI_BACKEND_TFLITE:
if (!RAI_backends.tflite.model_serialize) {
RAI_SetError(err, RAI_EBACKENDNOTLOADED, "Backend not loaded: TFLITE.\n");
RAI_SetError(err, RAI_EBACKENDNOTLOADED, "ERR Backend not loaded: TFLITE");
return REDISMODULE_ERR;
}
ret = RAI_backends.tflite.model_serialize(model, buffer, len, err);
break;
case RAI_BACKEND_TORCH:
if (!RAI_backends.torch.model_serialize) {
RAI_SetError(err, RAI_EBACKENDNOTLOADED, "Backend not loaded: TORCH.\n");
RAI_SetError(err, RAI_EBACKENDNOTLOADED, "ERR Backend not loaded: TORCH");
return REDISMODULE_ERR;
}
ret = RAI_backends.torch.model_serialize(model, buffer, len, err);
break;
case RAI_BACKEND_ONNXRUNTIME:
if (!RAI_backends.onnx.model_serialize) {
RAI_SetError(err, RAI_EBACKENDNOTLOADED, "Backend not loaded: ONNX.\n");
RAI_SetError(err, RAI_EBACKENDNOTLOADED, "ERR Backend not loaded: ONNX");
return REDISMODULE_ERR;
}
ret = RAI_backends.onnx.model_serialize(model, buffer, len, err);
break;
default:
RAI_SetError(err, RAI_EUNSUPPORTEDBACKEND, "Unsupported backend.\n");
RAI_SetError(err, RAI_EUNSUPPORTEDBACKEND, "ERR Unsupported backend");
return REDISMODULE_ERR;
}

Expand Down
Loading