DeepSpeed icon indicating copy to clipboard operation
DeepSpeed copied to clipboard

[BUG]all gather handle stale causing empty tensor

Open pengdurice opened this issue 2 months ago • 0 comments

Describe the bug

Under ZeRO-3, a parameter that has been released can be marked AVAILABLE while its storage holds zero elements. Every later fetch trusts that flag and skips the all-gather, so the next matmul runs on an empty weight:

RuntimeError: size mismatch, got input (2048), mat (2048x4096), vec (0)
  File "deepspeed/runtime/zero/linear.py", line 92, in forward
    output = input.matmul(weight.t())

vec (0) is a real weight with ds_numel = 524288 whose param.data is a 1-D tensor of length 0, the placeholder free_param leaves behind after partitioning.

The cause: wait() is not idempotent on two handle classes

PartitionedParameterCoordinator.__all_gather_params_ registers one handle object under every parameter of a gather group:

handle = param_group[0].all_gather_coalesced(param_group)
for param in param_group:
    self.__inflight_param_registry[param] = handle     # same object, many keys

_fetch_sub_module_impl then pops and waits those keys independently, one per module. So a group's handle is waited once per parameter in the group. Every multi-parameter handle class already treats that as expected and returns early:

class idempotent re-checks INFLIGHT in wait
AllGatherCoalescedHandle yes yes
NoGatherCoalescedHandle yes yes
AllReduceCoalescedHandle yes yes
AllGatherHandle no no
NoGatherHandle no no
MultipleAllGatherHandles no, plain loop over sub-handles delegates

The two single-parameter classes do not. Waiting one of them a second time re-runs the body unconditionally, and the last statement is ds_status = AVAILABLE. If the parameter was released in between, that sets AVAILABLE over freed storage.

AllGatherHandle cannot repair itself even in principle on the common path: _all_gather_sequential points param.data at the all-gather output buffer at launch time and does not hand that buffer to the handle, so wait() has nothing to rebuild from. AllGatherCoalescedHandle keeps its slices in self.partitions and rebuilds param.data on every wait, which is why it is safe.

The sequence

1. a gather group {A, B} is launched; registry[A] = registry[B] = H
2. A's module fetches: registry.pop(A).wait() -> H waits the whole group, B becomes AVAILABLE
   B's registry entry is still present; only A's was popped
3. B's module runs and returns: release -> free_param -> data = empty(0), status = NOT_AVAILABLE
4. a later module fetches B: registry.pop(B).wait() -> H runs AGAIN
   B's single-parameter sub-handle has no completion flag -> status = AVAILABLE over 0 elements
5. any later fetch of B sees AVAILABLE, launches no gather, and the matmul gets an empty weight

To Reproduce

Two independent configurations reach a single-parameter sub-handle. Both are reproduced at the handle level on 2 GPUs, with no model and no training loop, by calling all_gather_coalesced([victim, other]), waiting, releasing the victim, and waiting again:

configuration handle built result
stage3_allgather_sequential: true, one process group, two ordinary 4096x4096 weights MultipleAllGatherHandles[AllGatherHandle, AllGatherHandle] victim ends AVAILABLE over 0 elements
gather group spanning two process groups, victim alone in its bucket MultipleAllGatherHandles[MultipleAllGatherHandles[AllGatherHandle], NoGatherHandle] victim ends AVAILABLE over 0 elements

Controls that stay correct in the same script: two weights in one process group (coalesced handle), three weights in one process group, and a mixed bf16/fp32 group (the per-dtype split still yields coalesced handles).

The first configuration needs no expert parallelism and no MoE. One config flag is enough, and the victim is a plain nn.Linear weight.

The second is what a real run hit. With DeepSpeed's AutoEP, expert parameters are partitioned over _get_expert_data_parallel_group(...) and everything else over the data-parallel group (engine.py:_resolve_zero3_param_placement). Those groups differ for any ep_size > 1, so all_gather_coalesced splits the group and a lone non-expert parameter gets an AllGatherHandle. A prefetched group of {router gate weight, expert tensor} is exactly that shape.

Expected behavior

wait() should be idempotent for every handle class, as it already is for the three multi-parameter ones. A second wait on the same handle should be a no-op, leaving a released parameter NOT_AVAILABLE so the next fetch re-gathers it from ds_tensor, which release never frees.

System info

  • DeepSpeed: reproduced on master; the two classes are unchanged for several years
  • ZeRO stage 3, bf16, 2 GPUs for the handle-level reproduction
  • Originally found on an 8xH200 node, GLM-4.5-Air, ZeRO-3 with AutoEP at ep_size=8, stock stage3_param_persistence_threshold and stage3_prefetch_bucket_size

Scope

Any parameter above stage3_param_persistence_threshold can hit this, because parameters at or below it are marked persistent and never released. Below the threshold the window never opens.

pengdurice avatar Sep 11 '26 16:09 pengdurice