From: Parichay Kapoor Date: Mon, 1 Feb 2021 07:26:59 +0000 (+0900) Subject: [tensor] Split tensor constructor into 2 X-Git-Tag: accepted/tizen/unified/20210210.052156~11 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=3e45e83163de4ab927641adbb3f5fe39eeaa7521;p=platform%2Fcore%2Fml%2Fnntrainer.git [tensor] Split tensor constructor into 2 Create a new tensor constructor for lazy allocation than adding the lazy allocation feature to the existing one Signed-off-by: Parichay Kapoor --- diff --git a/nntrainer/tensor/manager.cpp b/nntrainer/tensor/manager.cpp index 2db24a4..053a4d1 100644 --- a/nntrainer/tensor/manager.cpp +++ b/nntrainer/tensor/manager.cpp @@ -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 diff --git a/nntrainer/tensor/tensor.cpp b/nntrainer/tensor/tensor.cpp index 2992aa2..6008014 100644 --- a/nntrainer/tensor/tensor.cpp +++ b/nntrainer/tensor/tensor.cpp @@ -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 diff --git a/nntrainer/tensor/tensor.h b/nntrainer/tensor/tensor.h index b069daa..e05b31f 100644 --- a/nntrainer/tensor/tensor.h +++ b/nntrainer/tensor/tensor.h @@ -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 diff --git a/nntrainer/tensor/var_grad.cpp b/nntrainer/tensor/var_grad.cpp index 9a72be7..d6d692f 100644 --- a/nntrainer/tensor/var_grad.cpp +++ b/nntrainer/tensor/var_grad.cpp @@ -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(dim, nullptr, alloc_now); + var = std::make_shared(dim, alloc_now); if (trainable) - grad = std::make_shared(dim, nullptr, alloc_now); + grad = std::make_shared(dim, alloc_now); else grad = std::make_shared(); } @@ -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(var->getDim(), nullptr, alloc_now_); + grad = std::make_shared(var->getDim(), alloc_now_); } }