@@ -478,16 +478,28 @@ def test_onnx_use_custom_allocator(env):
478478 for k in con .execute_command ("INFO MODULES" ).decode ().split ("#" )[4 ].split ()[1 :]}
479479
480480 # Expect using at least 130+63+(size of an address) + 2*(2+63+(size of an address)) bytes.
481- env .assertTrue (int (ai_memory_config ["ai_onnxruntime_memory" ]) > 334 )
481+ model_allocation_bytes_used = int (ai_memory_config ["ai_onnxruntime_memory" ])
482+ env .assertTrue (model_allocation_bytes_used > 334 )
482483 env .assertEqual (int (ai_memory_config ["ai_onnxruntime_memory_access_num" ]), 3 )
484+ con .execute_command ('AI.TENSORSET' , 'a_mul{1}' , 'FLOAT' , 3 , 2 , 'VALUES' , 1.0 , 2.0 , 3.0 , 4.0 , 5.0 , 6.0 )
483485
484- # Expect using the allocator free function when releasing the model and input and output names.
486+ # Running the model should access the allocator 6 times: allocating+freeing input+output names,
487+ # and allocating+freeing the output as OrtValue.
488+ con .execute_command ('AI.MODELRUN' , 'm{1}' , 'INPUTS' , 'a_mul{1}' , 'OUTPUTS' , 'b{1}' )
489+ values = con .execute_command ('AI.TENSORGET' , 'b{1}' , 'VALUES' )
490+ env .assertEqual (values , [b'1' , b'4' , b'9' , b'16' , b'25' , b'36' ])
491+ ai_memory_config = {k .split (":" )[0 ]: k .split (":" )[1 ]
492+ for k in con .execute_command ("INFO MODULES" ).decode ().split ("#" )[4 ].split ()[1 :]}
493+ env .assertEqual (int (ai_memory_config ["ai_onnxruntime_memory_access_num" ]), 9 )
494+ env .assertEqual (int (ai_memory_config ["ai_onnxruntime_memory" ]), model_allocation_bytes_used )
495+
496+ # Expect using the allocator free function 3 times: when releasing the model, input name and output name.
485497 con .execute_command ('AI.MODELDEL' , 'm{1}' )
486498 env .assertFalse (con .execute_command ('EXISTS' , 'm{1}' ))
487499 ai_memory_config = {k .split (":" )[0 ]: k .split (":" )[1 ]
488500 for k in con .execute_command ("INFO MODULES" ).decode ().split ("#" )[4 ].split ()[1 :]}
489501 env .assertEqual (int (ai_memory_config ["ai_onnxruntime_memory" ]), 0 )
490- env .assertEqual (int (ai_memory_config ["ai_onnxruntime_memory_access_num" ]), 6 )
502+ env .assertEqual (int (ai_memory_config ["ai_onnxruntime_memory_access_num" ]), 12 )
491503
492504 # test the use of Redis allocator in model run op.
493505 model_filename = os .path .join (test_data_path , 'mnist.onnx' )
@@ -502,7 +514,7 @@ def test_onnx_use_custom_allocator(env):
502514 env .assertEqual (ret , b'OK' )
503515 con .execute_command ('AI.TENSORSET' , 'a{1}' , 'FLOAT' , 1 , 1 , 28 , 28 , 'BLOB' , sample_raw )
504516
505- # Expect 16 allocator's access from onnx during the run (in addition to the allocations that were made while
517+ # Expect 18 allocator's access from onnx during the run (in addition to the allocations that were made while
506518 # creating the model).
507519 ai_memory_config = {k .split (":" )[0 ]: k .split (":" )[1 ]
508520 for k in con .execute_command ("INFO MODULES" ).decode ().split ("#" )[4 ].split ()[1 :]}
@@ -511,7 +523,7 @@ def test_onnx_use_custom_allocator(env):
511523 ai_memory_config = {k .split (":" )[0 ]: k .split (":" )[1 ]
512524 for k in con .execute_command ("INFO MODULES" ).decode ().split ("#" )[4 ].split ()[1 :]}
513525 allocator_access_num_after = ai_memory_config ["ai_onnxruntime_memory_access_num" ]
514- env .assertEqual (int (allocator_access_num_after ) - int (allocator_access_num_before ), 16 )
526+ env .assertEqual (int (allocator_access_num_after ) - int (allocator_access_num_before ), 18 )
515527
516528 values = con .execute_command ('AI.TENSORGET' , 'b{1}' , 'VALUES' )
517529 argmax = max (range (len (values )), key = lambda i : values [i ])
@@ -549,18 +561,26 @@ def test_onnx_use_custom_allocator_with_GPU(env):
549561 for k in con .execute_command ("INFO MODULES" ).decode ().split ("#" )[4 ].split ()[1 :]}
550562
551563 # Expect using at least 130+63+(size of an address) + 4*(2+63+(size of an address)) bytes.
552- env .assertTrue (int (ai_memory_config ["ai_onnxruntime_memory" ]) > 472 )
553- env .assertTrue (int (ai_memory_config ["ai_onnxruntime_memory" ]) < 705 )
564+ model_allocation_bytes_used = int (ai_memory_config ["ai_onnxruntime_memory" ])
565+ env .assertTrue (model_allocation_bytes_used > 472 )
566+ env .assertTrue (model_allocation_bytes_used < 705 )
554567 env .assertEqual (int (ai_memory_config ["ai_onnxruntime_memory_access_num" ]), 5 )
555568
556- # Make sure that allocator is not used for running and freeing the GPU model.
569+ # Make sure that allocator is not used for running and freeing the GPU model, except for
570+ # the input and output names allocations (and deallocations).
557571 con .execute_command ('AI.TENSORSET' , 'a{1}' , 'FLOAT' , 3 , 2 , 'VALUES' , 1.0 , 2.0 , 3.0 , 4.0 , 5.0 , 6.0 )
558572 con .execute_command ('AI.MODELRUN' , 'm_gpu{1}' , 'INPUTS' , 'a{1}' , 'OUTPUTS' , 'b{1}' )
559573 values = con .execute_command ('AI.TENSORGET' , 'b{1}' , 'VALUES' )
560574 env .assertEqual (values , [b'1' , b'4' , b'9' , b'16' , b'25' , b'36' ])
575+ # Expect that memory usage didn't change, and for another 4 accesses to the allocator (input and output names
576+ # allocation and free)
577+ env .assertEqual (int (ai_memory_config ["ai_onnxruntime_memory" ]), model_allocation_bytes_used )
578+ env .assertEqual (int (ai_memory_config ["ai_onnxruntime_memory_access_num" ]), 9 )
579+
580+ # Expect only 2 more accesses in delete - for deallocating input and output names
561581 con .execute_command ('AI.MODELDEL' , 'm_gpu{1}' )
562582 env .assertFalse (con .execute_command ('EXISTS' , 'm_gpu{1}' ))
563583 ai_memory_config = {k .split (":" )[0 ]: k .split (":" )[1 ]
564584 for k in con .execute_command ("INFO MODULES" ).decode ().split ("#" )[4 ].split ()[1 :]}
565- env .assertTrue (int (ai_memory_config ["ai_onnxruntime_memory " ]) < 705 )
566- env . assertEqual ( int ( ai_memory_config [ "ai_onnxruntime_memory_access_num" ]), 5 )
585+ env .assertEqual (int (ai_memory_config ["ai_onnxruntime_memory_access_num " ]), 11 )
586+
0 commit comments