Repository navigation
Flex tuned backward: leave caller tiles and small sparse blocks to Inductor - #15228
Conversation
…ductor #15193 adds 32-wide bwd_* tiles to every sm80 flex call with 128 < head_dim <= 256 that needs a backward. Two valid calls then fail to compile: - a caller that sets some tiles itself (kernel_options={'bwd_BLOCK_M1': 64}) gets ours for the rest, and N1 = 32 is not a multiple of M1 = 64, which Inductor rejects; - a block mask with 16-wide sparse blocks, where Inductor's defaults shrink to fit but fixed 32-wide tiles cannot. Add the tuned tiles only when the caller set no tile / warp / stage option and every sparse block size is a multiple of them. The default 128-wide masks keep the tuned path.
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. |
|
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". |
…6-wide sparse blocks [skip ci]
|
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". |
…X 4090) instead of eager (#15271) 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: | torch | forward default | backward default | |---|---|---| | 2.10 | FlexConfig(32, 64, 3 stages), needs 151552 B: fails | (64, 64, 64, 64, 2 stages), needs 135680 B: fails | | 2.14 | retries a smaller tile itself and compiles | still needs 114688 B: fails | 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: - forward 64 x 64, 1 stage, 4 warps; - backward 32 x 32 everywhere, 1 stage, 4 warps. **When it applies** (`_flex_small_smem_device_ok()`, read once at wrap time): - the device conditions: - CUDA, not ROCm, with torch >= 2.10, since the templates read the `fwd_` / `bwd_` keys from 2.10 on; - every visible device is sm8x with 101376 <= shared memory < 151552; - the call: - 128 < head_dim <= 256; - it needs backward, or q_len >= 128, so short no-grad queries keep flex_decoding's own config; - the caller set no tiles of its own; - the block mask's sparse block size is a multiple of these tiles. This is the same check #15228 added for the A100 tuned backward. **Routing:** - With the gate on, `_flex_preferred_fits` keeps Gemma 3 on flex. - `UNSLOTH_FLEX_SMALL_SMEM_TILES=0` restores today's SDPA route. **What else is unchanged:** - A100 keeps the tuned backward from #15193. - H100 / B200 have room for the defaults. - T4 never reaches flex. - sm120 and mixed boxes stay on #14670's route. - ROCm is excluded. ### Results Colab L4, Gemma-3-4B LoRA, bs 2 x 4096, 12 steps (4 warm-up). Base is main (SDPA route) and head is this PR, each run twice. Each torch version runs as one VM job; the base and head trees are main at a2aca8d and this branch. | torch | quant | main s / step | this PR s / step | peak GB main -> PR | last loss main / PR | |---|---|---|---|---|---| | 2.14.1 | 16-bit | 10.18 / 10.18 | 5.13 / 5.12 | 10.13 -> 9.91 / 10.04 | 1.2136, 1.2130 / 1.2125, 1.2125 | | 2.14.1 | 4-bit | 10.32 / 10.26 | 5.19 / 5.19 | 6.37 -> 6.15 / 6.28 | 1.2234, 1.2233 / 1.2242, 1.2243 | | 2.10.0 | 16-bit | 10.24 / 10.25 | 5.25 / 5.24 | 10.16 -> 9.91 | 1.2078, 1.2081 / 1.2074, 1.2072 | | 2.10.0 | 4-bit | 10.36 / 10.37 | 5.30 / 5.31 | 6.40 -> 6.15 | 1.2181, 1.2183 / 1.2176, 1.2178 | - **Speed:** step time halves in every cell on both torch versions, with lower peak memory and last losses within 0.001 of main. - **Engagement:** the head runs `attn_impl=flex_attention` and main runs eager; no "block compile failed" lines. - **Accuracy:** - At op level on Gemma 3 shapes (causal + sliding, padded), the new tiles on L4 bf16 vs fp32 SDPA give a relative error of 0.0020-0.0025 for out, dq, dk and dv, the same as the default tiles on B200. - For the whole model (gemma-3-270m) vs an fp32 eager reference, the gradient relative error is 0.110 on L4, no worse than HF's own bf16 paths on the same check (eager 0.157, SDPA 0.116). ### Tests - **New `tests/test_flex_small_smem_tiles.py`:** - the device gate per card and torch version; - the kill switch and ROCm; - the tile ranges and their divisibility into the default sparse block; - wrapper routing: training calls, no-grad prefill vs decode, caller tiles left alone, sparse blocks our tiles do not divide, and the A100 tuned backward still winning. - **New `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). - **Results with `test_flex_preferred_shared_memory.py` and `test_flex_tuned_backward_kernel_options.py` (B200):** 80 passed on torch 2.14; 79 passed and 1 skipped on torch 2.10. ### Not covered - Real sm86 cards, RTX 4090 and L40S. They are covered by construction (same shared memory and capability class), not measured. - torch 2.11 to 2.13 timing. - Fp32 reference checks on Gemma 3 4B / 12B, and the image bidirectional mask. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Fixes a regression from #15193.
The tuned flex backward tiles (
bwd_BLOCK_M1/N1/M2/N2 = 32, 1 stage) are added on sm80 for 128 < head_dim <= 256. Two cases break the compile there. Inductor raisesInvalid FlexAttention backward kernel options:kernel_options = {"bwd_BLOCK_M1": 64}merged with ourbwd_BLOCK_N1 = 32breaksbwd_BLOCK_N1 % bwd_BLOCK_M1 == 0.The default 128-wide masks that transformers and Unsloth build are not affected, so this needs a custom mask or custom kernel options. Reproduced with real compiles on B200 with
UNSLOTH_FLEX_TUNED_BACKWARD=1.Change
_flex_tuned_bwd_fitsadds the tuned tiles only when both hold:BLOCK_SIZEof the block mask is a multiple of the tuned tiles.Otherwise Inductor's own choice is kept, as before #15193.
The docstring now says the env var is read at
import unsloth(where the flex forward is wrapped), not at model load.Tests
New cases in
tests/test_flex_tuned_backward_kernel_options.py:BACKEND) keep them; tiles are added only when every sparse block size is a multiple of 32.bwd_BLOCK_M1 = 64. Both fail on main with the Inductor error.On B200, torch 2.14.1:
On torch 2.10.0: 35 passed, 1 skipped. The 16-wide block case skips there because plain
torch.compile(flex_attention)cannot compile 16-wide sparse blocks on 2.10 at all, with or without Unsloth; 2.14 shrinks its own tiles.The other
tests/test_flex_*.pyfiles give the same counts on main and on this PR.Cost: the new check costs 1.8 us per flex call. It only runs where the tuned tiles could apply: sm80, torch >= 2.10, calls that need a backward.