Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
飞不过沧海
Paddle
提交
e5e52249
P
Paddle
项目概览
飞不过沧海
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
e5e52249
编写于
12月 09, 2020
作者:
L
Leo Chen
提交者:
GitHub
12月 09, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
make gelu fp16 computing more robust (#29484)
上级
8094ac68
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
73 addition
and
20 deletion
+73
-20
paddle/fluid/operators/gelu_op.h
paddle/fluid/operators/gelu_op.h
+73
-20
未找到文件。
paddle/fluid/operators/gelu_op.h
浏览文件 @
e5e52249
...
...
@@ -36,10 +36,22 @@ struct GeluFunctor {
void
operator
()(
Device
d
,
X
x
,
Out
out
,
bool
approximate
)
const
{
if
(
approximate
)
{
// gelu(x) = 0.5 * x * (1 + tanh(sqrt(2 / \pi) * (x + 0.044715 * x^{3})))
auto
temp
=
(
static_cast
<
T
>
(
M_2_SQRTPI
*
M_SQRT1_2
)
*
(
x
+
static_cast
<
T
>
(
0.044715
)
*
x
.
cube
()))
.
tanh
();
out
.
device
(
d
)
=
x
*
static_cast
<
T
>
(
0.5
)
*
(
static_cast
<
T
>
(
1
)
+
temp
);
if
(
std
::
is_same
<
T
,
platform
::
float16
>::
value
)
{
VLOG
(
4
)
<<
"cast from float16 to float before computing"
;
auto
casted_x
=
x
.
template
cast
<
float
>();
auto
temp
=
(
static_cast
<
float
>
(
M_2_SQRTPI
*
M_SQRT1_2
)
*
(
casted_x
+
static_cast
<
float
>
(
0.044715
)
*
casted_x
.
cube
()))
.
tanh
();
out
.
device
(
d
)
=
(
casted_x
*
static_cast
<
float
>
(
0.5
)
*
(
static_cast
<
float
>
(
1
)
+
temp
))
.
template
cast
<
T
>();
}
else
{
auto
temp
=
(
static_cast
<
T
>
(
M_2_SQRTPI
*
M_SQRT1_2
)
*
(
x
+
static_cast
<
T
>
(
0.044715
)
*
x
.
cube
()))
.
tanh
();
out
.
device
(
d
)
=
x
*
static_cast
<
T
>
(
0.5
)
*
(
static_cast
<
T
>
(
1
)
+
temp
);
}
}
else
{
#if defined(PADDLE_WITH_MKLML) && !defined(_WIN32) && !defined(__APPLE__) && \
!defined(__OSX__) && !defined(PADDLE_WITH_CUDA)
...
...
@@ -60,8 +72,17 @@ struct GeluFunctor {
}
#else
// gelu(x) = 0.5 * x * (1 + erf(x / sqrt(2)))
auto
temp
=
(
x
*
static_cast
<
T
>
(
M_SQRT1_2
)).
erf
();
out
.
device
(
d
)
=
x
*
static_cast
<
T
>
(
0.5
)
*
(
static_cast
<
T
>
(
1
)
+
temp
);
if
(
std
::
is_same
<
T
,
platform
::
float16
>::
value
)
{
VLOG
(
4
)
<<
"cast from float16 to float before computing"
;
auto
casted_x
=
x
.
template
cast
<
float
>();
auto
temp
=
(
casted_x
*
static_cast
<
float
>
(
M_SQRT1_2
)).
erf
();
out
.
device
(
d
)
=
(
casted_x
*
static_cast
<
float
>
(
0.5
)
*
(
static_cast
<
float
>
(
1
)
+
temp
))
.
template
cast
<
T
>();
}
else
{
auto
temp
=
(
x
*
static_cast
<
T
>
(
M_SQRT1_2
)).
erf
();
out
.
device
(
d
)
=
x
*
static_cast
<
T
>
(
0.5
)
*
(
static_cast
<
T
>
(
1
)
+
temp
);
}
#endif
}
}
...
...
@@ -72,13 +93,32 @@ struct GeluGradFunctor {
template
<
typename
Device
,
typename
X
,
typename
dOut
,
typename
dX
>
void
operator
()(
Device
d
,
X
x
,
dOut
dout
,
dX
dx
,
bool
approximate
)
const
{
if
(
approximate
)
{
const
T
kAlpha
=
static_cast
<
T
>
(
M_2_SQRTPI
*
M_SQRT1_2
);
const
T
kBeta
=
kAlpha
*
static_cast
<
T
>
(
0.044715
)
*
static_cast
<
T
>
(
3
);
const
auto
y
=
(
kAlpha
*
((
static_cast
<
T
>
(
0.044715
)
*
x
.
cube
())
+
x
)).
tanh
();
dx
.
device
(
d
)
=
static_cast
<
T
>
(
0.5
)
*
dout
*
(
static_cast
<
T
>
(
1
)
+
y
+
(
x
-
x
*
y
.
square
())
*
(
kAlpha
+
kBeta
*
x
.
square
()));
if
(
std
::
is_same
<
T
,
platform
::
float16
>::
value
)
{
VLOG
(
4
)
<<
"cast from float16 to float before computing"
;
auto
casted_x
=
x
.
template
cast
<
float
>();
auto
casted_dout
=
dout
.
template
cast
<
float
>();
const
float
kAlpha
=
static_cast
<
float
>
(
M_2_SQRTPI
*
M_SQRT1_2
);
const
float
kBeta
=
kAlpha
*
static_cast
<
float
>
(
0.044715
)
*
static_cast
<
float
>
(
3
);
const
auto
y
=
(
kAlpha
*
((
static_cast
<
float
>
(
0.044715
)
*
casted_x
.
cube
())
+
casted_x
))
.
tanh
();
dx
.
device
(
d
)
=
(
static_cast
<
float
>
(
0.5
)
*
casted_dout
*
(
static_cast
<
float
>
(
1
)
+
y
+
(
casted_x
-
casted_x
*
y
.
square
())
*
(
kAlpha
+
kBeta
*
casted_x
.
square
())))
.
template
cast
<
T
>();
}
else
{
const
T
kAlpha
=
static_cast
<
T
>
(
M_2_SQRTPI
*
M_SQRT1_2
);
const
T
kBeta
=
kAlpha
*
static_cast
<
T
>
(
0.044715
)
*
static_cast
<
T
>
(
3
);
const
auto
y
=
(
kAlpha
*
((
static_cast
<
T
>
(
0.044715
)
*
x
.
cube
())
+
x
)).
tanh
();
dx
.
device
(
d
)
=
static_cast
<
T
>
(
0.5
)
*
dout
*
(
static_cast
<
T
>
(
1
)
+
y
+
(
x
-
x
*
y
.
square
())
*
(
kAlpha
+
kBeta
*
x
.
square
()));
}
}
else
{
#if defined(PADDLE_WITH_MKLML) && !defined(_WIN32) && !defined(__APPLE__) && \
!defined(__OSX__) && !defined(PADDLE_WITH_CUDA)
...
...
@@ -117,13 +157,26 @@ struct GeluGradFunctor {
#else
// gelu_grad(x) = dout * 0.5 * (1 + erf(x / sqrt(2)) + x * sqrt(2 / pi) *
// exp(- x^2 / 2)
auto
first
=
static_cast
<
T
>
(
0.5
)
*
(
static_cast
<
T
>
(
1
)
+
((
x
*
static_cast
<
T
>
(
M_SQRT1_2
)).
erf
()));
auto
second
=
static_cast
<
T
>
(
0.5
*
M_2_SQRTPI
*
M_SQRT1_2
)
*
x
*
(
-
static_cast
<
T
>
(
0.5
)
*
x
.
square
()).
exp
();
dx
.
device
(
d
)
=
dout
*
(
first
+
second
);
if
(
std
::
is_same
<
T
,
platform
::
float16
>::
value
)
{
VLOG
(
4
)
<<
"cast from float16 to float before computing"
;
auto
casted_x
=
x
.
template
cast
<
float
>();
auto
casted_dout
=
dout
.
template
cast
<
float
>();
auto
first
=
static_cast
<
float
>
(
0.5
)
*
(
static_cast
<
float
>
(
1
)
+
((
casted_x
*
static_cast
<
float
>
(
M_SQRT1_2
)).
erf
()));
auto
second
=
static_cast
<
float
>
(
0.5
*
M_2_SQRTPI
*
M_SQRT1_2
)
*
casted_x
*
(
-
static_cast
<
float
>
(
0.5
)
*
casted_x
.
square
()).
exp
();
dx
.
device
(
d
)
=
(
casted_dout
*
(
first
+
second
)).
template
cast
<
T
>();
}
else
{
auto
first
=
static_cast
<
T
>
(
0.5
)
*
(
static_cast
<
T
>
(
1
)
+
((
x
*
static_cast
<
T
>
(
M_SQRT1_2
)).
erf
()));
auto
second
=
static_cast
<
T
>
(
0.5
*
M_2_SQRTPI
*
M_SQRT1_2
)
*
x
*
(
-
static_cast
<
T
>
(
0.5
)
*
x
.
square
()).
exp
();
dx
.
device
(
d
)
=
dout
*
(
first
+
second
);
}
#endif
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录