Return node and worker info for PyTorch and some distributed environments.
(group=None)
| 18 | from webdataset.gopen import gopen_schemes, gopen |
| 19 | |
| 20 | def pytorch_worker_info(group=None): # sourcery skip: use-contextlib-suppress |
| 21 | """Return node and worker info for PyTorch and some distributed environments.""" |
| 22 | rank = 0 |
| 23 | world_size = 1 |
| 24 | worker = 0 |
| 25 | num_workers = 1 |
| 26 | try: |
| 27 | import torch.distributed |
| 28 | |
| 29 | if torch.distributed.is_available() and torch.distributed.is_initialized(): |
| 30 | group = group or torch.distributed.group.WORLD |
| 31 | rank = torch.distributed.get_rank(group=group) |
| 32 | world_size = torch.distributed.get_world_size(group=group) |
| 33 | except ModuleNotFoundError: |
| 34 | pass |
| 35 | try: |
| 36 | import torch.utils.data |
| 37 | |
| 38 | worker_info = torch.utils.data.get_worker_info() |
| 39 | if worker_info is not None: |
| 40 | worker = worker_info.id |
| 41 | num_workers = worker_info.num_workers |
| 42 | except ModuleNotFoundError: |
| 43 | pass |
| 44 | |
| 45 | return rank, world_size, worker, num_workers |
| 46 | |
| 47 | |
| 48 | def pytorch_worker_seed(group=None): |
no outgoing calls
no test coverage detected