[nnc backend] Add Reshape operation implementation (#443)
authorVladimir Plazun/AI Tools Lab /SRR/Engineer/삼성전자 <v.plazun@partner.samsung.com>
Tue, 10 Jul 2018 07:29:28 +0000 (10:29 +0300)
committerSergey Vostokov/AI Tools Lab /SRR/Staff Engineer/삼성전자 <s.vostokov@samsung.com>
Tue, 10 Jul 2018 07:29:28 +0000 (16:29 +0900)
Add Reshape operation implementation

Used by model IR interpreter

Signed-off-by: Vladimir Plazun <v.plazun@partner.samsung.com>
contrib/nnc/libs/backend/interpreter/core/include/interpreter/ops/Reshape.h [new file with mode: 0644]
contrib/nnc/libs/backend/interpreter/core/src/ops/Reshape.cpp [new file with mode: 0644]

diff --git a/contrib/nnc/libs/backend/interpreter/core/include/interpreter/ops/Reshape.h b/contrib/nnc/libs/backend/interpreter/core/include/interpreter/ops/Reshape.h
new file mode 100644 (file)
index 0000000..0962e01
--- /dev/null
@@ -0,0 +1,58 @@
+#ifndef _NNC_CORE_BACKEND_INTERPRETER_RESHAPE_IMPL_
+#define _NNC_CORE_BACKEND_INTERPRETER_RESHAPE_IMPL_
+
+#include "nnc/core/IR/model/operations/reshape_op.h"
+
+#include "interpreter/ops/OperationImpl.h"
+#include "interpreter/ops/Fill.h"
+
+namespace nncc
+{
+namespace contrib
+{
+namespace backend
+{
+namespace interpreter
+{
+namespace impl
+{
+
+using nncc::contrib::core::IR::model::ops::ReshapeOp;
+
+template <typename T> class Reshape : public OperationImpl<T>
+{
+public:
+  Reshape(const TensorVariant &input, const ReshapeOp &op) : _input(input), _op(op)
+  {
+    assert(num_elements(_op.getInputShape(0)) == num_elements(_op.getOutputShape(0)));
+  }
+
+  std::vector<TensorVariant> operator()() override
+  {
+    const Shape &outShape = _op.getOutputShape(0);
+    const Shape &inShape = _op.getInputShape(0);
+
+    ShapeRange inRange(inShape);
+    ShapeRange outRange(outShape);
+
+    auto inIter = inRange.begin();
+
+    auto out = OperationImpl<T>::allocate_tensor(outShape);
+
+    Tensor<float> outAccessor(out);
+    // Shapes element count compared in Reshape ctor
+    return Fill<T>(outShape, [this, &inIter](const Index &) -> float { return _input.at(*inIter++); })();
+  }
+
+private:
+  Tensor<T> _input;
+  const ReshapeOp &_op;
+};
+
+} // namespace impl
+} // namespace interpreter
+} // namespace backend
+} // namespace contrib
+} // namespace nncc
+
+#endif //_NNC_CORE_BACKEND_INTERPRETER_RESHAPE_IMPL_
diff --git a/contrib/nnc/libs/backend/interpreter/core/src/ops/Reshape.cpp b/contrib/nnc/libs/backend/interpreter/core/src/ops/Reshape.cpp
new file mode 100644 (file)
index 0000000..6e3ab17
--- /dev/null
@@ -0,0 +1 @@
+#include "interpreter/ops/Reshape.h"