Add Reduce operation implementation (#543)
authorVladimir Plazun/AI Tools Lab /SRR/Engineer/삼성전자 <v.plazun@partner.samsung.com>
Tue, 10 Jul 2018 07:47:32 +0000 (10:47 +0300)
committerSergey Vostokov/AI Tools Lab /SRR/Staff Engineer/삼성전자 <s.vostokov@samsung.com>
Tue, 10 Jul 2018 07:47:32 +0000 (16:47 +0900)
Used by model IR interpreter

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

diff --git a/contrib/nnc/libs/backend/interpreter/core/include/interpreter/ops/Reduce.h b/contrib/nnc/libs/backend/interpreter/core/include/interpreter/ops/Reduce.h
new file mode 100644 (file)
index 0000000..3ea7208
--- /dev/null
@@ -0,0 +1,64 @@
+#ifndef _NNC_CORE_BACKEND_INTERPRETER_REDUCE_IMPL_
+#define _NNC_CORE_BACKEND_INTERPRETER_REDUCE_IMPL_
+
+#include <functional>
+
+#include "nncc/core/ADT/tensor/Shape.h"
+#include "nnc/core/linalg/Tensor.h"
+
+#include "interpreter/ops/OperationImpl.h"
+#include "interpreter/ops/Fill.h"
+
+
+namespace nncc
+{
+namespace contrib
+{
+namespace backend
+{
+namespace interpreter
+{
+namespace impl
+{
+
+template <typename T> class Reduce : public OperationImpl<T>
+{
+public:
+  Reduce(const Shape &inputShape, const Shape &outputShape, const TensorVariant &input, uint32_t axis,
+         std::function<T(const T &, const T &)> reduceFunc)
+      : _inShape(inputShape), _outputShape(outputShape), _input(input), _axis(axis),
+        _reduceFunc(reduceFunc)
+  {
+    assert(outputShape.dim(axis) == 1);
+  }
+
+  std::vector<TensorVariant> operator()() override
+  {
+    return Fill<T>(_outputShape, [this](const Index &id) {
+      T element = T();
+      Index inputId = id;
+      uint32_t end = _inShape.dim(_axis);
+      for (uint32_t i = 0; i < end; ++i)
+      {
+        inputId.at(_axis) = i;
+        element = _reduceFunc(element, _input.at(inputId));
+      }
+      return element;
+    })();
+  }
+
+private:
+  const Shape &_inShape;
+  const Shape &_outputShape;
+  const Tensor<T> _input;
+  const uint32_t _axis;
+  const std::function<T(T, T)> _reduceFunc;
+};
+
+} // namespace impl
+} // namespace interpreter
+} // namespace backend
+} // namespace contrib
+} // namespace nncc
+
+#endif //_NNC_CORE_BACKEND_INTERPRETER_REDUCE_IMPL_
diff --git a/contrib/nnc/libs/backend/interpreter/core/src/ops/Reduce.cpp b/contrib/nnc/libs/backend/interpreter/core/src/ops/Reduce.cpp
new file mode 100644 (file)
index 0000000..3eec6fc
--- /dev/null
@@ -0,0 +1 @@
+#include "interpreter/ops/Reduce.h"