Repository navigation
Training SDPA: bucket sequence lengths where torch picks cuDNN, so padded batches stop rebuilding plans - #15134
Conversation
cuDNN attention builds a new plan for every new sequence length (~170 ms forward plus backward on H100). torch 2.14 picks cuDNN for training SDPA on H100, and padded batches change length every step, so nearly every step paid for a plan build. Causal calls with no mask now pad Q / K / V along the sequence to a multiple of UNSLOTH_SDPA_CUDNN_BUCKET (128; 0 disables) and slice the output, so the plan count is bounded by max_len / 128. Padded keys follow every real query, so causal masking hides them. Only applies where SDPA would pick cuDNN for the call (probed per shape), so A100 / L4 / RTX PRO 6000 (flash) and the SM100 no-cuDNN pin are unchanged.
|
@codex review |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 84c1042ff1
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| Q, K, V = (F.pad(x, (0, 0, 0, pad)) for x in (Q, K, V)) | ||
| out = scaled_dot_product_attention(Q, K, V, is_causal = True, **kwargs) |
There was a problem hiding this comment.
Disable local length bucketing during ring context parallelism
When ring context parallelism is active on a device where SDPA selects cuDNN (for example, H100 with torch 2.14), these tensors are local sequence shards, not complete sequences. ContextParallelManager.apply replaces F.scaled_dot_product_attention with the ring dispatcher, and _ring_sdpa_backends explicitly permits cuDNN on supported torch versions. Padding each shard here therefore inserts unmasked zero keys between real portions of the global sequence: later queries can attend to padding from earlier shards, changing softmax normalization and silently corrupting outputs and gradients. Slicing the local output does not remove those contributions. Exclude _ring_attention_active() calls from the bucketing gate, or implement globally aware padding and masking.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 6c83c4f: the bucketing gate now returns False whenever a context parallel manager is active (ring or ulysses), since the tensors can be sequence shards there. Covered by test_not_applied_under_context_parallelism[ring|ulysses].
|
@codex review |
|
Codex Review: Didn't find any major issues. Keep it up! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
On H100 with torch 2.14, SDPA picks cuDNN attention for training (
_fused_sdp_choicereturns cuDNN; torch 2.10 picks flash). cuDNN builds a new plan for every new sequence length, about 130 to 170 ms for forward plus backward. Padded batches change length almost every step, so that cost lands on nearly every step. #15019 handled the same issue on SM100 by keeping training off cuDNN there; H100 was not covered.Change
_training_sdpainunsloth/utils/attention_dispatch.pynow buckets the sequence length where the existing backend probe says SDPA would pick cuDNN.UNSLOTH_SDPA_CUDNN_BUCKET(default 128), runs the same SDPA call, and slices the output. The number of distinct plans is then bounded by max_len / 128.UNSLOTH_SDPA_CUDNN_BUCKET=0disables it.Results
Colab H100, Qwen3-0.6B 4-bit LoRA, padded batches with lengths 256 to 2048, batch size 8, 30 steps. Arms were interleaved 3 times with fresh caches per arm.
Tests
New
tests/test_sdpa_cudnn_length_bucket.py(16 tests):torch.compile(dynamic = True, fullgraph = True)across 4 lengths.All pass on B200 with torch 2.14.1 and 2.10.0.
14 attention test files, main vs this PR:
test_hf_packed_attention.py::test_varlen_matches_sdpa_over_the_dense_maskfailures also fail on main.Not covered