Skip to content

Commit dfaed79

Browse files
author
DvirDukhan
authored
Merge pull request #634 from RedisAI/Revert_changes_in_TensorGetAs_APIs
2 parents b99ada7 + 067f6c9 commit dfaed79

File tree

5 files changed

+48
-17
lines changed

5 files changed

+48
-17
lines changed

src/tensor.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -468,12 +468,12 @@ int RAI_TensorGetValueAsDouble(RAI_Tensor *t, long long i, double *val) {
468468
*val = ((double *)data)[i];
469469
break;
470470
default:
471-
return REDISMODULE_ERR;
471+
return 0;
472472
}
473473
} else {
474-
return REDISMODULE_ERR;
474+
return 0;
475475
}
476-
return REDISMODULE_OK;
476+
return 1;
477477
}
478478

479479
int RAI_TensorGetValueAsLongLong(RAI_Tensor *t, long long i, long long *val) {
@@ -497,7 +497,7 @@ int RAI_TensorGetValueAsLongLong(RAI_Tensor *t, long long i, long long *val) {
497497
*val = ((int64_t *)data)[i];
498498
break;
499499
default:
500-
return REDISMODULE_ERR;
500+
return 0;
501501
}
502502
} else if (dtype.code == kDLUInt) {
503503
switch (dtype.bits) {
@@ -514,12 +514,12 @@ int RAI_TensorGetValueAsLongLong(RAI_Tensor *t, long long i, long long *val) {
514514
*val = ((uint64_t *)data)[i];
515515
break;
516516
default:
517-
return REDISMODULE_ERR;
517+
return 0;
518518
}
519519
} else {
520-
return REDISMODULE_ERR;
520+
return 0;
521521
}
522-
return REDISMODULE_OK;
522+
return 1;
523523
}
524524

