From 2e34bf6ee159d10e02678d34ff506b1e57ff80c7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Vladimir=20Plazun/AI=20Tools=20Lab=20/SRR/Engineer/?= =?utf8?q?=EC=82=BC=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Fri, 6 Jul 2018 15:25:28 +0400 Subject: [PATCH] [nnc backend] Add elementwise operation impl (#416) [nnc backend] Add elementwise operation impl Used as a base for other operation implementations Signed-off-by: Vladimir Plazun --- .../core/include/interpreter/ops/Elementwise.h | 58 ++++++++++++++++++++++ .../interpreter/core/src/ops/Elementwise.cpp | 3 ++ 2 files changed, 61 insertions(+) create mode 100644 contrib/nnc/libs/backend/interpreter/core/include/interpreter/ops/Elementwise.h create mode 100644 contrib/nnc/libs/backend/interpreter/core/src/ops/Elementwise.cpp 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 index 0000000..585005b --- /dev/null +++ b/contrib/nnc/libs/backend/interpreter/core/include/interpreter/ops/Elementwise.h @@ -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 class Elementwise : public OperationImpl +{ +public: + explicit Elementwise(const Shape &shape) : _shape(shape){}; + + std::vector operator()() override + { + auto res = OperationImpl::allocate_tensor(_shape); + Tensor accessor(res); + + elemwise(accessor); + + return {res}; + } + + virtual T single(const Index &index) = 0; + +protected: + void elemwise(Tensor &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 index 0000000..e0e8e5a --- /dev/null +++ b/contrib/nnc/libs/backend/interpreter/core/src/ops/Elementwise.cpp @@ -0,0 +1,3 @@ +#include "interpreter/ops/elementwise.h" +//do not delete +//used to force compilation of elementwise.h -- 2.7.4