Skip to content

[distributed] DistributedConfig(fsdp_size=N, sp_size=M) - #50496

Open
3outeille wants to merge 14 commits into
cp-1-implementationsfrom
cp-3-cp-size
Open

3outeille wants to merge 14 commits into
cp-1-implementationsfrom
cp-3-cp-size

Conversation

@3outeille

@3outeille 3outeille commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

What does this PR do?

Stack: #50493 → this PR → #50796 → #50511 → #50514 → #50521 → #50522

model = AutoModelForCausalLM.from_pretrained(
    "Qwen/Qwen3-8B",
    attn_implementation="flash_attention_2",
    distributed_config=DistributedConfig(fsdp_size=2, sp_size=2),  # or just sp_size=2
)
loss = model(**batch).loss  # full sequences; ranks in an sp group get the same batch
loss.backward()

Quick GPU check (4× B200, bf16, FA2)

Qwen3-8B (bs_per_dp=1, fwd + bwd + AdamW):

seq len fsdp=1, sp=1 (1 GPU) fsdp=4, sp=1 (4 GPUs) fsdp=2, sp=2 (4 GPUs) fsdp=1, sp=4 (4 GPUs)
8k 121.5 GiB, 756 ms 88.3 GiB, 772 ms 51.6 GiB, 517 ms 32.7 GiB, 445 ms
16k OOM 163.9 GiB, 2298 ms 90.5 GiB, 1030 ms 51.6 GiB, 596 ms
32k OOM OOM OOM 90.6 GiB, 1416 ms

@HuggingFaceDocBuilderDev

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 #50502 November 9, 2026 11:03
@3outeille
3outeille removed this pull request from stack #50502 November 9, 2026 14:29
@3outeille
3outeille added this pull request to stack #50518 November 9, 2026 14:30
@3outeille 3outeille changed the title [distributed] Add context parallelism: DistributedConfig(cp_size=N) [distributed] DistributedConfig(cp_size=N) Nov 9, 2026
@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
@3outeille
3outeille force-pushed the cp-3-cp-size branch 2 times, most recently from e4604a6 to 81494d9 Compare November 10, 2026 05:18
@3outeille
3outeille force-pushed the cp-3-cp-size branch 2 times, most recently from b8825e4 to 945fa19 Compare November 10, 2026 06:03
@3outeille
3outeille added this pull request to stack #51004 November 20, 2026 08:50
@3outeille
3outeille removed this pull request from stack #51004 November 30, 2026 05:31
@3outeille
3outeille force-pushed the cp-3-cp-size branch 2 times, most recently from d2ec7b3 to 16f076e Compare November 30, 2026 07:07
@3outeille 3outeille changed the title [distributed] DistributedConfig(sp_size=N) [distributed] DistributedConfig(sp_size=N) + FSDP Nov 30, 2026
@3outeille 3outeille changed the title [distributed] DistributedConfig(sp_size=N) + FSDP [distributed] DistributedConfig(fsdp_size=N, sp_size=M) Nov 30, 2026
@3outeille
3outeille marked this pull request as ready for review November 30, 2026 08:30
@3outeille
3outeille added this pull request to stack #51606 December 2, 2026 05:05
@3outeille
3outeille force-pushed the cp-3-cp-size branch 2 times, most recently from 5c16611 to c6a5795 Compare December 2, 2026 07:20
@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

[For maintainers] Suggested jobs to run (before merge)

run-slow: longcat_flash

`sp_size` splits every sequence across the ranks of an `sp` mesh, which get the same batch and
keep a contiguous slice each. The plan follows the other parallelisms: a `base_model_sp_plan`
default in `PreTrainedConfig`, an `sp_plan` override merged and expanded like `tp_plan`, and
`apply_sequence_parallelism` installing the plan's styles after TP/EP and before FSDP2.

The model keeps taking whole sequences: the first decoder layer cuts `hidden_states`, every
layer cuts the rotary embeddings, the model-built mask and positions stay whole, and the loss is
the one of the whole sequences. FSDP2 shards over `sp`, and `dp_size` counts the sp ranks.
`fsdp_size`, `tp_size`, `pp_size` and `ep_size` are refused with it for now, and so is the
`Trainer`, whose dataloader would split the batch across an sp group.
A model sharded with `DistributedConfig(sp_size=N)` reproduces, step by step, the run of the same
weights on the full sequences: same loss, same logits on this rank's slice, same gradients. The
tests cover the plan, the refusals, packing, padding, `inputs_embeds`, sliding windows, a
registered attention style, saving, and HSDP composed with `sp_size`.
`DistributedConfig(fsdp_size=N, sp_size=M)` is no longer refused. FSDP2 shards over `fsdp` and
`sp` together: the ranks of an sp group hold the same batch, so their gradients are reduced
together and their parameters can be sharded together. That axis, `fsdp_sp`, is a dim of the
dense root mesh `(pp, dp_replicate, fsdp_sp, tp)`, and the five dense axes are an `_unflatten`
view of it, so that HSDP can slice `("dp_replicate", "fsdp_sp")` without slicing a flattened dim
from the root mesh, which torch deprecates. Requires `torch>=2.10`, refused in `DistributedConfig`;
`sp_size` alone keeps the plain mesh and shards over `sp`.
`fsdp_size=2 x sp_size=2` on four ranks against the same weights on the full sequences: same
loss, same logits on this rank's slice, same gradients.

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