Repository navigation
Flex small-smem tiles: keep float32 Gemma 3 off flex on sm86 / sm89, tile forced no-grad calls - #15529
Merged
Conversation
…tile forced no-grad calls Follow-up to #15271. - float32 (and UNSLOTH_FORCE_FLOAT32) attention needs 147456 B even with the small tiles, over the 101376 B of L4 / A10 / RTX 4090, so it keeps its previous eager route there. - The block compile hook forces the main flex kernel for no-grad checkpoint forwards too; those short-query calls now get the fitting tiles instead of Inductor's default ones. - Grad / forced checks run before query.shape[2] >= 128, so training calls add no sequence guard.
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. |
danielhanchen
added a commit
that referenced
this pull request
Oct 27, 2026
…the state's own order (#15548) `tests/python/test_docker_nb_refresh_state_durability.py::test_an_abandoned_restore_puts_the_tree_back` can fail on a Linux runner with no change involved, e.g. `Repo tests (CPU, studio)` on #15529 (job 122110779747, base ca640b8): ``` AssertionError: section 1b restored all 50 notebooks after deciding the state could not be written; the guard is evaluated once instead of per iteration ``` ## Cause The test deletes 50 notebooks and caps file size so that appends to the rewritten state start failing about 488 records in, then checks that section 1b stopped restoring after the first failed append. It picked the victims by name (the 50 highest), assuming they are the last 50 records. But `unsloth_sync_notebooks.sh` writes the state in `find` order, which is directory order. On XFS that happens to be creation order, so the victims straddle the cap and about 38 are restored. On ext4 (the runner's filesystem) directory order follows a per-filesystem hash, so the 50 highest names can all sit before the cap, every one of them is restored before any append fails, and the test reports a broken guard that is not broken. The script is right: it rewrites the state in its own order and stops restoring after the first failed append. Only the test's choice of victims depended on the filesystem. ## Change The victims are now the last 50 records in the state file's own order, so they straddle the cap whatever the directory order is. A new assert also requires at least one restore before the cap, so a setup where the victims miss the boundary fails loudly instead of passing without exercising the guard. ## Verification (local) - Reproduced on main by putting the 50 highest names first in the state, as ext4 can: "restored all 50", the same failure as CI. With the original order: 38 restored, pass. - This branch with the state as written, shuffled with four seeds, and with the highest names first: 6 of 6 pass. - Sabotage: removing the per-iteration `[ "$rs_ok" = 1 ] || continue` guard from the script fails the test in every one of those orders. - Whole file: 11 passed.
Member
Author
|
@codex review |
|
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". |
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.
Update after merging main: #15401 has since landed the plain float32 half of item 1 on main (
return dtype is not torch.float32in_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'stest_l4_float32_gemma3_does_not_get_the_small_tiles, extended with theUNSLOTH_FORCE_FLOAT32case.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:
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.query.shape[2] >= 128or 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.query.shape[2] >= 128before 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.pyonly:_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, orUNSLOTH_FORCE_FLOAT32=1).resolve_attention_implementationalready 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._flex_main_kernel_forced(kwargs): the caller'skernel_optionsforce the main kernel (FORCE_USE_FLEX_ATTENTION=TrueorBACKEND="TRITON"). Such calls get the small tiles like calls that need a backward.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.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):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=1to 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_ATTENTIONandBACKEND="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: adynamic=Truecompiled 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