Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
d623e863
P
Paddle
项目概览
机器未来
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
d623e863
编写于
11月 19, 2019
作者:
A
Adam
提交者:
Tao Luo
11月 19, 2019
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix GELU grad error (#21204)
test=develop
上级
a152315b
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
20 addition
and
29 deletion
+20
-29
paddle/fluid/operators/activation_op.h
paddle/fluid/operators/activation_op.h
+20
-29
未找到文件。
paddle/fluid/operators/activation_op.h
浏览文件 @
d623e863
...
@@ -340,10 +340,8 @@ struct GeluFunctor : public BaseActivationFunctor<T> {
...
@@ -340,10 +340,8 @@ struct GeluFunctor : public BaseActivationFunctor<T> {
}
}
};
};
// gelu_grad(x) = dout * (0.5 * (1 + erf(x / sqrt(2))) + 0.5 * 2 / sqrt(pie) /
// gelu_grad(x) = dout * (0.5 * (1 + erf(x / sqrt(2))) + 0.5 * 2 / sqrt(pi) /
// sqrt(2) * x * exp (-0.5 * sqrt(x)))
// sqrt(2) * x * exp (-0.5 * x^2))
// gelu_grad(x) = dout * (0.5 + 0.5 * erf(x * M_SQRT1_2) + (0.5 * M_2_SQRTPI *
// M_SQRT1_2) * x * exp (-0.5 * sqrt(x)))
template
<
typename
T
>
template
<
typename
T
>
struct
GeluGradFunctor
:
BaseActivationFunctor
<
T
>
{
struct
GeluGradFunctor
:
BaseActivationFunctor
<
T
>
{
template
<
typename
Device
,
typename
X
,
typename
Out
,
typename
dOut
,
template
<
typename
Device
,
typename
X
,
typename
Out
,
typename
dOut
,
...
@@ -353,42 +351,35 @@ struct GeluGradFunctor : BaseActivationFunctor<T> {
...
@@ -353,42 +351,35 @@ struct GeluGradFunctor : BaseActivationFunctor<T> {
!defined(__OSX__) && !defined(PADDLE_WITH_CUDA)
!defined(__OSX__) && !defined(PADDLE_WITH_CUDA)
auto
x_data
=
x
.
data
();
auto
x_data
=
x
.
data
();
auto
dx_data
=
dx
.
data
();
auto
dx_data
=
dx
.
data
();
auto
dout_data
=
dout
.
data
();
int
n
=
std
::
min
(
x
.
size
(),
dx
.
size
());
int
n
=
std
::
min
(
x
.
size
(),
dx
.
size
());
std
::
memset
(
dx_data
,
0
,
n
*
sizeof
(
T
));
auto
first
=
static_cast
<
T
*>
(
std
::
malloc
(
n
*
sizeof
(
T
)));
std
::
memset
(
first
,
0
,
n
*
sizeof
(
T
));
// First(dx_data) = erf(x * M_SQRT1_2)
math
::
CBlas
<
T
>::
AXPY
(
n
,
static_cast
<
T
>
(
M_SQRT1_2
),
x_data
,
1
,
dx_data
,
1
);
math
::
CBlas
<
T
>::
VMERF
(
n
,
dx_data
,
dx_data
,
VML_LA
);
// Second = 0.5 * M_2_SQRTPI * M_SQRT1_2 * x * exp (-0.5 * sqrt(x))
auto
second
=
static_cast
<
T
*>
(
std
::
malloc
(
n
*
sizeof
(
T
)));
auto
second
=
static_cast
<
T
*>
(
std
::
malloc
(
n
*
sizeof
(
T
)));
std
::
memset
(
second
,
0
,
n
*
sizeof
(
T
));
std
::
memset
(
second
,
0
,
n
*
sizeof
(
T
));
math
::
CBlas
<
T
>::
VSQUARE
(
n
,
x_data
,
second
);
// first = (0.5 * (1 + erf(x / sqrt(2))))
math
::
CBlas
<
T
>::
AXPY
(
n
,
static_cast
<
T
>
(
M_SQRT1_2
),
x_data
,
1
,
first
,
1
);
math
::
CBlas
<
T
>::
VMERF
(
n
,
first
,
first
,
VML_LA
);
for
(
int
i
=
0
;
i
<
n
;
i
++
)
{
for
(
int
i
=
0
;
i
<
n
;
i
++
)
{
second
[
i
]
*=
static_cast
<
T
>
(
-
0.5
);
first
[
i
]
+=
static_cast
<
T
>
(
1
);
}
}
math
::
CBlas
<
T
>::
SCAL
(
n
,
static_cast
<
T
>
(
0.5
),
first
,
1
);
// second = (0.5 * 2/sqrt(pi) * 1/sqrt(2) * x * exp(-0.5 * x^2))
math
::
CBlas
<
T
>::
VSQUARE
(
n
,
x_data
,
second
);
math
::
CBlas
<
T
>::
SCAL
(
n
,
-
static_cast
<
T
>
(
0.5
),
second
,
1
);
math
::
CBlas
<
T
>::
VEXP
(
n
,
second
,
second
);
math
::
CBlas
<
T
>::
VEXP
(
n
,
second
,
second
);
math
::
CBlas
<
T
>::
VMUL
(
n
,
x_data
,
second
,
second
);
math
::
CBlas
<
T
>::
VMUL
(
n
,
x_data
,
second
,
second
);
T
tmp
=
static_cast
<
T
>
(
0.5
)
*
static_cast
<
T
>
(
M_SQRT1_2
)
*
math
::
CBlas
<
T
>::
SCAL
(
n
,
static_cast
<
T
>
(
0.5
*
M_2_SQRTPI
*
M_SQRT1_2
),
static_cast
<
T
>
(
M_2_SQRTPI
);
second
,
1
);
for
(
int
i
=
0
;
i
<
n
;
i
++
)
{
second
[
i
]
*=
tmp
;
}
// Sum = 0.5 * First + Second
math
::
CBlas
<
T
>::
AXPY
(
n
,
static_cast
<
T
>
(
0.5
),
dx_data
,
1
,
second
,
1
);
// 0.5 + Sum
for
(
int
i
=
0
;
i
<
n
;
i
++
)
{
second
[
i
]
+=
static_cast
<
T
>
(
0.5
);
}
//
* dout
//
dx = dout * (first + second);
auto
dout_data
=
dout
.
data
(
);
math
::
CBlas
<
T
>::
VADD
(
n
,
first
,
second
,
first
);
math
::
CBlas
<
T
>::
VMUL
(
n
,
dout_data
,
second
,
dx_data
);
math
::
CBlas
<
T
>::
VMUL
(
n
,
dout_data
,
first
,
dx_data
);
std
::
free
(
first
);
std
::
free
(
second
);
std
::
free
(
second
);
#else
#else
auto
first
=
static_cast
<
T
>
(
0.5
)
*
auto
first
=
static_cast
<
T
>
(
0.5
)
*
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录