Enable distributed mode Args: set_cuda_current_device: If True, call torch.cuda.set_device() to set the current PyTorch CUDA device to the one matching the local rank. overwrite: If True, overwrites already set variables. Else fails.
(
*,
set_cuda_current_device: bool = True,
overwrite: bool = False,
allow_nccl_timeout: bool = False,
)
| 243 | |
| 244 | |
| 245 | def enable( |
| 246 | *, |
| 247 | set_cuda_current_device: bool = True, |
| 248 | overwrite: bool = False, |
| 249 | allow_nccl_timeout: bool = False, |
| 250 | ): |
| 251 | """Enable distributed mode |
| 252 | |
| 253 | Args: |
| 254 | set_cuda_current_device: If True, call torch.cuda.set_device() to set the |
| 255 | current PyTorch CUDA device to the one matching the local rank. |
| 256 | overwrite: If True, overwrites already set variables. Else fails. |
| 257 | """ |
| 258 | |
| 259 | global _LOCAL_RANK, _LOCAL_WORLD_SIZE |
| 260 | if _LOCAL_RANK >= 0 or _LOCAL_WORLD_SIZE >= 0: |
| 261 | raise RuntimeError("Distributed mode has already been enabled") |
| 262 | torch_env = _TorchDistributedEnvironment() |
| 263 | torch_env.export(overwrite=overwrite) |
| 264 | |
| 265 | if set_cuda_current_device: |
| 266 | torch.cuda.set_device(torch_env.local_rank) |
| 267 | |
| 268 | if allow_nccl_timeout: |
| 269 | # This allows to use torch distributed timeout in a NCCL backend |
| 270 | key, value = "NCCL_ASYNC_ERROR_HANDLING", "1" |
| 271 | if not overwrite: |
| 272 | _check_env_variable(key, value) |
| 273 | os.environ[key] = value |
| 274 | |
| 275 | dist.init_process_group(backend="nccl") |
| 276 | dist.barrier() |
| 277 | |
| 278 | # Finalize setup |
| 279 | _LOCAL_RANK = torch_env.local_rank |
| 280 | _LOCAL_WORLD_SIZE = torch_env.local_world_size |
| 281 | _restrict_print_to_main_process() |
nothing calls this directly
no test coverage detected