[neurun] Replace graph with model in Linear (#4519)
author김용섭/On-Device Lab(SR)/Engineer/삼성전자 <yons.kim@samsung.com>
Tue, 5 Mar 2019 07:33:05 +0000 (16:33 +0900)
committer박세희/On-Device Lab(SR)/Principal Engineer/삼성전자 <saehie.park@samsung.com>
Tue, 5 Mar 2019 07:33:05 +0000 (16:33 +0900)
Replace Graph with Model in Linear so that this would make Linear not
dependent on Graph

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

index 9ad5759..c435f4f 100644 (file)
@@ -215,8 +215,8 @@ std::unique_ptr<linear::Linear> Graph::linearize(void)
 {
   assert(_phase == Phase::MODEL);
 
-  auto linear =
-      nnfw::cpp14::make_unique<linear::Linear>(*this, releaseSubgraphSet(), releaseLowerInfo());
+  auto linear = nnfw::cpp14::make_unique<linear::Linear>(shareModel(), releaseSubgraphSet(),
+                                                         releaseLowerInfo());
 
   // TODO Move the operations and operands to linear object
   return std::move(linear);
index cc79cbc..1fd4d3c 100644 (file)
@@ -18,8 +18,6 @@
 
 #include "Linear.h"
 
-#include "graph/Graph.h"
-
 #include "graph/operation/LowerInfo.h"
 #include "graph/operand/LowerInfo.h"
 #include "backend/interface/IStageGenerator.h"
@@ -35,12 +33,12 @@ namespace neurun
 namespace linear
 {
 
-Linear::Linear(const graph::Graph &graph,
+Linear::Linear(const std::shared_ptr<const model::Model> &model,
                std::unique_ptr<std::vector<std::unique_ptr<model::operation::Subgraph>>> subg_set,
                std::unique_ptr<graph::LowerInfoMap> lower_info_map)
-    : _graph(graph), _subg_set(std::move(subg_set)), _lower_info_map(std::move(lower_info_map))
+    : _model(model), _subg_set(std::move(subg_set)), _lower_info_map(std::move(lower_info_map))
 {
-  assert(_subg_set && _lower_info_map);
+  assert(_model && _subg_set && _lower_info_map);
   for (const auto &subg : *_subg_set)
   {
     // Assume that the lower_infos of all nodes on a subgraph are identified on the subgraph
@@ -63,8 +61,7 @@ backend::TensorBuilderSet Linear::planTensors()
   using FnOnTensorBuilder =
       std::function<void(const model::operand::Index &ind, ITensorBuilderPtr)>;
 
-  const auto &graph = _graph;
-  auto iterTensorBuilders = [this, &graph](const model::operand::Index &ind, FnOnTensorBuilder fn) {
+  auto iterTensorBuilders = [this](const model::operand::Index &ind, FnOnTensorBuilder fn) {
     const auto lower_info = getLowerInfo(ind);
     for (auto backend : lower_info->def_backends())
     {
@@ -78,7 +75,7 @@ backend::TensorBuilderSet Linear::planTensors()
   model::operand::IndexMap<uint32_t> uses_map;
   std::vector<model::operand::Index> constants;
 
-  _graph.operands().iterate(
+  _model->operands.iterate(
       [&](const model::operand::Index &ind, const model::operand::Object &obj) {
         const auto lower_info = getLowerInfo(ind);
         uses_map[ind] = obj.getUses().size();
@@ -123,7 +120,8 @@ backend::TensorBuilderSet Linear::planTensors()
 
   // If a tensor is model output, increase the use of the tensor.
   // This aim is same to above one.
-  for (const auto &ind : _graph.getOutputs())
+  // for (const auto &ind : _graph.getOutputs())
+  for (const auto &ind : _model->outputs)
   {
     uses_map[ind]++;
   }
@@ -139,7 +137,7 @@ backend::TensorBuilderSet Linear::planTensors()
 
   // Allocate Model's inputs
   VERBOSE(LINEAR) << "TENSORS as MODEL INPUT" << std::endl;
-  for (const auto &ind : _graph.getInputs())
+  for (const auto &ind : _model->inputs)
   {
     iterTensorBuilders(ind, [](const model::operand::Index &ind, ITensorBuilderPtr tensor_builder) {
       tensor_builder->notifyFirstUse(ind);
@@ -150,7 +148,7 @@ backend::TensorBuilderSet Linear::planTensors()
   //   1. Scan USE of inputs. Decrease the USE and deallocate if the USE is 0
   //   2. Scan DEF of outputs. If the DEF, allocate it
   VERBOSE(LINEAR) << "TENSORS" << std::endl;
-  const auto &operands = _graph.operands();
+  const auto &operands = _model->operands;
   for (const auto &e : _elements)
   {
     for (const auto &op : e.subgraph->operations())
@@ -182,7 +180,7 @@ backend::TensorBuilderSet Linear::planTensors()
   }
 
   // Now, model outputs should be not deallocated
-  assert(std::all_of(_graph.getOutputs().begin(), _graph.getOutputs().end(),
+  assert(std::all_of(_model->outputs.begin(), _model->outputs.end(),
                      [&uses_map](const model::operand::Index &ind) { return uses_map[ind] > 0; }));
 
   // Set subtensor information
index 99b3389..78bfb75 100644 (file)
@@ -20,6 +20,7 @@
 #include <vector>
 #include <memory>
 
+#include "model/Model.h"
 #include "model/operation/Subgraph.h"
 #include "backend/interface/ITensorBuilder.h"
 #include "graph/LowerInfoMap.h"
@@ -37,14 +38,6 @@ struct NodeVisitor;
 
 namespace neurun
 {
-namespace graph
-{
-class Graph;
-} // namespace graph
-} // namespace neurun
-
-namespace neurun
-{
 namespace linear
 {
 
@@ -63,8 +56,7 @@ struct Element
 class Linear
 {
 public:
-  // TODO Change std::shared_ptr<Model> instead of Graph
-  Linear(const graph::Graph &graph,
+  Linear(const std::shared_ptr<const model::Model> &model,
          std::unique_ptr<std::vector<std::unique_ptr<model::operation::Subgraph>>> subg_set,
          std::unique_ptr<graph::LowerInfoMap> lower_info_map);
 
@@ -93,7 +85,7 @@ private:
   const graph::operand::LowerInfo *getLowerInfo(const model::operand::Index &index) const;
 
 private:
-  const graph::Graph &_graph;
+  std::shared_ptr<const model::Model> _model;
   std::unique_ptr<std::vector<std::unique_ptr<model::operation::Subgraph>>> _subg_set;
   std::unique_ptr<graph::LowerInfoMap> _lower_info_map;
   std::vector<Element> _elements;