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

【PFCC算子性能优化】 SeluKernel Optimization #44490

Merged
merged 5 commits into from
Aug 2, 2022
Merged
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
44 changes: 44 additions & 0 deletions paddle/phi/kernels/funcs/activation_functor.h
Original file line number Diff line number Diff line change
Expand Up @@ -2158,6 +2158,50 @@ struct CudaExpFunctor<double> : public BaseActivationFunctor<double> {
}
};

template <typename T>
struct CudaSeluFunctor : public BaseActivationFunctor<T> {
typename BaseActivationFunctor<T>::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<T>(0.0f);
};

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

对于以下内置变量可以用private关键字标注下:

 private :
    float scale;
    float alpha;
    double zero = static_cast<double>(0.0f);

template <>
struct CudaSeluFunctor<double> : public BaseActivationFunctor<double> {
typename BaseActivationFunctor<double>::AttrPair GetAttrs() {
return {{"scale", &scale}, {"alpha", &alpha}};
}

__device__ __forceinline__ double operator()(const double x) const {
double res = x;
double alpha_cast = static_cast<double>(alpha);
double scale_cast = static_cast<double>(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<double>(0.0f);
};

template <typename T>
struct CudaSquareFunctor : public BaseActivationFunctor<T> {
// square(x) = x * x
Expand Down
2 changes: 2 additions & 0 deletions paddle/phi/kernels/gpu/activation_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename T, typename Context>
void HardSwishKernel(const Context& dev_ctx,
Expand Down Expand Up @@ -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) {}
21 changes: 0 additions & 21 deletions paddle/phi/kernels/gpu/selu_kernel.cu

This file was deleted.