From 56e97aa47ac08386c1b246096da87c048153c166 Mon Sep 17 00:00:00 2001 From: Wang Huan Date: Wed, 10 Jan 2024 03:28:54 +0000 Subject: [PATCH 1/2] fix uncontiguous tensor resize bug --- paddle/phi/kernels/stride/as_strided_kernel.cc | 8 +++++--- paddle/phi/kernels/stride/tensor_unfold_kernel.cc | 9 +++++---- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/paddle/phi/kernels/stride/as_strided_kernel.cc b/paddle/phi/kernels/stride/as_strided_kernel.cc index 0cd63577d1e96..28ea8f4e63842 100644 --- a/paddle/phi/kernels/stride/as_strided_kernel.cc +++ b/paddle/phi/kernels/stride/as_strided_kernel.cc @@ -24,9 +24,11 @@ void AsStridedKernel(const Context& dev_ctx, const std::vector& stride, int64_t offset, DenseTensor* out) { - out->Resize(DDim(dims.data(), static_cast(dims.size()))); - out->set_strides(DDim(stride.data(), static_cast(stride.size()))); - out->set_offset(offset); + auto meta = out->meta(); + meta.dims = DDim(dims.data(), static_cast(dims.size())); + meta.strides = DDim(stride.data(), static_cast(stride.size())); + meta.offset = offset; + out->set_meta(meta); out->ResetHolder(input.Holder()); out->ShareInplaceVersionCounterWith(input); } diff --git a/paddle/phi/kernels/stride/tensor_unfold_kernel.cc b/paddle/phi/kernels/stride/tensor_unfold_kernel.cc index 0a4d4ee9af3e2..3aaa29ec7c038 100644 --- a/paddle/phi/kernels/stride/tensor_unfold_kernel.cc +++ b/paddle/phi/kernels/stride/tensor_unfold_kernel.cc @@ -61,10 +61,11 @@ void TensorUnfoldKernel(const Context& dev_ctx, } } - out->Resize(DDim(shape.data(), static_cast(shape.size()))); - out->set_strides(DDim(stride.data(), static_cast(stride.size()))); - out->set_offset(input.offset()); - out->ResetHolder(input.Holder()); + auto meta = out->meta(); + meta.dims = DDim(shape.data(), static_cast(shape.size())); + meta.strides = DDim(stride.data(), static_cast(stride.size())); + meta.offset = input.offset(); + out->set_meta(meta); out->ShareInplaceVersionCounterWith(input); } From 22c64de7a5963301fba28f6dc4ef07db3f91c0f1 Mon Sep 17 00:00:00 2001 From: Wang Huan Date: Wed, 10 Jan 2024 04:44:22 +0000 Subject: [PATCH 2/2] refine --- paddle/phi/kernels/stride/tensor_unfold_kernel.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/paddle/phi/kernels/stride/tensor_unfold_kernel.cc b/paddle/phi/kernels/stride/tensor_unfold_kernel.cc index 3aaa29ec7c038..79643ac3dc514 100644 --- a/paddle/phi/kernels/stride/tensor_unfold_kernel.cc +++ b/paddle/phi/kernels/stride/tensor_unfold_kernel.cc @@ -66,6 +66,7 @@ void TensorUnfoldKernel(const Context& dev_ctx, meta.strides = DDim(stride.data(), static_cast(stride.size())); meta.offset = input.offset(); out->set_meta(meta); + out->ResetHolder(input.Holder()); out->ShareInplaceVersionCounterWith(input); }