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

Used by model IR interpreter backend

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

diff --git a/contrib/nnc/libs/backend/interpreter/core/include/interpreter/ops/Bias.h b/contrib/nnc/libs/backend/interpreter/core/include/interpreter/ops/Bias.h
new file mode 100644 (file)
index 0000000..ea2ac39
--- /dev/null
@@ -0,0 +1,48 @@
+#ifndef _NNC_BACKEND_INTERPRETER_BIAS_
+#define _NNC_BACKEND_INTERPRETER_BIAS_
+
+#include "OperationImpl.h"
+#include "Fill.h"
+
+namespace nncc
+{
+namespace contrib
+{
+namespace backend
+{
+namespace interpreter
+{
+namespace impl
+{
+
+class BiasAdd : public OperationImpl<float>
+{
+public:
+  BiasAdd(const TensorVariant &input, const TensorVariant &weights, const Shape &outputShape)
+      : _weights(weights), _input(input), _outputShape(outputShape)
+  {
+    assert(_weights.getShape().rank() == 1);
+    assert(_outputShape.rank() == _input.getShape().rank());
+    assert(_outputShape.dim(_outputShape.rank() - 1) == _weights.getShape().dim(0));
+  }
+
+  std::vector<TensorVariant> operator()() override
+  {
+    return Fill<float>(_outputShape, [this](const Index &idx) {
+      return _input.at(idx) + _weights.at({idx.at(idx.rank() - 1)});
+    })();
+  }
+
+private:
+  const Tensor<float> _weights;
+  const Tensor<float> _input;
+  const Shape &_outputShape;
+};
+
+} // namespace impl
+} // namespace interpreter
+} // namespace backend
+} // namespace contrib
+} // namespace nncc
+
+#endif //_NNC_BACKEND_INTERPRETER_BIAS_
diff --git a/contrib/nnc/libs/backend/interpreter/core/src/ops/Bias.cpp b/contrib/nnc/libs/backend/interpreter/core/src/ops/Bias.cpp
new file mode 100644 (file)
index 0000000..66dd249
--- /dev/null
@@ -0,0 +1 @@
+#include "interpreter/ops/Bias.h"