Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
98899b73
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2292
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看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
98899b73
编写于
7月 24, 2020
作者:
A
Adam
提交者:
GitHub
7月 24, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix FC + GRU fuse pass (#25687)
上级
0e23dc3a
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
38 addition
and
40 deletion
+38
-40
paddle/fluid/framework/ir/fc_gru_fuse_pass.cc
paddle/fluid/framework/ir/fc_gru_fuse_pass.cc
+34
-38
paddle/fluid/inference/tests/api/analyzer_vis_tester.cc
paddle/fluid/inference/tests/api/analyzer_vis_tester.cc
+0
-2
paddle/fluid/operators/fused/fusion_gru_op.cc
paddle/fluid/operators/fused/fusion_gru_op.cc
+4
-0
未找到文件。
paddle/fluid/framework/ir/fc_gru_fuse_pass.cc
浏览文件 @
98899b73
...
@@ -26,15 +26,15 @@ static int BuildFusion(Graph* graph, const std::string& name_scope,
...
@@ -26,15 +26,15 @@ static int BuildFusion(Graph* graph, const std::string& name_scope,
GraphPatternDetector
gpd
;
GraphPatternDetector
gpd
;
auto
*
pattern
=
gpd
.
mutable_pattern
();
auto
*
pattern
=
gpd
.
mutable_pattern
();
// Create pattern.
patterns
::
FC
fc_pattern
(
pattern
,
name_scope
);
patterns
::
GRU
gru_pattern
(
pattern
,
name_scope
);
PDNode
*
x
=
PDNode
*
x
=
pattern
->
NewNode
(
patterns
::
UniqueKey
(
"x"
))
->
assert_var_not_persistable
();
pattern
->
NewNode
(
patterns
::
UniqueKey
(
"x"
))
->
assert_var_not_persistable
();
// Create pattern.
patterns
::
FC
fc_pattern
(
pattern
,
name_scope
);
auto
*
fc_out
=
fc_pattern
(
x
,
with_fc_bias
,
/* with_relu */
false
);
auto
*
fc_out
=
fc_pattern
(
x
,
with_fc_bias
,
/* with_relu */
false
);
fc_out
->
AsIntermediate
();
// fc_out is a tmp var, will be removed after fuse.
fc_out
->
AsIntermediate
();
// fc_out is a tmp var, will be removed after fuse.
patterns
::
GRU
gru_pattern
(
pattern
,
name_scope
);
gru_pattern
(
fc_out
);
gru_pattern
(
fc_out
);
// Create New OpDesc
// Create New OpDesc
...
@@ -48,17 +48,18 @@ static int BuildFusion(Graph* graph, const std::string& name_scope,
...
@@ -48,17 +48,18 @@ static int BuildFusion(Graph* graph, const std::string& name_scope,
SET_IN
(
X
,
x
);
SET_IN
(
X
,
x
);
SET_IN
(
WeightX
,
weight_x
);
SET_IN
(
WeightX
,
weight_x
);
SET_IN
(
WeightH
,
weight_h
);
SET_IN
(
WeightH
,
weight_h
);
if
(
with_fc_bias
)
{
SET_IN
(
Bias
,
bias
);
op_desc
.
SetInput
(
"Bias"
,
{
NEW_NAME
(
bias
)
+
bias
->
Name
()});
}
else
{
SET_IN
(
Bias
,
bias
);
}
#undef SET_IN
#undef SET_IN
// TODO(grygielski): Add H0 to the pass
op_desc
.
SetInput
(
"H0"
,
{});
op_desc
.
SetInput
(
"H0"
,
{});
op_desc
.
SetOutput
(
"Hidden"
,
{
hidden
->
Name
()});
op_desc
.
SetOutput
(
"Hidden"
,
{
hidden
->
Name
()});
op_desc
.
SetAttr
(
"is_reverse"
,
gru
->
Op
()
->
GetAttr
(
"is_reverse"
));
op_desc
.
SetAttr
(
"is_reverse"
,
gru
->
Op
()
->
GetAttr
(
"is_reverse"
));
op_desc
.
SetAttr
(
"origin_mode"
,
gru
->
Op
()
->
GetAttrIfExists
<
bool
>
(
"origin_mode"
));
// TODO(TJ): This should be a option for infer
// TODO(TJ): This should be a option for infer
op_desc
.
SetAttr
(
"use_seq"
,
true
);
op_desc
.
SetAttr
(
"use_seq"
,
true
);
op_desc
.
SetAttr
(
"activation"
,
gru
->
Op
()
->
GetAttr
(
"activation"
));
op_desc
.
SetAttr
(
"gate_activation"
,
gru
->
Op
()
->
GetAttr
(
"gate_activation"
));
#define SET_IMTERMEDIATE_OUT(key) op_desc.SetOutput(#key, {NEW_NAME(key)})
#define SET_IMTERMEDIATE_OUT(key) op_desc.SetOutput(#key, {NEW_NAME(key)})
SET_IMTERMEDIATE_OUT
(
ReorderedH0
);
SET_IMTERMEDIATE_OUT
(
ReorderedH0
);
...
@@ -68,35 +69,30 @@ static int BuildFusion(Graph* graph, const std::string& name_scope,
...
@@ -68,35 +69,30 @@ static int BuildFusion(Graph* graph, const std::string& name_scope,
#undef SET_IMTERMEDIATE_OUT
#undef SET_IMTERMEDIATE_OUT
auto
*
op
=
graph
->
CreateOpNode
(
&
op_desc
);
auto
*
op
=
graph
->
CreateOpNode
(
&
op_desc
);
PADDLE_ENFORCE_EQ
(
graph
->
Has
(
kParamScopeAttr
),
true
,
platform
::
errors
::
InvalidArgument
(
"Graph have no attr kParamScopeAttr."
));
auto
&
scope
=
graph
->
Get
<
Scope
>
(
kParamScopeAttr
);
if
(
with_fc_bias
)
{
if
(
with_fc_bias
)
{
// Fusion GRU bias = fcbias + grubias
auto
*
gru_bias_var
=
scope
->
FindVar
(
bias
->
Name
());
auto
*
fusion_bias_var
=
scope
.
Var
(
NEW_NAME
(
bias
)
+
bias
->
Name
());
auto
*
fc_bias_var
=
scope
->
FindVar
(
fc_bias
->
Name
());
auto
*
out_bias_tensor
=
PADDLE_ENFORCE_NE
(
fusion_bias_var
->
GetMutable
<
framework
::
LoDTensor
>
();
gru_bias_var
,
nullptr
,
PADDLE_ENFORCE_NOT_NULL
(
platform
::
errors
::
NotFound
(
"GRU bias var has not been found."
));
fusion_bias_var
,
PADDLE_ENFORCE_NE
(
platform
::
errors
::
InvalidArgument
(
fc_bias_var
,
nullptr
,
"Fusion bias variable's pointer cannot be nullptr."
));
platform
::
errors
::
NotFound
(
"FC bias var has not been found."
));
auto
*
gru_bias_var
=
scope
.
FindVar
(
bias
->
Name
());
auto
*
fc_bias_var
=
scope
.
FindVar
(
fc_bias
->
Name
());
auto
*
gru_bias_tensor
=
gru_bias_var
->
GetMutable
<
LoDTensor
>
();
PADDLE_ENFORCE_NOT_NULL
(
gru_bias_var
,
auto
*
fc_bias_tensor
=
fc_bias_var
->
GetMutable
<
LoDTensor
>
();
platform
::
errors
::
InvalidArgument
(
PADDLE_ENFORCE_EQ
(
"Gru bias var ptr cannot be nullptr."
));
gru_bias_tensor
->
numel
(),
fc_bias_tensor
->
numel
(),
PADDLE_ENFORCE_NOT_NULL
(
fc_bias_var
,
platform
::
errors
::
PreconditionNotMet
(
platform
::
errors
::
InvalidArgument
(
"GRU and FC biases have to have equal number of elements."
));
"Fc bias var ptr cannot be nullptr."
));
const
auto
&
gru_bias_tenosr
=
gru_bias_var
->
Get
<
framework
::
LoDTensor
>
();
auto
gru_bias_data
=
const
auto
&
fc_bias_tensor
=
fc_bias_var
->
Get
<
framework
::
LoDTensor
>
();
gru_bias_tensor
->
mutable_data
<
float
>
(
platform
::
CPUPlace
());
// new bias = fc bias + gru bias
auto
*
fc_bias_data
=
fc_bias_tensor
->
data
<
float
>
();
out_bias_tensor
->
Resize
(
gru_bias_tenosr
.
dims
());
auto
*
data
=
out_bias_tensor
->
mutable_data
<
float
>
(
platform
::
CPUPlace
());
// Recompute GRU bias
for
(
int
i
=
0
;
i
<
out_bias_tensor
->
numel
();
i
++
)
{
for
(
int
i
=
0
;
i
<
gru_bias_tensor
->
numel
();
++
i
)
{
data
[
i
]
=
gru_bias_data
[
i
]
+=
fc_bias_data
[
i
];
fc_bias_tensor
.
data
<
float
>
()[
i
]
+
gru_bias_tenosr
.
data
<
float
>
()[
i
];
}
}
}
}
#undef GET_NODE
#undef GET_NODE
...
@@ -117,7 +113,7 @@ static int BuildFusion(Graph* graph, const std::string& name_scope,
...
@@ -117,7 +113,7 @@ static int BuildFusion(Graph* graph, const std::string& name_scope,
IR_NODE_LINK_TO
(
x
,
op
);
IR_NODE_LINK_TO
(
x
,
op
);
IR_NODE_LINK_TO
(
weight_x
,
op
);
IR_NODE_LINK_TO
(
weight_x
,
op
);
IR_NODE_LINK_TO
(
weight_h
,
op
);
IR_NODE_LINK_TO
(
weight_h
,
op
);
IR_NODE_LINK_TO
(
bias
,
op
);
// actually should link to new bias if have
IR_NODE_LINK_TO
(
bias
,
op
);
IR_NODE_LINK_TO
(
op
,
hidden
);
IR_NODE_LINK_TO
(
op
,
hidden
);
// h0?
// h0?
return
op
;
return
op
;
...
...
paddle/fluid/inference/tests/api/analyzer_vis_tester.cc
浏览文件 @
98899b73
...
@@ -56,8 +56,6 @@ void SetConfig(AnalysisConfig *cfg) {
...
@@ -56,8 +56,6 @@ void SetConfig(AnalysisConfig *cfg) {
cfg
->
DisableGpu
();
cfg
->
DisableGpu
();
cfg
->
SwitchIrDebug
();
cfg
->
SwitchIrDebug
();
cfg
->
SwitchSpecifyInputNames
(
false
);
cfg
->
SwitchSpecifyInputNames
(
false
);
// TODO(TJ): fix fusion gru
cfg
->
pass_builder
()
->
DeletePass
(
"fc_gru_fuse_pass"
);
}
}
void
SetInput
(
std
::
vector
<
std
::
vector
<
PaddleTensor
>>
*
inputs
)
{
void
SetInput
(
std
::
vector
<
std
::
vector
<
PaddleTensor
>>
*
inputs
)
{
...
...
paddle/fluid/operators/fused/fusion_gru_op.cc
浏览文件 @
98899b73
...
@@ -183,6 +183,10 @@ void FusionGRUOpMaker::Make() {
...
@@ -183,6 +183,10 @@ void FusionGRUOpMaker::Make() {
"(bool, default: True) "
"(bool, default: True) "
"whether to use seq mode to compute GRU."
)
"whether to use seq mode to compute GRU."
)
.
SetDefault
(
true
);
.
SetDefault
(
true
);
AddAttr
<
bool
>
(
"origin_mode"
,
"bool"
"use origin mode in article https://arxiv.org/abs/1412.3555"
)
.
SetDefault
(
false
);
AddComment
(
R"DOC(
AddComment
(
R"DOC(
The Fusion complete GRU Operator.
The Fusion complete GRU Operator.
This operator fuse the fully-connected operator into GRU,
This operator fuse the fully-connected operator into GRU,
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录