@@ -106,6 +106,8 @@ ResultCode ANeuralNetworksExecution_setInput(
106106 BACKEND_CALL_RET (ret, imgdnnBindingAddInput, execution->imgdnn_binding_ ,
107107 execution->imgdnn_inputs_ [uindex], img_memory);
108108 IMGDNN_RETURN_ERR_IF_ERROR (ret);
109+ // Store the memory objects to free them after the execution
110+ execution->imgdnn_memories_ .push_back (img_memory);
109111 }
110112 return ANEURALNETWORKS_NO_ERROR;
111113}
@@ -149,7 +151,9 @@ ResultCode ANeuralNetworksExecution_setOutput(
149151 BACKEND_CALL_RET (ret, imgdnnBindingAddOutput, execution->imgdnn_binding_ ,
150152 execution->imgdnn_outputs_ [uindex], img_memory);
151153 IMGDNN_RETURN_ERR_IF_ERROR (ret);
152- // Store these memory objects to be able to lock them later
154+ // Store the memory objects to free them after the execution
155+ execution->imgdnn_memories_ .push_back (img_memory);
156+ // Store the memory objects to be able to lock them later
153157 execution->host_output_memories .emplace_back (data, img_memory);
154158 }
155159 return ANEURALNETWORKS_NO_ERROR;
@@ -285,9 +289,12 @@ ResultCode ANeuralNetworksExecution_compute(
285289 * @param ret error code
286290 */
287291inline void interopCheckImgdnnErr (imgdnn_err_code ret) {
292+ TENSOROPT_UNUSED_VARIABLE (ret);
293+ #ifdef VERBOSE_LOG
288294 if (ret != IMGDNN_SUCCESS) {
289- printf (" Error: IMGDNN execution failed with code %d " , ret);
295+ VLOG_AT (" Error: IMGDNN execution failed with code " << ret);
290296 }
297+ #endif
291298}
292299
293300/* *
@@ -330,35 +337,48 @@ ResultCode ANeuralNetworksExecution_startCompute(
330337 .get_access <cl::sycl::access::mode::write>(cgh));
331338 }
332339 cgh.interop_task ([execution](const cl::sycl::codeplay::interop_handle& h) {
340+ // imgdnn_memories objects need to be copied locally so that the next
341+ // execution is not blocked
342+ std::vector<imgdnn_memory> task_memories;
343+ task_memories.reserve (execution->imgdnn_memories_ .size () +
344+ execution->input_indexed_accessors .size () +
345+ execution->output_indexed_accessors .size ());
346+ task_memories = execution->imgdnn_memories_ ;
347+ execution->imgdnn_memories_ .clear ();
348+ imgdnn_err_code ret;
333349 // Bind inputs
334350 for (const auto & acc_pair : execution->input_indexed_accessors ) {
335351 imgdnn_memory img_memory =
336352 importImgMemory (execution, acc_pair.second , h);
337- imgdnn_err_code ret;
338353 BACKEND_CALL_RET (ret, imgdnnBindingAddInput, execution->imgdnn_binding_ ,
339354 execution->imgdnn_inputs_ [acc_pair.first ], img_memory);
340355 interopCheckImgdnnErr (ret);
356+ task_memories.push_back (img_memory);
341357 }
342358
343359 // Bind outputs
344360 for (const auto & acc_pair : execution->output_indexed_accessors ) {
345361 imgdnn_memory img_memory =
346362 importImgMemory (execution, acc_pair.second , h);
347- imgdnn_err_code ret;
348363 BACKEND_CALL_RET (
349364 ret, imgdnnBindingAddOutput, execution->imgdnn_binding_ ,
350365 execution->imgdnn_outputs_ [acc_pair.first ], img_memory);
351366 interopCheckImgdnnErr (ret);
367+ task_memories.push_back (img_memory);
352368 }
353369 execution->identified_memory_lock .unlock ();
354370
355- // Cannot use BACKEND_CALL_RET here as the macro uses std::cout.
356371 // The IMGDNN execution is made blocking so that the returned
357372 // SYCL event represents the execution of the whole graph.
358- auto ret = imgdnnNetworkObjectExecute (execution-> imgdnn_network_object_ ,
359- execution->imgdnn_binding_ , true , 0 ,
360- nullptr , nullptr );
373+ BACKEND_CALL_RET ( ret, imgdnnNetworkObjectExecute,
374+ execution->imgdnn_network_object_ ,
375+ execution-> imgdnn_binding_ , true , 0 , nullptr , nullptr );
361376 interopCheckImgdnnErr (ret);
377+
378+ for (auto img_mem : task_memories) {
379+ BACKEND_CALL_RET (ret, imgdnnMemoryDestroy, img_mem);
380+ interopCheckImgdnnErr (ret);
381+ }
362382 });
363383 });
364384 execution->dimensions .clear ();
0 commit comments