[neurun] Make TensorBuilder on CPU have MemoryAllocator (#3215)
author김용섭/동작제어Lab(SR)/Engineer/삼성전자 <yons.kim@samsung.com>
Wed, 17 Oct 2018 07:06:14 +0000 (16:06 +0900)
committer오형석/동작제어Lab(SR)/Staff Engineer/삼성전자 <hseok82.oh@samsung.com>
Wed, 17 Oct 2018 07:06:14 +0000 (16:06 +0900)
* [neurun] Make TensorBuilder have MemoryAllocator

Makes TensorBuilder have MemoryAllocator as its member. Now there is no
resolution to access MemoryAllocator globally.

Signed-off-by: Yongseop Kim <yons.kim@samsung.com>
* Fix format-checker fail

runtimes/neurun/src/backend/cpu/MemoryAllocator.cc
runtimes/neurun/src/backend/cpu/MemoryAllocator.h
runtimes/neurun/src/backend/cpu/TensorBuilder.cc
runtimes/neurun/src/backend/cpu/TensorBuilder.h
runtimes/neurun/test/backend/cpu/MemoryAllocator.cc

index 1a7319c..41b36bd 100644 (file)
@@ -30,18 +30,6 @@ BumpAllocator::~BumpAllocator()
     delete[] _base;
 }
 
-void BumpAllocator::reset()
-{
-  if (_base)
-    delete[] _base;
-
-  _base = nullptr;
-  _curr_pos = 0;
-  _reserved_size = 0;
-  _mem_idx = 0;
-  _mem_blk_map.clear();
-}
-
 uint32_t BumpAllocator::allocate(size_t size)
 {
   assert(size != 0);
index beaae6f..36877fa 100644 (file)
@@ -35,8 +35,6 @@ struct MemoryBlock
 struct IMemoryAllocator
 {
   virtual ~IMemoryAllocator() = default;
-  // FIXME Remove this when instance() is removed
-  virtual void reset() = 0;
   virtual uint32_t allocate(size_t) = 0;
   virtual void free(uint32_t) = 0;
   virtual void finalize() = 0;
@@ -48,7 +46,6 @@ class BumpAllocator : public IMemoryAllocator
 {
 public:
   virtual ~BumpAllocator() override;
-  virtual void reset() override;
   virtual uint32_t allocate(size_t size) override;
   virtual void free(uint32_t mem_id) override;
   virtual void finalize() override;
@@ -61,14 +58,6 @@ private:
   uint32_t _curr_pos = 0;
   uint32_t _mem_idx = 0;
   std::unordered_map<uint32_t, MemoryBlock> _mem_blk_map;
-
-public:
-  // This should be moved into something class in backend, not as global var
-  static IMemoryAllocator &instance()
-  {
-    static BumpAllocator inst;
-    return inst;
-  }
 };
 
 } // namespace cpu
index 74f1b25..5eee633 100644 (file)
@@ -28,7 +28,8 @@ namespace backend
 namespace cpu
 {
 
-TensorBuilder::TensorBuilder()
+// TODO Apply FirstFitAllocator in the future
+TensorBuilder::TensorBuilder() : _mem_alloc(std::make_shared<BumpAllocator>())
 {
   // DO NOTHING
 }
@@ -44,12 +45,7 @@ void TensorBuilder::mark(const ::neurun::graph::operand::Index &ind,
 void TensorBuilder::prepare(void)
 {
   assert(_tensors.size() == 0);
-
-  // TODO Do not use global var for accessing MemoryAllocator
-  auto &mem_alloc = BumpAllocator::instance();
-
-  // TODO Remove this when okay to pass tests in runtime_android_nn_test
-  mem_alloc.reset();
+  assert(_mem_alloc);
 
   for (auto &entry : _tensor_info_map)
   {
@@ -60,7 +56,7 @@ void TensorBuilder::prepare(void)
     // If we do not make tensor here currently, stages would cause segment fault
 
     const auto size = info.total_size(); // NOTE This size may not be accurate
-    auto mem_id = mem_alloc.allocate(size);
+    auto mem_id = _mem_alloc->allocate(size);
     _tensor_mem_map[ind] = mem_id;
   }
   assert(_tensor_info_map.size() == _tensor_mem_map.size());
@@ -71,16 +67,16 @@ void TensorBuilder::prepare(void)
   //     fn->configure(ifm_alloc->buffer(), param.ifm_shape, ker_alloc->buffer(), param.ker_shape,
   //   to
   //     fn->configure(ifm_alloc, param.ifm_shape, ker_alloc, param.ker_shape,
-  mem_alloc.finalize();
-  assert(mem_alloc.base());
+  _mem_alloc->finalize();
+  assert(_mem_alloc->base());
 
   for (auto &entry : _tensor_mem_map)
   {
     auto ind = entry.first;
     auto mem_id = entry.second;
-    auto mem_blk = mem_alloc.getMemoryBlock(mem_id);
+    auto mem_blk = _mem_alloc->getMemoryBlock(mem_id);
     auto &tensor = _tensors[ind];
-    tensor->setBuffer(mem_alloc.base() + mem_blk.base_offset);
+    tensor->setBuffer(_mem_alloc->base() + mem_blk.base_offset);
   }
 }
 
index a4cb260..72cf425 100644 (file)
@@ -30,6 +30,8 @@ namespace backend
 namespace cpu
 {
 
+struct IMemoryAllocator;
+
 class TensorBuilder : public ITensorBuilder
 {
 public:
@@ -52,6 +54,7 @@ private:
   std::unordered_map<graph::operand::Index, ::arm_compute::TensorInfo> _tensor_info_map;
   std::unordered_map<graph::operand::Index, std::shared_ptr<operand::Tensor>> _tensors;
   std::unordered_map<graph::operand::Index, uint32_t> _tensor_mem_map;
+  std::shared_ptr<IMemoryAllocator> _mem_alloc;
 };
 
 } // namespace cpu
index bb0b315..5a115d5 100644 (file)
@@ -20,9 +20,7 @@
 
 TEST(BumpAllocator, allocate_test)
 {
-  auto &allocator = ::neurun::backend::cpu::BumpAllocator::instance();
-
-  allocator.reset();
+  ::neurun::backend::cpu::BumpAllocator allocator;
 
   size_t mem_sz0 = 10;
   auto mem_id0 = allocator.allocate(mem_sz0);
@@ -47,9 +45,7 @@ TEST(BumpAllocator, allocate_test)
 
 TEST(BumpAllocator, finalize_test)
 {
-  auto &allocator = ::neurun::backend::cpu::BumpAllocator::instance();
-
-  allocator.reset();
+  ::neurun::backend::cpu::BumpAllocator allocator;
 
   size_t mem_sz0 = 10;
   allocator.allocate(mem_sz0);