[neurun] Remove backend dependency in View (#3821)
author김수진/동작제어Lab(SR)/Engineer/삼성전자 <sjsujin.kim@samsung.com>
Tue, 4 Dec 2018 08:14:35 +0000 (17:14 +0900)
committer오형석/동작제어Lab(SR)/Staff Engineer/삼성전자 <hseok82.oh@samsung.com>
Tue, 4 Dec 2018 08:14:35 +0000 (17:14 +0900)
* [neurun] Remove backend dependency in View

This commit removes `backend dependency` in `View` classes.

- Add `offset_element_in_bytes` abstract function to `ITensor`
- Make `View` gets `ITensor`

Signed-off-by: sjsujinkim <sjsujin.kim@samsung.com>
* Change offset_element_in_bytes to calcOffset

runtimes/neurun/src/backend/acl_cl/kernel/View.h
runtimes/neurun/src/backend/acl_cl/operand/ICLTensor.cc
runtimes/neurun/src/backend/acl_cl/operand/ICLTensor.h
runtimes/neurun/src/backend/cpu/operand/Tensor.cc
runtimes/neurun/src/backend/cpu/operand/Tensor.h
runtimes/neurun/src/backend/interface/operand/ITensor.h
runtimes/neurun/src/exec/Sink.h
runtimes/neurun/src/exec/Source.h
runtimes/neurun/src/util/feature/nchw/View.h

index a7d8191..24cf543 100644 (file)
@@ -19,7 +19,8 @@
 
 #include "util/kernel/Shape.h"
 #include "util/kernel/Reader.h"
-#include "backend/acl_cl/operand/ICLTensor.h"
+#include "backend/interface/operand/ITensor.h"
+#include "util/feature/Coordinate4D.h"
 
 #include <cassert>
 
@@ -35,10 +36,8 @@ template <typename T> class View;
 template <> class View<float> final : public nnfw::util::kernel::Reader<float>
 {
 public:
-  View(::neurun::backend::acl_cl::operand::ICLTensor *tensor) : _tensor{tensor}
+  View(::neurun::backend::operand::ITensor *tensor) : _tensor{tensor}
   {
-    assert(tensor->data_type() == ::arm_compute::DataType::F32);
-
     _shape.N = tensor->dimension(3);
     _shape.C = tensor->dimension(2);
     _shape.H = tensor->dimension(1);
@@ -71,12 +70,14 @@ public:
 private:
   size_t kernel_index_to_byte_offset(uint32_t nth, uint32_t ch, uint32_t row, uint32_t col) const
   {
-    return _tensor->info()->offset_element_in_bytes(::arm_compute::Coordinates{col, row, ch, nth});
+    return _tensor->calcOffset(
+        neurun::util::feature::Coordinate4D{static_cast<int32_t>(nth), static_cast<int32_t>(row),
+                                            static_cast<int32_t>(col), static_cast<int32_t>(ch)});
   }
 
 private:
   ::nnfw::util::kernel::Shape _shape;
-  ::neurun::backend::acl_cl::operand::ICLTensor *_tensor;
+  ::neurun::backend::operand::ITensor *_tensor;
 };
 
 } // namespace kernel
index a037222..23d723d 100644 (file)
@@ -15,6 +15,17 @@ size_t ICLTensor::dimension(size_t index) const { return info()->dimension(index
 
 size_t ICLTensor::num_dimensions() const { return info()->num_dimensions(); }
 
+size_t ICLTensor::calcOffset(const neurun::util::feature::Coordinate4D &coords)
+{
+  int32_t N = coords.n();
+  int32_t C = coords.c();
+  int32_t H = coords.h();
+  int32_t W = coords.w();
+
+  ::arm_compute::Coordinates coordinates{W, H, C, N};
+  return info()->offset_element_in_bytes(coordinates);
+}
+
 arm_compute::DataType ICLTensor::data_type() const { return info()->data_type(); }
 
 uint8_t *ICLTensor::buffer() const { return handle()->buffer(); }
index d98f4ff..226fbf8 100644 (file)
@@ -49,6 +49,7 @@ public:
   size_t total_size() const override;
   size_t dimension(size_t index) const override;
   size_t num_dimensions() const override;
+  size_t calcOffset(const neurun::util::feature::Coordinate4D &coords) override;
 
 public:
   arm_compute::DataType data_type() const;
index 0e4f34a..a525129 100644 (file)
@@ -16,6 +16,8 @@
 
 #include "Tensor.h"
 
+#define NO_USE(a) (void)(a)
+
 namespace neurun
 {
 namespace backend
@@ -25,7 +27,11 @@ namespace cpu
 namespace operand
 {
 
-// NO IMPLEMENTATION YET
+size_t Tensor::calcOffset(const neurun::util::feature::Coordinate4D &coords)
+{
+  NO_USE(coords);
+  throw std::runtime_error("offset_element_in_bytes is not supported for cpu::Tensor now.");
+}
 
 } // namespace operand
 } // namespace cpu
index 2c0144d..644b62f 100644 (file)
@@ -59,6 +59,7 @@ public:
   size_t dimension(size_t index) const override { return _info.shape().dim(index); }
   size_t num_dimensions() const override { return _info.shape().dims().size(); }
   size_t total_size() const override { return _info.total_size(); }
+  size_t calcOffset(const neurun::util::feature::Coordinate4D &coords) override;
 
 private:
   compiler::TensorInfo _info;
index 61e3687..8bc3ff4 100644 (file)
@@ -20,6 +20,8 @@
 #include <cstring>
 #include <cstdint>
 
+#include "util/feature/Coordinate4D.h"
+
 namespace neurun
 {
 namespace backend
@@ -37,6 +39,7 @@ public:
   virtual size_t total_size() const = 0;
   virtual size_t dimension(size_t index) const = 0;
   virtual size_t num_dimensions() const = 0;
+  virtual size_t calcOffset(const neurun::util::feature::Coordinate4D &coords) = 0;
 };
 
 } // namespace operand
index 6dafc8e..c18fa6a 100644 (file)
@@ -25,9 +25,9 @@
 #include "util/feature/nhwc/View.h"
 #include "util/feature/nchw/View.h"
 #include <util/feature/IndexIterator.h>
-#include "backend/interface/operand/ITensor.h"
 
 // TODO Remove these dependencies to arm_compute lib
+#include "backend/acl_cl/operand/ICLTensor.h"
 #include <arm_compute/core/Window.h>
 #include <arm_compute/core/Helpers.h>
 
index 21058c6..7953d33 100644 (file)
@@ -25,9 +25,9 @@
 #include "util/feature/nchw/View.h"
 #include "util/feature/nhwc/Reader.h"
 #include <util/feature/IndexIterator.h>
-#include "backend/interface/operand/ITensor.h"
 
 // TODO Remove these dependencies to arm_compute lib
+#include "backend/acl_cl/operand/ICLTensor.h"
 #include <arm_compute/core/Window.h>
 #include <arm_compute/core/Helpers.h>
 
index ca9cb4f..9c3a9d9 100644 (file)
@@ -19,7 +19,8 @@
 
 #include "util/feature/Reader.h"
 
-#include "backend/acl_cl/operand/ICLTensor.h"
+#include "backend/interface/operand/ITensor.h"
+#include "util/feature/Coordinate4D.h"
 
 #include <cassert>
 
@@ -35,10 +36,8 @@ namespace nchw
 template <typename T> class View final : public nnfw::util::feature::Reader<T>
 {
 public:
-  View(::neurun::backend::acl_cl::operand::ICLTensor *tensor) : _tensor{tensor}
+  View(::neurun::backend::operand::ITensor *tensor) : _tensor{tensor}
   {
-    assert(tensor->data_type() == ::arm_compute::DataType::F32);
-
     // TODO Validate whether tensor is a feature map, or not
 
     _shape.C = tensor->dimension(2);
@@ -86,21 +85,17 @@ public:
   }
 
 private:
-  size_t feature_index_to_byte_offset(uint32_t ch, uint32_t row, uint32_t col) const
-  {
-    // ARM Compute uses CHW ordering
-    return _tensor->info()->offset_element_in_bytes(::arm_compute::Coordinates{col, row, ch});
-  }
-  size_t feature_index_to_byte_offset(uint32_t batch, uint32_t ch, uint32_t row, uint32_t col) const
+  size_t feature_index_to_byte_offset(uint32_t batch, uint32_t ch = 0, uint32_t row = 0,
+                                      uint32_t col = 0) const
   {
-    // ARM Compute uses CHW ordering
-    return _tensor->info()->offset_element_in_bytes(
-        ::arm_compute::Coordinates{col, row, ch, batch});
+    return _tensor->calcOffset(
+        neurun::util::feature::Coordinate4D{static_cast<int32_t>(batch), static_cast<int32_t>(row),
+                                            static_cast<int32_t>(col), static_cast<int32_t>(ch)});
   }
 
 private:
   ::nnfw::util::feature::Shape _shape;
-  ::neurun::backend::acl_cl::operand::ICLTensor *_tensor;
+  ::neurun::backend::operand::ITensor *_tensor;
 };
 
 } // namespace nchw