test_multiprocess_reader_exception.py 5.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import paddle.fluid as fluid
from paddle.fluid.io import multiprocess_reader
import unittest
import numpy as np
import six
import sys


Z
Zeng Jinle 已提交
23 24 25 26
class ReaderException(Exception):
    pass


27 28 29 30 31 32 33 34 35 36 37
class TestMultiprocessReaderException(unittest.TestCase):
    def setUp(self):
        self.use_pipe = False
        self.raise_exception = False

    def places(self):
        if fluid.is_compiled_with_cuda():
            return [fluid.CPUPlace(), fluid.CUDAPlace(0)]
        else:
            return [fluid.CPUPlace()]

Z
Zeng Jinle 已提交
38 39 40 41
    def main_impl(self, place, iterable, use_legacy_py_reader):
        sample_num = 40
        batch_size = 4

42 43
        def fake_reader():
            def __impl__():
Z
Zeng Jinle 已提交
44
                for _ in range(sample_num):
45 46 47 48 49 50 51 52 53 54
                    if not self.raise_exception:
                        yield list(
                            np.random.uniform(
                                low=-1, high=1, size=[10])),
                    else:
                        raise ValueError()

            return __impl__

        with fluid.program_guard(fluid.Program(), fluid.Program()):
Z
Zeng Jinle 已提交
55 56 57
            if not use_legacy_py_reader:
                image = fluid.data(
                    name='image', dtype='float32', shape=[None, 10])
58

Z
Zeng Jinle 已提交
59 60 61 62 63 64
                reader = fluid.io.PyReader(
                    feed_list=[image], capacity=2, iterable=iterable)
            else:
                reader = fluid.layers.py_reader(
                    capacity=2, shapes=[[-1, 10], ], dtypes=['float32', ])
                image = fluid.layers.read_file(reader)
65 66 67 68 69 70

            image_p_1 = image + 1

            decorated_reader = multiprocess_reader(
                [fake_reader(), fake_reader()], use_pipe=self.use_pipe)

Z
Zeng Jinle 已提交
71 72 73 74
            if use_legacy_py_reader:
                reader.decorate_paddle_reader(
                    fluid.io.batch(
                        decorated_reader, batch_size=batch_size))
75
            else:
Z
Zeng Jinle 已提交
76 77 78 79
                if isinstance(place, fluid.CUDAPlace):
                    reader.decorate_sample_generator(
                        decorated_reader,
                        batch_size=batch_size,
80
                        places=fluid.cuda_places(0))
Z
Zeng Jinle 已提交
81 82 83 84 85
                else:
                    reader.decorate_sample_generator(
                        decorated_reader,
                        batch_size=batch_size,
                        places=fluid.cpu_places())
86 87 88 89

            exe = fluid.Executor(place)
            exe.run(fluid.default_startup_program())

Z
Zeng Jinle 已提交
90 91
            batch_num = int(sample_num * 2 / batch_size)

92 93 94
            if iterable:
                for _ in range(3):
                    num = 0
Z
Zeng Jinle 已提交
95 96 97 98 99 100
                    try:
                        for data in reader():
                            exe.run(feed=data, fetch_list=[image_p_1])
                            num += 1
                        self.assertEquals(num, batch_num)
                    except fluid.core.EnforceNotMet as ex:
101
                        self.assertEquals(num, 0)
Z
Zeng Jinle 已提交
102
                        raise ReaderException()
103 104 105 106 107 108 109 110 111 112
            else:
                for _ in range(3):
                    num = 0
                    reader.start()
                    try:
                        while True:
                            exe.run(fetch_list=[image_p_1])
                            num += 1
                    except fluid.core.EOFException:
                        reader.reset()
Z
Zeng Jinle 已提交
113 114 115 116 117 118
                        self.assertFalse(self.raise_exception)
                        self.assertEquals(num, batch_num)
                    except fluid.core.EnforceNotMet as ex:
                        self.assertTrue(self.raise_exception)
                        self.assertEquals(num, 0)
                        raise ReaderException()
119 120 121 122

    def test_main(self):
        for p in self.places():
            for iterable in [False, True]:
Z
Zeng Jinle 已提交
123 124 125 126 127 128
                use_legacy_py_reader_range = [False
                                              ] if iterable else [False, True]
                for use_legacy_py_reader in use_legacy_py_reader_range:
                    try:
                        with fluid.scope_guard(fluid.Scope()):
                            self.main_impl(p, iterable, use_legacy_py_reader)
129

Z
Zeng Jinle 已提交
130 131 132
                        self.assertTrue(not self.raise_exception)
                    except ReaderException:
                        self.assertTrue(self.raise_exception)
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154


class TestCase1(TestMultiprocessReaderException):
    def setUp(self):
        self.use_pipe = False
        self.raise_exception = True


class TestCase2(TestMultiprocessReaderException):
    def setUp(self):
        self.use_pipe = True
        self.raise_exception = False


class TestCase3(TestMultiprocessReaderException):
    def setUp(self):
        self.use_pipe = True
        self.raise_exception = True


if __name__ == '__main__':
    unittest.main()