Skip to content

Commit 3f4687b

Browse files
authored
Try to fix data loader test failed problem (#22622)
* split unittests in data loader test, test=release/1.7 * split unittests to different files, test=release/1.7 * remove repeat unittest, test=release/1.7
1 parent 926227c commit 3f4687b

File tree

3 files changed

+85
-88
lines changed

3 files changed

+85
-88
lines changed

python/paddle/fluid/tests/unittests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ if (APPLE OR WIN32)
195195
list(REMOVE_ITEM TEST_OPS test_dataset)
196196
list(REMOVE_ITEM TEST_OPS test_dataset_dataloader)
197197
list(REMOVE_ITEM TEST_OPS test_imperative_data_loader)
198+
list(REMOVE_ITEM TEST_OPS test_imperative_data_loader_exception)
198199
list(REMOVE_ITEM TEST_OPS test_imperative_data_loader_process)
199200
list(REMOVE_ITEM TEST_OPS test_imperative_signal_handler)
200201
endif()

python/paddle/fluid/tests/unittests/test_imperative_data_loader.py

Lines changed: 2 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import numpy as np
1818
import paddle.fluid as fluid
1919
from paddle.fluid import core
20-
import paddle.compat as cpt
2120

2221

2322
def get_random_images_and_labels(image_shape, label_shape):
@@ -35,19 +34,6 @@ def __reader__():
3534
return __reader__
3635

3736

38-
def sample_list_generator_creator(batch_size, batch_num):
39-
def __reader__():
40-
for _ in range(batch_num):
41-
sample_list = []
42-
for _ in range(batch_size):
43-
image, label = get_random_images_and_labels([784], [1])
44-
sample_list.append([image, label])
45-
46-
yield sample_list
47-
48-
return __reader__
49-
50-
5137
def batch_generator_creator(batch_size, batch_num):
5238
def __reader__():
5339
for _ in range(batch_num):
@@ -62,8 +48,8 @@ class TestDygraphhDataLoader(unittest.TestCase):
6248
def setUp(self):
6349
self.batch_size = 8
6450
self.batch_num = 4
65-
self.epoch_num = 2
66-
self.capacity = 2
51+
self.epoch_num = 1
52+
self.capacity = 5
6753

6854
def test_single_process_reader(self):
6955
with fluid.dygraph.guard():
@@ -95,20 +81,6 @@ def test_sample_genarator(self):
9581
self.assertEqual(label.shape, [self.batch_size, 1])
9682
self.assertEqual(relu.shape, [self.batch_size, 784])
9783

98-
def test_sample_list_generator(self):
99-
with fluid.dygraph.guard():
100-
loader = fluid.io.DataLoader.from_generator(
101-
capacity=self.capacity, use_multiprocess=True)
102-
loader.set_sample_list_generator(
103-
sample_list_generator_creator(self.batch_size, self.batch_num),
104-
places=fluid.CPUPlace())
105-
for _ in range(self.epoch_num):
106-
for image, label in loader():
107-
relu = fluid.layers.relu(image)
108-
self.assertEqual(image.shape, [self.batch_size, 784])
109-
self.assertEqual(label.shape, [self.batch_size, 1])
110-
self.assertEqual(relu.shape, [self.batch_size, 784])
111-
11284
def test_batch_genarator(self):
11385
with fluid.dygraph.guard():
11486
loader = fluid.io.DataLoader.from_generator(
@@ -124,63 +96,5 @@ def test_batch_genarator(self):
12496
self.assertEqual(relu.shape, [self.batch_size, 784])
12597

12698

127-
class TestDygraphhDataLoaderWithException(unittest.TestCase):
128-
def setUp(self):
129-
self.batch_num = 4
130-
self.capacity = 2
131-
132-
def test_not_capacity(self):
133-
with fluid.dygraph.guard():
134-
with self.assertRaisesRegexp(ValueError,
135-
"Please give value to capacity."):
136-
fluid.io.DataLoader.from_generator()
137-
138-
def test_single_process_with_thread_expection(self):
139-
def error_sample_genarator(batch_num):
140-
def __reader__():
141-
for _ in range(batch_num):
142-
yield [[[1, 2], [1]]]
143-
144-
return __reader__
145-
146-
with fluid.dygraph.guard():
147-
loader = fluid.io.DataLoader.from_generator(
148-
capacity=self.capacity, iterable=False, use_multiprocess=False)
149-
loader.set_batch_generator(
150-
error_sample_genarator(self.batch_num), places=fluid.CPUPlace())
151-
exception = None
152-
try:
153-
for _ in loader():
154-
print("test_single_process_with_thread_expection")
155-
except core.EnforceNotMet as ex:
156-
self.assertIn("Blocking queue is killed",
157-
cpt.get_exception_message(ex))
158-
exception = ex
159-
self.assertIsNotNone(exception)
160-
161-
def test_multi_process_with_thread_expection(self):
162-
def error_sample_genarator(batch_num):
163-
def __reader__():
164-
for _ in range(batch_num):
165-
yield [[[1, 2], [1]]]
166-
167-
return __reader__
168-
169-
with fluid.dygraph.guard():
170-
loader = fluid.io.DataLoader.from_generator(
171-
capacity=self.capacity, use_multiprocess=True)
172-
loader.set_batch_generator(
173-
error_sample_genarator(self.batch_num), places=fluid.CPUPlace())
174-
exception = None
175-
try:
176-
for _ in loader():
177-
print("test_multi_process_with_thread_expection")
178-
except core.EnforceNotMet as ex:
179-
self.assertIn("Blocking queue is killed",
180-
cpt.get_exception_message(ex))
181-
exception = ex
182-
self.assertIsNotNone(exception)
183-
184-
18599
if __name__ == '__main__':
186100
unittest.main()
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import sys
16+
import unittest
17+
import numpy as np
18+
import paddle.fluid as fluid
19+
from paddle.fluid import core
20+
import paddle.compat as cpt
21+
22+
23+
class TestDygraphhDataLoaderWithException(unittest.TestCase):
24+
def setUp(self):
25+
self.batch_num = 4
26+
self.capacity = 2
27+
28+
def test_not_capacity(self):
29+
with fluid.dygraph.guard():
30+
with self.assertRaisesRegexp(ValueError,
31+
"Please give value to capacity."):
32+
fluid.io.DataLoader.from_generator()
33+
34+
def test_single_process_with_thread_expection(self):
35+
def error_sample_genarator(batch_num):
36+
def __reader__():
37+
for _ in range(batch_num):
38+
yield [[[1, 2], [1]]]
39+
40+
return __reader__
41+
42+
with fluid.dygraph.guard():
43+
loader = fluid.io.DataLoader.from_generator(
44+
capacity=self.capacity, iterable=False, use_multiprocess=False)
45+
loader.set_batch_generator(
46+
error_sample_genarator(self.batch_num), places=fluid.CPUPlace())
47+
exception = None
48+
try:
49+
for _ in loader():
50+
print("test_single_process_with_thread_expection")
51+
except core.EnforceNotMet as ex:
52+
self.assertIn("Blocking queue is killed",
53+
cpt.get_exception_message(ex))
54+
exception = ex
55+
self.assertIsNotNone(exception)
56+
57+
def test_multi_process_with_thread_expection(self):
58+
def error_sample_genarator(batch_num):
59+
def __reader__():
60+
for _ in range(batch_num):
61+
yield [[[1, 2], [1]]]
62+
63+
return __reader__
64+
65+
with fluid.dygraph.guard():
66+
loader = fluid.io.DataLoader.from_generator(
67+
capacity=self.capacity, use_multiprocess=True)
68+
loader.set_batch_generator(
69+
error_sample_genarator(self.batch_num), places=fluid.CPUPlace())
70+
exception = None
71+
try:
72+
for _ in loader():
73+
print("test_multi_process_with_thread_expection")
74+
except core.EnforceNotMet as ex:
75+
self.assertIn("Blocking queue is killed",
76+
cpt.get_exception_message(ex))
77+
exception = ex
78+
self.assertIsNotNone(exception)
79+
80+
81+
if __name__ == '__main__':
82+
unittest.main()

0 commit comments

Comments
 (0)