[pooling] Do not allocate memory in initialize
authorParichay Kapoor <pk.kapoor@samsung.com>
Mon, 4 Jan 2021 10:13:59 +0000 (19:13 +0900)
committerJijoong Moon <jijoong.moon@samsung.com>
Mon, 25 Jan 2021 02:59:21 +0000 (11:59 +0900)
Set batch size in initialize for pooling layer allocates memory.
However, the final batch size is allowed to change in inference/training.
This unnecessarily changes the peak memory requirement.
For now, this memory is allocated with forwarding.
Later this will be handled as a tensor with manager once int data type is supported.

**Self evaluation:**
1. Build test: [x]Passed [ ]Failed [ ]Skipped
2. Run test: [x]Passed [ ]Failed [ ]Skipped

Signed-off-by: Parichay Kapoor <pk.kapoor@samsung.com>
nntrainer/layers/pooling2d_layer.cpp
nntrainer/layers/pooling2d_layer.h
nntrainer/models/neuralnet.cpp
nntrainer/models/neuralnet.h
nntrainer/tensor/manager.h

index d81f5a1..6319419 100644 (file)
@@ -55,14 +55,6 @@ int Pooling2DLayer::initialize(Manager &manager) {
     out_dim.width(in_dim.channel());
   }
 
-  if (pooling_type == PoolingType::max) {
-    max_idx.resize(out_dim.getDataLen());
-  }
-
-  if (pooling_type == PoolingType::global_max) {
-    max_idx_global.resize(out_dim.getDataLen());
-  }
-
   return status;
 }
 
@@ -77,6 +69,12 @@ void Pooling2DLayer::forwarding(bool training) {
     hidden_ = Tensor(hidden_dim);
   }
 
+  if (pooling_type == PoolingType::max) {
+    max_idx.resize(output_dim[0].getDataLen());
+  } else if (pooling_type == PoolingType::global_max) {
+    max_idx_global.resize(output_dim[0].getDataLen());
+  }
+
   for (unsigned int b = 0; b < in_dim.batch(); ++b) {
     Tensor in_padded;
     zero_pad(b, input_, padding.data(), in_padded);
@@ -182,16 +180,6 @@ int Pooling2DLayer::setSize(int *size, PropertyType type) {
   return status;
 }
 
-void Pooling2DLayer::setBatch(unsigned int batch) {
-  Layer::setBatch(batch);
-
-  if (pooling_type == PoolingType::max) {
-    max_idx.resize(output_dim[0].getDataLen());
-  } else if (pooling_type == PoolingType::global_max) {
-    max_idx_global.resize(output_dim[0].getDataLen());
-  }
-}
-
 void Pooling2DLayer::copy(std::shared_ptr<Layer> l) {
   Layer::copy(l);
 
index 175da9d..458f8c6 100644 (file)
@@ -100,11 +100,6 @@ public:
   void calcDerivative();
 
   /**
-   * @copydoc Layer::setBatch(unsigned int batch)
-   */
-  void setBatch(unsigned int batch);
-
-  /**
    * @brief     copy layer
    * @param[in] l layer to copy
    */
index fd7d8d8..3fa4875 100644 (file)
@@ -257,7 +257,8 @@ int NeuralNetwork::initialize() {
       l.setInputBuffers(in_out);
     }
   }
-  setBatchSize(batch_size);
+  setBatchSize();
+
   // Allocate and initialize weights
   manager->initializeWeights();
 
index 28d8913..6588629 100644 (file)
@@ -415,6 +415,11 @@ public:
     manager->setInferenceInOutMemoryOptimization(opt);
   }
 
+  /**
+   * @brief     Update batch size of the model as well as its layers/dataset
+   */
+  void setBatchSize() { setBatchSize(batch_size); }
+
 /// @todo Make a more common class have this
 /// Maybe appcontext can have this?
 #ifdef PROFILE
index 05393ac..b8c4c54 100644 (file)
@@ -235,8 +235,9 @@ public:
    */
   void setBatchSize(unsigned int batch) {
     if (!in_outs.empty() && !in_outs[0].empty()) {
-      max_derivative_size /= in_outs[0][0]->getDim().batch();
-      max_shared_inout /= in_outs[0][0]->getDim().batch();
+      unsigned int prev_batch = in_outs[0][0]->getDim().batch();
+      max_derivative_size /= prev_batch;
+      max_shared_inout /= prev_batch;
       max_derivative_size *= batch;
       max_shared_inout *= batch;
     }