From 0f14bfef4671cdaf38ee8814c8e17fd53757f3fa 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: Fri, 19 Jul 2019 15:40:42 +0900 Subject: [PATCH] Apply refactored ConstantInitializers (#5713) This commit applies refactored ConstantInitialzers. - Introduce ConstantInitializer into each backend. - Replace legacy ConstantInitializer with refactored ConstantInitializer. Signed-off-by: jiseob.jang --- runtimes/neurun/backend/acl_cl/Backend.h | 9 ++++++--- runtimes/neurun/backend/acl_cl/ConstantInitializer.cc | 1 + runtimes/neurun/backend/acl_neon/Backend.h | 9 ++++++--- .../neurun/backend/acl_neon/ConstantInitializer.cc | 1 + runtimes/neurun/backend/cpu/Backend.h | 9 ++++++--- runtimes/neurun/backend/cpu/ConstantInitializer.cc | 1 + runtimes/neurun/core/include/backend/Backend.h | 8 +++++++- .../neurun/core/include/backend/IConstantInitializer.h | 6 +----- runtimes/neurun/core/src/backend/Backend.cc | 9 ++++++++- runtimes/neurun/core/src/compiler/ExecutorFactory.cc | 18 ++++++++++++++---- runtimes/neurun/core/src/compiler/PlanBuilder.cc | 3 +++ runtimes/neurun/test/core/backend/ExecTime.test.cc | 10 ++++++++-- 12 files changed, 62 insertions(+), 22 deletions(-) diff --git a/runtimes/neurun/backend/acl_cl/Backend.h b/runtimes/neurun/backend/acl_cl/Backend.h index 567388f..c82cf03 100644 --- a/runtimes/neurun/backend/acl_cl/Backend.h +++ b/runtimes/neurun/backend/acl_cl/Backend.h @@ -22,6 +22,7 @@ #include #include "Config.h" +#include "ConstantInitializer.h" #include "KernelGenerator.h" #include "ShapeFixer.h" #include "MemoryManager.h" @@ -39,9 +40,11 @@ public: Backend(const neurun::model::Operands &operand_ctx, std::shared_ptr tensor_builder = std::make_shared(createMemoryManager())) - : ::neurun::backend::Backend{std::make_shared(), - std::make_shared(operand_ctx, tensor_builder), - std::make_shared(operand_ctx, tensor_builder)} + : ::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/ConstantInitializer.cc b/runtimes/neurun/backend/acl_cl/ConstantInitializer.cc index 255340a..ffaf51a 100644 --- a/runtimes/neurun/backend/acl_cl/ConstantInitializer.cc +++ b/runtimes/neurun/backend/acl_cl/ConstantInitializer.cc @@ -41,6 +41,7 @@ void ConstantInitializer::run() auto tensor_obj = _tensor_builder->wrapTensor(ind); fn(model_obj, *tensor_obj); } + _init_map.clear(); } void ConstantInitializer::visit(const model::operation::AbsNode &node) diff --git a/runtimes/neurun/backend/acl_neon/Backend.h b/runtimes/neurun/backend/acl_neon/Backend.h index 434c801..d710e47 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 "ConstantInitializer.h" #include "KernelGenerator.h" #include "ShapeFixer.h" #include "MemoryManager.h" @@ -39,9 +40,11 @@ public: Backend(const neurun::model::Operands &operand_ctx, std::shared_ptr tensor_builder = std::make_shared(createMemoryManager())) - : ::neurun::backend::Backend{std::make_shared(), - std::make_shared(operand_ctx, tensor_builder), - std::make_shared(operand_ctx, tensor_builder)} + : ::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/ConstantInitializer.cc b/runtimes/neurun/backend/acl_neon/ConstantInitializer.cc index 4f9e224..7366582 100644 --- a/runtimes/neurun/backend/acl_neon/ConstantInitializer.cc +++ b/runtimes/neurun/backend/acl_neon/ConstantInitializer.cc @@ -41,6 +41,7 @@ void ConstantInitializer::run() auto tensor_obj = _tensor_builder->wrapTensor(ind); fn(model_obj, *tensor_obj); } + _init_map.clear(); } void ConstantInitializer::visit(const model::operation::AddNode &node) diff --git a/runtimes/neurun/backend/cpu/Backend.h b/runtimes/neurun/backend/cpu/Backend.h index 9d47a92..47168e8 100644 --- a/runtimes/neurun/backend/cpu/Backend.h +++ b/runtimes/neurun/backend/cpu/Backend.h @@ -22,6 +22,7 @@ #include #include "Config.h" +#include "ConstantInitializer.h" #include "KernelGenerator.h" #include "ShapeFixer.h" @@ -37,9 +38,11 @@ class Backend : public ::neurun::backend::Backend public: Backend(const neurun::model::Operands &operand_ctx, 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)} + : ::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/ConstantInitializer.cc b/runtimes/neurun/backend/cpu/ConstantInitializer.cc index 8769cc8..8997933 100644 --- a/runtimes/neurun/backend/cpu/ConstantInitializer.cc +++ b/runtimes/neurun/backend/cpu/ConstantInitializer.cc @@ -41,6 +41,7 @@ void ConstantInitializer::run() auto tensor_obj = _tensor_builder->wrapTensor(ind); fn(model_obj, *tensor_obj); } + _init_map.clear(); } void ConstantInitializer::visit(const model::operation::AddNode &node) diff --git a/runtimes/neurun/core/include/backend/Backend.h b/runtimes/neurun/core/include/backend/Backend.h index fdcd5a6..b9b33bd 100644 --- a/runtimes/neurun/core/include/backend/Backend.h +++ b/runtimes/neurun/core/include/backend/Backend.h @@ -25,6 +25,7 @@ namespace backend { struct IConfig; +class IConstantInitializer; class IKernelGenerator; class IShapeFixer; struct ITensorBuilder; @@ -33,22 +34,27 @@ class Backend { public: Backend(const std::shared_ptr &backend_config, + const std::shared_ptr &constant_initializer, const std::shared_ptr &kernel_gen, const std::shared_ptr &shape_fixer); - Backend(void) : _config(nullptr), _kernel_gen(nullptr), _shape_fixer(nullptr) + Backend(void) + : _config(nullptr), _constant_initializer{nullptr}, _kernel_gen(nullptr), + _shape_fixer(nullptr) { // DO NOTHING } public: const std::shared_ptr config() const; + const std::shared_ptr constant_initializer() 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 _constant_initializer; std::shared_ptr _kernel_gen; std::shared_ptr _shape_fixer; }; diff --git a/runtimes/neurun/core/include/backend/IConstantInitializer.h b/runtimes/neurun/core/include/backend/IConstantInitializer.h index 63fbb4b..0545391 100644 --- a/runtimes/neurun/core/include/backend/IConstantInitializer.h +++ b/runtimes/neurun/core/include/backend/IConstantInitializer.h @@ -207,11 +207,7 @@ public: public: using Initializer = std::function; - void generate(const model::Subgraph &subg) - { - _init_map.clear(); - subg.accept(*this); - } + void generate(const model::Subgraph &subg) { subg.accept(*this); } protected: #define OP(InternalName, IsNnApi) \ diff --git a/runtimes/neurun/core/src/backend/Backend.cc b/runtimes/neurun/core/src/backend/Backend.cc index 4ba86da..28e3f8e 100644 --- a/runtimes/neurun/core/src/backend/Backend.cc +++ b/runtimes/neurun/core/src/backend/Backend.cc @@ -27,15 +27,22 @@ namespace backend { Backend::Backend(const std::shared_ptr &backend_config, + const std::shared_ptr &constant_initializer, const std::shared_ptr &kernel_gen, const std::shared_ptr &shape_fixer) - : _config(backend_config), _kernel_gen(kernel_gen), _shape_fixer(shape_fixer) + : _config(backend_config), _constant_initializer{constant_initializer}, _kernel_gen(kernel_gen), + _shape_fixer(shape_fixer) { backend_config->initialize(); } const std::shared_ptr Backend::config() const { return _config; } +const std::shared_ptr Backend::constant_initializer() const +{ + return _constant_initializer; +} + const std::shared_ptr Backend::kernel_gen() const { return _kernel_gen; diff --git a/runtimes/neurun/core/src/compiler/ExecutorFactory.cc b/runtimes/neurun/core/src/compiler/ExecutorFactory.cc index e76088e..4328c00 100644 --- a/runtimes/neurun/core/src/compiler/ExecutorFactory.cc +++ b/runtimes/neurun/core/src/compiler/ExecutorFactory.cc @@ -28,9 +28,9 @@ #include "OperationValidator.h" #include "SubTensorAnalyzer.h" #include "PlanBuilder.h" +#include "backend/IConstantInitializer.h" #include "backend/IKernelGenerator.h" #include "backend/IShapeFixer.h" -#include "ConstantInitializer.h" #include "cpp14/memory.h" namespace neurun @@ -107,7 +107,11 @@ exec::IExecutor *ExecutorFactory::createLinearExecutor(graph::Graph &graph) // TODO Add optimization passes plan_builder.finalize(linear.get(), tensor_builders); - ConstantInitializer{graph, *operand_context, *linear->getLowerInfo()}(); + // Initialize constant tensors + for (const auto backend : graph.backend_resolver()->getAllBackends()) + { + backend->constant_initializer()->run(); + } // Prepare each MemoryManager on each backend auto mem_mgrs = nnfw::cpp14::make_unique(); @@ -221,6 +225,8 @@ exec::IExecutor *ExecutorFactory::createDataflowExecutor(graph::Graph &graph, bo graph.subgraphs().iterate( [&](const model::SubgraphIndex &subg_index, const model::Subgraph &subg) { auto backend = graph.getLowerInfo(subg_index)->backend(); + auto constant_initializer = backend->constant_initializer(); + constant_initializer->generate(subg); // TODO This approach is temporal. See declaration of `setNextIndex`. execution_builder->setNextIndex(subg_index); auto kernel_gen = backend->kernel_gen(); @@ -232,9 +238,13 @@ exec::IExecutor *ExecutorFactory::createDataflowExecutor(graph::Graph &graph, bo tensor_builder->allocate(); } - auto lower_info = graph.releaseLowerInfo(); + // Initialize constant tensors + for (const auto backend : graph.backend_resolver()->getAllBackends()) + { + backend->constant_initializer()->run(); + } - ConstantInitializer{graph, *operand_context, *lower_info}(); + auto lower_info = graph.releaseLowerInfo(); // Prepare each MemoryManager on each backend auto mem_mgrs = nnfw::cpp14::make_unique(); diff --git a/runtimes/neurun/core/src/compiler/PlanBuilder.cc b/runtimes/neurun/core/src/compiler/PlanBuilder.cc index e3958c7..fbef2cc 100644 --- a/runtimes/neurun/core/src/compiler/PlanBuilder.cc +++ b/runtimes/neurun/core/src/compiler/PlanBuilder.cc @@ -18,6 +18,7 @@ #include "backend/operand/IObject.h" #include "backend/Backend.h" +#include "backend/IConstantInitializer.h" #include "backend/IKernelGenerator.h" #include "linear/Linear.h" @@ -45,6 +46,8 @@ void PlanBuilder::finalize(const linear::Linear *linear, auto execution_builder = nnfw::cpp14::make_unique(_functions); linear->iterate([&](const linear::Element &element) { auto backend = element.lower_info->backend(); + auto constant_initializer = backend->constant_initializer(); + constant_initializer->generate(*element.subgraph); auto kernel_gen = backend->kernel_gen(); kernel_gen->generate(*element.subgraph, execution_builder.get()); }); diff --git a/runtimes/neurun/test/core/backend/ExecTime.test.cc b/runtimes/neurun/test/core/backend/ExecTime.test.cc index 334b45f..b9d511c 100644 --- a/runtimes/neurun/test/core/backend/ExecTime.test.cc +++ b/runtimes/neurun/test/core/backend/ExecTime.test.cc @@ -16,6 +16,7 @@ #include "backend/ExecTime.h" #include "backend/IConfig.h" +#include "backend/IConstantInitializer.h" #include "backend/IKernelGenerator.h" #include "backend/IShapeFixer.h" #include "backend/Backend.h" @@ -35,6 +36,11 @@ struct MockConfig : public IConfig bool SupportSubTensorAlloc() override { return false; } }; +struct MockConstantInitializer : IConstantInitializer +{ + void run() {} +}; + struct MockKernelGenerator : IKernelGenerator { std::shared_ptr tensor_builder() final { return nullptr; } @@ -51,8 +57,8 @@ struct MockBackend : public ::neurun::backend::Backend * Required because we use pointers to backends instead of string identifiers. */ MockBackend() - : Backend{std::make_shared(), std::make_shared(), - std::make_shared()} {}; + : Backend{std::make_shared(), std::make_shared(), + std::make_shared(), std::make_shared()} {}; }; TEST(ExecTime, roundtrip_ok) -- 2.7.4