Skip to content

Commit 3b775bb

Browse files
committed
Remove test model and use only flow test (the test module is redundant since onnx memory info is saved in redis' INFO report, that can be produced by calling the INFO MODULES command)
1 parent c00874e commit 3b775bb

File tree

9 files changed

+62
-75
lines changed

9 files changed

+62
-75
lines changed

src/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
set(CMAKE_BUILD_TYPE Debug)
21
if (CMAKE_BUILD_TYPE STREQUAL Debug)
32
SET(DEBUG_SRC "${CMAKE_CURRENT_SOURCE_DIR}/../opt/readies/cetara/diag/gdb.c")
43
endif()

src/backends/onnxruntime.c

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,33 @@ OrtAllocator *global_allocator = NULL;
1717
unsigned long long OnnxMemory = 0;
1818
unsigned long long OnnxMemoryAccessCounter = 0;
1919

20+
const OrtMemoryInfo *AllocatorInfo(const OrtAllocator *allocator) {
21+
(void)allocator;
22+
const OrtApi *ort = OrtGetApiBase()->GetApi(1);
23+
OrtMemoryInfo *mem_info;
24+
if (ort->CreateCpuMemoryInfo(OrtDeviceAllocator, OrtMemTypeDefault, &mem_info) != NULL) {
25+
return NULL;
26+
}
27+
return mem_info;
28+
}
29+
30+
void *AllocatorAlloc(OrtAllocator *ptr, size_t size) {
31+
(void)ptr;
32+
void *p = RedisModule_Alloc(size);
33+
size_t allocated_size = RedisModule_MallocSize(p);
34+
atomic_fetch_add(&OnnxMemory, allocated_size);
35+
atomic_fetch_add(&OnnxMemoryAccessCounter, 1);
36+
return p;
37+
}
38+
39+
void AllocatorFree(OrtAllocator *ptr, void *p) {
40+
(void)ptr;
41+
size_t allocated_size = RedisModule_MallocSize(p);
42+
atomic_fetch_add(&OnnxMemory, -1 * allocated_size);
43+
atomic_fetch_add(&OnnxMemoryAccessCounter, 1);
44+
return RedisModule_Free(p);
45+
}
46+
2047
unsigned long long RAI_GetMemoryInfoORT() { return OnnxMemory; }
2148

2249
unsigned long long RAI_GetMemoryAccessORT() { return OnnxMemoryAccessCounter; }
@@ -269,33 +296,6 @@ RAI_Tensor *RAI_TensorCreateFromOrtValue(OrtValue *v, size_t batch_offset, long
269296
return NULL;
270297
}
271298

272-
const OrtMemoryInfo *myInfo(const OrtAllocator *allocator) {
273-
(void)allocator;
274-
const OrtApi *ort = OrtGetApiBase()->GetApi(1);
275-
OrtMemoryInfo *mem_info;
276-
if (ort->CreateCpuMemoryInfo(OrtDeviceAllocator, OrtMemTypeDefault, &mem_info) != NULL) {
277-
return NULL;
278-
}
279-
return mem_info;
280-
}
281-
282-
void *myAlloc(OrtAllocator *ptr, size_t size) {
283-
(void)ptr;
284-
void *p = RedisModule_Alloc(size);
285-
size_t allocated_size = RedisModule_MallocSize(p);
286-
atomic_fetch_add(&OnnxMemory, allocated_size);
287-
atomic_fetch_add(&OnnxMemoryAccessCounter, 1);
288-
return p;
289-
}
290-
291-
void myFree(OrtAllocator *ptr, void *p) {
292-
(void)ptr;
293-
size_t allocated_size = RedisModule_MallocSize(p);
294-
atomic_fetch_add(&OnnxMemory, -1 * allocated_size);
295-
atomic_fetch_add(&OnnxMemoryAccessCounter, 1);
296-
return RedisModule_Free(p);
297-
}
298-
299299
RAI_Model *RAI_ModelCreateORT(RAI_Backend backend, const char *devicestr, RAI_ModelOpts opts,
300300
const char *modeldef, size_t modellen, RAI_Error *error) {
301301

@@ -308,8 +308,8 @@ RAI_Model *RAI_ModelCreateORT(RAI_Backend backend, const char *devicestr, RAI_Mo
308308

309309
if (env == NULL) {
310310
ONNX_API(ort->CreateEnv(ORT_LOGGING_LEVEL_WARNING, "test", &env))
311-
ONNX_API(ort->CreateCustomDeviceAllocator(ORT_API_VERSION, myAlloc, myFree, myInfo,
312-
&global_allocator))
311+
ONNX_API(ort->CreateCustomDeviceAllocator(ORT_API_VERSION, AllocatorAlloc, AllocatorFree,
312+
AllocatorInfo, &global_allocator))
313313
ONNX_API(ort->RegisterCustomDeviceAllocator(env, global_allocator))
314314
}
315315

src/redisai.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#include <sys/resource.h>
2424
#include <sys/time.h>
2525
#include <unistd.h>
26-
#include "backends/onnxruntime.h"
2726

2827
#include "rmutil/alloc.h"
2928
#include "rmutil/args.h"
@@ -1093,9 +1092,6 @@ static int RedisAI_RegisterApi(RedisModuleCtx *ctx) {
10931092
REGISTER_API(DAGRunOpFree, ctx);
10941093
REGISTER_API(DAGFree, ctx);
10951094

1096-
// For ORT test module
1097-
REGISTER_API(LoadDefaultBackend, ctx);
1098-
10991095
return REDISMODULE_OK;
11001096
}
11011097

@@ -1112,6 +1108,9 @@ void RAI_moduleInfoFunc(RedisModuleInfoCtx *ctx, int for_crash_report) {
11121108
RAI_backends.onnx.get_memory_info());
11131109
RedisModule_InfoAddFieldULongLong(ctx, "onnxruntime_memory_access_num",
11141110
RAI_backends.onnx.get_memory_access_num());
1111+
} else {
1112+
RedisModule_InfoAddFieldULongLong(ctx, "onnxruntime_memory", 0);
1113+
RedisModule_InfoAddFieldULongLong(ctx, "onnxruntime_memory_access_num", 0);
11151114
}
11161115

11171116
struct rusage self_ru, c_ru;

src/tensor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#define SRC_TENSOR_H_
1212

1313
#include "config.h"
14-
//#include "dlpack/dlpack.h"
14+
#include "dlpack/dlpack.h"
1515
#include "err.h"
1616
#include "redismodule.h"
1717
#include "redisai.h"

src/tensor_struct.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#pragma once
22

33
#include "config.h"
4-
#include "../deps/linux-x64-cpu/dlpack/include/dlpack/dlpack.h"
5-
//#include "dlpack/dlpack.h"
4+
#include "dlpack/dlpack.h"
65
#include "limits.h"
76

87
#define LEN_UNKOWN ULONG_MAX

tests/flow/includes.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515
import paella
1616

1717
ROOT = os.environ.get("ROOT", None)
18-
if ROOT is None:
19-
sys.stderr.write("ROOT was not defined in the environment.\n")
20-
sys.exit(1)
2118
MAX_ITERATIONS = 2 if os.environ.get("MAX_ITERATIONS") == None else os.environ.get("MAX_ITERATIONS")
2219
TEST_TF = os.environ.get("TEST_TF") != "0" and os.environ.get("WITH_TF") != "0"
2320
TEST_TFLITE = os.environ.get("TEST_TFLITE") != "0" and os.environ.get("WITH_TFLITE") != "0"
File renamed without changes.

tests/flow/tests_onnx.py

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,12 @@
44
import redis
55
from includes import *
66
from RLTest import Env
7-
from functools import wraps
87

98
'''
109
python -m RLTest --test tests_onnx.py --module path/to/redisai.so
1110
'''
1211

1312

14-
def load_onnx_test_module(f):
15-
@wraps(f)
16-
def wrapper(env, *args, **kwargs):
17-
goal_dir = os.path.join(os.path.dirname(__file__), "../module/OnnxAllocator.so")
18-
TEST_MODULE_PATH = os.path.abspath(goal_dir)
19-
con = env.getConnection()
20-
modules = con.execute_command("MODULE", "LIST")
21-
if b'RAI_onnxAllocator' in [module[1] for module in modules]:
22-
return f(env, *args, **kwargs)
23-
try:
24-
ret = con.execute_command('MODULE', 'LOAD', TEST_MODULE_PATH)
25-
env.assertEqual(ret, b'OK')
26-
except Exception as e:
27-
env.assertFalse(True)
28-
env.debugPrint(str(e), force=True)
29-
return
30-
return f(env, *args, **kwargs)
31-
return wrapper
32-
33-
3413
def test_onnx_modelrun_mnist(env):
3514
if not TEST_ONNX:
3615
env.debugPrint("skipping {} since TEST_ONNX=0".format(sys._getframe().f_code.co_name), force=True)
@@ -475,17 +454,40 @@ def test_parallelism():
475454
env.assertEqual(load_time_config["ai_intra_op_parallelism"], "2")
476455

477456

478-
@load_onnx_test_module
479457
def test_onnx_use_custom_allocator(env):
480458
if not TEST_ONNX:
481459
env.debugPrint("skipping {} since TEST_ONNX=0".format(sys._getframe().f_code.co_name), force=True)
482460
return
461+
if DEVICE != 'CPU':
462+
env.debugPrint("skipping {} since the custom allocator is for CPU".format(sys._getframe().f_code.co_name), force=True)
463+
return
483464

484465
con = env.getConnection()
485-
ret = con.execute_command("RAI_onnxAllocator.modelSet")
486-
env.assertEquals(ret, b'OK')
487-
488466
test_data_path = os.path.join(os.path.dirname(__file__), 'test_data')
467+
model_filename = os.path.join(test_data_path, 'mul_1.onnx')
468+
with open(model_filename, 'rb') as f:
469+
model_pb = f.read()
470+
ai_memory_config = {k.split(":")[0]: k.split(":")[1]
471+
for k in con.execute_command("INFO MODULES").decode().split("#")[4].split()[1:]}
472+
env.assertEqual(int(ai_memory_config["ai_onnxruntime_memory"]), 0)
473+
474+
# Expect using the allocator during model set for allocating the model, its input name and output name.
475+
ret = con.execute_command('AI.MODELSET', 'm{1}', 'ONNX', DEVICE, 'BLOB', model_pb)
476+
env.assertEqual(ret, b'OK')
477+
ai_memory_config = {k.split(":")[0]: k.split(":")[1]
478+
for k in con.execute_command("INFO MODULES").decode().split("#")[4].split()[1:]}
479+
env.assertTrue(int(ai_memory_config["ai_onnxruntime_memory"]) > 100)
480+
env.assertEqual(int(ai_memory_config["ai_onnxruntime_memory_access_num"]), 3)
481+
482+
# Expect using the allocator free function when releasing the model.
483+
con.execute_command('AI.MODELDEL', 'm{1}')
484+
env.assertFalse(con.execute_command('EXISTS', 'm{1}'))
485+
ai_memory_config = {k.split(":")[0]: k.split(":")[1]
486+
for k in con.execute_command("INFO MODULES").decode().split("#")[4].split()[1:]}
487+
env.assertEqual(int(ai_memory_config["ai_onnxruntime_memory"]), 0)
488+
env.assertEqual(int(ai_memory_config["ai_onnxruntime_memory_access_num"]), 6)
489+
490+
# test allocator in model run op
489491
model_filename = os.path.join(test_data_path, 'mnist.onnx')
490492
sample_filename = os.path.join(test_data_path, 'one.raw')
491493

tests/module/Makefile

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,7 @@ else
1717
SHOBJ_LDFLAGS ?= -bundle -undefined dynamic_lookup
1818
endif
1919

20-
TEST_MODULES = \
21-
LLAPI.so \
22-
OnnxAllocator.so
23-
24-
SAVE_PATH = @echo "const char* path = \"$(dir $(abspath $(lastword $(MAKEFILE_LIST))))\";" > path.h
20+
TEST_MODULES = LLAPI.so
2521

2622
.PHONY: all
2723

@@ -31,17 +27,12 @@ all: $(TEST_MODULES)
3127
$(MAKE) CFLAGS="-m32" LDFLAGS="-melf_i386"
3228

3329
%.o: %.c
34-
${SAVE_PATH}
3530
$(CC) $(DEBUGFLAGS) -I../../src -I../../deps -DREDIS_MODULE_TARGET -DREDISMODULE_EXPERIMENTAL_API $(SHOBJ_CFLAGS) -fPIC -c $< -o $@
3631

3732
LLAPI.so: LLAPI.o DAG_utils.o
3833
$(CC) -o $@ $^ $(SHOBJ_LDFLAGS) -lc -lm
3934
chmod +x LLAPI.so
4035

41-
OnnxAllocator.so: OnnxAllocator.o
42-
$(CC) -o $@ $^ $(SHOBJ_LDFLAGS) -lc -lm
43-
chmod +x OnnxAllocator.so
44-
4536
.PHONY: clean
4637

4738
clean:

0 commit comments

Comments
 (0)