} 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);
// 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
// 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
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
/**
* @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
* @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)){};
/**
* @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){};
/**
* @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
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>();
}
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_);
}
}