Repository navigation
Flex wrapper: send short no-grad queries to the main kernel where flex_decoding cannot compile - #16092
Conversation
…ecoding cannot compile unsloth_zoo decides at import whether torch's flex_decoding can compile against the installed Triton (Triton 3.9 removed the block pointers torch 2.9 - 2.12's decode template builds). The registered Transformers flex wrapper now reads that flag once and forces the main flex kernel for short no-grad queries there, as it already does for calls that need a backward; a caller's options stay on top and a named BACKEND is left alone. An unsloth_zoo without the check keeps AUTO.
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. |
…UTO goes to TRITON, legacy tests pin the decode flag [skip ci]
|
Codex Review: Didn't find any major issues. Hooray! 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". |
…icit AUTO for FORCE_USE_FLEX_ATTENTION so torch 2.9 takes the main kernel too [skip ci]
|
Codex Review: Didn't find any major issues. What shall we delve into next? 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". |
[skip ci]
|
@codex review |
|
Codex Review: Didn't find any major issues. Delightful! 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". |
…one short prompt does not leave the process on eager attention (unslothai#16049) ## Problem On sm86 / sm89 cards (L4, A10, RTX 3090 / 4090, A6000, L40S; 101376 B shared memory per block), Gemma 3 generation speed depends on which prompts the process has seen. After one prompt of 17 to 127 tokens (a normal chat prompt), later calls run flex attention's eager `math_attention` fallback. On torch 2.14 that holds for every later call, 16k-token prompts and one-token decode steps included. A process that has only seen prompts of 128 tokens or more runs the compiled kernels, until the first short prompt arrives. Root cause: 1. A no-grad flex call with fewer than 128 query rows goes to Inductor's `flex_decoding` kernel. That kernel holds `next_pow2(q_len * query heads per kv head)` query rows in one tile. With Gemma 3 1B / 270M (4 query heads per kv head) and head_dim 256, a 28-token prompt needs a 128-row tile, which takes 114688 B and does not compile: `No valid triton configs. OutOfMemoryError: out of resource: triton_tem_fused_flex_attention_0 Required: 114688 Hardware limit:101376`. 2. unslothai#15271 passes fitting tiles on these cards, but only for training calls and queries of 128 or more. Short no-grad queries were left on flex_decoding's own config, which fits only up to 64 rows. A one-token decode step is 16 rows. 3. unsloth_zoo sets `torch._dynamo.config.suppress_errors = True`. Dynamo turns the suppressed error into the default `ConvertFrameReturn`, which marks the frame `SKIP` and applies it to the code object (`torch/_dynamo/types.py`, `apply_to_code = True`). That code object is `torch.nn.attention.flex_attention.flex_attention` itself, so later calls run eagerly, and eager flex is `math_attention`. ## Change `unsloth/models/_utils.py`, on the same devices and head dims as unslothai#15271: sm8x with 101376 <= shared memory < 151552, 128 < head_dim <= 256, torch >= 2.10. - A no-grad short query whose flex_decoding tile would hold more than 64 rows now gets the unslothai#15271 tiles. - These calls also get `FORCE_USE_FLEX_ATTENTION`, so they run on the main flex kernel. Leaving them on flex_decoding with a smaller tile is not safe: on torch 2.10 its grid has no split along query rows, so rows past 64 are never written, and on 2.11 it fails to compile at q_len 127. - `_flex_decode_rows` counts rows the way Inductor does: query length times query heads per kv head. When the head count is not a power of two, transformers repeats K / V, so the multiplier is 1. What stays the same: - One-token decode and any call at 64 rows or less keep flex_decoding's own config. - Training calls and queries of 128 or more are handled exactly as before. - A100, H100, B200, sm120, T4 and ROCm are outside the gate, so nothing changes there. - `UNSLOTH_FLEX_SMALL_SMEM_TILES=0` still turns the whole path off. ## Results All arms ran in one Colab L4 job, each in its own process. Setup: `unsloth/gemma-3-1b-it` 16-bit, transformers 4.57.6. - Prefill is the median of `generate(max_new_tokens=1)`. - Decode is (t33 - t1) / 32. - Extra memory is peak allocated minus allocated before the call. - "short" is the 28-token chat prompt. Arms: - long-only: 2k, 2k, 8k, 16k. - short-first: short, 2k, 2k, short, 8k, 16k. - long-then-short: 2k, short, 2k, short, 8k, 16k. torch 2.14.1+cu130: | arm | prefill 2k / 8k / 16k s | decode ms/tok at 2k / 8k / 16k | short prompt decode ms/tok | 16k extra MiB | math_attention per prefill | suppressed compile errors | |---|---|---|---|---|---|---| | main, long-only | 0.085 / 0.319 / 0.687 | 70.7 / 71.1 / 71.0 | | 684 | 0 | 0 | | main, short-first | 0.255 / 2.865 / 11.35 | 238.5 / 239.9 / 243.4 | 240.3 | 13681 | 26 | 5 | | main, long-then-short | 0.249 / 2.881 / 11.45 | 234.5 / 243.5 / 241.8 | 233.6 | 13681 | 26 | 5 | | this PR, long-only | 0.085 / 0.345 / 0.755 | 70.5 / 70.5 / 70.7 | | 684 | 0 | 0 | | this PR, short-first | 0.088 / 0.351 / 0.760 | 69.0 / 70.8 / 70.2 | 67.2 | 684 | 0 | 0 | | this PR, long-then-short | 0.088 / 0.346 / 0.753 | 69.3 / 70.9 / 71.4 | 65.5 | 684 | 0 | 0 | torch 2.10.0+cu128: | arm | prefill 2k / 8k / 16k s | decode ms/tok at 2k / 8k / 16k | short prompt decode ms/tok | 16k extra MiB | math_attention per prefill | suppressed compile errors | |---|---|---|---|---|---|---| | main, long-only | 0.090 / 0.344 / 0.733 | 73.1 / 73.1 / 73.2 | | 684 | 0 | 0 | | main, short-first | 0.089 / 2.725 / 10.38 | 71.9 / 243.3 / 279.9 | 250.9 | 13681 | 26 from the second short prompt on | 5 | | main, long-then-short | 0.275 / 2.724 / 10.39 | 237.7 / 275.7 / 276.9 | 238.7 | 13681 | 26 | 4 | | this PR, long-only | 0.089 / 0.346 / 0.732 | 73.2 / 73.4 / 73.0 | | 684 | 0 | 0 | | this PR, short-first | 0.090 / 0.351 / 0.725 | 72.3 / 72.7 / 73.8 | 69.8 | 684 | 0 | 0 | | this PR, long-then-short | 0.090 / 0.344 / 0.738 | 73.8 / 72.4 / 73.3 | 69.7 | 684 | 0 | 0 | - **Every arm of this PR runs compiled flex on both torch versions:** 0 math calls and 0 suppressed errors, whatever the prompt order. The 28-token prompt decodes at 65-70 ms/token instead of 234-251. - **The 2.14 prefill gap is not from this change.** On 2.14, main long-only prefills 8k / 16k about 8% faster than this PR's arms. The PR does not change the kernel for these calls: q_len >= 128 is handled as before. Main long-only ran first on a cold card. On 2.10, where it did not run first, the same comparison is 0.344 / 0.733 s against 0.346 / 0.732 s. - **Long prompts:** for the 2k / 8k / 16k prompts, greedy ids (1 and 33 new tokens) are identical across all 12 arms, including main's eager arms. - **28-token prompt, torch 2.14:** this PR's ids equal main's in both orders. - **28-token prompt, torch 2.10:** - The first token matches in every arm. - Long-then-short equals main. - Short-first diverges from main later in the 8- and 33-token outputs. - Op level, the earlier review sweep covered 27 shapes on an L4 (torch 2.10, 2.11 and 2.14) and all are correct with this change. ## Torch version matrix (L4) | torch | short prompt on main | this PR | |---|---|---| | 2.14.1 | fails to compile, then every later call is eager | compiled, any order | | 2.11.0 | fails to compile (op level) | compiled at op level for the admitted shapes; Gemma 3 generate not run | | 2.10.0 | fails to compile, then later calls go eager | compiled, any order | Remaining gap on torch 2.10 / 2.11, not fixed here: when the batch size changes within a process, Dynamo makes the batch dimension dynamic. A one-token decode step then cannot use flex_decoding, and the main kernel's default head_dim 256 tile (151552 B) does not fit. That compile fails, and under `suppress_errors` it falls back to eager the same way. This happened on 2.10 and 2.11 at op level; on 2.14 it compiles. ## Tests - `tests/test_flex_small_smem_tiles.py`: new parametrized `test_wrapper_short_prompt_whose_decode_tile_does_not_fit_gets_tiles`, which also asserts `FORCE_USE_FLEX_ATTENTION`. The 4 cells that expect tiles fail on main and pass here. The cells for one-token decode, 64 rows, Gemma 3 4B shapes and a non power of two head count pass on both. - `test_wrapper_no_grad_prefill_gets_tiles_decode_does_not` now also asserts that q_len >= 128 gets no `FORCE_USE_FLEX_ATTENTION`. ## Review fixes and L4 re-check (torch 2.10.0, 2.11.0, 2.12.0, 2.13.0, 2.14.1) Two fixes in how the short-query path merges the caller's `kernel_options`: - **BACKEND with our FORCE:** torch 2.10 to 2.14 raise when `kernel_options` carry `BACKEND` together with `FORCE_USE_FLEX_ATTENTION`, and a caller `BACKEND` survived next to our FORCE. Now a caller `AUTO` is dropped and our FORCE kept, the same rule main's flex_decoding guard (unslothai#16092) uses, which also holds on torch 2.9 (no `BACKEND` there). A caller `TRITON` (the main kernel) keeps our tiles without FORCE. - **A non-main BACKEND:** a caller asking for `TRITON_DECODE` or `FLASH` now gets its own options untouched. Our 64-row main-kernel tiles would otherwise reach flex_decoding with more rows than they cover. Both have tests that fail without the fix. `tests/test_flex*.py`: 195 passed, 12 skipped. Main is merged in. Colab L4, one venv per torch version. Every output row and head is compared with fp32 SDPA (tolerance 2e-2): - The Gemma 3 1B prompt shape (28 tokens x 4 heads) and the Gemma 2 2B / Gemma 3 4B shapes at 40 and 100 tokens fail to compile by default on every version, and compile and match SDPA with this PR. head_dim 192 also compiles and matches. - fp32 head_dim 256 short prompts still fail to compile with the tiles; they stay on the eager fallback as before. Gemma 3 1B generate with a 25-token first prompt (eager fallback calls per generate step): | order | main 2.11 / 2.12 | main 2.13 | unslothai#16049 + unslothai#16073 | |---|---|---|---| | 25-token prompt, long prompt, 8-token prompt | 832 / 832 / 572 | 832 / 832 / 572 | 0 / 0 / 0 | | long prompt, 25-token prompt | 0 / 832 | 0 / 832 | 0 / 0 | | batch 1, 2, 1 | 0 / 832 / 832 | 0 / 0 / 832 | 0 / 0 / 0 | On torch 2.10, 2.11 and 2.12 a batch change still falls back without unslothai#16073 (806 and 832 calls on 2.10 for this PR alone), so the two should land together. ## After the main merge (L4, Gemma 3 1B, merge base 3ecb1ba) Main's own flex wrapper change (unslothai#16092) landed before these PRs merged it in, so the end-to-end orders were run again on the merged heads. The numbers are math_attention fallback calls per generate step, then ms per token per step: | torch | order | main | unslothai#16049 + unslothai#16073 | |---|---|---|---| | 2.14.1 | batch 1, 2, 1 | 0 / 0 / 832; 1289 / 1580 / 1050 | 0 / 0 / 0; 652 / 786 / 616 | | 2.14.1 | long then short | 0 / 832; 927 / 854 | 0 / 0; 477 / 374 | | 2.14.1 | short first | 832 / 832 / 572; 1194 / 665 / 504 | 0 / 0 / 0; 798 / 649 / 556 | | 2.10.0 | batch 1, 2, 1 | 0 / 832 / 832; 1201 / 1218 / 566 | 0 / 0 / 0; 1070 / 931 / 585 | | 2.10.0 | long then short | 0 / 832; 899 / 747 | 0 / 0; 623 / 320 | | 2.10.0 | short first | 832 / 832 / 572; 1033 / 618 / 513 | 0 / 0 / 0; 761 / 630 / 494 | The stack never reaches the eager fallback. Main does on every order, and on 2.14.1 now too. Greedy ids diverge from the SDPA reference in a few steps where main, on the eager fallback, matched. That is the bf16 flex vs SDPA divergence both arms show elsewhere, already listed above as pre-existing. ## AMD ROCm (gfx1151, Linux) The flex wrapper tests (`test_flex_small_smem_tiles.py`, `test_flex_small_smem_tiles_gpu.py`, `test_flex_decode_guard_wrapper.py`, `test_flex_tuned_backward_kernel_options.py`) ran on main (3ecb1ba), this branch and the merge, in one job on one host: main 97 passed, this branch 110 passed, the merge the same, 0 failed and 2 skipped in each. Verdict NO_REGRESSION. The GPU was shared, so this is correctness only, not timing.
…so a batch-size change does not leave the process on eager attention (unslothai#16073) Stacked on unslothai#16049 (base branch `perf/r11-flex-first-call`). Review and merge that one first; this PR only adds the commit on top. ## What changes On sm86 / sm89 cards with 101376 B of shared memory (L4, A10, RTX 3090 / 4090, L40S), unslothai#16049 stops a short prompt from pinning Gemma 3 flex attention to the eager fallback. One path is still open on torch 2.10 and 2.11: a batch size change between `generate` calls. - After batch 1 then batch 2, Dynamo marks the batch dim dynamic. `_use_flex_decoding` needs a static batch, so even the one-token decode step (4 query rows on Gemma 3 1B, not admitted by unslothai#16049) moves to the main flex kernel. - The main kernel's default head_dim 256 tile needs 151552 B, the compile fails (`Required: 151552 Hardware limit:101376`), unsloth_zoo's `suppress_errors` turns that into a Dynamo skip of `flex_attention`, and every later call in the process, batch 1 and long prompts included, runs eager `math_attention`. - torch 2.14 compiles the same call, so nothing changes there. The fix, in `_wrap_flex_attention_forward`: - On torch before 2.14, a no-grad call with a bf16 / fp16 head_dim 256 query on these cards now also gets the small-smem tiles when its query is shorter than 128 and its decode tile fits (64 rows or fewer). - These calls do not get `FORCE_USE_FLEX_ATTENTION`. With a static batch they stay on flex_decoding, now with the tiles. With a dynamic batch the main kernel runs with tiles that fit. - They also get plain `BLOCK_M` equal to `fwd_BLOCK_M` (64). Before torch 2.14, flex_decoding computes `SAFE_M_BOUNDARY` from its own `BLOCK_M`, which is next_pow2(rows), and only afterwards lets `fwd_BLOCK_M` replace the tile. At exactly 16 or 32 rows the 64-row tile then runs without row masks, and the output is wrong or NaN. Examples are Gemma 3 1B with a 4 or 8 token prompt, 4B / 12B / 27B with an 8 or 16 token prompt, and speculative decoding. With `BLOCK_M` set as well, the boundary is computed for the tile that actually runs. - fp32 and head_dim 192 are left alone. With the tiles, fp32 decode needs 147456 B, and head_dim 192 decode failed to build on 2.10. Gemma 3 is bf16 at head_dim 256. - Training calls are unchanged. The new check comes after the backward check, and the torch version is read once, at wrap time. ## Results ### 16 / 32 decode rows and 1B / 4B generate against SDPA This run used one Colab L4 (sm89, 101376 B), with torch 2.10.0, 2.11.0 and 2.14.1 in fresh venvs. Base is unslothai#16049; head is this PR. Op-level cells: - 13 static cells (batch 1 and 2, 1 to 64 rows, including 16 and 32), against an fp32 reference with tolerance 0.05. - 12 cells from dynamic batch sequences. | torch | unslothai#16049 static | unslothai#16049 dynamic | tiles without `BLOCK_M` static | this PR static | this PR dynamic | |---|---|---|---|---|---| | 2.10.0 | 13 / 13 | 6 / 12 (151552 B compile failures) | 3 / 13 (max error 1.2 to 2.7, or NaN) | 13 / 13 | 12 / 12 | | 2.11.0 | 13 / 13 | 6 / 12 | 3 / 13 | 13 / 13 | 12 / 12 | | 2.14.1 | 13 / 13 | 12 / 12 | 3 / 13 | 13 / 13 | 12 / 12 | On 2.14 the shipped rule leaves these calls untouched. The last two columns there only show that the options would also be correct. Generate setup: - Models: Gemma 3 1B it (16-bit) and Gemma 3 4B it (4-bit). - Prompts, in this order: 4, 8, 16 and 28 (chat) tokens at batch 1, then the 28-token prompt at batch 2 and batch 1, then 8 tokens at batch 2 and 4 tokens at batch 1. - Decode ms/token is the min of 2 repeats. - Reference: the same model with `UNSLOTH_ENABLE_FLEX_ATTENTION=0` (SDPA). | torch | model | arm | decode before the batch change | decode after it | suppressed errors | eager flex calls (last rep) | |---|---|---|---|---|---|---| | 2.10.0 | 1B | unslothai#16049 | 68.7 to 70.1 | 262.7 to 296.5 | 4 | 858 per generate | | 2.10.0 | 1B | this PR | 68.7 to 70.2 | 69.5 to 71.1 | 0 | 0 | | 2.10.0 | 4B | unslothai#16049 | 94.2 to 95.2 | 361.3 to 369.6 | 4 | 1122 per generate | | 2.10.0 | 4B | this PR | 96.7 to 99.1 | 95.0 to 97.0 | 0 | 0 | | 2.11.0 | 1B | unslothai#16049 | 71.8 to 72.8 | 283.3 to 318.7 | 5 | 858 per generate | | 2.11.0 | 1B | this PR | 74.3 to 74.7 | 74.6 to 76.4 | 0 | 0 | | 2.11.0 | 4B | unslothai#16049 | 101.9 to 103.3 | 343.7 to 362.3 | 5 | 1122 per generate | | 2.11.0 | 4B | this PR | 103.6 to 104.8 | 103.2 to 105.0 | 0 | 0 | | 2.14.1 | 1B | unslothai#16049 | 67.4 to 68.3 | 67.9 to 69.6 | 0 | 0 | | 2.14.1 | 1B | this PR | 66.2 to 67.2 | 66.3 to 68.7 | 0 | 0 | | 2.14.1 | 4B | unslothai#16049 | 95.2 to 96.3 | 95.5 to 97.5 | 0 | 0 | | 2.14.1 | 4B | this PR | 95.0 to 96.3 | 95.1 to 96.1 | 0 | 0 | Reading the table: - On 2.14 the two arms run the same code, so that row pair is an A/A. Its spread is up to about 1.5%. - Before a batch change, decode on 2.10 / 2.11 is between equal and about 3% slower with the tiles, depending on model and version. The earlier 1B run in the next section measured equal or faster. After a batch change, unslothai#16049 is 3.5x to 4x slower. Token ids against SDPA, in all 8 steps of every compiled arm: - Each run is either identical to SDPA over all 33 tokens or first differs at token 4 to 32. That is the same range unslothai#16049 itself shows on 2.14 (token 4 for the 1B 16-token prompt). - The text stays coherent ("..., invented by Johannes Gutenberg in the mid-15th c..."). - No step produced NaN or garbage. ### Batch change, first run All numbers come from one L4 job (sm89, 101376 B). Generate runs on Gemma 3 1B it, 16-bit weights, transformers 4.57.6, greedy decoding. The prompts are the 28-token chat prompt and a 2048-token prompt. Each step runs in this order: short B1, short B2, short B1, 2k B1, 2k B2, 2k B1. Decode is (t33 - t1) / 32 at the min of 3 repeats. Base is unslothai#16049. Decode ms/token by step: | torch | arm | short B1 | short B2 | short B1 | 2k B1 | 2k B2 | 2k B1 | suppressed errors | |---|---|---|---|---|---|---|---|---| | 2.10.0 | unslothai#16049 | 69.1 | 248.9 | 226.9 | 248.2 | 253.8 | 269.8 | 4 | | 2.10.0 | this PR | 69.0 | 68.9 | 67.2 | 71.1 | 73.3 | 71.9 | 0 | | 2.11.0 | unslothai#16049 | 75.2 | 294.4 | 266.4 | 288.6 | 300.0 | 288.4 | 5 | | 2.11.0 | this PR | 72.7 | 75.3 | 73.9 | 76.0 | 79.2 | 77.4 | 0 | | 2.14.1 | unslothai#16049 | 65.5 | 67.0 | 66.2 | 70.5 | 72.1 | 69.5 | 0 | | 2.14.1 | this PR | 65.5 | 67.2 | 65.9 | 70.0 | 72.4 | 70.2 | 0 | Prefill shows the same pattern. On unslothai#16049, 2k batch 1 prefill on torch 2.10 takes 0.26 s after the batch change, against 0.086 s with this PR. On 2.11 it is 0.29 s against 0.090 s. `math_attention` (eager flex) calls per process: - 13962 on unslothai#16049 with 2.10 / 2.11; - 3328 in every compiled arm. These 3328 are counted during compile tracing, and every compiled arm has them. Token ids: - On 2.10 and 2.11, this PR gives the same ids as torch 2.14 (both arms) in every step. - On unslothai#16049 with 2.10 / 2.11, the batch 2 short prompt ran eager. Its 33-token continuation equals the batch 1 one, while the compiled batch 2 continuation differs after a few tokens, at bf16 level, on every torch. - The 2k ids are identical in all arms. Op level (plain torch flex, against an fp32 reference, tolerance 0.05): - Static matrix: 74 bf16 / fp16 head_dim 256 cells. They cover query heads per kv head 1 to 4, q_len 1 to 64, kv 29 to 2112, causal and sliding 512 windows, batch 1 and 2, and softcap. All are correct on 2.10, 2.11 and 2.14 with the new rule; max abs error is 0.0044. 50 of them ran on flex_decoding, 24 on the main kernel. - Generate-like dynamic sequences through one `torch.compile`, Gemma 3 1B and 4B shapes with batch 1, 2 and 3: | rule | 2.10 / 2.11 | 2.14 | |---|---|---| | unslothai#16049 | 14 / 22 correct, the other 8 fail to compile at 151552 B | 22 / 22 | | this PR | 22 / 22 | 22 / 22 | One-token decode timing, static batch 1, which stays on flex_decoding. Each value is the median of 5 x 200 calls, given for each of 3 rounds: | torch | kv | default A | default A again | tiles | |---|---|---|---|---| | 2.10.0 | 4096 | 167.6 / 168.8 / 167.0 us | 171.6 / 169.9 / 171.0 | 171.8 / 171.0 / 170.6 | | 2.10.0 | 512 | 165.7 / 167.6 / 165.5 | 167.3 / 167.0 / 166.0 | 164.1 / 168.6 / 163.2 | | 2.11.0 | 4096 | 160.5 / 157.3 / 159.5 | 158.5 / 160.3 / 160.7 | 160.1 / 161.3 / 161.9 | | 2.11.0 | 512 | 161.9 / 160.8 / 159.4 | 163.0 / 161.9 / 162.0 | 164.9 / 162.8 / 162.4 | On 2.10 the tiles are inside the A/A spread. On 2.11 they are at most about 1% slower at op level. End to end, decode is not slower on either version (table above). On 2.14 the tiles measured about 3% slower at op level, which is one reason the rule stops at 2.14. ## Tests - `tests/test_flex_small_smem_tiles.py` adds: - the torch gate (2.10, 2.11, 2.13 on; 2.14.0 and 2.14.1 off); - short no-grad cells at 1 to 64 rows that get the tiles without FORCE, plus 68 and 127 row cells that keep FORCE; - the dtype / head_dim limits; - a check that the gate is not read off these cards. - A test pins `BLOCK_M` to `fwd_BLOCK_M` for the not-forced short calls. - The new decode cells fail on unslothai#16049 and pass here. - `tests/test_flex_*.py`: 192 passed, 13 skipped on unslothai#16049; 211 passed, 13 skipped here. No failing IDs on either side. - `py_compile` passes on Python 3.9 to 3.14. ## Not covered - Hardware other than an L4: A10, RTX 3090 / 4090 and L40S were not run. They have the same 101376 B limit. - Gemma 3 12B / 27B generate was not run (4B 4-bit was). - fp32 and head_dim 192 short calls with a dynamic batch on 2.10 / 2.11 can still fail to compile. They are left as before because the tiles do not fit or build there. - CUDA-only code path; multi-OS CI not run. With this PR on top of unslothai#16049, Gemma 3 flex generate on these cards stays compiled in every prompt order and batch sequence tested on torch 2.10 to 2.14.1. unslothai#16041 (Gemma 3 generate on SDPA) is still faster for decode on top of this stack; see below. ## L4 re-check on torch 2.10.0 to 2.14.1 Colab L4, one venv per torch version, every output row and head compared with fp32 SDPA (tolerance 2e-2), bf16 head_dim 256: | decode rows | default | tiles without `BLOCK_M` | tiles + `BLOCK_M=64` (this PR) | |---|---|---|---| | 16 | ok | wrong on every version (max err up to 9.1, NaN on 2.13) | ok | | 32 | ok | wrong on every version (NaN on 2.12 / 2.13) | ok | | 64 | ok | ok | ok | Batch 1, then 2 (dynamic), then 1: | torch | default | tiles + `BLOCK_M=64` | |---|---|---| | 2.10.0, 2.11.0, 2.12.0 | fails to compile (out of shared memory) | compiles, correct | | 2.13.0, 2.14.1 | compiles, correct | compiles, correct | The `< 2.14` gate is safe on 2.13 (the tiles are correct there) but wider than needed; on 2.10 steady decode with the tiles is 0.7% slower than main, inside the noise. Gemma 3 1B generate, batch 1, 2, 1 on torch 2.10: main and unslothai#16049 alone fall back to eager attention (806 and 832 calls in steps 2 and 3, batch-2 tokens diverge from SDPA); with this PR there are none and the batch-2 tokens match SDPA. Gemma 3 270M: main 558 / 504 fallback calls, this PR 0. Gemma 3 1B decode, 256 new tokens, ms per token, median of 3 rotated rounds on one L4 (the stack's own A/A spread was within 1.6%): | torch | model | main | unslothai#16049 + unslothai#16073 | + unslothai#16041 | |---|---|---|---|---| | 2.14.1 | 16-bit | 69.78 | 69.64 | 57.90 | | 2.14.1 | 4-bit | 73.68 | 73.70 | 62.69 | | 2.10.0 | 16-bit | 68.98 | 69.47 | 57.71 | | 2.10.0 | 4-bit | 72.97 | 72.88 | 61.72 | Not covered: RTX 50xx (excluded), A10 / RTX 3090 / 4090 / L40S, a GPU training run, and the fp32 default for head_dim 256 short prompts. ## After the main merge (L4, Gemma 3 1B, merge base 3ecb1ba) Main's own flex wrapper change (unslothai#16092) landed before these PRs merged it in, so the end-to-end orders were run again on the merged heads. The numbers are math_attention fallback calls per generate step, then ms per token per step: | torch | order | main | unslothai#16049 + unslothai#16073 | |---|---|---|---| | 2.14.1 | batch 1, 2, 1 | 0 / 0 / 832; 1289 / 1580 / 1050 | 0 / 0 / 0; 652 / 786 / 616 | | 2.14.1 | long then short | 0 / 832; 927 / 854 | 0 / 0; 477 / 374 | | 2.14.1 | short first | 832 / 832 / 572; 1194 / 665 / 504 | 0 / 0 / 0; 798 / 649 / 556 | | 2.10.0 | batch 1, 2, 1 | 0 / 832 / 832; 1201 / 1218 / 566 | 0 / 0 / 0; 1070 / 931 / 585 | | 2.10.0 | long then short | 0 / 832; 899 / 747 | 0 / 0; 623 / 320 | | 2.10.0 | short first | 832 / 832 / 572; 1033 / 618 / 513 | 0 / 0 / 0; 761 / 630 / 494 | The stack never reaches the eager fallback. Main does on every order, and on 2.14.1 now too. Greedy ids diverge from the SDPA reference in a few steps where main, on the eager fallback, matched. That is the bf16 flex vs SDPA divergence both arms show elsewhere, already listed above as pre-existing. ## AMD ROCm (gfx1151, Linux) The flex wrapper tests (`test_flex_small_smem_tiles.py`, `test_flex_small_smem_tiles_gpu.py`, `test_flex_decode_guard_wrapper.py`, `test_flex_tuned_backward_kernel_options.py`) ran on main (3ecb1ba), this branch and the merge, in one job on one host: main 97 passed, this branch 129 passed, the merge the same, 0 failed and 2 skipped in each. Verdict NO_REGRESSION. The GPU was shared, so this is correctness only, not timing.
Summary
Triton 3.9 removed block pointers, which torch 2.9 to 2.12's flex_decoding template still builds. unsloth_zoo #2266 detects that pair once at import (
unsloth_zoo.flex_attention.utils.FLEX_DECODING_UNAVAILABLE) and sends short queries to the main flex kernel in zoo's own flex callers. Otherwise the first decode fails to compile and flex attention runs eagerly with dense scores for the rest of the process.Unsloth's registered Transformers flex wrapper (
_wrap_flex_attention_forwardinunsloth/models/_utils.py) wraps Transformers' ownflex_attention_forward, which compiles torch's flex itself, so the zoo guard never reaches it. Today the wrapper forces the main kernel only for calls that need a backward. A short no-grad query (masked or static-cache decode, evaluation) still goes to flex_decoding through AUTO.This PR:
_flex_decoding_unavailable()). An unsloth_zoo without the check reads as False, so nothing changes with an older zoo.FORCE_USE_FLEX_ATTENTIONoption the wrapper already adds for calls that need a backward. On the small shared memory cards the wrapper's fitted main-kernel tiles apply to those calls too, but only when the main kernel is the one that runs.kernel_optionsstay on top. A caller that keeps flex_decoding (FORCE_USE_FLEX_ATTENTION: False, or aBACKENDother thanAUTO) is left alone and gets no fitted tiles.BACKEND: "AUTO"can still pick flex_decoding. Where the main kernel is forced, including calls that need a backward, it is dropped forFORCE_USE_FLEX_ATTENTION: torch 2.9 has noBACKENDoption ("TRITON"would not stop flex_decoding there), and torch 2.10+ rejectsBACKENDtogether with the legacy knob. A caller that also setsFORCE_USE_FLEX_ATTENTION: Falsekeeps its options as they are.kernel_optionsgiven as any mapping (for example a read-onlyMappingProxyType) is read as a dict, as torch itself does, so itsBACKENDis seen by every check.Tests
New
tests/test_flex_decode_guard_wrapper.py(CPU), 25 tests:FORCE_USE_FLEX_ATTENTIONonly when the flag is set, and no options otherwise;FORCE_USE_FLEX_ATTENTION: False;BACKENDis left alone, also inside a read-only mapping, and an explicitAUTOis dropped for the legacy knob where the main kernel is forced, checked against both torch 2.9's and torch 2.10 to 2.12's selection rules;The first 10 fail on main; the explicit
AUTOand tile cases (4) also fail on this PR's first version, and 11 cases (explicitAUTOon torch 2.9, mappings) fail on its second.CPU, with the existing flex wrapper tests (
test_flex_tuned_backward_kernel_options.py,test_flex_small_smem_tiles.py,test_flex_large_head_dim_kernel_options.py): 121 passed, 3 skipped. The existing tests now pin the flag to False, since they describe a host where flex_decoding compiles. With the flag forced to True, 6 of them failed before that change and pass after it.Not covered
MASTER_ADDRenv was not set #2266, for zoo's own callers.