Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
ae74c404
P
Paddle
项目概览
Crayon鑫
/
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看板
未验证
提交
ae74c404
编写于
7月 06, 2021
作者:
W
Wangzheee
提交者:
GitHub
7月 06, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[pass_enhance] embedding_eltwise_layernorm_fuse_pass (#33973)
上级
f2068eec
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
71 addition
and
10 deletion
+71
-10
paddle/fluid/framework/ir/embedding_eltwise_layernorm_fuse_pass.cc
...uid/framework/ir/embedding_eltwise_layernorm_fuse_pass.cc
+68
-7
paddle/fluid/framework/ir/embedding_eltwise_layernorm_fuse_pass.h
...luid/framework/ir/embedding_eltwise_layernorm_fuse_pass.h
+3
-3
未找到文件。
paddle/fluid/framework/ir/embedding_eltwise_layernorm_fuse_pass.cc
浏览文件 @
ae74c404
...
...
@@ -136,8 +136,12 @@ void SkipLayerNorm::operator()() {
->
LinksFrom
({
eltwise_add_out
,
layer_norm_bias_var
,
layer_norm_scale_var
})
.
LinksTo
({
layer_norm_out
,
layer_norm_mean_var
,
layer_norm_variance_var
});
}
static
int
BuildFusion
(
Graph
*
graph
,
const
std
::
string
&
name_scope
/*const Scope* scope*/
)
{
}
// namespace patterns
int
EmbeddingEltwiseLayerNormFusePass
::
BuildFusion
(
Graph
*
graph
,
const
std
::
string
&
name_scope
/*const Scope* scope*/
)
const
{
GraphPatternDetector
gpd
;
auto
*
pattern
=
gpd
.
mutable_pattern
();
...
...
@@ -146,7 +150,8 @@ static int BuildFusion(Graph* graph, const std::string& name_scope
std
::
vector
<
std
::
unordered_set
<
Node
*>>
start_pattern_remove_nodes
;
// Create pattern.
Embedding2Eltwise1Pattern
start_pattern
(
pattern
,
name_scope
+
"/start"
);
patterns
::
Embedding2Eltwise1Pattern
start_pattern
(
pattern
,
name_scope
+
"/start"
);
start_pattern
();
auto
handler
=
[
&
](
const
GraphPatternDetector
::
subgraph_t
&
subgraph
,
Graph
*
g
)
{
...
...
@@ -162,6 +167,10 @@ static int BuildFusion(Graph* graph, const std::string& name_scope
start_pattern
);
GET_IR_NODE_FROM_SUBGRAPH
(
eltwise_add
,
eltwise_add
,
start_pattern
);
GET_IR_NODE_FROM_SUBGRAPH
(
eltwise_add_out
,
eltwise_add_out
,
start_pattern
);
if
(
!
IsCompat
(
subgraph
,
graph
))
{
LOG
(
WARNING
)
<<
"Pass(Embedding2Eltwise1Pattern) in op compat failed."
;
return
;
}
std
::
vector
<
std
::
pair
<
Node
*
,
Node
*>>
ins
;
ins
.
push_back
(
std
::
make_pair
(
lookup_table1_x
,
lookup_table1_w
));
ins
.
push_back
(
std
::
make_pair
(
lookup_table2_x
,
lookup_table2_w
));
...
...
@@ -182,7 +191,8 @@ static int BuildFusion(Graph* graph, const std::string& name_scope
GraphPatternDetector
gpd2
;
auto
*
pattern2
=
gpd2
.
mutable_pattern
();
Embedding1Eltwise1Pattern
second_pattern
(
pattern2
,
name_scope
+
"/second"
);
patterns
::
Embedding1Eltwise1Pattern
second_pattern
(
pattern2
,
name_scope
+
"/second"
);
second_pattern
();
auto
handler2
=
[
&
](
const
GraphPatternDetector
::
subgraph_t
&
subgraph
,
Graph
*
g
)
{
...
...
@@ -194,6 +204,10 @@ static int BuildFusion(Graph* graph, const std::string& name_scope
GET_IR_NODE_FROM_SUBGRAPH
(
eltwise_add_in
,
eltwise_add_in
,
second_pattern
);
GET_IR_NODE_FROM_SUBGRAPH
(
eltwise_add
,
eltwise_add
,
second_pattern
);
GET_IR_NODE_FROM_SUBGRAPH
(
eltwise_add_out
,
eltwise_add_out
,
second_pattern
);
if
(
!
IsCompat
(
subgraph
,
graph
))
{
LOG
(
WARNING
)
<<
"Pass(Embedding1Eltwise1Pattern) in op compat failed."
;
return
;
}
auto
in
=
std
::
make_pair
(
lookup_table1_x
,
lookup_table1_w
);
inner_pattern_ins
.
push_back
(
in
);
inner_pattern_tmp_in
.
push_back
(
eltwise_add_in
);
...
...
@@ -214,7 +228,8 @@ static int BuildFusion(Graph* graph, const std::string& name_scope
std
::
vector
<
std
::
unordered_set
<
Node
*>>
end_pattern_remove_nodes
;
GraphPatternDetector
gpd3
;
auto
*
pattern3
=
gpd3
.
mutable_pattern
();
SkipLayerNorm
skip_layernorm_pattern
(
pattern3
,
name_scope
+
"/third"
);
patterns
::
SkipLayerNorm
skip_layernorm_pattern
(
pattern3
,
name_scope
+
"/third"
);
skip_layernorm_pattern
();
auto
handler3
=
[
&
](
const
GraphPatternDetector
::
subgraph_t
&
subgraph
,
Graph
*
g
)
{
...
...
@@ -232,6 +247,10 @@ static int BuildFusion(Graph* graph, const std::string& name_scope
skip_layernorm_pattern
);
GET_IR_NODE_FROM_SUBGRAPH
(
layer_norm_variance
,
layer_norm_variance
,
skip_layernorm_pattern
);
if
(
!
IsCompat
(
subgraph
,
graph
))
{
LOG
(
WARNING
)
<<
"Pass(SkipLayerNorm) in op compat failed."
;
return
;
}
end_pattern_elt_out
.
push_back
(
eltwise_add_out
);
std
::
unordered_set
<
Node
*>
rm_nodes
;
rm_nodes
.
insert
({
layer_norm
,
layer_norm_mean
,
layer_norm_variance
});
...
...
@@ -349,11 +368,53 @@ static int BuildFusion(Graph* graph, const std::string& name_scope
return
fusion_count
;
}
}
// namespace patterns
EmbeddingEltwiseLayerNormFusePass
::
EmbeddingEltwiseLayerNormFusePass
()
{
AddOpCompat
(
OpCompat
(
"elementwise_add"
))
.
AddInput
(
"X"
)
.
IsTensor
()
.
End
()
.
AddInput
(
"Y"
)
.
IsTensor
()
.
End
()
.
AddOutput
(
"Out"
)
.
IsTensor
()
.
End
()
.
AddAttr
(
"axis"
)
.
IsIntIn
({
0
,
-
1
})
.
End
();
AddOpCompat
(
OpCompat
(
"layer_norm"
))
.
AddInput
(
"X"
)
.
IsTensor
()
.
End
()
.
AddInput
(
"Scale"
)
.
IsTensor
()
.
End
()
.
AddInput
(
"Bias"
)
.
IsTensor
()
.
End
()
.
AddOutput
(
"Y"
)
.
IsTensor
()
.
End
()
.
AddOutput
(
"Mean"
)
.
IsTensor
()
.
End
()
.
AddOutput
(
"Variance"
)
.
IsTensor
()
.
End
()
.
AddAttr
(
"epsilon"
)
.
IsNumGE
(
0.0
f
)
.
IsNumLE
(
0.001
f
)
.
End
()
.
AddAttr
(
"begin_norm_axis"
)
.
IsNumGT
(
0
)
.
End
();
}
void
EmbeddingEltwiseLayerNormFusePass
::
ApplyImpl
(
Graph
*
graph
)
const
{
FusePassBase
::
Init
(
name_scope_
,
graph
);
int
fusion_count
=
patterns
::
BuildFusion
(
graph
,
name_scope_
);
int
fusion_count
=
EmbeddingEltwiseLayerNormFusePass
::
BuildFusion
(
graph
,
name_scope_
);
if
(
fusion_count
>
0
)
{
graph
->
Set
(
kEmbEltwiseLayernormPass
,
new
bool
(
true
));
}
...
...
paddle/fluid/framework/ir/embedding_eltwise_layernorm_fuse_pass.h
浏览文件 @
ae74c404
...
...
@@ -19,8 +19,6 @@
#include <utility>
#include "paddle/fluid/framework/ir/fuse_pass_base.h"
#include "paddle/fluid/framework/ir/graph.h"
#include "paddle/fluid/framework/ir/graph_pattern_detector.h"
namespace
paddle
{
namespace
framework
{
...
...
@@ -150,11 +148,13 @@ struct SkipLayerNorm : public PatternBase {
class
EmbeddingEltwiseLayerNormFusePass
:
public
FusePassBase
{
public:
EmbeddingEltwiseLayerNormFusePass
();
virtual
~
EmbeddingEltwiseLayerNormFusePass
()
{}
protected:
void
ApplyImpl
(
Graph
*
graph
)
const
;
int
BuildFusion
(
Graph
*
graph
,
const
std
::
string
&
name_scope
/*const Scope* scope*/
)
const
;
const
std
::
string
name_scope_
{
"embedding_eltwise_layernorm_fuse"
};
};
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录