Apply Coordinates for sinking and sourcing (#5398)
author장지섭/On-Device Lab(SR)/Engineer/삼성전자 <jiseob.jang@samsung.com>
Fri, 14 Jun 2019 00:53:14 +0000 (09:53 +0900)
committer오형석/On-Device Lab(SR)/Staff Engineer/삼성전자 <hseok82.oh@samsung.com>
Fri, 14 Jun 2019 00:53:14 +0000 (09:53 +0900)
This commit applies Coordinates for sinking and sourcing.

Signed-off-by: jiseob.jang <jiseob.jang@samsung.com>
runtimes/neurun/core/src/exec/Sink.h
runtimes/neurun/core/src/exec/Source.h

index 9951bfe..a105260 100644 (file)
@@ -23,6 +23,7 @@
 #include "graph/operand/Layout.h"
 #include "util/feature/nhwc/View.h"
 #include "util/feature/nchw/View.h"
+#include "util/Utils.h"
 #include <misc/feature/IndexIterator.h>
 
 namespace neurun
@@ -69,35 +70,35 @@ public:
       }
       case 2:
       {
-        auto matrix_shape = _shape.asMatrix();
+        const int32_t copy_len = _shape.dim(1);
 
-        for (auto h = 0; h < matrix_shape.H; ++h)
+        for (auto i = 0; i < _shape.dim(0); ++i)
         {
-          neurun::util::feature::Coordinate4D coord{0, h, 0, 0};
-          memcpy(_output_buffer + h * matrix_shape.W, input_buffer + tensor.calcOffset(coord),
-                 matrix_shape.W * sizeof(T));
+          neurun::util::Coordinates coords{i, 0};
+          memcpy(_output_buffer + i * copy_len, input_buffer + tensor.calcOffset(coords),
+                 copy_len * sizeof(T));
         }
         break;
       }
       case 3:
       {
-        const int32_t depth = _shape.dim(0);
-        const int32_t height = _shape.dim(1);
-        const int32_t width = _shape.dim(2);
+        const int32_t dim1 = _shape.dim(1);
+        const int32_t dim2 = _shape.dim(2);
 
-        for (auto c = 0; c < depth; ++c)
+        for (auto i = 0; i < _shape.dim(0); ++i)
         {
-          for (auto h = 0; h < height; ++h)
+          for (auto j = 0; j < _shape.dim(1); ++j)
           {
-            neurun::util::feature::Coordinate4D coord{0, h, 0, c};
-            memcpy(_output_buffer + c * height * width + h * width,
-                   input_buffer + tensor.calcOffset(coord), width * sizeof(T));
+            neurun::util::Coordinates coords{i, j, 0};
+            memcpy(_output_buffer + i * dim1 * dim2 + j * dim2,
+                   input_buffer + tensor.calcOffset(coords), dim2 * sizeof(T));
           }
         }
         break;
       }
       case 4:
       {
+        // TODO Support from nhwc to nchw
         auto feature = _shape.asFeature();
 
         const util::feature::nchw::View<T> from{&tensor};
@@ -158,59 +159,33 @@ public:
 
         for (auto i = 0; i < _shape.dim(0); ++i)
         {
-          neurun::util::feature::Coordinate4D coord;
-          if (tensor.layout() == neurun::graph::operand::Layout::NHWC)
-          {
-            coord.w(i);
-          }
-          else if (tensor.layout() == neurun::graph::operand::Layout::NCHW)
-          {
-            coord.h(i);
-          }
-          else
-          {
-            throw std::runtime_error("Wrong Layout");
-          }
-          memcpy(_output_buffer + i * copy_len, input_buffer + tensor.calcOffset(coord),
+          neurun::util::Coordinates coords{i, 0};
+          memcpy(_output_buffer + i * copy_len, input_buffer + tensor.calcOffset(coords),
                  copy_len * sizeof(T));
         }
         break;
       }
       case 3:
       {
-        const int32_t width = _shape.dim(1);
-        const int32_t copy_len = _shape.dim(2);
+        const int32_t dim1 = _shape.dim(1);
+        const int32_t dim2 = _shape.dim(2);
 
         for (auto i = 0; i < _shape.dim(0); ++i)
         {
           for (auto j = 0; j < _shape.dim(1); ++j)
           {
-            neurun::util::feature::Coordinate4D coord;
-            if (tensor.layout() == neurun::graph::operand::Layout::NHWC)
-            {
-              coord.h(i);
-              coord.w(j);
-            }
-            else if (tensor.layout() == neurun::graph::operand::Layout::NCHW)
-            {
-              coord.c(i);
-              coord.h(j);
-            }
-            else
-            {
-              throw std::runtime_error("Wrong Layout");
-            }
-            memcpy(_output_buffer + i * width * copy_len + j * copy_len,
-                   input_buffer + tensor.calcOffset(coord), copy_len * sizeof(T));
+            neurun::util::Coordinates coords{i, j, 0};
+            memcpy(_output_buffer + i * dim1 * dim2 + j * dim2,
+                   input_buffer + tensor.calcOffset(coords), dim2 * sizeof(T));
           }
         }
         break;
       }
       case 4:
       {
-        const int32_t height = _shape.dim(1);
-        const int32_t width = _shape.dim(2);
-        const int32_t copy_len = _shape.dim(3);
+        const int32_t dim1 = _shape.dim(1);
+        const int32_t dim2 = _shape.dim(2);
+        const int32_t dim3 = _shape.dim(3);
 
         for (auto i = 0; i < _shape.dim(0); ++i)
         {
@@ -218,26 +193,9 @@ public:
           {
             for (auto k = 0; k < _shape.dim(2); ++k)
             {
-              neurun::util::feature::Coordinate4D coord;
-              if (tensor.layout() == neurun::graph::operand::Layout::NHWC)
-              {
-                coord.n(i);
-                coord.h(j);
-                coord.w(k);
-              }
-              else if (tensor.layout() == neurun::graph::operand::Layout::NCHW)
-              {
-                coord.n(i);
-                coord.c(j);
-                coord.h(k);
-              }
-              else
-              {
-                throw std::runtime_error("Wrong Layout");
-              }
-              memcpy(_output_buffer + i * height * width * copy_len + j * width * copy_len +
-                         k * copy_len,
-                     input_buffer + tensor.calcOffset(coord), copy_len * sizeof(T));
+              neurun::util::Coordinates coords{i, j, k, 0};
+              memcpy(_output_buffer + i * dim1 * dim2 * dim3 + j * dim2 * dim3 + k * dim3,
+                     input_buffer + tensor.calcOffset(coords), dim3 * sizeof(T));
             }
           }
         }
index ba27cf2..1af3e3a 100644 (file)
@@ -23,7 +23,7 @@
 #include "graph/operand/Layout.h"
 #include "util/feature/nchw/View.h"
 #include "util/feature/nhwc/Reader.h"
-#include "util/feature/Coordinate4D.h"
+#include "util/Utils.h"
 #include <misc/feature/IndexIterator.h>
 #include "model/Shape.h"
 
@@ -71,29 +71,29 @@ public:
       }
       case 2:
       {
-        auto matrix_shape = _shape.asMatrix();
+        const auto copy_len = _shape.dim(1);
 
-        for (auto h = 0; h < matrix_shape.H; ++h)
+        for (auto i = 0; i < _shape.dim(0); ++i)
         {
-          neurun::util::feature::Coordinate4D coord{0, h, 0, 0};
-          memcpy(output_buffer + tensor.calcOffset(coord), _input_buffer + h * matrix_shape.W,
-                 matrix_shape.W * sizeof(T));
+          neurun::util::Coordinates coords{i, 0};
+          memcpy(output_buffer + tensor.calcOffset(coords), _input_buffer + i * copy_len,
+                 copy_len * sizeof(T));
         }
         break;
       }
       case 3:
       {
-        const int32_t depth = _shape.dim(0);
-        const int32_t height = _shape.dim(1);
-        const int32_t width = _shape.dim(2);
+        const int32_t dim0 = _shape.dim(0);
+        const int32_t dim1 = _shape.dim(1);
+        const int32_t dim2 = _shape.dim(2);
 
-        for (auto c = 0; c < depth; ++c)
+        for (auto i = 0; i < dim0; ++i)
         {
-          for (auto h = 0; h < height; ++h)
+          for (auto j = 0; j < dim1; ++j)
           {
-            neurun::util::feature::Coordinate4D coord{0, h, 0, c};
-            memcpy(output_buffer + tensor.calcOffset(coord),
-                   _input_buffer + c * height * width + h * width, width * sizeof(T));
+            neurun::util::Coordinates coords{i, j, 0};
+            memcpy(output_buffer + tensor.calcOffset(coords),
+                   _input_buffer + i * dim1 * dim2 + j * dim2, dim2 * sizeof(T));
           }
         }
         break;
@@ -158,88 +158,45 @@ public:
       {
         const int32_t copy_len = _shape.dim(1);
 
-        for (auto i = 0; i < _shape.dim(0); ++i) // w
+        for (auto i = 0; i < _shape.dim(0); ++i)
         {
-          neurun::util::feature::Coordinate4D coord;
-          if (tensor.layout() == neurun::graph::operand::Layout::NHWC)
-          {
-            coord.w(i);
-          }
-          else if (tensor.layout() == neurun::graph::operand::Layout::NCHW)
-          {
-            coord.h(i);
-          }
-          else
-          {
-            throw std::runtime_error("Wrong Layout");
-          }
-          memcpy(output_buffer + tensor.calcOffset(coord), _input_buffer + i * copy_len,
+          neurun::util::Coordinates coords{i, 0};
+          memcpy(output_buffer + tensor.calcOffset(coords), _input_buffer + i * copy_len,
                  copy_len * sizeof(T));
         }
         break;
       }
       case 3:
       {
-        const int32_t width = _shape.dim(1);
-        const int32_t copy_len = _shape.dim(2);
+        const int32_t dim1 = _shape.dim(1);
+        const int32_t dim2 = _shape.dim(2);
 
-        for (auto i = 0; i < _shape.dim(0); ++i) // h
+        for (auto i = 0; i < _shape.dim(0); ++i)
         {
-          for (auto j = 0; j < _shape.dim(1); ++j) // w
+          for (auto j = 0; j < _shape.dim(1); ++j)
           {
-            neurun::util::feature::Coordinate4D coord;
-            if (tensor.layout() == neurun::graph::operand::Layout::NHWC)
-            {
-              coord.h(i);
-              coord.w(j);
-            }
-            else if (tensor.layout() == neurun::graph::operand::Layout::NCHW)
-            {
-              coord.c(i);
-              coord.h(j);
-            }
-            else
-            {
-              throw std::runtime_error("Wrong Layout");
-            }
-            memcpy(output_buffer + tensor.calcOffset(coord),
-                   _input_buffer + i * width * copy_len + j * copy_len, copy_len * sizeof(T));
+            neurun::util::Coordinates coords{i, j, 0};
+            memcpy(output_buffer + tensor.calcOffset(coords),
+                   _input_buffer + i * dim1 * dim2 + j * dim2, dim2 * sizeof(T));
           }
         }
         break;
       }
       case 4:
       {
-        const int32_t height = _shape.dim(1);
-        const int32_t width = _shape.dim(2);
-        const int32_t copy_len = _shape.dim(3);
-        for (auto i = 0; i < _shape.dim(0); ++i) // n
+        const int32_t dim1 = _shape.dim(1);
+        const int32_t dim2 = _shape.dim(2);
+        const int32_t dim3 = _shape.dim(3);
+        for (auto i = 0; i < _shape.dim(0); ++i)
         {
-          for (auto j = 0; j < _shape.dim(1); ++j) // h
+          for (auto j = 0; j < _shape.dim(1); ++j)
           {
-            for (auto k = 0; k < _shape.dim(2); ++k) // w
+            for (auto k = 0; k < _shape.dim(2); ++k)
             {
-              neurun::util::feature::Coordinate4D coord;
-              if (tensor.layout() == neurun::graph::operand::Layout::NHWC)
-              {
-                coord.n(i);
-                coord.h(j);
-                coord.w(k);
-              }
-              else if (tensor.layout() == neurun::graph::operand::Layout::NCHW)
-              {
-                coord.n(i);
-                coord.c(j);
-                coord.h(k);
-              }
-              else
-              {
-                throw std::runtime_error("Wrong Layout");
-              }
-              memcpy(output_buffer + tensor.calcOffset(coord),
-                     _input_buffer + i * height * width * copy_len + j * width * copy_len +
-                         k * copy_len,
-                     copy_len * sizeof(T));
+              neurun::util::Coordinates coords{i, j, k, 0};
+              memcpy(output_buffer + tensor.calcOffset(coords),
+                     _input_buffer + i * dim1 * dim2 * dim3 + j * dim2 * dim3 + k * dim3,
+                     dim3 * sizeof(T));
             }
           }
         }