Skip to content

Commit 9bf31c2

Browse files
committed
Extend GPU test
1 parent 3b6e094 commit 9bf31c2

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

src/backends/onnxruntime.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,33 +41,33 @@ void *AllocatorAlloc(OrtAllocator *ptr, size_t size) {
4141
// 64-byte aligned, and an additional space in the size of a pointer to store
4242
// the address that RedisModule_Alloc returns.
4343
int offset = 63 + sizeof(void *);
44-
void *p1 = (void *)RedisModule_Alloc(size + offset);
45-
size_t allocated_size = RedisModule_MallocSize(p1);
44+
void *allocated_address = (void *)RedisModule_Alloc(size + offset);
45+
size_t allocated_size = RedisModule_MallocSize(allocated_address);
4646
// Update the total number of bytes that onnx is using and the number of accesses
4747
// that onnx made to the allocator.
4848
atomic_fetch_add(&OnnxMemory, allocated_size);
4949
atomic_fetch_add(&OnnxMemoryAccessCounter, 1);
5050
// This operation guarantees that p2 is the closest 64-aligned address to (p1+size_t).
51-
void **p2 = (void **)(((size_t)(p1) + offset) & (~63));
51+
void **aligned_address = (void **)(((size_t)(allocated_address) + offset) & (~63));
5252
// This stores the address p1 right before p2 (so we can retrieve it when we free).
53-
p2[-1] = p1;
54-
return p2;
53+
aligned_address[-1] = allocated_address;
54+
return aligned_address;
5555
}
5656

57-
void AllocatorFree(OrtAllocator *ptr, void *p) {
57+
void AllocatorFree(OrtAllocator *ptr, void *aligned_address) {
5858
(void)ptr;
59-
if (p == NULL) {
59+
if (aligned_address == NULL) {
6060
return;
6161
}
6262
// Retrieve the address that we originally received from RedisModule_Alloc
6363
// (this is the address that we need to sent to RedisModule_Free).
64-
void *p1 = ((void **)p)[-1];
65-
size_t allocated_size = RedisModule_MallocSize(p1);
64+
void *allocated_address = ((void **)aligned_address)[-1];
65+
size_t allocated_size = RedisModule_MallocSize(allocated_address);
6666
// Update the total number of bytes that onnx is using and the number of accesses
6767
// that onnx made to the allocator.
6868
atomic_fetch_sub(&OnnxMemory, allocated_size);
6969
atomic_fetch_add(&OnnxMemoryAccessCounter, 1);
70-
return RedisModule_Free(p1);
70+
return RedisModule_Free(allocated_address);
7171
}
7272

7373
unsigned long long RAI_GetMemoryInfoORT() { return OnnxMemory; }

tests/flow/tests_onnx.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,3 +552,19 @@ def test_onnx_use_custom_allocator_with_GPU(env):
552552
env.assertTrue(int(ai_memory_config["ai_onnxruntime_memory"]) > 472)
553553
env.assertTrue(int(ai_memory_config["ai_onnxruntime_memory"]) < 705)
554554
env.assertEqual(int(ai_memory_config["ai_onnxruntime_memory_access_num"]), 5)
555+
556+
# Make sure that allocator is not used for running and freeing the GPU model.
557+
sample_filename = os.path.join(test_data_path, 'one.raw')
558+
with open(sample_filename, 'rb') as f:
559+
sample_raw = f.read()
560+
con.execute_command('AI.TENSORSET', 'a{1}', 'FLOAT', 1, 1, 28, 28, 'BLOB', sample_raw)
561+
con.execute_command('AI.MODELRUN', 'm_gpu{1}', 'INPUTS', 'a{1}', 'OUTPUTS', 'b{1}')
562+
values = con.execute_command('AI.TENSORGET', 'b{1}', 'VALUES')
563+
argmax = max(range(len(values)), key=lambda i: values[i])
564+
env.assertEqual(argmax, 1)
565+
con.execute_command('AI.MODELDEL', 'm{1}')
566+
env.assertFalse(con.execute_command('EXISTS', 'm_gpu{1}'))
567+
ai_memory_config = {k.split(":")[0]: k.split(":")[1]
568+
for k in con.execute_command("INFO MODULES").decode().split("#")[4].split()[1:]}
569+
env.assertTrue(int(ai_memory_config["ai_onnxruntime_memory"]) < 705)
570+
env.assertEqual(int(ai_memory_config["ai_onnxruntime_memory_access_num"]), 5)

0 commit comments

Comments
 (0)