Repository navigation
Flex attention: keep float32 Gemma 3 off the sm86 / sm89 small tiles - #15401
Merged
Merged
Conversation
The small-shared-memory tiles fit 2-byte dtypes only. At float32 the same tiles need 147456 B forward on an L4 (101376 B available) and Inductor raises No valid triton configs, so a float32 load keeps its previous 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. |
Member
Author
|
@codex review |
|
Codex Review: Didn't find any major issues. 👍 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]
danielhanchen
added a commit
that referenced
this pull request
Oct 27, 2026
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>
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.
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 adtype = torch.float32load on these cards now resolves toflex_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:InductorError: No valid triton configs. out of resource: triton_tem_fused_flex_attention_0 Required: 147456 Hardware limit: 101376Fix
_flex_preferred_fitstakes the load dtype. On a card that only fits flex through the small tiles it now returnsdtype 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_implementationalready receives the attention dtype from both loaders and now passes it through_supports_flex_attention.Before / after
tests/test_flex_preferred_shared_memory.py::test_l4_float32_gemma3_does_not_get_the_small_tilesflex_attentionfor float32)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.pyThe two flex unit files also pass on torch 2.10 and 2.14.1 (transformers 5.17).
Not covered