Add memcpy() version of generic TensorSource for 3D tensor (#2477)
author장지섭/동작제어Lab(SR)/Engineer/삼성전자 <jiseob.jang@samsung.com>
Fri, 31 Aug 2018 08:28:44 +0000 (17:28 +0900)
committer이춘석/동작제어Lab(SR)/Staff Engineer/삼성전자 <chunseok.lee@samsung.com>
Fri, 31 Aug 2018 08:28:44 +0000 (17:28 +0900)
This commit adds memcpy() version of generic TensorSource for 3D tensor.

Signed-off-by: jiseob.jang <jiseob.jang@samsung.com>
runtimes/pure_arm_compute/src/execution.cc
runtimes/pure_arm_compute/src/internal/Tensor3DSource.h [new file with mode: 0644]

index 4100969..babeece 100644 (file)
@@ -8,6 +8,7 @@
 
 #include "internal/VectorSource.h"
 #include "internal/MatrixSource.h"
+#include "internal/Tensor3DSource.h"
 #include "internal/FeatureSource.h"
 #include "internal/TensorSource.h"
 
@@ -82,6 +83,36 @@ static void asMatrixSource(ANeuralNetworksExecution *execution, int32_t type, in
   }
 }
 
+static void asTensor3DSource(ANeuralNetworksExecution *execution, int32_t type, int32_t index,
+                             const nnfw::util::tensor::Shape &shape, const void *buffer,
+                             size_t length)
+{
+  switch (type)
+  {
+    case ANEURALNETWORKS_FLOAT32:
+    case ANEURALNETWORKS_TENSOR_FLOAT32:
+      execution->source<Tensor3DSource<float>>(index, shape,
+                                               reinterpret_cast<const float *>(buffer), length);
+      break;
+    case ANEURALNETWORKS_INT32:
+    case ANEURALNETWORKS_TENSOR_INT32:
+      execution->source<Tensor3DSource<int32_t>>(index, shape,
+                                                 reinterpret_cast<const int32_t *>(buffer), length);
+      break;
+    case ANEURALNETWORKS_UINT32:
+      execution->source<Tensor3DSource<uint32_t>>(
+          index, shape, reinterpret_cast<const uint32_t *>(buffer), length);
+      break;
+    case ANEURALNETWORKS_TENSOR_QUANT8_ASYMM:
+      execution->source<Tensor3DSource<uint8_t>>(index, shape,
+                                                 reinterpret_cast<const uint8_t *>(buffer), length);
+      break;
+    default:
+      throw std::runtime_error("Not supported, yet");
+      break;
+  }
+}
+
 static void asTensorSource(ANeuralNetworksExecution *execution, int32_t type, int32_t index,
                            const nnfw::util::tensor::Shape &shape, const void *buffer,
                            size_t length)
@@ -352,7 +383,7 @@ int ANeuralNetworksExecution_setInput(ANeuralNetworksExecution *execution, int32
   {
     const auto &operand_shape = shape.asTensor();
 
-    asTensorSource(execution, input_type, index, operand_shape, buffer, length);
+    asTensor3DSource(execution, input_type, index, operand_shape, buffer, length);
   }
   else if (rank == 4)
   {
diff --git a/runtimes/pure_arm_compute/src/internal/Tensor3DSource.h b/runtimes/pure_arm_compute/src/internal/Tensor3DSource.h
new file mode 100644 (file)
index 0000000..bbc8101
--- /dev/null
@@ -0,0 +1,54 @@
+#ifndef __TENSOR3D_SOURCE_H__
+#define __TENSOR3D_SOURCE_H__
+
+#include "internal/Source.h"
+
+//
+// This is memcpy() version of generic TensorSource for 3D tensor
+//
+#include <arm_compute/core/ITensor.h>
+#include <arm_compute/core/Window.h>
+#include <arm_compute/core/Helpers.h>
+
+template <typename T> class Tensor3DSource final : public Source
+{
+public:
+  Tensor3DSource(const nnfw::util::tensor::Shape &shape, const T *base, const size_t size)
+      : _shape{shape}, _base{base}, _size{size}
+  {
+    // DO NOTHING
+  }
+
+public:
+  void push(::arm_compute::ITensor &tensor) const override
+  {
+    using ::arm_compute::Window;
+    using ::arm_compute::Iterator;
+    using ::arm_compute::Coordinates;
+    using ::arm_compute::execute_window_loop;
+
+    Window window;
+
+    window.use_tensor_dimensions(tensor.info()->tensor_shape(), ::arm_compute::Window::DimY);
+    int32_t height_width = _shape.dim(1) * _shape.dim(2);
+    int32_t width = _shape.dim(2);
+
+    Iterator it(&tensor, window);
+    execute_window_loop(window,
+                        [&](const ::arm_compute::Coordinates &id) {
+                          const auto z = id.z();
+                          const auto y = id.y();
+                          memcpy(it.ptr(), _base + z * height_width + y * width, width * sizeof(T));
+                        },
+                        it);
+  }
+
+private:
+  const nnfw::util::tensor::Shape _shape;
+
+private:
+  const T *const _base;
+  const size_t _size;
+};
+
+#endif // __TENSOR3D_SOURCE_H__