Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
8e02f290
P
Paddle
项目概览
BaiXuePrincess
/
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看板
未验证
提交
8e02f290
编写于
1月 29, 2023
作者:
Y
Yuang Liu
提交者:
GitHub
1月 29, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fused attention pass backward pattern (#49855)
上级
65bce2b3
变更
3
展开全部
隐藏空白更改
内联
并排
Showing
3 changed file
with
659 addition
and
10 deletion
+659
-10
paddle/fluid/framework/ir/fused_attention_pass.cc
paddle/fluid/framework/ir/fused_attention_pass.cc
+536
-3
paddle/fluid/framework/ir/fused_attention_pass.h
paddle/fluid/framework/ir/fused_attention_pass.h
+110
-1
python/paddle/fluid/tests/unittests/test_fused_attention_pass.py
...paddle/fluid/tests/unittests/test_fused_attention_pass.py
+13
-6
未找到文件。
paddle/fluid/framework/ir/fused_attention_pass.cc
浏览文件 @
8e02f290
此差异已折叠。
点击以展开。
paddle/fluid/framework/ir/fused_attention_pass.h
浏览文件 @
8e02f290
...
...
@@ -140,7 +140,116 @@ struct FusedAttentionGradPattern : public PatternBase {
bool
do_dropout
,
// dropout the softmax(qk) or not
bool
add_residual
);
// add residual to out linear or not
// TODO(Yuang Liu): add backward pattern
// post layer norm grad
PATTERN_DECL_NODE
(
post_layer_norm_grad_op
);
PATTERN_DECL_NODE
(
post_layer_norm_grad_scale
);
PATTERN_DECL_NODE
(
post_layer_norm_grad_bias
);
PATTERN_DECL_NODE
(
post_layer_norm_grad_mean
);
PATTERN_DECL_NODE
(
post_layer_norm_grad_variance
);
PATTERN_DECL_NODE
(
post_layer_norm_grad_x
);
PATTERN_DECL_NODE
(
post_layer_norm_grad_scale_grad
);
PATTERN_DECL_NODE
(
post_layer_norm_grad_bias_grad
);
PATTERN_DECL_NODE
(
post_layer_norm_grad_x_grad
);
// residual grad
PATTERN_DECL_NODE
(
residual_ele_add_grad_op
);
PATTERN_DECL_NODE
(
residual_ele_add_grad_x
);
PATTERN_DECL_NODE
(
residual_ele_add_grad_bias
);
PATTERN_DECL_NODE
(
residual_ele_add_grad_bias_grad
);
PATTERN_DECL_NODE
(
residual_ele_add_grad_x_grad
);
// out linear grad
PATTERN_DECL_NODE
(
out_linear_dropout_grad_op
);
PATTERN_DECL_NODE
(
out_linear_dropout_grad_mask
);
PATTERN_DECL_NODE
(
out_linear_dropout_grad_out
);
PATTERN_DECL_NODE
(
out_linear_ele_add_grad_op
);
PATTERN_DECL_NODE
(
out_linear_ele_add_grad_x
);
PATTERN_DECL_NODE
(
out_linear_ele_add_grad_bias
);
PATTERN_DECL_NODE
(
out_linear_ele_add_grad_x_grad
);
PATTERN_DECL_NODE
(
out_linear_ele_add_grad_bias_grad
);
PATTERN_DECL_NODE
(
out_linear_matmul_grad_op
);
PATTERN_DECL_NODE
(
out_linear_matmul_grad_x
);
PATTERN_DECL_NODE
(
out_linear_matmul_grad_w
);
PATTERN_DECL_NODE
(
out_linear_matmul_grad_x_grad
);
PATTERN_DECL_NODE
(
out_linear_matmul_grad_w_grad
);
// core attention grad
PATTERN_DECL_NODE
(
qkv_reshape_grad_op
);
PATTERN_DECL_NODE
(
qkv_reshape_grad_x_shape
);
PATTERN_DECL_NODE
(
qkv_reshape_grad_out
);
PATTERN_DECL_NODE
(
qkv_transpose_grad_op
);
PATTERN_DECL_NODE
(
qkv_transpose_grad_x_shape
);
PATTERN_DECL_NODE
(
qkv_transpose_grad_out
);
PATTERN_DECL_NODE
(
qkv_matmul_grad_op
);
PATTERN_DECL_NODE
(
qkv_matmul_grad_x
);
PATTERN_DECL_NODE
(
qkv_matmul_grad_w
);
PATTERN_DECL_NODE
(
qkv_matmul_grad_x_grad
);
PATTERN_DECL_NODE
(
qkv_matmul_grad_w_grad
);
PATTERN_DECL_NODE
(
attn_dropout_grad_op
);
PATTERN_DECL_NODE
(
attn_dropout_grad_mask
);
PATTERN_DECL_NODE
(
attn_dropout_grad_out
);
PATTERN_DECL_NODE
(
qk_softmax_grad_op
);
PATTERN_DECL_NODE
(
qk_softmax_grad_fwd_out
);
PATTERN_DECL_NODE
(
qk_softmax_grad_out
);
PATTERN_DECL_NODE
(
add_mask_ele_add_grad_op
);
PATTERN_DECL_NODE
(
add_mask_ele_add_grad_x
);
PATTERN_DECL_NODE
(
add_mask_ele_add_grad_bias
);
PATTERN_DECL_NODE
(
add_mask_ele_add_grad_x_grad
);
PATTERN_DECL_NODE
(
qk_scale_grad_op
);
PATTERN_DECL_NODE
(
qk_scale_grad_out
);
PATTERN_DECL_NODE
(
qk_matmul_grad_op
);
PATTERN_DECL_NODE
(
qk_matmul_grad_x
);
PATTERN_DECL_NODE
(
qk_matmul_grad_w
);
PATTERN_DECL_NODE
(
qk_matmul_grad_x_grad
);
PATTERN_DECL_NODE
(
qk_matmul_grad_w_grad
);
// fuse qkv projection grad
PATTERN_DECL_NODE
(
fuse_qkv_split_grad_op
);
// concat op
PATTERN_DECL_NODE
(
fuse_qkv_split_grad_out
);
PATTERN_DECL_NODE
(
fuse_qkv_transpose_grad_op
);
PATTERN_DECL_NODE
(
fuse_qkv_transpose_grad_x_shape
);
PATTERN_DECL_NODE
(
fuse_qkv_transpose_grad_out
);
PATTERN_DECL_NODE
(
fuse_qkv_reshape_grad_op
);
PATTERN_DECL_NODE
(
fuse_qkv_reshape_grad_x_shape
);
PATTERN_DECL_NODE
(
fuse_qkv_reshape_grad_out
);
PATTERN_DECL_NODE
(
fuse_qkv_ele_add_grad_op
);
PATTERN_DECL_NODE
(
fuse_qkv_ele_add_grad_x
);
PATTERN_DECL_NODE
(
fuse_qkv_ele_add_grad_bias
);
PATTERN_DECL_NODE
(
fuse_qkv_ele_add_grad_x_grad
);
PATTERN_DECL_NODE
(
fuse_qkv_ele_add_grad_bias_grad
);
PATTERN_DECL_NODE
(
fuse_qkv_matmul_grad_op
);
PATTERN_DECL_NODE
(
fuse_qkv_matmul_grad_x
);
PATTERN_DECL_NODE
(
fuse_qkv_matmul_grad_w
);
PATTERN_DECL_NODE
(
fuse_qkv_matmul_grad_x_grad
);
PATTERN_DECL_NODE
(
fuse_qkv_matmul_grad_w_grad
);
// pre layer norm grad
PATTERN_DECL_NODE
(
pre_layer_norm_grad_op
);
PATTERN_DECL_NODE
(
pre_layer_norm_grad_scale
);
PATTERN_DECL_NODE
(
pre_layer_norm_grad_bias
);
PATTERN_DECL_NODE
(
pre_layer_norm_grad_mean
);
PATTERN_DECL_NODE
(
pre_layer_norm_grad_variance
);
PATTERN_DECL_NODE
(
pre_layer_norm_grad_x
);
PATTERN_DECL_NODE
(
pre_layer_norm_grad_scale_grad
);
PATTERN_DECL_NODE
(
pre_layer_norm_grad_bias_grad
);
PATTERN_DECL_NODE
(
pre_layer_norm_grad_x_grad
);
// grad accumulation
PATTERN_DECL_NODE
(
grad_accumulation_sum_op
);
PATTERN_DECL_NODE
(
grad_accumulation_out
);
};
}
// namespace patterns
...
...
python/paddle/fluid/tests/unittests/test_fused_attention_pass.py
浏览文件 @
8e02f290
...
...
@@ -114,9 +114,7 @@ class TestFusedAttentionPass(unittest.TestCase):
hidden_size
=
768
num_heads
=
12
x_data
=
np
.
random
.
rand
(
batch_size
,
seq_len
,
hidden_size
).
astype
(
'float32'
)
x_data
=
np
.
random
.
rand
(
batch_size
,
seq_len
,
seq_len
).
astype
(
'float32'
)
mask_data
=
np
.
random
.
rand
(
batch_size
,
num_heads
,
seq_len
,
seq_len
).
astype
(
'float32'
)
...
...
@@ -127,7 +125,7 @@ class TestFusedAttentionPass(unittest.TestCase):
with
paddle
.
static
.
program_guard
(
main_prog
,
startup_prog
):
data
=
paddle
.
static
.
data
(
name
=
"x"
,
shape
=
[
-
1
,
seq_len
,
hidden_size
],
shape
=
[
-
1
,
seq_len
,
seq_len
],
dtype
=
'float32'
,
)
if
self
.
add_mask
:
...
...
@@ -138,6 +136,7 @@ class TestFusedAttentionPass(unittest.TestCase):
)
else
:
attn_mask
=
None
data_linear
=
paddle
.
nn
.
Linear
(
seq_len
,
hidden_size
)
multi_head_attn
=
MultiHeadAttention
(
hidden_size
,
num_heads
,
...
...
@@ -146,7 +145,9 @@ class TestFusedAttentionPass(unittest.TestCase):
post_ln
=
self
.
post_ln
,
attn_dropout
=
self
.
attn_dropout
,
)
out
=
multi_head_attn
(
data
,
attn_mask
)
attn_input
=
data_linear
(
data
)
out
=
multi_head_attn
(
attn_input
,
attn_mask
)
loss
=
paddle
.
mean
(
out
)
sgd_optimizer
=
paddle
.
fluid
.
optimizer
.
SGD
(
learning_rate
=
0.001
)
...
...
@@ -156,7 +157,13 @@ class TestFusedAttentionPass(unittest.TestCase):
pass_manager
.
apply
([
main_prog
],
[
startup_prog
])
ops
=
main_prog
.
global_block
().
ops
assert
ops
[
0
].
type
==
'reduce_mean'
assert
ops
[
2
].
type
==
'reduce_mean'
assert
ops
[
4
].
type
==
'reduce_mean_grad'
# two ops for linear, one op for reduce mean
# one fill constant
# one op for reduce mean grad, two ops for linear bwd
# the eighth op should be the optimizer
assert
ops
[
7
].
type
==
'sgd'
if
__name__
==
"__main__"
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录