[tensor] Add name identifier to Tensor
authorParichay Kapoor <pk.kapoor@samsung.com>
Thu, 12 Aug 2021 10:40:08 +0000 (19:40 +0900)
committerJijoong Moon <jijoong.moon@samsung.com>
Mon, 27 Sep 2021 04:29:43 +0000 (13:29 +0900)
Move name as an identifier to the tensor.
This name has been mvoed from the Var_Grad object.
Var_Grad now does not contain name itself.

The semantics of name for Tensor currently follows from var-grad:
- empty name is allowed
- repititive names are allowed
Restrictions can and will be added over time.

Resolves #1485

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

index f2de892..9abc409 100644 (file)
@@ -88,8 +88,9 @@ static auto rng = [] {
   return rng;
 }();
 
-Tensor::Tensor(const TensorDim &d, bool alloc_now, Tensor::Initializer init) :
-  Tensor() {
+Tensor::Tensor(const TensorDim &d, bool alloc_now, Tensor::Initializer init,
+               std::string name_) :
+  Tensor(name_) {
   if (d.getDataLen() != 0) {
     dim = d;
     strides = d.computeStrides();
@@ -557,9 +558,12 @@ void Tensor::createSharedDataTensor(const Tensor &src, Tensor &dest,
 }
 
 Tensor Tensor::getSharedDataTensor(const TensorDim dim_, unsigned int offset,
-                                   bool reset_stride) const {
+                                   bool reset_stride,
+                                   const std::string &name_) const {
   Tensor ret = *this;
   ret.dim = dim_;
+  if (!name_.empty())
+    ret.name = name_;
 
   if (dim_.getDataLen() + offset > dim.getDataLen())
     throw std::invalid_argument(
index cd56f9a..7787692 100644 (file)
@@ -73,11 +73,12 @@ public:
   /**
    * @brief     Basic Constructor of Tensor
    */
-  Tensor() :
+  Tensor(std::string name_ = "") :
     dim(TensorDim()),
     strides(dim.computeStrides()),
     is_contiguous(true),
     initializer(Initializer::NONE),
+    name(name_),
     data(nullptr),
     src_tensor() {}
 
@@ -85,9 +86,11 @@ public:
    * @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
+   * @param init Initializer for the tensor
+   * @param name Name of the tensor
    */
   Tensor(const TensorDim &d, bool alloc_now,
-         Initializer init = Initializer::NONE);
+         Initializer init = Initializer::NONE, std::string name = "");
 
   /**
    * @brief     Constructor of Tensor with dimension/buf
@@ -211,6 +214,7 @@ public:
     std::swap(lhs.is_contiguous, rhs.is_contiguous);
     std::swap(lhs.initializer, rhs.initializer);
     std::swap(lhs.data, rhs.data);
+    std::swap(lhs.name, rhs.name);
   }
 
   /**
@@ -830,7 +834,8 @@ public:
    * tensor.
    */
   Tensor getSharedDataTensor(const TensorDim dim, unsigned int offset,
-                             bool reset_stride = true) const;
+                             bool reset_stride = true,
+                             const std::string &name_ = "") const;
 
   /**
    * @brief make this tensor share memory with given tensor
@@ -1016,6 +1021,20 @@ public:
     return (b * strides[0] + c * strides[1] + h * strides[2] + w * strides[3]);
   }
 
+  /**
+   * @brief   Get name of the tensor
+   *
+   * @return name of the tensor
+   */
+  void setName(const std::string &name_) { name = name_; }
+
+  /**
+   * @brief   Get name of the tensor
+   *
+   * @return name of the tensor
+   */
+  const std::string &getName() const { return name; }
+
   static constexpr float epsilon = 1e-5;
 
 private:
@@ -1024,6 +1043,7 @@ private:
   std::array<unsigned int, TensorDim::MAXDIM> strides;
   bool is_contiguous;
   Tensor::Initializer initializer;
+  std::string name; /**< name of the tensor */
 
   std::shared_ptr<float> data;
 
index 8eb2435..2f4284a 100644 (file)
@@ -22,17 +22,19 @@ 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_),
-  name(name) {
-  var = std::make_shared<Tensor>(dim, alloc_now, init);
+  alloc_now(alloc_now_) {
+  var = std::make_shared<Tensor>(dim, alloc_now, init, name);
+
+  std::string grad_name = name + grad_suffix;
   if (need_gradient)
     /**
      * @todo gradient initializer should be none, and then they should be set
      * zero right before using by the user itself.
      */
-    grad = std::make_shared<Tensor>(dim, alloc_now, Tensor::Initializer::ZEROS);
+    grad = std::make_shared<Tensor>(dim, alloc_now, Tensor::Initializer::ZEROS,
+                                    grad_name);
   else
-    grad = std::make_shared<Tensor>();
+    grad = std::make_shared<Tensor>(grad_name);
 }
 
 void Var_Grad::initializeVariable(const Tensor &preallocated) {
@@ -64,8 +66,8 @@ void Var_Grad::needsGradient(bool ng) {
   need_gradient = ng;
   if (need_gradient && grad->empty()) {
     bool alloc_now_ = var->isAllocated();
-    grad =
-      std::make_shared<Tensor>(dim, alloc_now_, Tensor::Initializer::ZEROS);
+    grad = std::make_shared<Tensor>(dim, alloc_now_, Tensor::Initializer::ZEROS,
+                                    grad->getName());
   }
 }
 
index b37f7c0..de330c4 100644 (file)
@@ -86,13 +86,13 @@ 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))),
-    grad(std::make_shared<Tensor>()),
+    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()),
-    name(n) {
+    alloc_now(v.isAllocated()) {
     if (need_gradient)
-      grad = std::make_shared<Tensor>(g.getSharedDataTensor(dim, 0, false));
+      grad = std::make_shared<Tensor>(
+        g.getSharedDataTensor(dim, 0, false, n + grad_suffix));
   }
 
   /**
@@ -139,7 +139,7 @@ public:
     if (gtrain)
       initializeGradient(grad_preallocated);
     else
-      grad = std::make_shared<Tensor>();
+      grad = std::make_shared<Tensor>(grad->getName());
   }
 
   /**
@@ -186,17 +186,17 @@ public:
    *
    * @return std::string name
    */
-  const std::string &getName() const { return name; }
+  const std::string &getName() const { return var->getName(); }
 
   /**
-   * @brief Get the variable tensor (by name)
+   * @brief Get the variable tensor
    *
    * @return Tensor Variable tensor
    */
   Tensor getVariable() const { return *var.get(); }
 
   /**
-   * @brief Get the Gradient tensor (by name)
+   * @brief Get the Gradient tensor
    *
    * @return Tensor Gradient tensor
    */
@@ -345,13 +345,14 @@ public:
    */
   bool hasGradient() const { return need_gradient && !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 */
-  std::string name; /**< name of the parameter */
+  bool alloc_now; /**< if the tensor should be allocated instantly */
 };
 
 } // namespace nntrainer