525525
RAI_Tensor *RAI_TensorGetShallowCopy(RAI_Tensor *t) {
@@ -736,7 +736,7 @@ int RAI_TensorReplyWithValues(RedisModuleCtx *ctx, RAI_Tensor *t) {
736736
double val;
737737
for (i = 0; i < len; i++) {
738738
int ret = RAI_TensorGetValueAsDouble(t, i, &val);
739-
if (ret == REDISMODULE_ERR) {
739+
if (!ret) {
740740
RedisModule_ReplyWithError(ctx, "ERR cannot get values for this datatype");
741741
return -1;
742742
}
@@ -746,7 +746,7 @@ int RAI_TensorReplyWithValues(RedisModuleCtx *ctx, RAI_Tensor *t) {
746746
long long val;
747747
for (i = 0; i < len; i++) {
748748
int ret = RAI_TensorGetValueAsLongLong(t, i, &val);
749-
if (ret == REDISMODULE_ERR) {
749+
if (!ret) {
750750
RedisModule_ReplyWithError(ctx, "ERR cannot get values for this datatype");
751751
return -1;
752752
}

src/tensor.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ int RAI_TensorSetValueFromLongLong(RAI_Tensor *t, long long i, long long val);
237237
* @param t tensor to set the data
238238
* @param i dl_tensor data pointer position
239239
* @param val value to set the data from
240-
* @return 0 on success, or 1 if the setting failed
240+
* @return 1 on success, or 0 if the setting failed
241241
*/
242242
int RAI_TensorSetValueFromDouble(RAI_Tensor *t, long long i, double val);
243243

@@ -248,7 +248,7 @@ int RAI_TensorSetValueFromDouble(RAI_Tensor *t, long long i, double val);
248248
* @param t tensor to get the data
249249
* @param i dl_tensor data pointer position
250250
* @param val value to set the data to
251-
* @return 0 on success, or 1 if getting the data failed
251+
* @return 1 on success, or 0 if getting the data failed
252252
*/
253253
int RAI_TensorGetValueAsDouble(RAI_Tensor *t, long long i, double *val);
254254

@@ -259,7 +259,7 @@ int RAI_TensorGetValueAsDouble(RAI_Tensor *t, long long i, double *val);
259259
* @param t tensor to get the data
260260
* @param i dl_tensor data pointer position
261261
* @param val value to set the data to
262-
* @return 0 on success, or 1 if getting the data failed
262+
* @return 1 on success, or 0 if getting the data failed
263263
*/
264264
int RAI_TensorGetValueAsLongLong(RAI_Tensor *t, long long i, long long *val);
265265

tests/flow/tests_withGears.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,3 +348,34 @@ def TensorCreate_FromBlob(record):
348348

349349
values = con.execute_command('AI.TENSORGET', 'test2_res{1}', 'VALUES')
350350
env.assertEqual(values, [5, 6, 7, 8])
351+
352+
353+
@skip_if_gears_not_loaded
354+
def test_flatten_tensor_via_gears(env):
355+
script = '''
356+
357+
import redisAI
358+
359+
def FlattenTensor(record):
360+
361+
tensor = redisAI.createTensorFromValues('DOUBLE', [2,2], [1.0, 2.0, 3.0, 4.0])
362+
tensor_as_list = redisAI.tensorToFlatList(tensor)
363+
if tensor_as_list != [1.0, 2.0, 3.0, 4.0]:
364+
return "ERROR failed to flatten tensor to list of doubles"
365+
366+
tensor_blob = bytearray([5, 0, 6, 0, 7, 0, 8, 0])
367+
tensor = redisAI.createTensorFromBlob('UINT16', [2,2], tensor_blob)
368+
tensor_as_list = redisAI.tensorToFlatList(tensor)
369+
if tensor_as_list != [5, 6, 7, 8]:
370+
return "ERROR failed to flatten tensor to list of long long"
371+
return "test_OK"
372+
373+
374+
GB("CommandReader").map(FlattenTensor).register(trigger="FlattenTensor_test")
375+
'''
376+
377+
con = env.getConnection()
378+
ret = con.execute_command('rg.pyexecute', script)
379+
env.assertEqual(ret, b'OK')
380+
ret = con.execute_command('rg.trigger', 'FlattenTensor_test')
381+
env.assertEqual(ret[0], b'test_OK')

tests/module/DAG_utils.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ int testSimpleDAGRun(RedisModuleCtx *ctx) {
263263
double expceted[4] = {4, 9, 4, 9};
264264
double val;
265265
for (long long i = 0; i < 4; i++) {
266-
if(RedisAI_TensorGetValueAsDouble(out_tensor, i, &val) != 0) {
266+
if(!RedisAI_TensorGetValueAsDouble(out_tensor, i, &val)) {
267267
goto cleanup;
268268
}
269269
if (expceted[i] != val) {
@@ -316,7 +316,7 @@ int testSimpleDAGRun2(RedisModuleCtx *ctx) {
316316
double expceted[4] = {4, 6, 4, 6};
317317
double val;
318318
for (long long i = 0; i < 4; i++) {
319-
if(RedisAI_TensorGetValueAsDouble(out_tensor, i, &val) != 0) {
319+
if(!RedisAI_TensorGetValueAsDouble(out_tensor, i, &val)) {
320320
goto cleanup;
321321
}
322322
if (expceted[i] != val) {
@@ -427,7 +427,7 @@ int testDAGResnet(RedisModuleCtx *ctx) {
427427
RedisModule_Assert(_ResultsNumOutputs(results) == 1);
428428
RAI_Tensor *out_tensor = results.outputs[0];
429429
long long val;
430-
if(RedisAI_TensorGetValueAsLongLong(out_tensor, 0, &val) != 0) goto cleanup;
430+
if(!RedisAI_TensorGetValueAsLongLong(out_tensor, 0, &val)) goto cleanup;
431431
if (0 <= val && val <= 1000) {
432432
res = LLAPIMODULE_OK;
433433
}

tests/module/LLAPI.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ static void _ScriptFinishFunc(RAI_OnFinishCtx *onFinishCtx, void *private_data)
5353

5454
// Verify that we received the expected tensor at the end of the run.
5555
for (long long i = 0; i < 4; i++) {
56-
if(RedisAI_TensorGetValueAsDouble(tensor, i, &val[i]) != 0) {
56+
if(!RedisAI_TensorGetValueAsDouble(tensor, i, &val[i])) {
5757
goto finish;
5858
}
5959
if (expceted[i] != val[i]) {
@@ -86,7 +86,7 @@ static void _ModelFinishFunc(RAI_OnFinishCtx *onFinishCtx, void *private_data) {
8686

8787
// Verify that we received the expected tensor at the end of the run.
8888
for (long long i = 0; i < 4; i++) {
89-
if(RedisAI_TensorGetValueAsDouble(tensor, i, &val[i]) != 0) {
89+
if(!RedisAI_TensorGetValueAsDouble(tensor, i, &val[i])) {
9090
goto finish;
9191
}
9292
if (expceted[i] != val[i]) {

0 commit comments

Comments
 (0)