提交 2a251bbf 编写于 作者: T Tomasz Patejko

MKLDNN conv + elementwise_add fusion: some refactoring: consts, function calls...

MKLDNN conv + elementwise_add fusion: some refactoring: consts, function calls instead of constant values
上级 b8e54ab5
...@@ -47,14 +47,15 @@ struct Pattern : public PatternBase { ...@@ -47,14 +47,15 @@ struct Pattern : public PatternBase {
}; };
struct Conv { struct Conv {
std::string op_name() { return "conv2d"; } std::string op_name() const { return "conv2d"; }
std::string input_name() { return "Input"; } std::string input_name() const { return "Input"; }
std::string filter_name() { return "Filter"; } std::string filter_name() const { return "Filter"; }
std::string output_name() { return "Output"; } std::string residual_data_name() const { return "ResidualData"; }
std::string output_name() const { 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(op_name())->assert_is_op("conv2d"); auto conv_op = pattern->new_node(op_name())->assert_is_op(op_name());
auto input_var = pattern->new_node(input_name()) auto input_var = pattern->new_node(input_name())
->assert_is_op_input(op_name(), input_name()); ->assert_is_op_input(op_name(), input_name());
...@@ -74,15 +75,15 @@ struct Conv { ...@@ -74,15 +75,15 @@ struct Conv {
}; };
struct ElementwiseAdd { struct ElementwiseAdd {
std::string op_name() { return "elementwise_add"; } std::string op_name() const { return "elementwise_add"; }
std::string x_name() { return "X"; } std::string x_name() const { return "X"; }
std::string y_name() { return "Y"; } std::string y_name() const { return "Y"; }
std::string out_name() { return "Out"; } std::string out_name() const { 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 = auto elementwise_add_op =
pattern->new_node(op_name())->assert_is_op("elementwise_add"); pattern->new_node(op_name())->assert_is_op(op_name());
auto x_var = auto x_var =
pattern->new_node(x_name())->assert_is_op_input(op_name(), x_name()); pattern->new_node(x_name())->assert_is_op_input(op_name(), x_name());
...@@ -177,15 +178,17 @@ graph_ptr ConvElementwiseAddMKLDNNFusePass::ApplyImpl(graph_ptr graph) const { ...@@ -177,15 +178,17 @@ graph_ptr ConvElementwiseAddMKLDNNFusePass::ApplyImpl(graph_ptr graph) const {
conv_output->AsIntermediate(); conv_output->AsIntermediate();
auto fuse_conv = [](Graph* g, Node* conv_input, Node* conv_filter, auto fuse_conv = [&conv_pattern](Graph* g, Node* conv_input,
Node* conv_output, Node* elementwise_add_x) { Node* conv_filter,
Node* conv_output,
Node* elementwise_add_x) {
OpDesc op_desc; OpDesc op_desc;
op_desc.SetType("conv2d"); op_desc.SetType(conv_pattern.op_name());
op_desc.SetInput("Input", {conv_input->Name()}); op_desc.SetInput(conv_pattern.input_name(), {conv_input->Name()});
op_desc.SetInput("Filter", {conv_filter->Name()}); op_desc.SetInput(conv_pattern.filter_name(), {conv_filter->Name()});
op_desc.SetInput("ResidualData", {elementwise_add_x->Name()}); op_desc.SetInput(conv_pattern.residual_data_name(), {elementwise_add_x->Name()});
op_desc.SetOutput("Output", {conv_output->Name()}); op_desc.SetOutput(conv_pattern.output_name(), {conv_output->Name()});
op_desc.SetAttr("use_mkldnn", true); op_desc.SetAttr("use_mkldnn", true);
op_desc.SetAttr("fuse_eltwise", true); op_desc.SetAttr("fuse_eltwise", true);
...@@ -198,8 +201,8 @@ graph_ptr ConvElementwiseAddMKLDNNFusePass::ApplyImpl(graph_ptr graph) const { ...@@ -198,8 +201,8 @@ graph_ptr ConvElementwiseAddMKLDNNFusePass::ApplyImpl(graph_ptr graph) const {
patterns::LinkNodes(fused_conv_op, conv_output); patterns::LinkNodes(fused_conv_op, conv_output);
}; };
auto handler = [&](const GraphPatternDetector::subgraph_t& subgraph, auto handler = [&conv_pattern, &elementwise_add_pattern, pattern_ptr, fuse_conv]
Graph* g) { (const GraphPatternDetector::subgraph_t& subgraph, Graph* g) {
auto conv_op = patterns::GetNodeFromSubgraph(subgraph, pattern_ptr, auto conv_op = patterns::GetNodeFromSubgraph(subgraph, pattern_ptr,
conv_pattern.op_name()); conv_pattern.op_name());
auto conv_input = patterns::GetNodeFromSubgraph(subgraph, pattern_ptr, auto conv_input = patterns::GetNodeFromSubgraph(subgraph, pattern_ptr,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册