From 4e72878b4147b769e48f0bc6ec74a9f5f57c89cd Mon Sep 17 00:00:00 2001 From: co63oc Date: Sat, 28 Jun 2025 16:36:56 +0800 Subject: [PATCH 1/3] Fix --- .../infer_symbolic_shape/binary_infer_sym.cc | 12 +++- paddle/phi/infermeta/binary.cc | 21 +++--- .../kernels/gpu/depthwise_conv_grad_kernel.cu | 13 ++++ .../phi/kernels/gpu/depthwise_conv_kernel.cu | 4 ++ paddle/phi/kernels/gpudnn/conv_grad_kernel.cu | 14 +++- paddle/phi/kernels/gpudnn/conv_kernel.cu | 4 ++ .../phi/kernels/impl/conv_grad_kernel_impl.h | 15 +++- paddle/phi/kernels/impl/conv_kernel_impl.h | 4 ++ paddle/phi/kernels/xpu/conv_grad_kernel.cc | 13 ++++ paddle/phi/kernels/xpu/conv_kernel.cc | 4 ++ test/legacy_test/test_functional_conv1d.py | 69 +++++++++++++++---- test/legacy_test/test_functional_conv2d.py | 54 +++++++++++++++ test/legacy_test/test_functional_conv3d.py | 54 +++++++++++++++ 13 files changed, 255 insertions(+), 26 deletions(-) diff --git a/paddle/fluid/pir/dialect/operator/interface/infer_symbolic_shape/binary_infer_sym.cc b/paddle/fluid/pir/dialect/operator/interface/infer_symbolic_shape/binary_infer_sym.cc index 2bb9befe1ce2c..0491ffb30e52c 100644 --- a/paddle/fluid/pir/dialect/operator/interface/infer_symbolic_shape/binary_infer_sym.cc +++ b/paddle/fluid/pir/dialect/operator/interface/infer_symbolic_shape/binary_infer_sym.cc @@ -414,7 +414,11 @@ bool Conv2dOpInferSymbolicShape(pir::Operation *op, const symbol::ShapeOrDataDimExprs &shape_data = [&] { std::vector out_s_or_d({in_s_or_d.shape()[0]}); if (!channel_last) { - out_s_or_d.push_back(filter_s_or_d.shape()[0]); + if (filter_s_or_d.shape()[1] == 0) { + out_s_or_d.push_back(symbol::DimExpr{0}); + } else { + out_s_or_d.push_back(filter_s_or_d.shape()[0]); + } } for (size_t i = 0; i < in_data_dims.size(); ++i) { @@ -427,7 +431,11 @@ bool Conv2dOpInferSymbolicShape(pir::Operation *op, out_s_or_d.push_back(output_size); } if (channel_last) { - out_s_or_d.push_back(filter_s_or_d.shape()[0]); + if (filter_s_or_d.shape()[1] == 0) { + out_s_or_d.push_back(symbol::DimExpr{0}); + } else { + out_s_or_d.push_back(filter_s_or_d.shape()[0]); + } } return symbol::ShapeOrDataDimExprs{ diff --git a/paddle/phi/infermeta/binary.cc b/paddle/phi/infermeta/binary.cc index f3392caa15595..766528e00b177 100644 --- a/paddle/phi/infermeta/binary.cc +++ b/paddle/phi/infermeta/binary.cc @@ -588,13 +588,6 @@ void ConvInferMeta(const MetaTensor& input, const bool channel_last = (config.is_run_mkldnn_kernel == false) && (data_format == "NHWC" || data_format == "NDHWC"); - for (int i = 0; i < 2; ++i) { - PADDLE_ENFORCE_NE(in_dims[i], - 0, - common::errors::InvalidArgument( - "The size of Op(Conv) inputs should not be 0.")); - } - PADDLE_ENFORCE_EQ( in_dims.size() == 4 || in_dims.size() == 5, true, @@ -706,11 +699,15 @@ void ConvInferMeta(const MetaTensor& input, std::vector output_shape({in_dims[0]}); if (!channel_last) { - output_shape.push_back(filter_dims[0]); + if (filter_dims[1] == 0) { + output_shape.push_back(0); + } else { + output_shape.push_back(filter_dims[0]); + } } for (int i = 0; i < in_data_dims.size(); ++i) { if ((!config.is_runtime) && - (in_data_dims[i] <= 0 || filter_dims[i + 2] <= 0)) { + (in_data_dims[i] < 0 || filter_dims[i + 2] < 0)) { output_shape.push_back(-1); } else { const int dkernel = @@ -723,7 +720,11 @@ void ConvInferMeta(const MetaTensor& input, } } if (channel_last) { - output_shape.push_back(filter_dims[0]); + if (filter_dims[1] == 0) { + output_shape.push_back(0); + } else { + output_shape.push_back(filter_dims[0]); + } } out->set_dims(common::make_ddim(output_shape)); diff --git a/paddle/phi/kernels/gpu/depthwise_conv_grad_kernel.cu b/paddle/phi/kernels/gpu/depthwise_conv_grad_kernel.cu index 2c3baea01c162..02c8625ab8966 100644 --- a/paddle/phi/kernels/gpu/depthwise_conv_grad_kernel.cu +++ b/paddle/phi/kernels/gpu/depthwise_conv_grad_kernel.cu @@ -18,6 +18,7 @@ #include "paddle/phi/common/float16.h" #include "paddle/phi/core/kernel_registry.h" #include "paddle/phi/kernels/cpu/conv_util.h" +#include "paddle/phi/kernels/full_kernel.h" #include "paddle/phi/kernels/funcs/batch_norm_utils.h" #include "paddle/phi/kernels/funcs/math_function.h" #include "paddle/phi/kernels/gpu/depthwise_conv.h" @@ -40,6 +41,18 @@ void DepthwiseConvGradKernel(const Context& dev_ctx, const DenseTensor* output_grad = &out_grad; if (!input_grad && !filter_grad) return; + // 0-size + if (input.numel() == 0) { + if (input_grad) dev_ctx.template Alloc(input_grad); + if (filter_grad) { + phi::Full( + dev_ctx, + phi::IntArray(common::vectorize(filter_grad->dims())), + 0, + filter_grad); + } + return; + } bool has_fuse_relu = dev_ctx.HasDnnAttr("fuse_relu_before_depthwise_conv"); bool fuse_relu = diff --git a/paddle/phi/kernels/gpu/depthwise_conv_kernel.cu b/paddle/phi/kernels/gpu/depthwise_conv_kernel.cu index deabeca836117..7c86b6ac596a6 100644 --- a/paddle/phi/kernels/gpu/depthwise_conv_kernel.cu +++ b/paddle/phi/kernels/gpu/depthwise_conv_kernel.cu @@ -33,6 +33,10 @@ void DepthwiseConvKernel(const Context& dev_ctx, const std::vector& dilations_t, const std::string& data_format, DenseTensor* out) { + if (input.numel() == 0) { + dev_ctx.template Alloc(out); + return; + } DenseTensor* output = out; dev_ctx.template Alloc(output); diff --git a/paddle/phi/kernels/gpudnn/conv_grad_kernel.cu b/paddle/phi/kernels/gpudnn/conv_grad_kernel.cu index f6a43f198d5fd..1cb7c205844da 100644 --- a/paddle/phi/kernels/gpudnn/conv_grad_kernel.cu +++ b/paddle/phi/kernels/gpudnn/conv_grad_kernel.cu @@ -31,10 +31,10 @@ #include "paddle/phi/common/bfloat16.h" #include "paddle/phi/common/float16.h" #include "paddle/phi/kernels/cpu/conv_util.h" +#include "paddle/phi/kernels/full_kernel.h" #include "paddle/phi/kernels/funcs/batch_norm_utils.h" #include "paddle/phi/kernels/funcs/padding.h" #include "paddle/phi/kernels/impl/conv_cudnn_impl.h" - #ifdef PADDLE_WITH_CUDNN_FRONTEND // clang-format off #include "paddle/phi/backends/dynload/cudnn_frontend.h" @@ -416,6 +416,18 @@ void ConvCudnnGradKernel(const Context& dev_ctx, const std::string& data_format, DenseTensor* input_grad, DenseTensor* filter_grad) { + // 0-size + if (input.numel() == 0) { + if (input_grad) dev_ctx.template Alloc(input_grad); + if (filter_grad) { + phi::Full( + dev_ctx, + phi::IntArray(common::vectorize(filter_grad->dims())), + 0, + filter_grad); + } + return; + } if (input_grad) { dev_ctx.template Alloc(input_grad); } diff --git a/paddle/phi/kernels/gpudnn/conv_kernel.cu b/paddle/phi/kernels/gpudnn/conv_kernel.cu index 96bc014662c55..998360dcb8aba 100644 --- a/paddle/phi/kernels/gpudnn/conv_kernel.cu +++ b/paddle/phi/kernels/gpudnn/conv_kernel.cu @@ -313,6 +313,10 @@ void ConvCudnnKernel(const Context& dev_ctx, int groups, const std::string& data_format, DenseTensor* output) { + if (input.numel() == 0) { + dev_ctx.template Alloc(output); + return; + } dev_ctx.template Alloc(output); std::vector paddings = paddings_t; std::vector dilations = dilations_t; diff --git a/paddle/phi/kernels/impl/conv_grad_kernel_impl.h b/paddle/phi/kernels/impl/conv_grad_kernel_impl.h index 3baf3fd84b0c4..6066720ab0793 100644 --- a/paddle/phi/kernels/impl/conv_grad_kernel_impl.h +++ b/paddle/phi/kernels/impl/conv_grad_kernel_impl.h @@ -15,12 +15,12 @@ #pragma once #include "paddle/phi/kernels/cpu/conv_util.h" +#include "paddle/phi/kernels/full_kernel.h" #include "paddle/phi/kernels/funcs/batch_norm_utils.h" #include "paddle/phi/kernels/funcs/blas/blas.h" #include "paddle/phi/kernels/funcs/im2col.h" #include "paddle/phi/kernels/funcs/math_function.h" #include "paddle/phi/kernels/funcs/vol2col.h" - namespace phi { template @@ -45,6 +45,19 @@ void ConvGradKernel(const Context& dev_ctx, std::vector dilations = dilations_t; DenseTensor filter = filter_t; + // 0-size + if (input.numel() == 0) { + if (input_grad) dev_ctx.template Alloc(input_grad); + if (filter_grad) { + phi::Full( + dev_ctx, + phi::IntArray(common::vectorize(filter_grad->dims())), + 0, + filter_grad); + } + return; + } + const bool channel_last = (data_format == "NHWC" || data_format == "NDHWC"); DenseTensor transformed_input(input.type()); diff --git a/paddle/phi/kernels/impl/conv_kernel_impl.h b/paddle/phi/kernels/impl/conv_kernel_impl.h index e40ba59a2d3a1..1427a3f6cad0a 100644 --- a/paddle/phi/kernels/impl/conv_kernel_impl.h +++ b/paddle/phi/kernels/impl/conv_kernel_impl.h @@ -38,6 +38,10 @@ void ConvKernelImpl(const Context& dev_ctx, std::vector paddings = paddings_t; std::vector dilations = dilations_t; DenseTensor filter = filter_t; + if (input.numel() == 0) { + dev_ctx.template Alloc(output); + return; + } // The filter will be reshaped in the calculations, // so here use an assignment operation, // that avoids modifying the variable in the Scope. diff --git a/paddle/phi/kernels/xpu/conv_grad_kernel.cc b/paddle/phi/kernels/xpu/conv_grad_kernel.cc index ff5a327ff3fe9..e54f22a28dce7 100644 --- a/paddle/phi/kernels/xpu/conv_grad_kernel.cc +++ b/paddle/phi/kernels/xpu/conv_grad_kernel.cc @@ -17,6 +17,7 @@ #include "paddle/phi/backends/xpu/enforce_xpu.h" #include "paddle/phi/core/kernel_registry.h" #include "paddle/phi/kernels/cpu/conv_util.h" +#include "paddle/phi/kernels/full_kernel.h" #include "paddle/phi/kernels/xpu/conv_utils_xpu.h" #include "paddle/phi/kernels/xpu/xpu_api_wrapper.h" #ifdef PADDLE_WITH_XPU_XRE5 @@ -47,6 +48,18 @@ void ConvGradKernel(const Context& dev_ctx, // so here use an assignment operation, // that avoids modifying the variable in the Scope. if (!input_grad && !filter_grad) return; + // 0-size + if (input.numel() == 0) { + if (input_grad) dev_ctx.template Alloc(input_grad); + if (filter_grad) { + phi::Full( + dev_ctx, + phi::IntArray(common::vectorize(filter_grad->dims())), + 0, + filter_grad); + } + return; + } PADDLE_ENFORCE_EQ( data_format == "NDHWC", false, diff --git a/paddle/phi/kernels/xpu/conv_kernel.cc b/paddle/phi/kernels/xpu/conv_kernel.cc index c51dd9b2eeba9..5b70b105dd3c5 100644 --- a/paddle/phi/kernels/xpu/conv_kernel.cc +++ b/paddle/phi/kernels/xpu/conv_kernel.cc @@ -37,6 +37,10 @@ void ConvKernel(const Context& dev_ctx, int groups, const std::string& data_format, DenseTensor* out) { + if (input.numel() == 0) { + dev_ctx.template Alloc(out); + return; + } using XPUType = typename XPUTypeTrait::Type; std::vector paddings(paddings_t.begin(), paddings_t.end()); std::vector dilations(dilations_t.begin(), dilations_t.end()); diff --git a/test/legacy_test/test_functional_conv1d.py b/test/legacy_test/test_functional_conv1d.py index 760e1a2f2bc21..645c24017ac8d 100644 --- a/test/legacy_test/test_functional_conv1d.py +++ b/test/legacy_test/test_functional_conv1d.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import os import unittest from unittest import TestCase @@ -20,6 +21,7 @@ import paddle import paddle.base.dygraph as dg import paddle.nn.functional as F +from paddle import base class TestFunctionalConv1DError(TestCase): @@ -70,18 +72,6 @@ def setUp(self): self.data_format = "NCL" -class TestFunctionalConv1DErrorCase2(TestFunctionalConv1DError): - def setUp(self): - self.input = np.random.randn(0, 0, 0) - self.filter = np.random.randn(1, 0, 0) - self.bias = None - self.padding = 0 - self.stride = 1 - self.dilation = 1 - self.groups = 1 - self.data_format = "NCL" - - class TestFunctionalConv1D_CPU_FP16(TestCase): def setUp(self): self.padding = 0 @@ -108,5 +98,60 @@ def test_cpu_fp16(self): np.testing.assert_allclose(y.numpy(), [[[2]]]) +class TestFunctionalConv1D_ZeroSize(TestCase): + def init_data(self): + self.input = np.random.randn(0, 1, 2) + self.filter = np.random.randn(1, 1, 2) + self.np_out = np.random.random([0, 1, 1]) + + def setUp(self): + self.init_data() + self.bias = None + self.padding = 0 + self.stride = 1 + self.dilation = 1 + self.groups = 1 + self.data_format = "NCL" + self.places = [] + if ( + os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() + in ['1', 'true', 'on'] + or not base.core.is_compiled_with_cuda() + ): + self.places.append(base.CPUPlace()) + if base.core.is_compiled_with_cuda(): + self.places.append(base.CUDAPlace(0)) + + def test_dygraph(self): + for place in self.places: + with dg.guard(place): + input = paddle.to_tensor(self.input) + input.stop_gradient = False + filter = paddle.to_tensor(self.filter) + filter.stop_gradient = False + y = F.conv1d( + input, + filter, + self.bias, + padding=self.padding, + stride=self.stride, + dilation=self.dilation, + groups=self.groups, + data_format=self.data_format, + ) + np.testing.assert_allclose(y.numpy(), self.np_out) + loss = y.sum() + loss.backward() + np.testing.assert_allclose(input.grad.shape, input.shape) + np.testing.assert_allclose(filter.grad, np.zeros(filter.shape)) + + +class TestFunctionalConv1D_ZeroSize2(TestFunctionalConv1D_ZeroSize): + def init_data(self): + self.input = np.random.randn(0, 0, 2) + self.filter = np.random.randn(1, 0, 2) + self.np_out = np.random.random([0, 0, 1]) + + if __name__ == "__main__": unittest.main() diff --git a/test/legacy_test/test_functional_conv2d.py b/test/legacy_test/test_functional_conv2d.py index 1b69e2cc15035..bdaea7ea8f65c 100644 --- a/test/legacy_test/test_functional_conv2d.py +++ b/test/legacy_test/test_functional_conv2d.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import os import unittest from unittest import TestCase @@ -282,6 +283,59 @@ def setUp(self): self.data_format = "NCHW" +class TestFunctionalConv2D_ZeroSize(TestCase): + def init_data(self): + self.input = np.random.random([0, 3, 4, 4]) + self.filter = np.random.random([2, 3, 3, 3]) + self.np_out = np.random.random([0, 2, 2, 2]) + + def setUp(self): + self.init_data() + self.bias = None + self.padding = 0 + self.stride = 1 + self.dilation = 1 + self.groups = 1 + self.data_format = "NCHW" + self.places = [] + if ( + os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() + in ['1', 'true', 'on'] + or not base.core.is_compiled_with_cuda() + ): + self.places.append(base.CPUPlace()) + if base.core.is_compiled_with_cuda(): + self.places.append(base.CUDAPlace(0)) + + def test_dygraph(self): + for place in self.places: + with dg.guard(place): + input = paddle.to_tensor(self.input) + input.stop_gradient = False + filter = paddle.to_tensor(self.filter) + y = F.conv2d( + input, + filter, + self.bias, + padding=self.padding, + stride=self.stride, + dilation=self.dilation, + groups=self.groups, + data_format=self.data_format, + ) + np.testing.assert_allclose(y.numpy(), self.np_out) + loss = y.sum() + loss.backward() + np.testing.assert_allclose(input.grad.shape, input.shape) + + +class TestFunctionalConv2D_ZeroSize2(TestFunctionalConv2D_ZeroSize): + def init_data(self): + self.input = np.random.random([0, 0, 4, 4]) + self.filter = np.random.random([2, 0, 3, 3]) + self.np_out = np.random.random([0, 0, 2, 2]) + + if __name__ == "__main__": paddle.enable_static() unittest.main() diff --git a/test/legacy_test/test_functional_conv3d.py b/test/legacy_test/test_functional_conv3d.py index 2b61cf8570178..b6179c96928a3 100644 --- a/test/legacy_test/test_functional_conv3d.py +++ b/test/legacy_test/test_functional_conv3d.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import os import unittest from unittest import TestCase @@ -263,6 +264,59 @@ def setUp(self): self.data_format = "NCDHW" +class TestFunctionalConv3D_ZeroSize(TestCase): + def init_data(self): + self.input = np.random.random([4, 3, 0, 8, 8]) + self.filter = np.random.random([5, 3, 3, 3, 3]) + self.np_out = np.random.random([4, 5, 0, 8, 8]) + + def setUp(self): + self.init_data() + self.bias = None + self.padding = 1 + self.stride = 1 + self.dilation = 1 + self.groups = 1 + self.data_format = "NCDHW" + self.places = [] + if ( + os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() + in ['1', 'true', 'on'] + or not base.core.is_compiled_with_cuda() + ): + self.places.append(base.CPUPlace()) + if base.core.is_compiled_with_cuda(): + self.places.append(base.CUDAPlace(0)) + + def test_dygraph(self): + for place in self.places: + with dg.guard(place): + input = paddle.to_tensor(self.input) + input.stop_gradient = False + filter = paddle.to_tensor(self.filter) + y = F.conv3d( + input, + filter, + self.bias, + padding=self.padding, + stride=self.stride, + dilation=self.dilation, + groups=self.groups, + data_format=self.data_format, + ) + np.testing.assert_allclose(y.numpy(), self.np_out) + loss = y.sum() + loss.backward() + np.testing.assert_allclose(input.grad.shape, input.shape) + + +class TestFunctionalConv3D_ZeroSize2(TestFunctionalConv3D_ZeroSize): + def init_data(self): + self.input = np.random.random([4, 0, 0, 8, 8]) + self.filter = np.random.random([5, 0, 3, 3, 3]) + self.np_out = np.random.random([4, 0, 0, 8, 8]) + + if __name__ == "__main__": paddle.enable_static() unittest.main() From c301dc71f2b91933d05340c9af39ddd59d86b45c Mon Sep 17 00:00:00 2001 From: co63oc Date: Sat, 28 Jun 2025 17:11:06 +0800 Subject: [PATCH 2/3] Fix --- paddle/phi/kernels/gpu/depthwise_conv_kernel.cu | 4 +++- paddle/phi/kernels/gpudnn/conv_kernel.cu | 7 ++++--- paddle/phi/kernels/impl/conv_kernel_impl.h | 4 +++- paddle/phi/kernels/xpu/conv_kernel.cc | 4 +++- test/legacy_test/test_functional_conv1d.py | 4 ++-- test/legacy_test/test_functional_conv2d.py | 4 ++-- test/legacy_test/test_functional_conv3d.py | 4 ++-- 7 files changed, 19 insertions(+), 12 deletions(-) diff --git a/paddle/phi/kernels/gpu/depthwise_conv_kernel.cu b/paddle/phi/kernels/gpu/depthwise_conv_kernel.cu index 7c86b6ac596a6..267bf08cee532 100644 --- a/paddle/phi/kernels/gpu/depthwise_conv_kernel.cu +++ b/paddle/phi/kernels/gpu/depthwise_conv_kernel.cu @@ -17,6 +17,7 @@ #include "paddle/phi/common/float16.h" #include "paddle/phi/core/kernel_registry.h" #include "paddle/phi/kernels/cpu/conv_util.h" +#include "paddle/phi/kernels/full_kernel.h" #include "paddle/phi/kernels/funcs/batch_norm_utils.h" #include "paddle/phi/kernels/gpu/depthwise_conv.h" @@ -34,7 +35,8 @@ void DepthwiseConvKernel(const Context& dev_ctx, const std::string& data_format, DenseTensor* out) { if (input.numel() == 0) { - dev_ctx.template Alloc(out); + phi::Full( + dev_ctx, phi::IntArray(common::vectorize(out->dims())), 0, out); return; } DenseTensor* output = out; diff --git a/paddle/phi/kernels/gpudnn/conv_kernel.cu b/paddle/phi/kernels/gpudnn/conv_kernel.cu index 998360dcb8aba..fe79925cfbce9 100644 --- a/paddle/phi/kernels/gpudnn/conv_kernel.cu +++ b/paddle/phi/kernels/gpudnn/conv_kernel.cu @@ -13,9 +13,9 @@ // limitations under the License. #include "paddle/phi/kernels/conv_kernel.h" -#include "paddle/phi/kernels/gpudnn/conv_gpudnn.h" - #include "glog/logging.h" +#include "paddle/phi/kernels/full_kernel.h" +#include "paddle/phi/kernels/gpudnn/conv_gpudnn.h" #include "paddle/phi/backends/context_pool.h" #include "paddle/phi/backends/gpu/gpu_context.h" @@ -314,7 +314,8 @@ void ConvCudnnKernel(const Context& dev_ctx, const std::string& data_format, DenseTensor* output) { if (input.numel() == 0) { - dev_ctx.template Alloc(output); + phi::Full( + dev_ctx, phi::IntArray(common::vectorize(output->dims())), 0, output); return; } dev_ctx.template Alloc(output); diff --git a/paddle/phi/kernels/impl/conv_kernel_impl.h b/paddle/phi/kernels/impl/conv_kernel_impl.h index 1427a3f6cad0a..ac44f760c8af6 100644 --- a/paddle/phi/kernels/impl/conv_kernel_impl.h +++ b/paddle/phi/kernels/impl/conv_kernel_impl.h @@ -16,6 +16,7 @@ #include "paddle/phi/kernels/conv_kernel.h" #include "paddle/phi/kernels/cpu/conv_util.h" +#include "paddle/phi/kernels/full_kernel.h" #include "paddle/phi/kernels/funcs/batch_norm_utils.h" #include "paddle/phi/kernels/funcs/blas/blas.h" #include "paddle/phi/kernels/funcs/im2col.h" @@ -39,7 +40,8 @@ void ConvKernelImpl(const Context& dev_ctx, std::vector dilations = dilations_t; DenseTensor filter = filter_t; if (input.numel() == 0) { - dev_ctx.template Alloc(output); + phi::Full( + dev_ctx, phi::IntArray(common::vectorize(output->dims())), 0, output); return; } // The filter will be reshaped in the calculations, diff --git a/paddle/phi/kernels/xpu/conv_kernel.cc b/paddle/phi/kernels/xpu/conv_kernel.cc index 5b70b105dd3c5..7fd5c37c9b729 100644 --- a/paddle/phi/kernels/xpu/conv_kernel.cc +++ b/paddle/phi/kernels/xpu/conv_kernel.cc @@ -17,6 +17,7 @@ #include "paddle/phi/backends/xpu/enforce_xpu.h" #include "paddle/phi/core/kernel_registry.h" #include "paddle/phi/kernels/cpu/conv_util.h" +#include "paddle/phi/kernels/full_kernel.h" #include "paddle/phi/kernels/xpu/conv_utils_xpu.h" #include "paddle/phi/kernels/xpu/xpu_api_wrapper.h" #ifdef PADDLE_WITH_XPU_XRE5 @@ -38,7 +39,8 @@ void ConvKernel(const Context& dev_ctx, const std::string& data_format, DenseTensor* out) { if (input.numel() == 0) { - dev_ctx.template Alloc(out); + phi::Full( + dev_ctx, phi::IntArray(common::vectorize(out->dims())), 0, out); return; } using XPUType = typename XPUTypeTrait::Type; diff --git a/test/legacy_test/test_functional_conv1d.py b/test/legacy_test/test_functional_conv1d.py index 645c24017ac8d..f5490e93da7f7 100644 --- a/test/legacy_test/test_functional_conv1d.py +++ b/test/legacy_test/test_functional_conv1d.py @@ -102,7 +102,7 @@ class TestFunctionalConv1D_ZeroSize(TestCase): def init_data(self): self.input = np.random.randn(0, 1, 2) self.filter = np.random.randn(1, 1, 2) - self.np_out = np.random.random([0, 1, 1]) + self.np_out = np.zeros([0, 1, 1]) def setUp(self): self.init_data() @@ -150,7 +150,7 @@ class TestFunctionalConv1D_ZeroSize2(TestFunctionalConv1D_ZeroSize): def init_data(self): self.input = np.random.randn(0, 0, 2) self.filter = np.random.randn(1, 0, 2) - self.np_out = np.random.random([0, 0, 1]) + self.np_out = np.zeros([0, 0, 1]) if __name__ == "__main__": diff --git a/test/legacy_test/test_functional_conv2d.py b/test/legacy_test/test_functional_conv2d.py index bdaea7ea8f65c..97bf245ec281d 100644 --- a/test/legacy_test/test_functional_conv2d.py +++ b/test/legacy_test/test_functional_conv2d.py @@ -287,7 +287,7 @@ class TestFunctionalConv2D_ZeroSize(TestCase): def init_data(self): self.input = np.random.random([0, 3, 4, 4]) self.filter = np.random.random([2, 3, 3, 3]) - self.np_out = np.random.random([0, 2, 2, 2]) + self.np_out = np.zeros([0, 2, 2, 2]) def setUp(self): self.init_data() @@ -333,7 +333,7 @@ class TestFunctionalConv2D_ZeroSize2(TestFunctionalConv2D_ZeroSize): def init_data(self): self.input = np.random.random([0, 0, 4, 4]) self.filter = np.random.random([2, 0, 3, 3]) - self.np_out = np.random.random([0, 0, 2, 2]) + self.np_out = np.zeros([0, 0, 2, 2]) if __name__ == "__main__": diff --git a/test/legacy_test/test_functional_conv3d.py b/test/legacy_test/test_functional_conv3d.py index b6179c96928a3..aea661586feb1 100644 --- a/test/legacy_test/test_functional_conv3d.py +++ b/test/legacy_test/test_functional_conv3d.py @@ -268,7 +268,7 @@ class TestFunctionalConv3D_ZeroSize(TestCase): def init_data(self): self.input = np.random.random([4, 3, 0, 8, 8]) self.filter = np.random.random([5, 3, 3, 3, 3]) - self.np_out = np.random.random([4, 5, 0, 8, 8]) + self.np_out = np.zeros([4, 5, 0, 8, 8]) def setUp(self): self.init_data() @@ -314,7 +314,7 @@ class TestFunctionalConv3D_ZeroSize2(TestFunctionalConv3D_ZeroSize): def init_data(self): self.input = np.random.random([4, 0, 0, 8, 8]) self.filter = np.random.random([5, 0, 3, 3, 3]) - self.np_out = np.random.random([4, 0, 0, 8, 8]) + self.np_out = np.zeros([4, 0, 0, 8, 8]) if __name__ == "__main__": From 8fb677bc3c9678fb9ef31044f9ba624616a3ee06 Mon Sep 17 00:00:00 2001 From: co63oc Date: Sat, 28 Jun 2025 21:20:23 +0800 Subject: [PATCH 3/3] Fix --- test/legacy_test/test_functional_conv2d.py | 38 +++------------------- test/legacy_test/test_functional_conv3d.py | 38 +++------------------- 2 files changed, 10 insertions(+), 66 deletions(-) diff --git a/test/legacy_test/test_functional_conv2d.py b/test/legacy_test/test_functional_conv2d.py index 97bf245ec281d..e9fd4f46ff57b 100644 --- a/test/legacy_test/test_functional_conv2d.py +++ b/test/legacy_test/test_functional_conv2d.py @@ -219,15 +219,15 @@ def setUp(self): class TestFunctionalConv2DErrorCase12(TestCase): def setUp(self): - self.input = np.array([]) - self.filter = np.array([]) - self.num_filters = 0 - self.filter_size = 0 + self.input = np.random.randn(1, 3, 3, 3) + self.filter = np.random.randn(3, 3, 1, 1) + self.num_filters = 3 + self.filter_size = 1 self.bias = None self.padding = 0 self.stride = 1 self.dilation = 1 - self.groups = 1 + self.groups = 0 self.data_format = "NCHW" def dygraph_case(self): @@ -255,34 +255,6 @@ def test_dygraph_exception(self): self.dygraph_case() -class TestFunctionalConv2DErrorCase13(TestFunctionalConv2DErrorCase12): - def setUp(self): - self.input = np.random.randn(1, 3, 3, 3) - self.filter = np.random.randn(3, 3, 1, 1) - self.num_filters = 3 - self.filter_size = 1 - self.bias = None - self.padding = 0 - self.stride = 1 - self.dilation = 1 - self.groups = 0 - self.data_format = "NCHW" - - -class TestFunctionalConv2DErrorCase14(TestFunctionalConv2DErrorCase12): - def setUp(self): - self.input = np.random.randn(0, 0, 0, 0) - self.filter = np.random.randn(1, 0, 0, 0) - self.num_filters = 0 - self.filter_size = 0 - self.bias = None - self.padding = 0 - self.stride = 1 - self.dilation = 1 - self.groups = 1 - self.data_format = "NCHW" - - class TestFunctionalConv2D_ZeroSize(TestCase): def init_data(self): self.input = np.random.random([0, 3, 4, 4]) diff --git a/test/legacy_test/test_functional_conv3d.py b/test/legacy_test/test_functional_conv3d.py index aea661586feb1..2145ea4a7d063 100644 --- a/test/legacy_test/test_functional_conv3d.py +++ b/test/legacy_test/test_functional_conv3d.py @@ -200,15 +200,15 @@ def setUp(self): class TestFunctionalConv3DErrorCase11(TestCase): def setUp(self): - self.input = np.array([]) - self.filter = np.array([]) - self.num_filters = 0 - self.filter_size = 0 + self.input = np.random.randn(1, 3, 3, 3, 3) + self.filter = np.random.randn(3, 3, 1, 1, 1) + self.num_filters = 3 + self.filter_size = 1 self.bias = None self.padding = 0 self.stride = 1 self.dilation = 1 - self.groups = 1 + self.groups = 0 self.data_format = "NCDHW" def dygraph_case(self): @@ -236,34 +236,6 @@ def test_dygraph_exception(self): self.dygraph_case() -class TestFunctionalConv3DErrorCase12(TestFunctionalConv3DErrorCase11): - def setUp(self): - self.input = np.random.randn(1, 3, 3, 3, 3) - self.filter = np.random.randn(3, 3, 1, 1, 1) - self.num_filters = 3 - self.filter_size = 1 - self.bias = None - self.padding = 0 - self.stride = 1 - self.dilation = 1 - self.groups = 0 - self.data_format = "NCDHW" - - -class TestFunctionalConv3DErrorCase13(TestFunctionalConv3DErrorCase11): - def setUp(self): - self.input = np.random.randn(0, 0, 0, 0, 0) - self.filter = np.random.randn(1, 0, 0, 0, 0) - self.num_filters = 1 - self.filter_size = 1 - self.bias = None - self.padding = 0 - self.stride = 1 - self.dilation = 1 - self.groups = 1 - self.data_format = "NCDHW" - - class TestFunctionalConv3D_ZeroSize(TestCase): def init_data(self): self.input = np.random.random([4, 3, 0, 8, 8])