[graph/manager] Enable memory v1 optimizations
authorParichay Kapoor <pk.kapoor@samsung.com>
Fri, 3 Sep 2021 08:59:21 +0000 (17:59 +0900)
committerJijoong Moon <jijoong.moon@samsung.com>
Tue, 5 Oct 2021 11:48:57 +0000 (20:48 +0900)
This patch adds interface to enable memory optimizations with the neural
network. Enabling the interface changes the planner being used for the
memory allocation.
With this patch, OptimizedV1Planner is put to use when enabling
optimizations.

Unittest of models is updated to disable optimizations in the
non-optimized test cases.

Signed-off-by: Parichay Kapoor <pk.kapoor@samsung.com>
jni/Android.mk
nntrainer/graph/network_graph.h
nntrainer/models/neuralnet.h
nntrainer/tensor/manager.cpp
nntrainer/tensor/manager.h
nntrainer/tensor/memory_pool.cpp
nntrainer/tensor/tensor_pool.cpp
test/unittest/unittest_nntrainer_models.cpp

index 796772c66df81859d14cf3c0d742ec11e47ec7f1..65c675116be494a015ecd6fd09e6edb97ed3be54 100644 (file)
@@ -142,6 +142,7 @@ NNTRAINER_SRCS := $(NNTRAINER_ROOT)/nntrainer/models/neuralnet.cpp \
                   $(NNTRAINER_ROOT)/nntrainer/tensor/tensor_pool.cpp \
                   $(NNTRAINER_ROOT)/nntrainer/tensor/memory_pool.cpp \
                   $(NNTRAINER_ROOT)/nntrainer/tensor/basic_planner.cpp \
+                  $(NNTRAINER_ROOT)/nntrainer/tensor/optimized_v1_planner.cpp \
                   $(NNTRAINER_ROOT)/nntrainer/tensor/blas_interface.cpp \
                   $(NNTRAINER_ROOT)/nntrainer/layers/layer_node.cpp \
                   $(NNTRAINER_ROOT)/nntrainer/layers/layer_context.cpp \
index 362fc0b3693b0a5345fc06403cedc907ffe476b2..5ef4116b80ee8c7b5e7f8b9d805e5343309c9f30 100644 (file)
@@ -308,6 +308,15 @@ public:
    */
   void deallocateWeights() { tensor_manager->deallocateWeights(); }
 
+  /**
+   * @brief     Enable the memory optimizations for the network
+   *
+   * @param val true to enable, else false
+   */
+  void setMemoryOptimizations(bool val) {
+    tensor_manager->setOptimizations(val);
+  }
+
   /**
    * @brief     Create optimizer variable for every weights
    *
index c415aad9497e2e7dbe74f4c60fae7f00e6c4da5f..1f4a6fb45f678cee644c4451462313beab747b75 100644 (file)
@@ -277,6 +277,20 @@ public:
   std::vector<float *> inference(std::vector<float *> &input,
                                  unsigned int batch);
 
+  /**
+   * @brief     Enable the memory optimizations for the network
+   *
+   */
+  void enableMemoryOptimizations() { model_graph.setMemoryOptimizations(true); }
+
+  /**
+   * @brief     Enable the memory optimizations for the network
+   *
+   */
+  void disableMemoryOptimizations() {
+    model_graph.setMemoryOptimizations(false);
+  }
+
   /**
    * @brief     Run NeuralNetwork train with callback function by user
    * @param[in] dt datatype (mode) where it should be
index 801dcabbc28016cec4a7850a2e2ccc5e9b94830a..a150f626e2103b745f8309912501611aa7216cb8 100644 (file)
@@ -129,7 +129,7 @@ MMapedMemory::~MMapedMemory() noexcept {
 
 void Manager::allocateWeights(unsigned int max_exec_order_) {
   if (!weight_pool.isAllocated()) {
-    weight_pool.finalize(BasicPlanner(), 0, max_exec_order_);
+    finalizeTensorPool(weight_pool, 0, max_exec_order_);
     weight_pool.allocate();
   }
 }
@@ -143,7 +143,7 @@ void Manager::allocateTensors(unsigned int max_exec_order_) {
   allocateWeights(max_exec_order_);
 
   if (!tensor_pool.isAllocated()) {
-    tensor_pool.finalize(BasicPlanner(), 0, max_exec_order_);
+    finalizeTensorPool(tensor_pool, 0, max_exec_order_);
     if (tensor_pool.minMemoryRequirement() > 0)
       tensor_pool.allocate();
   }
@@ -521,4 +521,12 @@ std::vector<Weight *> Manager::getWeights() {
   return all_weights;
 }
 
+void Manager::finalizeTensorPool(TensorPool &pool, unsigned int start,
+                                 unsigned int end) {
+  if (enable_optimizations)
+    pool.finalize(OptimizedV1Planner(), start, end);
+  else
+    pool.finalize(BasicPlanner(), start, end);
+}
+
 } // namespace nntrainer
index 62425d6ec8783d861d0edbe30fc866b951afac9d..af6f08db30cf544806a72edf054d415fb7e54247 100644 (file)
@@ -111,7 +111,7 @@ public:
   /**
    * @brief     Constructor of Manager
    */
-  Manager() = default;
+  Manager() : enable_optimizations(true) {}
 
   /**
    * @brief Construct a new Manager object (deleted)
@@ -289,6 +289,13 @@ public:
    */
   void deallocateWeights();
 
+  /**
+   * @brief Set optimizations for manager
+   *
+   * @param val true to enable, else false
+   */
+  void setOptimizations(bool val) { enable_optimizations = val; }
+
 private:
   /** @todo: merge this list to one */
   std::vector<std::unique_ptr<Weight>>
@@ -302,6 +309,18 @@ private:
 
   TensorPool weight_pool; /**< tensor pool to request tensors */
   TensorPool tensor_pool; /**< tensor pool to request tensors */
+
+  bool enable_optimizations; /**< to enable memory optimizations */
+
+  /**
+   * @brief Finalize the given tensor pool
+   *
+   * @param pool Tensor pool to finalize
+   * @param start Start execution order
+   * @param end End execution order
+   */
+  void finalizeTensorPool(TensorPool &pool, unsigned int start,
+                          unsigned int end);
 };
 
 } // namespace nntrainer
index 32775b9bc0f2ab6930f587c4d45465a98873650c..9b620c4b6189dc0c0cfa3a7b837d0edf7197c0ca 100644 (file)
@@ -71,7 +71,7 @@ double MemoryPool::planLayout(const MemoryPlanner &planner) {
   if (pool_size < min_pool_size || !validateLayout())
     throw std::runtime_error("Planned layout is not feasible");
 
-  return double(pool_size) / double(min_pool_size);
+  return double(min_pool_size) / double(pool_size);
 }
 
 /**
index b84a9f627d26db737c716c36b1293f04214c52f8..15bc2c51ea6c62f63f87d4127c96557b41da412b 100644 (file)
@@ -14,6 +14,7 @@
  */
 
 #include <memory_pool.h>
+#include <nntrainer_log.h>
 #include <tensor.h>
 #include <tensor_pool.h>
 #include <tensor_wrap_specs.h>
@@ -132,8 +133,10 @@ void TensorPool::finalize(const MemoryPlanner &planner,
   }
 
   /** 4. finalizeLayout for the memory pool. */
-  if (bytes_requested > 0)
-    mem_pool.planLayout(planner);
+  if (bytes_requested > 0) {
+    double efficiency = mem_pool.planLayout(planner);
+    ml_logd("Memory layout efficiency = %lf", efficiency);
+  }
 }
 
 /**
index 87e32dfa932cc0e0b11c699a905791ce8660b0e3..0b7d3ec0b71bd4f5ed19b522363f2d1646bad8f4 100644 (file)
@@ -383,6 +383,8 @@ GraphWatcher::GraphWatcher(const std::string &config, const bool opt) :
 
   /** Disable memory optimization as memory being matched for each layer
    */
+  if (!opt)
+    nn.disableMemoryOptimizations();
 
   if (nn.loadFromConfig(config)) {
     throw std::invalid_argument("load from config failed!");