[neurun] Introduce Model class (#3586)
author이한종/동작제어Lab(SR)/Engineer/삼성전자 <hanjoung.lee@samsung.com>
Thu, 15 Nov 2018 02:41:50 +0000 (11:41 +0900)
committer오형석/동작제어Lab(SR)/Staff Engineer/삼성전자 <hseok82.oh@samsung.com>
Thu, 15 Nov 2018 02:41:50 +0000 (11:41 +0900)
Model class is a container of actual operands/operations and model's
inputs/outputs.

Signed-off-by: Hanjoung Lee <hanjoung.lee@samsung.com>
runtimes/neurun/src/graph/Graph.cc
runtimes/neurun/src/graph/Graph.h
runtimes/neurun/src/graph/Model.h [new file with mode: 0644]

index ce51ebe..d263fed 100644 (file)
@@ -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<operation::Node> &&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<operand::Data> &&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<operand::Index, std::unique_ptr<operand::LowerInfo>> 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::LowerInfo>(operand::asShape4D(object.shape()));
     });
 
-    _backend_resolver = nnfw::make_unique<codegen::BackendResolver>(_operands);
+    _backend_resolver = nnfw::make_unique<codegen::BackendResolver>(_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<false>;
 template <bool is_const>
 void Graph::DefaultIterator<is_const>::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<is_const>::iterate(GraphRef graph, const IterFn &fn)
 {
   assert(!graph.isBuildingPhase()); // Restrict iteration condition
 
-  std::vector<bool> visited(graph._operations.size(), false);
+  std::vector<bool> visited(graph.operations().size(), false);
 
   std::function<void(const operation::Index &, NodeRef)> dfs_recursive =
       [&](const operation::Index &index, NodeRef node) -> void {
@@ -291,17 +291,17 @@ void Graph::PostDfsIterator<is_const>::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; }));
index de7ee34..e5ca1ec 100644 (file)
@@ -20,9 +20,7 @@
 #include <functional>
 
 #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 (file)
index 0000000..e3d9f73
--- /dev/null
@@ -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__