|
13 | 13 | # limitations under the License.
|
14 | 14 | import inspect
|
15 | 15 | import multiprocessing
|
16 |
| -import platform |
17 | 16 | from abc import ABC
|
18 | 17 | from copy import deepcopy
|
19 | 18 | from typing import Iterable, List, Tuple, Union
|
@@ -54,53 +53,53 @@ class TrainerDataLoadingMixin(ABC):
|
54 | 53 | dev_debugger: InternalDebugger
|
55 | 54 |
|
56 | 55 | def _worker_check(self, dataloader: DataLoader, name: str) -> None:
|
57 |
| - on_windows = platform.system() == 'Windows' |
| 56 | + if not isinstance(dataloader, DataLoader): |
| 57 | + return |
58 | 58 |
|
59 |
| - # ddp_spawn + num_workers > 0 don't mix! tell the user |
60 |
| - is_dataloader = isinstance(dataloader, DataLoader) |
61 | 59 | using_spawn = self.accelerator_connector.distributed_backend == "ddp_spawn"
|
62 |
| - if is_dataloader and not on_windows: |
63 |
| - if dataloader.num_workers > 0 and using_spawn: |
64 |
| - # checks for the attr persistent_workers available in pytorch >= 1.7 |
65 |
| - if hasattr(dataloader, "persistent_workers"): |
66 |
| - if not dataloader.persistent_workers: |
67 |
| - rank_zero_warn( |
68 |
| - 'num_workers>0, persistent_workers=False, and accelerator=ddp_spawn' |
69 |
| - ' may result in data loading bottlenecks.' |
70 |
| - ' Consider setting persistent_workers=True' |
71 |
| - ' (this is a limitation of Python .spawn() and PyTorch)' |
72 |
| - ) |
73 |
| - else: |
| 60 | + num_cpus = multiprocessing.cpu_count() |
| 61 | + |
| 62 | + # ddp_spawn + num_workers > 0 don't mix! tell the user |
| 63 | + if dataloader.num_workers > 0 and using_spawn: |
| 64 | + # checks for the attr persistent_workers available in pytorch >= 1.7 |
| 65 | + if hasattr(dataloader, "persistent_workers"): |
| 66 | + if not dataloader.persistent_workers: |
74 | 67 | rank_zero_warn(
|
75 |
| - 'num_workers>0 and accelerator=ddp_spawn do not mix well' |
76 |
| - ' and may result in data loading bottlenecks.' |
77 |
| - ' Consider setting accelerator=ddp to use num_workers>0' |
| 68 | + 'num_workers>0, persistent_workers=False, and accelerator=ddp_spawn' |
| 69 | + ' may result in data loading bottlenecks.' |
| 70 | + ' Consider setting persistent_workers=True' |
78 | 71 | ' (this is a limitation of Python .spawn() and PyTorch)'
|
79 | 72 | )
|
| 73 | + else: |
| 74 | + rank_zero_warn( |
| 75 | + 'num_workers>0 and accelerator=ddp_spawn do not mix well' |
| 76 | + ' and may result in data loading bottlenecks.' |
| 77 | + ' Consider setting accelerator=ddp to use num_workers>0' |
| 78 | + ' (this is a limitation of Python .spawn() and PyTorch)' |
| 79 | + ) |
80 | 80 |
|
81 |
| - elif dataloader.num_workers == 0 and using_spawn: |
82 |
| - # checks for the attr persistent_workers available in pytorch >= 1.7 |
83 |
| - if hasattr(dataloader, "persistent_workers"): |
84 |
| - if not dataloader.persistent_workers: |
85 |
| - rank_zero_warn( |
86 |
| - 'accelerator=ddp_spawn and num_workers=0 may result in data loading bottlenecks.' |
87 |
| - ' Consider setting num_workers>0 and persistent_workers=True' |
88 |
| - ) |
89 |
| - else: |
| 81 | + elif dataloader.num_workers == 0 and using_spawn: |
| 82 | + # checks for the attr persistent_workers available in pytorch >= 1.7 |
| 83 | + if hasattr(dataloader, "persistent_workers"): |
| 84 | + if not dataloader.persistent_workers: |
90 | 85 | rank_zero_warn(
|
91 | 86 | 'accelerator=ddp_spawn and num_workers=0 may result in data loading bottlenecks.'
|
92 |
| - ' Consider setting accelerator=ddp and set num_workers>0' |
| 87 | + ' Consider setting num_workers>0 and persistent_workers=True' |
93 | 88 | )
|
94 |
| - |
95 |
| - elif dataloader.num_workers <= 2 and multiprocessing.cpu_count() > 2 and not using_spawn: |
96 |
| - num_cpus = multiprocessing.cpu_count() |
| 89 | + else: |
97 | 90 | rank_zero_warn(
|
98 |
| - f'The dataloader, {name}, does not have many workers which may be a bottleneck.' |
99 |
| - ' Consider increasing the value of the `num_workers` argument`' |
100 |
| - f' (try {num_cpus} which is the number of cpus on this machine)' |
101 |
| - f' in the `DataLoader` init to improve performance.' |
| 91 | + 'accelerator=ddp_spawn and num_workers=0 may result in data loading bottlenecks.' |
| 92 | + ' Consider setting accelerator=ddp and set num_workers>0' |
102 | 93 | )
|
103 | 94 |
|
| 95 | + elif dataloader.num_workers <= 2 < num_cpus and not using_spawn: |
| 96 | + rank_zero_warn( |
| 97 | + f'The dataloader, {name}, does not have many workers which may be a bottleneck.' |
| 98 | + ' Consider increasing the value of the `num_workers` argument`' |
| 99 | + f' (try {num_cpus} which is the number of cpus on this machine)' |
| 100 | + f' in the `DataLoader` init to improve performance.' |
| 101 | + ) |
| 102 | + |
104 | 103 | def auto_add_sampler(self, dataloader: DataLoader, shuffle: bool) -> DataLoader:
|
105 | 104 |
|
106 | 105 | # don't do anything if it's not a dataloader
|
|
0 commit comments