[Common/CodeClean] struct for tensor memories
authorJaeyun Jung <jy1210.jung@samsung.com>
Mon, 20 Nov 2023 12:59:27 +0000 (21:59 +0900)
committerjaeyun-jung <39614140+jaeyun-jung@users.noreply.github.com>
Tue, 21 Nov 2023 10:27:59 +0000 (19:27 +0900)
Code clean, use GstTensorMemory struct defined in nnstreamer.

Signed-off-by: Jaeyun Jung <jy1210.jung@samsung.com>
c/src/ml-api-common.c
c/src/ml-api-inference-pipeline.c
c/src/ml-api-inference-single.c
c/src/ml-api-internal.h
c/src/ml-api-service-query-client.c
java/android/nnstreamer/src/main/jni/nnstreamer-native-api.c

index 34a1730..71a8ad3 100644 (file)
@@ -953,9 +953,9 @@ _ml_tensors_data_destroy_internal (ml_tensors_data_h data, gboolean free_data)
             status);
     } else {
       for (i = 0; i < ML_TENSOR_SIZE_LIMIT; i++) {
-        if (_data->tensors[i].tensor) {
-          g_free (_data->tensors[i].tensor);
-          _data->tensors[i].tensor = NULL;
+        if (_data->tensors[i].data) {
+          g_free (_data->tensors[i].data);
+          _data->tensors[i].data = NULL;
         }
       }
     }
@@ -1028,7 +1028,7 @@ _ml_tensors_data_create_no_alloc (const ml_tensors_info_h info,
       ml_tensor_info_s *_tensor_info = ml_tensors_info_get_nth_info (_info, i);
       _data->tensors[i].size =
           _ml_tensor_info_get_size (_tensor_info, _info->is_extended);
-      _data->tensors[i].tensor = NULL;
+      _data->tensors[i].data = NULL;
     }
     G_UNLOCK_UNLESS_NOLOCK (*_info);
   }
@@ -1068,7 +1068,7 @@ _ml_tensors_data_clone_no_alloc (const ml_tensors_data_s * data_src,
 
   _data->num_tensors = data_src->num_tensors;
   memcpy (_data->tensors, data_src->tensors,
-      sizeof (ml_tensor_data_s) * data_src->num_tensors);
+      sizeof (GstTensorMemory) * data_src->num_tensors);
 
   *data = _data;
   G_UNLOCK_UNLESS_NOLOCK (*_data);
@@ -1107,8 +1107,7 @@ ml_tensors_data_clone (const ml_tensors_data_h in, ml_tensors_data_h * out)
   _out = (ml_tensors_data_s *) (*out);
 
   for (i = 0; i < _out->num_tensors; ++i) {
-    memcpy (_out->tensors[i].tensor, _in->tensors[i].tensor,
-        _in->tensors[i].size);
+    memcpy (_out->tensors[i].data, _in->tensors[i].data, _in->tensors[i].size);
   }
 
 error:
@@ -1154,8 +1153,8 @@ ml_tensors_data_create (const ml_tensors_info_h info, ml_tensors_data_h * data)
   }
 
   for (i = 0; i < _data->num_tensors; i++) {
-    _data->tensors[i].tensor = g_malloc0 (_data->tensors[i].size);
-    if (_data->tensors[i].tensor == NULL) {
+    _data->tensors[i].data = g_malloc0 (_data->tensors[i].size);
+    if (_data->tensors[i].data == NULL) {
       goto failed_oom;
     }
   }
@@ -1203,7 +1202,7 @@ ml_tensors_data_get_tensor_data (ml_tensors_data_h data, unsigned int index,
     goto report;
   }
 
-  *raw_data = _data->tensors[index].tensor;
+  *raw_data = _data->tensors[index].data;
   *data_size = _data->tensors[index].size;
 
 report:
@@ -1250,8 +1249,8 @@ ml_tensors_data_set_tensor_data (ml_tensors_data_h data, unsigned int index,
     goto report;
   }
 
-  if (_data->tensors[index].tensor != raw_data)
-    memcpy (_data->tensors[index].tensor, raw_data, data_size);
+  if (_data->tensors[index].data != raw_data)
+    memcpy (_data->tensors[index].data, raw_data, data_size);
 
 report:
   G_UNLOCK_UNLESS_NOLOCK (*_data);
index c8b5c72..8a02fb6 100644 (file)
@@ -325,7 +325,7 @@ cb_sink_event (GstElement * e, GstBuffer * b, gpointer user_data)
       goto error;
     }
 
