Skip to content

[distributed] Isolate FSDP from CP's default-group reuse to prevent a deadlock - #5645

Closed
zpcore wants to merge 3 commits into
pytorch:mainfrom
zpcore:fix-cp-fsdp-shared-communicator
Closed

zpcore wants to merge 3 commits into
pytorch:mainfrom
zpcore:fix-cp-fsdp-shared-communicator

Conversation

@zpcore

@zpcore zpcore commented Oct 28, 2026

Copy link
Copy Markdown

Bug

DeviceMesh._init_one_process_group reuses the default (WORLD) process group -- instead of creating a dedicated communicator -- for any mesh dimension (including one produced by DeviceMesh._flatten) whose ranks span every rank, as long as no explicit backend_override is given.

FSDP shards parameters on dp_shard * cp. When dp_shard == 1 (a pure-CP configuration, e.g. cp == world_size), that flattened axis spans every rank -- and so does CP's own cp axis. Both silently collapse onto the same WORLD process group / NCCL communicator. dp_replicate (pure DDP spanning the world) and edp_shard (EP with no other sparse-region parallelism) have the same failure mode on their own single axis.

FSDP issues its all-gather/reduce-scatter from its own dedicated CUDA streams, while CP's collectives run on the default/compute stream. Nothing orders those two streams against each other, so the two collectives can be enqueued in different host-side orders across ranks. Two collectives sharing one communicator must be posted in the same order by every rank, so a mismatched order deadlocks NCCL instead of merely serializing. Whether the groups collide is config-deterministic, but the deadlock itself is a scheduling race -- which is why it does not reproduce on every run.

Fix

Give FSDP's shard/replicate axes a dedicated backend_override so they never alias the default group:

  • dp_replicate and edp_shard are built directly by ParallelismContext.build_mesh's unflatten_mesh closure; give them an explicit override there.
  • dp_shard * cp is flattened later, inside resolve_fsdp_mesh, when passed to fully_shard via DataParallelMeshDims. Pre-flatten it eagerly there with an explicit override so FSDP2's own (unoverridden) internal flatten just hits the cache we already populated.

The override passed is the mesh's own already-configured backend (dist.get_backend_config), so this only changes communicator identity, never which backend is used -- real NCCL/Gloo meshes and "fake"-backed test meshes behave the same as before, just with a dedicated communicator.

Tests

Added TestFsdpCpCommunicatorIsolation to tests/unit_tests/cpu/distributed/test_parallelism_context.py (CPU/fake-mesh, no GPU needed):

  • test_pure_cp_fsdp_group_is_not_shared_with_cp_group: dp_shard=1, cp=4 -- the historically-buggy configuration -- asserts FSDP's shard group and CP's group have different group_names. Fails on main with AssertionError: AssertionError not raised, confirming the two groups collide today.
  • test_pure_ddp_dp_replicate_group_is_dedicated: dp_replicate=4 (pure DDP spanning the world) asserts its group differs from the default group.

Ran the full existing tests/unit_tests/cpu/distributed/ suite (163 tests) to confirm no regressions.

… deadlock

DeviceMesh._init_one_process_group reuses the default (WORLD) process
group -- instead of creating a dedicated communicator -- for any mesh
dimension (including one produced by DeviceMesh._flatten) whose ranks
span every rank, unless an explicit backend_override is passed.

FSDP shards parameters on dp_shard * cp. When dp_shard == 1 (the common
CP-only configuration), that flattened axis spans every rank, and so
does CPs own cp axis. Both silently collapsed onto the same WORLD
process group / NCCL communicator. dp_replicate (pure DDP) and
edp_shard (EP with no other sparse-region parallelism) have the same
failure mode on their own, single-axis groups.

FSDP issues its all-gather/reduce-scatter from its own dedicated CUDA
streams, while CP's collectives run on the default/compute stream.
Nothing orders those two streams against each other, so the two
collectives can be enqueued in different host-side orders across
ranks. Two collectives sharing one communicator must be posted in the
same order by every rank, so a mismatched order deadlocks NCCL instead
of merely serializing -- this is config-deterministic (whether the
groups collide) but the deadlock itself is a scheduling race, which is
why it does not reproduce on every run.

Fix: give FSDP's shard/replicate axes a dedicated backend_override so
they never alias the default group, whether built directly
(dp_replicate, edp_shard, in ParallelismContext.build_mesh) or
flattened later from multiple axes (dp_shard * cp, in
resolve_fsdp_mesh).

Adds a CPU regression test reproducing the dp_shard=1/cp=world_size
collision and the pure-DDP dp_replicate=world_size collision.
@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Oct 28, 2026
@zpcore
zpcore requested a review from wconstab as a code owner October 28, 2026 07:39
@meta-cla

meta-cla Bot commented Oct 28, 2026

Copy link
Copy Markdown

Hi @zpcore!

Thank you for your pull request.

We require contributors to sign our Contributor License Agreement, and yours needs attention.

You currently have a record in our system, but the CLA is no longer valid, and will need to be resubmitted.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@pytorch-bot

pytorch-bot Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

The following ciflow label(s) have been added but CI has not been triggered yet because the workflows are awaiting approval:

  • ciflow/fake-pg

Once a maintainer approves the workflows (scroll to the bottom of the PR page), the corresponding CI jobs will be triggered automatically. Please ping one of the reviewers if you do not have access to approve and run workflows.

@fegin

fegin commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

This looks like a real issue but can you describe what's your settings to make this happen? I have ran CP8 and CP16 with dp_shard being 1 multiple times, I never encountered such an issue.

@fegin

fegin commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

Btw, @zpcore can you sign Meta CLA, thanks!

@zpcore

zpcore commented Oct 28, 2026

Copy link
Copy Markdown
Author

Hi @fegin , we noticed this deadlock occasionally in training jobs. @tranle-fw is the expert working on the fix.

Hi @tranle-fw, is there an easy way to reproduce the race condition?

…ten under FSDP2's name

fully_shard validates every shard name against the storage mesh's dim
names, so passing the flattened 'dp_shard_cp' name broke all CP configs.
Keep the ('dp_shard', 'cp') tuple and pre-flatten with the same
'_'.join name FSDP2 uses so its own flatten hits the cached dedicated PG.
The unit test now goes through _validate_mesh/_get_mesh_info.
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Oct 28, 2026
@fegin

fegin commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

@tranle-fw can you provide the settings including parallelisms and hardware settings? Thanks

@tranle-fw

Copy link
Copy Markdown

@fegin This is race condition so it's extremely hard to reproduce. The hang happens since FSDP all-gather is scheduled on a different stream than our CP communication. This cause indeterministic ordering on the GPU kernel execution order. For example, rank0 can execute CP comm first, while rank1 can execute FSDP all-gather since there's no cross-rank order synchronization.

I added a simple local repro that forces this misalignment in ordering across rank through the host. W/ shared communicator, we reproduce the deadlock issue. W/ separate pg, all ranks can complete.

Simple repro: zpcore#1

@tranle-fw

Copy link
Copy Markdown

Superseded by #5663, opened under tranle-fw to maintain the combined change. It retains Pei's original commits and adds the byte-identical tested repro and sanitized evidence. Please continue review there.

@zpcore

zpcore commented Oct 29, 2026

Copy link
Copy Markdown
Author

Hi @fegin , let's use #5663, which contains the reproduce test. Close this one for now. Thanks

@zpcore zpcore closed this Oct 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/fake-pg Run 1-GPU Fake PG integration tests CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants