From ec0cb7c1ec8221583a26abb558749fecaccf2f0a Mon Sep 17 00:00:00 2001 From: =?utf8?q?Ivan=20Vagin/AI=20Tools=20Lab=20/SRR/Engineer/=EC=82=BC?= =?utf8?q?=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Mon, 8 Jul 2019 13:16:37 +0300 Subject: [PATCH] [neurun] Initial implementation of KernelGenerator (#5574) * Implementated KernelGenerator interface * Implemented empty KernelGenerator classes for each backend Signed-off-by: Ivan Vagin --- runtimes/neurun/backend/acl_cl/Backend.h | 2 + runtimes/neurun/backend/acl_cl/KernelGenerator.cc | 170 +++++++++++++++++++++ runtimes/neurun/backend/acl_cl/KernelGenerator.h | 105 +++++++++++++ runtimes/neurun/backend/acl_neon/Backend.h | 2 + .../neurun/backend/acl_neon/KernelGenerator.cc | 88 +++++++++++ runtimes/neurun/backend/acl_neon/KernelGenerator.h | 61 ++++++++ runtimes/neurun/backend/cpu/Backend.h | 2 + runtimes/neurun/backend/cpu/KernelGenerator.cc | 80 ++++++++++ runtimes/neurun/backend/cpu/KernelGenerator.h | 61 ++++++++ .../neurun/backend/hi_perf_cpu/KernelGenerator.cc | 18 +++ .../neurun/backend/hi_perf_cpu/KernelGenerator.h | 49 ++++++ runtimes/neurun/core/include/backend/Backend.h | 6 +- .../neurun/core/include/backend/IKernelGenerator.h | 63 ++++++++ runtimes/neurun/core/src/backend/Backend.cc | 10 +- .../neurun/core/src/compiler/ExecutorFactory.cc | 25 +-- runtimes/neurun/core/src/compiler/PlanBuilder.cc | 13 +- runtimes/neurun/test/core/backend/ExecTime.test.cc | 8 +- 17 files changed, 748 insertions(+), 15 deletions(-) create mode 100644 runtimes/neurun/backend/acl_cl/KernelGenerator.cc create mode 100644 runtimes/neurun/backend/acl_cl/KernelGenerator.h create mode 100644 runtimes/neurun/backend/acl_neon/KernelGenerator.cc create mode 100644 runtimes/neurun/backend/acl_neon/KernelGenerator.h create mode 100644 runtimes/neurun/backend/cpu/KernelGenerator.cc create mode 100644 runtimes/neurun/backend/cpu/KernelGenerator.h create mode 100644 runtimes/neurun/backend/hi_perf_cpu/KernelGenerator.cc create mode 100644 runtimes/neurun/backend/hi_perf_cpu/KernelGenerator.h create mode 100644 runtimes/neurun/core/include/backend/IKernelGenerator.h diff --git a/runtimes/neurun/backend/acl_cl/Backend.h b/runtimes/neurun/backend/acl_cl/Backend.h index 72a6a0f..1f0c449 100644 --- a/runtimes/neurun/backend/acl_cl/Backend.h +++ b/runtimes/neurun/backend/acl_cl/Backend.h @@ -23,6 +23,7 @@ #include "Config.h" #include "StageGenerator.h" +#include "KernelGenerator.h" #include "ShapeFixer.h" #include "MemoryManager.h" @@ -41,6 +42,7 @@ public: std::make_shared(createMemoryManager())) : ::neurun::backend::Backend{std::make_shared(), std::make_shared(operand_ctx, tensor_builder), + std::make_shared(operand_ctx, tensor_builder), std::make_shared(operand_ctx, tensor_builder)} { // DO NOTHING diff --git a/runtimes/neurun/backend/acl_cl/KernelGenerator.cc b/runtimes/neurun/backend/acl_cl/KernelGenerator.cc new file mode 100644 index 0000000..b125612 --- /dev/null +++ b/runtimes/neurun/backend/acl_cl/KernelGenerator.cc @@ -0,0 +1,170 @@ +/* + * 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 "KernelGenerator.h" + +#include // Include all ARM Compute CL functions +#include // Include all ARM Compute EX CL functions +#include +#include + +#include "kernel/ConcatLayer.h" +#include "model/Index.h" +#include "model/DataType.h" +#include "model/InternalType.h" +#include "compiler/IExecutionBuilder.h" +#include "exec/NopFunction.h" +#include "util/logging.h" +#include "util/Utils.h" +#include "util/Padding.h" +#include "acl_common/AclFunction.h" +#include "acl_common/Convert.h" +#include "acl_common/Swizzle.h" + +using ::neurun::compiler::IExecutionBuilder; + +namespace neurun +{ +namespace backend +{ +namespace acl_cl +{ + +using ::neurun::backend::acl_common::asAclFunction; + +// +// KernelGenerator +// +KernelGenerator::KernelGenerator(const neurun::model::Operands &ctx, + const std::shared_ptr &tensor_builder) + : _ctx(ctx), _tensor_builder(tensor_builder) +{ + // DO NOTHING +} + +void KernelGenerator::visit(const model::operation::CastNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::Conv2DNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::DepthwiseConv2DNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::MaxPool2DNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::AvgPool2DNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::ConcatNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::FullyConnectedNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::MulNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::ReduceSumNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::ReshapeNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::SqueezeNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::TanhNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::SoftmaxNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::StridedSliceNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::TransposeNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::AddNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::SubNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::DivNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::ExpNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::LogisticNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::LogicalAndNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::LSTMNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::ReduceMaxNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::ComparisonNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::RSQRTNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::ReLUNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::ResizeBilinearNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::ReLU1Node & /*node*/) {} + +void KernelGenerator::visit(const model::operation::ReLU6Node & /*node*/) {} + +void KernelGenerator::visit(const model::operation::RNNNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::FloorNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::SpaceToDepthNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::L2Pool2DNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::EmbeddingLookupNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::L2NormalizationNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::HashtableLookupNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::PReLUNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::TransposeConvNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::SQRTNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::LogicalOrNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::LogicalNotNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::SquaredDifferenceNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::TopKV2Node & /*node*/) {} + +void KernelGenerator::visit(const model::operation::GatherNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::NegNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::AbsNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::ArgMaxNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::DequantizeNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::MeanNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::LocalResponseNormalizationNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::DepthToSpaceNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::ReduceMinNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::SplitNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::UnpackNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::PadNode & /*node*/) {} + +} // namespace acl_cl +} // namespace backend +} // namespace neurun diff --git a/runtimes/neurun/backend/acl_cl/KernelGenerator.h b/runtimes/neurun/backend/acl_cl/KernelGenerator.h new file mode 100644 index 0000000..6c70e1d --- /dev/null +++ b/runtimes/neurun/backend/acl_cl/KernelGenerator.h @@ -0,0 +1,105 @@ +/* + * 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_BACKEND_ACL_CL_KERNEL_GENERATOR_H__ +#define __NEURUN_BACKEND_ACL_CL_KERNEL_GENERATOR_H__ + +#include + +#include "model/Operands.h" +#include "TensorBuilder.h" + +namespace neurun +{ +namespace backend +{ +namespace acl_cl +{ + +class KernelGenerator : public IKernelGenerator +{ +public: + KernelGenerator(const neurun::model::Operands &ctx, + const std::shared_ptr &tensor_builder); + + std::shared_ptr tensor_builder() override { return _tensor_builder; } + + void visit(const model::operation::Conv2DNode &) override; + void visit(const model::operation::DepthwiseConv2DNode &) override; + void visit(const model::operation::MaxPool2DNode &) override; + void visit(const model::operation::AvgPool2DNode &) override; + void visit(const model::operation::ConcatNode &) override; + void visit(const model::operation::FullyConnectedNode &) override; + void visit(const model::operation::MulNode &) override; + void visit(const model::operation::ReduceSumNode &) override; + void visit(const model::operation::ReshapeNode &) override; + void visit(const model::operation::SqueezeNode &) override; + void visit(const model::operation::TanhNode &) override; + void visit(const model::operation::SoftmaxNode &) override; + void visit(const model::operation::StridedSliceNode &) override; + void visit(const model::operation::TransposeNode &) override; + void visit(const model::operation::AddNode &) override; + void visit(const model::operation::SubNode &) override; + void visit(const model::operation::CastNode &) override; + void visit(const model::operation::DivNode &) override; + void visit(const model::operation::ExpNode &) override; + void visit(const model::operation::LogisticNode &) override; + void visit(const model::operation::ReduceMaxNode &) override; + void visit(const model::operation::ComparisonNode &) override; + void visit(const model::operation::LogicalAndNode &) override; + void visit(const model::operation::LSTMNode &) override; + void visit(const model::operation::RSQRTNode &) override; + void visit(const model::operation::ReLUNode &) override; + void visit(const model::operation::ResizeBilinearNode &) override; + void visit(const model::operation::ReLU1Node &) override; + void visit(const model::operation::ReLU6Node &) override; + void visit(const model::operation::RNNNode &) override; + void visit(const model::operation::FloorNode &) override; + void visit(const model::operation::SpaceToDepthNode &) override; + void visit(const model::operation::L2Pool2DNode &) override; + void visit(const model::operation::EmbeddingLookupNode &) override; + void visit(const model::operation::L2NormalizationNode &) override; + void visit(const model::operation::HashtableLookupNode &) override; + void visit(const model::operation::PReLUNode &) override; + void visit(const model::operation::TransposeConvNode &) override; + void visit(const model::operation::SQRTNode &) override; + void visit(const model::operation::LogicalOrNode &) override; + void visit(const model::operation::LogicalNotNode &) override; + void visit(const model::operation::SquaredDifferenceNode &) override; + void visit(const model::operation::TopKV2Node &) override; + void visit(const model::operation::GatherNode &) override; + void visit(const model::operation::NegNode &) override; + void visit(const model::operation::AbsNode &) override; + void visit(const model::operation::ArgMaxNode &) override; + void visit(const model::operation::DequantizeNode &) override; + void visit(const model::operation::MeanNode &) override; + void visit(const model::operation::LocalResponseNormalizationNode &) override; + void visit(const model::operation::DepthToSpaceNode &) override; + void visit(const model::operation::ReduceMinNode &) override; + void visit(const model::operation::SplitNode &) override; + void visit(const model::operation::UnpackNode &) override; + void visit(const model::operation::PadNode &) override; + +private: + const neurun::model::Operands &_ctx; + std::shared_ptr _tensor_builder; +}; + +} // namespace acl_cl +} // namespace backend +} // namespace neurun + +#endif // __NEURUN_BACKEND_ACL_CL_KERNEL_GENERATOR_H__ diff --git a/runtimes/neurun/backend/acl_neon/Backend.h b/runtimes/neurun/backend/acl_neon/Backend.h index 57110db..9b1c7d7 100644 --- a/runtimes/neurun/backend/acl_neon/Backend.h +++ b/runtimes/neurun/backend/acl_neon/Backend.h @@ -22,6 +22,7 @@ #include #include "Config.h" +#include "KernelGenerator.h" #include "StageGenerator.h" #include "ShapeFixer.h" #include "MemoryManager.h" @@ -41,6 +42,7 @@ public: std::make_shared(createMemoryManager())) : ::neurun::backend::Backend{std::make_shared(), std::make_shared(operand_ctx, tensor_builder), + std::make_shared(operand_ctx, tensor_builder), std::make_shared(operand_ctx, tensor_builder)} { // DO NOTHING diff --git a/runtimes/neurun/backend/acl_neon/KernelGenerator.cc b/runtimes/neurun/backend/acl_neon/KernelGenerator.cc new file mode 100644 index 0000000..62029c9 --- /dev/null +++ b/runtimes/neurun/backend/acl_neon/KernelGenerator.cc @@ -0,0 +1,88 @@ +/* + * 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 "KernelGenerator.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "kernel/ConcatLayer.h" +#include "util/Padding.h" +#include "model/Index.h" +#include "model/DataType.h" +#include "model/InternalType.h" +#include "compiler/IExecutionBuilder.h" +#include "exec/NopFunction.h" +#include "util/logging.h" +#include "util/Utils.h" +#include "acl_common/Convert.h" +#include "acl_common/Swizzle.h" + +using ::neurun::compiler::IExecutionBuilder; + +namespace neurun +{ +namespace backend +{ +namespace acl_neon +{ + +using ::neurun::backend::acl_common::asAclFunction; + +// +// KernelGenerator +// +KernelGenerator::KernelGenerator(const neurun::model::Operands &ctx, + const std::shared_ptr &tensor_builder) + : _ctx(ctx), _tensor_builder(tensor_builder) +{ + // DO NOTHING +} + +void KernelGenerator::visit(const model::operation::Conv2DNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::DepthwiseConv2DNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::MaxPool2DNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::AvgPool2DNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::ConcatNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::FullyConnectedNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::MulNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::ReshapeNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::TanhNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::SoftmaxNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::AddNode & /*node*/) {} + +} // namespace acl_neon +} // namespace backend +} // namespace neurun diff --git a/runtimes/neurun/backend/acl_neon/KernelGenerator.h b/runtimes/neurun/backend/acl_neon/KernelGenerator.h new file mode 100644 index 0000000..3e1ad59 --- /dev/null +++ b/runtimes/neurun/backend/acl_neon/KernelGenerator.h @@ -0,0 +1,61 @@ +/* + * 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_BACKEND_ACL_NEON_KERNEL_GENERATOR_H__ +#define __NEURUN_BACKEND_ACL_NEON_KERNEL_GENERATOR_H__ + +#include + +#include "model/Operands.h" +#include "TensorBuilder.h" + +namespace neurun +{ +namespace backend +{ +namespace acl_neon +{ + +class KernelGenerator : public IKernelGenerator +{ +public: + KernelGenerator(const neurun::model::Operands &ctx, + const std::shared_ptr &tensor_builder); + + std::shared_ptr tensor_builder() override { return _tensor_builder; } + + void visit(const model::operation::Conv2DNode &) override; + void visit(const model::operation::DepthwiseConv2DNode &) override; + void visit(const model::operation::MaxPool2DNode &) override; + void visit(const model::operation::AvgPool2DNode &) override; + void visit(const model::operation::ConcatNode &) override; + void visit(const model::operation::FullyConnectedNode &) override; + void visit(const model::operation::MulNode &) override; + void visit(const model::operation::ReshapeNode &) override; + void visit(const model::operation::TanhNode &) override; + void visit(const model::operation::SoftmaxNode &) override; + void visit(const model::operation::AddNode &) override; + +private: + const neurun::model::Operands &_ctx; + std::shared_ptr _tensor_builder; +}; + +} // namespace acl_neon +} // namespace backend +} // namespace neurun + +#endif // __NEURUN_BACKEND_ACL_NEON_KERNEL_GENERATOR_H__ diff --git a/runtimes/neurun/backend/cpu/Backend.h b/runtimes/neurun/backend/cpu/Backend.h index fac1dcb..ddb6656 100644 --- a/runtimes/neurun/backend/cpu/Backend.h +++ b/runtimes/neurun/backend/cpu/Backend.h @@ -22,6 +22,7 @@ #include #include "Config.h" +#include "KernelGenerator.h" #include "StageGenerator.h" #include "ShapeFixer.h" @@ -39,6 +40,7 @@ public: std::shared_ptr tensor_builder = std::make_shared()) : ::neurun::backend::Backend{std::make_shared(), std::make_shared(operand_ctx, tensor_builder), + std::make_shared(operand_ctx, tensor_builder), std::make_shared(operand_ctx, tensor_builder)} { // DO NOTHING diff --git a/runtimes/neurun/backend/cpu/KernelGenerator.cc b/runtimes/neurun/backend/cpu/KernelGenerator.cc new file mode 100644 index 0000000..391b2b1 --- /dev/null +++ b/runtimes/neurun/backend/cpu/KernelGenerator.cc @@ -0,0 +1,80 @@ +/* + * 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 "KernelGenerator.h" + +#include + +#include "cpp14/memory.h" +#include "util/Padding.h" +#include "kernel/OperationUtils.h" +#include "kernel/ConvolutionLayer.h" +#include "kernel/AvgPoolLayer.h" +#include "kernel/MaxPoolLayer.h" +#include "kernel/ConcatLayer.h" +#include "kernel/FullyConnectedLayer.h" +#include "kernel/ReshapeLayer.h" +#include "kernel/SoftMaxLayer.h" +#include "kernel/PermuteLayer.h" +#include "kernel/DepthwiseConvolutionLayer.h" +#include "kernel/AddLayer.h" + +#include +#include + +#include "util/logging.h" + +#include "util/Utils.h" + +namespace neurun +{ +namespace backend +{ +namespace cpu +{ + +KernelGenerator::KernelGenerator(const neurun::model::Operands &operand_ctx, + const std::shared_ptr &tensor_builder) + : _ctx(operand_ctx), _tensor_builder(tensor_builder) +{ + // DO NOTHING +} + +void KernelGenerator::visit(const model::operation::Conv2DNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::DepthwiseConv2DNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::MaxPool2DNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::AvgPool2DNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::ConcatNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::FullyConnectedNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::MulNode &) {} + +void KernelGenerator::visit(const model::operation::ReshapeNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::SoftmaxNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::AddNode & /*node*/) {} + +void KernelGenerator::visit(const model::operation::PermuteNode & /*node*/) {} + +} // namespace cpu +} // namespace backend +} // namespace neurun diff --git a/runtimes/neurun/backend/cpu/KernelGenerator.h b/runtimes/neurun/backend/cpu/KernelGenerator.h new file mode 100644 index 0000000..1e1f48a --- /dev/null +++ b/runtimes/neurun/backend/cpu/KernelGenerator.h @@ -0,0 +1,61 @@ +/* + * 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_BACKEND_CPU_KERNEL_GENERATOR_H__ +#define __NEURUN_BACKEND_CPU_KERNEL_GENERATOR_H__ + +#include "backend/IKernelGenerator.h" +#include "model/Operands.h" +#include "operand/Tensor.h" +#include "TensorBuilder.h" + +namespace neurun +{ +namespace backend +{ +namespace cpu +{ + +class KernelGenerator : public IKernelGenerator +{ +public: + KernelGenerator(const neurun::model::Operands &ctx, + const std::shared_ptr &tensor_builder); + + std::shared_ptr tensor_builder() override { return _tensor_builder; } + + void visit(const model::operation::Conv2DNode &) override; + void visit(const model::operation::DepthwiseConv2DNode &) override; + void visit(const model::operation::MaxPool2DNode &) override; + void visit(const model::operation::AvgPool2DNode &) override; + void visit(const model::operation::ConcatNode &) override; + void visit(const model::operation::FullyConnectedNode &) 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::AddNode &) override; + void visit(const model::operation::PermuteNode &) override; + +private: + const neurun::model::Operands &_ctx; + std::shared_ptr _tensor_builder; +}; + +} // namespace cpu +} // namespace backend +} // namespace neurun + +#endif // __NEURUN_BACKEND_CPU_KERNEL_GENERATOR_H__ diff --git a/runtimes/neurun/backend/hi_perf_cpu/KernelGenerator.cc b/runtimes/neurun/backend/hi_perf_cpu/KernelGenerator.cc new file mode 100644 index 0000000..a13fe12 --- /dev/null +++ b/runtimes/neurun/backend/hi_perf_cpu/KernelGenerator.cc @@ -0,0 +1,18 @@ +/* + * 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 "KernelGenerator.h" +// to force compilation diff --git a/runtimes/neurun/backend/hi_perf_cpu/KernelGenerator.h b/runtimes/neurun/backend/hi_perf_cpu/KernelGenerator.h new file mode 100644 index 0000000..c714b69 --- /dev/null +++ b/runtimes/neurun/backend/hi_perf_cpu/KernelGenerator.h @@ -0,0 +1,49 @@ +/* + * 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_BACKEND_HI_PERF_CPU_KERNEL_GENERATOR_H__ +#define __NEURUN_BACKEND_HI_PERF_CPU_KERNEL_GENERATOR_H__ + +#include + +#include "model/Operands.h" +#include "TensorBuilder.h" + +namespace neurun +{ +namespace backend +{ +namespace hi_perf_cpu +{ + +class KernelGenerator : public IKernelGenerator +{ +public: + KernelGenerator(const neurun::model::Operands &ctx, + const std::shared_ptr &tensor_builder); + std::shared_ptr tensor_builder() override; + // TODO add more ops + +private: + const neurun::model::Operands &_ctx; + std::shared_ptr _tensor_builder; +}; + +} // namespace hi_perf_cpu +} // namespace backend +} // namespace neurun + +#endif // __NEURUN_BACKEND_HI_PERF_CPU_KERNEL_GENERATOR_H__ diff --git a/runtimes/neurun/core/include/backend/Backend.h b/runtimes/neurun/core/include/backend/Backend.h index 4a1f57a..6054edd 100644 --- a/runtimes/neurun/core/include/backend/Backend.h +++ b/runtimes/neurun/core/include/backend/Backend.h @@ -26,6 +26,7 @@ namespace backend struct IConfig; class IStageGenerator; +class IKernelGenerator; class IShapeFixer; struct ITensorBuilder; @@ -34,9 +35,10 @@ class Backend public: Backend(const std::shared_ptr &backend_config, const std::shared_ptr &stage_gen, + const std::shared_ptr &kernel_gen, const std::shared_ptr &shape_fixer); - Backend(void) : _config(nullptr), _stage_gen(nullptr), _shape_fixer(nullptr) + Backend(void) : _config(nullptr), _stage_gen(nullptr), _kernel_gen(nullptr), _shape_fixer(nullptr) { // DO NOTHING } @@ -44,12 +46,14 @@ public: public: const std::shared_ptr config() const; const std::shared_ptr stage_gen() const; + const std::shared_ptr kernel_gen() const; const std::shared_ptr shape_fixer() const; const std::shared_ptr tensor_builder() const; private: std::shared_ptr _config; std::shared_ptr _stage_gen; + std::shared_ptr _kernel_gen; std::shared_ptr _shape_fixer; }; diff --git a/runtimes/neurun/core/include/backend/IKernelGenerator.h b/runtimes/neurun/core/include/backend/IKernelGenerator.h new file mode 100644 index 0000000..98ebd55 --- /dev/null +++ b/runtimes/neurun/core/include/backend/IKernelGenerator.h @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2018 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_BACKEND_IKERNEL_GENERATOR_H__ +#define __NEURUN_BACKEND_IKERNEL_GENERATOR_H__ + +#include +#include + +#include "ITensorBuilder.h" +#include "compiler/IExecutionBuilder.h" +#include "model/OperationVisitor.h" +#include "model/Subgraph.h" +#include "cpp14/memory.h" + +namespace neurun +{ +namespace backend +{ + +class IKernelGenerator : model::OperationVisitor +{ +public: + virtual ~IKernelGenerator() = default; + + virtual std::shared_ptr tensor_builder() = 0; + + void generate(const model::Operation &node, neurun::compiler::IExecutionBuilder *executionBuilder) + { + _execution_builder = executionBuilder; + node.accept(*this); + } + +protected: +#define OP(InternalName, IsNnApi) \ + virtual void visit(const model::operation::InternalName &) override \ + { \ + throw std::runtime_error("NYI"); \ + } +#include "model/Operations.lst" +#undef OP + +protected: + neurun::compiler::IExecutionBuilder *_execution_builder; +}; + +} // namespace backend +} // namespace neurun + +#endif // __NEURUN_BACKEND_IKERNEL_GENERATOR_H__ diff --git a/runtimes/neurun/core/src/backend/Backend.cc b/runtimes/neurun/core/src/backend/Backend.cc index babfe39..b588606 100644 --- a/runtimes/neurun/core/src/backend/Backend.cc +++ b/runtimes/neurun/core/src/backend/Backend.cc @@ -19,6 +19,7 @@ #include "backend/IConfig.h" #include "backend/ITensorBuilder.h" #include "backend/IStageGenerator.h" +#include "backend/IKernelGenerator.h" #include "backend/IShapeFixer.h" namespace neurun @@ -28,8 +29,10 @@ namespace backend Backend::Backend(const std::shared_ptr &backend_config, const std::shared_ptr &stage_gen, + const std::shared_ptr &kernel_gen, const std::shared_ptr &shape_fixer) - : _config(backend_config), _stage_gen(stage_gen), _shape_fixer(shape_fixer) + : _config(backend_config), _stage_gen(stage_gen), _kernel_gen(kernel_gen), + _shape_fixer(shape_fixer) { backend_config->initialize(); } @@ -41,6 +44,11 @@ const std::shared_ptr Backend::stage_gen() con return _stage_gen; } +const std::shared_ptr Backend::kernel_gen() const +{ + return _kernel_gen; +} + const std::shared_ptr Backend::shape_fixer() const { return _shape_fixer; diff --git a/runtimes/neurun/core/src/compiler/ExecutorFactory.cc b/runtimes/neurun/core/src/compiler/ExecutorFactory.cc index 2b30ca8..7d09606 100644 --- a/runtimes/neurun/core/src/compiler/ExecutorFactory.cc +++ b/runtimes/neurun/core/src/compiler/ExecutorFactory.cc @@ -28,6 +28,7 @@ #include "OperationValidator.h" #include "SubTensorAnalyzer.h" #include "PlanBuilder.h" +#include "backend/IKernelGenerator.h" #include "backend/IShapeFixer.h" #include "ConstantInitializer.h" #include "cpp14/memory.h" @@ -78,8 +79,8 @@ exec::IExecutor *ExecutorFactory::createLinearExecutor(graph::Graph &graph) // SubTensorInfo should be generated after lower, before stage generation and finalize // because SubTensorAnalyzer assume that insert permutation is already finished // lower: decide backend and insert permutation - // stage generation: prepare codegen to optimization - // finalize: generate tensor using subtensor info, then execute stage + // fix shapes: prepare codegen to optimization + // finalize: generate tensor using subtensor info, then generate kernels // Generated SubTensorInfo is in operand(Object) // for easy pass SubTensorInfo to plan builder and tensor builder linear->accept(SubTensorAnalyzer{graph.operands()}); @@ -215,16 +216,22 @@ exec::IExecutor *ExecutorFactory::createDataflowExecutor(graph::Graph &graph, bo exec::DataflowExecutor::CodeMap _code_map; }; - ExecutionBuilder execution_builder; + auto execution_builder = nnfw::cpp14::make_unique(); - // Generate and process stages + // Generate and process stages, generate kernels + // TODO: when code will be moved from StageGenerator to KernelGenerator - remove StageGenerator + // related code graph.subg_ctx().iterate( [&](const model::SubgraphIndex &subg_index, const model::Subgraph &subg) { auto backend = graph.getLowerInfo(subg_index)->backend(); - auto stage_gen = backend->stage_gen(); // TODO This approach is temporal. See declaration of `setNextIndex`. - execution_builder.setNextIndex(subg_index); - (*stage_gen->generate(subg))(execution_builder); + execution_builder->setNextIndex(subg_index); + + auto stage_gen = backend->stage_gen(); + (*stage_gen->generate(subg))(*execution_builder); + + auto kernel_gen = backend->kernel_gen(); + kernel_gen->generate(subg, execution_builder.get()); }); for (const auto &tensor_builder : tensor_builders) @@ -248,14 +255,14 @@ exec::IExecutor *ExecutorFactory::createDataflowExecutor(graph::Graph &graph, bo return new exec::ParallelExecutor{ graph.shareModel(), std::move(graph.releaseSubgraphContext()), operand_context, std::move(lower_info), - std::move(mem_mgrs), std::move(execution_builder.releaseCodeMap())}; + std::move(mem_mgrs), std::move(execution_builder->releaseCodeMap())}; } else { auto exec = new exec::DataflowExecutor{ graph.shareModel(), std::move(graph.releaseSubgraphContext()), operand_context, std::move(lower_info), - std::move(mem_mgrs), std::move(execution_builder.releaseCodeMap())}; + std::move(mem_mgrs), std::move(execution_builder->releaseCodeMap())}; if (util::ConfigManager::instance().get(util::config::PROFILING_MODE)) { auto et = std::make_shared(graph.backend_resolver()->getAllBackends()); diff --git a/runtimes/neurun/core/src/compiler/PlanBuilder.cc b/runtimes/neurun/core/src/compiler/PlanBuilder.cc index 691b41a..22f0707 100644 --- a/runtimes/neurun/core/src/compiler/PlanBuilder.cc +++ b/runtimes/neurun/core/src/compiler/PlanBuilder.cc @@ -19,6 +19,7 @@ #include "backend/operand/IObject.h" #include "linear/Linear.h" #include "backend/Backend.h" +#include "backend/IKernelGenerator.h" namespace neurun { @@ -45,12 +46,18 @@ void PlanBuilder::finalize(const linear::Linear *linear, }); } - // Generate and process stages - ExecutionBuilder execution_builder{_operations}; + // Generate and process stages and generate kernels + // TODO: when code will be moved from StageGenerator to KernelGenerator - remove StageGenerator + // related code + auto execution_builder = nnfw::cpp14::make_unique(_operations); linear->iterate([&](const linear::Element &element) { auto backend = element.lower_info->backend(); + auto stage_gen = backend->stage_gen(); - (*stage_gen->generate(*element.subgraph))(execution_builder); + (*stage_gen->generate(*element.subgraph))(*execution_builder); + + auto kernel_gen = backend->kernel_gen(); + kernel_gen->generate(*element.subgraph, execution_builder.get()); }); // Allocate Tensor Memory for cl_tensors diff --git a/runtimes/neurun/test/core/backend/ExecTime.test.cc b/runtimes/neurun/test/core/backend/ExecTime.test.cc index 6c5275c..be02b4a 100644 --- a/runtimes/neurun/test/core/backend/ExecTime.test.cc +++ b/runtimes/neurun/test/core/backend/ExecTime.test.cc @@ -17,6 +17,7 @@ #include "backend/ExecTime.h" #include "backend/IConfig.h" #include "backend/IStageGenerator.h" +#include "backend/IKernelGenerator.h" #include "backend/IShapeFixer.h" #include "backend/Backend.h" #include @@ -40,6 +41,11 @@ struct MockStageGenerator : IStageGenerator std::shared_ptr tensor_builder() final { return nullptr; } }; +struct MockKernelGenerator : IKernelGenerator +{ + std::shared_ptr tensor_builder() final { return nullptr; } +}; + struct MockShapeFixer : IShapeFixer { std::shared_ptr tensor_builder() final { return nullptr; } @@ -52,7 +58,7 @@ struct MockBackend : public ::neurun::backend::Backend */ MockBackend() : Backend{std::make_shared(), std::make_shared(), - std::make_shared()} {}; + std::make_shared(), std::make_shared()} {}; }; TEST(ExecTime, roundtrip_ok) -- 2.7.4