Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
2add31f4
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
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看板
提交
2add31f4
编写于
3月 06, 2023
作者:
C
cxxly
提交者:
Xiaoxu Chen
3月 23, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[prim] add gelu vjp rule
上级
325fdf1d
变更
5
显示空白变更内容
内联
并排
Showing
5 changed file
with
97 addition
and
9 deletion
+97
-9
paddle/fluid/prim/api/api.yaml
paddle/fluid/prim/api/api.yaml
+2
-0
paddle/fluid/prim/api/composite_backward/composite_backward_api.h
...luid/prim/api/composite_backward/composite_backward_api.h
+79
-4
paddle/phi/api/yaml/backward.yaml
paddle/phi/api/yaml/backward.yaml
+1
-0
python/paddle/fluid/tests/unittests/prim/test_comp_custom_vjp.py
...paddle/fluid/tests/unittests/prim/test_comp_custom_vjp.py
+2
-4
python/paddle/fluid/tests/unittests/test_activation_op.py
python/paddle/fluid/tests/unittests/test_activation_op.py
+13
-1
未找到文件。
paddle/fluid/prim/api/api.yaml
浏览文件 @
2add31f4
...
...
@@ -46,3 +46,5 @@
-
where
-
reshape
-
split
-
erf
-
tanh
paddle/fluid/prim/api/composite_backward/composite_backward_api.h
浏览文件 @
2add31f4
...
...
@@ -22,8 +22,10 @@
#include "paddle/fluid/prim/api/all.h"
#include "paddle/fluid/prim/api/generated_prim/prim_generated_api.h"
#include "paddle/phi/common/amp_type_traits.h"
#include "paddle/phi/common/int_array.h"
#include "paddle/phi/core/ddim.h"
namespace
paddle
{
namespace
prim
{
using
Tensor
=
paddle
::
Tensor
;
...
...
@@ -1176,10 +1178,10 @@ void dropout_grad(const Tensor& mask,
}
else
{
if
(
mode
==
"upscale_in_train"
)
{
if
(
p
.
to
<
float
>
()
==
1.0
f
)
{
set_output
<
T
>
(
out_grad
*
0.0
,
x_grad
);
set_output
<
T
>
(
scale
<
T
>
(
out_grad
,
0.0
)
,
x_grad
);
}
else
{
set_output
<
T
>
(
out_grad
*
cast
<
T
>
(
mask
,
out_grad
.
dtype
())
/
(
1.0
-
p
.
to
<
float
>
(
)),
set_output
<
T
>
(
scale
<
T
>
(
out_grad
*
cast
<
T
>
(
mask
,
out_grad
.
dtype
()),
1.0
/
(
1.0
-
p
.
to
<
float
>
()
)),
x_grad
);
}
}
else
{
...
...
@@ -1362,5 +1364,78 @@ void batch_norm_grad(const Tensor& x,
}
}
template
<
typename
T
>
void
gelu_grad
(
const
Tensor
&
x
,
const
Tensor
&
out_grad
,
bool
approximate
,
Tensor
*
x_grad
)
{
if
(
!
x_grad
)
return
;
// Promote to fp32 when the input type is fp16 for keeping consistent with
// phi kernel
if
(
x
.
dtype
()
==
phi
::
DataType
::
FLOAT16
||
x
.
dtype
()
==
phi
::
DataType
::
BFLOAT16
)
{
auto
promoted_x
=
cast
<
T
>
(
x
,
phi
::
DataType
::
FLOAT32
);
auto
promoted_out_grad
=
cast
<
T
>
(
out_grad
,
phi
::
DataType
::
FLOAT32
);
if
(
approximate
)
{
float
kbeta
=
M_SQRT2
*
M_2_SQRTPI
*
0.5
;
float
kkappa
=
0.044715
;
auto
x_sq
=
promoted_x
*
promoted_x
;
auto
x_cube
=
x_sq
*
promoted_x
;
auto
inner
=
kbeta
*
(
promoted_x
+
kkappa
*
x_cube
);
auto
tanh_inner
=
tanh
<
T
>
(
inner
);
auto
left
=
scale
<
T
>
(
promoted_x
,
0.5
);
auto
right
=
scale
<
T
>
(
tanh_inner
,
1.
,
1.
);
auto
left_derivative
=
scale
<
T
>
(
right
,
0.5
);
auto
tanh_derivative
=
scale
<
T
>
(
tanh_inner
*
tanh_inner
,
-
1.
,
1.
);
auto
inner_derivative
=
kbeta
*
(
scale
<
T
>
(
3
*
kkappa
*
x_sq
,
1.
,
1.
));
auto
right_derivative
=
left
*
tanh_derivative
*
inner_derivative
;
set_output
<
T
>
(
cast
<
T
>
(
promoted_out_grad
*
(
left_derivative
+
right_derivative
),
x
.
type
()),
x_grad
);
}
else
{
float
kalpha
=
M_SQRT1_2
;
float
kbeta
=
M_2_SQRTPI
*
M_SQRT1_2
*
0.5
;
auto
cdf
=
scale
<
T
>
(
scale
<
T
>
(
erf
<
T
>
(
kalpha
*
promoted_x
),
1.
,
1.
),
0.5
);
auto
pdf
=
kbeta
*
exp
<
T
>
(
scale
<
T
>
(
promoted_x
*
promoted_x
,
-
0.5
));
set_output
<
T
>
(
cast
<
T
>
(
promoted_out_grad
*
(
cdf
+
promoted_x
*
pdf
),
x
.
type
()),
x_grad
);
}
}
else
{
// Scale only support fp32 attr in static graph mode, use elementwise_xx
// when precision is over fp32.
if
(
approximate
)
{
auto
kBeta
=
M_SQRT2
*
M_2_SQRTPI
*
0.5
;
auto
kKappa
=
0.044715
;
auto
x_sq
=
x
*
x
;
auto
x_cube
=
x_sq
*
x
;
auto
inner
=
kBeta
*
(
x
+
kKappa
*
x_cube
);
auto
tanh_inner
=
tanh
<
T
>
(
inner
);
auto
left
=
scale
<
T
>
(
x
,
0.5
);
auto
right
=
scale
<
T
>
(
tanh_inner
,
1.
,
1.
);
auto
left_derivative
=
scale
<
T
>
(
right
,
0.5
);
auto
tanh_derivative
=
scale
<
T
>
(
tanh_inner
*
tanh_inner
,
-
1.
,
1.
);
auto
inner_derivative
=
kBeta
*
(
scale
<
T
>
(
3
*
kKappa
*
x_sq
,
1.
,
1.
));
auto
right_derivative
=
left
*
tanh_derivative
*
inner_derivative
;
set_output
<
T
>
(
out_grad
*
(
left_derivative
+
right_derivative
),
x_grad
);
}
else
{
auto
kAlpha
=
M_SQRT1_2
;
auto
kBeta
=
M_2_SQRTPI
*
M_SQRT1_2
*
0.5
;
auto
cdf
=
scale
<
T
>
(
scale
<
T
>
(
erf
<
T
>
(
kAlpha
*
x
),
1.
,
1.
),
0.5
);
auto
pdf
=
kBeta
*
exp
<
T
>
(
scale
<
T
>
(
x
*
x
,
-
0.5
));
set_output
<
T
>
(
out_grad
*
(
cdf
+
x
*
pdf
),
x_grad
);
}
}
}
}
// namespace prim
}
// namespace paddle
paddle/phi/api/yaml/backward.yaml
浏览文件 @
2add31f4
...
...
@@ -635,6 +635,7 @@
param
:
[
x
]
kernel
:
func
:
gelu_grad
composite
:
gelu_grad(x, out_grad, approximate, x_grad)
-
backward_op
:
grid_sample_grad
forward
:
grid_sample (Tensor x, Tensor grid, str mode, str padding_mode, bool align_corners) -> Tensor(out)
...
...
python/paddle/fluid/tests/unittests/prim/test_comp_custom_vjp.py
浏览文件 @
2add31f4
...
...
@@ -44,8 +44,7 @@ class TestCustomVJP(unittest.TestCase):
'fill_any_like'
,
'cast'
,
'elementwise_mul'
,
'fill_constant'
,
'elementwise_div'
,
'scale'
,
)
self
.
ops_all_enable
=
(
'uniform_random'
,
...
...
@@ -59,8 +58,7 @@ class TestCustomVJP(unittest.TestCase):
'fill_any_like'
,
'cast'
,
'elementwise_mul'
,
'fill_constant'
,
'elementwise_div'
,
'scale'
,
)
def
test_enable_prim_fwd
(
self
):
...
...
python/paddle/fluid/tests/unittests/test_activation_op.py
浏览文件 @
2add31f4
...
...
@@ -2031,6 +2031,10 @@ class TestGeluApproximate(TestActivation):
self
.
outputs
=
{
'Out'
:
out
}
self
.
attrs
=
{
"approximate"
:
approximate
}
# The backward decomposite of gelu is inconsistent with raw kernel,
# lower threshold to support 1e-5 for pass the unittest
self
.
rev_comp_rtol
=
1e-5
def
test_check_output
(
self
):
self
.
check_output
(
check_prim
=
True
)
...
...
@@ -2057,6 +2061,9 @@ class TestGelu(TestActivation):
self
.
inputs
=
{
'X'
:
x
}
self
.
outputs
=
{
'Out'
:
out
}
self
.
attrs
=
{
"approximate"
:
approximate
}
# The backward decomposite of gelu is inconsistent with raw kernel,
# lower threshold to support 1e-5 for pass the unittest
self
.
rev_comp_rtol
=
1e-5
def
if_enable_cinn
(
self
):
self
.
enable_cinn
=
False
...
...
@@ -2088,6 +2095,11 @@ class TestGELUAPI(unittest.TestCase):
if
paddle
.
is_compiled_with_cuda
()
else
paddle
.
CPUPlace
()
)
self
.
enable_cinn
=
False
# The backward decomposite of gelu is inconsistent with raw kernel,
# lower threshold to support 1e-5 for pass the unittest
self
.
rev_comp_rtol
=
1e-5
def
test_static_api
(
self
):
with
paddle_static_guard
():
...
...
@@ -3910,7 +3922,7 @@ create_test_act_fp16_class(TestAsinh, grad_atol=0.85)
create_test_act_fp16_class
(
TestAtanh
,
grad_atol
=
0.85
)
create_test_act_fp16_class
(
TestRound
,
grad_check
=
False
)
create_test_act_fp16_class
(
TestRelu
,
check_prim
=
True
)
create_test_act_fp16_class
(
TestGelu
,
check_prim
=
True
)
create_test_act_fp16_class
(
TestGelu
,
check_prim
=
True
,
enable_cinn
=
False
)
create_test_act_fp16_class
(
TestBRelu
)
create_test_act_fp16_class
(
TestRelu6
)
create_test_act_fp16_class
(
TestSoftRelu
,
grad_atol
=
0.85
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录