dataloader_iter.py 25.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# Copyright (c) 2020 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 os
import six
import sys
import time
import signal
20
import numbers
21 22 23 24 25
import logging
import itertools
import threading
import numpy as np
import multiprocessing
26
from collections import namedtuple
27
from paddle.fluid.framework import _set_expected_place, _current_expected_place
28 29

# NOTE: queue has a different name in python2 and python3
T
tianshuo78520a 已提交
30
import queue
31

32 33
import paddle
from .. import core, layers
34
from ..framework import in_dygraph_mode
35
from ..multiprocess_utils import _set_SIGCHLD_handler, MP_STATUS_CHECK_INTERVAL, CleanupFuncRegistrar
36
from .fetcher import _IterableDatasetFetcher, _MapDatasetFetcher
37
from .batch_sampler import _InfiniteIterableSampler
38 39 40 41
from .collate import default_collate_fn, default_convert_fn
from .worker import ParentWatchDog, get_worker_info, _worker_loop, \
        _DatasetKind, _IterableDatasetStopIteration, _WorkerException
from .flat import _flatten_batch, _restore_batch
42 43

__all__ = ['get_worker_info']
44

45

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
class _DataLoaderIterBase(object):
    """
    Iterator implement of DataLoader, will load and feed mini-batch
    data by setting in given dataloader.

    Args:
        loader(instance of DataLoader): instance of `fluid.io.DataLoader`
    """

    def __init__(self, loader):
        self._dataset = loader.dataset
        self._feed_list = loader.feed_list or []
        self._places = loader.places
        self._return_list = loader.return_list
        self._batch_sampler = loader.batch_sampler
61
        self._auto_collate_batch = loader.auto_collate_batch
62 63 64
        self._num_workers = loader.num_workers
        self._use_buffer_reader = loader.use_buffer_reader
        self._use_shared_memory = loader.use_shared_memory
65
        self._timeout = loader.timeout if loader.timeout > 0 else MP_STATUS_CHECK_INTERVAL
66
        self._worker_init_fn = loader.worker_init_fn
67
        self._dataset_kind = loader.dataset_kind
68
        self._pin_memory = loader.pin_memory
69

70 71 72 73 74 75 76
        if self._auto_collate_batch:
            self._sampler_iter = iter(loader.batch_sampler)
            self._collate_fn = loader.collate_fn or default_collate_fn
        else:
            if self._dataset_kind == _DatasetKind.MAP:
                self._sampler_iter = iter(list(range(len(self._dataset))))
            else:
77 78
                self._sampler_iter = iter(
                    _InfiniteIterableSampler(self._dataset, 1))
79
            self._collate_fn = loader.collate_fn or default_convert_fn
80

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
        # LoDTensorBlockingQueue instance for create_py_reader and a thread
        # to put mini-batch data to self._blocking_queue, mini-batch data
        # will be get from:
        # 1. multi-process mode: get data from workers' result queue
        # 2. single-process mode: read mini-batch data in main process
        self._blocking_queue = None
        self._thread = None
        self._thread_done_event = threading.Event()

    def __iter__(self):
        return self

    def __len__(self):
        return len(self._batch_sampler)


class _DataLoaderIterSingleProcess(_DataLoaderIterBase):
    """
    Single process implement of DataLoaderIter, loading data from
    loader.data in main process
    """

    def __init__(self, loader):
        super(_DataLoaderIterSingleProcess, self).__init__(loader)

106
        self._dataset_fetcher = _DatasetKind.create_fetcher(
107 108
            self._dataset_kind, self._dataset, self._auto_collate_batch,
            self._collate_fn, True)
109

110 111 112 113 114 115 116 117
        # NOTE: _structrue_infos used to record the data structure of
        # batch to restore batch structure after reading Tensor
        # from blocking_queue in single-process mode. Note that
        # only single process is used in single-process mode, we
        # can record the data structure sequencely in a list without
        # recording the send and recv index
        self._structure_infos = []

118 119 120 121 122 123 124
        # NOTE: len(self._places) batch data compose as an output
        # iteration, set blocking_queue can cache 2 iteration datas
        # at most here
        self._blocking_queue_capacity = 2 * len(self._places)

        self._init_thread()

125 126 127 128 129 130 131
        # if user exit python program when dataloader is still
        # iterating, resource may no release safely, so we
        # add __del__ function to to CleanupFuncRegistrar
        # to make sure __del__ is always called when program
        # exit for resoure releasing safely
        CleanupFuncRegistrar.register(self.__del__)

132 133 134 135 136 137 138
    def _init_thread(self):
        self._var_names = [v.name for v in self._feed_list]
        self._shapes = [v.shape for v in self._feed_list]
        self._dtypes = [v.dtype for v in self._feed_list]
        self._need_check_feed = [
            v.desc.need_check_feed() for v in self._feed_list
        ]
139
        # if only 1 place, do not need to keep order
140
        self._blocking_queue = core.init_lod_tensor_blocking_queue(
141 142
            core.Variable(), self._blocking_queue_capacity,
            len(self._places) > 1)
143 144
        self._reader = core.create_py_reader(
            self._blocking_queue, self._var_names, self._shapes, self._dtypes,
145 146
            self._need_check_feed, self._places, self._use_buffer_reader, True,
            self._pin_memory)
147

148 149
        self._thread = threading.Thread(
            target=self._thread_loop, args=(_current_expected_place(), ))
150 151 152
        self._thread.daemon = True
        self._thread.start()

153
    def _thread_loop(self, legacy_expected_place):
154
        try:
155 156 157 158 159 160 161
            #NOTE(zhiqiu): Set the expected place for new thread as the same as father thread,
            # and it will call platform::SetDeviceId() in c++ internally.
            # If we do not set cudaDeviceId in new thread, the default cudaDeviceId will be 0,
            # Which may cost hundreds of MB of GPU memory on CUDAPlace(0) if calling some cuda 
            # APIs in this thread.
            _set_expected_place(legacy_expected_place)

162 163
            for indices in self._sampler_iter:
                # read data from dataset in mini-batch
164
                batch = self._dataset_fetcher.fetch(indices)
165

166 167 168 169
                # flat batch and record structure infos
                batch, structure = _flatten_batch(batch)
                self._structure_infos.append(structure)

170 171 172
                # pack as LoDTensorArray
                array = core.LoDTensorArray()
                for slot in batch:
K
Kaipeng Deng 已提交
173 174 175
                    if isinstance(slot, paddle.Tensor):
                        slot = slot.value().get_tensor()
                    elif not isinstance(slot, core.LoDTensor):
176 177 178 179 180 181 182 183 184
                        tmp = core.LoDTensor()
                        tmp.set(slot, core.CPUPlace())
                        slot = tmp

                    array.append(slot)

                if not self._blocking_queue.push(array):
                    break

185 186 187
                if self._thread_done_event.is_set():
                    break

188
            self._blocking_queue.close()
189
            self._shutdown_thread()
190 191
        except StopIteration:
            self._blocking_queue.close()
192 193
        except Exception:
            self._blocking_queue.kill()
194
            self._shutdown_thread()
195 196 197 198 199 200
            logging.warning("DataLoader reader thread raised an exception.")
            six.reraise(*sys.exc_info())

    def __next__(self):
        try:
            if in_dygraph_mode():
201 202
                data = self._reader.read_next_var_list()
                data = _restore_batch(data, self._structure_infos.pop(0))
203 204
            else:
                if self._return_list:
205 206 207 208 209 210 211 212
                    data = self._reader.read_next_list()
                    data = [
                        _restore_batch(d, s)
                        for d, s in zip(data, self._structure_infos[:len(
                            self._places)])
                    ]
                    self._structure_infos = self._structure_infos[len(
                        self._places):]
213 214 215 216
                    # static graph organized data on multi-device with list, if
                    # place number is 1, there is only 1 device, extra the data
                    # from list for devices to be compatible with dygraph mode
                    if len(self._places) == 1:
217
                        data = data[0]
218
                else:
219 220 221
                    data = self._reader.read_next()

            return data
222
        except StopIteration:
223
            self._reader.shutdown()
224 225
            six.reraise(*sys.exc_info())

226 227 228 229 230 231 232
    def _shutdown_thread(self):
        if self._thread:
            self._thread_done_event.set()
            if self._thread is not threading.current_thread():
                self._thread.join()
                self._thread = None

233 234 235 236
    # python2 compatibility
    def next(self):
        return self.__next__()

237 238 239 240 241
    def __del__(self):
        # _blocking_queue in keep order mode holds sub-threads
        # need to release thread resources on unexpected exit
        if self._blocking_queue:
            self._blocking_queue.close()
242 243 244 245
        # NOTE: blocking queue should be closed firstly for
        # blocking queue read may hang and _thread_done_event
        # cannot be checked
        self._shutdown_thread()
246

247 248 249 250 251 252 253 254 255 256 257 258 259

class _DataLoaderIterMultiProcess(_DataLoaderIterBase):
    def __init__(self, loader):
        super(_DataLoaderIterMultiProcess, self).__init__(loader)

        assert self._num_workers > 0,  "Multi-process DataLoader " \
                    "invalid num_workers({})".format(self._num_workers)

        # subprocess wrokers' result queue
        self._data_queue = None

        # data get from _data_queue will be reordered by _rcvd_idx
        # for data order keeping, data index not equal _rcvd_idx 
260
        # will be cached in _task_infos
261 262 263
        self._send_idx = 0
        self._rcvd_idx = 0
        self._batches_outstanding = 0
264
        self._task_infos = {}
265
        self._structure_infos = []
266 267 268 269 270 271 272 273 274 275

        # indices outstand as _outstanding_capacity at first, and
        # blocking_queue capacity is also _outstanding_capacity.
        # _outstanding_capacity here to make sure each indices_queue
        # has at least 2 indices, and outstanding batch cached
        # output data for at least 2 iterations(Note that len(_places)
        # batches will be composed as an iteration output)
        self._outstanding_capacity = 2 * max(self._num_workers,
                                             len(self._places))

276 277 278
        # see _try_put_indices
        self._thread_lock = threading.Lock()

279
        # init workers and indices queues and put 2 indices in each indices queue
280 281 282 283
        self._init_workers()
        for _ in range(self._outstanding_capacity):
            self._try_put_indices()

284 285 286
        self._init_thread()
        self._shutdown = False

287 288
        # if user exit python program when dataloader is still
        # iterating, resource may no release safely, so we
289 290
        # add _shutdown_on_exit function to to CleanupFuncRegistrar
        # to make sure _try_shutdown_all is always called when program
291
        # exit for resoure releasing safely
292 293 294 295 296
        # worker join may hang for in _try_shutdown_all call in atexit
        # for main process is in atexit state in some OS, so we add
        # timeout=1 for shutdown function call in atexit, for shutdown
        # function call in __del__, we keep it as it is
        CleanupFuncRegistrar.register(self._shutdown_on_exit)
297

298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
    def _init_workers(self):
        # multiprocess worker and indice queue list initial as empty
        self._workers = []
        self._worker_status = []
        self._indices_queues = []
        self._workers_idx_cycle = itertools.cycle(range(self._num_workers))

        # create data_queue for workers
        self._data_queue = multiprocessing.Queue()

        # event for workers and thread, thread event is only need 
        # in multi-processing mode
        self._workers_done_event = multiprocessing.Event()
        self._thread_done_event = threading.Event()

        for i in range(self._num_workers):
            indices_queue = multiprocessing.Queue()
            self._indices_queues.append(indices_queue)
            worker = multiprocessing.Process(
317
                target=_worker_loop,
318 319
                args=(self._dataset, self._dataset_kind, indices_queue,
                      self._data_queue, self._workers_done_event,
320 321 322
                      self._auto_collate_batch, self._collate_fn,
                      self._worker_init_fn, i, self._num_workers,
                      self._use_shared_memory))
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
            worker.daemon = True
            worker.start()
            self._workers.append(worker)
            self._worker_status.append(True)

        core._set_process_pids(id(self), tuple(w.pid for w in self._workers))
        _set_SIGCHLD_handler()

    def _clear_and_remove_data_queue(self):
        if self._data_queue is not None:
            while True:
                try:
                    self._data_queue.get_nowait()
                except:
                    self._data_queue.cancel_join_thread()
                    self._data_queue.close()
                    break

    def _init_thread(self):
        self._var_names = [v.name for v in self._feed_list]
        self._shapes = [v.shape for v in self._feed_list]
        self._dtypes = [v.dtype for v in self._feed_list]
        self._need_check_feed = [
            v.desc.need_check_feed() for v in self._feed_list
        ]
348
        # if only 1 place, do not need to keep order
349
        self._blocking_queue = core.init_lod_tensor_blocking_queue(
350
            core.Variable(), self._outstanding_capacity, len(self._places) > 1)
351 352
        self._reader = core.create_py_reader(
            self._blocking_queue, self._var_names, self._shapes, self._dtypes,
353 354
            self._need_check_feed, self._places, self._use_buffer_reader, True,
            self._pin_memory)
355 356

        self._thread_done_event = threading.Event()
357 358
        self._thread = threading.Thread(
            target=self._thread_loop, args=(_current_expected_place(), ))
359 360 361 362 363 364 365 366
        self._thread.daemon = True
        self._thread.start()

    def _shutdown_worker(self, worker_id):
        if self._worker_status[worker_id]:
            self._indices_queues[worker_id].put(None)
            self._worker_status[worker_id] = False

367
    def _try_shutdown_all(self, timeout=None):
368 369 370 371 372 373 374 375 376 377 378 379
        if not self._shutdown:
            try:
                self._exit_thread_expectedly()
                self._clear_and_remove_data_queue()

                # set _workers_done_event should be set before put None
                # to indices_queue, workers wll exit on reading None from
                # indices_queue
                self._workers_done_event.set()
                for i in range(self._num_workers):
                    self._shutdown_worker(i)

380 381 382 383 384 385
                if not self._shutdown:
                    for w in self._workers:
                        w.join(timeout)
                    for q in self._indices_queues:
                        q.cancel_join_thread()
                        q.close()
386 387 388 389 390 391 392 393 394 395 396 397 398
            finally:
                core._erase_process_pids(id(self))
                self._shutdown = True

    def _exit_thread_expectedly(self):
        self._thread_done_event.set()
        self._blocking_queue.close()

    def _exit_thread_unexpectedly(self):
        self._thread_done_event.set()
        self._blocking_queue.kill()
        logging.error("DataLoader reader thread raised an exception!")

399 400 401 402 403 404 405 406
    def _thread_loop(self, legacy_expected_place):
        #NOTE(zhiqiu): Set the expected place for new thread as the same as father thread,
        # and it will call platform::SetDeviceId() in c++ internally.
        # If we do not set cudaDeviceId in new thread, the default cudaDeviceId will be 0,
        # Which may cost hundreds of MB of GPU memory on CUDAPlace(0) if calling some cuda 
        # APIs in this thread.
        _set_expected_place(legacy_expected_place)

407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
        while not self._thread_done_event.is_set():
            batch = self._get_data()
            if not self._thread_done_event.is_set():
                if batch is None:
                    self._exit_thread_expectedly()
                else:
                    try:
                        # pack as LoDTensorArray
                        array = core.LoDTensorArray()
                        if self._use_shared_memory:
                            for tensor in batch:
                                array.append(tensor)
                        else:
                            # LoDTensor not in shared memory is not
                            # serializable, cannot be create in workers
                            for slot in batch:
K
Kaipeng Deng 已提交
423 424 425
                                if isinstance(slot, paddle.Tensor):
                                    slot = slot.value().get_tensor()
                                elif not isinstance(slot, core.LoDTensor):
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
                                    tmp = core.LoDTensor()
                                    tmp.set(slot, core.CPUPlace())
                                    slot = tmp
                                array.append(slot)

                        if not self._blocking_queue.push(array):
                            self._blocking_queue.close()
                    except:
                        self._exit_thread_unexpectedly()
                        six.reraise(*sys.exc_info())
                    finally:
                        self._rcvd_idx += 1

    def _get_data(self):
        while not self._thread_done_event.is_set():
441 442 443 444 445 446
            # For IterableDataset, batch indices is generated infinitely
            # for each worker to raise StopIteration, but a StopIteration
            # raising process will discard a batch indices which is count
            # in _send_idx but will not increase _rcvd_idx, so we check 
            # whether the worker is still alive here to skip the discarded
            # batch indices and increase _rcvd_idx
447 448
            if self._dataset_kind == _DatasetKind.ITER:
                while self._rcvd_idx < self._send_idx:
449
                    sys.stdout.flush()
450
                    info = self._task_infos[self._rcvd_idx]
451
                    if len(info) == 3 or self._worker_status[info[0]]:
452 453 454 455 456 457 458 459 460 461 462 463 464
                        break
                    del self._task_infos[self._rcvd_idx]
                    self._rcvd_idx += 1
                    self._batches_outstanding -= 1
                else:
                    # NOTE: _rcvd_idx and _send_idx only record batches among
                    #       workers, if batches among workers drained, there
                    #       may also be data in blocking queue
                    if self._batches_outstanding < len(self._places):
                        return None
                    continue

            if self._rcvd_idx in self._task_infos and \
465 466 467 468
                    len(self._task_infos[self._rcvd_idx]) == 3:
                info = self._task_infos.pop(self._rcvd_idx)
                self._structure_infos.append(info[2])
                return info[1]
469

470 471 472
            try:
                # [ avoid hang ]: main process may blocking at _reader.read_next when
                # KeyboardInterrupt, we do following tradeoff:
473
                # 1. get data with timeout, MP_STATUS_CHECK_INTERVAL(5s) as timeout
474 475 476 477 478 479 480
                #    default, if KeyboardInterrupt blocking, failed workers will be
                #    checked and raise RuntimeError to quit DataLoader in timeout
                #    exception handling.
                # 2. if get data timeout and check workers all alive, continue to
                #    get data again
                data = self._data_queue.get(timeout=self._timeout)
            except Exception as e:
481 482 483 484 485
                # check if thread done event set when waiting data
                if self._thread_done_event.is_set():
                    continue

                # check failed workers
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
                failed_workers = []
                for i, w in enumerate(self._workers):
                    if self._worker_status[i] and not w.is_alive():
                        failed_workers.append(w)
                        self._shutdown_worker(i)
                if len(failed_workers) > 0:
                    self._exit_thread_unexpectedly()
                    pids = ', '.join(str(w.pid) for w in failed_workers)
                    raise RuntimeError("DataLoader {} workers exit unexpectedly, " \
                                "pids: {}".format(len(failed_workers), pids))

                # get(timeout) will call _poll(timeout) and may raise IOError
                if isinstance(e, queue.Empty) or isinstance(e, IOError):
                    # continue on timeout to keep getting data from queue
                    continue

                self._exit_thread_unexpectedly()
                logging.error("DataLoader reader thread failed({}) to read data from " \
                              "workers' result queue.".format(e))
                six.reraise(*sys.exc_info())
            else:
