[VarGrad] Cleanup for VarGrad
authorParichay Kapoor <pk.kapoor@samsung.com>
Thu, 12 Aug 2021 10:57:34 +0000 (19:57 +0900)
committerJijoong Moon <jijoong.moon@samsung.com>
Mon, 27 Sep 2021 04:29:43 +0000 (13:29 +0900)
This patch cleansup VarGrad implementation, and ends up removing all its
members except the variable and gradient tensors (fitting exactly to its
description).

Resolves #1486

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

index 2f4284a..c3d7259 100644 (file)
 namespace nntrainer {
 
 Var_Grad::Var_Grad(const TensorDim &dim, const Tensor::Initializer init,
-                   bool ng, bool alloc_now_, const std::string &name) :
-  dim(dim),
-  need_gradient(ng),
-  alloc_now(alloc_now_) {
+                   bool need_gradient, bool alloc_now,
+                   const std::string &name) {
   var = std::make_shared<Tensor>(dim, alloc_now, init, name);
 
   std::string grad_name = name + grad_suffix;
@@ -62,12 +60,11 @@ void Var_Grad::initializeGradient(const Tensor &preallocated) {
 
 void Var_Grad::initializeShared() { grad->makeSharedDataTensor(*var.get()); }
 
-void Var_Grad::needsGradient(bool ng) {
-  need_gradient = ng;
+void Var_Grad::needsGradient(bool need_gradient) {
   if (need_gradient && grad->empty()) {
-    bool alloc_now_ = var->isAllocated();
-    grad = std::make_shared<Tensor>(dim, alloc_now_, Tensor::Initializer::ZEROS,
-                                    grad->getName());
+    grad =
+      std::make_shared<Tensor>(var->getDim(), var->isAllocated(),
+                               Tensor::Initializer::ZEROS, grad->getName());
   }
 }
 
index de330c4..b015f3a 100644 (file)
@@ -85,14 +85,12 @@ public:
    */
   explicit Var_Grad(const Tensor &v, const Tensor &g,
                     const std::string &n = "") :
-    dim(v.getDim()),
-    var(std::make_shared<Tensor>(v.getSharedDataTensor(dim, 0, false, n))),
-    grad(std::make_shared<Tensor>(n + grad_suffix)),
-    need_gradient(!g.empty()),
-    alloc_now(v.isAllocated()) {
-    if (need_gradient)
+    var(
+      std::make_shared<Tensor>(v.getSharedDataTensor(v.getDim(), 0, false, n))),
+    grad(std::make_shared<Tensor>(n + grad_suffix)) {
+    if (!g.empty())
       grad = std::make_shared<Tensor>(
-        g.getSharedDataTensor(dim, 0, false, n + grad_suffix));
+        g.getSharedDataTensor(v.getDim(), 0, false, n + grad_suffix));
   }
 
   /**
@@ -165,7 +163,7 @@ public:
    *
    * @return TensorDim Dimension
    */
-  TensorDim getDim() const { return dim; }
+  TensorDim getDim() const { return var->getDim(); }
 
   /**
    * @brief Get if the Var_Grad is need_gradient
@@ -173,7 +171,7 @@ public:
    * @retval true if need_gradient
    * @retval false is not need_gradient
    */
-  bool needsGradient() const { return need_gradient; }
+  bool needsGradient() const { return hasGradient(); }
 
   /**
    * @brief set if the Var_Grad should need gradient
@@ -234,15 +232,15 @@ public:
    *
    * @note New dimension must maintain the shape of the variable
    */
-  void reset(const TensorDim &tdim, Tensor::Initializer init, bool ng) {
-    dim = tdim;
+  void reset(const TensorDim &dim, Tensor::Initializer init, bool ng) {
     if (!var->empty())
       var->reshape(dim);
     var->initialize(init);
 
-    if (!grad->empty())
+    if (ng && !grad->empty())
       grad->reshape(dim);
-    need_gradient = ng;
+    else
+      grad = std::make_shared<Tensor>(grad->getName());
     resetGradient();
   }
 
@@ -252,8 +250,6 @@ public:
    * @param batch batch size
    */
   void setBatchSize(unsigned int batch) {
-    dim.batch(batch);
-
     if (!var->empty())
       var->updateBatch(batch);
     if (!grad->empty())
@@ -343,16 +339,13 @@ public:
    * @note this is can return is the var_grad needs gradient but it not
    * empty
    */
-  bool hasGradient() const { return need_gradient && !grad->empty(); }
+  bool hasGradient() const { return !grad->empty(); }
 
   inline static const std::string grad_suffix = ":grad";
 
 protected:
-  TensorDim dim;                /**< dimension of the tensor */
   std::shared_ptr<Tensor> var;  /**< variable to be updated and used */
   std::shared_ptr<Tensor> grad; /**< gradient for the variable */
-  bool need_gradient;           /**< if this variable needs gradient */
-  bool alloc_now; /**< if the tensor should be allocated instantly */
 };
 
 } // namespace nntrainer
index 50db0ca..743b45d 100644 (file)
@@ -33,7 +33,7 @@ Weight::Weight(const TensorDim &dim, const Tensor::Initializer init,
 void Weight::initializeGradient(const Tensor &preallocated) {
   // Use self variable to initialize itself
   Var_Grad::initializeGradient(preallocated);
-  if (alloc_now)
+  if (var->isAllocated())
     allocateOptimizerVariables();
 }