From 7c1e98cb53b6490eb07c6294f46a29e8ce1dc0f9 Mon Sep 17 00:00:00 2001 From: Sergei Barannikov/AI Tools Lab /SRR/Engineer/Samsung Electronics Date: Thu, 28 Nov 2019 14:57:52 +0300 Subject: [PATCH] [neurun] Avoid using Model in ExecManager (#9260) Pass Graph instead of Model to ExecManager. Signed-off-by: Sergei Barannikov --- runtime/neurun/core/src/compiler/Compiler.cc | 2 +- runtime/neurun/core/src/exec/interp/ExecManager.cc | 16 ++--- runtime/neurun/core/src/exec/interp/ExecManager.h | 9 ++- .../neurun/test/core/exec/interp/ExecManager.cc | 79 +++++++++++----------- 4 files changed, 54 insertions(+), 52 deletions(-) diff --git a/runtime/neurun/core/src/compiler/Compiler.cc b/runtime/neurun/core/src/compiler/Compiler.cc index aef9514..7335828 100644 --- a/runtime/neurun/core/src/compiler/Compiler.cc +++ b/runtime/neurun/core/src/compiler/Compiler.cc @@ -52,7 +52,7 @@ void Compiler::compile(void) if (!checkCompilable()) { - _executor = std::make_shared(_graph->shareModel()); + _executor = std::make_shared(*_graph); return; } diff --git a/runtime/neurun/core/src/exec/interp/ExecManager.cc b/runtime/neurun/core/src/exec/interp/ExecManager.cc index 96f503e..82e2fd9 100644 --- a/runtime/neurun/core/src/exec/interp/ExecManager.cc +++ b/runtime/neurun/core/src/exec/interp/ExecManager.cc @@ -38,10 +38,10 @@ void ExecManager::execute(const IODescription &desc) ***********************************************************************/ model::OperandIndexMap> tensor_map; - for (uint32_t n = 0; n < _model->inputs.size(); n++) + for (uint32_t n = 0; n < _graph.getInputs().size(); n++) { neurun::model::IOIndex index{n}; - const auto input_index = _model->inputs.at(index); + const auto input_index = _graph.getInputs().at(index); const auto &input = *desc.inputs.at(n); auto input_tensor = std::make_shared(input.info); @@ -50,10 +50,10 @@ void ExecManager::execute(const IODescription &desc) tensor_map[input_index] = input_tensor; } - for (uint32_t n = 0; n < _model->outputs.size(); n++) + for (uint32_t n = 0; n < _graph.getOutputs().size(); n++) { neurun::model::IOIndex index{n}; - const auto output_index = _model->outputs.at(index); + const auto output_index = _graph.getOutputs().at(index); const auto &output = *desc.outputs.at(n); auto output_tensor = std::make_shared(output.info); @@ -67,10 +67,10 @@ void ExecManager::execute(const IODescription &desc) Execution environment will be assigned to invoked interpreter instance ***********************************************************************/ - std::unique_ptr interp_env = nnfw::cpp14::make_unique(_model); + std::unique_ptr interp_env = nnfw::cpp14::make_unique(_graph.shareModel()); // Assign input tensor into interpreter execution environment - for (auto index : _model->inputs) + for (auto index : _graph.getInputs()) { if (tensor_map.find(index) != tensor_map.end()) { @@ -80,7 +80,7 @@ void ExecManager::execute(const IODescription &desc) } // Assign output tensor into interpreter execution environment - for (auto index : _model->outputs) + for (auto index : _graph.getOutputs()) { if (tensor_map.find(index) != tensor_map.end()) { @@ -90,7 +90,7 @@ void ExecManager::execute(const IODescription &desc) } // Allocate constant tensor - _model->operands.iterate([&](const model::OperandIndex &ind, const model::Operand &obj) { + _graph.operands().iterate([&](const model::OperandIndex &ind, const model::Operand &obj) { if (obj.isConstant()) { VERBOSE(INTERPRETER) << "Allocate and assign constant tensor. operand index:" << ind.value() diff --git a/runtime/neurun/core/src/exec/interp/ExecManager.h b/runtime/neurun/core/src/exec/interp/ExecManager.h index 77486dc..53a5316 100644 --- a/runtime/neurun/core/src/exec/interp/ExecManager.h +++ b/runtime/neurun/core/src/exec/interp/ExecManager.h @@ -22,8 +22,7 @@ #ifndef __NEURUN_EXEC_INTERP_EXEC_MANAGER_H_ #define __NEURUN_EXEC_INTERP_EXEC_MANAGER_H_ -#include "model/OperandIndexMap.h" -#include "model/OperationIndexMap.h" +#include "graph/Graph.h" #include "exec/IExecutor.h" #include "Tensor.h" @@ -40,7 +39,7 @@ namespace interp class ExecManager final : public IExecutor { public: - ExecManager(const std::shared_ptr &model) : _model{model} + explicit ExecManager(const graph::Graph &graph) : _graph(graph) { // DO NOTHING } @@ -50,7 +49,7 @@ public: * @brief Return graph model * @return Graph model */ - const model::Model &model() override { return *_model; } + const model::Model &model() override { return *_graph.shareModel(); } void setIndexedRanks(std::shared_ptr>) override{ // Not implemented }; @@ -61,7 +60,7 @@ public: void execute(const IODescription &desc) final; private: - std::shared_ptr _model; + const graph::Graph &_graph; model::OperandIndexMap> _tensor_map; }; diff --git a/runtime/neurun/test/core/exec/interp/ExecManager.cc b/runtime/neurun/test/core/exec/interp/ExecManager.cc index f02fb04..e66be39 100644 --- a/runtime/neurun/test/core/exec/interp/ExecManager.cc +++ b/runtime/neurun/test/core/exec/interp/ExecManager.cc @@ -44,7 +44,8 @@ protected: // model output: add result // lhs, rhs, result shape: {1, 2, 2, 1} // activation: none (constant) - std::unique_ptr model = nnfw::cpp14::make_unique(); + auto model = nnfw::cpp14::make_unique(); + _graph = nnfw::cpp14::make_unique(std::move(model)); // Add operands @@ -53,9 +54,9 @@ protected: Shape shape_scalar(0); TypeInfo type_scalar{DataType::INT32}; - auto operand_lhs = model->operands.emplace(shape, type); - auto operand_rhs = model->operands.emplace(shape, type); - auto operand_result = model->operands.emplace(shape, type); + auto operand_lhs = _graph->addOperand(shape, type); + auto operand_rhs = _graph->addOperand(shape, type); + auto operand_result = _graph->addOperand(shape, type); // Add operations @@ -63,18 +64,17 @@ protected: param.activation = neurun::model::Activation::NONE; auto input_set = OperandIndexSequence{operand_lhs, operand_rhs}; auto output_set = OperandIndexSequence{operand_result}; - model->operations.push(nnfw::cpp14::make_unique(input_set, output_set, param)); + _graph->addOperation(nnfw::cpp14::make_unique(input_set, output_set, param)); // Identify model inputs and outputs - model->inputs.append(operand_lhs); - model->inputs.append(operand_rhs); - model->outputs.append(operand_result); + _graph->getInputs().append(operand_lhs); + _graph->getInputs().append(operand_rhs); + _graph->getOutputs().append(operand_result); - _graph = nnfw::cpp14::make_unique<::neurun::graph::Graph>(std::move(model)); _graph->finishBuilding(); - _executor = nnfw::cpp14::make_unique(_graph->shareModel()); + _executor = nnfw::cpp14::make_unique(*_graph); } void CreateTwoStepModel() @@ -87,7 +87,8 @@ protected: // result2 <= (result1 + rhs2) // lhs, rhs1, rh2, result1, result2 shape: {1, 2, 2, 1} // activation: none (constant) - std::unique_ptr model = nnfw::cpp14::make_unique(); + auto model = nnfw::cpp14::make_unique(); + _graph = nnfw::cpp14::make_unique(std::move(model)); // 1st add operands (result1 <= lhs + rhs1) @@ -98,12 +99,13 @@ protected: static int32_t rhs2_data[4] = {3, 1, -1, 5}; - auto operand_lhs = model->operands.emplace(shape, type); - auto operand_rhs1 = model->operands.emplace(shape, type); - auto operand_result1 = model->operands.emplace(shape, type); - auto operand_rhs2 = model->operands.emplace(shape, type); - auto operand_result2 = model->operands.emplace(shape, type); - model->operands.at(operand_rhs2) + auto operand_lhs = _graph->addOperand(shape, type); + auto operand_rhs1 = _graph->addOperand(shape, type); + auto operand_result1 = _graph->addOperand(shape, type); + auto operand_rhs2 = _graph->addOperand(shape, type); + auto operand_result2 = _graph->addOperand(shape, type); + _graph->operands() + .at(operand_rhs2) .data(nnfw::cpp14::make_unique(reinterpret_cast(&rhs2_data), 16)); @@ -113,26 +115,23 @@ protected: param1.activation = neurun::model::Activation::NONE; auto input_set1 = OperandIndexSequence{operand_lhs, operand_rhs1}; auto output_set1 = OperandIndexSequence{operand_result1}; - model->operations.push( - nnfw::cpp14::make_unique(input_set1, output_set1, param1)); + _graph->addOperation(nnfw::cpp14::make_unique(input_set1, output_set1, param1)); operation::Add::Param param2; param2.activation = neurun::model::Activation::NONE; auto input_set2 = OperandIndexSequence{operand_result1, operand_rhs2}; auto output_set2 = OperandIndexSequence{operand_result2}; - model->operations.push( - nnfw::cpp14::make_unique(input_set2, output_set2, param2)); + _graph->addOperation(nnfw::cpp14::make_unique(input_set2, output_set2, param2)); // Identify model inputs and outputs - model->inputs.append(operand_lhs); - model->inputs.append(operand_rhs1); - model->outputs.append(operand_result2); + _graph->getInputs().append(operand_lhs); + _graph->getInputs().append(operand_rhs1); + _graph->getOutputs().append(operand_result2); - _graph = nnfw::cpp14::make_unique<::neurun::graph::Graph>(std::move(model)); _graph->finishBuilding(); - _executor = nnfw::cpp14::make_unique(_graph->shareModel()); + _executor = nnfw::cpp14::make_unique(*_graph); } void CreateUnspecifiedDimensionsModel() @@ -142,7 +141,8 @@ protected: // model output: add result // lhs, rhs, result shape: {1, unknown, 2, 1} // activation: none (constant) - std::unique_ptr model = nnfw::cpp14::make_unique(); + auto model = nnfw::cpp14::make_unique(); + _graph = nnfw::cpp14::make_unique(std::move(model)); // Add operands @@ -151,15 +151,16 @@ protected: Shape shape_scalar(0); TypeInfo type_scalar{DataType::INT32}; - auto operand_lhs = model->operands.emplace(shape, type); - auto operand_rhs = model->operands.emplace(shape, type); + auto operand_lhs = _graph->addOperand(shape, type); + auto operand_rhs = _graph->addOperand(shape, type); - auto operand_activation = model->operands.emplace(shape_scalar, type_scalar); - model->operands.at(operand_activation) + auto operand_activation = _graph->addOperand(shape_scalar, type_scalar); + _graph->operands() + .at(operand_activation) .data(nnfw::cpp14::make_unique( reinterpret_cast(&_activation_value), 4)); - auto operand_result = model->operands.emplace(shape, type); + auto operand_result = _graph->addOperand(shape, type); // Add operations @@ -167,17 +168,17 @@ protected: param.activation = neurun::model::Activation::NONE; auto input_set = OperandIndexSequence{operand_lhs, operand_rhs}; auto output_set = OperandIndexSequence{operand_result}; + _graph->addOperation(nnfw::cpp14::make_unique(input_set, output_set, param)); // Identify model inputs and outputs - model->inputs.append(operand_lhs); - model->inputs.append(operand_rhs); - model->outputs.append(operand_result); + _graph->getInputs().append(operand_lhs); + _graph->getInputs().append(operand_rhs); + _graph->getOutputs().append(operand_result); - _graph = nnfw::cpp14::make_unique<::neurun::graph::Graph>(std::move(model)); _graph->finishBuilding(); - _executor = nnfw::cpp14::make_unique(_graph->shareModel()); + _executor = nnfw::cpp14::make_unique(*_graph); } void createExecution() { _execution = nnfw::cpp14::make_unique(_executor); } @@ -192,7 +193,9 @@ protected: TEST_F(InterpExecManagerTest, create_empty) { - _executor = nnfw::cpp14::make_unique(std::make_shared()); + neurun::graph::Graph graph(nnfw::cpp14::make_unique()); + graph.finishBuilding(); + _executor = nnfw::cpp14::make_unique(graph); ASSERT_NE(_executor, nullptr); } -- 2.7.4