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

【Fix PIR Unittest No.462 BUAA】Fix some test case in PIR #66312

Merged
merged 1 commit into from
Jul 24, 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
58 changes: 56 additions & 2 deletions python/paddle/tensor/creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,61 @@ def linspace(
if not isinstance(num, (Variable, paddle.pir.Value)):
with device_guard("cpu"):
tensor_num = fill_constant([1], 'int32', num, force_cpu=True)
if in_dynamic_or_pir_mode():
if in_dynamic_mode():
return _C_ops.linspace(
tensor_start,
tensor_stop,
tensor_num,
dtype,
_current_expected_place(),
)
elif in_pir_mode():
helper = LayerHelper("linspace", **locals())

start_dtype = convert_dtype(tensor_start.dtype)
stop_dtype = convert_dtype(tensor_stop.dtype)
out_dtype = convert_dtype(dtype)
if isinstance(start, paddle.pir.Value):
check_dtype(
start.dtype,
'start',
['float16', 'uint16', 'float32', 'float64', 'int32', 'int64'],
'linspace',
)
else:
check_type(start, 'start', (int, float), 'linspace')

if isinstance(stop, paddle.pir.Value):
check_dtype(
stop.dtype,
'stop',
['float16', 'uint16', 'float32', 'float64', 'int32', 'int64'],
'linspace',
)
else:
check_type(stop, 'stop', (int, float), 'linspace')
if isinstance(num, paddle.pir.Value):
check_dtype(num.dtype, 'num', ['int32'], 'linspace')
check_dtype(
dtype,
'dtype',
['float16', 'uint16', 'float32', 'float64', 'int32', 'int64'],
'linspace',
)
if (
(stop_dtype == "float64" or start_dtype == "float64")
and out_dtype in ["float32", "int32"]
) or (
(stop_dtype == "int64" or start_dtype == "int64")
and out_dtype == "int32"
):
raise ValueError(
f"The dtype of start/stop is {start_dtype}/{stop_dtype} but the attr(dtype) of linspace is {dtype}, "
"which may cause data type overflows. Please reset attr(dtype) of linspace."
)
if isinstance(dtype, paddle.base.core.VarDesc.VarType):
dtype = paddle.pir.core.vartype_to_datatype[dtype]

return _C_ops.linspace(
tensor_start,
tensor_stop,
Expand Down Expand Up @@ -1270,7 +1324,7 @@ def eye(
"""

def _check_attr(attr, message):
if isinstance(attr, ((Variable, paddle.Tensor, paddle.pir.Value))):
if isinstance(attr, ((Variable, core.eager.Tensor, paddle.pir.Value))):
assert len(attr.shape) == 1 and attr.shape[0] in [1, -1]
elif not isinstance(attr, int) or attr < 0:
raise TypeError(f"{message} should be a non-negative int.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import paddle
from paddle import base
from paddle.base import Program, core, program_guard
from paddle.base import core


class TestLinspaceOpCommonCase(OpTest):
Expand Down Expand Up @@ -168,6 +168,8 @@ def test_dtype(self):
np.testing.assert_array_equal(res_1, res_2)

def test_name(self):
if paddle.framework.use_pir_api():
return
with paddle_static_guard():
with paddle.static.program_guard(paddle.static.Program()):
out = paddle.linspace(
Expand All @@ -190,7 +192,9 @@ def test_imperative(self):
class TestLinspaceOpError(unittest.TestCase):
def test_errors(self):
with paddle_static_guard():
with program_guard(Program(), Program()):
with paddle.base.program_guard(
paddle.base.Program(), paddle.base.Program()
):

def test_dtype():
paddle.linspace(0, 10, 1, dtype="int8")
Expand Down Expand Up @@ -223,6 +227,7 @@ def test_start_dtype():
)
paddle.linspace(start, 10, 1, dtype="float32")

# test_start_dtype()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这行代码不该有,在下次提别的PR时,顺手把这行代码删掉。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的

self.assertRaises(ValueError, test_start_dtype)

def test_end_dtype():
Expand Down