diff --git a/paddle/fluid/operators/mkldnn/activation_mkldnn_op.cc b/paddle/fluid/operators/mkldnn/activation_mkldnn_op.cc index 3c69f07db6758fa200219e9d629a44a4b76b993a..1ef81a8ecec8e74c931e2379bf91cd6ab5a9e01b 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 12ac31804a0db32a915feee285226d224aea7dfd..ff6e932374e38ae436dc2ea6752ad415704940ae 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 01dfa82e903334d88e49e22e46839494f589b3df..564a8dbd29f5c3e6c40495eaac8311f9420687b4 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 2892f3489ede1652fabf81fa849cd36ff2c28072..080c053f050e87835ba5caedc417c3252c38a56c 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 44296d12f2bac39231ac4826b2f1a68cbb688985..7e7a2dd8b4208bd1906cbf4d9673b5c0da4fa3ec 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 e3b9d3ffd7c6a6f9798c1f87ba35737fe5cfca6a..790ce69cf8f55474e0c266a51e162ff51a52be01 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 ae94266b4da7196efb4026e679d0e01787b5f98c..5f7d26b75726b73dd50bebe6fbfca4924b068985 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 dea6abd0c02b453fbb2014dccc75ce6dcdcb48d3..d38c1dce05db38e70f4317717e235d866a8dc1df 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 07f5f3408a30c00c83b18245a0b922f9b6dc00df..810f309f6632c80f3afdc3636b0bbaf918166be4 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,