From 41f3d78fdfd27b54195ef07c0b696c87168e675e Mon Sep 17 00:00:00 2001 From: Tomasz Patejko Date: Mon, 17 Sep 2018 10:08:05 +0200 Subject: [PATCH] MKLDNN conv + elementwise_add fusion: output and elemwise param share data in conv primitive. Output is properly allocated --- .../framework/ir/conv_elementwise_add_mkldnn_fuse_pass.cc | 4 +++- paddle/fluid/operators/conv_mkldnn_op.cc | 3 ++- paddle/fluid/operators/conv_op.cc | 3 ++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/paddle/fluid/framework/ir/conv_elementwise_add_mkldnn_fuse_pass.cc b/paddle/fluid/framework/ir/conv_elementwise_add_mkldnn_fuse_pass.cc index f2ff0bf13b5..1ede53f4689 100644 --- a/paddle/fluid/framework/ir/conv_elementwise_add_mkldnn_fuse_pass.cc +++ b/paddle/fluid/framework/ir/conv_elementwise_add_mkldnn_fuse_pass.cc @@ -118,6 +118,7 @@ void CorrectGraphEdges(Graph* graph, Node* from, Node* to) { if (same != std::end(node.inputs)) { LinkNodes(to, &node); + node.Op()->SetInput("X", {to->Name()}); } } } @@ -145,7 +146,7 @@ graph_ptr ConvElementwiseAddMKLDNNFusePass::ApplyImpl(graph_ptr graph) const { op_desc.SetInput("Input", {conv_input->Name()}); op_desc.SetInput("Filter", {conv_filter->Name()}); - op_desc.SetInput("ElementwiseParameter", {elementwise_add_x->Name()}); + op_desc.SetInput("EltwiseParameter", {elementwise_add_x->Name()}); op_desc.SetOutput("Output", {conv_output->Name()}); op_desc.SetAttr("use_mkldnn", true); @@ -155,6 +156,7 @@ graph_ptr ConvElementwiseAddMKLDNNFusePass::ApplyImpl(graph_ptr graph) const { patterns::LinkNodes(conv_input, fused_conv_op); patterns::LinkNodes(conv_filter, fused_conv_op); + patterns::LinkNodes(elementwise_add_x, fused_conv_op); patterns::LinkNodes(fused_conv_op, conv_output); }; diff --git a/paddle/fluid/operators/conv_mkldnn_op.cc b/paddle/fluid/operators/conv_mkldnn_op.cc index d9666c1cedc..c849caf94ff 100644 --- a/paddle/fluid/operators/conv_mkldnn_op.cc +++ b/paddle/fluid/operators/conv_mkldnn_op.cc @@ -396,7 +396,8 @@ class ConvMKLDNNOpKernel : public paddle::framework::OpKernel { PADDLE_ENFORCE(eltwise_param_data != nullptr, "Provide data if you want MKLDNN conv+elementwise_add fusion"); PADDLE_ENFORCE_EQ(output->dims(), eltwise_param->dims(), "Output and elementwise parameter need to have the same dimension sizes"); - output_data = const_cast(eltwise_param_data); + output_data = output->mutable_data(ctx.GetPlace()); + output->ShareDataWith(*eltwise_param); } else { output_data = output->mutable_data(ctx.GetPlace(), handler.GetDstMemorySize()); diff --git a/paddle/fluid/operators/conv_op.cc b/paddle/fluid/operators/conv_op.cc index efb8c627375..99c50a52078 100644 --- a/paddle/fluid/operators/conv_op.cc +++ b/paddle/fluid/operators/conv_op.cc @@ -134,7 +134,8 @@ void Conv2DOpMaker::Make() { .Reuse("Input"); AddInput("EltwiseParameter", "(Tensor) Tensor to which convolution output will be added." - "Used on with fuse_eltwise fusion."); + "Used on with fuse_eltwise fusion.") + .AsDispensable(); AddAttr>("strides", "(vector default:{1, 1}), the " "strides(h_stride, w_stride) of " -- GitLab