(rank: int, world_size: int, port: int, fn: Callable, args: tuple)
| 81 | fn(*args) |
| 82 | finally: |
| 83 | dist.destroy_process_group() |
| 84 | |
| 85 | |
| 86 | def _distributed_worker(rank: int, world_size: int, port: int, fn: Callable, args: tuple) -> None: |
| 87 | if rank == 0: |
| 88 | _print_gpu_topology() |
| 89 | print( |
| 90 | f"Rank {rank}: available CPUs: {sorted(os.sched_getaffinity(0))}", |
| 91 | flush=True, |
| 92 | ) |
| 93 | device_id = rank % torch.cuda.device_count() |
| 94 | torch.cuda.set_device(device_id) |
| 95 | backend = "nccl" if torch.cuda.device_count() >= world_size else "gloo" |
| 96 | if rank == 0: |
| 97 | print(f"Using distributed backend: '{backend}'") |
| 98 | |
| 99 | dist.init_process_group( |
| 100 | backend=backend, |
| 101 | init_method=f"tcp://localhost:{port}", |
| 102 | rank=rank, |
| 103 | world_size=world_size, |
| 104 | device_id=device_id, |
| 105 | ) |
| 106 | try: |
| 107 | fn(*args) |
| 108 | finally: |
| 109 | dist.destroy_process_group() |
| 110 |
nothing calls this directly
no test coverage detected