From 1b250710a1d6984e200ff0e98bccf9ab1eb27df0 Mon Sep 17 00:00:00 2001 From: Nyakku Shigure Date: Thu, 10 Nov 2022 10:22:23 +0800 Subject: [PATCH] [CodeStyle][F821] fix test_exception in test_unpool3d_op and test_unpool_op (#47756) * [Fix][F821] fix TestUnpoolOpException * fix TestUnpoolOpException * fix TestUnpool3DOpException * remove unused variables * fix the regexp does not match the C++ traceback * add missing error message for gpu unpool_kernel * Revert "add missing error message for gpu unpool_kernel" This reverts commit 17ef7a127e1c3ee00f9102c37ad8cea35953f20c. * assertion indices_value_error errors are only reported on the CPU * for test * run test_exception in dygraph mode --- .../fluid/tests/unittests/test_unpool3d_op.py | 64 ++++++++++----- .../fluid/tests/unittests/test_unpool_op.py | 78 +++++++++++++------ 2 files changed, 98 insertions(+), 44 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_unpool3d_op.py b/python/paddle/fluid/tests/unittests/test_unpool3d_op.py index 3db2d1514ca..af17073758f 100644 --- a/python/paddle/fluid/tests/unittests/test_unpool3d_op.py +++ b/python/paddle/fluid/tests/unittests/test_unpool3d_op.py @@ -17,6 +17,7 @@ import numpy as np from op_test import OpTest import paddle import paddle.nn.functional as F +from paddle.fluid import core paddle.enable_static() paddle.seed(2022) @@ -178,36 +179,42 @@ class TestUnpool3DOpOutput(TestUnpool3DOp): class TestUnpool3DOpException(unittest.TestCase): + def setUp(self): + paddle.disable_static() + + def tearDown(self): + paddle.enable_static() + def test_exception(self): def indices_size_error(): - data = paddle.randint(shape=[1, 1, 3, 3, 3]) + data = paddle.rand(shape=[1, 1, 3, 3, 3]) indices = paddle.reshape( paddle.arange(0, 36), shape=[1, 1, 3, 3, 4] - ) - MaxUnPool3D = F.maxunpool3d(data, indices, kernel_size=2, stride=2) + ).astype("int32") + F.max_unpool3d(data, indices, kernel_size=2, stride=2) def indices_value_error(): - data = paddle.randint(shape=[1, 1, 3, 3, 3]) + data = paddle.rand(shape=[1, 1, 3, 3, 3]) indices = paddle.reshape( - paddle.arange(4, 40), shape=[1, 1, 3, 3, 3] - ) - MaxUnPool3D = F.maxunpool3d(data, indices, kernel_size=2, stride=2) + paddle.arange(195, 222), shape=[1, 1, 3, 3, 3] + ).astype("int32") + F.max_unpool3d(data, indices, kernel_size=2, stride=2) def data_format_error(): - data = paddle.randint(shape=[1, 1, 3, 3, 3]) + data = paddle.rand(shape=[1, 1, 3, 3, 3]) indices = paddle.reshape( paddle.arange(0, 27), shape=[1, 1, 3, 3, 3] - ) - MaxUnPool3D = F.maxunpool3d( + ).astype("int32") + F.max_unpool3d( data, indices, kernel_size=2, stride=2, data_format="NDHWC" ) def data_outputsize_error(): - data = paddle.randint(shape=[1, 1, 3, 3, 3]) + data = paddle.rand(shape=[1, 1, 3, 3, 3]) indices = paddle.reshape( paddle.arange(0, 27), shape=[1, 1, 3, 3, 3] - ) - MaxUnPool3D = F.maxunpool3d( + ).astype("int32") + F.max_unpool3d( data, indices, kernel_size=2, @@ -216,19 +223,36 @@ class TestUnpool3DOpException(unittest.TestCase): ) def data_outputsize_error2(): - data = paddle.randint(shape=[1, 1, 3, 3, 3]) + data = paddle.rand(shape=[1, 1, 3, 3, 3]) indices = paddle.reshape( paddle.arange(0, 27), shape=[1, 1, 3, 3, 3] ) - MaxUnPool3D = F.maxunpool3d( + F.max_unpool3d( data, indices, kernel_size=2, stride=2, output_size=[10, 10, 10] ) - self.assertRaises(ValueError, indices_size_error) - self.assertRaises(ValueError, indices_value_error) - self.assertRaises(ValueError, data_format_error) - self.assertRaises(ValueError, data_outputsize_error) - self.assertRaises(ValueError, data_outputsize_error2) + self.assertRaisesRegex( + ValueError, + r"The dimensions of Input\(X\) must equal to", + indices_size_error, + ) + if not core.is_compiled_with_cuda(): + self.assertRaisesRegex( + ValueError, + r"index should less than output", + indices_value_error, + ) + self.assertRaisesRegex( + ValueError, + r"Attr\(data_format\) should be 'NCDHW'", + data_format_error, + ) + self.assertRaisesRegex( + ValueError, r"invalid output_size", data_outputsize_error + ) + self.assertRaisesRegex( + ValueError, r"invalid output_size", data_outputsize_error2 + ) class TestUnpool3DOpAPI_dygraph(unittest.TestCase): diff --git a/python/paddle/fluid/tests/unittests/test_unpool_op.py b/python/paddle/fluid/tests/unittests/test_unpool_op.py index 00c48e0872d..4ae6919552c 100644 --- a/python/paddle/fluid/tests/unittests/test_unpool_op.py +++ b/python/paddle/fluid/tests/unittests/test_unpool_op.py @@ -18,7 +18,7 @@ import numpy as np from op_test import OpTest import paddle import paddle.nn.functional as F -from paddle.fluid import Program, program_guard +from paddle.fluid import Program, program_guard, core from test_attribute_var import UnittestBase @@ -178,46 +178,76 @@ class TestUnpoolOpOutput(TestUnpoolOp): class TestUnpoolOpException(unittest.TestCase): - def test_exception(self): - import paddle.nn.functional as F - import paddle + def setUp(self): + paddle.disable_static() + + def tearDown(self): + paddle.enable_static() + def test_exception(self): def indices_size_error(): - data = paddle.randint(shape=[1, 1, 3, 3]) - indices = paddle.reshape(paddle.arange(0, 12), shape[1, 1, 3, 4]) - MaxPool2D = F.maxunpool2d(data, indices, kernel_size=2, stride=2) + data = paddle.rand(shape=[1, 1, 3, 3]) + indices = paddle.reshape( + paddle.arange(0, 12), shape=[1, 1, 3, 4] + ).astype("int32") + F.max_unpool2d(data, indices, kernel_size=2, stride=2) def indices_value_error(): - data = paddle.randint(shape=[1, 1, 3, 3]) - indices = paddle.reshape(paddle.arange(4, 40), shape[1, 1, 3, 4]) - MaxPool2D = F.maxunpool2d(data, indices, kernel_size=2, stride=2) + data = paddle.rand(shape=[1, 1, 3, 3]) + indices = paddle.reshape( + paddle.arange(31, 40), shape=[1, 1, 3, 3] + ).astype("int32") + F.max_unpool2d(data, indices, kernel_size=2, stride=2) def data_format_error(): - data = paddle.randint(shape=[1, 1, 3, 3]) - indices = paddle.reshape(paddle.arange(4, 40), shape[1, 1, 3, 4]) - MaxPool2D = F.maxunpool2d( + data = paddle.rand(shape=[1, 1, 3, 3]) + indices = paddle.reshape( + paddle.arange(0, 9), shape=[1, 1, 3, 3] + ).astype("int32") + F.max_unpool2d( data, indices, kernel_size=2, stride=2, data_format="NHWC" ) def data_outputsize_error(): - data = paddle.randint(shape=[1, 1, 3, 3]) - indices = paddle.reshape(paddle.arange(4, 40), shape[1, 1, 3, 4]) - MaxPool2D = F.maxunpool2d( + data = paddle.rand(shape=[1, 1, 3, 3]) + indices = paddle.reshape( + paddle.arange(0, 9), shape=[1, 1, 3, 3] + ).astype("int32") + F.max_unpool2d( data, indices, kernel_size=2, stride=2, output_size=[5, 6, 7, 8] ) def data_outputsize_error2(): - data = paddle.randint(shape=[1, 1, 3, 3]) - indices = paddle.reshape(paddle.arange(4, 40), shape[1, 1, 3, 4]) - MaxPool2D = F.maxunpool2d( + data = paddle.rand(shape=[1, 1, 3, 3]) + indices = paddle.reshape( + paddle.arange(0, 9), shape=[1, 1, 3, 3] + ).astype("int32") + F.max_unpool2d( data, indices, kernel_size=2, stride=2, output_size=[100, 100] ) - self.assertRaises(ValueError, indices_size_error) - self.assertRaises(ValueError, indices_value_error) - self.assertRaises(ValueError, data_format_error) - self.assertRaises(ValueError, data_outputsize_error) - self.assertRaises(ValueError, data_outputsize_error2) + self.assertRaisesRegex( + ValueError, + r"The dimensions of Input\(X\) must equal to", + indices_size_error, + ) + if not core.is_compiled_with_cuda(): + self.assertRaisesRegex( + ValueError, + r"index should less than output", + indices_value_error, + ) + self.assertRaisesRegex( + ValueError, + r"Attr\(data_format\) should be 'NCHW'", + data_format_error, + ) + self.assertRaisesRegex( + ValueError, r"invalid output_size", data_outputsize_error + ) + self.assertRaisesRegex( + ValueError, r"invalid output_size", data_outputsize_error2 + ) class TestUnpoolOpAPI_dy(unittest.TestCase): -- GitLab