diff --git a/paddle/fluid/framework/tensor_util.cc b/paddle/fluid/framework/tensor_util.cc index 0d8ed3c4eb118ec7f3b1a616c14c27990b302495..80f6d7a66eea0acca28f0d697d601da2c575e5b9 100644 --- a/paddle/fluid/framework/tensor_util.cc +++ b/paddle/fluid/framework/tensor_util.cc @@ -1205,8 +1205,8 @@ std::ostream& operator<<(std::ostream& os, const phi::DenseTensor& t) { << "\n"; #ifdef PADDLE_WITH_MKLDNN - os << " - format: " - << dnnl_fmt_tag2str(static_cast(t.format())) << "\n"; + os << " - memory desc: " + << (t.mem_desc()) << "\n"; #endif DenseTensor tensor; diff --git a/paddle/fluid/operators/mkldnn/mul_mkldnn_op.cc b/paddle/fluid/operators/mkldnn/mul_mkldnn_op.cc index 29329351de8d62463be32c7cd694ddcbcefcf3d6..092410c2b861581003765904d45d1a09923d1f56 100644 --- a/paddle/fluid/operators/mkldnn/mul_mkldnn_op.cc +++ b/paddle/fluid/operators/mkldnn/mul_mkldnn_op.cc @@ -203,15 +203,15 @@ class MulPrimitiveFactory { const ExecutionContext &ctx) { Tensor x_tmp; Tensor data_matrix; - MKLDNNMemoryFormat src_fmt = data->format(); MKLDNNMemoryFormat dst_fmt; - auto src_mdesc = CreateMemDescriptor(data, src_fmt); - - if ((data->dims().size() == 4 && - src_fmt != (dst_fmt = MKLDNNMemoryFormat::nchw)) || - (data->dims().size() == 5 && - src_fmt != (dst_fmt = MKLDNNMemoryFormat::ncdhw))) { - auto dst_mdesc = CreateMemDescriptor(data, dst_fmt); + // This code is enforcing plain (non-blocked) memory arrangement + // in order to flatten (reduce dimensionality) of Tensor later + auto src_mdesc = data->mem_desc(); + auto dst_mdesc = data->dims().size() >= 4 ? (data->dims().size() == 5 ? + CreateMemDescriptor(data, MKLDNNMemoryFormat::ncdhw) : + CreateMemDescriptor(data, MKLDNNMemoryFormat::nchw) ) : src_mdesc; + + if (src_mdesc != dst_mdesc) { x_tmp.mutable_data(ctx.GetPlace(), data->memory_size()); Reorder(src_mdesc, diff --git a/paddle/fluid/operators/mkldnn/requantize_mkldnn_op.cc b/paddle/fluid/operators/mkldnn/requantize_mkldnn_op.cc index 23409db02bec9064a324ad2c6fc779e0888bdf9a..eab3a1f9ca378e8da5c767e9b73c9743169bd606 100644 --- a/paddle/fluid/operators/mkldnn/requantize_mkldnn_op.cc +++ b/paddle/fluid/operators/mkldnn/requantize_mkldnn_op.cc @@ -85,15 +85,13 @@ class ReQuantOpKernel : public framework::OpKernel { const T* input_data = input->data(); if (reorder_p == nullptr) { - auto dst_tz = phi::vectorize(output->dims()); auto src_dt = framework::ToMKLDNNDataType( framework::TransToProtoVarType(input->dtype())); auto dst_dt = with_shift ? framework::MKLDNNDataType::u8 : src_dt; - auto src_md = platform::MKLDNNMemDesc({src_tz}, src_dt, input->format()); src_memory = std::make_shared( - src_md, engine, to_void_cast(input_data)); - auto dst_md = platform::MKLDNNMemDesc({dst_tz}, dst_dt, input->format()); + input->mem_desc(), engine, to_void_cast(input_data)); + auto dst_md = platform::MKLDNNMemDesc({src_tz}, dst_dt, input->mem_desc().strides); dnnl::primitive_attr attri; int mask = 0; diff --git a/paddle/fluid/operators/mkldnn/reshape_mkldnn_op.cc b/paddle/fluid/operators/mkldnn/reshape_mkldnn_op.cc index d38c1dce05db38e70f4317717e235d866a8dc1df..e405bf3c6fa8b8270e679a75e32dc8997a0bb683 100644 --- a/paddle/fluid/operators/mkldnn/reshape_mkldnn_op.cc +++ b/paddle/fluid/operators/mkldnn/reshape_mkldnn_op.cc @@ -83,7 +83,7 @@ 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( @@ -356,7 +356,7 @@ 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_dst_memory_p, diff --git a/paddle/fluid/operators/slice_op.cc b/paddle/fluid/operators/slice_op.cc index 44a9bd24032ab5bd8320c6c6044071283de89422..37fa72a5ef714b612f2b983e71f3c991f1534821 100644 --- a/paddle/fluid/operators/slice_op.cc +++ b/paddle/fluid/operators/slice_op.cc @@ -161,11 +161,7 @@ class SliceOp : public framework::OperatorWithKernel { // reorders, because if blocked dimension is not divisible by 8 or // 16(depending on which blocking format is used) submemory cannot be // created, so in that scenario a fallback is needed - auto tmp_md = dnnl::memory::desc( - phi::vectorize(ctx.Input("Input")->dims()), - dnnl::memory::data_type::f32, - ctx.Input("Input")->format()); - if (tmp_md.data.format_desc.blocking.inner_nblks == 0) + if (ctx.Input("Input")->mem_desc().data.format_desc.blocking.inner_nblks == 0) return framework::OpKernelType(input_data_type, ctx.GetPlace(), framework::DataLayout::kMKLDNN, @@ -336,13 +332,7 @@ class SliceOpGrad : public framework::OperatorWithKernel { // reorders, because if blocked dimension is not divisible by 8 or // 16(depending on which blocking format is used) submemory cannot be // created, so in that scenario a fallback is needed - auto tmp_md = dnnl::memory::desc( - phi::vectorize( - ctx.Input(framework::GradVarName("Out")) - ->dims()), - dnnl::memory::data_type::f32, - ctx.Input(framework::GradVarName("Out"))->format()); - if (tmp_md.data.format_desc.blocking.inner_nblks == 0) + if (ctx.Input(framework::GradVarName("Out"))->mem_desc().data.format_desc.blocking.inner_nblks == 0) return framework::OpKernelType(input_data_type, ctx.GetPlace(), framework::DataLayout::kMKLDNN, diff --git a/paddle/phi/core/dense_tensor.cc b/paddle/phi/core/dense_tensor.cc index 6c9291f816f7a14afba35a798cf2e5af926a1ab0..6c18562edeb6a5cda8b445ed9e5ebb2c5fea3fc8 100644 --- a/paddle/phi/core/dense_tensor.cc +++ b/paddle/phi/core/dense_tensor.cc @@ -56,7 +56,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 } @@ -66,7 +65,6 @@ DenseTensor& DenseTensor::operator=(const DenseTensor& other) { holder_ = other.holder_; inplace_version_counter_ = other.inplace_version_counter_; #ifdef PADDLE_WITH_MKLDNN - format_ = other.format_; mem_desc_ = other.mem_desc_; #endif return *this; @@ -77,7 +75,6 @@ DenseTensor& DenseTensor::operator=(DenseTensor&& other) { std::swap(holder_, other.holder_); 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 abf242acdb22a57267fdd7be9559b48e61623ed9..712ce125ceb68353f9361e13c4c29cdb258fa982 100644 --- a/paddle/phi/core/dense_tensor.h +++ b/paddle/phi/core/dense_tensor.h @@ -227,16 +227,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_impl.cc b/paddle/phi/core/dense_tensor_impl.cc index 0e42c1daf699a01281760193c1ad73fe26519f39..3ccc4d8a0f3daff7223f1426911ff73e2281eae7 100644 --- a/paddle/phi/core/dense_tensor_impl.cc +++ b/paddle/phi/core/dense_tensor_impl.cc @@ -365,7 +365,6 @@ DenseTensor& DenseTensor::ShareDataWith(const DenseTensor& src) { meta_.layout = src.meta_.layout; meta_.offset = src.meta_.offset; #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 94c0924e36e00bf60c5e0fbc4287c2321b458b80..11ef9d2ad23fa9cb0ec7cc9fc78989ca88bc63d1 100644 --- a/paddle/phi/kernels/onednn/split_kernel.cc +++ b/paddle/phi/kernels/onednn/split_kernel.cc @@ -49,7 +49,7 @@ 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], slice_mem_p->get_desc(), dev_ctx.GetPlace()); auto reorder_p = reorder_handler.AcquireReorder(reorder_dst_memory_p, slice_mem_p);