diff --git a/paddle/fluid/framework/tensor_util.cc b/paddle/fluid/framework/tensor_util.cc index 691189b2e4cc3ef75a7d5d72cb8f7e555c1c3769..bff439d13a50871f74f985360e5b3e16ac9ae349 100644 --- a/paddle/fluid/framework/tensor_util.cc +++ b/paddle/fluid/framework/tensor_util.cc @@ -1245,11 +1245,6 @@ std::ostream& operator<<(std::ostream& os, const phi::DenseTensor& t) { os << " - shape: [" << t.dims() << "]\n"; os << " - layout: " << phi::DataLayoutToString(t.layout()) << "\n"; -#ifdef PADDLE_WITH_MKLDNN - os << " - format: " - << dnnl_fmt_tag2str(static_cast(t.format())) << "\n"; -#endif - DenseTensor tensor; tensor.Resize(t.dims()); if (paddle::platform::is_cpu_place(t.place())) { diff --git a/paddle/fluid/operators/fused/mkldnn/fusion_rnn_mkldnn.h b/paddle/fluid/operators/fused/mkldnn/fusion_rnn_mkldnn.h index 01c7e99185ed706f3aa890b64df0a62548d45a03..b04b3d19281ba6fa38108409632da7a341d3e2de 100644 --- a/paddle/fluid/operators/fused/mkldnn/fusion_rnn_mkldnn.h +++ b/paddle/fluid/operators/fused/mkldnn/fusion_rnn_mkldnn.h @@ -77,9 +77,12 @@ class RNNMKLDNNHandler : public platform::MKLDNNHandlerT { } } - bool is_NTC() { - return (platform::GetMKLDNNFormat(this->fwd_pd_->dst_desc()) == - dnnl::memory::format_tag::ntc); + bool is_NTC() { return this->is_NTC(this->fwd_pd_->dst_desc()); } + + bool is_NTC(const dnnl::memory::desc& md) { + auto ntc_md = dnnl::memory::desc( + md.dims(), md.data_type(), dnnl::memory::format_tag::ntc); + return md == ntc_md; } void reorderRNNdata(void* input_data, @@ -165,8 +168,7 @@ class RNNMKLDNNHandler : public platform::MKLDNNHandlerT { auto* x_onednn_data = memory_p->get_data_handle(); memset(x_onednn_data, 0, sizeof(T) * N * Ti * IC); - if (platform::GetMKLDNNFormat(this->fwd_pd_->src_desc()) == - dnnl::memory::format_tag::ntc) { + if (is_NTC(this->fwd_pd_->src_desc())) { reorderRNNdata(x_data, x_onednn_data, input_lod, diff --git a/paddle/fluid/operators/fused/mkldnn/multi_gru_mkldnn_op.cc b/paddle/fluid/operators/fused/mkldnn/multi_gru_mkldnn_op.cc index 25fe82d1e27fec5421dde124b8eb7e3a18efb28b..79ce2ea2c90a29f967b6574dd981cc704041f2a2 100644 --- a/paddle/fluid/operators/fused/mkldnn/multi_gru_mkldnn_op.cc +++ b/paddle/fluid/operators/fused/mkldnn/multi_gru_mkldnn_op.cc @@ -256,8 +256,7 @@ class MultiGRUHandler { auto* x_onednn_data = memory_p->get_data_handle(); memset(x_onednn_data, 0, sizeof(T) * N_ * Ti_ * ICs[0]); - if (platform::GetMKLDNNFormat(gru_pds_[{0, L2R}]->src_desc()) == - dnnl::memory::format_tag::ntc) { + if (isNTC(gru_pds_[{0, L2R}]->src_desc())) { reorderPPtoNTC(x_data, x_onednn_data, x_lod_, 0, L2R); } else { reorderPPtoTNC(x_data, x_onednn_data, x_lod_, 0, L2R); @@ -601,16 +600,18 @@ class MultiGRUHandler { void reorderOutput(std::shared_ptr mem, int layer) { auto* data = mem->get_data_handle(); auto* hidden_data = to_void_cast(hidden_->mutable_data(place_)); - if (isNTC(layers_ - 1)) { + + if (isNTC(gru_pds_[{layers_ - 1, L2R}]->dst_desc())) { reorderNTCtoPP(data, hidden_data, layers_ - 1); } else { reorderTNCtoPP(data, hidden_data, layers_ - 1); } } - bool isNTC(int layer) { - return (platform::GetMKLDNNFormat(gru_pds_[{layer, L2R}]->dst_desc()) == - dnnl::memory::format_tag::ntc); + bool isNTC(const dnnl::memory::desc& md) { + auto ntc_md = dnnl::memory::desc( + md.dims(), md.data_type(), dnnl::memory::format_tag::ntc); + return md == ntc_md; } int getLayers() const { return layers_; } diff --git a/paddle/fluid/operators/mkldnn/dequantize_mkldnn_op.cc b/paddle/fluid/operators/mkldnn/dequantize_mkldnn_op.cc index e0ba5b0aa5ce2caaeb7c1170cc92b0bd633971fc..c4b8b267a00c4bdedccb287f5d572ac3c7d81a16 100644 --- a/paddle/fluid/operators/mkldnn/dequantize_mkldnn_op.cc +++ b/paddle/fluid/operators/mkldnn/dequantize_mkldnn_op.cc @@ -30,7 +30,6 @@ using platform::to_void_cast; using Tensor = phi::DenseTensor; using dnnl::stream; using phi::DataLayout; -using platform::GetMKLDNNFormat; template class DeQuantOpKernel : public framework::OpKernel { diff --git a/paddle/fluid/operators/mkldnn/fc_mkldnn_op.cc b/paddle/fluid/operators/mkldnn/fc_mkldnn_op.cc index f47838283db95ebd32ade43dc74f0df5e9b2b38a..28bceeff53607e99d3ed56bc4fb8d9c7bf19c74f 100644 --- a/paddle/fluid/operators/mkldnn/fc_mkldnn_op.cc +++ b/paddle/fluid/operators/mkldnn/fc_mkldnn_op.cc @@ -29,7 +29,6 @@ using dnnl::stream; using framework::DDim; using framework::ExecutionContext; using LoDTensor = phi::DenseTensor; -using platform::GetMKLDNNFormat; using platform::MKLDNNDeviceContext; using platform::MKLDNNGetDataType; using platform::to_void_cast; diff --git a/paddle/fluid/operators/mkldnn/interpolate_mkldnn_op.cc b/paddle/fluid/operators/mkldnn/interpolate_mkldnn_op.cc index dc88440e0d32e382b678f0ad3b042ae2824739e9..a868bc3b502ebad865c08402210740e175071de9 100644 --- a/paddle/fluid/operators/mkldnn/interpolate_mkldnn_op.cc +++ b/paddle/fluid/operators/mkldnn/interpolate_mkldnn_op.cc @@ -25,7 +25,6 @@ using dnnl::reorder; using dnnl::resampling_forward; using dnnl::stream; using phi::DataLayout; -using platform::GetMKLDNNFormat; using platform::to_void_cast; template diff --git a/paddle/fluid/operators/mkldnn/matmul_v2_mkldnn_op.cc b/paddle/fluid/operators/mkldnn/matmul_v2_mkldnn_op.cc index 0978bf01e44432ba39f651ba53bbd2521ed85f00..d0dab34bcebb34dc016416df5145b27dc671c4c2 100644 --- a/paddle/fluid/operators/mkldnn/matmul_v2_mkldnn_op.cc +++ b/paddle/fluid/operators/mkldnn/matmul_v2_mkldnn_op.cc @@ -16,7 +16,6 @@ limitations under the License. */ namespace { using dnnl::memory; using paddle::framework::ExecutionContext; -using paddle::platform::GetMKLDNNFormat; using paddle::platform::MatMulV2MKLDNNHandler; using paddle::platform::MKLDNNDeviceContext; using paddle::platform::MKLDNNFormatForSize; diff --git a/paddle/fluid/operators/mkldnn/prelu_mkldnn_op.cc b/paddle/fluid/operators/mkldnn/prelu_mkldnn_op.cc index 246bfacca7f4ef04809fd56840a1d47a327d392d..4c517a622815e0a407c19a136e54089b6d3fb6c3 100644 --- a/paddle/fluid/operators/mkldnn/prelu_mkldnn_op.cc +++ b/paddle/fluid/operators/mkldnn/prelu_mkldnn_op.cc @@ -20,7 +20,6 @@ namespace operators { using dnnl::memory; -using platform::GetMKLDNNFormat; using platform::MKLDNNDeviceContext; using platform::MKLDNNGetDataType; using platform::to_void_cast; diff --git a/paddle/fluid/operators/mkldnn/quantize_mkldnn_op.cc b/paddle/fluid/operators/mkldnn/quantize_mkldnn_op.cc index 4847c75ec757b10907167c4f1b19f9d0b0d34858..bd62094d89e6470377de4174301aea7d08e035a3 100644 --- a/paddle/fluid/operators/mkldnn/quantize_mkldnn_op.cc +++ b/paddle/fluid/operators/mkldnn/quantize_mkldnn_op.cc @@ -29,7 +29,6 @@ using platform::to_void_cast; using Tensor = phi::DenseTensor; using dnnl::stream; using phi::DataLayout; -using platform::GetMKLDNNFormat; template class QuantOpKernel : public framework::OpKernel { diff --git a/paddle/fluid/operators/mkldnn/reshape_mkldnn_op.cc b/paddle/fluid/operators/mkldnn/reshape_mkldnn_op.cc index 7810b48635e27085e5a839ade3a8d0dda01ef3e0..6480125c93fd7978a8822fc9f637f4d7f481efb7 100644 --- a/paddle/fluid/operators/mkldnn/reshape_mkldnn_op.cc +++ b/paddle/fluid/operators/mkldnn/reshape_mkldnn_op.cc @@ -30,7 +30,6 @@ enum class ReshapeKernelOpName { namespace paddle { namespace operators { -using platform::GetMKLDNNFormat; using platform::to_void_cast; static std::vector extract_shape( @@ -83,13 +82,13 @@ class ReshapeMKLDNNKernel : public framework::OpKernel { onednn_engine); auto reorder_src_memory_p = reorder_handler.AcquireSrcMemory( - x->format(), platform::to_void_cast(x->data())); + x->mem_desc(), platform::to_void_cast(x->data())); out->Resize(x_dims); // to match x numel, format is changed later // reorder is done into a plain tag to allow usage with blocked formats auto reorder_dst_memory_p = reorder_handler.AcquireDstMemory( out, getPlainFormatTag(x), ctx.GetPlace()); - auto reorder_p = reorder_handler.AcquireReorder(reorder_src_memory_p, - reorder_dst_memory_p); + auto reorder_p = reorder_handler.AcquireReorder(reorder_dst_memory_p, + reorder_src_memory_p); auto& astream = platform::MKLDNNDeviceContext::tls().get_stream(); reorder_p->execute(astream, *reorder_src_memory_p, *reorder_dst_memory_p); @@ -97,9 +96,8 @@ class ReshapeMKLDNNKernel : public framework::OpKernel { astream.wait(); out->Resize(out_dims); - out->set_layout(phi::DataLayout::kMKLDNN); - out->set_format(GetMKLDNNFormat( - reorder_dst_memory_p->get_desc().reshape(phi::vectorize(out_dims)))); + out->set_mem_desc( + reorder_dst_memory_p->get_desc().reshape(phi::vectorize(out_dims))); } void InferInOutShape(const framework::ExecutionContext& ctx, @@ -358,20 +356,18 @@ class ReshapeGradMKLDNNKernel : public ReshapeMKLDNNKernel { onednn_engine); auto reorder_src_memory_p = reorder_handler.AcquireSrcMemory( - dout->format(), platform::to_void_cast(dout->data())); + dout->mem_desc(), platform::to_void_cast(dout->data())); auto reorder_dst_memory_p = reorder_handler.AcquireDstMemory( dx, this->getPlainFormatTag(dout), ctx.GetPlace()); - auto reorder_p = reorder_handler.AcquireReorder(reorder_src_memory_p, - reorder_dst_memory_p); + auto reorder_p = reorder_handler.AcquireReorder(reorder_dst_memory_p, + reorder_src_memory_p); auto& astream = platform::MKLDNNDeviceContext::tls().get_stream(); reorder_p->execute(astream, *reorder_src_memory_p, *reorder_dst_memory_p); astream.wait(); dx->Resize(dx_dims); - dx->set_layout(phi::DataLayout::kMKLDNN); - dx->set_format(GetMKLDNNFormat( - reorder_dst_memory_p->get_desc().reshape(phi::vectorize(dx_dims)))); + reorder_dst_memory_p->get_desc().reshape(phi::vectorize(dx_dims)); } void InferOutputShapeInGrad(const framework::ExecutionContext& ctx, diff --git a/paddle/fluid/platform/mkldnn_helper.h b/paddle/fluid/platform/mkldnn_helper.h index 3bbb0917a134d4c7a39ae5318b5d2ac9879af6ce..b57ee09a509600c6255e9a9a25f429b254dab378 100644 --- a/paddle/fluid/platform/mkldnn_helper.h +++ b/paddle/fluid/platform/mkldnn_helper.h @@ -201,164 +201,6 @@ inline void Reorder(dnnl::memory src, astream.wait(); } -inline dnnl::memory::format_tag GetMKLDNNFormat(dnnl::memory::desc mem_desc) { - auto ndims = mem_desc.data.ndims; - auto strides = mem_desc.data.format_desc.blocking.strides; - auto inner_nblks = mem_desc.data.format_desc.blocking.inner_nblks; - auto inner_blks = mem_desc.data.format_desc.blocking.inner_blks; - auto inner_idxs = mem_desc.data.format_desc.blocking.inner_idxs; - - if (ndims == 1) { - return dnnl::memory::format_tag::x; - } else if (ndims == 2) { - if (inner_nblks == 0) { - if (strides[0] >= strides[1]) { - return dnnl::memory::format_tag::nc; - } else { - return dnnl::memory::format_tag::cn; - } - } - } else if (ndims == 3) { - if (inner_nblks == 0) { - if (strides[0] >= strides[1] && strides[1] >= strides[2]) { - return dnnl::memory::format_tag::ncw; - } else if (strides[1] >= strides[0] && strides[0] >= strides[2]) { - return dnnl::memory::format_tag::ntc; - } else { - return dnnl::memory::format_tag::nwc; - } - } - } else if (ndims == 4) { - if (inner_nblks == 0) { - if (strides[0] >= strides[1] && strides[1] >= strides[2] && - strides[2] >= strides[3]) { - return dnnl::memory::format_tag::abcd; - } else if (strides[2] >= strides[3] && strides[3] >= strides[1] && - strides[1] >= strides[0]) { - return dnnl::memory::format_tag::cdba; - } else if (strides[0] >= strides[2] && strides[2] >= strides[3] && - strides[3] >= strides[1]) { - return dnnl::memory::format_tag::acdb; - } else if (strides[0] >= strides[1] && strides[1] >= strides[3] && - strides[3] >= strides[2]) { - return dnnl::memory::format_tag::abdc; - } else if (strides[2] >= strides[3] && strides[3] >= strides[1] && - strides[1] >= strides[0]) { - return dnnl::memory::format_tag::cdba; - } else { - return dnnl::memory::format_tag::dcab; - } - } else if (inner_nblks == 1) { - if (inner_blks[0] == 16 && inner_idxs[0] == 1) { - return dnnl::memory::format_tag::nChw16c; - } else if (inner_blks[0] == 8 && inner_idxs[0] == 1) { - return dnnl::memory::format_tag::nChw8c; - } else if (inner_blks[0] == 8 && inner_idxs[0] == 0) { - if (strides[0] >= strides[2] && strides[2] >= strides[3] && - strides[3] >= strides[1]) { - return dnnl::memory::format_tag::Acdb8a; - } - } else if (inner_blks[0] == 4 && inner_idxs[0] == 1) { - return dnnl::memory::format_tag::nChw4c; - } else if (inner_blks[0] == 16 && inner_idxs[0] == 0) { - if (strides[0] >= strides[2] && strides[2] >= strides[3] && - strides[3] >= strides[1]) { - return dnnl::memory::format_tag::Acdb16a; - } - } - } else if (inner_nblks == 2) { - if (inner_blks[0] == 16 && inner_blks[1] == 16) { - if (inner_idxs[0] == 1 && inner_idxs[1] == 0) { - return dnnl::memory::format_tag::OIhw16i16o; - } - } else if (inner_blks[0] == 8 && inner_blks[1] == 8) { - if (inner_idxs[0] == 1 && inner_idxs[1] == 0) { - return dnnl::memory::format_tag::OIhw8i8o; - } - } - } - } else if (ndims == 5) { - if (inner_nblks == 0) { - if (strides[0] >= strides[1] && strides[1] >= strides[2] && - strides[2] >= strides[3] && strides[3] >= strides[4]) { - return dnnl::memory::format_tag::abcde; - } else if (strides[0] >= strides[2] && strides[2] >= strides[1] && - strides[1] >= strides[3] && strides[3] >= strides[4]) { - return dnnl::memory::format_tag::acbde; - } else if (strides[0] >= strides[2] && strides[2] >= strides[3] && - strides[3] >= strides[4] && strides[4] >= strides[1]) { - return dnnl::memory::format_tag::acdeb; - } - } else if (inner_nblks == 1) { - if (inner_blks[0] == 4 && inner_idxs[0] == 1) { - if (strides[0] >= strides[1] && strides[1] >= strides[2] && - strides[2] >= strides[3] && strides[3] >= strides[4]) { - return dnnl::memory::format_tag::aBcde4b; - } - } else if (inner_blks[0] == 8 && inner_idxs[0] == 0) { - if (strides[0] >= strides[2] && strides[2] >= strides[3] && - strides[3] >= strides[4] && strides[4] >= strides[1]) { - return dnnl::memory::format_tag::Acdeb8a; - } - if (strides[0] >= strides[1] && strides[1] >= strides[2] && - strides[2] >= strides[3] && strides[3] >= strides[4]) { - return dnnl::memory::format_tag::Abcde8a; - } - } else if (inner_blks[0] == 8 && inner_idxs[0] == 1) { - if (strides[0] >= strides[1] && strides[1] >= strides[2] && - strides[2] >= strides[3] && strides[3] >= strides[4]) { - return dnnl::memory::format_tag::aBcde8b; - } - } else if (inner_blks[0] == 16 && inner_idxs[0] == 0) { - if (strides[0] >= strides[2] && strides[2] >= strides[3] && - strides[3] >= strides[4] && strides[4] >= strides[1]) { - return dnnl::memory::format_tag::Acdeb16a; - } - if (strides[0] >= strides[1] && strides[1] >= strides[2] && - strides[2] >= strides[3] && strides[3] >= strides[4]) { - return dnnl::memory::format_tag::Abcde16a; - } - } else if (inner_blks[0] == 16 && inner_idxs[0] == 1) { - if (strides[0] >= strides[1] && strides[1] >= strides[2] && - strides[2] >= strides[3] && strides[3] >= strides[4]) { - return dnnl::memory::format_tag::aBcde16b; - } - } - } - } else if (ndims == 6) { - if (inner_nblks == 0) { - if (strides[0] >= strides[1] && strides[1] >= strides[2] && - strides[2] >= strides[3] && strides[3] >= strides[4] && - strides[4] >= strides[5]) { - return dnnl::memory::format_tag::abcdef; - } else if (strides[0] >= strides[2] && strides[2] >= strides[1] && - strides[1] >= strides[3] && strides[3] >= strides[4] && - strides[4] >= strides[5]) { - return dnnl::memory::format_tag::acbdef; - } - } - } - // DEBUG CODE - KEEP UNTILL TENSOR.MEMORY_DESC IMPLEMENTED - // std::cout<<"@@@@@@@@@@ UNDEFINED FORMAT @@@@@@@@@@@@@@@@@@@"<= strides[1]) { - return dnnl::memory::format_tag::nc; - } else { - return dnnl::memory::format_tag::cn; - } - } - } else if (ndims == 3) { - if (inner_nblks == 0) { - if (strides[0] >= strides[1] && strides[1] >= strides[2]) { - return dnnl::memory::format_tag::ncw; - } else if (strides[1] >= strides[0] && strides[0] >= strides[2]) { - return dnnl::memory::format_tag::ntc; - } else { - return dnnl::memory::format_tag::nwc; - } - } - } else if (ndims == 4) { - if (inner_nblks == 0) { - if (strides[0] >= strides[1] && strides[1] >= strides[2] && - strides[2] >= strides[3]) { - return dnnl::memory::format_tag::abcd; - } else if (strides[2] >= strides[3] && strides[3] >= strides[1] && - strides[1] >= strides[0]) { - return dnnl::memory::format_tag::cdba; - } else if (strides[0] >= strides[2] && strides[2] >= strides[3] && - strides[3] >= strides[1]) { - return dnnl::memory::format_tag::acdb; - } else if (strides[0] >= strides[1] && strides[1] >= strides[3] && - strides[3] >= strides[2]) { - return dnnl::memory::format_tag::abdc; - } else if (strides[2] >= strides[3] && strides[3] >= strides[1] && - strides[1] >= strides[0]) { - return dnnl::memory::format_tag::cdba; - } else { - return dnnl::memory::format_tag::dcab; - } - } else if (inner_nblks == 1) { - if (inner_blks[0] == 16 && inner_idxs[0] == 1) { - return dnnl::memory::format_tag::nChw16c; - } else if (inner_blks[0] == 8 && inner_idxs[0] == 1) { - return dnnl::memory::format_tag::nChw8c; - } else if (inner_blks[0] == 8 && inner_idxs[0] == 0) { - if (strides[0] >= strides[2] && strides[2] >= strides[3] && - strides[3] >= strides[1]) { - return dnnl::memory::format_tag::Acdb8a; - } - } else if (inner_blks[0] == 4 && inner_idxs[0] == 1) { - return dnnl::memory::format_tag::nChw4c; - } else if (inner_blks[0] == 16 && inner_idxs[0] == 0) { - if (strides[0] >= strides[2] && strides[2] >= strides[3] && - strides[3] >= strides[1]) { - return dnnl::memory::format_tag::Acdb16a; - } - } - } else if (inner_nblks == 2) { - if (inner_blks[0] == 16 && inner_blks[1] == 16) { - if (inner_idxs[0] == 1 && inner_idxs[1] == 0) { - return dnnl::memory::format_tag::OIhw16i16o; - } - } else if (inner_blks[0] == 8 && inner_blks[1] == 8) { - if (inner_idxs[0] == 1 && inner_idxs[1] == 0) { - return dnnl::memory::format_tag::OIhw8i8o; - } - } - } - } else if (ndims == 5) { - if (inner_nblks == 0) { - if (strides[0] >= strides[1] && strides[1] >= strides[2] && - strides[2] >= strides[3] && strides[3] >= strides[4]) { - return dnnl::memory::format_tag::abcde; - } else if (strides[0] >= strides[2] && strides[2] >= strides[1] && - strides[1] >= strides[3] && strides[3] >= strides[4]) { - return dnnl::memory::format_tag::acbde; - } else if (strides[0] >= strides[2] && strides[2] >= strides[3] && - strides[3] >= strides[4] && strides[4] >= strides[1]) { - return dnnl::memory::format_tag::acdeb; - } - } else if (inner_nblks == 1) { - if (inner_blks[0] == 8 && inner_idxs[0] == 0) { - if (strides[0] >= strides[2] && strides[2] >= strides[3] && - strides[3] >= strides[4] && strides[4] >= strides[1]) { - return dnnl::memory::format_tag::Acdeb8a; - } - if (strides[0] >= strides[1] && strides[1] >= strides[2] && - strides[2] >= strides[3] && strides[3] >= strides[4]) { - return dnnl::memory::format_tag::Abcde8a; - } - } else if (inner_blks[0] == 8 && inner_idxs[0] == 1) { - if (strides[0] >= strides[1] && strides[1] >= strides[2] && - strides[2] >= strides[3] && strides[3] >= strides[4]) { - return dnnl::memory::format_tag::aBcde8b; - } - } else if (inner_blks[0] == 16 && inner_idxs[0] == 0) { - if (strides[0] >= strides[2] && strides[2] >= strides[3] && - strides[3] >= strides[4] && strides[4] >= strides[1]) { - return dnnl::memory::format_tag::Acdeb16a; - } - if (strides[0] >= strides[1] && strides[1] >= strides[2] && - strides[2] >= strides[3] && strides[3] >= strides[4]) { - return dnnl::memory::format_tag::Abcde16a; - } - } else if (inner_blks[0] == 16 && inner_idxs[0] == 1) { - if (strides[0] >= strides[1] && strides[1] >= strides[2] && - strides[2] >= strides[3] && strides[3] >= strides[4]) { - return dnnl::memory::format_tag::aBcde16b; - } - } - } - } else if (ndims == 6) { - if (inner_nblks == 0) { - if (strides[0] >= strides[1] && strides[1] >= strides[2] && - strides[2] >= strides[3] && strides[3] >= strides[4] && - strides[4] >= strides[5]) { - return dnnl::memory::format_tag::abcdef; - } else if (strides[0] >= strides[2] && strides[2] >= strides[1] && - strides[1] >= strides[3] && strides[3] >= strides[4] && - strides[4] >= strides[5]) { - return dnnl::memory::format_tag::acbdef; - } - } - } - // DEBUG CODE - KEEP UNTILL TENSOR.MEMORY_DESC IMPLEMENTED - // std::cout<<"@@@@@@@@@@ UNDEFINED FORMAT @@@@@@@@@@@@@@@@@@@"< AcquireDstMemory( + DenseTensor* output, + const std::vector& dims, + const std::vector& strides, + Place place) { + auto dst_md = dnnl::memory::desc(dims, dtype_dst_, strides); + auto dst_data = output->mutable_data(place, ptype_dst_, dst_md.get_size()); + return std::make_shared(dst_md, engine_, dst_data); + } + std::shared_ptr AcquireDstMemory( DenseTensor* output, const std::vector& dims, diff --git a/paddle/phi/core/dense_tensor.cc b/paddle/phi/core/dense_tensor.cc index 8a2d0e8a46bd460b104d08640d637ca2ab25dba2..844182ec3fc82ee3db46b866c13f57d88b90b48c 100644 --- a/paddle/phi/core/dense_tensor.cc +++ b/paddle/phi/core/dense_tensor.cc @@ -58,7 +58,6 @@ DenseTensor::DenseTensor(const DenseTensor& other) : meta_(other.meta()) { inplace_version_counter_ = other.inplace_version_counter_; #ifdef PADDLE_WITH_MKLDNN - format_ = other.format_; mem_desc_ = other.mem_desc_; #endif } @@ -70,7 +69,6 @@ DenseTensor& DenseTensor::operator=(const DenseTensor& other) { std::move(CopyStorageProperties(other.storage_properties_)); inplace_version_counter_ = other.inplace_version_counter_; #ifdef PADDLE_WITH_MKLDNN - format_ = other.format_; mem_desc_ = other.mem_desc_; #endif return *this; @@ -82,7 +80,6 @@ DenseTensor& DenseTensor::operator=(DenseTensor&& other) { storage_properties_ = std::move(other.storage_properties_); std::swap(inplace_version_counter_, other.inplace_version_counter_); #ifdef PADDLE_WITH_MKLDNN - format_ = other.format_; mem_desc_ = other.mem_desc_; #endif return *this; diff --git a/paddle/phi/core/dense_tensor.h b/paddle/phi/core/dense_tensor.h index e0d620ac3a53e05704ec4995370e009bb78b4644..0785af7b7037ca5c2769887c5119871ae5ab2be0 100644 --- a/paddle/phi/core/dense_tensor.h +++ b/paddle/phi/core/dense_tensor.h @@ -274,16 +274,6 @@ In the final state, we should come up with a MKLDNN_Tensor and move the following codes there. */ #ifdef PADDLE_WITH_MKLDNN - /** - * @brief the detail format of memory block which have layout as kMKLDNN - * - * @note MKLDNN lib support various memory format like nchw, nhwc, nChw8C, - * nChw16c, etc. For a MKLDNN memory block, layout will be set as - * DataLayout::kMKLDNN meanwhile detail memory format will be kept in - * this field. - */ - dnnl::memory::format_tag format_ = dnnl::memory::format_tag::undef; - /// \brief memory descriptor of tensor which have layout set as kMKLDNN dnnl::memory::desc mem_desc_; #endif diff --git a/paddle/phi/core/dense_tensor.inl b/paddle/phi/core/dense_tensor.inl index 38e5a5f1091d1cdea245480d8883f218f01ba105..c876ba679ba5dc1b4986424b742826f293520427 100644 --- a/paddle/phi/core/dense_tensor.inl +++ b/paddle/phi/core/dense_tensor.inl @@ -123,12 +123,6 @@ inline void set_mem_desc(const dnnl::memory::desc& mem_desc) { meta_.layout = DataLayout::kMKLDNN; } -dnnl::memory::format_tag format() const; - -inline void set_format(const dnnl::memory::format_tag format) { - format_ = format; -} - #endif /* ------------------------------ */ diff --git a/paddle/phi/core/dense_tensor_impl.cc b/paddle/phi/core/dense_tensor_impl.cc index b78a9dc135e485863139605e1a7dc20c0df3bb52..f16b6625011a1ad77eecca0a1eaca55c79105288 100644 --- a/paddle/phi/core/dense_tensor_impl.cc +++ b/paddle/phi/core/dense_tensor_impl.cc @@ -19,10 +19,6 @@ limitations under the License. */ #include "paddle/phi/core/compat/convert_utils.h" #include "paddle/phi/core/dense_tensor.h" -#ifdef PADDLE_WITH_MKLDNN -#include "paddle/fluid/platform/mkldnn_utils.h" -#endif - namespace phi { /* --------------------------- */ /* From phi::DenseTensor */ @@ -348,16 +344,7 @@ std::vector DenseTensor::Chunk(int64_t chunks, } #ifdef PADDLE_WITH_MKLDNN -dnnl::memory::desc DenseTensor::mem_desc() const { - return mem_desc_ ? mem_desc_ - : dnnl::memory::desc(phi::vectorize(meta_.dims), - phi::TransToOneDNNDataType(meta_.dtype), - format_); -} - -dnnl::memory::format_tag DenseTensor::format() const { - return mem_desc_ ? paddle::platform::GetMKLDNNFormat(mem_desc_) : format_; -} +dnnl::memory::desc DenseTensor::mem_desc() const { return mem_desc_; } #endif // NOTE: For historical reasons, this interface has a special behavior, @@ -373,7 +360,6 @@ DenseTensor& DenseTensor::ShareDataWith(const DenseTensor& src) { storage_properties_ = std::move(CopyStorageProperties(src.storage_properties_)); #ifdef PADDLE_WITH_MKLDNN - format_ = src.format_; mem_desc_ = src.mem_desc_; #endif return *this; diff --git a/paddle/phi/kernels/onednn/split_kernel.cc b/paddle/phi/kernels/onednn/split_kernel.cc index 062233b19805e9c6b95e1231654f9028d26069ad..c4371a24ac252ff7fc51c246b341f171cc8f72b1 100644 --- a/paddle/phi/kernels/onednn/split_kernel.cc +++ b/paddle/phi/kernels/onednn/split_kernel.cc @@ -19,6 +19,23 @@ namespace phi { +const std::vector get_slice_strides( + const std::vector& out_vec_dims, + const dnnl::memory::desc& full_md, + int axis) { + auto strides = full_md.data.format_desc.blocking.strides; + auto ndims = full_md.data.ndims; + auto full_dims = full_md.data.dims; + auto splitted_stride = strides[axis]; + std::vector slice_strides(ndims, splitted_stride); + for (int16_t i = 0; i < ndims; ++i) { + slice_strides[i] = strides[i] > splitted_stride + ? (strides[i] / full_dims[axis]) * out_vec_dims[axis] + : strides[i]; + } + return slice_strides; +} + template void SplitKernel(const Context& dev_ctx, const DenseTensor& x, @@ -49,7 +66,10 @@ void SplitKernel(const Context& dev_ctx, out_vec_dims, offset, reorder_src_memory_p); auto reorder_dst_memory_p = reorder_handler.AcquireDstMemory( - out[i], out_vec_dims, x.format(), dev_ctx.GetPlace()); + out[i], + out_vec_dims, + get_slice_strides(out_vec_dims, x.mem_desc(), axis), + dev_ctx.GetPlace()); auto reorder_p = reorder_handler.AcquireReorder(reorder_dst_memory_p, slice_mem_p);