Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
441d3a47
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看板
提交
441d3a47
编写于
9月 13, 2018
作者:
T
Tomasz Patejko
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
MKLDNN conv + elementwise_add: added some refactoring in the pass
上级
38b7b34b
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
48 addition
and
45 deletion
+48
-45
paddle/fluid/framework/ir/conv_elementwise_add_mkldnn_fuse_pass.cc
...uid/framework/ir/conv_elementwise_add_mkldnn_fuse_pass.cc
+48
-45
未找到文件。
paddle/fluid/framework/ir/conv_elementwise_add_mkldnn_fuse_pass.cc
浏览文件 @
441d3a47
...
@@ -18,41 +18,41 @@ struct Pattern : public PatternBase {
...
@@ -18,41 +18,41 @@ struct Pattern : public PatternBase {
PDPattern
*
node_pattern
()
{
return
pattern
;
}
PDPattern
*
node_pattern
()
{
return
pattern
;
}
public:
public:
std
::
string
node_name
(
std
::
string
op_name
)
std
::
string
node_name
(
std
::
string
op_name
)
{
{
return
PDNodeName
(
name_scope
(),
repr
(),
id
(),
op_name
);
return
PDNodeName
(
name_scope
(),
repr
(),
id
(),
op_name
);
}
}
PDNode
*
retrieve_node
(
std
::
string
op_name
)
PDNode
*
retrieve_node
(
std
::
string
op_name
)
{
{
return
node_pattern
()
->
RetrieveNode
(
node_name
(
op_name
));
return
node_pattern
()
->
RetrieveNode
(
node_name
(
op_name
));
}
}
PDNode
*
new_node
(
std
::
string
op_name
)
PDNode
*
new_node
(
std
::
string
op_name
)
{
{
return
node_pattern
()
->
NewNode
(
node_name
(
op_name
));
return
node_pattern
()
->
NewNode
(
node_name
(
op_name
));
}
}
};
};
struct
Conv
{
struct
Conv
{
std
::
string
conv
_name
()
{
return
"conv2d"
;
}
std
::
string
op
_name
()
{
return
"conv2d"
;
}
std
::
string
input_name
()
{
return
"Input"
;
}
std
::
string
input_name
()
{
return
"Input"
;
}
std
::
string
filter_name
()
{
return
"Filter"
;
}
std
::
string
filter_name
()
{
return
"Filter"
;
}
std
::
string
output_name
()
{
return
"Output"
;
}
std
::
string
output_name
()
{
return
"Output"
;
}
std
::
function
<
PDNode
*
()
>
operator
()(
std
::
shared_ptr
<
Pattern
>
pattern
)
{
std
::
function
<
PDNode
*
()
>
operator
()(
std
::
shared_ptr
<
Pattern
>
pattern
)
{
return
[
&
]()
->
PDNode
*
{
return
[
&
]()
->
PDNode
*
{
auto
conv_op
=
pattern
->
new_node
(
conv
_name
())
auto
conv_op
=
pattern
->
new_node
(
op
_name
())
->
assert_is_op
(
"conv2d"
);
->
assert_is_op
(
"conv2d"
);
auto
input_var
=
pattern
->
new_node
(
input_name
())
auto
input_var
=
pattern
->
new_node
(
input_name
())
->
assert_is_op_input
(
conv_name
(),
input_name
());
->
assert_is_op_input
(
op_name
(),
input_name
());
auto
filter_var
=
pattern
->
new_node
(
filter_name
())
auto
filter_var
=
pattern
->
new_node
(
filter_name
())
->
assert_is_op_input
(
conv_name
(),
filter_name
());
->
assert_is_op_input
(
op_name
(),
filter_name
());
auto
output_var
=
pattern
->
new_node
(
output_name
())
auto
output_var
=
pattern
->
new_node
(
output_name
())
->
assert_is_op_output
(
conv_name
(),
output_name
());
->
assert_is_op_output
(
op_name
(),
output_name
());
conv_op
->
LinksFrom
({
input_var
,
filter_var
});
conv_op
->
LinksFrom
({
input_var
,
filter_var
});
conv_op
->
LinksTo
({
output_var
});
conv_op
->
LinksTo
({
output_var
});
...
@@ -63,24 +63,27 @@ struct Conv {
...
@@ -63,24 +63,27 @@ struct Conv {
};
};
struct
ElementwiseAdd
{
struct
ElementwiseAdd
{
std
::
string
elementwise_add
_name
()
{
return
"elementwise_add"
;
}
std
::
string
op
_name
()
{
return
"elementwise_add"
;
}
std
::
string
x_name
()
{
return
"X"
;
}
std
::
string
x_name
()
{
return
"X"
;
}
std
::
string
y_name
()
{
return
"Y"
;
}
std
::
string
y_name
()
{
return
"Y"
;
}
std
::
string
out_name
()
{
return
"Out"
;
}
std
::
string
out_name
()
{
return
"Out"
;
}
std
::
function
<
PDNode
*
(
PDNode
*
)
>
operator
()(
std
::
shared_ptr
<
Pattern
>
pattern
)
{
std
::
function
<
PDNode
*
(
PDNode
*
)
>
operator
()(
std
::
shared_ptr
<
Pattern
>
pattern
)
{
return
[
&
](
PDNode
*
conv_output
)
->
PDNode
*
{
return
[
&
](
PDNode
*
conv_output
)
->
PDNode
*
{
auto
elementwise_add_op
=
pattern
->
new_node
(
elementwise_add
_name
())
auto
elementwise_add_op
=
pattern
->
new_node
(
op
_name
())
->
assert_is_op
(
"elementwise_add"
);
->
assert_is_op
(
"elementwise_add"
);
auto
y_var
=
pattern
->
new_node
(
y_name
())
auto
y_var
=
pattern
->
new_node
(
y_name
())
->
assert_is_op_input
(
elementwise_add_name
(),
y_name
());
->
assert_is_op_input
(
op_name
(),
y_name
());
conv_output
->
assert_is_op_input
(
elementwise_add_name
(),
x_name
());
conv_output
->
assert_is_op_input
(
op_name
(),
x_name
());
auto
out_var
=
pattern
->
new_node
(
out_name
())
auto
out_var
=
pattern
->
new_node
(
out_name
())
->
AsOutput
()
->
AsOutput
()
->
assert_is_op_output
(
elementwise_add_name
(),
out_name
());
->
assert_is_op_output
(
op_name
(),
out_name
());
elementwise_add_op
->
LinksFrom
({
y_var
,
conv_output
});
elementwise_add_op
->
LinksFrom
({
y_var
,
conv_output
});
elementwise_add_op
->
LinksTo
({
out_var
});
elementwise_add_op
->
LinksTo
({
out_var
});
...
@@ -89,11 +92,10 @@ struct ElementwiseAdd {
...
@@ -89,11 +92,10 @@ struct ElementwiseAdd {
};
};
}
}
};
};
}
// namespace patterns
Node
*
GetNodeFromSubgraph
(
const
GraphPatternDetector
::
subgraph_t
&
subgraph
,
Node
*
GetNodeFromSubgraph
(
const
GraphPatternDetector
::
subgraph_t
&
subgraph
,
std
::
shared_ptr
<
patterns
::
Pattern
>
pattern
,
const
std
::
string
&
op_name
)
std
::
shared_ptr
<
patterns
::
Pattern
>
pattern
,
{
const
std
::
string
&
op_name
)
{
PADDLE_ENFORCE
(
subgraph
.
count
(
pattern
->
retrieve_node
(
op_name
)),
PADDLE_ENFORCE
(
subgraph
.
count
(
pattern
->
retrieve_node
(
op_name
)),
"Node not found for PDNode %s"
,
pattern
->
node_name
(
op_name
));
"Node not found for PDNode %s"
,
pattern
->
node_name
(
op_name
));
Node
*
var
=
subgraph
.
at
(
pattern
->
retrieve_node
(
op_name
));
Node
*
var
=
subgraph
.
at
(
pattern
->
retrieve_node
(
op_name
));
...
@@ -102,7 +104,10 @@ Node* GetNodeFromSubgraph(const GraphPatternDetector::subgraph_t& subgraph,
...
@@ -102,7 +104,10 @@ Node* GetNodeFromSubgraph(const GraphPatternDetector::subgraph_t& subgraph,
return
var
;
return
var
;
}
}
using
graph_ptr
=
std
::
unique_ptr
<
ir
::
Graph
>
;
void
LinkNodes
(
Node
*
from
,
Node
*
to
)
{
from
->
outputs
.
push_back
(
to
);
to
->
inputs
.
push_back
(
from
);
}
void
CorrectGraphEdges
(
Graph
*
graph
,
Node
*
from
,
Node
*
to
)
{
void
CorrectGraphEdges
(
Graph
*
graph
,
Node
*
from
,
Node
*
to
)
{
for
(
auto
&
node
:
GraphTraits
::
DFS
(
*
graph
))
{
for
(
auto
&
node
:
GraphTraits
::
DFS
(
*
graph
))
{
...
@@ -112,11 +117,12 @@ void CorrectGraphEdges(Graph* graph, Node* from, Node* to) {
...
@@ -112,11 +117,12 @@ void CorrectGraphEdges(Graph* graph, Node* from, Node* to) {
[
from
](
Node
*
n
)
{
return
n
==
from
;
});
[
from
](
Node
*
n
)
{
return
n
==
from
;
});
if
(
same
!=
std
::
end
(
node
.
inputs
))
{
if
(
same
!=
std
::
end
(
node
.
inputs
))
{
node
.
inputs
.
push_back
(
to
);
LinkNodes
(
to
,
&
node
);
to
->
outputs
.
push_back
(
&
node
);
}
}
}
}
}
}
}
// namespace patterns
using
graph_ptr
=
std
::
unique_ptr
<
ir
::
Graph
>
;
graph_ptr
ConvElementwiseAddMKLDNNFusePass
::
ApplyImpl
(
graph_ptr
graph
)
const
{
graph_ptr
ConvElementwiseAddMKLDNNFusePass
::
ApplyImpl
(
graph_ptr
graph
)
const
{
FusePassBase
::
Init
(
"conv_elementwise_add_mkldnn_fuse_pass"
,
graph
.
get
());
FusePassBase
::
Init
(
"conv_elementwise_add_mkldnn_fuse_pass"
,
graph
.
get
());
...
@@ -133,11 +139,6 @@ graph_ptr ConvElementwiseAddMKLDNNFusePass::ApplyImpl(graph_ptr graph) const {
...
@@ -133,11 +139,6 @@ graph_ptr ConvElementwiseAddMKLDNNFusePass::ApplyImpl(graph_ptr graph) const {
conv_output
->
AsIntermediate
();
conv_output
->
AsIntermediate
();
auto
link_nodes_to
=
[](
Node
*
a
,
Node
*
b
)
{
a
->
outputs
.
push_back
(
b
);
b
->
inputs
.
push_back
(
a
);
};
auto
fuse_conv
=
[
&
](
Graph
*
g
,
Node
*
conv_input
,
Node
*
conv_filter
,
Node
*
y
)
{
auto
fuse_conv
=
[
&
](
Graph
*
g
,
Node
*
conv_input
,
Node
*
conv_filter
,
Node
*
y
)
{
OpDesc
op_desc
;
OpDesc
op_desc
;
op_desc
.
SetType
(
"conv2d"
);
op_desc
.
SetType
(
"conv2d"
);
...
@@ -150,29 +151,31 @@ graph_ptr ConvElementwiseAddMKLDNNFusePass::ApplyImpl(graph_ptr graph) const {
...
@@ -150,29 +151,31 @@ graph_ptr ConvElementwiseAddMKLDNNFusePass::ApplyImpl(graph_ptr graph) const {
auto
fused_conv_op
=
g
->
CreateOpNode
(
&
op_desc
);
auto
fused_conv_op
=
g
->
CreateOpNode
(
&
op_desc
);
link_nodes_to
(
conv_input
,
fused_conv_op
);
patterns
::
LinkNodes
(
conv_input
,
fused_conv_op
);
link_nodes_to
(
conv_filter
,
fused_conv_op
);
patterns
::
LinkNodes
(
conv_filter
,
fused_conv_op
);
link_nodes_to
(
fused_conv_op
,
y
);
patterns
::
LinkNodes
(
fused_conv_op
,
y
);
};
auto
remove_unused_nodes
=
[](
Graph
*
g
,
const
std
::
unordered_set
<
const
Node
*>&
removed_nodes
)
{
GraphSafeRemoveNodes
(
g
,
removed_nodes
);
};
};
auto
handler
=
[
&
](
const
GraphPatternDetector
::
subgraph_t
&
subgraph
,
Graph
*
g
)
{
auto
handler
=
[
&
](
const
GraphPatternDetector
::
subgraph_t
&
subgraph
,
Graph
*
g
)
{
auto
conv_op
=
GetNodeFromSubgraph
(
subgraph
,
pattern_ptr
,
conv_pattern
.
conv_name
());
auto
conv_op
=
patterns
::
GetNodeFromSubgraph
(
subgraph
,
pattern_ptr
,
auto
conv_input
=
GetNodeFromSubgraph
(
subgraph
,
pattern_ptr
,
conv_pattern
.
input_name
());
conv_pattern
.
op_name
());
auto
conv_filter
=
GetNodeFromSubgraph
(
subgraph
,
pattern_ptr
,
conv_pattern
.
filter_name
());
auto
conv_input
=
patterns
::
GetNodeFromSubgraph
(
subgraph
,
pattern_ptr
,
auto
conv_output
=
GetNodeFromSubgraph
(
subgraph
,
pattern_ptr
,
conv_pattern
.
output_name
());
conv_pattern
.
input_name
());
auto
conv_filter
=
patterns
::
GetNodeFromSubgraph
(
subgraph
,
pattern_ptr
,
auto
elementwise_add_op
=
GetNodeFromSubgraph
(
subgraph
,
pattern_ptr
,
elementwise_add_pattern
.
elementwise_add_name
());
conv_pattern
.
filter_name
());
auto
elementwise_add_y
=
GetNodeFromSubgraph
(
subgraph
,
pattern_ptr
,
elementwise_add_pattern
.
y_name
());
auto
conv_output
=
patterns
::
GetNodeFromSubgraph
(
subgraph
,
pattern_ptr
,
auto
elementwise_add_out
=
GetNodeFromSubgraph
(
subgraph
,
pattern_ptr
,
elementwise_add_pattern
.
out_name
());
conv_pattern
.
output_name
());
auto
elementwise_add_op
=
patterns
::
GetNodeFromSubgraph
(
subgraph
,
pattern_ptr
,
elementwise_add_pattern
.
op_name
());
auto
elementwise_add_y
=
patterns
::
GetNodeFromSubgraph
(
subgraph
,
pattern_ptr
,
elementwise_add_pattern
.
y_name
());
auto
elementwise_add_out
=
patterns
::
GetNodeFromSubgraph
(
subgraph
,
pattern_ptr
,
elementwise_add_pattern
.
out_name
());
fuse_conv
(
g
,
conv_input
,
conv_filter
,
elementwise_add_y
);
fuse_conv
(
g
,
conv_input
,
conv_filter
,
elementwise_add_y
);
CorrectGraphEdges
(
g
,
elementwise_add_out
,
elementwise_add_y
);
patterns
::
CorrectGraphEdges
(
g
,
elementwise_add_out
,
elementwise_add_y
);
patterns
::
GraphSafeRemoveNodes
(
g
,
{
conv_output
,
elementwise_add_out
,
conv_op
,
elementwise_add_op
});
remove_unused_nodes
(
g
,
{
conv_output
,
elementwise_add_out
,
conv_op
,
elementwise_add_op
});
};
};
gpd
(
graph
.
get
(),
handler
);
gpd
(
graph
.
get
(),
handler
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录