[neurun] Move the ownership of LowerInfoMap (#4504)
author김용섭/On-Device Lab(SR)/Engineer/삼성전자 <yons.kim@samsung.com>
Wed, 27 Feb 2019 01:35:38 +0000 (10:35 +0900)
committer오형석/On-Device Lab(SR)/Staff Engineer/삼성전자 <hseok82.oh@samsung.com>
Wed, 27 Feb 2019 01:35:38 +0000 (10:35 +0900)
Move the ownership of LowerInfoMap from Graph to Linear

Signed-off-by: Yongseop Kim <yons.kim@samsung.com>
runtimes/neurun/src/compiler/Compiler.cc
runtimes/neurun/src/compiler/ConstantInitializer.cc
runtimes/neurun/src/compiler/ConstantInitializer.h
runtimes/neurun/src/graph/Graph.cc
runtimes/neurun/src/linear/Linear.cc
runtimes/neurun/src/linear/Linear.h

index e837329..03bb7c6 100644 (file)
@@ -112,14 +112,14 @@ void Compiler::compile(void)
   // TODO Add optimization passes
   plan_builder.finalize(tensor_builders);
 
-  ConstantInitializer{*_model, *operand_context}();
+  ConstantInitializer{*_model, *operand_context, *linear->getLowerInfo()}();
 
   /********************************
    * Code generation phase finished
    ********************************/
   auto plan = std::make_shared<Plan>(operand_context, operation_sequence);
   _executor =
-      std::make_shared<exec::Executor>(_model->shareModel(), _model->releaseLowerInfo(), plan);
+      std::make_shared<exec::Executor>(_model->shareModel(), linear->releaseLowerInfo(), plan);
   _state = State::COMPILED;
 }
 
index b95e264..1455224 100644 (file)
 #include "util/feature/nchw/View.h"
 #include "misc/feature/IndexIterator.h"
 #include "util/logging.h"
