[Layout] Skip layout conversion for raw data layout
authorDongju Chae <dongju.chae@samsung.com>
Thu, 5 Aug 2021 01:12:04 +0000 (10:12 +0900)
committer채동주/On-Device Lab(SR)/Staff Engineer/삼성전자 <dongju.chae@samsung.com>
Thu, 5 Aug 2021 05:39:07 +0000 (14:39 +0900)
This patch makes the data converter skip layout conversion
for raw data layout.

Signed-off-by: Dongju Chae <dongju.chae@samsung.com>
src/core/ne-data.cc
src/core/ne-data.h
src/core/ne-model.cc

index 95f543f..1082fcb 100644 (file)
@@ -212,6 +212,10 @@ DataConverter::checkCapability () {
   if (src_layout_ == dst_layout_)
     return true;
 
+  /* raw layout */
+  if (src_layout_ == DATA_LAYOUT_RAW || dst_layout_ == DATA_LAYOUT_RAW)
+    return true;
+
   /* standard -> trinity */
   if ((src_layout_ == DATA_LAYOUT_NHWC || src_layout_ == DATA_LAYOUT_NCHW) &&
       (dst_layout_ == DATA_LAYOUT_TRIV || dst_layout_ == DATA_LAYOUT_TRIV2))
@@ -226,6 +230,22 @@ DataConverter::checkCapability () {
 }
 
 /**
+ * @brief check layout conversion is required
+ * @return true if required. otherwise false
+ */
+bool
+DataConverter::needLayoutConversion () {
+  /* don't care about DATA_LAYOUT_RAW */
+  if (to_npu_ && dst_layout_ == DATA_LAYOUT_RAW)
+    return false;
+
+  if (!to_npu_ && src_layout_ == DATA_LAYOUT_RAW)
+    return false;
+
+  return src_layout_ != dst_layout_;
+}
+
+/**
  * @brief convert data format, copying the converted source data to destination.
  * @return the number of bytes copied
  */
@@ -233,42 +253,42 @@ size_t
 DataConverter::perform () {
   /* check parameters first */
   if (!src_ || !dst_ || size_ == 0 || !dims_) {
-    logwarn (TAG, "Skip data conversion: Invalid parameter detected");
+    logerr (TAG, "Invalid parameter detected");
     return 0;
   }
 
   if (!checkCapability ()) {
-    logwarn (TAG, "Skip data conversion: Unsupported layout detected");
+    logerr (TAG, "Unable to pass capability check");
     return 0;
   }
 
-  /* trinity device assumes the NHWC-based layout */
-  uint32_t batch = dims_[0];
-  uint32_t height = dims_[1];
-  uint32_t width = dims_[2];
-  uint32_t depth = dims_[3];
+  DECLARE_QUANTIZER (src_type_, dst_type_);
 
-  uint32_t src_data_size = get_data_size (src_type_);
-  uint32_t dst_data_size = get_data_size (dst_type_);
-  uint32_t npu_data_size;
-  if (to_npu_)
-    npu_data_size = dst_data_size;
-  else
-    npu_data_size = src_data_size;
+  if (needLayoutConversion ()) {
+    /* trinity device assumes the NHWC-based layout */
+    uint32_t batch = dims_[0];
+    uint32_t height = dims_[1];
+    uint32_t width = dims_[2];
+    uint32_t depth = dims_[3];
 
-  uint32_t granularity = DATA_GRANULARITY;
-  uint32_t granularity_div = 1;
+    uint32_t src_data_size = get_data_size (src_type_);
+    uint32_t dst_data_size = get_data_size (dst_type_);
+    uint32_t npu_data_size;
+    if (to_npu_)
+      npu_data_size = dst_data_size;
+    else
+      npu_data_size = src_data_size;
 
-  if (npu_tops_ == 2)
-    granularity_div *= 2;
-  if (npu_data_size == 2)
-    granularity_div *= 2;
+    uint32_t granularity = DATA_GRANULARITY;
+    uint32_t granularity_div = 1;
 
-  granularity /= granularity_div;
+    if (npu_tops_ == 2)
+      granularity_div *= 2;
+    if (npu_data_size == 2)
+      granularity_div *= 2;
 
-  DECLARE_QUANTIZER (src_type_, dst_type_);
+    granularity /= granularity_div;
 
-  if (src_layout_ != dst_layout_) {
     uint32_t MPA_L = granularity;
     uint32_t src_offset;
     uint32_t dst_offset;
index 594ac5d..b3c4576 100644 (file)
@@ -36,6 +36,7 @@ class DataConverter {
 
   size_t perform ();
   bool checkCapability ();
+  bool needLayoutConversion ();
 
   void setData (void *src, void *dst, size_t size) {
     src_ = src;
index b9af613..3b260aa 100644 (file)
@@ -413,10 +413,14 @@ void
 Model::updateDataInfo () {
   for (uint32_t idx = 0; idx < in_.num_info; idx++) {
     if (in_.info[idx].layout == DATA_LAYOUT_MODEL) {
-      if (meta_->getVersion () < 3)
+      if (meta_->getVersion () >= 3) {
+        if (meta_->getInputSegmentLayout (idx) != DATA_LAYOUT_NONE)
+          in_.info[idx].layout = meta_->getInputSegmentLayout (idx);
+        else
+          in_.info[idx].layout = DATA_LAYOUT_TRIV2;
+      } else {
         in_.info[idx].layout = DATA_LAYOUT_TRIV;
-      else
-        in_.info[idx].layout = DATA_LAYOUT_TRIV2;
+      }
     }
     if (in_.info[idx].type == DATA_TYPE_MODEL)
       in_.info[idx].type = meta_->getInputQuantType (idx);
@@ -424,10 +428,14 @@ Model::updateDataInfo () {
 
   for (uint32_t idx = 0; idx < out_.num_info; idx++) {
     if (out_.info[idx].layout == DATA_LAYOUT_MODEL) {
-      if (meta_->getVersion () < 3)
+      if (meta_->getVersion () >= 3) {
+        if (meta_->getOutputSegmentLayout (idx) != DATA_LAYOUT_NONE)
+          out_.info[idx].layout = meta_->getOutputSegmentLayout (idx);
+        else
+          out_.info[idx].layout = DATA_LAYOUT_TRIV2;
+      } else {
         out_.info[idx].layout = DATA_LAYOUT_TRIV;
-      else
-        out_.info[idx].layout = DATA_LAYOUT_TRIV2;
+      }
     }
     if (out_.info[idx].type == DATA_TYPE_MODEL)
       out_.info[idx].type = meta_->getOutputQuantType (idx);
@@ -617,10 +625,14 @@ Model::finalize () {
   int version = getMetadata ()->getVersion ();
   for (uint32_t idx = 0; idx < input_num; idx++) {
     if (in_.info[idx].layout == DATA_LAYOUT_MODEL) {
-      if (version >= 3)
-        in_.info[idx].layout = DATA_LAYOUT_TRIV2;
-      else
-        in_.info[idx].layout = DATA_LAYOUT_SRNPU;
+      if (version >= 3) {
+        if (meta_->getInputSegmentLayout (idx) != DATA_LAYOUT_NONE)
+          in_.info[idx].layout = meta_->getInputSegmentLayout (idx);
+        else
+          in_.info[idx].layout = DATA_LAYOUT_TRIV2;
+      } else {
+        in_.info[idx].layout = DATA_LAYOUT_TRIV;
+      }
     }
     if (in_.info[idx].type == DATA_TYPE_MODEL) {
       if (version >= 3)
@@ -632,10 +644,14 @@ Model::finalize () {
 
   for (uint32_t idx = 0; idx < output_num; idx++) {
     if (out_.info[idx].layout == DATA_LAYOUT_MODEL) {
-      if (version >= 3)
-        out_.info[idx].layout = DATA_LAYOUT_TRIV2;
-      else
-        out_.info[idx].layout = DATA_LAYOUT_SRNPU;
+      if (version >= 3) {
+        if (meta_->getOutputSegmentLayout (idx) != DATA_LAYOUT_NONE)
+          out_.info[idx].layout = meta_->getOutputSegmentLayout (idx);
+        else
+          out_.info[idx].layout = DATA_LAYOUT_TRIV2;
+      } else {
+        out_.info[idx].layout = DATA_LAYOUT_TRIV;
+      }
     }
     if (out_.info[idx].type == DATA_TYPE_MODEL) {
       if (version >= 3)