From 4324195f1f3a3a4d2c22d2e60890ebb1b4bb4d3f Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EC=9E=A5=EC=A7=80=EC=84=AD/On-Device=20Lab=28SR=29/Enginee?= =?utf8?q?r/=EC=82=BC=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Thu, 18 Jul 2019 17:32:59 +0900 Subject: [PATCH] Introduce ConstantInitializer into acl neon backend (#5693) This commit introduces ConstantInitializer into acl neon backend. Signed-off-by: jiseob.jang --- .../neurun/backend/acl_neon/ConstantInitializer.cc | 160 +++++++++++++++++++++ .../neurun/backend/acl_neon/ConstantInitializer.h | 62 ++++++++ 2 files changed, 222 insertions(+) create mode 100644 runtimes/neurun/backend/acl_neon/ConstantInitializer.cc create mode 100644 runtimes/neurun/backend/acl_neon/ConstantInitializer.h diff --git a/runtimes/neurun/backend/acl_neon/ConstantInitializer.cc b/runtimes/neurun/backend/acl_neon/ConstantInitializer.cc new file mode 100644 index 0000000..0b902d9 --- /dev/null +++ b/runtimes/neurun/backend/acl_neon/ConstantInitializer.cc @@ -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 &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 index 0000000..67c7257 --- /dev/null +++ b/runtimes/neurun/backend/acl_neon/ConstantInitializer.h @@ -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 +#include +#include "TensorBuilder.h" + +namespace neurun +{ +namespace backend +{ +namespace acl_neon +{ + +class ConstantInitializer : public IConstantInitializer +{ +public: + ConstantInitializer(const model::Operands &operands, + const std::shared_ptr &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 _tensor_builder; +}; + +} // namespace acl_neon +} // namespace backend +} // namespace neurun + +#endif // __NEURUN_COMPILER_ACL_NEON_CONSTANT_INITIALIZER_H__ -- 2.7.4