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