From f1cebfefddd3bf2dd0fc51fca1746bf0cbfbcc49 Mon Sep 17 00:00:00 2001 From: Jacek Czaja Date: Tue, 4 Oct 2022 17:00:08 +0200 Subject: [PATCH] - more fixes --- paddle/fluid/framework/tensor_util.cc | 4 ++-- paddle/fluid/operators/mkldnn/mul_mkldnn_op.cc | 16 ++++++++-------- .../operators/mkldnn/requantize_mkldnn_op.cc | 6 ++---- .../fluid/operators/mkldnn/reshape_mkldnn_op.cc | 4 ++-- paddle/fluid/operators/slice_op.cc | 14 ++------------ paddle/phi/core/dense_tensor.cc | 3 --- paddle/phi/core/dense_tensor.h | 10 ---------- paddle/phi/core/dense_tensor_impl.cc | 1 - paddle/phi/kernels/onednn/split_kernel.cc | 2 +- 9 files changed, 17 insertions(+), 43 deletions(-) diff --git a/paddle/fluid/framework/tensor_util.cc b/paddle/fluid/framework/tensor_util.cc index 0d8ed3c4eb1..80f6d7a66ee 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 29329351de8..092410c2b86 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 23409db02be..eab3a1f9ca3 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 d38c1dce05d..e405bf3c6fa 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 44a9bd24032..37fa72a5ef7 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 6c9291f816f..6c18562edeb 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 abf242acdb2..712ce125ceb 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 0e42c1daf69..3ccc4d8a0f3 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 94c0924e36e..11ef9d2ad23 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); -- GitLab