From d7652d5f26f2def294146418b06ded113b9ec44f Mon Sep 17 00:00:00 2001 From: Jacek Czaja Date: Tue, 4 Oct 2022 08:15:20 +0200 Subject: [PATCH] first commit --- .../operators/mkldnn/activation_mkldnn_op.cc | 1 - .../operators/mkldnn/dequantize_mkldnn_op.cc | 1 - paddle/fluid/operators/mkldnn/fc_mkldnn_op.cc | 1 - .../operators/mkldnn/interpolate_mkldnn_op.cc | 1 - .../operators/mkldnn/matmul_v2_mkldnn_op.cc | 1 - .../fluid/operators/mkldnn/prelu_mkldnn_op.cc | 1 - .../operators/mkldnn/quantize_mkldnn_op.cc | 1 - .../operators/mkldnn/reshape_mkldnn_op.cc | 17 +- paddle/fluid/platform/mkldnn_helper.h | 158 ---------------- paddle/fluid/platform/mkldnn_utils.h | 172 ------------------ paddle/phi/core/dense_tensor.inl | 6 - paddle/phi/core/dense_tensor_impl.cc | 10 +- 12 files changed, 7 insertions(+), 363 deletions(-) delete mode 100644 paddle/fluid/platform/mkldnn_utils.h diff --git a/paddle/fluid/operators/mkldnn/activation_mkldnn_op.cc b/paddle/fluid/operators/mkldnn/activation_mkldnn_op.cc index 3c69f07db67..1ef81a8ecec 100644 --- a/paddle/fluid/operators/mkldnn/activation_mkldnn_op.cc +++ b/paddle/fluid/operators/mkldnn/activation_mkldnn_op.cc @@ -28,7 +28,6 @@ using dnnl::primitive; using dnnl::stream; using framework::DataLayout; -using platform::GetMKLDNNFormat; using platform::MKLDNNDeviceContext; using platform::to_void_cast; diff --git a/paddle/fluid/operators/mkldnn/dequantize_mkldnn_op.cc b/paddle/fluid/operators/mkldnn/dequantize_mkldnn_op.cc index 12ac31804a0..ff6e932374e 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 framework::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 01dfa82e903..564a8dbd29f 100644 --- a/paddle/fluid/operators/mkldnn/fc_mkldnn_op.cc +++ b/paddle/fluid/operators/mkldnn/fc_mkldnn_op.cc @@ -30,7 +30,6 @@ using framework::DataLayout; using framework::DDim; using framework::ExecutionContext; using framework::LoDTensor; -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 2892f3489ed..080c053f050 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 framework::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 44296d12f2b..7e7a2dd8b42 100644 --- a/paddle/fluid/operators/mkldnn/matmul_v2_mkldnn_op.cc +++ b/paddle/fluid/operators/mkldnn/matmul_v2_mkldnn_op.cc @@ -17,7 +17,6 @@ namespace { using dnnl::memory; using paddle::framework::DataLayout; 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 e3b9d3ffd7c..790ce69cf8f 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 ae94266b4da..5f7d26b7572 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 framework::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 dea6abd0c02..d38c1dce05d 100644 --- a/paddle/fluid/operators/mkldnn/reshape_mkldnn_op.cc +++ b/paddle/fluid/operators/mkldnn/reshape_mkldnn_op.cc @@ -31,7 +31,6 @@ namespace paddle { namespace operators { using paddle::framework::LoDTensor; -using platform::GetMKLDNNFormat; using platform::to_void_cast; static std::vector extract_shape( @@ -89,8 +88,8 @@ class ReshapeMKLDNNKernel : public framework::OpKernel { // 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); @@ -98,9 +97,7 @@ class ReshapeMKLDNNKernel : public framework::OpKernel { astream.wait(); out->Resize(out_dims); - out->set_layout(framework::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, @@ -362,17 +359,15 @@ class ReshapeGradMKLDNNKernel : public ReshapeMKLDNNKernel { dout->format(), 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(framework::DataLayout::kMKLDNN); - dx->set_format(GetMKLDNNFormat( - reorder_dst_memory_p->get_desc().reshape(phi::vectorize(dx_dims)))); + dx->set_mem_desc(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 07f5f3408a3..810f309f663 100644 --- a/paddle/fluid/platform/mkldnn_helper.h +++ b/paddle/fluid/platform/mkldnn_helper.h @@ -202,164 +202,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 @@@@@@@@@@@@@@@@@@@"< 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_; -} + return mem_desc_; #endif // NOTE: For historical reasons, this interface has a special behavior, -- GitLab