@@ -286,10 +286,9 @@ static int _testSimpleDAGRun2Error(RedisModuleCtx *ctx, RAI_DAGRunCtx *run_info)
286286 int res = LLAPIMODULE_ERR ;
287287
288288 RedisModuleKey * key ;
289- RAI_Tensor * tensor = _getFromKeySpace (ctx , "a{1}" , & key );
289+ RAI_Tensor * tensor = ( RAI_Tensor * ) _getFromKeySpace (ctx , "a{1}" , & key );
290290 RedisModule_CloseKey (key );
291291 RedisAI_DAGAddTensorSet (run_info , "input1" , tensor );
292- //RedisAI_DAGLoadTensor(run_info, "a{1}", err);
293292
294293 // The script myscript{1} should exist in key space.
295294 RAI_Script * script = (RAI_Script * )_getFromKeySpace (ctx , "myscript{1}" , & key );
@@ -383,3 +382,86 @@ int RAI_llapi_DAGRun(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
383382 }
384383 return RedisModule_ReplyWithSimpleString (ctx , "DAG run success" );
385384}
385+
386+ int RAI_llapi_DAG_resnet (RedisModuleCtx * ctx , RedisModuleString * * argv , int argc ) {
387+ REDISMODULE_NOT_USED (argv );
388+
389+ if (argc > 1 ) {
390+ RedisModule_WrongArity (ctx );
391+ return REDISMODULE_OK ;
392+ }
393+ RAI_Error * err ;
394+ RedisAI_InitError (& err );
395+ RAI_Tensor * * outputs = array_new (RAI_Tensor * , 1 );
396+ RAI_Error * * opsStatus = array_new (RAI_Error * , 1 );
397+ DAGRunResults results = {.outputs = outputs , .opsStatus = opsStatus };
398+ const char * test_res = "DAG resnet failed" ;
399+
400+ // Build the DAG with LOAD->SCRIPTRUN->MODELRUN->MODELRUN-SCRIPTRUN->SCRIPTRUN->TENSORGET
401+ RAI_DAGRunCtx * run_info = RedisAI_DAGRunCtxCreate ();
402+ int status = RedisAI_DAGLoadTensor (run_info , "image:{{1}}" , err );
403+ if (status != REDISMODULE_OK ) goto cleanup ;
404+
405+ RedisModuleKey * key ;
406+ RAI_Script * script = (RAI_Script * )_getFromKeySpace (ctx , "imagenet_script1:{{1}}" , & key );
407+ RedisModule_CloseKey (key );
408+ RAI_DAGRunOp * script_op = RedisAI_DAGCreateScriptRunOp (script , "pre_process_3ch" );
409+ RedisAI_DAGRunOpAddInput (script_op , "image:{{1}}" );
410+ RedisAI_DAGRunOpAddOutput (script_op , "tmp_key:{{1}}" );
411+ RedisAI_DAGAddRunOp (run_info , script_op , err );
412+
413+ RAI_Model * model = (RAI_Model * )_getFromKeySpace (ctx , "imagenet_model1:{{1}}" , & key );
414+ RedisModule_CloseKey (key );
415+ RAI_DAGRunOp * model_op = RedisAI_DAGCreateModelRunOp (model );
416+ RedisAI_DAGRunOpAddInput (model_op , "tmp_key:{{1}}" );
417+ RedisAI_DAGRunOpAddOutput (model_op , "tmp_key2_0" );
418+ status = RedisAI_DAGAddRunOp (run_info , model_op , err );
419+ if (status != REDISMODULE_OK ) goto cleanup ;
420+
421+ model = (RAI_Model * )_getFromKeySpace (ctx , "imagenet_model2:{{1}}" , & key );
422+ RedisModule_CloseKey (key );
423+ model_op = RedisAI_DAGCreateModelRunOp (model );
424+ RedisAI_DAGRunOpAddInput (model_op , "tmp_key:{{1}}" );
425+ RedisAI_DAGRunOpAddOutput (model_op , "tmp_key2_1" );
426+ status = RedisAI_DAGAddRunOp (run_info , model_op , err );
427+ if (status != REDISMODULE_OK ) goto cleanup ;
428+
429+ script_op = RedisAI_DAGCreateScriptRunOp (script , "ensemble" );
430+ RedisAI_DAGRunOpAddInput (script_op , "tmp_key2_0" );
431+ RedisAI_DAGRunOpAddInput (script_op , "tmp_key2_1" );
432+ RedisAI_DAGRunOpAddOutput (script_op , "tmp_key_1:{{1}}" );
433+ RedisAI_DAGAddRunOp (run_info , script_op , err );
434+
435+ script_op = RedisAI_DAGCreateScriptRunOp (script , "post_process" );
436+ RedisAI_DAGRunOpAddInput (script_op , "tmp_key_1:{{1}}" );
437+ RedisAI_DAGRunOpAddOutput (script_op , "output:{{1}}" );
438+ RedisAI_DAGAddRunOp (run_info , script_op , err );
439+
440+ RedisAI_DAGAddTensorGet (run_info , "output:{{1}}" , err );
441+
442+ pthread_mutex_lock (& global_lock );
443+ if (RedisAI_DAGRun (run_info , _DAGFinishFunc , & results , err ) != REDISMODULE_OK ) {
444+ pthread_mutex_unlock (& global_lock );
445+ goto cleanup ;
446+ }
447+ // Wait until the onFinish callback returns.
448+ pthread_cond_wait (& global_cond , & global_lock );
449+ pthread_mutex_unlock (& global_lock );
450+
451+ // Verify that we received the expected output.
452+ RedisModule_Assert (array_len (results .outputs ) == 1 );
453+ RAI_Tensor * out_tensor = outputs [0 ];
454+ long long val ;
455+ if (RedisAI_TensorGetValueAsLongLong (out_tensor , 0 , & val ) != 0 ) goto cleanup ;
456+ RedisAI_TensorFree (out_tensor );
457+ if (0 <= val && val <= 1000 ) {
458+ test_res = "DAG resnet success" ;
459+ }
460+
461+ cleanup :
462+ RedisAI_FreeError (err );
463+ array_free (results .outputs );
464+ array_free (results .opsStatus );
465+ RedisAI_DAGFree (run_info );
466+ return RedisModule_ReplyWithSimpleString (ctx , test_res );
467+ }
0 commit comments