Skip to content

Commit

Permalink
[ Tensor ] remove sscal to set zero.
Browse files Browse the repository at this point in the history
We do need to remove the Nan or Inf value in Tensor by call setZero().
However, if we using sscal, then Nan or Inf values are remain still.
This PR change the sscal to memset.

Resolves:

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

Signed-off-by: jijoong.moon <[email protected]>
  • Loading branch information
jijoongmoon committed Jun 9, 2024
1 parent b154146 commit 8e06368
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
3 changes: 1 addition & 2 deletions nntrainer/layers/layer_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ InitLayerContext::InitLayerContext(
prefix(prefix_),
tensor_type(tensor_type_),
loss_scale(loss_scale_),
mode(mode_){
mode(mode_) {
NNTR_THROW_IF(!validate(), std::invalid_argument)
<< "Invalid init context name: " << name
<< " num inputs: " << getNumInputs();
Expand Down Expand Up @@ -292,7 +292,6 @@ const Tensor RunLayerContext::getIncomingDerivative(unsigned int idx) const {
return getOutputGrad(idx);
}


/**
* @brief Get the Input tensor object
*
Expand Down
4 changes: 2 additions & 2 deletions nntrainer/tensor/manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -455,8 +455,8 @@ std::vector<Weight *> Manager::requestWeights(
* reduce the memory.
*/
bool is_wgrad = true;
if (Weight::isGradientClipByGlobalNorm(clip_by_global_norm))
is_wgrad = false;
// if (Weight::isGradientClipByGlobalNorm(clip_by_global_norm))
// is_wgrad = false;
grad = tensor_pool.request(name + Var_Grad::grad_suffix, dim_g,
grad_exec_order, grad_ls,
Tensor::Initializer::ZEROS, is_wgrad);
Expand Down
8 changes: 6 additions & 2 deletions nntrainer/tensor/tensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3319,13 +3319,17 @@ void Tensor::setValue(float val) {
void Tensor::setZero() {
if (dim.getDataType() == ml::train::TensorDim::DataType::FP32) {
if (contiguous)
sscal(size(), 0, getData<float>(), 1);
// sscal(size(), 0, getData<float>(), 1);
/// @note we cannot use sscal, when we set zero. if the data is inf or
/// NaN, then the inf or NaN still remain.
memset(getData<_FP16>(), 0, sizeof(float) * size());
else
apply_i<float>([](float val) -> float { return 0; });
} else if (dim.getDataType() == ml::train::TensorDim::DataType::FP16) {
#ifdef ENABLE_FP16
if (contiguous)
sscal(size(), 0, getData<_FP16>(), 1);
// sscal(size(), 0, getData<_FP16>(), 1);
memset(getData<_FP16>(), 0, sizeof(_FP16) * size());
else
apply_i<_FP16>([](_FP16 val) -> _FP16 { return 0; });
#else
Expand Down

0 comments on commit 8e06368

Please sign in to comment.