Skip to content

Commit ee286e5

Browse files
stas00sfc-gh-truwaseloadamssfc-gh-sbekman
authored
set device_id in torch's init_process_group (#7266)
This PR overcomes this issue when using any `torch.distributed` calls w/ deepspeed: ``` [W404 00:15:21.693690333 ProcessGroupNCCL.cpp:4561] [PG ID 0 PG GUID 0 Rank 0] using GPU 0 to perform barrier as devices used by this process are currently unknown. This can potentially cause a hang if this rank to GPU mapping is incorrect. Specify device_ids in barrier() to force use of a particular device, or call init_process_group() with a device_id. ``` by setting `device_id` to the correct device corresponding to `LOCAL_RANK` env var. ------------------- Update: discovered `torch.dist` deadlocks with `torch=>2.7.0` when using `device_id` arg - switching to draft for now as we can't commit this until we know how to work around this. --------- Signed-off-by: Stas Bekman <stas@stason.org> Signed-off-by: Stas Bekman <stas.bekman@snowflake.com> Co-authored-by: Olatunji Ruwase <tunji.ruwase@snowflake.com> Co-authored-by: Logan Adams <114770087+loadams@users.noreply.github.com> Co-authored-by: Stas Bekman <stas.bekman@snowflake.com>
1 parent 88ba24a commit ee286e5

2 files changed

Lines changed: 18 additions & 6 deletions

File tree

accelerator/cuda_accelerator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def communication_backend_version(self):
6464
return torch.cuda.nccl.version()
6565

6666
def device(self, device_index=None):
67-
return torch.cuda.device(device_index)
67+
return torch.device('cuda', device_index)
6868

6969
def set_device(self, device_index):
7070
torch.cuda.set_device(device_index)

deepspeed/comm/torch.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
import deepspeed
77
from deepspeed import utils
8+
from packaging import version
9+
import inspect
810

911
from .utils import *
1012
from .backend import *
@@ -145,11 +147,21 @@ def has_reduce_scatter_tensor(self):
145147

146148
def init_process_group(self, backend, timeout, init_method, rank, world_size):
147149
if not torch.distributed.is_initialized():
148-
torch.distributed.init_process_group(backend,
149-
timeout=timeout,
150-
init_method=init_method,
151-
rank=rank,
152-
world_size=world_size)
150+
kwargs = dict(
151+
timeout=timeout,
152+
init_method=init_method,
153+
rank=rank,
154+
world_size=world_size,
155+
)
156+
157+
# 1. device_id arg was added in torch==2.3
158+
# 2. setting device_id leads to hanging in 2.6.0<torch<2.7.1 https://github.com/pytorch/pytorch/issues/153960
159+
if 'device_id' in inspect.signature(torch.distributed.init_process_group).parameters and not (
160+
version.parse("2.6.0") < version.parse(torch.__version__) < version.parse("2.7.1")):
161+
local_rank = int(os.environ.get('LOCAL_RANK', 0))
162+
kwargs.update(device_id=get_accelerator().device(local_rank))
163+
torch.distributed.init_process_group(backend, **kwargs)
164+
153165
self.using_mpi = torch.distributed.get_backend() == 'mpi'
154166

155167
@disable_compiler_collective

0 commit comments

Comments
 (0)