MCPcopy Create free account
hub / github.com/hankcs/HanLP / __init__

Method __init__

hanlp/common/dataset.py:553–581  ·  view source on GitHub ↗

A dataloader wrapper which speeds up bachifying using multi-processing. It works best for dataloaders of which the bachify takes very long time. But it introduces extra GPU memory consumption since prefetched batches are stored in a ``Queue`` on GPU. .. Caution::

(self, dataloader: torch.utils.data.DataLoader, prefetch: int = 10, batchify: Callable = None)

Source from the content-addressed store, hash-verified

551
552class PrefetchDataLoader(DataLoader):
553 def __init__(self, dataloader: torch.utils.data.DataLoader, prefetch: int = 10, batchify: Callable = None) -> None:
554 """ A dataloader wrapper which speeds up bachifying using multi-processing. It works best for dataloaders
555 of which the bachify takes very long time. But it introduces extra GPU memory consumption since prefetched
556 batches are stored in a ``Queue`` on GPU.
557
558 .. Caution::
559
560 PrefetchDataLoader only works in spawn mode with the following initialization code:
561
562 Examples::
563
564 if __name__ == '__main__':
565 import torch
566
567 torch.multiprocessing.set_start_method('spawn')
568
569 And these 2 lines **MUST** be put into ``if __name__ == '__main__':`` block.
570
571 Args:
572 dataloader: A :class:`~torch.utils.data.DatasetLoader` to be prefetched.
573 prefetch: Number of batches to prefetch.
574 batchify: A bachify function called on each batch of samples. In which case, the inner dataloader shall
575 return samples without really bachify them.
576 """
577 super().__init__(dataset=dataloader)
578 self._batchify = batchify
579 self.prefetch = None if isdebugging() else prefetch
580 if self.prefetch:
581 self._fire_process(dataloader, prefetch)
582
583 def _fire_process(self, dataloader, prefetch):
584 self.queue = mp.Queue(prefetch)

Callers

nothing calls this directly

Calls 3

_fire_processMethod · 0.95
isdebuggingFunction · 0.90
__init__Method · 0.45

Tested by

no test coverage detected