diff --git a/paddle/phi/kernels/funcs/activation_functor.h b/paddle/phi/kernels/funcs/activation_functor.h index ee79cafd155dcfbdedf66ca4a56dd299d6cb6ab4..542c59bec1b453963f27a73cb0b43d5a7b70b60e 100644 --- a/paddle/phi/kernels/funcs/activation_functor.h +++ b/paddle/phi/kernels/funcs/activation_functor.h @@ -2158,6 +2158,50 @@ struct CudaExpFunctor : public BaseActivationFunctor { } }; +template +struct CudaSeluFunctor : public BaseActivationFunctor { + typename BaseActivationFunctor::AttrPair GetAttrs() { + return {{"scale", &scale}, {"alpha", &alpha}}; + } + + __device__ __forceinline__ T operator()(const T x) const { + T res = x; + if (res <= zero) { + res = alpha * expf(res) - alpha; + } + res *= scale; + return res; + } + + private: + float scale; + float alpha; + T zero = static_cast(0.0f); +}; + +template <> +struct CudaSeluFunctor : public BaseActivationFunctor { + typename BaseActivationFunctor::AttrPair GetAttrs() { + return {{"scale", &scale}, {"alpha", &alpha}}; + } + + __device__ __forceinline__ double operator()(const double x) const { + double res = x; + double alpha_cast = static_cast(alpha); + double scale_cast = static_cast(scale); + if (res <= zero) { + res = alpha_cast * exp(res) - alpha_cast; + } + res *= scale_cast; + return res; + } + + private: + float scale; + float alpha; + double zero = static_cast(0.0f); +}; + template struct CudaSquareFunctor : public BaseActivationFunctor { // square(x) = x * x diff --git a/paddle/phi/kernels/gpu/activation_kernel.cu b/paddle/phi/kernels/gpu/activation_kernel.cu index 6e116a3e157503563123bd096466231747763005..0e9e754a99706675ae1f130aa118246f137ba5cf 100644 --- a/paddle/phi/kernels/gpu/activation_kernel.cu +++ b/paddle/phi/kernels/gpu/activation_kernel.cu @@ -132,6 +132,7 @@ DEFINE_GPU_ACT_KERNEL_WITH_TWO_ATTRS(HardSigmoid, CudaHardSigmoidFunctor, slope, offset) +DEFINE_GPU_ACT_KERNEL_WITH_TWO_ATTRS(Selu, CudaSeluFunctor, scale, alpha) template void HardSwishKernel(const Context& dev_ctx, @@ -265,3 +266,4 @@ PD_REGISTER_KERNEL(pow, int, int64_t, phi::dtype::float16) {} +PD_REGISTER_KERNEL(selu, GPU, ALL_LAYOUT, phi::SeluKernel, float, double) {} diff --git a/paddle/phi/kernels/gpu/selu_kernel.cu b/paddle/phi/kernels/gpu/selu_kernel.cu deleted file mode 100644 index 99303d8c18a97893a939a8f358bac02603fae329..0000000000000000000000000000000000000000 --- a/paddle/phi/kernels/gpu/selu_kernel.cu +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/phi/kernels/selu_kernel.h" - -#include "paddle/phi/backends/gpu/gpu_context.h" -#include "paddle/phi/core/kernel_registry.h" -#include "paddle/phi/kernels/impl/selu_kernel_impl.h" - -PD_REGISTER_KERNEL(selu, GPU, ALL_LAYOUT, phi::SeluKernel, float, double) {}