Build PyTorch DataLoader. In distributed training, each GPU/process has a dataloader. In non-distributed training, there is only one dataloader for all GPUs. Args: dataset (:obj:`Dataset`): A PyTorch dataset. samples_per_gpu (int): Number of training samples on each GPU
(dataset: Dataset,
samples_per_gpu: int,
workers_per_gpu: int,
num_gpus: Optional[int] = 1,
dist: Optional[bool] = True,
shuffle: Optional[bool] = True,
round_up: Optional[bool] = True,
seed: Optional[Union[int, None]] = None,
persistent_workers: Optional[bool] = True,
**kwargs)
| 44 | |
| 45 | |
| 46 | def build_dataloader(dataset: Dataset, |
| 47 | samples_per_gpu: int, |
| 48 | workers_per_gpu: int, |
| 49 | num_gpus: Optional[int] = 1, |
| 50 | dist: Optional[bool] = True, |
| 51 | shuffle: Optional[bool] = True, |
| 52 | round_up: Optional[bool] = True, |
| 53 | seed: Optional[Union[int, None]] = None, |
| 54 | persistent_workers: Optional[bool] = True, |
| 55 | **kwargs): |
| 56 | """Build PyTorch DataLoader. |
| 57 | |
| 58 | In distributed training, each GPU/process has a dataloader. |
| 59 | In non-distributed training, there is only one dataloader for all GPUs. |
| 60 | |
| 61 | Args: |
| 62 | dataset (:obj:`Dataset`): A PyTorch dataset. |
| 63 | samples_per_gpu (int): Number of training samples on each GPU, i.e., |
| 64 | batch size of each GPU. |
| 65 | workers_per_gpu (int): How many subprocesses to use for data loading |
| 66 | for each GPU. |
| 67 | num_gpus (int, optional): Number of GPUs. Only used in non-distributed |
| 68 | training. |
| 69 | dist (bool, optional): Distributed training/test or not. Default: True. |
| 70 | shuffle (bool, optional): Whether to shuffle the data at every epoch. |
| 71 | Default: True. |
| 72 | round_up (bool, optional): Whether to round up the length of dataset by |
| 73 | adding extra samples to make it evenly divisible. Default: True. |
| 74 | persistent_workers (bool): If True, the data loader will not shutdown |
| 75 | the worker processes after a dataset has been consumed once. |
| 76 | This allows to maintain the workers Dataset instances alive. |
| 77 | The argument also has effect in PyTorch>=1.7.0. |
| 78 | Default: True |
| 79 | kwargs: any keyword argument to be used to initialize DataLoader |
| 80 | |
| 81 | Returns: |
| 82 | DataLoader: A PyTorch dataloader. |
| 83 | """ |
| 84 | rank, world_size = get_dist_info() |
| 85 | if dist: |
| 86 | sampler = DistributedSampler(dataset, |
| 87 | world_size, |
| 88 | rank, |
| 89 | shuffle=shuffle, |
| 90 | round_up=round_up) |
| 91 | shuffle = False |
| 92 | batch_size = samples_per_gpu |
| 93 | num_workers = workers_per_gpu |
| 94 | else: |
| 95 | sampler = None |
| 96 | batch_size = num_gpus * samples_per_gpu |
| 97 | num_workers = num_gpus * workers_per_gpu |
| 98 | |
| 99 | init_fn = partial( |
| 100 | worker_init_fn, num_workers=num_workers, rank=rank, |
| 101 | seed=seed) if seed is not None else None |
| 102 | |
| 103 | data_loader = DataLoader(dataset, |
no test coverage detected