Repository navigation
Flex attention: fitting tiles for Gemma 3 on sm86 / sm89 (L4, A10, RTX 4090) instead of eager - #15271
Merged
Merged
Conversation
…X 4090) instead of eager Inductor's default flex tiles at 128 < head_dim <= 256 on sm86 / sm89 need 151552 (forward) and 135680 (backward) bytes of shared memory per block; these cards have 101376, so the compile fails and Gemma 3 was sent to eager. With torch >= 2.10, pass fwd_ / bwd_ kernel_options that fit (forward 64x64 one stage, backward 32x32 one stage) and keep Gemma 3 on flex there. UNSLOTH_FLEX_SMALL_SMEM_TILES=0 restores the eager route.
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. |
… only where default tiles fit
|
Codex Review: Didn't find any major issues. Already looking forward to the next diff. 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". |
This was referenced Oct 26, 2026
danielhanchen
added a commit
that referenced
this pull request
Oct 27, 2026
…15401) ## Problem #15271 lets Gemma 3 (head_dim 256) use flex attention on sm86 / sm89 cards (A10, L4, RTX 3090 / 4090) by passing smaller `fwd_*` / `bwd_*` tiles. Those tiles were sized for 2-byte dtypes. The load-time fit check (`_flex_preferred_fits`) has no dtype, so a `dtype = torch.float32` load on these cards now resolves to `flex_attention`, and the wrapper injects the same tiles for float32 queries. At float32 every tile doubles in size and Inductor cannot compile the kernel. Before #15271 the same load resolved to eager. Measured on a real L4 (sm89, 101376 B shared memory per block, torch 2.11.0+cu130, Triton 3.6.0), compiled flex forward + backward at `[1, 4, 512, 256]`, GQA 4/2, causal block mask: | Case | Result | |---|---| | main tiles, bfloat16 | OK, finite | | main tiles, float32 | `InductorError: No valid triton configs. out of resource: triton_tem_fused_flex_attention_0 Required: 147456 Hardware limit: 101376` | | default tiles, float32 | same error, Required: 135424 | | SDPA, float32 | OK, finite | ## Fix `_flex_preferred_fits` takes the load dtype. On a card that only fits flex through the small tiles it now returns `dtype is not torch.float32`, so a float32 load keeps its previous route (eager for Gemma 3, whose bundled SDPA is disabled by name). bf16 / fp16 / unspecified dtype loads are unchanged, and cards whose default template fits (A100, H100, B200) are unchanged at every dtype. `resolve_attention_implementation` already receives the attention dtype from both loaders and now passes it through `_supports_flex_attention`. ## Before / after | Test | origin/main | this branch | |---|---|---| | `tests/test_flex_preferred_shared_memory.py::test_l4_float32_gemma3_does_not_get_the_small_tiles` | FAIL (`flex_attention` for float32) | PASS | | `tests/test_flex_preferred_shared_memory.py`, `test_flex_small_smem_tiles.py`, `test_flex_attention_needs_ampere.py`, `test_flex_head_dim_version_gate.py`, `test_flex_large_head_dim_fallback.py` | 112 passed, 1 skipped | 113 passed, 1 skipped | The two flex unit files also pass on torch 2.10 and 2.14.1 (transformers 5.17). ## Not covered - No float32 tile set was searched for: float32 keeps the pre-#15271 route rather than a new, unmeasured kernel. - A float32 model trained under bf16 autocast would fit the tiles, but the load-time check cannot see autocast, so it takes the conservative route. - Only an L4 was run; A10 / RTX 3090 (sm86) share the 101376 B limit but were not executed. transformers 4.57.6 was not run (no unsloth_zoo in that environment). --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
danielhanchen
added a commit
that referenced
this pull request
Oct 28, 2026
…tile forced no-grad calls (#15529) **Update after merging main:** #15401 has since landed the plain float32 half of item 1 on main (`return dtype is not torch.float32` in `_flex_preferred_fits`). After merging main this PR keeps the rest: `UNSLOTH_FORCE_FLOAT32` (bf16 load, float32 Gemma 3 attention) is treated as float32 through `_flex_attention_runs_float32`, and forced no-grad main-kernel calls get the small tiles (item 2). The float32 test is now main's `test_l4_float32_gemma3_does_not_get_the_small_tiles`, extended with the `UNSLOTH_FORCE_FLOAT32` case. ## Summary Follow-up to #15271, which routes Gemma 3 (head_dim 256) to flex attention on sm86 / sm89 cards (L4, A10, RTX 3090 / 4090, L40S; 101376 B shared memory per block) with smaller tiles instead of eager. Three gaps in that change: 1. The routing gate does not look at the dtype. A float32 forward with the small tiles needs 147456 B of shared memory, so float32 Gemma 3 (and `UNSLOTH_FORCE_FLOAT32`, which runs Gemma 3 attention in float32) now picks flex on these cards where it used to run eager. The decoder blocks fail to compile (15 of 18 fall back to the stock forward, 3 to flex outside the block), and training is 6x to 7x slower than the eager route it had before. 2. The wrapper attaches the small tiles only when `query.shape[2] >= 128` or the call needs a backward. The per-block compile hook forces the main flex kernel (`FORCE_USE_FLEX_ATTENTION=True`) for every call inside a compiled block, including the no-grad first forward that Unsloth gradient checkpointing runs. For a padded batch shorter than 128 tokens that call gets Inductor's default head_dim 256 tiles (151552 B on torch 2.10), the block compile fails, and 15 of 18 Gemma-3-270m blocks fall back to the stock forward. 3. The same condition evaluates `query.shape[2] >= 128` before the grad check, so training calls add a sequence-length guard and a dynamic-shape block compiles one extra graph when the length crosses 128. ## Change `unsloth/models/_utils.py` only: - `_flex_preferred_fits(config, dtype)` takes the small-tile shortcut only when attention does not run in float32 (`_flex_attention_runs_float32`: a float32 load dtype, or `UNSLOTH_FORCE_FLOAT32=1`). `resolve_attention_implementation` already receives the load dtype and now passes it through `_supports_flex_attention`. float32 Gemma 3 on these cards goes back to eager; bf16 / fp16 keep flex. Cards whose default template fits (A100, H100, B200) are unchanged in every dtype. - New `_flex_main_kernel_forced(kwargs)`: the caller's `kernel_options` force the main kernel (`FORCE_USE_FLEX_ATTENTION=True` or `BACKEND="TRITON"`). Such calls get the small tiles like calls that need a backward. - The small-tile condition is reordered to grad, then forced, then `query.shape[2] >= 128`, so training and forced calls never evaluate the shape. ## Results Gemma-3-270m (`unsloth/gemma-3-270m-it`) LoRA r=8, `use_gradient_checkpointing="unsloth"`, padded batch of 4 at 41 tokens (attention mask kept), no autocast, 12 steps, Colab L4 (sm89, 101376 B). Base = main at 4f84e8f, fix = this branch. One run per arm; step time is the median of steps 4 to 12. | torch | dtype | arm | attention | blocks compiled (full / split / off) | block compile failures | step time | |---|---|---|---|---|---|---| | 2.10.0+cu128 | fp32 | base | flex_attention | 0 / 3 / 15 | yes, 147456 B > 101376 B | 844.7 ms | | 2.10.0+cu128 | fp32 | fix | eager | 18 / 0 / 0 | none | 141.7 ms | | 2.10.0+cu128 | bf16 | base | flex_attention | 0 / 3 / 15 | yes, 151552 B > 101376 B | 867.3 ms | | 2.10.0+cu128 | bf16 | fix | flex_attention | 18 / 0 / 0 | none | 114.8 ms | | 2.14.1+cu132 | fp32 | base | flex_attention | 0 / 3 / 15 | yes, 147456 B > 101376 B | 997.5 ms | | 2.14.1+cu132 | fp32 | fix | eager | 18 / 0 / 0 | none | 142.4 ms | | 2.14.1+cu132 | bf16 | base | flex_attention | 18 / 0 / 0 | none | 130.9 ms | | 2.14.1+cu132 | bf16 | fix | flex_attention | 18 / 0 / 0 | none | 121.5 ms | float32 losses match between arms to 4 decimals at every step. On torch 2.14 the default forward tile already fits for the short no-grad call, so the bf16 rows are the same route. The step time difference there is within single-run noise. Kernel check on the same L4 (compiled `flex_attention`, head_dim 256, GQA 4:1, `FORCE_USE_FLEX_ATTENTION=True`): | torch | dtype | call | default tiles | small tiles | |---|---|---|---|---| | 2.10 | fp32 | q_len 64 no-grad | fails, 135424 B | fails, 147456 B | | 2.10 | bf16 | q_len 64 no-grad | fails, 151552 B | runs | | 2.10 | bf16 | q_len 256 fwd + bwd | fails, 151552 B | runs | | 2.14 | fp32 | q_len 64 no-grad | runs | fails, 147456 B | | 2.14 | bf16 | q_len 64 no-grad | runs | runs | | 2.14 | bf16 | q_len 256 fwd + bwd | fails, 114688 B (backward) | runs | ## Tests New tests. Each fails on main and passes here: - `tests/test_flex_preferred_shared_memory.py::test_l4_float32_gemma3_does_not_get_the_small_tiles` (main's test, extended): a mocked sm89 card with 101376 B on torch 2.10 resolves float32 Gemma 3 to eager, bf16 / fp16 to flex, `UNSLOTH_FORCE_FLOAT32=1` to eager, and float32 on an A100 to flex as before. - `tests/test_flex_small_smem_tiles.py::test_wrapper_forced_main_kernel_no_grad_short_query_gets_tiles` (`FORCE_USE_FLEX_ATTENTION` and `BACKEND="TRITON"`): a no-grad q_len 64 call that forces the main kernel gets the small tiles. `BACKEND="AUTO"` does not. - `tests/test_flex_small_smem_tiles.py::test_wrapper_training_calls_add_no_sequence_guard`: a `dynamic=True` compiled block over q_len 256, 512, 64, 32 builds as many graphs with the small-tile gate on as with it off (main: 3 vs 2). Related suites (42 files that touch attention resolution, the flex wrapper, block compile or forced float32): 757 passed, 31 skipped, 12 failed with the fix. Main has the same 12 failures plus the 4 new tests. The 12 shared failures are unrelated to this change (sentence transformer config kwargs, `test_qwen3_5_gdn_block_has_no_break`, lfm2_vl flash scoping, stablelm TP oracle). ## Not covered - A10 / RTX 3090 / RTX 4090 / L40S were not run. They share the 101376 B limit and the gate, but only an L4 was measured. - torch 2.11 to 2.13 were not run on hardware. The tests mock the device and version. - fp32 flex with tiles that fit 101376 B was not searched. float32 keeps the eager route it had before #15271. On torch 2.14 the default fp32 forward tile fits for short no-grad calls, but a full fp32 training step on these cards was not validated on flex. - One run per arm, no A/A repeats. The 6x to 7.5x step time differences are far outside run-to-run noise, but the 2.14 bf16 difference is not a result. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
danielhanchen
added a commit
to Etherll/unsloth
that referenced
this pull request
Nov 1, 2026
…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.
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.
Gemma 3 trains on flex attention with head_dim 256. Since #14670, cards whose shared memory cannot hold Inductor's default head_dim 256 flex tiles send Gemma 3 to the eager label (SDPA). That covers every sm86 / sm89 card: A10, RTX 3090 / 4090, A6000, L4 and L40S, all at 101376 B per block.
On an L4 the default tiles do not fit:
SDPA works there, but with Gemma 3's 1024-token sliding window it has to take the window as a dense mask, which is slow.
Change (
unsloth/models/_utils.py)Tile set: a set that fits these cards and was the fastest that compiled on an L4:
When it applies (
_flex_small_smem_device_ok(), read once at wrap time):fwd_/bwd_keys from 2.10 on;Routing:
_flex_preferred_fitskeeps Gemma 3 on flex.UNSLOTH_FLEX_SMALL_SMEM_TILES=0restores today's SDPA route.What else is unchanged:
Results
Colab L4, Gemma-3-4B LoRA, bs 2 x 4096, 12 steps (4 warm-up). For torch 2.10 / 2.14, base is main (SDPA route) and head is this PR, each run twice; for torch 2.11-2.13 (after merge, one run each) it is
UNSLOTH_FLEX_SMALL_SMEM_TILES=0vs default on main. Each torch version runs in its own venv.attn_impl=flex_attentionand main runs eager; no "block compile failed" lines. On torch 2.11-2.13 the eager route had 8-10 graph breaks per run and the flex route 0.Tests
tests/test_flex_small_smem_tiles.py:tests/test_flex_small_smem_tiles_gpu.py: compiles forward + backward with the gate forced on, checks the kernels need under 101376 B (65544 / 65536 B), and checks the result against fp32 SDPA (cos > 0.999).test_flex_preferred_shared_memory.pyandtest_flex_tuned_backward_kernel_options.py(B200): 80 passed on torch 2.14; 79 passed and 1 skipped on torch 2.10.Not covered