Skip to content

Commit 4f942c4

Browse files
committed
Address review comments re const-correctness
1 parent a7b27b6 commit 4f942c4

File tree

6 files changed

+24
-58
lines changed

6 files changed

+24
-58
lines changed

src/backends/onnxruntime.c

Lines changed: 7 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -78,40 +78,6 @@ DLDataType RAI_GetDLDataTypeFromORT(ONNXTensorElementDataType dtype) {
7878
return (DLDataType){ .bits = 0 };
7979
}
8080

81-
// OrtValue* RAI_OrtValueFromTensor(RAI_Tensor* t, RAI_Error *error) {
82-
// const OrtApi* ort = OrtGetApiBase()->GetApi(1);
83-
// OrtMemoryInfo* memory_info;
84-
// OrtStatus* status;
85-
// status = ort->CreateCpuMemoryInfo(OrtArenaAllocator, OrtMemTypeDefault, &memory_info);
86-
// if (status != NULL) {
87-
// goto error;
88-
// }
89-
//
90-
// OrtValue* out;
91-
// status = OrtCreateTensorWithDataAsOrtValue(
92-
// allocator_info,
93-
// t->tensor.dl_tensor.data,
94-
// RAI_TensorByteSize(t),
95-
// t->tensor.dl_tensor.shape,
96-
// t->tensor.dl_tensor.ndim,
97-
// RAI_GetOrtDataTypeFromDL(t->tensor.dl_tensor.dtype),
98-
// &out);
99-
//
100-
// if (status != NULL) {
101-
// OrtReleaseAllocatorInfo(allocator_info);
102-
// goto error;
103-
// }
104-
//
105-
// OrtReleaseAllocatorInfo(allocator_info);
106-
//
107-
// return out;
108-
//
109-
// error:
110-
// RAI_SetError(error, RAI_EMODELCREATE, OrtGetErrorMessage(status));
111-
// OrtReleaseStatus(status);
112-
// return NULL;
113-
// }
114-
11581
OrtValue* RAI_OrtValueFromTensors(RAI_Tensor** ts, size_t count, RAI_Error *error) {
11682
OrtStatus* status = NULL;
11783
const OrtApi* ort = OrtGetApiBase()->GetApi(1);
@@ -140,7 +106,7 @@ OrtValue* RAI_OrtValueFromTensors(RAI_Tensor** ts, size_t count, RAI_Error *erro
140106

141107
RAI_Tensor* t0 = ts[0];
142108

143-
int ndim = t0->tensor.dl_tensor.ndim;
109+
const int ndim = t0->tensor.dl_tensor.ndim;
144110
int64_t batched_shape[ndim];
145111

146112
for (size_t i=0; i<ndim; i++) {
@@ -267,11 +233,11 @@ RAI_Tensor* RAI_TensorCreateFromOrtValue(OrtValue* v, size_t batch_offset, size_
267233
goto error;
268234
}
269235

270-
size_t len = dtype.bits * elem_count;
236+
const size_t len = dtype.bits * elem_count;
271237

272-
size_t total_bytesize = len * sizeof(char);
273-
size_t sample_bytesize = total_bytesize / total_batch_size;
274-
size_t batch_bytesize = sample_bytesize * batch_size;
238+
const size_t total_bytesize = len * sizeof(char);
239+
const size_t sample_bytesize = total_bytesize / total_batch_size;
240+
const size_t batch_bytesize = sample_bytesize * batch_size;
275241

276242
char *data = RedisModule_Calloc(batch_bytesize, sizeof(*data));
277243
memcpy(data, ort_data + batch_offset, batch_bytesize);
@@ -489,8 +455,8 @@ int RAI_ModelRunORT(RAI_ModelRunCtx *mctx, RAI_Error *error)
489455
OrtValue *inputs[n_input_nodes];
490456
OrtValue *outputs[n_output_nodes];
491457

492-
size_t ninputs = array_len(mctx->batches[0].inputs);
493-
size_t noutputs = array_len(mctx->batches[0].outputs);
458+
const size_t ninputs = array_len(mctx->batches[0].inputs);
459+
const size_t noutputs = array_len(mctx->batches[0].outputs);
494460

495461
if (ninputs != n_input_nodes) {
496462
char msg[70];

src/backends/tensorflow.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ RAI_Tensor* RAI_TensorCreateFromTFTensor(TF_Tensor *tensor, size_t batch_offset,
8686
.device_id = 0
8787
};
8888

89-
size_t ndims = TF_NumDims(tensor);
89+
const size_t ndims = TF_NumDims(tensor);
9090

91-
int64_t total_batch_size = TF_Dim(tensor, 0);
91+
const int64_t total_batch_size = TF_Dim(tensor, 0);
9292

9393
int64_t* shape = RedisModule_Calloc(ndims, sizeof(*shape));
9494
int64_t* strides = RedisModule_Calloc(ndims, sizeof(*strides));
@@ -101,15 +101,15 @@ RAI_Tensor* RAI_TensorCreateFromTFTensor(TF_Tensor *tensor, size_t batch_offset,
101101
strides[i] *= strides[i+1] * shape[i+1];
102102
}
103103

104-
size_t sample_bytesize = TF_TensorByteSize(tensor) / total_batch_size;
104+
const size_t sample_bytesize = TF_TensorByteSize(tensor) / total_batch_size;
105105

106106
// FIXME: In TF, RunSession allocates memory for output tensors
107107
// This means that we either memcpy the tensor data and let
108108
// Redis be responsible for the memory, or we reuse the TF
109109
// allocated memory, which might not be optimal down the road
110110
// Note: on YOLO this has no impact on perf
111111
#ifdef RAI_COPY_RUN_OUTPUT
112-
size_t len = sample_bytesize * batch_size;
112+
const size_t len = sample_bytesize * batch_size;
113113
char* data = RedisModule_Calloc(len, sizeof(*data));
114114
memcpy(data, TF_TensorData(tensor) + sample_bytesize * batch_offset, len);
115115
#endif
@@ -181,7 +181,7 @@ TF_Tensor* RAI_TFTensorFromTensors(RAI_Tensor** ts, size_t count){
181181

182182
RAI_Tensor* t0 = ts[0];
183183

184-
int ndim = t0->tensor.dl_tensor.ndim;
184+
const int ndim = t0->tensor.dl_tensor.ndim;
185185
int64_t batched_shape[ndim];
186186

187187
for (size_t i=0; i<ndim; i++) {

src/backends/tflite.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ int RAI_ModelRunTFLite(RAI_ModelRunCtx* mctx, RAI_Error *error) {
9090
return 1;
9191
}
9292

93-
size_t ninputs = array_len(mctx->batches[0].inputs);
94-
size_t noutputs = array_len(mctx->batches[0].outputs);
93+
const size_t ninputs = array_len(mctx->batches[0].inputs);
94+
const size_t noutputs = array_len(mctx->batches[0].outputs);
9595

9696
RAI_Tensor* inputs[ninputs];
9797

src/backends/torch.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ int RAI_ModelRunTorch(RAI_ModelRunCtx* mctx, RAI_Error *error) {
7171
return 1;
7272
}
7373

74-
size_t ninputs = array_len(mctx->batches[0].inputs);
75-
size_t noutputs = array_len(mctx->batches[0].outputs);
74+
const size_t ninputs = array_len(mctx->batches[0].inputs);
75+
const size_t noutputs = array_len(mctx->batches[0].outputs);
7676

7777
RAI_Tensor* inputs[ninputs];
7878

src/model.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@ static void* RAI_Model_RdbLoad(struct RedisModuleIO *io, int encver) {
1717
RAI_Backend backend = RedisModule_LoadUnsigned(io);
1818
const char *devicestr = RedisModule_LoadStringBuffer(io, NULL);
1919

20-
size_t batchsize = RedisModule_LoadUnsigned(io);
21-
size_t minbatchsize = RedisModule_LoadUnsigned(io);
20+
const size_t batchsize = RedisModule_LoadUnsigned(io);
21+
const size_t minbatchsize = RedisModule_LoadUnsigned(io);
2222

23-
size_t ninputs = RedisModule_LoadUnsigned(io);
23+
const size_t ninputs = RedisModule_LoadUnsigned(io);
2424
const char **inputs = RedisModule_Alloc(ninputs * sizeof(char*));
2525

2626
for (size_t i=0; i<ninputs; i++) {
2727
inputs[i] = RedisModule_LoadStringBuffer(io, NULL);
2828
}
2929

30-
size_t noutputs = RedisModule_LoadUnsigned(io);
30+
const size_t noutputs = RedisModule_LoadUnsigned(io);
3131

3232
const char **outputs = RedisModule_Alloc(ninputs * sizeof(char*));
3333

src/tensor.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ RAI_Tensor* RAI_TensorCreateByConcatenatingTensors(RAI_Tensor** ts, long long n)
310310
long long batch_sizes[n];
311311
long long batch_offsets[n];
312312

313-
long long ndims = RAI_TensorNumDims(ts[0]);
313+
const long long ndims = RAI_TensorNumDims(ts[0]);
314314
long long dims[ndims];
315315

316316
// TODO check that all tensors have compatible dims
@@ -333,7 +333,7 @@ RAI_Tensor* RAI_TensorCreateByConcatenatingTensors(RAI_Tensor** ts, long long n)
333333
}
334334
dims[0] = total_batch_size;
335335

336-
long long dtype_size = RAI_TensorDataSize(ts[0]);
336+
const long long dtype_size = RAI_TensorDataSize(ts[0]);
337337

338338
DLDataType dtype = RAI_TensorDataType(ts[0]);
339339

@@ -348,10 +348,10 @@ RAI_Tensor* RAI_TensorCreateByConcatenatingTensors(RAI_Tensor** ts, long long n)
348348

349349
RAI_Tensor* RAI_TensorCreateBySlicingTensor(RAI_Tensor* t, long long offset, long long len) {
350350

351-
long long ndims = RAI_TensorNumDims(t);
351+
const long long ndims = RAI_TensorNumDims(t);
352352
long long dims[ndims];
353353

354-
long long dtype_size = RAI_TensorDataSize(t);
354+
const long long dtype_size = RAI_TensorDataSize(t);
355355
long long sample_size = 1;
356356

357357
for (long long i=1; i<ndims; i++) {

0 commit comments

Comments
 (0)