Introduce ConstantInitializer into acl neon backend (#5693)
author장지섭/On-Device Lab(SR)/Engineer/삼성전자 <jiseob.jang@samsung.com>
Thu, 18 Jul 2019 08:32:59 +0000 (17:32 +0900)
committer이한종/On-Device Lab(SR)/Engineer/삼성전자 <hanjoung.lee@samsung.com>
Thu, 18 Jul 2019 08:32:59 +0000 (17:32 +0900)
This commit introduces ConstantInitializer into acl neon backend.

Signed-off-by: jiseob.jang <jiseob.jang@samsung.com>
runtimes/neurun/backend/acl_neon/ConstantInitializer.cc [new file with mode: 0644]
runtimes/neurun/backend/acl_neon/ConstantInitializer.h [new file with mode: 0644]

diff --git a/runtimes/neurun/backend/acl_neon/ConstantInitializer.cc b/runtimes/neurun/backend/acl_neon/ConstantInitializer.cc
new file mode 100644 (file)
index 0000000..0b902d9
--- /dev/null
@@ -0,0 +1,160 @@
+/*
+ * 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 "ConstantInitializer.h"
+
+namespace neurun
+{
+namespace backend
+{
+namespace acl_neon
+{
+
+ConstantInitializer::ConstantInitializer(const model::Operands &operands,
+                                         const std::shared_ptr<TensorBuilder> &tensor_builder)
+    : _operands{operands}, _tensor_builder{tensor_builder}
+{
+  // DO NOTHING
+}
+
+void ConstantInitializer::run()
+{
+  for (const auto &it : _init_map)
+  {
+    const auto &ind = it.first;
+    const auto &fn = it.second;
+
+    const auto &model_obj = _operands.at(ind);
+    auto tensor_obj = _tensor_builder->wrapTensor(ind);
+    fn(model_obj, *tensor_obj);
+  }
+}
+
+void ConstantInitializer::visit(const model::operation::AddNode &node)
+{
+  const auto &lhs_index = node.getInputs().at(model::operation::AddNode::LHS);
+  const auto &lhs_obj = _operands.at(lhs_index);
+  registerPermuteInitializer(lhs_index, lhs_obj);
+
+  const auto &rhs_index = node.getInputs().at(model::operation::AddNode::RHS);
+  const auto &rhs_obj = _operands.at(rhs_index);
+  registerPermuteInitializer(rhs_index, rhs_obj);
+}
+
+void ConstantInitializer::visit(const model::operation::AvgPool2DNode &node)
+{
+  const auto &input_index = node.getInputs().at(model::operation::AvgPool2DNode::INPUT);
+  const auto &input_obj = _operands.at(input_index);
+  registerPermuteInitializer(input_index, input_obj);
+}
+
+void ConstantInitializer::visit(const model::operation::ConcatNode &node)
+{
+  const auto inputs = node.getInputs();
+  for (const auto &input_index : inputs)
+  {
+    const auto &input_obj = _operands.at(input_index);
+    registerPermuteInitializer(input_index, input_obj);
+  }
+}
+
+void ConstantInitializer::visit(const model::operation::Conv2DNode &node)
+{
+  const auto &input_index = node.getInputs().at(model::operation::Conv2DNode::INPUT);
+  const auto &input_obj = _operands.at(input_index);
+  registerPermuteInitializer(input_index, input_obj);
+
+  const auto &kernel_index = node.getInputs().at(model::operation::Conv2DNode::KERNEL);
+  const auto &kernel_obj = _operands.at(kernel_index);
+  registerPermuteInitializer(kernel_index, kernel_obj);
+
+  const auto &bias_index = node.getInputs().at(model::operation::Conv2DNode::BIAS);
+  const auto &bias_obj = _operands.at(bias_index);
+  registerDefaultInitializer(bias_index, bias_obj);
+}
+
+void ConstantInitializer::visit(const model::operation::DepthwiseConv2DNode &node)
+{
+  const auto &input_index = node.getInputs().at(model::operation::DepthwiseConv2DNode::INPUT);
+  const auto &input_obj = _operands.at(input_index);
+  registerPermuteInitializer(input_index, input_obj);
+
+  const auto &kernel_index = node.getInputs().at(model::operation::DepthwiseConv2DNode::KERNEL);
+  const auto &kernel_obj = _operands.at(kernel_index);
+  registerPermuteInitializer(kernel_index, kernel_obj);
+
+  const auto &bias_index = node.getInputs().at(model::operation::DepthwiseConv2DNode::BIAS);
+  const auto &bias_obj = _operands.at(bias_index);
+  registerDefaultInitializer(bias_index, bias_obj);
+}
+
+void ConstantInitializer::visit(const model::operation::FullyConnectedNode &node)
+{
+  const auto &input_index = node.getInputs().at(model::operation::FullyConnectedNode::INPUT);
+  const auto &input_obj = _operands.at(input_index);
+  registerPermuteInitializer(input_index, input_obj);
+
+  const auto &weight_index = node.getInputs().at(model::operation::FullyConnectedNode::WEIGHT);
+  const auto &weight_obj = _operands.at(weight_index);
+  registerDefaultInitializer(weight_index, weight_obj);
+
+  const auto &bias_index = node.getInputs().at(model::operation::FullyConnectedNode::BIAS);
+  const auto &bias_obj = _operands.at(bias_index);
+  registerDefaultInitializer(bias_index, bias_obj);
+}
+
+void ConstantInitializer::visit(const model::operation::MaxPool2DNode &node)
+{
+  const auto &input_index = node.getInputs().at(model::operation::MaxPool2DNode::INPUT);
+  const auto &input_obj = _operands.at(input_index);
+  registerPermuteInitializer(input_index, input_obj);
+}
+
+void ConstantInitializer::visit(const model::operation::MulNode &node)
+{
+  const auto &lhs_index = node.getInputs().at(model::operation::MulNode::LHS);
+  const auto &lhs_obj = _operands.at(lhs_index);
+  registerPermuteInitializer(lhs_index, lhs_obj);
+
+  const auto &rhs_index = node.getInputs().at(model::operation::MulNode::RHS);
+  const auto &rhs_obj = _operands.at(rhs_index);
+  registerPermuteInitializer(rhs_index, rhs_obj);
+}
+
+void ConstantInitializer::visit(const model::operation::ReshapeNode &node)
+{
+  const auto &input_index = node.getInputs().at(model::operation::ReshapeNode::INPUT);
+  const auto &input_obj = _operands.at(input_index);
+  registerPermuteInitializer(input_index, input_obj);
+}
+
+void ConstantInitializer::visit(const model::operation::SoftmaxNode &node)
+{
+  const auto &input_index = node.getInputs().at(model::operation::SoftmaxNode::INPUT);
+  const auto &input_obj = _operands.at(input_index);
+  registerPermuteInitializer(input_index, input_obj);
+}
+
+void ConstantInitializer::visit(const model::operation::TanhNode &node)
+{
+  const auto &input_index = node.getInputs().at(model::operation::TanhNode::INPUT);
+  const auto &input_obj = _operands.at(input_index);
+  registerPermuteInitializer(input_index, input_obj);
+}
+
+} // namespace acl_neon
+} // namespace backend
+} // namespace neurun
diff --git a/runtimes/neurun/backend/acl_neon/ConstantInitializer.h b/runtimes/neurun/backend/acl_neon/ConstantInitializer.h
new file mode 100644 (file)
index 0000000..67c7257
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+ * 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.
+ */
+
+#ifndef __NEURUN_COMPILER_ACL_NEON_CONSTANT_INITIALIZER_H__
+#define __NEURUN_COMPILER_ACL_NEON_CONSTANT_INITIALIZER_H__
+
+#include <backend/IConstantInitializer.h>
+#include <model/Operands.h>
+#include "TensorBuilder.h"
+
+namespace neurun
+{
+namespace backend
+{
+namespace acl_neon
+{
+
+class ConstantInitializer : public IConstantInitializer
+{
+public:
+  ConstantInitializer(const model::Operands &operands,
+                      const std::shared_ptr<TensorBuilder> &tensor_builder);
+
+public:
+  void run() override;
+
+public:
+  void visit(const model::operation::AddNode &) override;
+  void visit(const model::operation::AvgPool2DNode &) override;
+  void visit(const model::operation::ConcatNode &) override;
+  void visit(const model::operation::Conv2DNode &) override;
+  void visit(const model::operation::DepthwiseConv2DNode &) override;
+  void visit(const model::operation::FullyConnectedNode &) override;
+  void visit(const model::operation::MaxPool2DNode &) override;
+  void visit(const model::operation::MulNode &) override;
+  void visit(const model::operation::ReshapeNode &) override;
+  void visit(const model::operation::SoftmaxNode &) override;
+  void visit(const model::operation::TanhNode &) override;
+
+private:
+  const model::Operands &_operands;
+  std::shared_ptr<TensorBuilder> _tensor_builder;
+};
+
+} // namespace acl_neon
+} // namespace backend
+} // namespace neurun
+
+#endif // __NEURUN_COMPILER_ACL_NEON_CONSTANT_INITIALIZER_H__