Skip to content

Compiled blocks: keep the xformers stand-in SDPA off cuDNN attention - #15009

Merged
danielhanchen merged 2 commits into
mainfrom
dh/s4-padded-block-sdpa
Oct 23, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
dh/s4-padded-block-sdpa

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

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_forward at 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:

  • Llama median step: 170 ms. Qwen3: 261 ms, 4622 vs 3705 useful tokens/s against the stock forward.
  • 0 graph breaks.
  • Losses unchanged within noise.

Other GPUs do not pick cuDNN for these calls, so nothing changes there.

Not covered

  • B200 numbers come from a shared local card (no exclusive lease; Colab has no B200). Effect sizes are 1.5x to 2x, but there is no clean A/A on that card.
  • The no-xformers SM100 stock path has the same cuDNN per-length plan cost. Eager SDPA on B200 picks cuDNN too (micro test: 155 ms on a first-seen length, 1 ms on a repeat). That path is not changed here; pinning it would need its own A/B.
  • The one-time alignment recompile is not removed. Possible mitigations are padding batches to a multiple of 8 (TRL 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.
  • L4 was not rerun with per-step diagnostics, and the earlier L4 run had no stock-padded arm, so the L4 padded cost is not separated there.
  • H100 was not run.

Tests

tests/utils/test_attention_rerouted_sdpa.py checks 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).

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.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-23T17:41:43.571256Z 19a97c2 Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: 19a97c2268

ℹ️ 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".

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
danielhanchen merged commit 217fa4c into main Oct 23, 2026
34 of 37 checks passed
@danielhanchen
danielhanchen deleted the dh/s4-padded-block-sdpa branch October 23, 2026 20:32
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant