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. |
3outeille
force-pushed
the
cp-9-ep-x-cp
branch
from
November 9, 2026 14:37
d0d2eb7 to
58051e9
Compare
3outeille
force-pushed
the
cp-7-auto-pad
branch
from
November 9, 2026 14:37
4249c6d to
043fd08
Compare
This was referenced Nov 9, 2026
3outeille
force-pushed
the
cp-9-ep-x-cp
branch
from
November 10, 2026 02:50
58051e9 to
62333ac
Compare
3outeille
force-pushed
the
cp-7-auto-pad
branch
from
November 10, 2026 02:51
043fd08 to
274c694
Compare
3outeille
force-pushed
the
cp-9-ep-x-cp
branch
from
November 10, 2026 03:01
62333ac to
963136f
Compare
3outeille
force-pushed
the
cp-7-auto-pad
branch
from
November 10, 2026 03:01
274c694 to
a5d8617
Compare
3outeille
force-pushed
the
cp-9-ep-x-cp
branch
from
November 10, 2026 05:19
963136f to
dcd89f0
Compare
3outeille
force-pushed
the
cp-7-auto-pad
branch
from
November 10, 2026 05:19
a5d8617 to
ed0c679
Compare
3outeille
force-pushed
the
cp-9-ep-x-cp
branch
from
November 10, 2026 05:45
dcd89f0 to
95dc69d
Compare
3outeille
force-pushed
the
cp-7-auto-pad
branch
from
November 10, 2026 05:45
ed0c679 to
0473203
Compare
3outeille
force-pushed
the
cp-9-ep-x-cp
branch
from
November 10, 2026 06:03
95dc69d to
55ebcac
Compare
3outeille
force-pushed
the
cp-7-auto-pad
branch
2 times, most recently
from
November 12, 2026 06:19
811cfea to
dca9b1c
Compare
3outeille
force-pushed
the
cp-9-ep-x-cp
branch
from
November 12, 2026 07:15
a17b368 to
b5fac32
Compare
3outeille
force-pushed
the
cp-7-auto-pad
branch
from
November 12, 2026 07:15
dca9b1c to
df47b56
Compare
3outeille
force-pushed
the
cp-9-ep-x-cp
branch
from
November 12, 2026 08:01
b5fac32 to
4a238a2
Compare
3outeille
force-pushed
the
cp-7-auto-pad
branch
from
November 12, 2026 08:01
df47b56 to
ed345b3
Compare
3outeille
force-pushed
the
cp-9-ep-x-cp
branch
from
November 12, 2026 09:14
4a238a2 to
a0334ef
Compare
3outeille
force-pushed
the
cp-7-auto-pad
branch
from
November 12, 2026 09:14
ed345b3 to
ed63651
Compare
`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.
`fsdp_cp` is now an axis of a dense root mesh `(pp, dp_replicate, fsdp_cp, tp)`, and the five dense axes are an unflattened view of it: FSDP2 composes the mesh it shards over with the mesh the parameters are already sharded on, and torch only composes meshes built from the same root. Requires `torch>=2.10` (`DeviceMesh._unflatten`), refused in `DistributedConfig`. Under TP this rank holds `num_heads / tp_size` heads and Ulysses splits those across the cp group, so `cp_size * tp_size` must divide the attention and key/value heads.
`tp_size=2 x cp_size=2` on four ranks, plain and packed, against the same weights on the full sequences, plus the gathered save, the head divisibility check under TP, and a test that the two plans compose: the attention module whose call CP routes is the one whose projections TP sharded.
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.
… cp_size The root pre-hook pads every sequence-shaped input at its end instead of raising: `input_ids` with the model's `pad_token_id` (0 without one), `inputs_embeds` with zeros, `position_ids` with the positions that continue each row's last document, a 2D `attention_mask` with zeros, and `labels` or a caller's `shift_labels` with -100. The padding happens before the model builds its mask and its rotary embeddings, so nothing on the padding is attended to or trained on, and the loss wrapper counts `num_items_in_batch` on the real tokens only.
…vide A length of 7 over 2 ranks (plain, packed, `inputs_embeds` and through a 2D padding mask) and a length of 5 over 4 ranks, compared on the real positions of the logits only, plus a single-process test of the hook's padding of every input, of a positional `input_ids` and of a config without a `pad_token_id`.
3outeille
force-pushed
the
cp-9-ep-x-cp
branch
from
November 12, 2026 09:29
a0334ef to
aba53d9
Compare
3outeille
force-pushed
the
cp-7-auto-pad
branch
from
November 12, 2026 09:29
ed63651 to
d5bdb5c
Compare
3outeille
added a commit
that referenced
this pull request
Nov 12, 2026
…ks in `apply_context_parallelism`, pad the sequences - `context_parallel_loss` is a module-level function that `apply_context_parallelism` sets as `model.loss_function`, the seam transformers has for a model's loss; `ModelContextParallel` is about the forward only. - The model checks open `apply_context_parallelism` instead of a `check` method. - A sequence length that `cp_size` does not divide is padded at its end to a multiple of it, in `ModelContextParallel.transform_inputs_pre_forward`, before the model builds its masks: `input_ids` get `pad_token_id` (or 0), `inputs_embeds` zeros, `attention_mask` zeros, `labels` and `shift_labels` -100, and `position_ids` continue those of the last document. The logits are this rank's slice of the padded sequence; the loss counts the real tokens only. Was #50515.
3outeille
force-pushed
the
cp-9-ep-x-cp
branch
from
November 12, 2026 10:09
aba53d9 to
a32f0d5
Compare
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 → #50496 → #50511 → #50514 → this PR → #50499 → #50521 → #50522
With context parallelism, a sequence length that
cp_sizedoes not divide used to raise. NowModelContextParallel, the style of the model itself (#50493), pads the full sequences at the end, before the model builds its masks:input_idsgetpad_token_id(or 0), andinputs_embedsget zeros. No real token attends to the padding, because it comes after them.position_ids, if given, continue those of the last document.attention_mask, if given, gets zeros.labelsandshift_labelsget -100, sonum_items_in_batchstill counts the real tokens only.outputs.logitsis this rank's slice of the padded sequence.Tests
tests/context_parallel, CPU (gloo):inputs_embeds, with a padding mask) and length 5 with cp=4;ModelContextParallelpads.