Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Tensor] Proposal for unifying tensor addition: remove add_i implementation #2858

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading