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] A-1、A-3 Adapt fill_constant and mean test_errors #60695

Merged
merged 9 commits into from
Feb 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
2 changes: 1 addition & 1 deletion python/paddle/tensor/stat.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def mean(x, axis=None, keepdim=False, name=None):
check_variable_and_dtype(
x,
'x/input',
['uint16', 'float16', 'float32', 'float64'],
['uint16', "int32", 'float16', 'float32', 'float64'],
'mean/reduce_mean',
)
check_type(
Expand Down
93 changes: 34 additions & 59 deletions test/legacy_test/test_fill_constant_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

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


Expand Down Expand Up @@ -419,8 +419,11 @@ def test_ninf(self):


class TestFillConstantOpError(unittest.TestCase):
def test_errors(self):
with paddle_static_guard(), program_guard(Program(), Program()):
@test_with_pir_api
def test_errors1(self):
with paddle_static_guard(), paddle.static.program_guard(
paddle.static.Program(), paddle.static.Program()
):
# for ci coverage
x1 = paddle.static.data(name='x1', shape=[-1, 1], dtype="int16")
self.assertRaises(
Expand All @@ -440,27 +443,30 @@ def test_errors(self):
out=x1,
)

# The argument dtype of fill_constant_op must be one of bool, float16,
# float32, float64, uint8, int16, int32 or int64
x2 = paddle.static.data(name='x2', shape=[-1, 1], dtype="int32")

x3 = np.random.randn(100, 100).astype('int32')
self.assertRaises(
TypeError,
paddle.tensor.fill_constant,
shape=[1],
shape=[100, 100],
value=5,
dtype='float64',
out=x2,
out=x3,
)

x3 = np.random.randn(100, 100).astype('int32')
def test_errors2(self):
with paddle_static_guard(), paddle.static.program_guard(
paddle.static.Program(), paddle.static.Program()
):
# The argument dtype of fill_constant_op must be one of bool, float16,
# float32, float64, uint8, int16, int32 or int64
x2 = paddle.static.data(name='x2', shape=[-1, 1], dtype="int32")
self.assertRaises(
TypeError,
paddle.tensor.fill_constant,
shape=[100, 100],
shape=[1],
value=5,
dtype='float64',
out=x3,
out=x2,
)

# The argument shape's type of fill_constant_op must be list, tuple or Variable.
Expand Down Expand Up @@ -490,57 +496,26 @@ def test_shape_tensor_list_dtype():

self.assertRaises(TypeError, test_shape_tensor_list_dtype)

with paddle.pir_utils.IrGuard(), program_guard(Program()):
x1 = paddle.static.data(name='x1', shape=[-1, 1], dtype="int16")
self.assertRaises(
TypeError,
paddle.tensor.fill_constant,
shape=[1],
value=5,
dtype='uint4',
)

self.assertRaises(
TypeError,
paddle.tensor.fill_constant,
shape=[1.1],
value=5,
dtype='float32',
out=x1,
)

x3 = np.random.randn(100, 100).astype('int32')
self.assertRaises(
TypeError,
paddle.tensor.fill_constant,
shape=[100, 100],
value=5,
dtype='float64',
out=x3,
)

def test_pir_errors(self):
def test_shape_type():
# The shape dtype of fill_constant_op must be int32 or int64.
# test_shape_tensor_dtype:
with paddle.pir_utils.IrGuard():
pir_program = paddle.static.Program()
with paddle.static.program_guard(pir_program):
shape = paddle.static.data(
name="shape_tensor", shape=[2], dtype="int32"
)
out = paddle.tensor.fill_constant(
shape=shape, dtype="float32", value=1
)
exe = base.Executor(place=base.CPUPlace())
exe.run(
feed={
"shape_tensor": np.array([1, 2]).astype("float32")
},
fetch_list=[out],
)

self.assertRaises(ValueError, test_shape_type)
shape = paddle.static.data(
name="shape_tensor", shape=[2], dtype="int32"
)
out = paddle.tensor.fill_constant(
shape=shape, dtype="float32", value=1
)
exe = base.Executor(place=base.CPUPlace())
exe.run(
feed={"shape_tensor": np.array([1, 2]).astype("float32")},
fetch_list=[out],
)

with paddle.pir_utils.IrGuard():
pir_program = paddle.static.Program()
with paddle.static.program_guard(pir_program):
self.assertRaises(ValueError, test_shape_type)


class TestFillConstantOp_ValueTensorBf16(OpTest):
Expand Down
31 changes: 6 additions & 25 deletions test/legacy_test/test_mean_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,34 +84,19 @@ def setUp(self):
else paddle.CPUPlace()
)

@test_with_pir_api
def test_errors(self):
paddle.enable_static()
with program_guard(Program(), Program()):
# The input type of mean_op must be Variable.
input1 = 12
self.assertRaises(TypeError, paddle.mean, input1)
# The input dtype of mean_op must be float16, float32, float64.
input2 = paddle.static.data(
name='input2', shape=[-1, 12, 10], dtype="int32"
)
self.assertRaises(TypeError, paddle.mean, input2)
input3 = paddle.static.data(
name='input3', shape=[-1, 4], dtype="float16"
)
paddle.nn.functional.softmax(input3)

with paddle.pir_utils.IrGuard(), program_guard(Program(), Program()):
input1 = 12
self.assertRaises(TypeError, paddle.mean, input1)

input2 = paddle.static.data(
name='input2', shape=[2, 3, 4, 5], dtype="int32"
)

out = paddle.mean(input2)

exe = paddle.static.Executor(self.place)
res = exe.run(feed={'input2': self.x}, fetch_list=[out])
if paddle.is_compiled_with_cuda():
input3 = paddle.static.data(
name='input3', shape=[-1, 4], dtype="float16"
)
paddle.nn.functional.softmax(input3)

paddle.disable_static()

Expand Down Expand Up @@ -536,10 +521,6 @@ def test_errors(self):
x = paddle.to_tensor(x)
self.assertRaises(Exception, paddle.mean, x, -3)
self.assertRaises(Exception, paddle.mean, x, 2)
paddle.enable_static()
with paddle.static.program_guard(paddle.static.Program()):
x = paddle.static.data('X', [10, 12], 'int32')
self.assertRaises(TypeError, paddle.mean, x)


class TestMeanWithTensorAxis1(TestReduceOPTensorAxisBase):
Expand Down
4 changes: 0 additions & 4 deletions test/xpu/test_mean_op_xpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,6 @@ def test_errors(self):
input1 = 12
self.assertRaises(TypeError, paddle.mean, input1)
# The input dtype of mean_op must be float16, float32, float64.
input2 = paddle.static.data(
name='input2', shape=[-1, 12, 10], dtype="int32"
)
self.assertRaises(TypeError, paddle.mean, input2)
input3 = paddle.static.data(
name='input3', shape=[-1, 4], dtype="float16"
)
Expand Down