From 8e226f40972f73d8f8ccca7c63f4cdbf5361afe4 Mon Sep 17 00:00:00 2001 From: ooooo <3164076421@qq.com> Date: Sat, 19 Jul 2025 07:23:57 +0800 Subject: [PATCH 1/9] refine full and full_like for fill_value check and type annotations --- python/paddle/tensor/creation.py | 29 ++++++++++++++++++++++----- test/legacy_test/test_full_like_op.py | 24 ++++++++++++++++++++-- test/legacy_test/test_full_op.py | 20 ++++++++++++++++++ 3 files changed, 66 insertions(+), 7 deletions(-) diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index cb9b300b6d624f..f88ee17e466358 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -1043,7 +1043,7 @@ def get_slice( def full_like( x: paddle.Tensor, - fill_value: bool | float, + fill_value: bool | builtins.complex, dtype: DTypeLike | None = None, name: str | None = None, ) -> paddle.Tensor: @@ -1054,9 +1054,9 @@ def full_like( Args: x(Tensor): The input tensor which specifies shape and data type. The data type can be bool, float16, float32, float64, int32, int64. - fill_value(bool|float|int): The value to fill the tensor with. Note: this value shouldn't exceed the range of the output data type. + fill_value(bool|float|int|complex): The value to fill the tensor with. Note: this value shouldn't exceed the range of the output data type. dtype(np.dtype|str, optional): The data type of output. The data type can be one - of bool, float16, float32, float64, int32, int64. The default value is None, which means the output + of bool, float16, float32, float64, int32, int64, complex64, complex128. The default value is None, which means the output data type is the same as input. name(str|None, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. @@ -1074,6 +1074,10 @@ def full_like( [[2. 2. 2.] [2. 2. 2.]] """ + if not isinstance(fill_value, (bool, int, float, builtins.complex)): + raise TypeError( + f"fill_value should be bool, int, float or complex, but received {type(fill_value)}." + ) if dtype is None: dtype = x.dtype @@ -1516,7 +1520,7 @@ def _check_attr(attr, message): def full( shape: ShapeLike, - fill_value: bool | float | paddle.Tensor, + fill_value: bool | builtins.complex | paddle.Tensor, dtype: DTypeLike | None = None, name: str | None = None, ) -> paddle.Tensor: @@ -1528,7 +1532,7 @@ def full( shape (tuple|list|Tensor): Shape of the Tensor to be created. The data type is ``int32`` or ``int64`` . If ``shape`` is a list or tuple, each element of it should be integer or 0-D Tensor with shape []. If ``shape`` is an Tensor, it should be an 1-D Tensor which represents a list. - fill_value(bool|float|int|Tensor): The constant value used to initialize the Tensor to be created. + fill_value(bool|float|int|complex|Tensor): The constant value used to initialize the Tensor to be created. If ``fill_value`` is an Tensor, it should be an 0-D Tensor which represents a scalar. dtype(np.dtype|str, optional): Data type of the output Tensor which can be float16, float32, float64, int32, int64, if dtype is `None`, the data @@ -1574,6 +1578,21 @@ def full( [2. 2.] [2. 2.]] """ + if not isinstance( + fill_value, + ( + int, + float, + bool, + builtins.complex, + core.eager.Tensor, + Variable, + paddle.pir.Value, + ), + ): + raise TypeError( + f"The fill_value must be one of [int, float, bool, complex, core.eager.Tensor,Variable, paddle.pir.Value]. But received {type(fill_value)}." + ) if dtype is None: if isinstance(fill_value, (bool)): diff --git a/test/legacy_test/test_full_like_op.py b/test/legacy_test/test_full_like_op.py index 77b7941240051b..df0cd146f3dfc9 100644 --- a/test/legacy_test/test_full_like_op.py +++ b/test/legacy_test/test_full_like_op.py @@ -16,6 +16,7 @@ import numpy as np from op_test import OpTest, convert_float_to_uint16 +from utils import dygraph_guard import paddle import paddle.framework.dtype as dtypes @@ -41,7 +42,7 @@ def fill_any_like_wrapper(x, value, out_dtype=None, name=None): return paddle.full_like(x, value, tmp_dtype, name) -class TestFullOp(unittest.TestCase): +class TestFullLikeOp(unittest.TestCase): """Test fill_any_like op(whose API is full_like) for attr out.""" def test_attr_tensor_API(self): @@ -94,7 +95,7 @@ def test_full_like_fill_inf(self): paddle.enable_static() -class TestFullOpError(unittest.TestCase): +class TestFullLikeOpError(unittest.TestCase): def test_errors(self): with paddle.static.program_guard( @@ -115,6 +116,25 @@ def test_errors(self): dtype='uint4', ) + def test_fill_value_errors(self): + with dygraph_guard(): + # The fill_value must be one of [int, float, bool, complex, core.eager.Tensor, Variable, paddle.pir.Value]. + self.assertRaises( + TypeError, + paddle.full, + shape=[1], + dtype="float32", + fill_value=np.array([1.0], dtype=np.float32), + ) + + self.assertRaises( + TypeError, + paddle.full, + shape=[1], + dtype="float32", + fill_value=[1.0], + ) + class TestFullLikeOp1(OpTest): # test basic diff --git a/test/legacy_test/test_full_op.py b/test/legacy_test/test_full_op.py index f9154d42a39101..6ce63e5bba46e9 100644 --- a/test/legacy_test/test_full_op.py +++ b/test/legacy_test/test_full_op.py @@ -15,6 +15,7 @@ import unittest import numpy as np +from utils import dygraph_guard import paddle from paddle import base @@ -403,6 +404,25 @@ def test_shape_tensor_list_dtype(): self.assertRaises(TypeError, test_shape_tensor_list_dtype) paddle.disable_static() + def test_fill_value_errors(self): + with dygraph_guard(): + # The fill_value must be one of [int, float, bool, complex, core.eager.Tensor, Variable, paddle.pir.Value]. + self.assertRaises( + TypeError, + paddle.full, + shape=[1], + dtype="float32", + fill_value=np.array([1.0], dtype=np.float32), + ) + + self.assertRaises( + TypeError, + paddle.full, + shape=[1], + dtype="float32", + fill_value=[1.0], + ) + if __name__ == "__main__": unittest.main() From dfc634495f38f2b00c97d51cbdc9a4fc711e6e71 Mon Sep 17 00:00:00 2001 From: ooooo <3164076421@qq.com> Date: Sat, 19 Jul 2025 07:52:36 +0800 Subject: [PATCH 2/9] refine --- python/paddle/tensor/creation.py | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index f88ee17e466358..277ac386197771 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -1043,7 +1043,7 @@ def get_slice( def full_like( x: paddle.Tensor, - fill_value: bool | builtins.complex, + fill_value: bool | builtins.complex | paddle.Tensor, dtype: DTypeLike | None = None, name: str | None = None, ) -> paddle.Tensor: @@ -1054,7 +1054,8 @@ def full_like( Args: x(Tensor): The input tensor which specifies shape and data type. The data type can be bool, float16, float32, float64, int32, int64. - fill_value(bool|float|int|complex): The value to fill the tensor with. Note: this value shouldn't exceed the range of the output data type. + fill_value(bool|float|int|complex|Tensor): The value to fill the tensor with. Note: this value shouldn't exceed the range of the output data type. + If ``fill_value`` is an Tensor, it should be an 0-D Tensor which represents a scalar. dtype(np.dtype|str, optional): The data type of output. The data type can be one of bool, float16, float32, float64, int32, int64, complex64, complex128. The default value is None, which means the output data type is the same as input. @@ -1074,7 +1075,9 @@ def full_like( [[2. 2. 2.] [2. 2. 2.]] """ - if not isinstance(fill_value, (bool, int, float, builtins.complex)): + if not isinstance( + fill_value, (bool, int, float, builtins.complex, paddle.Tensor) + ): raise TypeError( f"fill_value should be bool, int, float or complex, but received {type(fill_value)}." ) @@ -1579,16 +1582,7 @@ def full( [2. 2.]] """ if not isinstance( - fill_value, - ( - int, - float, - bool, - builtins.complex, - core.eager.Tensor, - Variable, - paddle.pir.Value, - ), + fill_value, (int, float, bool, builtins.complex, paddle.Tensor) ): raise TypeError( f"The fill_value must be one of [int, float, bool, complex, core.eager.Tensor,Variable, paddle.pir.Value]. But received {type(fill_value)}." From f16a4ccf108b49f3b5d8c0b1983770ffed658deb Mon Sep 17 00:00:00 2001 From: ooooo <3164076421@qq.com> Date: Sat, 19 Jul 2025 08:13:24 +0800 Subject: [PATCH 3/9] refine --- python/paddle/tensor/creation.py | 26 +++++++++++++--- test/legacy_test/test_full_like_op.py | 44 +++++++++++++++++++++++++-- test/legacy_test/test_full_op.py | 2 +- 3 files changed, 65 insertions(+), 7 deletions(-) diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index 277ac386197771..a59c3c154d34f3 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -1076,10 +1076,19 @@ def full_like( [2. 2. 2.]] """ if not isinstance( - fill_value, (bool, int, float, builtins.complex, paddle.Tensor) + fill_value, + ( + bool, + int, + float, + builtins.complex, + core.eager.Tensor, + Variable, + paddle.pir.Value, + ), ): raise TypeError( - f"fill_value should be bool, int, float or complex, but received {type(fill_value)}." + f"The fill_value should be bool, int, float, complex or Tensor, but received {type(fill_value)}." ) if dtype is None: @@ -1582,10 +1591,19 @@ def full( [2. 2.]] """ if not isinstance( - fill_value, (int, float, bool, builtins.complex, paddle.Tensor) + fill_value, + ( + int, + float, + bool, + builtins.complex, + core.eager.Tensor, + Variable, + paddle.pir.Value, + ), ): raise TypeError( - f"The fill_value must be one of [int, float, bool, complex, core.eager.Tensor,Variable, paddle.pir.Value]. But received {type(fill_value)}." + f"The fill_value should be bool, int, float, complex or Tensor, but received {type(fill_value)}." ) if dtype is None: diff --git a/test/legacy_test/test_full_like_op.py b/test/legacy_test/test_full_like_op.py index df0cd146f3dfc9..16ea74bc288deb 100644 --- a/test/legacy_test/test_full_like_op.py +++ b/test/legacy_test/test_full_like_op.py @@ -16,7 +16,7 @@ import numpy as np from op_test import OpTest, convert_float_to_uint16 -from utils import dygraph_guard +from utils import dygraph_guard, static_guard import paddle import paddle.framework.dtype as dtypes @@ -118,7 +118,7 @@ def test_errors(self): def test_fill_value_errors(self): with dygraph_guard(): - # The fill_value must be one of [int, float, bool, complex, core.eager.Tensor, Variable, paddle.pir.Value]. + # The fill_value must be one of [int, float, bool, complex, Tensor]. self.assertRaises( TypeError, paddle.full, @@ -289,5 +289,45 @@ def test_full_like_kernel_gpu_zero_size(self): paddle.enable_static() +class TestFullLikeWithTensorValue(unittest.TestCase): + def test_dygraph_api(self): + with dygraph_guard(): + base_np = np.array([[1, 2], [3, 4]], dtype=np.float32) + value_np = np.array([5.0], dtype=np.float32) + base_tensor = paddle.to_tensor(base_np) + value_tensor = paddle.to_tensor(value_np) + result = paddle.full_like(base_tensor, value_tensor) + expected = np.full_like(base_np, value_np) + self.assertTrue(np.array_equal(result.numpy(), expected)) + + def test_static_api(self): + with static_guard(): + startup_program = paddle.static.Program() + train_program = paddle.static.Program() + with paddle.static.program_guard(train_program, startup_program): + base_tensor = paddle.static.data( + name='base_tensor', dtype='float32', shape=[2, 2] + ) + value_tensor = paddle.static.data( + name='value_tensor', dtype='float32', shape=[1] + ) + result = paddle.full_like(base_tensor, value_tensor) + + place = paddle.CPUPlace() + exe = paddle.static.Executor(place) + + base_np = np.array([[1, 2], [3, 4]], dtype=np.float32) + value_np = np.array([5.0], dtype=np.float32) + + res = exe.run( + train_program, + feed={'base_tensor': base_np, 'value_tensor': value_np}, + fetch_list=[result], + ) + + expected = np.full_like(base_np, value_np) + self.assertTrue(np.array_equal(res[0], expected)) + + if __name__ == "__main__": unittest.main() diff --git a/test/legacy_test/test_full_op.py b/test/legacy_test/test_full_op.py index 6ce63e5bba46e9..528f29acf730b1 100644 --- a/test/legacy_test/test_full_op.py +++ b/test/legacy_test/test_full_op.py @@ -406,7 +406,7 @@ def test_shape_tensor_list_dtype(): def test_fill_value_errors(self): with dygraph_guard(): - # The fill_value must be one of [int, float, bool, complex, core.eager.Tensor, Variable, paddle.pir.Value]. + # The fill_value must be one of [int, float, bool, complex, Tensor]. self.assertRaises( TypeError, paddle.full, From 4d0360a46c9000770da807de7661a518acf1ad71 Mon Sep 17 00:00:00 2001 From: ooooo <3164076421@qq.com> Date: Sat, 19 Jul 2025 08:15:32 +0800 Subject: [PATCH 4/9] refine --- python/paddle/tensor/creation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index a59c3c154d34f3..6be70cebacbacb 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -1547,7 +1547,7 @@ def full( fill_value(bool|float|int|complex|Tensor): The constant value used to initialize the Tensor to be created. If ``fill_value`` is an Tensor, it should be an 0-D Tensor which represents a scalar. dtype(np.dtype|str, optional): Data type of the output Tensor - which can be float16, float32, float64, int32, int64, if dtype is `None`, the data + which can be float16, float32, float64, int32, int64, complex64, complex128. If dtype is `None`, the data type of created Tensor is `float32`. name(str|None, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. From 580a84fd1322a8e1984d3e2b8100ff032c1ab807 Mon Sep 17 00:00:00 2001 From: ooooo <3164076421@qq.com> Date: Sat, 19 Jul 2025 08:23:43 +0800 Subject: [PATCH 5/9] pass approve ci --- test/legacy_test/test_full_like_op.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/legacy_test/test_full_like_op.py b/test/legacy_test/test_full_like_op.py index 16ea74bc288deb..4ebd205b45b516 100644 --- a/test/legacy_test/test_full_like_op.py +++ b/test/legacy_test/test_full_like_op.py @@ -298,7 +298,7 @@ def test_dygraph_api(self): value_tensor = paddle.to_tensor(value_np) result = paddle.full_like(base_tensor, value_tensor) expected = np.full_like(base_np, value_np) - self.assertTrue(np.array_equal(result.numpy(), expected)) + np.testing.assert_array_equal(result.numpy(), expected) def test_static_api(self): with static_guard(): @@ -326,7 +326,7 @@ def test_static_api(self): ) expected = np.full_like(base_np, value_np) - self.assertTrue(np.array_equal(res[0], expected)) + np.testing.assert_array_equal(res[0], expected) if __name__ == "__main__": From e2d530b7ade000e4fd222c91f362cb8bda51ee1f Mon Sep 17 00:00:00 2001 From: ooooo <3164076421@qq.com> Date: Sat, 19 Jul 2025 09:42:10 +0800 Subject: [PATCH 6/9] refine code --- python/paddle/tensor/creation.py | 33 +++++++++----------------------- 1 file changed, 9 insertions(+), 24 deletions(-) diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index 6be70cebacbacb..81d2e0639ab995 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -16,6 +16,7 @@ import builtins import math +import numbers import re import warnings from typing import TYPE_CHECKING, overload @@ -1043,7 +1044,7 @@ def get_slice( def full_like( x: paddle.Tensor, - fill_value: bool | builtins.complex | paddle.Tensor, + fill_value: Numeric, dtype: DTypeLike | None = None, name: str | None = None, ) -> paddle.Tensor: @@ -1054,7 +1055,7 @@ def full_like( Args: x(Tensor): The input tensor which specifies shape and data type. The data type can be bool, float16, float32, float64, int32, int64. - fill_value(bool|float|int|complex|Tensor): The value to fill the tensor with. Note: this value shouldn't exceed the range of the output data type. + fill_value(Scalar|Tensor): The value to fill the tensor with. Note: this value shouldn't exceed the range of the output data type. If ``fill_value`` is an Tensor, it should be an 0-D Tensor which represents a scalar. dtype(np.dtype|str, optional): The data type of output. The data type can be one of bool, float16, float32, float64, int32, int64, complex64, complex128. The default value is None, which means the output @@ -1077,18 +1078,10 @@ def full_like( """ if not isinstance( fill_value, - ( - bool, - int, - float, - builtins.complex, - core.eager.Tensor, - Variable, - paddle.pir.Value, - ), + (numbers.Number, core.eager.Tensor, Variable, paddle.pir.Value), ): raise TypeError( - f"The fill_value should be bool, int, float, complex or Tensor, but received {type(fill_value)}." + f"The fill_value should be scalar or Tensor, but received {type(fill_value)}." ) if dtype is None: @@ -1532,7 +1525,7 @@ def _check_attr(attr, message): def full( shape: ShapeLike, - fill_value: bool | builtins.complex | paddle.Tensor, + fill_value: Numeric, dtype: DTypeLike | None = None, name: str | None = None, ) -> paddle.Tensor: @@ -1544,7 +1537,7 @@ def full( shape (tuple|list|Tensor): Shape of the Tensor to be created. The data type is ``int32`` or ``int64`` . If ``shape`` is a list or tuple, each element of it should be integer or 0-D Tensor with shape []. If ``shape`` is an Tensor, it should be an 1-D Tensor which represents a list. - fill_value(bool|float|int|complex|Tensor): The constant value used to initialize the Tensor to be created. + fill_value(Scalar|Tensor): The constant value used to initialize the Tensor to be created. If ``fill_value`` is an Tensor, it should be an 0-D Tensor which represents a scalar. dtype(np.dtype|str, optional): Data type of the output Tensor which can be float16, float32, float64, int32, int64, complex64, complex128. If dtype is `None`, the data @@ -1592,18 +1585,10 @@ def full( """ if not isinstance( fill_value, - ( - int, - float, - bool, - builtins.complex, - core.eager.Tensor, - Variable, - paddle.pir.Value, - ), + (numbers.Number, core.eager.Tensor, Variable, paddle.pir.Value), ): raise TypeError( - f"The fill_value should be bool, int, float, complex or Tensor, but received {type(fill_value)}." + f"The fill_value should be scalar or Tensor, but received {type(fill_value)}." ) if dtype is None: From 9f4040091634db8a4328e5ba293004709d55501b Mon Sep 17 00:00:00 2001 From: ooooo <3164076421@qq.com> Date: Sat, 19 Jul 2025 10:40:56 +0800 Subject: [PATCH 7/9] adapt string numeric values usage --- python/paddle/tensor/creation.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index 81d2e0639ab995..bdd2799f2771c3 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -1044,7 +1044,7 @@ def get_slice( def full_like( x: paddle.Tensor, - fill_value: Numeric, + fill_value: Numeric | str, dtype: DTypeLike | None = None, name: str | None = None, ) -> paddle.Tensor: @@ -1076,9 +1076,10 @@ def full_like( [[2. 2. 2.] [2. 2. 2.]] """ + # Include str type check to handle string numeric values like "0.5" that occur in CI tests. if not isinstance( fill_value, - (numbers.Number, core.eager.Tensor, Variable, paddle.pir.Value), + (numbers.Number, str, core.eager.Tensor, Variable, paddle.pir.Value), ): raise TypeError( f"The fill_value should be scalar or Tensor, but received {type(fill_value)}." @@ -1525,7 +1526,7 @@ def _check_attr(attr, message): def full( shape: ShapeLike, - fill_value: Numeric, + fill_value: Numeric | str, dtype: DTypeLike | None = None, name: str | None = None, ) -> paddle.Tensor: @@ -1583,9 +1584,10 @@ def full( [2. 2.] [2. 2.]] """ + # Include str type check to handle string numeric values like "0.5" that occur in CI tests. if not isinstance( fill_value, - (numbers.Number, core.eager.Tensor, Variable, paddle.pir.Value), + (numbers.Number, str, core.eager.Tensor, Variable, paddle.pir.Value), ): raise TypeError( f"The fill_value should be scalar or Tensor, but received {type(fill_value)}." From b8010bb57f635fdb3524737107a69fed58d16366 Mon Sep 17 00:00:00 2001 From: ooooo <3164076421@qq.com> Date: Sat, 19 Jul 2025 10:46:24 +0800 Subject: [PATCH 8/9] add more comments --- python/paddle/tensor/creation.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index bdd2799f2771c3..c69200210a3122 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -1077,6 +1077,7 @@ def full_like( [2. 2. 2.]] """ # Include str type check to handle string numeric values like "0.5" that occur in CI tests. + # The compatible method for fliud operators, may be it can be removed in the future. if not isinstance( fill_value, (numbers.Number, str, core.eager.Tensor, Variable, paddle.pir.Value), @@ -1585,6 +1586,7 @@ def full( [2. 2.]] """ # Include str type check to handle string numeric values like "0.5" that occur in CI tests. + # The compatible method for fliud operators, may be it can be removed in the future. if not isinstance( fill_value, (numbers.Number, str, core.eager.Tensor, Variable, paddle.pir.Value), From 25ea9e613b812acba0dd5a85aa9decadd0f94baf Mon Sep 17 00:00:00 2001 From: ooooo <3164076421@qq.com> Date: Wed, 23 Jul 2025 06:29:39 +0800 Subject: [PATCH 9/9] add more tests --- python/paddle/tensor/creation.py | 4 ++-- test/legacy_test/test_full_like_op.py | 32 +++++++++++++++++++++------ test/legacy_test/test_full_op.py | 10 ++++++++- 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index c69200210a3122..a070208818f3e6 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -1083,7 +1083,7 @@ def full_like( (numbers.Number, str, core.eager.Tensor, Variable, paddle.pir.Value), ): raise TypeError( - f"The fill_value should be scalar or Tensor, but received {type(fill_value)}." + f"The fill_value should be int, float, bool, complex, np.number, string numeric value or Tensor, but received {type(fill_value)}." ) if dtype is None: @@ -1592,7 +1592,7 @@ def full( (numbers.Number, str, core.eager.Tensor, Variable, paddle.pir.Value), ): raise TypeError( - f"The fill_value should be scalar or Tensor, but received {type(fill_value)}." + f"The fill_value should be int, float, bool, complex, np.number, string numeric values or Tensor, but received {type(fill_value)}." ) if dtype is None: diff --git a/test/legacy_test/test_full_like_op.py b/test/legacy_test/test_full_like_op.py index 4ebd205b45b516..03596bb07b17f9 100644 --- a/test/legacy_test/test_full_like_op.py +++ b/test/legacy_test/test_full_like_op.py @@ -118,21 +118,29 @@ def test_errors(self): def test_fill_value_errors(self): with dygraph_guard(): - # The fill_value must be one of [int, float, bool, complex, Tensor]. + # The fill_value must be one of [int, float, bool, complex, Tensor, np.number]. self.assertRaises( TypeError, - paddle.full, - shape=[1], - dtype="float32", + paddle.full_like, + x=paddle.to_tensor([1.0, 2.0]), fill_value=np.array([1.0], dtype=np.float32), + dtype="float32", ) self.assertRaises( TypeError, - paddle.full, - shape=[1], - dtype="float32", + paddle.full_like, + x=paddle.to_tensor([1.0, 2.0]), fill_value=[1.0], + dtype="float32", + ) + + self.assertRaises( + TypeError, + paddle.full_like, + x=paddle.to_tensor([1.0, 2.0]), + fill_value=np.bool_(True), + dtype="bool", ) @@ -219,6 +227,16 @@ def test_skip_data_transform(self): paddle.enable_static() +class TestFullLikeOp5(TestFullLikeOp1): + def init_data(self): + self.fill_value = True + self.shape = [10, 10] + self.dtype = np.bool + + def if_enable_cinn(self): + pass + + class TestFullLikeFP16Op(TestFullLikeOp1): def init_data(self): self.fill_value = 6666 diff --git a/test/legacy_test/test_full_op.py b/test/legacy_test/test_full_op.py index 528f29acf730b1..e66f79ba3942df 100644 --- a/test/legacy_test/test_full_op.py +++ b/test/legacy_test/test_full_op.py @@ -406,7 +406,7 @@ def test_shape_tensor_list_dtype(): def test_fill_value_errors(self): with dygraph_guard(): - # The fill_value must be one of [int, float, bool, complex, Tensor]. + # The fill_value must be one of [int, float, bool, complex, np.number, Tensor]. self.assertRaises( TypeError, paddle.full, @@ -423,6 +423,14 @@ def test_fill_value_errors(self): fill_value=[1.0], ) + self.assertRaises( + TypeError, + paddle.full, + shape=[1], + dtype="bool", + fill_value=np.bool_(True), + ) + if __name__ == "__main__": unittest.main()