From 57934f0f4a2a3cbb09f5d242dbd0a3202f6cf6b6 Mon Sep 17 00:00:00 2001 From: Jacek Czaja Date: Mon, 20 Sep 2021 13:35:15 +0200 Subject: [PATCH 1/5] - candidate fix --- paddle/fluid/operators/mkldnn/conv_mkldnn_op.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/paddle/fluid/operators/mkldnn/conv_mkldnn_op.cc b/paddle/fluid/operators/mkldnn/conv_mkldnn_op.cc index 09386fc31ee31..d8510aead6915 100644 --- a/paddle/fluid/operators/mkldnn/conv_mkldnn_op.cc +++ b/paddle/fluid/operators/mkldnn/conv_mkldnn_op.cc @@ -706,7 +706,6 @@ class ConvMKLDNNOpKernel : public paddle::framework::OpKernel { platform::CreateKey(dev_ctx, src_tz, src_dt, ctx.InputName("Input") + ctx.InputName("Filter")); - const std::string key_conv_pd = key + "@conv_pd"; bool need_s8_to_u8 = false; std::shared_ptr conv_p; std::shared_ptr src_memory_p; @@ -721,6 +720,7 @@ class ConvMKLDNNOpKernel : public paddle::framework::OpKernel { // are merged/unified, this will disappear auto key_tid = platform::ExtendKeyWithThreadInfoIfNeeded(dev_ctx, key); + const std::string key_conv_pd = key_tid + "@conv_pd"; auto prim_key = key_tid + "@conv_p"; auto dst_key = key_tid + "@dst_mem_p"; auto src_key = key_tid + "@src_mem_p"; From b10e795c70ce067b89ad860f97e7bfe443e03147 Mon Sep 17 00:00:00 2001 From: Jacek Czaja Date: Tue, 21 Sep 2021 19:19:27 +0200 Subject: [PATCH 2/5] - More fixes to #34554 --- paddle/fluid/platform/mkldnn_reuse.h | 56 ++++++++++++---------------- 1 file changed, 23 insertions(+), 33 deletions(-) diff --git a/paddle/fluid/platform/mkldnn_reuse.h b/paddle/fluid/platform/mkldnn_reuse.h index 29a3f8e9dcd3c..d6ab9e50a066e 100644 --- a/paddle/fluid/platform/mkldnn_reuse.h +++ b/paddle/fluid/platform/mkldnn_reuse.h @@ -603,7 +603,6 @@ class MKLDNNHandler { const std::string& base_key) : dev_ctx_(dev_ctx), engine_(engine), - key_common_(base_key), key_(platform::ExtendKeyWithThreadInfoIfNeeded(dev_ctx, base_key)) { platform::MKLDNNDeviceContext::tls().log_lib_version(); } @@ -789,7 +788,6 @@ class MKLDNNHandler { protected: const MKLDNNDeviceContext& dev_ctx_; mkldnn::engine engine_; - std::string key_common_; std::string key_; }; @@ -1371,42 +1369,34 @@ class ConvMKLDNNTemplateHandler : public MKLDNNHandler { // Conv PD has to be passed to Grad op that // may be exxecuted by diffrent thread, hence // for that one we use key that does not contain TID - const std::string key_conv_pd = key_common_ + "@conv_pd"; + const std::string key_conv_pd = key_ + "@conv_pd"; conv_pd_ = std::static_pointer_cast( dev_ctx_.GetBlob(key_conv_pd)); if (conv_pd_ == nullptr) { - static std::mutex acquire_barrier; - std::lock_guard block_threads_until_finish_this_job( - acquire_barrier); - - conv_pd_ = std::static_pointer_cast( - dev_ctx_.GetBlob(key_conv_pd)); - if (conv_pd_ == nullptr) { - mkldnn::memory::dims stride_dims = strides; - mkldnn::memory::dims dilations_dims = dilations; - auto mkldnn_paddings = ToMkldnnPadding(paddings); - - auto conv_desc = - bias ? typename forward_t::desc( - fwd_prop_kind, convolutional_algorithm::T, - src, weights, *bias, dst, stride_dims, dilations_dims, - mkldnn_paddings[0], mkldnn_paddings[1]) - : typename forward_t::desc( - fwd_prop_kind, convolutional_algorithm::T, - src, weights, dst, stride_dims, dilations_dims, - mkldnn_paddings[0], mkldnn_paddings[1]); - - mkldnn::primitive_attr conv_attr = - CreatePostOps(fuse_activation, fuse_alpha, fuse_beta, - fuse_residual_conn, output_shift_scale, sum_scale); - - conv_pd_.reset(new typename forward_t::primitive_desc( - conv_desc, conv_attr, engine)); - // Save conv_pd/src_memory/weights_memory for backward pass - dev_ctx_.SetBlob(key_conv_pd, conv_pd_); - } + mkldnn::memory::dims stride_dims = strides; + mkldnn::memory::dims dilations_dims = dilations; + auto mkldnn_paddings = ToMkldnnPadding(paddings); + + auto conv_desc = + bias ? typename forward_t::desc( + fwd_prop_kind, convolutional_algorithm::T, src, + weights, *bias, dst, stride_dims, dilations_dims, + mkldnn_paddings[0], mkldnn_paddings[1]) + : typename forward_t::desc( + fwd_prop_kind, convolutional_algorithm::T, src, + weights, dst, stride_dims, dilations_dims, + mkldnn_paddings[0], mkldnn_paddings[1]); + + mkldnn::primitive_attr conv_attr = + CreatePostOps(fuse_activation, fuse_alpha, fuse_beta, + fuse_residual_conn, output_shift_scale, sum_scale); + + conv_pd_.reset( + new typename forward_t::primitive_desc(conv_desc, conv_attr, engine)); + // Save conv_pd/src_memory/weights_memory for backward pass + dev_ctx_.SetBlob(key_conv_pd, conv_pd_); } return conv_pd_; From da416cf33f9cca9f3564ce4e37aafd58b0c64b77 Mon Sep 17 00:00:00 2001 From: Jacek Czaja Date: Wed, 22 Sep 2021 15:17:13 +0200 Subject: [PATCH 3/5] - another incosnstent fix to key --- paddle/fluid/operators/mkldnn/conv_mkldnn_op.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/paddle/fluid/operators/mkldnn/conv_mkldnn_op.cc b/paddle/fluid/operators/mkldnn/conv_mkldnn_op.cc index d8510aead6915..10d98790164ec 100644 --- a/paddle/fluid/operators/mkldnn/conv_mkldnn_op.cc +++ b/paddle/fluid/operators/mkldnn/conv_mkldnn_op.cc @@ -730,6 +730,7 @@ class ConvMKLDNNOpKernel : public paddle::framework::OpKernel { auto user_residual_key = key_tid + "@user_residual_data_mem_p"; auto src_reorder_key = key_tid + "@src_mem_preorder_p"; auto residual_reorder_key = key_tid + "@residual_data_mem_preorder_p"; + auto scale_bias_key = key_tid + "@scale_bias"; conv_p = std::static_pointer_cast( dev_ctx.GetBlob(prim_key)); @@ -946,7 +947,6 @@ class ConvMKLDNNOpKernel : public paddle::framework::OpKernel { } // create convolution op primitive - auto scale_bias_key = key + "@scale_bias"; conv_p = handler->AcquireConvolution(); if (bias) { const K* bias_data = bias->data(); From 1591c769bb15f8f80d35e8e3b6037527d2bfd15b Mon Sep 17 00:00:00 2001 From: Jacek Czaja Date: Wed, 22 Sep 2021 16:14:23 +0200 Subject: [PATCH 4/5] - Remvoed unneeded line --- paddle/fluid/operators/mkldnn/conv_mkldnn_op.cc | 1 - 1 file changed, 1 deletion(-) diff --git a/paddle/fluid/operators/mkldnn/conv_mkldnn_op.cc b/paddle/fluid/operators/mkldnn/conv_mkldnn_op.cc index 10d98790164ec..a7552953d73e9 100644 --- a/paddle/fluid/operators/mkldnn/conv_mkldnn_op.cc +++ b/paddle/fluid/operators/mkldnn/conv_mkldnn_op.cc @@ -730,7 +730,6 @@ class ConvMKLDNNOpKernel : public paddle::framework::OpKernel { auto user_residual_key = key_tid + "@user_residual_data_mem_p"; auto src_reorder_key = key_tid + "@src_mem_preorder_p"; auto residual_reorder_key = key_tid + "@residual_data_mem_preorder_p"; - auto scale_bias_key = key_tid + "@scale_bias"; conv_p = std::static_pointer_cast( dev_ctx.GetBlob(prim_key)); From ea94e705ada46456bdfe1d71c544222befb40615 Mon Sep 17 00:00:00 2001 From: Jacek Czaja Date: Thu, 23 Sep 2021 13:03:40 +0200 Subject: [PATCH 5/5] - matching the cache behaviour to other ops --- .../fluid/operators/mkldnn/conv_mkldnn_op.cc | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/paddle/fluid/operators/mkldnn/conv_mkldnn_op.cc b/paddle/fluid/operators/mkldnn/conv_mkldnn_op.cc index a7552953d73e9..1b69dd7ea00c7 100644 --- a/paddle/fluid/operators/mkldnn/conv_mkldnn_op.cc +++ b/paddle/fluid/operators/mkldnn/conv_mkldnn_op.cc @@ -731,12 +731,13 @@ class ConvMKLDNNOpKernel : public paddle::framework::OpKernel { auto src_reorder_key = key_tid + "@src_mem_preorder_p"; auto residual_reorder_key = key_tid + "@residual_data_mem_preorder_p"; - conv_p = std::static_pointer_cast( - dev_ctx.GetBlob(prim_key)); + conv_pd = + std::static_pointer_cast( + dev_ctx.GetBlob(key_conv_pd)); auto& astream = platform::MKLDNNDeviceContext::tls().get_stream(); - if (conv_p == nullptr || !is_test) { + if (conv_pd == nullptr || !is_test) { float fuse_alpha = ctx.Attr("fuse_alpha"); float fuse_beta = ctx.Attr("fuse_beta"); bool force_fp32_output = ctx.Attr("force_fp32_output"); @@ -999,13 +1000,10 @@ class ConvMKLDNNOpKernel : public paddle::framework::OpKernel { dev_ctx.GetBlob(weights_key)); dst_memory_p = std::static_pointer_cast(dev_ctx.GetBlob(dst_key)); - conv_pd = - std::static_pointer_cast( - dev_ctx.GetBlob(key_conv_pd)); - if (conv_pd) { - handler.reset(new platform::ConvMKLDNNHandler(conv_pd, dev_ctx, - mkldnn_engine, key)); - } + conv_p = std::static_pointer_cast( + dev_ctx.GetBlob(prim_key)); + handler.reset(new platform::ConvMKLDNNHandler(conv_pd, dev_ctx, + mkldnn_engine, key)); if (fuse_residual_conn) { auto residual_param = ctx.Input("ResidualData");