Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PIR] D-16 Adapt full test_errors #62830

Merged
merged 8 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions python/paddle/tensor/creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -907,15 +907,15 @@ def fill_constant(shape, dtype, value, force_cpu=False, out=None, name=None):
value = float(value)
if isinstance(shape, (list, tuple)):
shape = paddle.utils.convert_shape_to_list(shape)

else:
paddle.utils.check_shape(shape)
if isinstance(shape, (list, tuple)):
if paddle.utils._contain_var(shape):
shape = paddle.utils.get_int_tensor_list(shape, place)
elif isinstance(shape, paddle.pir.Value):
pass
else:
TypeError("Shape only supports OpResult, or list, or tuple.")
raise TypeError("Shape only supports Value, or list, or tuple.")

if out is None:
out = _C_ops.full(shape, value, dtype, place)
Expand Down
14 changes: 11 additions & 3 deletions python/paddle/utils/layers_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
_current_expected_place,
in_dygraph_mode,
)
from ..pir import Value


def convert_to_list(value, n, name, dtype=int):
Expand Down Expand Up @@ -496,11 +497,11 @@ def check_shape(shape):
"""
Check shape type and shape elements type before passing it to fill_constant
"""
if isinstance(shape, Variable):
if isinstance(shape, (Variable, Value)):
check_dtype(shape.dtype, 'shape', ['int32', 'int64'], 'fill_constant')
else:
elif isinstance(shape, (list, tuple)):
for ele in shape:
if not isinstance(ele, Variable):
if not isinstance(ele, (Variable, Value)):
if ele < 0:
raise ValueError(
"All elements in ``shape`` must be positive when it's a list or tuple"
Expand All @@ -509,6 +510,13 @@ def check_shape(shape):
raise TypeError(
"All elements in ``shape`` must be integers when it's a list or tuple"
)
else:
check_dtype(
ele.dtype,
'element of shape',
['int32', 'int64'],
'fill_constant',
0x45f marked this conversation as resolved.
Show resolved Hide resolved
)


def try_set_static_shape_tensor(tensor, shape):
Expand Down
10 changes: 8 additions & 2 deletions test/legacy_test/test_full_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@

import paddle
from paddle import base
from paddle.base import Program, program_guard
from paddle.pir_utils import test_with_pir_api


# Test python API
class TestFullAPI(unittest.TestCase):
@test_with_pir_api
def test_api(self):
paddle.enable_static()
with paddle.static.program_guard(paddle.static.Program()):
positive_2_int32 = paddle.tensor.fill_constant([1], "int32", 2)

Expand Down Expand Up @@ -98,6 +98,7 @@ def test_api(self):
np.testing.assert_array_equal(
res_7, np.full([1, 2], 1.1, dtype="float32")
)
paddle.disable_static()

def test_api_eager(self):
with base.dygraph.base.guard():
Expand Down Expand Up @@ -184,8 +185,12 @@ def test_api_eager(self):


class TestFullOpError(unittest.TestCase):
@test_with_pir_api
def test_errors(self):
with program_guard(Program(), Program()):
paddle.enable_static()
with paddle.static.program_guard(
paddle.static.Program(), paddle.static.Program()
):
# for ci coverage
self.assertRaises(
TypeError, paddle.full, shape=[1], fill_value=5, dtype='uint4'
Expand Down Expand Up @@ -216,6 +221,7 @@ def test_shape_tensor_list_dtype():
paddle.full(shape=[shape, 2], dtype="float32", fill_value=1)

self.assertRaises(TypeError, test_shape_tensor_list_dtype)
paddle.disable_static()


if __name__ == "__main__":
Expand Down