From 1436d871001f9dc677ea25f73d3c654a10ae2746 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EC=9D=B4=ED=95=9C=EC=A2=85/On-Device=20Lab=28SR=29/Enginee?= =?utf8?q?r/=EC=82=BC=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Wed, 27 Mar 2019 13:52:16 +0900 Subject: [PATCH] [neurun] Reuse code in Executor::execute (#4869) Reuse setting inputs and outputs part with template method pattern. Signed-off-by: Hanjoung Lee --- runtimes/neurun/core/src/exec/DataflowExecutor.cc | 38 ++------------------- runtimes/neurun/core/src/exec/DataflowExecutor.h | 2 +- runtimes/neurun/core/src/exec/Executor.cc | 38 +-------------------- runtimes/neurun/core/src/exec/Executor.h | 2 +- runtimes/neurun/core/src/exec/ExecutorBase.cc | 40 +++++++++++++++++++++++ runtimes/neurun/core/src/exec/ExecutorBase.h | 5 ++- 6 files changed, 49 insertions(+), 76 deletions(-) diff --git a/runtimes/neurun/core/src/exec/DataflowExecutor.cc b/runtimes/neurun/core/src/exec/DataflowExecutor.cc index 85ad420..2778ff0 100644 --- a/runtimes/neurun/core/src/exec/DataflowExecutor.cc +++ b/runtimes/neurun/core/src/exec/DataflowExecutor.cc @@ -80,30 +80,9 @@ DataflowExecutor::DataflowExecutor(const std::shared_ptr &mo } } -void DataflowExecutor::execute() +void DataflowExecutor::executeImpl() { - // Set input(s) - for (uint32_t n = 0; n < _model->inputs.size(); ++n) - { - auto setter = [&](::neurun::backend::operand::ITensor &tensor) { - _sources.at(n)->push(tensor); - }; - - neurun::model::operand::IO::Index input_index{n}; - - ::neurun::model::operand::Index index{_model->inputs.at(input_index)}; - const auto operand_li = _lower_info->operand.at(index).get(); - if (operand_li->def_backends().empty()) - { - // This input is not used (i.e. constant, EX. reshape's axis) - continue; - } - - auto object = _operand_context->at(index); - - object->access(setter); - } - + // TODO Fix unnecessary indentation // Execution { assert(_waiting_jobs.empty()); @@ -133,19 +112,6 @@ void DataflowExecutor::execute() assert(_waiting_jobs.empty()); } - - // Get output(s) - for (uint32_t n = 0; n < _model->outputs.size(); ++n) - { - auto getter = [&](::neurun::backend::operand::ITensor &tensor) { _sinks.at(n)->pull(tensor); }; - - neurun::model::operand::IO::Index output_index{n}; - - ::neurun::model::operand::Index index{_model->outputs.at(output_index)}; - auto object = _operand_context->at(index); - - object->access(getter); - } } } // namespace exec diff --git a/runtimes/neurun/core/src/exec/DataflowExecutor.h b/runtimes/neurun/core/src/exec/DataflowExecutor.h index b47b145..30b0e04 100644 --- a/runtimes/neurun/core/src/exec/DataflowExecutor.h +++ b/runtimes/neurun/core/src/exec/DataflowExecutor.h @@ -55,7 +55,7 @@ public: const std::shared_ptr &operand_context, std::unique_ptr lower_info, CodeMap &&code_map); - void execute() override; + void executeImpl() override; private: CodeMap _code_map; diff --git a/runtimes/neurun/core/src/exec/Executor.cc b/runtimes/neurun/core/src/exec/Executor.cc index 3c129f8..e04ff03 100644 --- a/runtimes/neurun/core/src/exec/Executor.cc +++ b/runtimes/neurun/core/src/exec/Executor.cc @@ -26,49 +26,13 @@ namespace neurun namespace exec { -void Executor::execute() +void Executor::executeImpl() { - // Set input(s) - for (uint32_t n = 0; n < _model->inputs.size(); ++n) - { - auto setter = [&](::neurun::backend::operand::ITensor &tensor) { - _sources.at(n)->push(tensor); - }; - - neurun::model::operand::IO::Index input_index{n}; - - ::neurun::model::operand::Index index{_model->inputs.at(input_index)}; - auto operand_li = _lower_info->operand.at(index).get(); - if (operand_li->def_backends().size() == 0) - { - // This input is not used (EX. reshape's axis) - continue; - } - - auto object = _operand_context->at(index); - - object->access(setter); - } - - // Execution const auto &operations = _plan->operations(); for (uint32_t n = 0; n < operations.size(); ++n) { operations.at(n).run(); } - - // Get output(s) - for (uint32_t n = 0; n < _model->outputs.size(); ++n) - { - auto getter = [&](::neurun::backend::operand::ITensor &tensor) { _sinks.at(n)->pull(tensor); }; - - neurun::model::operand::IO::Index output_index{n}; - - ::neurun::model::operand::Index index{_model->outputs.at(output_index)}; - auto object = _operand_context->at(index); - - object->access(getter); - } } } // namespace exec diff --git a/runtimes/neurun/core/src/exec/Executor.h b/runtimes/neurun/core/src/exec/Executor.h index 12078d8..425d1af 100644 --- a/runtimes/neurun/core/src/exec/Executor.h +++ b/runtimes/neurun/core/src/exec/Executor.h @@ -51,7 +51,7 @@ public: } public: - virtual void execute(void) override; + void executeImpl(void) override; private: std::shared_ptr _plan; diff --git a/runtimes/neurun/core/src/exec/ExecutorBase.cc b/runtimes/neurun/core/src/exec/ExecutorBase.cc index d3cf0ea..2039467 100644 --- a/runtimes/neurun/core/src/exec/ExecutorBase.cc +++ b/runtimes/neurun/core/src/exec/ExecutorBase.cc @@ -92,5 +92,45 @@ void ExecutorBase::setOutput(const model::operand::IO::Index &index, } } +void ExecutorBase::execute() +{ + // Set input(s) + for (uint32_t n = 0; n < _model->inputs.size(); ++n) + { + auto setter = [&](::neurun::backend::operand::ITensor &tensor) { + _sources.at(n)->push(tensor); + }; + + neurun::model::operand::IO::Index input_index{n}; + + ::neurun::model::operand::Index index{_model->inputs.at(input_index)}; + const auto operand_li = _lower_info->operand.at(index).get(); + if (operand_li->def_backends().empty()) + { + // This input is not used (i.e. constant, EX. reshape's axis) + continue; + } + + auto object = _operand_context->at(index); + + object->access(setter); + } + + executeImpl(); + + // Get output(s) + for (uint32_t n = 0; n < _model->outputs.size(); ++n) + { + auto getter = [&](::neurun::backend::operand::ITensor &tensor) { _sinks.at(n)->pull(tensor); }; + + neurun::model::operand::IO::Index output_index{n}; + + ::neurun::model::operand::Index index{_model->outputs.at(output_index)}; + auto object = _operand_context->at(index); + + object->access(getter); + } +} + } // namespace exec } // namespace neurun diff --git a/runtimes/neurun/core/src/exec/ExecutorBase.h b/runtimes/neurun/core/src/exec/ExecutorBase.h index a427c31..1d1a8fe 100644 --- a/runtimes/neurun/core/src/exec/ExecutorBase.h +++ b/runtimes/neurun/core/src/exec/ExecutorBase.h @@ -52,7 +52,10 @@ public: virtual void setOutput(const model::operand::IO::Index &index, const model::operand::TypeInfo &type, const model::operand::Shape &shape, void *buffer, size_t length) override; - virtual void execute(void) = 0; + + void execute(void) final; + + virtual void executeImpl(void) = 0; private: template -- 2.7.4