Repository navigation
head_dim 512 attention: per-GPU tiles, split dK / dV passes, TMA on sm12x - #17860
Merged
Merged
Conversation
…els on sm12x The autotuned configs were all 4 warps, mostly 1 stage, and the dK / dV pass held two [BLOCK_N, 512] fp32 accumulators. Timed on Colab at Gemma-4 global layer shapes (16 q heads, 1 kv head, head_dim 512, S 1K to 8K, causal / sliding / packed): - A100 (sm80): fixed tiles with 2-3 stages, dK and dV in two passes with one accumulator each. Kernel fwd+bwd 12% (1K) to 25% (8K causal) faster than main. - L4 class (sm8x, ~101 KB shared memory): fixed tiles, fused dK / dV pass (the split pass measured slower there end to end). - RTX PRO 6000 / sm12x: TMA tensor-descriptor kernels that load every [*, 512] tile in two head_dim halves (TMA box limit 256); 26-30% faster fwd+bwd at 4K and 8K. Fixed tiles launch the jit function directly, so each kernel compiles one variant instead of four autotune configs. Other GPUs, other head dims (zoo's Gemma-4 sliding band at 256) and UNSLOTH_LH_TILES=0 keep the autotuned fused path. A tile a GPU cannot fit falls back to autotune; a failing TMA launch falls back to the pointer kernels. Kill switches: UNSLOTH_LH_TILES=0, UNSLOTH_LH_SPLIT_DKDV=0, UNSLOTH_LH_TMA=0.
… head_dim 256 The same kernels serve zoo's Gemma-4 sliding layers past the 1024 window (16 q / 8 kv heads, head_dim 256). Timed on Colab at that shape (window 1024, S 2K and 8K): A100 fwd+bwd 28-32% faster with (64, 32) / (64, 64) tiles, RTX PRO 6000 21% with 3-stage tiles; the dK / dV pass stays fused and the TMA kernels stay head_dim 512 only. Tables are now per head_dim; untimed head dims and GPUs keep autotune.
Member
Author
|
@codex review |
Codex Review Summary: ✅ CompletedThis 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. More of your lovely PRs please. 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]
Penguinjanator
pushed a commit
to Penguinjanator/unsloth
that referenced
this pull request
Nov 16, 2026
…on without Triton, kernel and test gates (unslothai#17882) Backend CI on main has been red since today's merges. Every failure traces to one commit; this fixes the unsloth side (the gloo-cp shard also needs unslothai/unsloth-zoo#2542, since CI installs zoo from main). | CI shard / test | Cause | Fix | |---|---|---| | gloo: `test_qwen4exp_parallel_matches_one_process[pp*]`, `test_pp_splits_streams_and_runs_the_mixer_last`, all of `test_frozen_table_pp_gloo.py` | unslothai#17785: transformers 5.17 (the CI pin) names the Qwen4Exp layer type `qwen_sparse_attention`, 5.22 renamed it `indexed_attention`; pipeline parallel only accepted the new name, so every rank raised "hybrid layer stacks" | accept both names in `_PP_HYBRID_FAMILIES` | | diffusion (Python 3.13): 13 tests in `test_diffusion_int8_attention_triton_device.py` | unslothai#17855: `_launch` read `triton.runtime` while `triton` is `None` on that shard | check `triton is not None` first | | rest: `test_every_constexpr_line_has_a_comment` | unslothai#17860: 23 uncommented `tl.constexpr` parameters in `large_head_attention.py` | comments added; `_tma_tile`'s signature split one parameter per line | | rest: `test_every_launch_requests_fp_fusion` | unslothai#17860: the `_launch` helper forwards `enable_fp_fusion` from every caller, but the gate did not know it is a helper | registered like `layernorm.py`'s | | rest: `test_no_file_is_dropped_or_run_twice` | unslothai#17868: `tests/test_ddp_push_rank0_gloo.py` matched both the gloo shard and rest-2's `test_[dimsu]*.py` | renamed `tests/test_hub_push_rank0_ddp_gloo.py` | | rest-2: `test_checked_in_file_reads_name_an_encoding` | unslothai#17867: `read_text()` without an encoding in `tests/test_bind_local_cuda_device.py` | `encoding = "utf-8"` | | rest-2: `test_sentence_transformer_trainer_checks_the_placement_before_the_deferred_compile[*]` | unslothai#17859: `patch_train_dataloader` assumed every trainer class has `get_train_dataloader` | skip the patch when the method is missing | | rest-2: `test_router_aux_loss_gradient_survives_unsloth_checkpointing[*]` | unslothai#17873: the gpu-marked test still runs under CI's `-m 'not server and not e2e'` | `has_real_accelerator()` skip next to the gpu mark | | studio: `test_no_skip_guard_reads_a_spoofable_accelerator_probe` | unslothai#17850: `test_block_cudagraphs_side_grad.py` skipped on `torch.cuda.is_available()`, which the CUDA spoof fakes | skip on `has_real_cuda()` | ## Tests In a venv matching CI (Python 3.12, torch 2.10.0+cpu, transformers 5.17.0, TRL 1.15, PEFT 0.21.3, `UNSLOTH_COMPILE_DISABLE=1`, `-m 'not server and not e2e'`): - gloo: `test_qwen4exp_parallel_gloo.py` failed on main, 9 / 9 pass here; `test_frozen_table_pp_gloo.py` 13 / 13. - rest / rest-2 / studio: the nine affected files had exactly CI's 9 failures on main; 153 passed / 6 skipped here. - diffusion: with Triton blocked (as on the Python 3.13 shard), the 13 failures pass here; with Triton present everything passes. The only non-test code changes are the PP layer-type name, the Triton `None` check and the Sentence Transformers method check; `large_head_attention.py` changes are comments only.
shimmyshimmer
pushed a commit
to shimmyshimmer/unsloth-staging-4
that referenced
this pull request
Nov 17, 2026
… q/k RoPE int64 offsets, Gemma-4 band route int32 guard (unslothai#18141) Three fixes from a re-check of recent kernel PRs. 1. **Cold probes ask about capture on the operand's card (unslothai#17795, unslothai#17593, unslothai#17637).** The cold `verdict()` of the MiniMax-H3 row-mod kernel (`video_minimax_h3_rowmod.py`), the H3 q/k norm (`video_minimax_h3_qknorm.py`) and the FLUX.3 q/k RoPE (`diffusion_flux3_qk_rope.py`) read `torch.cuda.is_current_stream_capturing()` on the current card before switching to the operand's card. With another card current, the timing / equality probe could start inside a CUDA graph capture on the operand's card, then cache a failure or raise. The query now runs under `torch.cuda.device(x.device)`, with the probe. 2. **MOSS-Image fused q/k norm + RoPE: int64 row offsets (unslothai#17099).** The kernel multiplied the token index by the fused-QKV row stride in int32, which wraps past 2**31 (a 9216-wide row past about 233k tokens). The row index is now int64 before any stride product; output bits are unchanged. 3. **Block compile: the Gemma-4 sliding band route checks the interval kernel's int32 limit (unslothai#17817, unslothai#17860).** The long-band route in `unsloth/models/_block_compile.py` called `large_head_attention` directly and skipped the int32 span check the large-head router applies on its own path (`_shapes_qualify`), so a long enough sliding layer reached the int32-addressed kernel. The band route now keeps masked SDPA past that limit. ### GPU results, base (main 62d66cb) vs this branch Capture probes, Kaggle 2x T4 (sm75), torch 2.11.0+cu130: cuda:0 current, a `torch.cuda.graph` capture active on cuda:1, operand on cuda:1, cold call, probe runs counted. | probe | base | this branch | |---|---|---| | H3 row-mod `verdict` | probe ran inside the capture and cached `{exact: False, use: False}`; the next eager call still returns the cached failure | returns None, no probe, nothing cached; the next eager call computes `{exact: True, use: True}` | | H3 q/k norm `verdict` | probe ran inside the capture and the call raised `Cannot copy between CPU and CUDA tensors during CUDA graph capture` | returns None, no probe; the next eager call computes the real verdict | | FLUX.3 q/k RoPE `verdict` | probe ran inside the capture and cached a RuntimeError failure for cuda:1 for the process | returns None, nothing cached; the next eager call runs the real check | The captured graph itself replayed correctly in both arms; the defect is the probe running inside the capture and the cached failure or raise. MOSS and band route, L4 (sm89), Colab, torch 2.12.1+cu130, Triton 3.7.1: | fix | check | base | this branch | |---|---|---|---| | MOSS int64 offsets | fused kernel engaged (4 launches per arm); 1x4096x24, 2x1029x24, 1x16384x24, 1x37x4 | q / k / v hashes | identical to base; v equal to eager, q / k within 0.03125 of eager in both arms | | band route guard | S600 / w512 / hd256, S2048 / w512 / hd128, S4096 / w1024 / hd256 (kernel route), S256 / w512 (causal SDPA) | routes and output hashes | same routes, identical hashes; max error vs fp32 about 0.008 | ### Tests - `studio/backend/tests/test_diffusion_capture_probe_device.py` and `test_h3_row_mod.py::test_cold_verdict_asks_about_capture_on_the_operand_card`: on the T4 pair base 3 failed / 14 passed, this branch 17 passed (two-card tests included). - `studio/backend/tests/test_diffusion_moss_qk_rope_offsets.py` lowers the kernel to TTIR and rejects i32 row-stride products: fails on main; on the L4 base 1 failed / 8 passed, this branch 9 passed, and the existing MOSS fused GPU tests pass in both arms. - `tests/test_block_compile.py::test_gemma4_long_band_past_int32_keeps_the_plain_route`: fails on main; on the L4 base 1 failed / 83 passed, this branch 84 passed (4 skips, the same version gates in both arms). - CPU (torch 2.14.1) on this branch: 13 passed / 5 skipped in the probe and MOSS files, 74 passed / 14 skipped in `test_block_compile.py`. ### Not covered - The overflow sizes themselves (over 233k MOSS tokens, a band span past 2**31) cannot be allocated; the TTIR and CPU route tests are the evidence there. - A full MOSS-Image render with real weights; the op and its unit tests ran. - On fp16 / sm75 the FLUX.3 q/k RoPE check reports a real bit mismatch, so its fused path stays off on T4 regardless of this change; the capture fix is shown by the probe count, not by the fused path engaging there. - sm80 and sm120 cards; the capture probes ran on T4 only, MOSS and the band route on L4 only. - The GPU runs used main 62d66cb; the branch was then rebased onto main 142c398 and only the CPU tests were rerun there.
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.
Faster
large_head_attention(the head_dim 512 Triton kernel used by Gemma-4, and by zoo's Gemma-4 sliding layers at head_dim 256).What changed
H100, B200 and any GPU without a table keep main's autotuned path. A tile that does not fit falls back to autotune, and a failed TMA launch falls back to the regular kernels.
Kill switches:
UNSLOTH_LH_TILES=0,UNSLOTH_LH_SPLIT_DKDV=0,UNSLOTH_LH_TMA=0.Speed
Kernel, forward + backward vs main. Main repeated as a noise check was within about 1%.
End to end, gemma-4-12b QLoRA (real config, random weights; runs ordered base / head / head / base):
Alternatives checked
None of these has a head_dim 512 training kernel that beats this one:
Correctness
tests/test_large_head_attention.pypasses on B200 (torch 2.14.1 and 2.15) and on G4 (torch 2.10-2.14).