507 508 509 510 511 512 513 514 515 516 517 518
                if self._dataset_kind == _DatasetKind.ITER and isinstance(
                        data, _IterableDatasetStopIteration):
                    # if a worker get StopIteraion, we shutdown this worker,
                    # note that this batch indices to trigger StopIteration
                    # is discard, outstanding batch number should be decrease
                    # and another indices should be put for other workers
                    # may still working.
                    self._shutdown_worker(data.worker_id)
                    self._batches_outstanding -= 1
                    self._try_put_indices()
                    continue

519 520 521 522 523
                idx, batch, structure = data
                if isinstance(batch, _WorkerException):
                    self._exit_thread_unexpectedly()
                    batch.reraise()

524
                if idx == self._rcvd_idx:
525
                    del self._task_infos[idx]
526
                    self._structure_infos.append(structure)
527 528
                    return batch
                else:
529
                    self._task_infos[idx] += (batch, structure)
530 531 532
                    continue

    def _try_put_indices(self):
533
        assert self._batches_outstanding <= self._outstanding_capacity, \
534
                    "too many indices have been put to queue"
535 536 537 538 539 540 541 542 543 544 545 546 547 548
        # In multi-process mode for IterableDataset, _try_put_indices will
        # be called both in main process(for our implement has blocking queue,
        # and blocking queue read is in main process) and thread, which may
        # cause error following error
        #   1. "ValueError: generator already executing" in next(self._sampler_iter)
        #   2. re-enter in increase _send_idx
        # add a lock for threading save, for _try_put_indices is only a slight
        # function which is not in data reading pipeline, this lock almost no
        # influence on performance
        with self._thread_lock:
            try:
                indices = next(self._sampler_iter)
            except StopIteration:
                return
549

550 551 552 553 554 555
            for i in range(self._num_workers):
                worker_idx = next(self._workers_idx_cycle)
                if self._worker_status[worker_idx]:
                    break
            else:
                return
556

557 558 559 560
            self._indices_queues[worker_idx].put((self._send_idx, indices))
            self._task_infos[self._send_idx] = (worker_idx, )
            self._batches_outstanding += 1
            self._send_idx += 1
561 562 563 564

    def __del__(self):
        self._try_shutdown_all()

565 566 567
    def _shutdown_on_exit(self):
        self._try_shutdown_all(1)

568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
    def __next__(self):
        try:
            # _batches_outstanding here record the total batch data number
            # in 'from after _try_put_indices to beforeoutput data', this
            # value should be _outstanding_capacity if data is not drained,
            # if _batches_outstanding is less than _places number, there are
            # no enough data to generate next output, close blocking_queue and
            # set _thread_done_event here, py_reader will raise StopIteration,
            # end workers and indices_queues in StopIteration handling
            if self._batches_outstanding < len(self._places):
                self._thread_done_event.set()
                self._blocking_queue.close()

            if in_dygraph_mode():
                data = self._reader.read_next_var_list()
583
                data = _restore_batch(data, self._structure_infos.pop(0))
584 585 586
            else:
                if self._return_list:
                    data = self._reader.read_next_list()
587 588 589 590 591 592 593
                    data = [
                        _restore_batch(d, s)
                        for d, s in zip(data, self._structure_infos[:len(
                            self._places)])
                    ]
                    self._structure_infos = self._structure_infos[len(
                        self._places):]
594 595 596 597 598 599 600 601 602 603
                    # static graph organized data on multi-device with list, if
                    # place number is 1, there is only 1 device, extra the data
                    # from list for devices to be compatible with dygraph mode
                    if len(self._places) == 1:
                        data = data[0]
                else:
                    data = self._reader.read_next()
            self._on_output_batch()
            return data
        except StopIteration:
604
            self._reader.shutdown()
605 606 607 608 609 610 611 612 613 614 615
            self._try_shutdown_all()
            six.reraise(*sys.exc_info())

    # python2 compatibility
    def next(self):
        return self.__next__()

    def _on_output_batch(self):
        for _ in range(len(self._places)):
            self._batches_outstanding -= 1
            self._try_put_indices()