[Common] dimension in tensor-meta
authorJaeyun Jung <jy1210.jung@samsung.com>
Thu, 7 Sep 2023 08:45:24 +0000 (17:45 +0900)
committerjaeyun-jung <39614140+jaeyun-jung@users.noreply.github.com>
Tue, 12 Sep 2023 02:42:32 +0000 (11:42 +0900)
Set max length of tensor dimension in tensor-meta, same with tensor rank limit.

Signed-off-by: Jaeyun Jung <jy1210.jung@samsung.com>
gst/nnstreamer/include/tensor_typedef.h
gst/nnstreamer/nnstreamer_plugin_api_util_impl.c
tests/common/unittest_common.cc

index e753bba..1ce48ba 100644 (file)
 #define NNS_TENSOR_SIZE_EXTRA_LIMIT (240)
 #define NNS_TENSOR_DIM_NULL ({0, 0, 0, 0})
 
-/**
- * @brief The maximum rank in meta info (see GstTensorMetaInfo).
- * This RANK is applied to meta info of other/tensors-flexible only and
- * does not affect other/tensors(s)'s NNS_TENSOR_RANK_LIMIT.
- */
-#define NNS_TENSOR_META_RANK_LIMIT     (16)
-
 #define NNS_MIMETYPE_TENSOR "other/tensor"
 #define NNS_MIMETYPE_TENSORS "other/tensors"
 
@@ -291,7 +284,7 @@ typedef struct
   uint32_t magic;
   uint32_t version;
   uint32_t type;
-  uint32_t dimension[NNS_TENSOR_META_RANK_LIMIT];
+  tensor_dim dimension;
   uint32_t format;
   uint32_t media_type;
 
index d9aa7cb..9715e74 100644 (file)
@@ -1419,8 +1419,6 @@ gst_tensor_meta_info_get_version (GstTensorMetaInfo * meta,
 gboolean
 gst_tensor_meta_info_validate (GstTensorMetaInfo * meta)
 {
-  guint i;
-
   g_return_val_if_fail (meta != NULL, FALSE);
 
   if (!GST_TENSOR_META_IS_VALID (meta))
@@ -1432,17 +1430,12 @@ gst_tensor_meta_info_validate (GstTensorMetaInfo * meta)
     return FALSE;
   }
 
-  for (i = 0; i < NNS_TENSOR_META_RANK_LIMIT; i++) {
-    if (meta->dimension[i] == 0) {
-      if (i == 0) {
-        gchar *dim_str = gst_tensor_get_dimension_string (meta->dimension);
-        nns_logd ("Failed to validate tensor meta info. Given dimension: %s",
-            dim_str);
-        g_free (dim_str);
-        return FALSE;
-      }
-      break;
-    }
+  if (!gst_tensor_dimension_is_valid (meta->dimension)) {
+    gchar *dim_str = gst_tensor_get_dimension_string (meta->dimension);
+    nns_logd ("Failed to validate tensor meta info. Given dimension: %s",
+        dim_str);
+    g_free (dim_str);
+    return FALSE;
   }
 
   if (meta->format >= _NNS_TENSOR_FORMAT_END) {
@@ -1489,7 +1482,6 @@ gst_tensor_meta_info_get_header_size (GstTensorMetaInfo * meta)
 gsize
 gst_tensor_meta_info_get_data_size (GstTensorMetaInfo * meta)
 {
-  guint i;
   gsize dsize;
 
   g_return_val_if_fail (meta != NULL, 0);
@@ -1503,14 +1495,9 @@ gst_tensor_meta_info_get_data_size (GstTensorMetaInfo * meta)
     return meta->sparse_info.nnz * (dsize + sizeof (guint));
   }
 
-  for (i = 0; i < NNS_TENSOR_META_RANK_LIMIT; i++) {
-    if (meta->dimension[i] == 0)
-      break;
+  dsize *= gst_tensor_get_element_count (meta->dimension);
 
-    dsize *= meta->dimension[i];
-  }
-
-  return (i > 0) ? dsize : 0;
+  return dsize;
 }
 
 /**
@@ -1555,8 +1542,7 @@ gst_tensor_meta_info_parse_header (GstTensorMetaInfo * meta, gpointer header)
   meta->magic = val[0];
   meta->version = val[1];
   meta->type = val[2];
-  memcpy (meta->dimension, &val[3],
-      sizeof (uint32_t) * NNS_TENSOR_META_RANK_LIMIT);
+  memcpy (meta->dimension, &val[3], sizeof (uint32_t) * NNS_TENSOR_RANK_LIMIT);
   meta->format = val[19];
   meta->media_type = val[20];
 
@@ -1590,22 +1576,8 @@ gst_tensor_meta_info_convert (GstTensorMetaInfo * meta, GstTensorInfo * info)
 
   info->type = meta->type;
 
-  for (i = 0; i < NNS_TENSOR_META_RANK_LIMIT; i++) {    /* lgtm[cpp/constant-comparison] */
-    if (i >= NNS_TENSOR_RANK_LIMIT) {
-      if (meta->dimension[i] > 0) {
-        nns_loge ("Given meta has invalid dimension (dimension[%u] %u).",
-            i, meta->dimension[i]);
-        nns_loge ("Failed to set info, max rank should be %u.",
-            NNS_TENSOR_RANK_LIMIT);
-        return FALSE;
-      }
-
-      /* tensor-info max rank is NNS_TENSOR_RANK_LIMIT */
-      break;
-    }
-
+  for (i = 0; i < NNS_TENSOR_RANK_LIMIT; i++)
     info->dimension[i] = meta->dimension[i];
-  }
 
   return TRUE;
 }
index ede2420..70af922 100644 (file)
@@ -1310,7 +1310,7 @@ TEST (commonMetaInfo, initDefaultValue)
   EXPECT_EQ (meta.type, _NNS_END);
   EXPECT_EQ (meta.format, _NNS_TENSOR_FORMAT_STATIC);
   EXPECT_EQ ((media_type) meta.media_type, _NNS_TENSOR);
-  for (i = 0; i < NNS_TENSOR_META_RANK_LIMIT; i++)
+  for (i = 0; i < NNS_TENSOR_RANK_LIMIT; i++)
     EXPECT_EQ (meta.dimension[i], 0U);
 
   /* current version after init */