Skip to content

Commit b4a4977

Browse files
authored
[0-size Tensor No.169] Add 0-size Tensor support for paddle.nn.functional.fold [fluid_ops]
1 parent 2fe1203 commit b4a4977

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

python/paddle/nn/functional/common.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from __future__ import annotations
1616

17+
import math
1718
from typing import TYPE_CHECKING, Literal
1819

1920
import numpy
@@ -2753,6 +2754,9 @@ def fold(
27532754
)
27542755

27552756
assert len(x.shape) == 3, "input should be the format of [N, C, L]"
2757+
assert (
2758+
math.prod(x.shape) > 0
2759+
), "The number of elements must greater than zero."
27562760

27572761
def _is_list_or_tuple_(data):
27582762
return isinstance(data, (list, tuple))

test/legacy_test/test_fold_op.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,17 @@ def test_GT_0():
283283
strides=0,
284284
)
285285

286+
def test_zero_size():
287+
x = paddle.randn(shape=[0, 1, 1], dtype="float32")
288+
out = fold(
289+
x,
290+
output_sizes=[0, 0],
291+
kernel_sizes=[0, 0],
292+
dilations=0,
293+
paddings=[0, 0],
294+
strides=0,
295+
)
296+
286297
self.assertRaises(AssertionError, test_input_shape)
287298
self.assertRaises(AssertionError, test_kernel_shape)
288299
self.assertRaises(ValueError, test_padding_shape)
@@ -292,6 +303,7 @@ def test_GT_0():
292303
self.assertRaises(TypeError, test_output_size_2)
293304
self.assertRaises(ValueError, test_block_h_w)
294305
self.assertRaises(ValueError, test_GT_0)
306+
self.assertRaises(AssertionError, test_zero_size)
295307

296308

297309
if __name__ == '__main__':

0 commit comments

Comments
 (0)