/* Copyright (c) 2020 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/fluid/operators/amp/fp16_type_traits.h" #include "paddle/fluid/operators/elementwise/elementwise_op_broadcast.cu.h" #include "paddle/fluid/operators/gelu_op.h" namespace paddle { namespace operators { template struct GeluWithApproximateFunctor { using MPType = typename details::MPTypeTrait::Type; inline HOSTDEVICE T operator()(T arg_x) { // this function is tanh approximation of gelu MPType x = static_cast(arg_x); MPType one = static_cast(1); MPType half = static_cast(0.5); MPType kAlpha = static_cast(M_2_SQRTPI * M_SQRT1_2); auto tanh_out = tanh(kAlpha * x * (one + static_cast(GELU_CONSTANT) * x * x)); MPType out = x * half * (one + tanh_out); return static_cast(out); } }; template struct GeluWithoutApproximateFunctor { using MPType = typename details::MPTypeTrait::Type; inline HOSTDEVICE T operator()(T arg_x) { // actual gelu with approximation = false MPType x = static_cast(arg_x); return static_cast(x * normcdf(x)); } }; template class GeluKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& context) const override { auto* out = context.Output("Out"); auto* in = context.Input("X"); auto approximate = context.Attr("approximate"); out->mutable_data(in->place()); std::vector ins = {in}; std::vector outs = {out}; const auto& dev_ctx = context.template device_context(); if (approximate) { LaunchElementwiseCudaKernel( dev_ctx, ins, &outs, 0, GeluWithApproximateFunctor()); } else { LaunchElementwiseCudaKernel( dev_ctx, ins, &outs, 0, GeluWithoutApproximateFunctor()); } } }; template struct GeluWithApproximateGradFunctor { using MPType = typename details::MPTypeTrait::Type; inline HOSTDEVICE T operator()(T arg_x, T arg_dout) { MPType x = static_cast(arg_x); MPType dout = static_cast(arg_dout); MPType one = static_cast(1); MPType half = static_cast(0.5); MPType kAlpha = static_cast(M_2_SQRTPI * M_SQRT1_2); MPType kBeta = kAlpha * static_cast(GELU_CONSTANT) * static_cast(3); auto cube_x = x * x * x; auto tanh_out = tanh(kAlpha * ((static_cast(GELU_CONSTANT) * cube_x) + x)); auto ans = half * (one + tanh_out + (one - tanh_out * tanh_out) * (x * kAlpha + kBeta * cube_x)); return static_cast(ans * dout); } }; template struct GeluWithoutApproximateGradFunctor { using MPType = typename details::MPTypeTrait::Type; inline HOSTDEVICE T operator()(T arg_x, T arg_dout) { MPType x = static_cast(arg_x); MPType dout = static_cast(arg_dout); constexpr MPType kBeta = M_2_SQRTPI * M_SQRT1_2 * static_cast(0.5); const MPType cdf = normcdf(x); const MPType pdf = exp(static_cast(-0.5) * x * x) * kBeta; return static_cast(dout * (cdf + x * pdf)); } }; template class GeluGradKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& context) const override { auto* x = context.Input("X"); auto* dout = context.Input(framework::GradVarName("Out")); auto* dx = context.Output(framework::GradVarName("X")); auto approximate = context.Attr("approximate"); dx->mutable_data(dout->place()); std::vector ins = {x, dout}; std::vector outs = {dx}; const auto& dev_ctx = context.template device_context(); if (approximate) { LaunchElementwiseCudaKernel( dev_ctx, ins, &outs, 0, GeluWithApproximateGradFunctor()); } else { LaunchElementwiseCudaKernel( dev_ctx, ins, &outs, 0, GeluWithoutApproximateGradFunctor()); } } }; } // namespace operators } // namespace paddle namespace ops = paddle::operators; REGISTER_OP_CUDA_KERNEL( gelu, ops::GeluKernel, ops::GeluKernel, ops::GeluKernel); REGISTER_OP_CUDA_KERNEL( gelu_grad, ops::GeluGradKernel, ops::GeluGradKernel, ops::GeluGradKernel);