[neurun] Do not collect TensorBuilders one by one (#6458)
author이한종/On-Device Lab(SR)/Engineer/삼성전자 <hanjoung.lee@samsung.com>
Mon, 12 Aug 2019 10:34:39 +0000 (19:34 +0900)
committer오형석/On-Device Lab(SR)/Staff Engineer/삼성전자 <hseok82.oh@samsung.com>
Mon, 12 Aug 2019 10:34:39 +0000 (19:34 +0900)
* [neurun] Do not collect TensorBuilders one by one

To get the full set of tensor builders currently using it used to
collect from all the `LowerInfo`s. Now that `BackendResolver` manages
them all we can just use it.
Allow allocation of 0-byte object

Signed-off-by: Hanjoung Lee <hanjoung.lee@samsung.com>
runtimes/neurun/backend/cpu/MemoryPlanner.cc
runtimes/neurun/core/src/compiler/BackendResolver.h
runtimes/neurun/core/src/compiler/ExecutorFactory.cc
runtimes/neurun/core/src/compiler/Linear.cc
runtimes/neurun/core/src/compiler/Linear.h

index 30135c1..8eaf7bb 100644 (file)
@@ -27,8 +27,6 @@ namespace cpu
 
 Allocator::Allocator(uint32_t capacity)
 {
-  assert(!_base && capacity != 0);
-
   _base = nnfw::cpp14::make_unique<uint8_t[]>(capacity);
 
   VERBOSE(ALLOC) << "allocation capacity: " << capacity << std::endl;
index 75c4327..a88d249 100644 (file)
@@ -23,6 +23,7 @@
 #include "util/logging.h"
 #include "backend/Backend.h"
 #include "backend/BackendManager.h"
+#include "backend/ITensorBuilder.h"
 #include "model/OperationIndexMap.h"
 
 namespace neurun
@@ -58,6 +59,16 @@ public:
     return _context_manager.at(backend).get();
   }
 
+  backend::TensorBuilderSet tensor_builders() const
+  {
+    backend::TensorBuilderSet ret;
+    for (const auto &e : _context_manager)
+    {
+      ret.insert(e.second->tensor_builder);
+    }
+    return ret;
+  }
+
   const backend::Backend *getBackend(const model::OperationIndex &index) const
   {
     return getBackendContext(index)->backend;
index 8c67e8c..16e4991 100644 (file)
@@ -102,7 +102,9 @@ exec::IExecutor *ExecutorFactory::createLinearExecutor(graph::Graph &graph)
     shape_fixer->fix(*element.subgraph);
   });
 
-  auto tensor_builders = linear->planTensors();
+  linear->planTensors();
+
+  auto tensor_builders = linear->backend_resolver()->tensor_builders();
 
   // Prepare tensors
   for (auto &tensor_builder : tensor_builders)
@@ -189,8 +191,6 @@ exec::IExecutor *ExecutorFactory::createDataflowExecutor(graph::Graph &graph, bo
         shape_fixer->fix(subg);
       });
 
-  backend::TensorBuilderSet tensor_builders;
-
   graph.operands().iterate([&](const model::OperandIndex &ind, const model::Operand &obj) {
     const auto lower_info = graph.getLowerInfo(ind);
     for (auto factor : lower_info->def_factors())
@@ -221,11 +221,11 @@ exec::IExecutor *ExecutorFactory::createDataflowExecutor(graph::Graph &graph, bo
         // To make this never be deallocated, this is a workaround to use static memory planner
         tensor_builder->notifyFirstUse(ind);
       }
-
-      tensor_builders.insert(tensor_builder);
     }
   });
 
+  auto tensor_builders = graph.backend_resolver()->tensor_builders();
+
   for (auto &tensor_builder : tensor_builders)
   {
     tensor_builder->prepare();
index dfeb89e..8ae4204 100644 (file)
@@ -154,9 +154,8 @@ void Linear::accept(model::OperationVisitor &&visitor) const
   }
 }
 
-backend::TensorBuilderSet Linear::planTensors()
+void Linear::planTensors()
 {
-  backend::TensorBuilderSet tensor_builders;
   model::OperandIndexMap<std::shared_ptr<backend::ITensorBuilder>> tensor_builder_map;
 
   // NOTE
@@ -215,9 +214,6 @@ backend::TensorBuilderSet Linear::planTensors()
         tensor_builder->registerTensorInfo(ind, info, layout);
       }
 
-      // Prepare tensor builders to be returned
-      tensor_builders.insert(tensor_builder);
-
       tensor_builder_map[ind] = tensor_builder;
     }
   });
@@ -304,10 +300,6 @@ backend::TensorBuilderSet Linear::planTensors()
   assert(std::all_of(
       def_map.begin(), def_map.end(),
       [](std::pair<const model::OperandIndex, uint32_t> it) { return it.second == 0; }));
-
-  // Set subtensor information
-  // Todo: move this phase outside as optimization phase
-  return tensor_builders;
 }
 
 void Linear::iterate(const std::function<void(const Element &element)> &fn) const
index 16b1f40..78c782a 100644 (file)
@@ -69,8 +69,7 @@ public:
 public:
   void accept(model::OperationVisitor &&visitor) const;
 
-  // TODO Should not return TensorBuilderSet
-  backend::TensorBuilderSet planTensors();
+  void planTensors();
 
   void iterate(const std::function<void(const Element &element)> &fn) const;
 
@@ -88,6 +87,8 @@ public:
     return _backend_resolver->getBackendContext(backend);
   }
 
+  const compiler::BackendResolver *backend_resolver() const { return _backend_resolver.get(); }
+
 private:
   // TODO Replace these getLowerInfo methods with ones of LowerInfoMap in the future
   const graph::operation::LowerInfo *getLowerInfo(const model::SubgraphIndex &index) const;