-#include "graph/operand/LowerInfo.h"
+#include "graph/LowerInfoMap.h"
 
 namespace neurun
 {
 namespace compiler
 {
 
-ConstantInitializer::ConstantInitializer(const graph::Graph &graph, operand::Context &operands)
-    : _graph{graph}, _operands{operands}
+ConstantInitializer::ConstantInitializer(const graph::Graph &graph, operand::Context &operands,
+                                         const graph::LowerInfoMap &lower_info_map)
+    : _graph{graph}, _operands{operands}, _lower_info_map{lower_info_map}
 {
 }
 
@@ -43,8 +44,15 @@ void ConstantInitializer::run(const model::operand::Index &ind,
                               const neurun::model::operand::Object &model_obj)
 {
   neurun::model::operand::Index index(ind);
-  auto layout =
-      _graph.getLowerInfo(ind)->def_backends().getOnlyElement()->config()->getOperandLayout();
+
+  graph::operand::LowerInfo *lower_info = nullptr;
+  {
+    auto itr = _lower_info_map.operand.find(ind);
+    lower_info = itr->second.get();
+    assert(lower_info);
+  }
+
+  auto layout = lower_info->def_backends().getOnlyElement()->config()->getOperandLayout();
   const auto shape = model_obj.shape();
   auto base = reinterpret_cast<const T *>(model_obj.data().base());
   auto size = model_obj.data().size();
index 6da14d0..8ecc21d 100644 (file)
 
 namespace neurun
 {
+namespace graph
+{
+
+struct LowerInfoMap;
+
+} // namespace graph
+} // namespace neurun
+
+namespace neurun
+{
 namespace compiler
 {
 
 class ConstantInitializer
 {
 public:
-  ConstantInitializer(const graph::Graph &graph, operand::Context &operands);
+  // TODO Change std::shared_ptr<Model> instead of Graph
+  ConstantInitializer(const graph::Graph &graph, operand::Context &operands,
+                      const graph::LowerInfoMap &lower_info_map);
 
   void operator()();
 
@@ -40,6 +52,7 @@ private:
 private:
   const graph::Graph &_graph;
   operand::Context &_operands;
+  const graph::LowerInfoMap &_lower_info_map;
 };
 
 } // namespace compiler
index 706941f..50acab8 100644 (file)
@@ -213,7 +213,7 @@ std::unique_ptr<linear::Linear> Graph::linearize(void)
 {
   assert(_phase == Phase::MODEL);
 
-  auto linear = nnfw::cpp14::make_unique<linear::Linear>(*this);
+  auto linear = nnfw::cpp14::make_unique<linear::Linear>(*this, releaseLowerInfo());
 
   // TODO Move the operations and operands to linear object
   return std::move(linear);
index 197bf8c..3ff73a6 100644 (file)
@@ -35,8 +35,11 @@ namespace neurun
 namespace linear
 {
 
-Linear::Linear(const graph::Graph &graph) : _graph(graph)
+Linear::Linear(const graph::Graph &graph, std::unique_ptr<graph::LowerInfoMap> lower_info_map)
+    : _graph(graph), _lower_info_map(std::move(lower_info_map))
 {
+  assert(_lower_info_map);
+
   // TODO: Move this code to graph
 
   // Linearize graph with subgraphs by topological sort while assuming that
@@ -131,7 +134,7 @@ Linear::Linear(const graph::Graph &graph) : _graph(graph)
   {
     // Assume that the backend of all nodes on a subgraph are identified on the subgraph
     const auto &first_ind = subgraph->operations()[0].index;
-    auto lower_info = _graph.getLowerInfo(first_ind);
+    auto lower_info = getLowerInfo(first_ind);
     _elements.emplace_back(std::move(subgraph), lower_info);
   }
 
@@ -155,8 +158,8 @@ backend::TensorBuilderSet Linear::planTensors()
       std::function<void(const model::operand::Index &ind, ITensorBuilderPtr)>;
 
   const auto &graph = _graph;
-  auto iterTensorBuilders = [&graph](const model::operand::Index &ind, FnOnTensorBuilder fn) {
-    const auto lower_info = graph.getLowerInfo(ind);
+  auto iterTensorBuilders = [this, &graph](const model::operand::Index &ind, FnOnTensorBuilder fn) {
+    const auto lower_info = getLowerInfo(ind);
     for (auto backend : lower_info->def_backends())
     {
       auto tensor_builder = backend->tensor_builder();
@@ -171,7 +174,7 @@ backend::TensorBuilderSet Linear::planTensors()
 
   _graph.operands().iterate(
       [&](const model::operand::Index &ind, const model::operand::Object &obj) {
-        const auto lower_info = graph.getLowerInfo(ind);
+        const auto lower_info = getLowerInfo(ind);
         uses_map[ind] = obj.getUses().size();
 
         // If a tensor is a constant, increase the use of the tensor.
@@ -289,5 +292,25 @@ void Linear::iterate(const std::function<void(const Element &element)> &fn) cons
   }
 }
 
+const graph::operation::LowerInfo *Linear::getLowerInfo(const model::operation::Index &index) const
+{
+  if (!_lower_info_map)
+    return nullptr;
+  auto itr = _lower_info_map->operation.find(index);
+  if (itr == _lower_info_map->operation.end())
+    return nullptr;
+  return itr->second.get();
+}
+
+const graph::operand::LowerInfo *Linear::getLowerInfo(const model::operand::Index &index) const
+{
+  if (!_lower_info_map)
+    return nullptr;
+  auto itr = _lower_info_map->operand.find(index);
+  if (itr == _lower_info_map->operand.end())
+    return nullptr;
+  return itr->second.get();
+}
+
 } // namespace linear
 } // namespace neurun
index 6c0d873..b513db4 100644 (file)
@@ -22,6 +22,7 @@
 
 #include "model/operation/Subgraph.h"
 #include "backend/interface/ITensorBuilder.h"
+#include "graph/LowerInfoMap.h"
 
 namespace neurun
 {
@@ -65,7 +66,8 @@ struct Element
 class Linear
 {
 public:
-  Linear(const graph::Graph &graph);
+  // TODO Change std::shared_ptr<Model> instead of Graph
+  Linear(const graph::Graph &graph, std::unique_ptr<graph::LowerInfoMap> lower_info_map);
 
 public:
   Linear(const Linear &linear) = delete;
@@ -78,8 +80,19 @@ public:
 
   void iterate(const std::function<void(const Element &element)> &fn) const;
 
+  std::unique_ptr<graph::LowerInfoMap> releaseLowerInfo() { return std::move(_lower_info_map); }
+
+  graph::LowerInfoMap *getLowerInfo() { return _lower_info_map.get(); }
+
+private:
+  // TODO Replace these getLowerInfo methods with ones of LowerInfoMap in the future
+  const graph::operation::LowerInfo *getLowerInfo(const model::operation::Index &index) const;
+
+  const graph::operand::LowerInfo *getLowerInfo(const model::operand::Index &index) const;
+
 private:
   const graph::Graph &_graph;
+  std::unique_ptr<graph::LowerInfoMap> _lower_info_map;
   std::vector<Element> _elements;
 };