Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
aff43684
P
Paddle
项目概览
PaddlePaddle
/
Paddle
接近 1 年 前同步成功
通知
2292
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
aff43684
编写于
12月 21, 2021
作者:
C
crystal
提交者:
GitHub
12月 21, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
use elementwise to optimize gelu forward implementation on GPU (#38188)
* relu forward opt * add gelu functor * optimize code
上级
d9780a22
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
59 addition
and
0 deletion
+59
-0
paddle/fluid/operators/gelu_op.cu
paddle/fluid/operators/gelu_op.cu
+59
-0
未找到文件。
paddle/fluid/operators/gelu_op.cu
浏览文件 @
aff43684
...
...
@@ -12,9 +12,68 @@ 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"
#include "paddle/fluid/platform/float16.h"
namespace
paddle
{
namespace
operators
{
template
<
typename
T
>
struct
GeluWithApproximateFunctor
{
using
MPType
=
typename
details
::
MPTypeTrait
<
T
>::
Type
;
inline
HOSTDEVICE
T
operator
()(
T
arg_x
)
{
// this function is tanh approximation of gelu
MPType
x
=
static_cast
<
MPType
>
(
arg_x
);
MPType
one
=
static_cast
<
MPType
>
(
1
);
MPType
out
=
x
*
static_cast
<
MPType
>
(
0.5
)
*
(
one
+
tanh
(
static_cast
<
MPType
>
(
0.79788456
)
*
x
*
(
one
+
static_cast
<
MPType
>
(
0.044715
)
*
x
*
x
)));
return
static_cast
<
T
>
(
out
);
}
};
template
<
typename
T
>
struct
GeluWithoutApproximateFunctor
{
using
MPType
=
typename
details
::
MPTypeTrait
<
T
>::
Type
;
inline
HOSTDEVICE
T
operator
()(
T
arg_x
)
{
// actual gelu with approximation = false
MPType
x
=
static_cast
<
MPType
>
(
arg_x
);
MPType
erf_out
=
erf
(
x
*
static_cast
<
MPType
>
(
M_SQRT1_2
));
MPType
out
=
x
*
static_cast
<
MPType
>
(
0.5
)
*
(
static_cast
<
MPType
>
(
1
)
+
erf_out
);
return
static_cast
<
T
>
(
out
);
}
};
template
<
typename
T
>
class
GeluKernel
<
platform
::
CUDADeviceContext
,
T
>
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
auto
*
out
=
context
.
Output
<
framework
::
Tensor
>
(
"Out"
);
auto
*
in
=
context
.
Input
<
framework
::
Tensor
>
(
"X"
);
auto
approximate
=
context
.
Attr
<
bool
>
(
"approximate"
);
out
->
mutable_data
<
T
>
(
in
->
place
());
std
::
vector
<
const
framework
::
Tensor
*>
ins
=
{
in
};
std
::
vector
<
framework
::
Tensor
*>
outs
=
{
out
};
const
auto
&
dev_ctx
=
context
.
template
device_context
<
platform
::
CUDADeviceContext
>();
if
(
approximate
)
{
LaunchElementwiseCudaKernel
<
ElementwiseType
::
kBinary
,
T
,
T
>
(
dev_ctx
,
ins
,
&
outs
,
0
,
GeluWithApproximateFunctor
<
T
>
());
}
else
{
LaunchElementwiseCudaKernel
<
ElementwiseType
::
kBinary
,
T
,
T
>
(
dev_ctx
,
ins
,
&
outs
,
0
,
GeluWithoutApproximateFunctor
<
T
>
());
}
}
};
}
// namespace operators
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
REGISTER_OP_CUDA_KERNEL
(
gelu
,
ops
::
GeluKernel
<
paddle
::
platform
::
CUDADeviceContext
,
float
>
,
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录