summaryrefslogtreecommitdiff
path: root/runtime/neurun/backend/srcn/kernel/AddLayer.cc
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/neurun/backend/srcn/kernel/AddLayer.cc')
-rw-r--r--runtime/neurun/backend/srcn/kernel/AddLayer.cc123
1 files changed, 123 insertions, 0 deletions
diff --git a/runtime/neurun/backend/srcn/kernel/AddLayer.cc b/runtime/neurun/backend/srcn/kernel/AddLayer.cc
new file mode 100644
index 000000000..b53dfe89d
--- /dev/null
+++ b/runtime/neurun/backend/srcn/kernel/AddLayer.cc
@@ -0,0 +1,123 @@
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "AddLayer.h"
+
+#include "OperationUtils.h"
+#include "ncnn/layer/binaryop.h"
+
+#include "cpp14/memory.h"
+
+namespace
+{
+std::unique_ptr<nnfw::ncnn::Mat>
+convertMatIgnoreLayout(neurun::backend::srcn::kernel::TensorDescriptor &desc, void *data)
+{
+ if (desc.dimensions.size() == 1)
+ {
+ return nnfw::cpp14::make_unique<nnfw::ncnn::Mat>(desc.dimensions[0], data);
+ }
+ else if (desc.dimensions.size() == 2)
+ {
+ return nnfw::cpp14::make_unique<nnfw::ncnn::Mat>(desc.dimensions[1], desc.dimensions[0], data);
+ }
+ else if (desc.dimensions.size() == 3)
+ {
+ return nnfw::cpp14::make_unique<nnfw::ncnn::Mat>(desc.dimensions[2], desc.dimensions[1],
+ desc.dimensions[0], data);
+ }
+ else // rank == 4 and N == 1
+ {
+ return nnfw::cpp14::make_unique<nnfw::ncnn::Mat>(desc.dimensions[3], desc.dimensions[2],
+ desc.dimensions[1], data);
+ }
+}
+} // namespace
+
+namespace neurun
+{
+namespace backend
+{
+namespace srcn
+{
+namespace kernel
+{
+
+void AddLayer::addFloat32()
+{
+ assert(_activation == ir::Activation::NONE);
+
+ // ncnn kernel support
+ // 1. rank < 4
+ // 2. broadcasting
+ // 2-1 lhs, rhs have same rank, or
+ // 2-2 model layout and backend layout is same
+ // For safety, block all broadcasting (enable when ready)
+
+ assert(_lhsDescr.dimensions.size() < 4 ||
+ (_lhsDescr.dimensions.size() == 4 && _lhsDescr.dimensions[0] == 1));
+ assert(_rhsDescr.dimensions.size() < 4 ||
+ (_rhsDescr.dimensions.size() == 4 && _rhsDescr.dimensions[0] == 1));
+ assert((_lhsDescr.dimensions.size() == _rhsDescr.dimensions.size()));
+
+ nnfw::ncnn::BinaryOpParam param;
+ param.op_type = nnfw::ncnn::BinaryOp::Operation_ADD;
+
+ auto lhs_mat = convertMatIgnoreLayout(_lhsDescr, _lhsData.v);
+ auto rhs_mat = convertMatIgnoreLayout(_rhsDescr, _rhsData.v);
+ auto out_mat = convertMatIgnoreLayout(_outputDescr, _outputData.v);
+
+ ::nnfw::ncnn::ncnn_binary_op(param, *lhs_mat.get(), *rhs_mat.get(), *out_mat.get());
+}
+
+void AddLayer::addQuant8()
+{
+ // quant8 add is not implemented yet
+ throw std::runtime_error{"NYI"};
+}
+
+void AddLayer::configure(uint8_t *lhsData, const TensorDescriptor &lhsDescr, uint8_t *rhsData,
+ const TensorDescriptor &rhsDescr, const ir::Activation activation,
+ uint8_t *outputData, const TensorDescriptor &outputDescr,
+ const ir::Layout backendLayout)
+{
+ _lhsData.u8 = lhsData;
+ _lhsDescr = lhsDescr;
+ _rhsData.u8 = rhsData;
+ _rhsDescr = rhsDescr;
+ _inputType = lhsDescr.type;
+ _activation = activation;
+ _outputData.u8 = outputData;
+ _outputDescr = outputDescr;
+ _backendLayout = backendLayout;
+}
+
+void AddLayer::run()
+{
+ if (_inputType == OperandType::FLOAT32)
+ {
+ addFloat32();
+ }
+ else if (_inputType == OperandType::QUANT8_ASYMM)
+ {
+ addQuant8();
+ }
+}
+
+} // namespace kernel
+} // namespace srcn
+} // namespace backend
+} // namespace neurun