Skip to content

Commit

Permalink
[Tensor] Proposal for unifying tensor addition: remove add_i implemen…
Browse files Browse the repository at this point in the history
…ation

This pull request proposes the removal of the add_i implementation and its unification with the add function. Currently, the use of two distinct kernels for tensor addition can result in performance inefficiencies. To enhance overall performance, it is crucial to address this issue. Additionally, we will need to undertake follow-up work to successfully integrate ele_add and sxapy.

**Self-evaluation:**
1. Build test: [X]Passed [ ]Failed [ ]Skipped
2. Run test:   [X]Passed [ ]Failed [ ]Skipped

Signed-off-by: Donghyeon Jeong <[email protected]>
  • Loading branch information
djeong20 committed Jan 7, 2025
1 parent 252b2df commit 4d20835
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 57 deletions.
15 changes: 0 additions & 15 deletions nntrainer/tensor/float_tensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -440,21 +440,6 @@ Tensor &FloatTensor::add_strided(Tensor const &input, Tensor &output,
return output;
}

int FloatTensor::add_i(Tensor const &m, Tensor &output, float const alpha) {
auto f = [&](const BroadcastInfo &e, const float *buf, const float *m_buf,
float *out_buf) {
saxpy(e.buffer_size, alpha, m_buf, e.strides[3], out_buf, strides[3]);
};

try {
apply_broadcast(m, f, output);
} catch (std::exception &err) {
ml_loge("%s %s", typeid(err).name(), err.what());
return ML_ERROR_INVALID_PARAMETER;
}
return ML_ERROR_NONE;
}

int FloatTensor::add_i_partial(unsigned int len, unsigned int addr_idx,
Tensor &m, unsigned int incX, unsigned int incY,
const Tensor alphas, unsigned int alpha_idx) {
Expand Down
5 changes: 0 additions & 5 deletions nntrainer/tensor/float_tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -308,11 +308,6 @@ class FloatTensor : public TensorBase {
Tensor &add_strided(Tensor const &input, Tensor &output,
const float beta) const override;

/**
* @copydoc Tensor::add_i(Tensor const &m, float const alpha)
*/
int add_i(Tensor const &m, Tensor &output, float const alpha) override;

/**
* @copydoc Tensor::add_i_partial()
*/
Expand Down
16 changes: 0 additions & 16 deletions nntrainer/tensor/half_tensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,22 +422,6 @@ Tensor &HalfTensor::add_strided(Tensor const &input, Tensor &output,
return output;
}

int HalfTensor::add_i(Tensor const &m, Tensor &output, float const alpha) {
auto f = [&](const BroadcastInfo &e, const _FP16 *buf, const _FP16 *m_buf,
_FP16 *out_buf) {
saxpy(e.buffer_size, alpha, m_buf, e.strides[3], out_buf, strides[3]);
/// @todo: saxpy is not valid for _FP16
};

try {
apply_broadcast(m, f, output);
} catch (std::exception &err) {
ml_loge("%s %s", typeid(err).name(), err.what());
return ML_ERROR_INVALID_PARAMETER;
}
return ML_ERROR_NONE;
}

int HalfTensor::add_i_partial(unsigned int len, unsigned int addr_idx,
Tensor &m, unsigned int incX, unsigned int incY,
const Tensor alphas, unsigned int alpha_idx) {
Expand Down
5 changes: 0 additions & 5 deletions nntrainer/tensor/half_tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -308,11 +308,6 @@ class HalfTensor : public TensorBase {
Tensor &add_strided(Tensor const &input, Tensor &output,
const float beta) const override;

/**
* @copydoc Tensor::add_i(Tensor const &m, float const alpha)
*/
int add_i(Tensor const &m, Tensor &output, float const alpha) override;

/**
* @copydoc Tensor::add_i_partial()
*/
Expand Down
17 changes: 12 additions & 5 deletions nntrainer/tensor/tensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -419,10 +419,6 @@ Tensor &Tensor::multiply(Tensor const &m, Tensor &output,
std::invalid_argument)
<< getName() << " is not contiguous, cannot multiply";

NNTR_THROW_IF(!getContiguous() || !m.getContiguous() ||
!output.getContiguous(),
std::invalid_argument)
<< getName() << " is not contiguous, cannot multiply";
itensor->multiply(m, output, beta);
return output;
}
Expand Down Expand Up @@ -521,7 +517,13 @@ Tensor &Tensor::add(float const &value, Tensor &output) const {
}

int Tensor::add_i(Tensor const &m, float const alpha) {
return itensor->add_i(m, *this, alpha);
try {
itensor->add(m, *this, alpha);
} catch (std::exception &err) {
ml_loge("%s %s", typeid(err).name(), err.what());
return ML_ERROR_INVALID_PARAMETER;
}
return ML_ERROR_NONE;
}

int Tensor::add_i_partial(unsigned int len, unsigned int addr_idx, Tensor &m,
Expand All @@ -537,6 +539,11 @@ Tensor Tensor::add(Tensor const &m, float const alpha) const {
}

Tensor &Tensor::add(Tensor const &m, Tensor &output, float const alpha) const {
NNTR_THROW_IF(m.getFormat() != this->getFormat(), std::invalid_argument)
<< "Tensor Format of " << getName() << ":"
<< ((bool)(this->getFormat()) ? "NHWC" : "NCHW") << " is not match. ("
<< ((bool)(m.getFormat()) ? "NHWC" : "NCHW") << ")";

NNTR_THROW_IF(!itensor->getContiguous() || !m.getContiguous() ||
!output.getContiguous(),
std::invalid_argument)
Expand Down
6 changes: 0 additions & 6 deletions nntrainer/tensor/tensor_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -410,12 +410,6 @@ Tensor &TensorBase::add_strided(Tensor const &input, Tensor &output,
getStringDataType());
}

int TensorBase::add_i(Tensor const &m, Tensor &output, float const alpha) {
throw std::invalid_argument(
"Tensor::add_i() is currently not supported in tensor data type " +
getStringDataType());
}

int TensorBase::add_i_partial(unsigned int len, unsigned int addr_idx,
Tensor &m, unsigned int incX, unsigned int incY,
const Tensor alphas, unsigned int alpha_idx) {
Expand Down
5 changes: 0 additions & 5 deletions nntrainer/tensor/tensor_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -306,11 +306,6 @@ class TensorBase {
virtual Tensor &add_strided(Tensor const &input, Tensor &output,
const float beta) const;

/**
* @copydoc Tensor::add_i(Tensor const &m, float const alpha)
*/
virtual int add_i(Tensor const &m, Tensor &output, float const alpha);

/**
* @copydoc Tensor::add_i_partial()
*/
Expand Down

0 comments on commit 4d20835

Please sign in to comment.