From ddd5656aaf01aff9ac1489c19b707bf4e56e87ce Mon Sep 17 00:00:00 2001 From: Weilong Wu Date: Wed, 7 Dec 2022 10:49:18 +0800 Subject: [PATCH] rm _test_eager_guard (#48767) --- python/paddle/tests/test_async_read_write.py | 64 ++++---------- .../test_callback_reduce_lr_on_plateau.py | 15 +--- python/paddle/tests/test_callback_visualdl.py | 8 +- python/paddle/tests/test_datasets.py | 85 +++---------------- python/paddle/tests/test_dlpack.py | 36 ++------ 5 files changed, 35 insertions(+), 173 deletions(-) diff --git a/python/paddle/tests/test_async_read_write.py b/python/paddle/tests/test_async_read_write.py index 6605ac6d81..4fc2003988 100644 --- a/python/paddle/tests/test_async_read_write.py +++ b/python/paddle/tests/test_async_read_write.py @@ -19,7 +19,6 @@ import numpy as np import paddle from paddle.device import cuda from paddle.fluid import core -from paddle.fluid.framework import _in_legacy_dygraph, _test_eager_guard class TestAsyncRead(unittest.TestCase): @@ -40,24 +39,14 @@ class TestAsyncRead(unittest.TestCase): def func_test_async_read_empty_offset_and_count(self): with cuda.stream_guard(self.stream): - if _in_legacy_dygraph(): - core.async_read( - self.src, - self.dst, - self.index, - self.buffer, - self.empty, - self.empty, - ) - else: - core.eager.async_read( - self.src, - self.dst, - self.index, - self.buffer, - self.empty, - self.empty, - ) + core.eager.async_read( + self.src, + self.dst, + self.index, + self.buffer, + self.empty, + self.empty, + ) array1 = paddle.gather(self.src, self.index) array2 = self.dst[: len(self.index)] @@ -71,14 +60,9 @@ class TestAsyncRead(unittest.TestCase): np.array([5, 10], dtype="int64"), place=paddle.CPUPlace() ) with cuda.stream_guard(self.stream): - if _in_legacy_dygraph(): - core.async_read( - self.src, self.dst, self.index, self.buffer, offset, count - ) - else: - core.eager.async_read( - self.src, self.dst, self.index, self.buffer, offset, count - ) + core.eager.async_read( + self.src, self.dst, self.index, self.buffer, offset, count + ) # index data index_array1 = paddle.gather(self.src, self.index) count_numel = paddle.sum(count).numpy()[0] @@ -101,26 +85,14 @@ class TestAsyncRead(unittest.TestCase): dst = paddle.empty([40], dtype="float32") buffer_ = paddle.empty([20]).pin_memory() with cuda.stream_guard(self.stream): - if _in_legacy_dygraph(): - core.async_read( - src, dst, self.index, buffer_, self.empty, self.empty - ) - else: - core.eager.async_read( - src, dst, self.index, buffer_, self.empty, self.empty - ) + core.eager.async_read( + src, dst, self.index, buffer_, self.empty, self.empty + ) array1 = paddle.gather(src, self.index) array2 = dst[: len(self.index)] np.testing.assert_allclose(array1.numpy(), array2.numpy(), rtol=1e-05) def test_main(self): - with _test_eager_guard(): - self.func_setUp() - self.func_test_async_read_empty_offset_and_count() - self.func_setUp() - self.func_test_async_read_success() - self.func_setUp() - self.func_test_async_read_only_1dim() self.func_setUp() self.func_test_async_read_empty_offset_and_count() self.func_setUp() @@ -145,10 +117,7 @@ class TestAsyncWrite(unittest.TestCase): np.array([40, 60], dtype="int64"), place=paddle.CPUPlace() ) with cuda.stream_guard(self.stream): - if _in_legacy_dygraph(): - core.async_write(self.src, self.dst, offset, count) - else: - core.eager.async_write(self.src, self.dst, offset, count) + core.eager.async_write(self.src, self.dst, offset, count) offset_a = paddle.gather(self.dst, paddle.to_tensor(np.arange(0, 40))) offset_b = paddle.gather(self.dst, paddle.to_tensor(np.arange(60, 120))) @@ -158,9 +127,6 @@ class TestAsyncWrite(unittest.TestCase): ) def test_async_write_success(self): - with _test_eager_guard(): - self.func_setUp() - self.func_test_async_write_success() self.func_setUp() self.func_test_async_write_success() diff --git a/python/paddle/tests/test_callback_reduce_lr_on_plateau.py b/python/paddle/tests/test_callback_reduce_lr_on_plateau.py index c221c6d482..9e98ee4841 100644 --- a/python/paddle/tests/test_callback_reduce_lr_on_plateau.py +++ b/python/paddle/tests/test_callback_reduce_lr_on_plateau.py @@ -17,7 +17,6 @@ import unittest import paddle import paddle.vision.transforms as T from paddle import Model -from paddle.fluid.framework import _test_eager_guard from paddle.metric import Accuracy from paddle.nn.layer.loss import CrossEntropyLoss from paddle.static import InputSpec @@ -32,7 +31,7 @@ class CustomMnist(MNIST): class TestReduceLROnPlateau(unittest.TestCase): - def func_reduce_lr_on_plateau(self): + def test_reduce_lr_on_plateau(self): transform = T.Compose([T.Transpose(), T.Normalize([127.5], [127.5])]) train_dataset = CustomMnist(mode='train', transform=transform) val_dataset = CustomMnist(mode='test', transform=transform) @@ -57,12 +56,7 @@ class TestReduceLROnPlateau(unittest.TestCase): callbacks=[callbacks], ) - def test_reduce_lr_on_plateau(self): - with _test_eager_guard(): - self.func_reduce_lr_on_plateau() - self.func_reduce_lr_on_plateau() - - def func_warn_or_error(self): + def test_warn_or_error(self): with self.assertRaises(ValueError): paddle.callbacks.ReduceLROnPlateau(factor=2.0) # warning @@ -113,11 +107,6 @@ class TestReduceLROnPlateau(unittest.TestCase): callbacks=[callbacks], ) - def test_warn_or_error(self): - with _test_eager_guard(): - self.func_warn_or_error() - self.func_warn_or_error() - if __name__ == '__main__': unittest.main() diff --git a/python/paddle/tests/test_callback_visualdl.py b/python/paddle/tests/test_callback_visualdl.py index 5ad245a163..62a6c08b41 100644 --- a/python/paddle/tests/test_callback_visualdl.py +++ b/python/paddle/tests/test_callback_visualdl.py @@ -18,7 +18,6 @@ import unittest import paddle import paddle.vision.transforms as T -from paddle.fluid.framework import _test_eager_guard from paddle.static import InputSpec from paddle.vision.datasets import MNIST @@ -35,7 +34,7 @@ class TestCallbacks(unittest.TestCase): def tearDown(self): shutil.rmtree(self.save_dir) - def func_visualdl_callback(self): + def test_visualdl_callback(self): inputs = [InputSpec([-1, 1, 28, 28], 'float32', 'image')] labels = [InputSpec([None, 1], 'int64', 'label')] @@ -58,11 +57,6 @@ class TestCallbacks(unittest.TestCase): train_dataset, eval_dataset, batch_size=64, callbacks=callback ) - def test_visualdl_callback(self): - with _test_eager_guard(): - self.func_visualdl_callback() - self.func_visualdl_callback() - if __name__ == '__main__': unittest.main() diff --git a/python/paddle/tests/test_datasets.py b/python/paddle/tests/test_datasets.py index 2890a87746..422f7729c9 100644 --- a/python/paddle/tests/test_datasets.py +++ b/python/paddle/tests/test_datasets.py @@ -22,7 +22,6 @@ import numpy as np import paddle.vision.transforms as T from paddle.dataset.common import _check_exists_and_download -from paddle.fluid.framework import _test_eager_guard from paddle.vision.datasets import ( MNIST, DatasetFolder, @@ -47,7 +46,7 @@ class TestFolderDatasets(unittest.TestCase): def tearDown(self): shutil.rmtree(self.data_dir) - def func_test_dataset(self): + def test_dataset(self): dataset_folder = DatasetFolder(self.data_dir) for _ in dataset_folder: @@ -60,12 +59,7 @@ class TestFolderDatasets(unittest.TestCase): for _ in dataset_folder: pass - def test_dataset(self): - with _test_eager_guard(): - self.func_test_dataset() - self.func_test_dataset() - - def func_test_folder(self): + def test_folder(self): loader = ImageFolder(self.data_dir) for _ in loader: @@ -77,12 +71,7 @@ class TestFolderDatasets(unittest.TestCase): assert len(loader) == 4 - def test_folder(self): - with _test_eager_guard(): - self.func_test_folder() - self.func_test_folder() - - def func_test_transform(self): + def test_transform(self): def fake_transform(img): return img @@ -96,12 +85,7 @@ class TestFolderDatasets(unittest.TestCase): for _ in loader: pass - def test_transform(self): - with _test_eager_guard(): - self.func_test_transform() - self.func_test_transform() - - def func_test_errors(self): + def test_errors(self): with self.assertRaises(RuntimeError): ImageFolder(self.empty_dir) with self.assertRaises(RuntimeError): @@ -110,14 +94,9 @@ class TestFolderDatasets(unittest.TestCase): with self.assertRaises(ValueError): _check_exists_and_download('temp_paddle', None, None, None, False) - def test_errors(self): - with _test_eager_guard(): - self.func_test_errors() - self.func_test_errors() - class TestMNISTTest(unittest.TestCase): - def func_test_main(self): + def test_main(self): transform = T.Transpose() mnist = MNIST(mode='test', transform=transform) self.assertTrue(len(mnist) == 10000) @@ -130,14 +109,9 @@ class TestMNISTTest(unittest.TestCase): self.assertTrue(label.shape[0] == 1) self.assertTrue(0 <= int(label) <= 9) - def test_main(self): - with _test_eager_guard(): - self.func_test_main() - self.func_test_main() - class TestMNISTTrain(unittest.TestCase): - def func_test_main(self): + def test_main(self): transform = T.Transpose() mnist = MNIST(mode='train', transform=transform) self.assertTrue(len(mnist) == 60000) @@ -166,14 +140,9 @@ class TestMNISTTrain(unittest.TestCase): with self.assertRaises(ValueError): mnist = MNIST(mode='train', transform=transform, backend=1) - def test_main(self): - with _test_eager_guard(): - self.func_test_main() - self.func_test_main() - class TestFASHIONMNISTTest(unittest.TestCase): - def func_test_main(self): + def test_main(self): transform = T.Transpose() mnist = FashionMNIST(mode='test', transform=transform) self.assertTrue(len(mnist) == 10000) @@ -186,14 +155,9 @@ class TestFASHIONMNISTTest(unittest.TestCase): self.assertTrue(label.shape[0] == 1) self.assertTrue(0 <= int(label) <= 9) - def test_main(self): - with _test_eager_guard(): - self.func_test_main() - self.func_test_main() - class TestFASHIONMNISTTrain(unittest.TestCase): - def func_test_main(self): + def test_main(self): transform = T.Transpose() mnist = FashionMNIST(mode='train', transform=transform) self.assertTrue(len(mnist) == 60000) @@ -222,26 +186,16 @@ class TestFASHIONMNISTTrain(unittest.TestCase): with self.assertRaises(ValueError): mnist = FashionMNIST(mode='train', transform=transform, backend=1) - def test_main(self): - with _test_eager_guard(): - self.func_test_main() - self.func_test_main() - - def func_test_dataset_value(self): + def test_dataset_value(self): fmnist = FashionMNIST(mode='train') value = np.mean([np.array(x[0]) for x in fmnist]) # 72.94035223214286 was getted from competitive products np.testing.assert_allclose(value, 72.94035223214286) - def test_dataset_value(self): - with _test_eager_guard(): - self.func_test_dataset_value() - self.func_test_dataset_value() - class TestFlowersTrain(unittest.TestCase): - def func_test_main(self): + def test_main(self): flowers = Flowers(mode='train') self.assertTrue(len(flowers) == 6149) @@ -254,14 +208,9 @@ class TestFlowersTrain(unittest.TestCase): self.assertTrue(image.shape[2] == 3) self.assertTrue(label.shape[0] == 1) - def test_main(self): - with _test_eager_guard(): - self.func_test_main() - self.func_test_main() - class TestFlowersValid(unittest.TestCase): - def func_test_main(self): + def test_main(self): flowers = Flowers(mode='valid') self.assertTrue(len(flowers) == 1020) @@ -274,14 +223,9 @@ class TestFlowersValid(unittest.TestCase): self.assertTrue(image.shape[2] == 3) self.assertTrue(label.shape[0] == 1) - def test_main(self): - with _test_eager_guard(): - self.func_test_main() - self.func_test_main() - class TestFlowersTest(unittest.TestCase): - def func_test_main(self): + def test_main(self): flowers = Flowers(mode='test') self.assertTrue(len(flowers) == 1020) @@ -310,11 +254,6 @@ class TestFlowersTest(unittest.TestCase): with self.assertRaises(ValueError): flowers = Flowers(mode='test', backend=1) - def test_main(self): - with _test_eager_guard(): - self.func_test_main() - self.func_test_main() - if __name__ == '__main__': unittest.main() diff --git a/python/paddle/tests/test_dlpack.py b/python/paddle/tests/test_dlpack.py index 88a831be22..77ffdbeced 100644 --- a/python/paddle/tests/test_dlpack.py +++ b/python/paddle/tests/test_dlpack.py @@ -19,11 +19,10 @@ import numpy as np import paddle import paddle.fluid as fluid import paddle.fluid.core as core -from paddle.fluid.framework import _test_eager_guard class TestDLPack(unittest.TestCase): - def func_test_dlpack_dygraph(self): + def test_dlpack_dygraph(self): paddle.disable_static() tensor = paddle.to_tensor(np.array([1, 2, 3, 4]).astype('int')) dlpack = paddle.utils.dlpack.to_dlpack(tensor) @@ -38,12 +37,7 @@ class TestDLPack(unittest.TestCase): np.array(out_from_dlpack), np.array([1, 2, 3, 4]).astype('int') ) - def test_dlpack_dygraph(self): - with _test_eager_guard(): - self.func_test_dlpack_dygraph() - self.func_test_dlpack_dygraph() - - def func_test_dlpack_tensor_larger_than_2dim(self): + def test_dlpack_tensor_larger_than_2dim(self): paddle.disable_static() numpy_data = np.random.randn(4, 5, 6) t = paddle.to_tensor(numpy_data) @@ -52,11 +46,6 @@ class TestDLPack(unittest.TestCase): out = paddle.utils.dlpack.from_dlpack(dlpack) np.testing.assert_allclose(numpy_data, out.numpy(), rtol=1e-05) - def test_dlpack_tensor_larger_than_2dim(self): - with _test_eager_guard(): - self.func_test_dlpack_tensor_larger_than_2dim() - self.func_test_dlpack_tensor_larger_than_2dim() - def test_dlpack_static(self): paddle.enable_static() tensor = fluid.create_lod_tensor( @@ -87,7 +76,7 @@ class TestDLPack(unittest.TestCase): np.array([[1], [2], [3], [4]]).astype('int'), ) - def func_test_dlpack_dtype_conversion(self): + def test_dlpack_dtype_conversion(self): paddle.disable_static() # DLpack does not explicitly support bool data type. dtypes = [ @@ -119,11 +108,6 @@ class TestDLPack(unittest.TestCase): self.assertEqual(x.dtype, o.dtype) np.testing.assert_allclose(x.numpy(), o.numpy(), rtol=1e-05) - def test_dlpack_dtype_conversion(self): - with _test_eager_guard(): - self.func_test_dlpack_dtype_conversion() - self.func_test_dlpack_dtype_conversion() - def test_dlpack_deletion(self): # See Paddle issue 47171 if paddle.is_compiled_with_cuda(): @@ -134,23 +118,13 @@ class TestDLPack(unittest.TestCase): class TestRaiseError(unittest.TestCase): - def func_test_from_dlpack_raise_type_error(self): + def test_from_dlpack_raise_type_error(self): self.assertRaises( TypeError, paddle.utils.dlpack.from_dlpack, np.zeros(5) ) - def test_from_dlpack_raise_type_error(self): - with _test_eager_guard(): - self.func_test_from_dlpack_raise_type_error() - self.func_test_from_dlpack_raise_type_error() - - def func_test_to_dlpack_raise_type_error(self): - self.assertRaises(TypeError, paddle.utils.dlpack.to_dlpack, np.zeros(5)) - def test_to_dlpack_raise_type_error(self): - with _test_eager_guard(): - self.func_test_to_dlpack_raise_type_error() - self.func_test_to_dlpack_raise_type_error() + self.assertRaises(TypeError, paddle.utils.dlpack.to_dlpack, np.zeros(5)) if __name__ == '__main__': -- GitLab