[tensor] Split tensor constructor into 2
authorParichay Kapoor <pk.kapoor@samsung.com>
Mon, 1 Feb 2021 07:26:59 +0000 (16:26 +0900)
committerJijoong Moon <jijoong.moon@samsung.com>
Fri, 5 Feb 2021 01:50:57 +0000 (10:50 +0900)
Create a new tensor constructor for lazy allocation
than adding the lazy allocation feature to the existing one

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

index 2db24a4..053a4d1 100644 (file)
@@ -214,7 +214,7 @@ Manager::AllocFunc Manager::getAllocFunc(bool is_weight) {
   } else if (!is_weight) {
     /** only for gradients */
     if (max_grad_size > 0 && enable_gradient_memory_opt) {
-      shared_grad = Tensor({(unsigned int)max_grad_size}, nullptr, false);
+      shared_grad = Tensor(TensorDim({max_grad_size}), false);
 
       allocate_func = [this](const TensorDim &dim, unsigned int offset) {
         return shared_grad.getSharedDataTensor(dim, offset);
@@ -437,7 +437,7 @@ void Manager::initializeTensors(bool trainable) {
 
   // Allocate shared derivative memory
   if (max_derivative_size > 0 && enable_activation_memory_opt && trainable)
-    shared_deriv = Tensor({max_derivative_size}, nullptr, false);
+    shared_deriv = Tensor(TensorDim({max_derivative_size}), false);
 
   // @todo Do not count memory of the input tensor of the input layer in the
   // estimate of max_shared_inout as it is not used
@@ -445,7 +445,7 @@ void Manager::initializeTensors(bool trainable) {
   // Allocate shared input/output memory for inference
   // @note Memory for label is not allocated here as inference doesnt has label
   if (!trainable && enable_inference_inout_memory_opt)
-    shared_inout = Tensor({max_shared_inout}, nullptr, false);
+    shared_inout = Tensor(TensorDim({max_shared_inout}), false);
 
   /**
    * A single buffer (shared_inout) provides memory for inputs and outputs of a
index 2992aa2..6008014 100644 (file)
@@ -89,24 +89,23 @@ static auto rng = [] {
   return rng;
 }();
 
-Tensor::Tensor(const TensorDim &d, const float *buf, bool alloc_now) :
-  Tensor() {
+Tensor::Tensor(const TensorDim &d, const float *buf) : Tensor() {
   if (d.getDataLen() != 0) {
-    if (buf != nullptr && !alloc_now)
-      throw std::invalid_argument(
-        "alloc_now must be true to create a tensor with a buffer");
-
     dim = d;
     strides = d.computeStrides();
 
-    if (alloc_now)
-      allocate();
+    allocate();
 
     if (buf != nullptr)
       copy(buf);
   }
 }
 
+Tensor::Tensor(const TensorDim &d, bool alloc_now) : Tensor(d, nullptr) {
+  if (d.getDataLen() != 0 && alloc_now)
+    allocate();
+}
+
 /**
  * @class SrcSharedTensor
  * @brief Source of the shared tensor
index b069daa..e05b31f 100644 (file)
@@ -64,11 +64,17 @@ public:
   /**
    * @brief     Constructor of Tensor with dimension/buf
    * @param d Tensor dim for this tensor
-   * @param bug buffer
-   * @alloc_now If the memory of the tensor must be allocated
-   * @throws if buf is not null and allow_now is false
+   * @param buf buffer
+   * @note Memory for this tensor is instantaneously allocated
+   */
+  Tensor(const TensorDim &d, const float *buf = nullptr);
+
+  /**
+   * @brief     Constructor of Tensor with dimension, possibly lazily
+   * @param d Tensor dim for this tensor
+   * @param alloc_now If the memory of the tensor must be allocated
    */
-  Tensor(const TensorDim &d, const float *buf = nullptr, bool alloc_now = true);
+  Tensor(const TensorDim &d, bool alloc_now);
 
   /**
    * @brief Construct a new Tensor object from a buffer
@@ -105,7 +111,8 @@ public:
    * @param[in] height Height of Tensor
    * @param[in] width Width of Tensor
    */
-  Tensor(int batch, int channel, int height, int width) :
+  Tensor(unsigned int batch, unsigned int channel, unsigned int height,
+         unsigned int width) :
     Tensor(TensorDim(batch, channel, height, width)){};
 
   /**
@@ -114,7 +121,7 @@ public:
    * @param[in] height Height of Tensor
    * @param[in] width Width of Tensor
    */
-  Tensor(int channel, int height, int width) :
+  Tensor(unsigned int channel, unsigned int height, unsigned int width) :
     Tensor(1, channel, height, width){};
 
   /**
@@ -122,13 +129,14 @@ public:
    * @param[in] height Height of Tensor
    * @param[in] width Width of Tensor
    */
-  Tensor(int height, int width) : Tensor(1, 1, height, width){};
+  Tensor(unsigned int height, unsigned int width) :
+    Tensor(1, 1, height, width){};
 
   /**
    * @brief     Constructor of Tensor with just width
    * @param[in] width Width of Tensor
    */
-  explicit Tensor(int width) : Tensor(1, 1, 1, width){};
+  explicit Tensor(unsigned int width) : Tensor(1, 1, 1, width){};
 
   /**
    * @brief     Constructor of Tensor
index 9a72be7..d6d692f 100644 (file)
@@ -22,9 +22,9 @@ Var_Grad::Var_Grad(const TensorDim &dim, bool train, bool alloc_now_,
   trainable(train),
   alloc_now(alloc_now_),
   name(name) {
-  var = std::make_shared<Tensor>(dim, nullptr, alloc_now);
+  var = std::make_shared<Tensor>(dim, alloc_now);
   if (trainable)
-    grad = std::make_shared<Tensor>(dim, nullptr, alloc_now);
+    grad = std::make_shared<Tensor>(dim, alloc_now);
   else
     grad = std::make_shared<Tensor>();
 }
@@ -53,7 +53,7 @@ void Var_Grad::setTrainable(bool train) {
   trainable = train;
   if (trainable && grad->uninitialized()) {
     bool alloc_now_ = var->isAllocated();
-    grad = std::make_shared<Tensor>(var->getDim(), nullptr, alloc_now_);
+    grad = std::make_shared<Tensor>(var->getDim(), alloc_now_);
   }
 }