From b09769c329068b303d43150f7bd3c04df97f1fe9 Mon Sep 17 00:00:00 2001 From: yuanwei66 Date: Thu, 22 Apr 2021 11:10:13 +0800 Subject: [PATCH] convert the implementation of simoidGrad TanhGrad AbsGrad CPU ops to nnacl --- .../cpu/eltwise_grad_cpu_kernel.cc | 33 ++++++++++++------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/mindspore/ccsrc/backend/kernel_compiler/cpu/eltwise_grad_cpu_kernel.cc b/mindspore/ccsrc/backend/kernel_compiler/cpu/eltwise_grad_cpu_kernel.cc index fb9f1d88a0..41e2d375de 100644 --- a/mindspore/ccsrc/backend/kernel_compiler/cpu/eltwise_grad_cpu_kernel.cc +++ b/mindspore/ccsrc/backend/kernel_compiler/cpu/eltwise_grad_cpu_kernel.cc @@ -19,6 +19,7 @@ #include "common/thread_pool.h" #include "runtime/device/cpu/cpu_device_address.h" #include "nnacl/fp32_grad/activation_grad.h" +#include "nnacl/fp32_grad/arithmetic_grad.h" #include "nnacl/errorcode.h" namespace mindspore { @@ -49,21 +50,25 @@ void EltWiseGradCPUKernel::ReLU6Grad(const T *input1, const T *input2, T *out template void EltWiseGradCPUKernel::AbsGrad(const T *input1, const T *input2, T *out, size_t start, size_t end) { - for (size_t i = start; i < end; i++) { - if (input1[i] > 0) { - out[i] = input2[i]; - } else if (input1[i] < 0) { - out[i] = -input2[i]; - } else { - out[i] = 0; + if constexpr (std::is_same_v) { + int ret = ::ElementAbsGrad(input1 + start, input2 + start, out + start, end - start); + if (ret == NNACL_ERR) { + MS_LOG(EXCEPTION) << "AbsGrad failed."; } + } else { + MS_LOG(EXCEPTION) << "AbsGrad only support float"; } } template void EltWiseGradCPUKernel::SigmoidGrad(const T *input1, const T *input2, T *out, size_t start, size_t end) { - for (size_t i = start; i < end; i++) { - out[i] = input2[i] * input1[i] * (1 - input1[i]); + if constexpr (std::is_same_v) { + int ret = ::SigmoidGrad(input2 + start, input1 + start, end - start, out + start); + if (ret == NNACL_ERR) { + MS_LOG(EXCEPTION) << "SigmoidGrad failed."; + } + } else { + MS_LOG(EXCEPTION) << "SigmoidGrad only support float"; } } @@ -76,9 +81,13 @@ void EltWiseGradCPUKernel::SqrtGrad(const T *input1, const T *input2, T *out, template void EltWiseGradCPUKernel::TanhGrad(const T *input1, const T *input2, T *out, size_t start, size_t end) { - for (size_t i = start; i < end; i++) { - T tmp = input1[i] * input1[i]; - out[i] = input2[i] * (1 - tmp); + if constexpr (std::is_same_v) { + int ret = ::TanhGrad(input2 + start, input1 + start, end - start, out + start); + if (ret == NNACL_ERR) { + MS_LOG(EXCEPTION) << "TanhGrad failed."; + } + } else { + MS_LOG(EXCEPTION) << "TanhGrad only support float"; } }