Repository navigation
Compiled blocks: keep the xformers stand-in SDPA off cuDNN attention - #15009
Merged
Merged
Conversation
Inside a compiled decoder block, plain causal xformers calls run as SDPA. On SM100 with torch 2.13+ SDPA then picks cuDNN attention, which builds a new plan for every new sequence length, so padded batches of varying length paid it in every step (Llama-3.2-1B LoRA, B200: 439 ms median step compiled vs 227 ms stock). Pin that call to flash / efficient / math, the kernels xformers itself runs; the backend list is built at import so tracing it adds no graph break.
Member
Author
|
@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. |
|
Codex Review: Didn't find any major issues. Keep them coming! 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". |
danielhanchen
added a commit
that referenced
this pull request
Oct 23, 2026
…#15018) `tests/test_source_read_encoding.py::test_checked_in_file_reads_name_an_encoding[8]` fails on `main`, for example in `Repo tests (CPU, rest)` on #15009 and #15010: ``` studio/backend/tests/test_pretokenized_dataset.py:313: read_text() ``` #14884 added a read of `core/training/trainer.py` with the platform default encoding. This passes `encoding = "utf-8"`. ## Verification - On `main`: `-k checked_in_file_reads` gives 1 failed, 15 passed. - On this branch: 16 passed. - `studio/backend/tests/test_pretokenized_dataset.py`: 56 passed.
danielhanchen
added a commit
to danielhanchen/unsloth-staging-2
that referenced
this pull request
Oct 24, 2026
…#15019) Stacked on unslothai#15009 (base branch `dh/s4-padded-block-sdpa`); merge that first. On SM100 GPUs with torch 2.13 and later, SDPA picks cuDNN attention. cuDNN builds a new execution plan for every new sequence length, about 150 ms forward plus backward. Padded training batches change length every step, so without xformers the stock training path paid that in nearly every step. Training calls now run SDPA on the flash, memory-efficient and math kernels on SM100: - The attention forwards pass their module's `.training` in `AttentionContext`, and `run_attention` hands it down as an argument (no global state). The flag also covers the no-grad forward of a checkpointed layer. - Eval and generation keep the default choice. `UNSLOTH_SDPA_TRAIN_CUDNN=1` restores cuDNN. - Under block compile and gradient checkpointing this adds no graph break and no recompile. Exclusive B200, no xformers, interleaved runs, useful tokens per second: | model, batches | before | after | |---|---|---| | Qwen3-0.6B LoRA, padded | 2777 / 3060 | 5164 / 5333 | | Llama-3.2-1B LoRA, padded | 3210 | 5192 (median step 291 to 138 ms) | | Llama-3.2-1B LoRA, padding-free | 7335 / 7442 | 7362 / 6978 (unchanged; this path already avoids cuDNN) | Losses match within run-to-run noise. ## Not covered FastModel and transformers' own attention modules call torch SDPA directly, so padded FastModel training on SM100 is not changed here and was not measured. H100 and ROCm were not run. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
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.
Inside a compiled decoder block, plain causal xformers calls run as SDPA. On SM100 GPUs with torch 2.13 and later, SDPA then picks cuDNN attention. cuDNN builds a new execution plan for every new sequence length, about 150 ms per length forward plus backward. So padded training batches of varying length paid that in every step.
Results
Llama-3.2-1B LoRA on B200: 439 ms median step with compiled blocks vs 227 ms with the stock forward. Qwen3-0.6B LoRA: 504 ms vs 331 ms. The profile shows
aten::_cudnn_attention_forwardat 5.4 ms of host time per call.Fix
This pins that one call to flash / efficient / math, the kernels xformers itself runs there. After the change:
Other GPUs do not pick cuDNN for these calls, so nothing changes there.
Not covered
pad_to_multiple_of), or warming both variants. Neither was tried. Its cost depends on compile workers: 20 to 44 s with the single-worker settings used here, less with the default parallel compile.Tests
tests/utils/test_attention_rerouted_sdpa.pychecks that the rerouted call runs under the flash / efficient / math pin and the plain SDPA path is unchanged; it fails on main. The attention CPU tests pass (77 passed, 6 skipped).