Repository navigation
[distributed] Context parallelism: padding masks, sliding windows and chunked attention with sdpa - #50516
Closed
3outeille wants to merge 2 commits into
Closed
[distributed] Context parallelism: padding masks, sliding windows and chunked attention with sdpa#505163outeille wants to merge 2 commits into
distributed] Context parallelism: padding masks, sliding windows and chunked attention with sdpa#505163outeille wants to merge 2 commits into
Conversation
This was referenced Nov 9, 2026
…Ulysses context parallelism The all-to-all gathers the sequence before every attention call, so the mask of the whole sequence can be built there from transformers' own mask primitives: causal, packed documents, padding, sliding window and chunked attention. - `_shard_inputs` keeps the full 2D padding mask on the implementation and passes `attention_mask=None` to the model. - `UlyssesCP.attention` rebuilds the `sdpa` mask for the whole sequence, or returns `None` when it is plain causal so that the kernel relies on `is_causal`. - flash kernels get the full 2D mask, which unpads and rebuilds the sequence boundaries. - `UlyssesCP.check` only refuses sliding-window layers whose window is not visible at the attention call, and `chunked_attention` layers with a flash kernel.
…h context parallelism - the mask rebuilt over the whole sequence is compared with the one the model builds on an unsharded run, for causal, packed, padded, sliding-window and chunked attention; - right and left padding batches, a sliding-window model, a mix of sliding and full layers, and a chunked model are compared with the same weights on full sequences (loss, this rank's logits, every gradient); - the flash padding test runs on 2 GPUs with `require_flash_attn`; - the checks now accept sliding windows and chunked attention with `sdpa`, and refuse what they cannot rebuild.
|
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-7-auto-pad
branch
from
November 9, 2026 14:37
4249c6d to
043fd08
Compare
This was referenced Nov 9, 2026
Member
Author
|
Not needed any more: with the plan-based rewrite the model builds its own masks over the whole sequence, so padding masks, sliding windows and chunked attention work without context parallel code (now tested in #50496). |
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 → #50515 → this PR → #50499 → #50521 (Trainer) → #50522 (docs)
After the all-to-all, attention sees the whole sequence, so the mask of the whole sequence can be rebuilt there. This PR rebuilds it with the
masking_utilsmask functions, so it matches what the model builds on an unsharded run. That lifts three refusals:attention_maskis kept for the full sequence.sdpagets the full mask. Flash kernels get the 2D mask and unpad, as on an unsharded run. 4D masks are still refused.sdpa: each layer's window comes from thesliding_windowthe model passes to the attention call, elsemodule.sliding_window, else the config for layers thatlayer_typesmarks as sliding.sdpa. It is still refused with flash kernels.With
sdpa, the whole-sequence mask takesbatch × seq × seqbooleans, so a flash kernel is the better choice for long sequences (the docs PR says so).Tests
tests/context_parallel, CPU (gloo). Loss, logits slice and every gradient match an unsharded run for: right and left padding, a sliding window withsdpa(unpacked and packed), a gemma3-stylelayer_typesmix, chunked attention, and padding combined with the automatic padding to a multiple ofcp_size.CP test from #50499 on every causal LM, with the tester configs unchanged:
afmoepacked andhrm_text(both fail on an unsharded or FSDP-only run too, see #50499),gemma4_text(float32 rounding of the embedding gradient; passes atatol=1e-4)On 2× H100 (Qwen3-0.6B, bf16), CP gives the same loss and logits as an unsharded run for batches with left and right padding (
sdpaand the FA3 hub kernel), and for alternating sliding-window layers withsdpa, unpacked and packed.