-    _data->tensors[i].tensor = map[i].data;
+    _data->tensors[i].data = map[i].data;
     _data->tensors[i].size = map[i].size;
   }
 
@@ -374,7 +374,7 @@ cb_sink_event (GstElement * e, GstBuffer * b, gpointer user_data)
       gst_tensor_meta_info_convert (&meta,
           gst_tensors_info_get_nth_info (&gst_info, i));
 
-      _data->tensors[i].tensor = map[i].data + hsize;
+      _data->tensors[i].data = map[i].data + hsize;
       _data->tensors[i].size = map[i].size - hsize;
     }
   } else {
@@ -1715,7 +1715,7 @@ ml_pipeline_src_input_data (ml_pipeline_src_h h, ml_tensors_data_h data,
   for (i = 0; i < _data->num_tensors; i++) {
     GstTensorInfo *_gst_tensor_info =
         gst_tensors_info_get_nth_info (&gst_info, i);
-    mem_data = _data->tensors[i].tensor;
+    mem_data = _data->tensors[i].data;
     mem_size = _data->tensors[i].size;
 
     mem = tmp = gst_memory_new_wrapped (GST_MEMORY_FLAG_READONLY,
@@ -2800,7 +2800,7 @@ ml_pipeline_custom_invoke (void *data, const GstTensorFilterProperties * prop,
 
   _data = (ml_tensors_data_s *) in_data;
   for (i = 0; i < _data->num_tensors; i++)
-    _data->tensors[i].tensor = in[i].data;
+    _data->tensors[i].data = in[i].data;
 
   status = _ml_tensors_data_create_no_alloc (c->out_info, &out_data);
   if (status != ML_ERROR_NONE) {
@@ -2810,7 +2810,7 @@ ml_pipeline_custom_invoke (void *data, const GstTensorFilterProperties * prop,
 
   _data = (ml_tensors_data_s *) out_data;
   for (i = 0; i < _data->num_tensors; i++)
-    _data->tensors[i].tensor = out[i].data;
+    _data->tensors[i].data = out[i].data;
 
   /* call invoke callback */
   status = c->cb (in_data, out_data, c->pdata);
@@ -3040,7 +3040,7 @@ ml_pipeline_if_custom (const GstTensorsInfo * info,
 
   _data = (ml_tensors_data_s *) in_data;
   for (i = 0; i < _data->num_tensors; i++)
-    _data->tensors[i].tensor = input[i].data;
+    _data->tensors[i].data = input[i].data;
 
   /* call invoke callback */
   g_mutex_lock (&c->lock);
index 3cfefe7..2ccef73 100644 (file)
@@ -289,7 +289,7 @@ __setup_in_out_tensors (ml_single * single_h)
   in_tensors->num_tensors = single_h->in_info.num_tensors;
   for (i = 0; i < in_tensors->num_tensors; i++) {
     /** memory will be allocated by tensor_filter_single */
-    in_tensors->tensors[i].tensor = NULL;
+    in_tensors->tensors[i].data = NULL;
     in_tensors->tensors[i].size =
         gst_tensors_info_get_size (&single_h->in_info, i);
   }
@@ -301,7 +301,7 @@ __setup_in_out_tensors (ml_single * single_h)
   out_tensors->num_tensors = single_h->out_info.num_tensors;
   for (i = 0; i < out_tensors->num_tensors; i++) {
     /** memory will be allocated by tensor_filter_single */
-    out_tensors->tensors[i].tensor = NULL;
+    out_tensors->tensors[i].data = NULL;
     out_tensors->tensors[i].size =
         gst_tensors_info_get_size (&single_h->out_info, i);
   }
@@ -321,8 +321,7 @@ __destroy_notify (gpointer data_h, gpointer single_data)
 
   if (G_LIKELY (single_h->filter)) {
     if (single_h->klass->allocate_in_invoke (single_h->filter)) {
-      single_h->klass->destroy_notify (single_h->filter,
-          (GstTensorMemory *) data->tensors);
+      single_h->klass->destroy_notify (single_h->filter, data->tensors);
     }
   }
 
@@ -397,7 +396,6 @@ __invoke (ml_single * single_h, ml_tensors_data_h in, ml_tensors_data_h out)
 {
   ml_tensors_data_s *in_data, *out_data;
   int status = ML_ERROR_NONE;
-  GstTensorMemory *in_tensors, *out_tensors;
 
   in_data = (ml_tensors_data_s *) in;
   out_data = (ml_tensors_data_s *) out;
@@ -408,12 +406,9 @@ __invoke (ml_single * single_h, ml_tensors_data_h in, ml_tensors_data_h out)
     return ML_ERROR_STREAMS_PIPE;
   }
 
-  in_tensors = (GstTensorMemory *) in_data->tensors;
-  out_tensors = (GstTensorMemory *) out_data->tensors;
-
   /* Invoke the thread. */
-  if (!single_h->klass->invoke (single_h->filter, in_tensors, out_tensors,
-          single_h->free_output)) {
+  if (!single_h->klass->invoke (single_h->filter, in_data->tensors,
+          out_data->tensors, single_h->free_output)) {
     const char *fw_name = _ml_get_nnfw_subplugin_name (single_h->nnfw);
     _ml_error_report
         ("Failed to invoke the tensors. The invoke callback of the tensor-filter subplugin '%s' has failed. Please contact the author of tensor-filter-%s (nnstreamer-%s) or review its source code. Note that this usually happens when the designated framework does not support the given model (e.g., trying to run tf-lite 2.6 model with tf-lite 1.13).",
@@ -1303,7 +1298,7 @@ _ml_single_invoke_validate_data (ml_single_h single,
         _model->num_tensors);
 
   for (i = 0; i < _data->num_tensors; i++) {
-    if (G_UNLIKELY (!_data->tensors[i].tensor))
+    if (G_UNLIKELY (!_data->tensors[i].data))
       _ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
           "The %d-th input tensor is not valid. There is no valid dimension metadata for this tensor.",
           i);
index 9d2a7f9..91605b4 100644 (file)
@@ -15,6 +15,7 @@
 
 #include <glib.h>
 #include <ml-api-common.h>
+#include <tensor_typedef.h>
 
 #ifdef __cplusplus
 extern "C" {
@@ -226,21 +227,12 @@ typedef struct {
 typedef int (*ml_handle_destroy_cb) (void *handle, void *user_data);
 
 /**
- * @brief An instance of a single input or output frame.
- * @since_tizen 5.5
- */
-typedef struct {
-  void *tensor; /**< The instance of tensor data. */
-  size_t size; /**< The size of tensor. */
-} ml_tensor_data_s;
-
-/**
  * @brief An instance of input or output frames. #ml_tensors_info_h is the handle for tensors metadata.
  * @since_tizen 5.5
  */
 typedef struct {
   unsigned int num_tensors; /**< The number of tensors. */
-  ml_tensor_data_s tensors[ML_TENSOR_SIZE_LIMIT]; /**< The list of tensor data. NULL for unused tensors. */
+  GstTensorMemory tensors[ML_TENSOR_SIZE_LIMIT]; /**< The list of tensor data. NULL for unused tensors. */
 
   /* private */
   ml_tensors_info_h info;
index 634a44b..aecbbad 100644 (file)
@@ -65,7 +65,7 @@ _sink_callback_for_query_client (const ml_tensors_data_h data,
   _copied_data_s = (ml_tensors_data_s *) copied_data;
 
   for (i = 0; i < count; ++i) {
-    memcpy (_copied_data_s->tensors[i].tensor, data_s->tensors[i].tensor,
+    memcpy (_copied_data_s->tensors[i].data, data_s->tensors[i].data,
         data_s->tensors[i].size);
   }
 
index 99727a6..a46aefe 100644 (file)
@@ -470,7 +470,7 @@ nns_convert_tensors_data (pipeline_info_s * pipe_info, JNIEnv * env,
     jobject tensor = (*env)->GetObjectArrayElement (env, data_arr, i);
     gpointer data_ptr = (*env)->GetDirectBufferAddress (env, tensor);
 
-    memcpy (data_ptr, data->tensors[i].tensor, data->tensors[i].size);
+    memcpy (data_ptr, data->tensors[i].data, data->tensors[i].size);
     (*env)->DeleteLocalRef (env, tensor);
   }
 
@@ -542,12 +542,12 @@ nns_parse_tensors_data (pipeline_info_s * pipe_info, JNIEnv * env,
       gpointer data_ptr = (*env)->GetDirectBufferAddress (env, tensor);
 
       if (clone) {
-        if (data->tensors[i].tensor == NULL)
-          data->tensors[i].tensor = g_malloc (data_size);
+        if (data->tensors[i].data == NULL)
+          data->tensors[i].data = g_malloc (data_size);
 
-        memcpy (data->tensors[i].tensor, data_ptr, data_size);
+        memcpy (data->tensors[i].data, data_ptr, data_size);
       } else {
-        data->tensors[i].tensor = data_ptr;
+        data->tensors[i].data = data_ptr;
       }
 
       data->tensors[i].size = data_size;