@@ -216,54 +216,61 @@ _helper_parse_tensor_attributes (JsonObject *tensor_obj, vsi_nn_tensor_attr_t *v
216216static int
217217_json_create_neural_network (vivante_handle_s *self)
218218{
219- g_autofree gchar *json_string = NULL ;
220- g_autoptr (GError) err = NULL ;
221- g_autoptr (JsonParser) parser = NULL ;
219+ const guint node_num = 1U ; /* single NBG node */
220+ const guint const_tensors_num = 0U ; /* * @todo support this */
221+
222+ gchar *json_string = NULL ;
223+ GError *err = NULL ;
224+ JsonParser *parser = NULL ;
222225 JsonNode *root_node = NULL ;
223226 JsonObject *root_obj = NULL ;
224227 JsonArray *input_array = NULL , *output_array = NULL ;
225228 guint input_tensors_num = 0 , output_tensors_num = 0 ;
229+ guint normal_tensors_num = 0 , virtual_tensors_num = 0 ;
226230 vsi_nn_node_t *node = NULL ;
231+ int ret = HAL_ML_ERROR_RUNTIME_ERROR ;
227232
228233 self->ctx = vsi_nn_CreateContext ();
229234 if (!self->ctx ) {
230235 g_critical (" [vivante] Failed to create VSI context." );
231- return HAL_ML_ERROR_RUNTIME_ERROR ;
236+ goto cleanup ;
232237 }
233238
234239 if (!g_file_get_contents (self->json_path , &json_string, NULL , &err)) {
235240 g_critical (" [vivante] Failed to read JSON file '%s': %s" , self->json_path ,
236241 err ? err->message : " Unknown error" );
237- return HAL_ML_ERROR_IO_ERROR ;
242+ ret = HAL_ML_ERROR_IO_ERROR ;
243+ goto cleanup;
238244 }
239245
240246 parser = json_parser_new ();
241247 if (!json_parser_load_from_data (parser, json_string, -1 , &err)) {
242248 g_critical (" [vivante] Failed to parse JSON: %s" , err ? err->message : " Unknown error" );
243- return HAL_ML_ERROR_INVALID_PARAMETER ;
249+ ret = HAL_ML_ERROR_INVALID_PARAMETER ;
250+ goto cleanup;
244251 }
245252
246253 root_node = json_parser_get_root (parser);
247254 if (!root_node || !JSON_NODE_HOLDS_OBJECT (root_node)) {
248255 g_critical (" [vivante] JSON root is not a valid object." );
249- return HAL_ML_ERROR_INVALID_PARAMETER ;
256+ ret = HAL_ML_ERROR_INVALID_PARAMETER ;
257+ goto cleanup;
250258 }
251259 root_obj = json_node_get_object (root_node);
252260
253261 input_array = json_object_get_array_member (root_obj, " input_tensors" );
254262 output_array = json_object_get_array_member (root_obj, " output_tensors" );
255263 if (!input_array || !output_array) {
256264 g_critical (" [vivante] JSON must contain 'input_tensors' and 'output_tensors' arrays." );
257- return HAL_ML_ERROR_INVALID_PARAMETER ;
265+ ret = HAL_ML_ERROR_INVALID_PARAMETER ;
266+ goto cleanup;
258267 }
259268
260269 input_tensors_num = json_array_get_length (input_array);
261270 output_tensors_num = json_array_get_length (output_array);
262271
263- const guint node_num = 1U ; /* single NBG node */
264- const guint const_tensors_num = 0U ; /* * @todo support this */
265- const guint normal_tensors_num = input_tensors_num + output_tensors_num;
266- const guint virtual_tensors_num = output_tensors_num;
272+ normal_tensors_num = input_tensors_num + output_tensors_num;
273+ virtual_tensors_num = output_tensors_num;
267274
268275 self->graph = vsi_nn_CreateGraph (self->ctx ,
269276 normal_tensors_num + virtual_tensors_num + const_tensors_num, node_num);
@@ -313,7 +320,7 @@ _json_create_neural_network (vivante_handle_s *self)
313320 }
314321
315322 for (guint i = 0 ; i < input_tensors_num; ++i) {
316- g_info (" [vivante] print Input tensor #%u: " , self->graph ->input .tensors [i]);
323+ g_info (" [vivante] Print input tensor #%u (%u): " , i , self->graph ->input .tensors [i]);
317324 vsi_nn_tensor_t *tensor
318325 = vsi_nn_GetTensor (self->graph , self->graph ->input .tensors [i]);
319326 vsi_nn_PrintTensor (tensor, self->graph ->input .tensors [i]);
@@ -344,7 +351,7 @@ _json_create_neural_network (vivante_handle_s *self)
344351 }
345352
346353 for (guint i = 0 ; i < output_tensors_num; ++i) {
347- g_info (" [vivante] print output tensor #%u: " , self->graph ->output .tensors [i]);
354+ g_info (" [vivante] Print output tensor #%u (%u): " , i , self->graph ->output .tensors [i]);
348355 vsi_nn_tensor_t *tensor
349356 = vsi_nn_GetTensor (self->graph , self->graph ->output .tensors [i]);
350357 vsi_nn_PrintTensor (tensor, self->graph ->output .tensors [i]);
@@ -356,11 +363,16 @@ _json_create_neural_network (vivante_handle_s *self)
356363 goto cleanup;
357364 }
358365
359- return HAL_ML_ERROR_NONE ;
366+ ret = HAL_ML_ERROR_NONE ;
360367
361368cleanup:
362- _json_release_neural_network (self);
363- return HAL_ML_ERROR_RUNTIME_ERROR ;
369+ if (ret != HAL_ML_ERROR_NONE )
370+ _json_release_neural_network (self);
371+
372+ g_clear_error (&err);
373+ g_clear_pointer (&json_string, g_free);
374+ g_clear_object (&parser);
375+ return ret;
364376}
365377
366378/* * @brief Releases all resources associated with a JSON-based graph. */
@@ -424,35 +436,23 @@ _so_create_neural_network (vivante_handle_s *self)
424436 return HAL_ML_ERROR_NONE ;
425437}
426438
427- /* ===================================================================
428- * Main HAL Implementation Functions
429- * ===================================================================
430- */
431- static int
432- ml_vivante_init (void **backend_private)
439+ /* * @brief Initialize handle. */
440+ static void
441+ _init_vivante_handle (vivante_handle_s *vivante)
433442{
434- vivante_handle_s * vivante = g_new0 (vivante_handle_s, 1 );
443+ memset ( vivante, 0 , sizeof (vivante_handle_s) );
435444
436445 gst_tensors_info_init (&vivante->inputInfo );
437446 gst_tensors_info_init (&vivante->outputInfo );
438447
439448 vivante->use_json_for_graph = TRUE ;
440449 vivante->has_post_process = FALSE ;
441-
442- *backend_private = vivante;
443- return HAL_ML_ERROR_NONE ;
444450}
445451
446- static int
447- ml_vivante_deinit (void *backend_private)
452+ /* * @brief Close model and clear internal data in handle. */
453+ static void
454+ _clear_vivante_handle (vivante_handle_s *vivante)
448455{
449- vivante_handle_s *vivante = (vivante_handle_s *) backend_private;
450-
451- if (!vivante) {
452- g_critical (" [vivante] invalid backend_private" );
453- return HAL_ML_ERROR_INVALID_PARAMETER ;
454- }
455-
456456 if (vivante->use_json_for_graph ) {
457457 _json_release_neural_network (vivante);
458458 } else {
@@ -470,6 +470,36 @@ ml_vivante_deinit (void *backend_private)
470470 g_free (vivante->model_path );
471471 g_free (vivante->json_path );
472472 g_free (vivante->so_path );
473+
474+ _init_vivante_handle (vivante);
475+ }
476+
477+ /* ===================================================================
478+ * Main HAL Implementation Functions
479+ * ===================================================================
480+ */
481+ static int
482+ ml_vivante_init (void **backend_private)
483+ {
484+ vivante_handle_s *vivante = g_new0 (vivante_handle_s, 1 );
485+
486+ _init_vivante_handle (vivante);
487+
488+ *backend_private = vivante;
489+ return HAL_ML_ERROR_NONE ;
490+ }
491+
492+ static int
493+ ml_vivante_deinit (void *backend_private)
494+ {
495+ vivante_handle_s *vivante = (vivante_handle_s *) backend_private;
496+
497+ if (!vivante) {
498+ g_critical (" [vivante] invalid backend_private" );
499+ return HAL_ML_ERROR_INVALID_PARAMETER ;
500+ }
501+
502+ _clear_vivante_handle (vivante);
473503 g_free (vivante);
474504
475505 return HAL_ML_ERROR_NONE ;
@@ -486,6 +516,11 @@ ml_vivante_configure_instance (void *backend_private, const void *prop_)
486516 return HAL_ML_ERROR_INVALID_PARAMETER ;
487517 }
488518
519+ if (vivante->model_path ) {
520+ g_critical (" [vivante] invalid state, clear old data." );
521+ _clear_vivante_handle (vivante);
522+ }
523+
489524 vivante->model_path = g_strdup (prop->model_files [0 ]);
490525 // Default loading strategy: if more than one model file is given, assume .so loading.
491526 if (prop->num_models > 1 ) {
0 commit comments