|
| 1 | +// Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include <glog/logging.h> |
| 16 | + |
| 17 | +#include <algorithm> |
| 18 | +#include <cmath> |
| 19 | +#include <memory> |
| 20 | +#include <string> |
| 21 | +#include <unordered_set> |
| 22 | +#include <utility> |
| 23 | +#include <vector> |
| 24 | +#ifndef _USE_MATH_DEFINES |
| 25 | +#define _USE_MATH_DEFINES |
| 26 | +#endif |
| 27 | + |
| 28 | +#include <type_traits> |
| 29 | + |
| 30 | +#include "paddle/phi/common/float16.h" |
| 31 | +#include "paddle/phi/core/kernel_registry.h" |
| 32 | +#include "paddle/phi/core/tensor_utils.h" |
| 33 | +#include "paddle/phi/kernels/funcs/activation_functor.h" |
| 34 | +#include "paddle/phi/kernels/funcs/blas/blas.h" |
| 35 | +#include "paddle/phi/kernels/funcs/eigen/common.h" |
| 36 | + |
| 37 | +namespace phi { |
| 38 | + |
| 39 | +template <typename T> |
| 40 | +struct SoftReluGradFunctor { |
| 41 | + float threshold; |
| 42 | + void SetAttrs(float threshold_) { threshold = threshold_; } |
| 43 | + |
| 44 | + template <typename Device, |
| 45 | + typename X, |
| 46 | + typename Out, |
| 47 | + typename dOut, |
| 48 | + typename dX> |
| 49 | + void operator()(Device d, X x UNUSED, Out out, dOut dout, dX dx) { |
| 50 | + auto tmp = static_cast<T>(threshold); |
| 51 | + auto temp = ((out > -tmp) * (out < tmp)).template cast<T>(); |
| 52 | + dx.device(d) = dout * (static_cast<T>(1) - (-out).exp()) * temp; |
| 53 | + } |
| 54 | +}; |
| 55 | + |
| 56 | +template <typename T, typename Context> |
| 57 | +void SoftmaxGradKernel(const Context& dev_ctx, |
| 58 | + const DenseTensor& x_in, |
| 59 | + const DenseTensor& out_in, |
| 60 | + const DenseTensor& out_grad, |
| 61 | + float threshold, |
| 62 | + DenseTensor* x_grad) { |
| 63 | + dev_ctx.template Alloc<T>(x_grad); |
| 64 | + auto dout = phi::EigenVector<T>::Flatten(out_grad); |
| 65 | + auto out = phi::EigenVector<T>::Flatten(out_in); |
| 66 | + auto dx = phi::EigenVector<T>::Flatten(*x_grad); |
| 67 | + auto x = phi::EigenVector<T>::Flatten(x_in); |
| 68 | + auto* eigen_dev = dev_ctx.eigen_device(); |
| 69 | + SoftReluGradFunctor<T> functor; |
| 70 | + functor.SetAttrs(threshold); |
| 71 | + // use 32bit index to speed up computation |
| 72 | + bool use_32bit_index = out.size() < Eigen::NumTraits<int>::highest(); |
| 73 | + bool is_gpu_place = dev_ctx.GetPlace().GetType() == phi::AllocationType::GPU; |
| 74 | + if (use_32bit_index && is_gpu_place) { |
| 75 | + functor(*eigen_dev, |
| 76 | + To32BitIndex(x), |
| 77 | + To32BitIndex(out), |
| 78 | + To32BitIndex(dout), |
| 79 | + To32BitIndex(dx)); |
| 80 | + } else { |
| 81 | + functor(*eigen_dev, x, out, dout, dx); |
| 82 | + } |
| 83 | +} |
| 84 | +} // namespace phi |
| 85 | + |
| 86 | +PD_REGISTER_KERNEL( |
| 87 | + soft_relu_grad, CPU, ALL_LAYOUT, phi::SoftmaxGradKernel, float, double) {} |
0 commit comments