From 41168b170fa0ca1b8f13e3d7c5ba95cf9d99a276 Mon Sep 17 00:00:00 2001 From: "Jin, Youzhi" Date: Wed, 21 Oct 2026 11:38:58 +0000 Subject: [PATCH 1/2] ZeRO-3: gather partition groups in the same order on every rank all_gather_coalesced splits its parameters by partition process group and issues one all-gather per group. With AutoEP, dense and expert parameters belong to different groups. The ZeRO-3 prefetcher passes a set, whose iteration order follows object addresses and can differ across ranks, so ranks could issue the per-group all-gathers in different orders and deadlock. Training hung from the third step, once prefetching started. Sort the parameters by ds_id on entry, before grouping, instead of only inside each group. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: Jin, Youzhi --- .../runtime/zero/partition_parameters.py | 16 ++++---- .../zero/test_zero_stale_handle_wait.py | 41 +++++++++++++++++++ 2 files changed, 49 insertions(+), 8 deletions(-) diff --git a/deepspeed/runtime/zero/partition_parameters.py b/deepspeed/runtime/zero/partition_parameters.py index 5b18225ddb57..13359a558527 100644 --- a/deepspeed/runtime/zero/partition_parameters.py +++ b/deepspeed/runtime/zero/partition_parameters.py @@ -1540,7 +1540,14 @@ def _all_gather_coalesced(params, world_size, rank_in_group, use_secondary_tenso def all_gather_coalesced(params: Iterable[Parameter], safe_mode: bool = False, quantize: bool = False) -> AllGatherCoalescedHandle: - params = list(params) + # ensure that each rank has params in same order. callers may pass a set, whose + # order differs across ranks. the allgather is done by flattening the parameter + # list into a single tensor that can be allgathered in a single call - this means + # that if each rank gives a list of the same parameters in a different order we + # will silently get incorrect parameter values. params of different partition + # groups are gathered one group at a time, so a different order would also issue + # the per-group collectives in a different order and deadlock. + params = sorted(params, key=lambda p: p.ds_id) if len(params) == 0: return _no_gather_coalesced(params) @@ -1575,13 +1582,6 @@ def all_gather_coalesced(params: Iterable[Parameter], world_size = self.num_ranks_in_param_group #pprint(dir(ds_process_group)) - # ensure that each rank has params in same order. the allgather - # is done by flattening the parameter list into a single tensor that - # can be allgathered in a single call - this means that if each rank - # gives a list of the same parameters in a different order we will - # silently get incorrect parameter values, and have very difficult - # to debug correctness issues. - params = sorted(params, key=lambda p: p.ds_id) if logger.isEnabledFor(logging.DEBUG): debug_rank0(f"-allgather_coalesced: {[p.ds_id for p in params]}") diff --git a/tests/unit/runtime/zero/test_zero_stale_handle_wait.py b/tests/unit/runtime/zero/test_zero_stale_handle_wait.py index 3137c08d3db4..278face2f18a 100644 --- a/tests/unit/runtime/zero/test_zero_stale_handle_wait.py +++ b/tests/unit/runtime/zero/test_zero_stale_handle_wait.py @@ -84,3 +84,44 @@ def __init__(self): victim = _stale_wait_cycle(solo.w, dense.weight) assert _released(victim), ("a second wait() on a released parameter marked it " f"{victim.ds_status.name} over {victim.data.numel()} elements") + + +class TestMultiGroupGatherOrder(DistributedTest): + world_size = 4 + + def test_rank_dependent_param_order(self): + # Expert-parallel parameters are partitioned over a subgroup while dense parameters use + # the whole world, so all_gather_coalesced issues one all-gather per group. The ZeRO-3 + # prefetcher passes a set, whose order differs across ranks; every rank must still issue + # the per-group all-gathers in the same order, or the collectives deadlock. + rank = dist.get_rank() + pair_group = None + for ranks in ([0, 1], [2, 3]): + group = dist.new_group(ranks) + if rank in ranks: + pair_group = group + + class PairGroupParam(torch.nn.Module): + + def __init__(self): + super().__init__() + self.w = torch.nn.Parameter(torch.full((HIDDEN, HIDDEN), float(rank // 2 + 1))) + self.w.ds_zero_partition_process_group = pair_group + + class DenseParam(torch.nn.Module): + + def __init__(self): + super().__init__() + self.w = torch.nn.Parameter(torch.full((HIDDEN, HIDDEN), 3.0)) + + with deepspeed.zero.Init(dtype=preferred_dtype()): + dense = DenseParam() + pair = PairGroupParam() + + params = [pair.w, dense.w] + if rank % 2: + params.reverse() + params[0].all_gather_coalesced(params).wait() + + assert torch.equal(dense.w, torch.full_like(dense.w, 3.0)) + assert torch.equal(pair.w, torch.full_like(pair.w, float(rank // 2 + 1))) From 6c819a9722953ca912e9bde6b0ef81926a8d8e47 Mon Sep 17 00:00:00 2001 From: "Jin, Youzhi" Date: Wed, 21 Oct 2026 13:37:05 +0000 Subject: [PATCH 2/2] Reproduce the gather-order deadlock with two ranks A second group over the same two ranks is enough to split the gather into two per-group collectives, so the test no longer needs four devices. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: Jin, Youzhi --- .../zero/test_zero_stale_handle_wait.py | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/tests/unit/runtime/zero/test_zero_stale_handle_wait.py b/tests/unit/runtime/zero/test_zero_stale_handle_wait.py index 278face2f18a..c89bc36c18dc 100644 --- a/tests/unit/runtime/zero/test_zero_stale_handle_wait.py +++ b/tests/unit/runtime/zero/test_zero_stale_handle_wait.py @@ -87,25 +87,22 @@ def __init__(self): class TestMultiGroupGatherOrder(DistributedTest): - world_size = 4 + world_size = 2 def test_rank_dependent_param_order(self): - # Expert-parallel parameters are partitioned over a subgroup while dense parameters use - # the whole world, so all_gather_coalesced issues one all-gather per group. The ZeRO-3 + # Expert-parallel parameters are partitioned over their own group while dense parameters + # use the whole world, so all_gather_coalesced issues one all-gather per group. The ZeRO-3 # prefetcher passes a set, whose order differs across ranks; every rank must still issue - # the per-group all-gathers in the same order, or the collectives deadlock. + # the per-group all-gathers in the same order, or the collectives deadlock. A separate + # group over the same two ranks is enough to get two groups. rank = dist.get_rank() - pair_group = None - for ranks in ([0, 1], [2, 3]): - group = dist.new_group(ranks) - if rank in ranks: - pair_group = group + pair_group = dist.new_group([0, 1]) class PairGroupParam(torch.nn.Module): def __init__(self): super().__init__() - self.w = torch.nn.Parameter(torch.full((HIDDEN, HIDDEN), float(rank // 2 + 1))) + self.w = torch.nn.Parameter(torch.full((HIDDEN, HIDDEN), 2.0)) self.w.ds_zero_partition_process_group = pair_group class DenseParam(torch.nn.Module): @@ -124,4 +121,4 @@ def __init__(self): params[0].all_gather_coalesced(params).wait() assert torch.equal(dense.w, torch.full_like(dense.w, 3.0)) - assert torch.equal(pair.w, torch.full_like(pair.w, float(rank // 2 + 1))) + assert torch.equal(pair.w, torch.full_like(pair.w, 2.0))