[nnc backend] Add elementwise operation impl (#416)
authorVladimir Plazun/AI Tools Lab /SRR/Engineer/삼성전자 <v.plazun@partner.samsung.com>
Fri, 6 Jul 2018 11:25:28 +0000 (15:25 +0400)
committerSergey Vostokov/AI Tools Lab /SRR/Staff Engineer/삼성전자 <s.vostokov@samsung.com>
Fri, 6 Jul 2018 11:25:28 +0000 (20:25 +0900)
 [nnc backend] Add elementwise operation impl

Used as a base for other operation implementations

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

diff --git a/contrib/nnc/libs/backend/interpreter/core/include/interpreter/ops/Elementwise.h b/contrib/nnc/libs/backend/interpreter/core/include/interpreter/ops/Elementwise.h
new file mode 100644 (file)
index 0000000..585005b
--- /dev/null
@@ -0,0 +1,58 @@
+#ifndef _NNC_CORE_BACKEND_INTERPRETER_ELEMENTWISE_IMPL_
+#define _NNC_CORE_BACKEND_INTERPRETER_ELEMENTWISE_IMPL_
+
+#include "nnc/core/linalg/ShapeRange.h"
+#include "nnc/core/linalg/Tensor.h"
+
+#include "interpreter/ops/OperationImpl.h"
+
+namespace nncc
+{
+namespace contrib
+{
+namespace backend
+{
+namespace interpreter
+{
+namespace impl
+{
+
+using nncc::contrib::core::ADT::TensorVariant;
+using nncc::contrib::core::data::Tensor;
+
+template <typename T> class Elementwise : public OperationImpl<T>
+{
+public:
+  explicit Elementwise(const Shape &shape) : _shape(shape){};
+
+  std::vector<TensorVariant> operator()() override
+  {
+    auto res = OperationImpl<T>::allocate_tensor(_shape);
+    Tensor<T> accessor(res);
+
+    elemwise(accessor);
+
+    return {res};
+  }
+
+  virtual T single(const Index &index) = 0;
+
+protected:
+  void elemwise(Tensor<T> &res)
+  {
+    for (auto &idx : ShapeRange(_shape))
+    {
+      res.at(idx) = single(idx);
+    }
+  }
+
+  const Shape &_shape;
+};
+
+} // namespace impl
+} // namespace interpreter
+} // namespace backend
+} // namespace contrib
+} // namespace nncc
+
+#endif //_NNC_CORE_BACKEND_INTERPRETER_ELEMENTWISE_IMPL_
diff --git a/contrib/nnc/libs/backend/interpreter/core/src/ops/Elementwise.cpp b/contrib/nnc/libs/backend/interpreter/core/src/ops/Elementwise.cpp
new file mode 100644 (file)
index 0000000..e0e8e5a
--- /dev/null
@@ -0,0 +1,3 @@
+#include "interpreter/ops/elementwise.h"
+//do not delete
+//used to force compilation of elementwise.h