Skip to content

[distributed] Context parallelism with expert parallelism - #50514

Draft
3outeille wants to merge 2 commits into
cp-6-tp-x-cpfrom
cp-9-ep-x-cp
Draft

3outeille wants to merge 2 commits into
cp-6-tp-x-cpfrom
cp-9-ep-x-cp

Conversation

@3outeille

@3outeille 3outeille commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

🤗 Transformers CI

What does this PR do?

Stack: #50493 → #50496 → #50511 → this PR → #50515 → #50499 → #50521 → #50522

Context parallelism now composes with expert parallelism: DistributedConfig(cp_size=C, ep_size=E, ...). Only pp_size is still refused with cp_size.

  • Token dispatch (ep_dispatch_experts, the default of most MoE models): each rank dispatches the tokens of its own cp slice. A cp slice plays the role a batch plays for the expert mesh. efsdp = fsdp * cp * tp / ep already covers the world.
  • Expert gradients: set_gradient_divide_factor(dp_size) already gives the gradient of the full-sequence loss, because dp_size includes cp_size and the CP loss hook scales each rank's gradient by cp_size. No code change; only the comment is updated.
  • The all-reduce expert plans still need ep_size=tp_size, and the ranks of a tp group hold the same slice.

Tests

tests/context_parallel, CPU (gloo), tiny Qwen3-MoE. Loss, logits slice and every gradient, experts included, match an unsharded run:

setup plan ranks
cp=2 × ep=2 token dispatch 2
cp=2 × fsdp=2 × ep=4 token dispatch 4
cp=2 × fsdp=2 × ep=2 (efsdp=2) token dispatch 4
cp=2 × tp=2 × ep=4 token dispatch 4
cp=2 × dp_replicate=2 × ep=2 token dispatch 4
tp=2 × cp=2 × ep=2 all-reduce 4

The existing EP / FSDP tests of qwen3_moe and tests/expert_parallel pass.

@HuggingFaceDocBuilderDev

HuggingFaceDocBuilderDev commented Nov 9, 2026 •

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@3outeille
3outeille added this pull request to stack #50517 November 9, 2026 14:29
@3outeille
3outeille removed this pull request from stack #50517 November 9, 2026 14:30
@3outeille
3outeille added this pull request to stack #50518 November 9, 2026 14:30
@3outeille
3outeille removed this pull request from stack #50518 November 9, 2026 14:36
@3outeille
3outeille added this pull request to stack #50520 November 9, 2026 14:37
@3outeille
3outeille removed this pull request from stack #50520 November 10, 2026 02:50
@3outeille
3outeille added this pull request to stack #50546 November 10, 2026 02:51
Each rank of a cp group dispatches the tokens of its own slice only, so no token reaches an expert
twice and `efsdp * ep` still covers `fsdp * cp * tp`. The expert gradient divide factor already
counts the cp ranks through `dp_size`, so only the explanatory comment and the mesh docstring
change; `cp_size` with `ep_size` is no longer refused.
The tiny Qwen3-MoE against the same weights on the full sequences, with token dispatch on
`cp2 x ep2`, `cp2 x fsdp2 x ep4`, `cp2 x fsdp2 x ep2` (efsdp), `cp2 x tp2 x ep4` and
`cp2 x hsdp2 x ep2`, plus the all-reduce plan with `ep_size=tp_size=2` where the ep group holds
one cp slice. Every case stays within four ranks.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants