From a779eac70843c6f848001942cbcfff99b4df4285 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EC=9D=B4=ED=95=9C=EC=A2=85/=EB=8F=99=EC=9E=91=EC=A0=9C?= =?utf8?q?=EC=96=B4Lab=28SR=29/Engineer/=EC=82=BC=EC=84=B1=EC=A0=84?= =?utf8?q?=EC=9E=90?= Date: Thu, 15 Nov 2018 11:41:50 +0900 Subject: [PATCH] [neurun] Introduce Model class (#3586) Model class is a container of actual operands/operations and model's inputs/outputs. Signed-off-by: Hanjoung Lee --- runtimes/neurun/src/graph/Graph.cc | 32 +++++++++++++++--------------- runtimes/neurun/src/graph/Graph.h | 23 +++++++++------------- runtimes/neurun/src/graph/Model.h | 40 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 30 deletions(-) create mode 100644 runtimes/neurun/src/graph/Model.h diff --git a/runtimes/neurun/src/graph/Graph.cc b/runtimes/neurun/src/graph/Graph.cc index ce51ebe..d263fed 100644 --- a/runtimes/neurun/src/graph/Graph.cc +++ b/runtimes/neurun/src/graph/Graph.cc @@ -42,32 +42,32 @@ Graph::~Graph(void) = default; operand::Index Graph::addOperand(const operand::Shape &shape, const operand::TypeInfo &type) { - return _operands.append(shape, type); + return _model.operands.append(shape, type); } operation::Index Graph::addOperation(std::unique_ptr &&node) { assert(isBuildingPhase()); - return _operations.append(std::move(node)); + return _model.operations.append(std::move(node)); } void Graph::setOperandValue(const operand::Index &ind, std::unique_ptr &&data) { assert(isBuildingPhase()); - assert(_operands.exist(ind)); - _operands.at(ind).data(std::move(data)); + assert(_model.operands.exist(ind)); + _model.operands.at(ind).data(std::move(data)); } void Graph::addInput(const operand::Index &ind) { assert(isBuildingPhase()); - _inputs.append(ind); + _model.inputs.append(ind); } void Graph::addOutput(const operand::Index &ind) { assert(isBuildingPhase()); - _outputs.append(ind); + _model.outputs.append(ind); } void Graph::finishBuilding(void) @@ -94,14 +94,14 @@ void Graph::lower(void) // operand::LowerInfo holder std::unordered_map> operands_lower_info; - _operands.iterate([&](const operand::Index &index, const operand::Object &object) { + _model.operands.iterate([&](const operand::Index &index, const operand::Object &object) { operands_lower_info[index] = nnfw::make_unique(operand::asShape4D(object.shape())); }); - _backend_resolver = nnfw::make_unique(_operands); + _backend_resolver = nnfw::make_unique(_model.operands); - _operations.iterate([&](const operation::Index &, operation::Node &node) { + _model.operations.iterate([&](const operation::Index &, operation::Node &node) { auto backend = _backend_resolver->getBackend(typeid(node)); // Operation LowerInfo @@ -135,7 +135,7 @@ void Graph::lower(void) // Add DefBackend constants same as UseBackend // NOTE This assumes a constant operand is used by only one operation - _operations.iterate([&](const operation::Index &, operation::Node &node) { + _model.operations.iterate([&](const operation::Index &, operation::Node &node) { // LowerInfo for input operands for (auto operand : node.getInputs()) { @@ -148,7 +148,7 @@ void Graph::lower(void) }); // Set LowerInfo for each operand from the operand::LowerInfo holder - _operands.iterate([&](const operand::Index &index, operand::Object &object) { + _model.operands.iterate([&](const operand::Index &index, operand::Object &object) { object.lower_info(std::move(operands_lower_info[index])); // Dump operand LowerInfo @@ -269,7 +269,7 @@ template class Graph::PostDfsIterator; template void Graph::DefaultIterator::iterate(GraphRef graph, const IterFn &fn) const { - graph._operations.iterate([&](const operation::Index &, NodeRef node) -> void { fn(node); }); + graph.operations().iterate([&](const operation::Index &, NodeRef node) -> void { fn(node); }); } // @@ -281,7 +281,7 @@ void Graph::PostDfsIterator::iterate(GraphRef graph, const IterFn &fn) { assert(!graph.isBuildingPhase()); // Restrict iteration condition - std::vector visited(graph._operations.size(), false); + std::vector visited(graph.operations().size(), false); std::function dfs_recursive = [&](const operation::Index &index, NodeRef node) -> void { @@ -291,17 +291,17 @@ void Graph::PostDfsIterator::iterate(GraphRef graph, const IterFn &fn) for (auto output : node.getOutputs()) { - const auto &operand = graph._operands.at(output); + const auto &operand = graph.operands().at(output); for (const auto &use : operand.getUses().list()) { - dfs_recursive(use, graph._operations.at(use)); + dfs_recursive(use, graph.operations().at(use)); } } fn(node); }; - graph._operations.iterate(dfs_recursive); + graph.operations().iterate(dfs_recursive); // All of the operations(nodes) must have been visited. assert(std::all_of(visited.begin(), visited.end(), [](bool v) { return v; })); diff --git a/runtimes/neurun/src/graph/Graph.h b/runtimes/neurun/src/graph/Graph.h index de7ee34..e5ca1ec 100644 --- a/runtimes/neurun/src/graph/Graph.h +++ b/runtimes/neurun/src/graph/Graph.h @@ -20,9 +20,7 @@ #include #include "graph/operation/Node.h" -#include "graph/operation/Set.h" -#include "graph/operand/IndexSet.h" -#include "graph/operand/Set.h" +#include "graph/Model.h" namespace neurun { @@ -116,21 +114,18 @@ private: // Accessors public: - const operand::IndexSet &getInputs() const { return _inputs; } - const operand::IndexSet &getOutputs() const { return _outputs; } - operand::IndexSet &getOutputs() { return _outputs; } - const operand::Set &operands() const { return _operands; } - operand::Set &operands() { return _operands; } // TODO Remove this non-const accessor - const operation::Set &operations() const { return _operations; } - operation::Set &operations() { return _operations; } + const operand::IndexSet &getInputs() const { return _model.inputs; } + const operand::IndexSet &getOutputs() const { return _model.outputs; } + operand::IndexSet &getOutputs() { return _model.outputs; } + const operand::Set &operands() const { return _model.operands; } + operand::Set &operands() { return _model.operands; } // TODO Remove this non-const accessor + const operation::Set &operations() const { return _model.operations; } + operation::Set &operations() { return _model.operations; } const codegen::BackendResolver *backend_resolver() const { return _backend_resolver.get(); } private: Phase _phase{Phase::BUILDING}; - operation::Set _operations; - operand::Set _operands; - operand::IndexSet _inputs; - operand::IndexSet _outputs; + Model _model; // For LOWERED phase private: diff --git a/runtimes/neurun/src/graph/Model.h b/runtimes/neurun/src/graph/Model.h new file mode 100644 index 0000000..e3d9f73 --- /dev/null +++ b/runtimes/neurun/src/graph/Model.h @@ -0,0 +1,40 @@ +/* + * 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_GRAPH_MODEL_H__ +#define __NEURUN_GRAPH_MODEL_H__ + +#include "graph/operation/Set.h" +#include "graph/operand/IndexSet.h" +#include "graph/operand/Set.h" + +namespace neurun +{ +namespace graph +{ + +struct Model +{ + operation::Set operations; + operand::Set operands; + operand::IndexSet inputs; + operand::IndexSet outputs; +}; + +} // namespace graph +} // namespace neurun + +#endif // __NEURUN_GRAPH_MODEL_H__ -- 2.7.4