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

rm _test_eager_guard #48767

Merged
merged 1 commit into from
Dec 7, 2022
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
64 changes: 15 additions & 49 deletions python/paddle/tests/test_async_read_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
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):
Expand All @@ -40,24 +39,14 @@ def func_setUp(self):

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)]

Expand All @@ -71,14 +60,9 @@ def func_test_async_read_success(self):
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]
Expand All @@ -101,26 +85,14 @@ def func_test_async_read_only_1dim(self):
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()
Expand All @@ -145,10 +117,7 @@ def func_test_async_write_success(self):
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)))
Expand All @@ -158,9 +127,6 @@ def func_test_async_write_success(self):
)

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()

Expand Down
15 changes: 2 additions & 13 deletions python/paddle/tests/test_callback_reduce_lr_on_plateau.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
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
Expand All @@ -32,7 +31,7 @@ def __len__(self):


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)
Expand All @@ -57,12 +56,7 @@ def func_reduce_lr_on_plateau(self):
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
Expand Down Expand Up @@ -113,11 +107,6 @@ def func_warn_or_error(self):
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()
8 changes: 1 addition & 7 deletions python/paddle/tests/test_callback_visualdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

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

Expand All @@ -35,7 +34,7 @@ def setUp(self):
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')]

Expand All @@ -58,11 +57,6 @@ def func_visualdl_callback(self):
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()
85 changes: 12 additions & 73 deletions python/paddle/tests/test_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

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,
Expand All @@ -47,7 +46,7 @@ def setUp(self):
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:
Expand All @@ -60,12 +59,7 @@ def func_test_dataset(self):
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:
Expand All @@ -77,12 +71,7 @@ def func_test_folder(self):

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

Expand All @@ -96,12 +85,7 @@ def fake_transform(img):
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):
Expand All @@ -110,14 +94,9 @@ def func_test_errors(self):
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)
Expand All @@ -130,14 +109,9 @@ def func_test_main(self):
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)
Expand Down Expand Up @@ -166,14 +140,9 @@ def func_test_main(self):
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)
Expand All @@ -186,14 +155,9 @@ def func_test_main(self):
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)
Expand Down Expand Up @@ -222,26 +186,16 @@ def func_test_main(self):
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)

Expand All @@ -254,14 +208,9 @@ def func_test_main(self):
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)

Expand All @@ -274,14 +223,9 @@ def func_test_main(self):
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)

Expand Down Expand Up @@ -310,11 +254,6 @@ def func_test_main(self):
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()
Loading