Repository navigation
Conversation
This was referenced Nov 9, 2026
|
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. |
This was referenced Nov 9, 2026
distributed] Add context parallelism: DistributedConfig(cp_size=N)distributed] DistributedConfig(cp_size=N)
This was referenced Nov 9, 2026
3outeille
force-pushed
the
cp-3-cp-size
branch
from
November 10, 2026 02:50
4ff461b to
0a373ad
Compare
3outeille
force-pushed
the
cp-3-cp-size
branch
2 times, most recently
from
November 10, 2026 05:18
e4604a6 to
81494d9
Compare
3outeille
force-pushed
the
cp-3-cp-size
branch
2 times, most recently
from
November 10, 2026 06:03
b8825e4 to
945fa19
Compare
3outeille
force-pushed
the
cp-3-cp-size
branch
from
November 12, 2026 06:19
945fa19 to
438df28
Compare
3outeille
force-pushed
the
cp-3-cp-size
branch
from
November 12, 2026 07:09
438df28 to
aa548b8
Compare
3outeille
force-pushed
the
cp-3-cp-size
branch
from
November 12, 2026 08:01
aa548b8 to
dbf2340
Compare
3outeille
force-pushed
the
cp-3-cp-size
branch
from
November 12, 2026 09:13
dbf2340 to
af970d9
Compare
3outeille
force-pushed
the
cp-3-cp-size
branch
2 times, most recently
from
November 12, 2026 10:08
e2b848d to
81c03ad
Compare
Contributor
|
[For maintainers] Suggested jobs to run (before merge) run-slow: beam, diffusion_gemma, gemma3n, gemma4, gemma4_unified, longcat_flash, ministral3 |
`cp_size` splits every sequence across the ranks of a `cp` mesh, which get the same batch and keep a contiguous slice each. The plan follows the other parallelisms: a `base_model_cp_plan` default in `PreTrainedConfig`, a `cp_plan` override merged and expanded like `tp_plan`, and `apply_context_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 `fsdp` and `cp` together (`fsdp_cp`), and `dp_size` counts the cp ranks. `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 a cp group.
Each case compares a 2-rank run against the same weights on the full sequences: loss, this rank's slice of the logits and every gradient, for plain, packed and `inputs_embeds` batches, with `fsdp_size=2` and `dp_replicate_size=2`, and with left and right padding through a 2D `attention_mask`. Plus a sliding-window Mistral and a Gemma3 mix of sliding and full layers with `sdpa`, a custom and an unknown `cp_implementation`, the refusals, and the save / distributed checkpoint round trips.
3outeille
force-pushed
the
cp-3-cp-size
branch
from
November 12, 2026 10:32
81c03ad to
bac853d
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Stack: #50493 → this PR → #50511 → #50514 → #50499 → #50521 → #50522
Wires the context parallel (CP) module of #50493 into
transformers.distributed, with the same plan API as TP, EP and FSDP. Each rank of a cp group keeps a contiguous slice of every sequence, so activation memory per GPU shrinks withcp_size. The training loop does not change:What this PR brings
DistributedConfig(cp_size, cp_plan).dp_sizenow includescp_size: the ranks of a cp group share a batch but process different tokens, so FSDP2 reduces gradients over them too.PreTrainedConfig.base_model_cp_plandefaults to{"layers.*": "cp_split_sequence", "layers.*.self_attn": "cp_ulysses"}, likebase_model_fsdp_plan.model.cp_plan, overrides withcp_plan(another attention algorithm iscp_plan={"model.layers.*.self_attn": "cp_ring"}, a registered style), and the "nocp_plan" error, which is also raised when the plan matches nothing or lacks a split or an attention style.None: gemma3n, gemma4, beam, ministral3 (gemma4_unifiedanddiffusion_gemmainherit it from gemma4).longcat_flashdeclares its own plan.maybe_distribute_modelcallsapply_context_parallelism([distributed] Context parallel module, with Ulysses #50493) with the resolved plan, before FSDP2, like the other parallelisms.(pp, dp_replicate, fsdp, cp, tp). FSDP2 shards overfsdp × cp, an axis of the root mesh(pp, dp_replicate, fsdp_cp, tp)thatfsdpandcpare an_unflattenview of, so that it composes withtplater. CP needstorch>=2.10.pp,tporeptogether with cp (TP and EP come next in the stack);Trainer.What #50493 brings
The module itself,
context_parallel.py:ContextParallelLayerand the styles (cp_split_sequence, the attention styles withcp_ulysses,ModelContextParallelwith the checks, the cache refusal and the loss) andapply_context_parallelism. This PR adds no code to that file.Tests
tests/context_parallel, CPU (gloo), throughfrom_pretrained(distributed_config=...). The loss, this rank's logits and every gradient match an unsharded run of the same weights:inputs_embeds, left and right paddingAlso covered: a custom attention style named in
cp_plan, the config refusals, the plan resolution and its errors, andsave_pretrained(gathered and DCP).GPU check (2× H100, Qwen3-0.6B, bf16)
With
sdpaand the FA3 hub kernel, CP and an unsharded run give the same loss and logits. This holds for packed batches, padded batches and sliding windows. The gradients match within bf16 rounding (at most 1.5% relative).Peak memory per GPU and step time (forward + backward + AdamW), FA3: