[MemoryManager] Apply MemoryManager to TensorBuilder on ACL backend (#5407)
author김용섭/On-Device Lab(SR)/Engineer/삼성전자 <yons.kim@samsung.com>
Wed, 19 Jun 2019 02:03:30 +0000 (11:03 +0900)
committer이춘석/On-Device Lab(SR)/Staff Engineer/삼성전자 <chunseok.lee@samsung.com>
Wed, 19 Jun 2019 02:03:30 +0000 (11:03 +0900)
Each TensorBuilder on each acl backend such as cl and neon has
AclMemoryManager instead of Tensors.

Signed-off-by: Yongseop Kim <yons.kim@samsung.com>
runtimes/neurun/backend/acl_common/TemplTensorBuilder.h

index 8d96aad..18eff19 100644 (file)
@@ -22,6 +22,9 @@
 #include <arm_compute/core/Types.h>
 #include <backend/ITensorBuilder.h>
 #include "model/OperandIndexMap.h"
+#include "AclMemoryManager.h"
+#include "util/config/ConfigManager.h"
+#include "cpp14/memory.h"
 
 namespace neurun
 {
@@ -63,6 +66,9 @@ public:
   std::shared_ptr<backend::operand::IObject> wrapTensor(const model::OperandIndex &ind) override;
   void iterate(const IterateFunction &fn) override;
 
+  // TODO Pass MemoryManager to Executor instead of TensorBuilder
+  // std::unique_ptr<IMemoryManager> releaseMemoryManager(void) override;
+
   std::shared_ptr<T_ITensor> at(const ::neurun::model::OperandIndex &ind);
   /**
    * @brief     Check child tensor is allocated as subtensor of parent tensor
@@ -78,10 +84,9 @@ private:
   model::OperandIndexMap<model::OperandInfo> _tensor_info_map;
   model::OperandIndexMap<compiler::SubTensorInfo> _subtensor_info_map;
   model::OperandIndexMap<bool> _apply_dim_correction_map;
-  model::OperandIndexMap<std::shared_ptr<T_Tensor>> _tensors;
-  model::OperandIndexMap<std::shared_ptr<T_SubTensor>> _subtensors;
-  model::OperandIndexMap<std::shared_ptr<T_Object>> _objects;
   graph::operand::Layout _layout;
+
+  std::unique_ptr<AclMemoryManager<T_Tensor, T_SubTensor, T_Object>> _mem_mgr;
 };
 
 } // namespace acl_common
@@ -105,7 +110,18 @@ namespace acl_common
 template <typename T_ITensor, typename T_Tensor, typename T_SubTensor, typename T_Object>
 TemplTensorBuilder<T_ITensor, T_Tensor, T_SubTensor, T_Object>::TemplTensorBuilder()
 {
-  // DO NOTHING
+  _mem_mgr = nnfw::cpp14::make_unique<AclMemoryManager<T_Tensor, T_SubTensor, T_Object>>();
+  assert(_mem_mgr);
+
+// TODO After LinearAclMemoryManager lands, apply below code
+#if 0
+  const std::string executor_str =
+      config::ConfigManager::instance().get<std::string>(config::EXECUTOR);
+  if (executor_str == "Linear")
+    _mem_mgr = nnfw::cpp14::make_unique<LinearAclMemoryManager<T_Tensor, T_SubTensor, T_Object>>();
+  else
+    _mem_mgr = nnfw::cpp14::make_unique<AclMemoryManager<T_Tensor, T_SubTensor, T_Object>>();
+#endif
 }
 
 template <typename T_ITensor, typename T_Tensor, typename T_SubTensor, typename T_Object>
@@ -113,7 +129,7 @@ void TemplTensorBuilder<T_ITensor, T_Tensor, T_SubTensor, T_Object>::registerTen
     const model::OperandIndex &ind, const model::OperandInfo &info,
     const graph::operand::Layout &layout)
 {
-  assert(_tensors.size() == 0);
+  assert(_mem_mgr->tensors().size() == 0);
 
   _tensor_info_map.insert({ind, info});
   _apply_dim_correction_map.insert({ind, true});
@@ -124,7 +140,7 @@ template <typename T_ITensor, typename T_Tensor, typename T_SubTensor, typename
 void TemplTensorBuilder<T_ITensor, T_Tensor, T_SubTensor, T_Object>::registerSubTensorInfo(
     const model::OperandIndex &ind, const compiler::SubTensorInfo &info)
 {
-  assert(_tensors.size() == 0);
+  assert(_mem_mgr->tensors().size() == 0);
 
   _subtensor_info_map.insert({ind, info});
   _apply_dim_correction_map.insert({ind, true});
@@ -147,12 +163,12 @@ void TemplTensorBuilder<T_ITensor, T_Tensor, T_SubTensor, T_Object>::notifyLastU
 template <typename T_ITensor, typename T_Tensor, typename T_SubTensor, typename T_Object>
 void TemplTensorBuilder<T_ITensor, T_Tensor, T_SubTensor, T_Object>::prepare(void)
 {
-  assert(_tensors.size() == 0);
+  assert(_mem_mgr->tensors().size() == 0);
 
   // TODO Handle SubTensor(subsumption)
   //      Currently this TemplTensorBuilder does not have subsumption info yet
   //      Allocated subtensor will be mapped to _subtensors instead of _tensors
-  assert(_subtensors.size() == 0);
+  assert(_mem_mgr->subtensors().size() == 0);
 
   for (auto &entry : _tensor_info_map)
   {
@@ -160,8 +176,7 @@ void TemplTensorBuilder<T_ITensor, T_Tensor, T_SubTensor, T_Object>::prepare(voi
     const auto &info = entry.second;
     auto tensor_info =
         asTensorInfo(info.shape(), info.typeInfo(), _layout, _apply_dim_correction_map[ind]);
-    auto tensor = std::make_shared<T_Tensor>(tensor_info);
-    _tensors[ind] = tensor;
+    _mem_mgr->buildTensor(ind, tensor_info);
   }
 
   // To make subtensor, parent tensor must be made first
@@ -174,6 +189,8 @@ void TemplTensorBuilder<T_ITensor, T_Tensor, T_SubTensor, T_Object>::prepare(voi
   //    3-2) If parent tensor is not made, we can't make child tensor yet
   //         Push parent tensor index to stack and return to 4)
   //  4) If stack is empty, return to 1), else return to 2)
+  auto &tensors = _mem_mgr->tensors();
+  auto &subtensors = _mem_mgr->subtensors();
   for (auto &entry : _subtensor_info_map)
   {
     model::OperandIndex ind = entry.first;
@@ -187,7 +204,7 @@ void TemplTensorBuilder<T_ITensor, T_Tensor, T_SubTensor, T_Object>::prepare(voi
       const auto &info = _subtensor_info_map.at(current);
 
       // Already generated SubTensor
-      if (_subtensors.find(current) != _subtensors.end())
+      if (subtensors.find(current) != subtensors.end())
       {
         stack.pop();
         continue;
@@ -196,15 +213,15 @@ void TemplTensorBuilder<T_ITensor, T_Tensor, T_SubTensor, T_Object>::prepare(voi
       auto parent = info.parent();
       std::shared_ptr<T_ITensor> parent_tensor;
 
-      if (_tensors.find(parent) != _tensors.end())
+      if (tensors.find(parent) != tensors.end())
       {
         // Parent is allocated as tensor
-        parent_tensor = _tensors[parent];
+        parent_tensor = tensors[parent];
       }
-      else if (_subtensors.find(parent) != _subtensors.end())
+      else if (subtensors.find(parent) != subtensors.end())
       {
         // Parent is allocated as subtensor
-        parent_tensor = _subtensors[parent];
+        parent_tensor = subtensors[parent];
       }
       else
       {
@@ -223,7 +240,7 @@ void TemplTensorBuilder<T_ITensor, T_Tensor, T_SubTensor, T_Object>::prepare(voi
       auto shape = asTensorShape(info.shape(), _layout, _apply_dim_correction_map[current]);
       ::arm_compute::Coordinates coordinates = asTensorCoordinate(info.offset(), _layout);
       auto tensor = std::make_shared<T_SubTensor>(parent_tensor.get(), shape, coordinates, true);
-      _subtensors[current] = tensor;
+      subtensors[current] = tensor;
       stack.pop();
     }
   }
@@ -232,13 +249,9 @@ void TemplTensorBuilder<T_ITensor, T_Tensor, T_SubTensor, T_Object>::prepare(voi
 template <typename T_ITensor, typename T_Tensor, typename T_SubTensor, typename T_Object>
 void TemplTensorBuilder<T_ITensor, T_Tensor, T_SubTensor, T_Object>::allocate(void)
 {
-  assert(_tensor_info_map.size() == _tensors.size());
+  assert(_tensor_info_map.size() == _mem_mgr->tensors().size());
 
-  for (const auto &tensor_entry : _tensors)
-  {
-    auto tensor = tensor_entry.second;
-    tensor->allocator()->allocate();
-  }
+  _mem_mgr->allocate();
 }
 
 template <typename T_ITensor, typename T_Tensor, typename T_SubTensor, typename T_Object>
@@ -246,13 +259,14 @@ std::shared_ptr<::neurun::backend::operand::ITensor>
 TemplTensorBuilder<T_ITensor, T_Tensor, T_SubTensor, T_Object>::tensorAt(
     const model::OperandIndex &ind)
 {
-  if (_tensors.find(ind) != _tensors.end())
+  auto &tensors = _mem_mgr->tensors();
+  if (tensors.find(ind) != tensors.end())
   {
-    return _tensors.at(ind);
+    return tensors.at(ind);
   }
   else
   {
-    return _subtensors.at(ind);
+    return _mem_mgr->subtensors().at(ind);
   }
 }
 
@@ -261,32 +275,18 @@ std::shared_ptr<backend::operand::IObject>
 TemplTensorBuilder<T_ITensor, T_Tensor, T_SubTensor, T_Object>::wrapTensor(
     const model::OperandIndex &ind)
 {
-  if (_objects.find(ind) != _objects.end())
-  {
-    return _objects.at(ind);
-  }
-  else
-  {
-    if (_tensors.find(ind) != _tensors.end())
-    {
-      return _objects[ind] = std::make_shared<T_Object>(_tensors.at(ind));
-    }
-    else
-    {
-      return _objects[ind] = std::make_shared<T_Object>(_subtensors.at(ind));
-    }
-  }
+  return _mem_mgr->wrapTensor(ind);
 }
 
 template <typename T_ITensor, typename T_Tensor, typename T_SubTensor, typename T_Object>
 void TemplTensorBuilder<T_ITensor, T_Tensor, T_SubTensor, T_Object>::iterate(
     const IterateFunction &fn)
 {
-  for (auto it : _tensors)
+  for (auto it : _mem_mgr->tensors())
   {
     fn(it.first);
   }
-  for (auto it : _subtensors)
+  for (auto it : _mem_mgr->subtensors())
   {
     fn(it.first);
   }
@@ -296,13 +296,14 @@ template <typename T_ITensor, typename T_Tensor, typename T_SubTensor, typename
 std::shared_ptr<T_ITensor> TemplTensorBuilder<T_ITensor, T_Tensor, T_SubTensor, T_Object>::at(
     const ::neurun::model::OperandIndex &ind)
 {
-  if (_tensors.find(ind) != _tensors.end())
+  auto &tensors = _mem_mgr->tensors();
+  if (tensors.find(ind) != tensors.end())
   {
-    return _tensors.at(ind);
+    return tensors.at(ind);
   }
   else
   {
-    return _subtensors.at(ind);
+    return _mem_mgr->subtensors().at(ind);
   }
 }
 
@@ -315,7 +316,8 @@ bool TemplTensorBuilder<T_ITensor, T_Tensor, T_SubTensor, T_Object>::isSubTensor
     return false;
   }
 
-  if (_subtensors.find(child) == _subtensors.end())
+  auto &subtensors = _mem_mgr->subtensors();
+  if (subtensors.find(child) == subtensors.end())
   {
     return false;
   }