Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Add a Triton head_dim 512 attention kernel for Gemma-4 global layers (#13869)

Daniel Han7c4f20ce1415
diff --git a/tests/test_large_head_attention.py b/tests/test_large_head_attention.py
new file mode 100644
index 00000000000..87d2e6ff242
--- /dev/null
+++ b/tests/test_large_head_attention.py
@@ -0,0 +1,387 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2023-present Daniel Han-Chen & the Unsloth team. All rights reserved.
+
+"""head_dim 512 attention (unsloth/kernels/large_head_attention.py).
+
+CPU: mask -> per-row key interval extraction, and the router leaving every unsupported call
+to the original SDPA. CUDA: fwd + bwd against fp32 SDPA math over causal / packed / padded /
+GQA shapes, and the router engaging on a Gemma-4 style call.
+"""
+
+import importlib.util
+import os
+
+import pytest
+import torch
+
+from real_accelerator import has_real_cuda # tests/_shared, on sys.path via tests/conftest.py
+
+pytest.importorskip("triton")
+
+_PATH = os.path.join(
+ os.path.dirname(__file__), "..", "unsloth", "kernels", "large_head_attention.py"
+)
+
+
+def _load():
+ spec = importlib.util.spec_from_file_location("_unsloth_large_head_attention", _PATH)
+ module = importlib.util.module_from_spec(spec)
+ spec.loader.exec_module(module)
+ return module
+
+
+LH = _load()
+
+
+def _causal(S):
+ i = torch.arange(S)
+ return i[None, :] <= i[:, None]
+
+
+def _packed(S, starts):
+ doc = torch.zeros(S, dtype = torch.long)
+ for s in starts:
+ doc[s:] += 1
+ return _causal(S) & (doc[None, :] == doc[:, None])
+
+
+def test_intervals_causal_packed_padded_float():
+ S = 37
+ lo, hi = LH.intervals_from_mask(_causal(S)[None, None], 1, S)
+ assert lo.tolist() == [[0] * S] and hi.tolist() == [list(range(S))]
+
+ lo, hi = LH.intervals_from_mask(_packed(S, [5, 6, 20])[None, None], 1, S)
+ assert lo[0, :5].tolist() == [0] * 5 and lo[0, 5].item() == 5 and lo[0, 6].item() == 6
+ assert lo[0, 20:].tolist() == [20] * (S - 20) and hi.tolist() == [list(range(S))]
+
+ i = torch.arange(S)
+ right_pad = (_causal(S) & (i[None, :] < 30))[None, None]
+ lo, hi = LH.intervals_from_mask(right_pad.expand(2, 1, S, S), 2, S)
+ assert hi[1, 29:].tolist() == [29] * (S - 29)
+
+ additive = torch.zeros(1, 1, S, S)
+ additive[~_packed(S, [9])[None, None]] = torch.finfo(torch.float32).min
+ lo, _ = LH.intervals_from_mask(additive, 3, S)
+ assert lo.shape == (3, S) and lo[2, 9].item() == 9
+
+
+def test_intervals_reject_unrepresentable_masks():
+ S = 16
+ i = torch.arange(S)
+ left_pad = _causal(S) & (i[None, :] >= 3) # rows 0..2 attend nothing
+ assert LH.intervals_from_mask(left_pad[None, None], 1, S) is None
+ holes = _causal(S).clone()
+ holes[10, 4] = False
+ assert LH.intervals_from_mask(holes[None, None], 1, S) is None
+ per_head = _causal(S)[None, None].expand(1, 2, S, S)
+ assert LH.intervals_from_mask(per_head, 1, S) is None
+ soft = torch.zeros(1, 1, S, S)
+ soft[0, 0, 5, 2] = -1.0 # a bias, not a mask
+ assert LH.intervals_from_mask(soft, 1, S) is None
+ assert LH.intervals_from_mask(_causal(S)[None, None, :, :8], 1, S) is None
+
+
+def test_mask_verdict_is_cached_and_invalidated():
+ S = 12
+ mask = _causal(S)[None, None].clone()
+ assert LH.intervals_from_mask(mask, 1, S) is not None
+ mask[0, 0, 7, 2] = False
+ assert LH.intervals_from_mask(mask, 1, S) is None
+
+
+def test_inference_mask_is_rescanned_after_in_place_update():
+ S = 12
+ with torch.inference_mode():
+ mask = _causal(S)[None, None].clone()
+ assert LH.intervals_from_mask(mask, 1, S) is not None
+ mask[0, 0, 7, 2] = False
+ assert LH.intervals_from_mask(mask, 1, S) is None
+ mask.copy_(_packed(S, [5, 7])[None, None])
+ lo, hi = LH.intervals_from_mask(mask, 1, S)
+ assert lo[0, 6].item() == 5 and hi[0, 4].item() == 4
+
+
+def _sentinel():
+ calls = []
+
+ def original(
+ module,
+ query,
+ key,
+ value,
+ attention_mask,
+ dropout = 0.0,
+ scaling = None,
+ is_causal = None,
+ **kwargs,
+ ):
+ calls.append(dict(dropout = dropout, scaling = scaling, is_causal = is_causal, **kwargs))
+ return "original", None
+
+ return calls, original
+
+
+class _Module(torch.nn.Module):
+ is_causal = True
+
+
+def test_router_defers_everything_it_cannot_run():
+ calls, original = _sentinel()
+ router = LH._make_router(original)
+ before = LH.LARGE_HEAD_ENGAGED["calls"]
+ q = torch.zeros(1, 4, 8, 512)
+ kv = torch.zeros(1, 2, 8, 512)
+ # CPU tensors, a 256 head, dropout and a softcap never reach the kernel.
+ assert router(_Module(), q, kv, kv, None, scaling = 1.0)[0] == "original"
+ small = torch.zeros(1, 4, 8, 256)
+ assert router(_Module(), small, small[:, :2], small[:, :2], None)[0] == "original"
+ assert router(_Module(), q, kv, kv, None, dropout = 0.1)[0] == "original"
+ assert router(_Module(), q, kv, kv, None, softcap = 30.0, sliding_window = None)[0] == "original"
+ assert len(calls) == 4
+ assert calls[0]["scaling"] == 1.0 and calls[3]["softcap"] == 30.0
+ assert LH.LARGE_HEAD_ENGAGED["calls"] == before
+
+
+def test_kill_switch_skips_install(monkeypatch):
+ monkeypatch.setenv("UNSLOTH_HD512_ATTENTION", "0")
+ LH._enabled.cache_clear()
+ try:
+ assert LH.patch_large_head_attention() is False
+ finally:
+ monkeypatch.delenv("UNSLOTH_HD512_ATTENTION")
+ LH._enabled.cache_clear()
+
+
+def _reference(q, k, v, allowed):
+ groups = q.shape[1] // k.shape[1]
+ k = k.repeat_interleave(groups, 1)
+ v = v.repeat_interleave(groups, 1)
+ scores = (q @ k.transpose(-1, -2)) * q.shape[-1] ** -0.5
+ scores = scores.masked_fill(~allowed, float("-inf"))
+ return (scores.softmax(-1) @ v).transpose(1, 2)
+
+
[email protected](not has_real_cuda(), reason = "needs CUDA")
[email protected]("S", [17, 64, 131])
[email protected]("heads", [(4, 4), (8, 2), (8, 1)])
[email protected]("kind", ["causal", "packed", "right_pad"])
+def test_kernel_matches_reference(S, heads, kind):
+ H, HKV = heads
+ dtype = torch.bfloat16 if torch.cuda.get_device_capability()[0] >= 8 else torch.float16
+ torch.manual_seed(S + H)
+ B, D = 2, 512
+ allowed = (
+ {
+ "causal": _causal(S),
+ "packed": _packed(S, [1, S // 3, S // 3 + 2]),
+ "right_pad": _causal(S) & (torch.arange(S)[None, :] < S - 5),
+ }[kind]
+ .cuda()[None, None]
+ .expand(B, 1, S, S)
+ )
+ q = torch.randn(B, S, H, D, device = "cuda", dtype = dtype).transpose(1, 2)
+ k = torch.randn(B, S, HKV, D, device = "cuda", dtype = dtype).transpose(1, 2)
+ v = torch.randn(B, S, HKV, D, device = "cuda", dtype = dtype).transpose(1, 2)
+ grad = torch.randn(B, S, H, D, device = "cuda", dtype = dtype)
+ lo, hi = LH.intervals_from_mask(allowed, B, S)
+
+ ours_in = [x.detach().clone().requires_grad_() for x in (q, k, v)]
+ out = LH.large_head_attention(*ours_in, lo, hi)
+ out.backward(grad)
+ ref_in = [x.detach().float().requires_grad_() for x in (q, k, v)]
+ ref = _reference(*ref_in, allowed)
+ ref.backward(grad.float())
+
+ for a, b in zip([out] + [x.grad for x in ours_in], [ref] + [x.grad for x in ref_in]):
+ assert torch.isfinite(a).all()
+ err = (a.float() - b).abs()
+ assert err.max() <= 0.03 * b.abs().max() + 0.02
+ assert err.mean() <= 0.01 * b.abs().mean() + 2e-3
+
+
[email protected](not has_real_cuda(), reason = "needs CUDA")
+def test_router_engages_on_gemma4_global_call():
+ calls, original = _sentinel()
+ router = LH._make_router(original)
+ dtype = torch.bfloat16 if torch.cuda.get_device_capability()[0] >= 8 else torch.float16
+ q = torch.randn(1, 8, 40, 512, device = "cuda", dtype = dtype)
+ kv = torch.randn(1, 1, 40, 512, device = "cuda", dtype = dtype)
+ before = LH.LARGE_HEAD_ENGAGED["calls"]
+ out, weights = router(_Module(), q, kv, kv, None, dropout = 0.0, scaling = 1.0, sliding_window = None)
+ assert weights is None and out.shape == (1, 40, 8, 512) and out.is_contiguous()
+ assert LH.LARGE_HEAD_ENGAGED["calls"] == before + 1 and not calls
+ left_pad = _causal(40).cuda() & (torch.arange(40, device = "cuda")[None, :] >= 3)
+ assert router(_Module(), q, kv, kv, left_pad[None, None])[0] == "original"
+
+
[email protected](not has_real_cuda(), reason = "needs CUDA")
+def test_router_defers_trainable_additive_mask():
+ calls, original = _sentinel()
+ router = LH._make_router(original)
+ dtype = _cuda_dtype()
+ q = torch.randn(1, 4, 24, 512, device = "cuda", dtype = dtype)
+ kv = torch.randn(1, 2, 24, 512, device = "cuda", dtype = dtype)
+ additive = torch.zeros(1, 1, 24, 24, device = "cuda", dtype = dtype)
+ additive[~_causal(24).cuda()[None, None]] = torch.finfo(dtype).min
+ before = LH.LARGE_HEAD_ENGAGED["calls"]
+ assert router(_Module(), q, kv, kv, additive)[0] != "original"
+ assert LH.LARGE_HEAD_ENGAGED["calls"] == before + 1
+ assert router(_Module(), q, kv, kv, additive.requires_grad_())[0] == "original"
+ assert LH.LARGE_HEAD_ENGAGED["calls"] == before + 1 and len(calls) == 1
+
+
+def _cuda_dtype():
+ return torch.bfloat16 if torch.cuda.get_device_capability()[0] >= 8 else torch.float16
+
+
[email protected](not has_real_cuda(), reason = "needs CUDA")
+def test_self_check_leaves_global_rng_alone(monkeypatch):
+ device, dtype = torch.device("cuda", torch.cuda.current_device()), _cuda_dtype()
+ LH._SELF_CHECK.pop((str(device), dtype), None)
+ seeded = []
+ monkeypatch.setattr(torch.cuda, "manual_seed_all", lambda seed: seeded.append(seed))
+ state = torch.cuda.get_rng_state(device)
+ assert LH._self_check(device, dtype) is True
+ assert not seeded # would reseed the model's other GPUs
+ assert torch.equal(torch.cuda.get_rng_state(device), state)
+
+
[email protected](not has_real_cuda(), reason = "needs CUDA")
+def test_self_check_under_inference_mode():
+ device, dtype = torch.device("cuda", torch.cuda.current_device()), _cuda_dtype()
+ LH._SELF_CHECK.pop((str(device), dtype), None)
+ with torch.inference_mode():
+ assert LH._self_check(device, dtype) is True
+
+
[email protected](not has_real_cuda(), reason = "needs CUDA")
+def test_patch_self_checks_so_compiled_graphs_use_the_kernel(monkeypatch):
+ from transformers.modeling_utils import ALL_ATTENTION_FUNCTIONS
+
+ device, dtype = torch.device("cuda", torch.cuda.current_device()), _cuda_dtype()
+ LH._SELF_CHECK.pop((str(device), dtype), None)
+ model = torch.nn.Linear(4, 4).to(device = device, dtype = dtype)
+ model.head_dim = 512
+ saved = ALL_ATTENTION_FUNCTIONS["sdpa"]
+ monkeypatch.setattr(LH, "_PATCHED", [False])
+ try:
+ assert LH.patch_large_head_attention(model) is True
+ finally:
+ ALL_ATTENTION_FUNCTIONS["sdpa"] = saved
+
+ graphs = []
+
+ def backend(gm, example_inputs):
+ graphs.append(gm.code)
+ return gm.forward
+
+ calls, original = _sentinel()
+ router = LH._make_router(original)
+ q = torch.randn(1, 4, 24, 512, device = device, dtype = dtype)
+ kv = torch.randn(1, 2, 24, 512, device = device, dtype = dtype)
+ torch._dynamo.reset()
+ out, _ = torch.compile(router, backend = backend)(_Module(), q, kv, kv, None, scaling = 512**-0.5)
+ assert not calls and out.shape == (1, 24, 4, 512)
+ assert any("large_head_attention_fwd" in code for code in graphs)
+ assert LH._SELF_CHECK.get((str(device), dtype)) is True
+
+
[email protected](not has_real_cuda(), reason = "needs CUDA")
+def test_router_defers_autocast_dtype_change():
+ calls, original = _sentinel()
+ router = LH._make_router(original)
+ q = torch.randn(1, 4, 24, 512, device = "cuda", dtype = torch.float16)
+ kv = torch.randn(1, 2, 24, 512, device = "cuda", dtype = torch.float16)
+ before = LH.LARGE_HEAD_ENGAGED["calls"]
+ with torch.autocast("cuda", dtype = torch.float16):
+ assert router(_Module(), q, kv, kv, None)[0] != "original"
+ assert LH.LARGE_HEAD_ENGAGED["calls"] == before + 1
+ with torch.autocast("cuda", dtype = torch.bfloat16):
+ assert router(_Module(), q, kv, kv, None)[0] == "original"
+ assert LH.LARGE_HEAD_ENGAGED["calls"] == before + 1 and len(calls) == 1
+
+
[email protected](not has_real_cuda(), reason = "needs CUDA")
+def test_router_honours_math_sdpa_selection():
+ from torch.nn.attention import SDPBackend, sdpa_kernel
+
+ calls, original = _sentinel()
+ router = LH._make_router(original)
+ dtype = _cuda_dtype()
+ q = torch.randn(1, 4, 24, 512, device = "cuda", dtype = dtype)
+ kv = torch.randn(1, 2, 24, 512, device = "cuda", dtype = dtype)
+ before = LH.LARGE_HEAD_ENGAGED["calls"]
+ with sdpa_kernel([SDPBackend.MATH]):
+ assert router(_Module(), q, kv, kv, None)[0] == "original"
+ if hasattr(torch._C, "_get_sdp_priority_order"):
+ with sdpa_kernel([SDPBackend.MATH, SDPBackend.EFFICIENT_ATTENTION], set_priority = True):
+ assert router(_Module(), q, kv, kv, None)[0] == "original"
+ with sdpa_kernel([SDPBackend.EFFICIENT_ATTENTION, SDPBackend.MATH]):
+ assert router(_Module(), q, kv, kv, None)[0] != "original"
+ assert LH.LARGE_HEAD_ENGAGED["calls"] == before + 1
+
+
+def test_router_keeps_wrapped_router_sentinels():
+ # unsloth_zoo's gemma-4 sliding router re-patches on every model load unless the current sdpa
+ # carries its sentinel; without it, it would wrap this router and the two would recurse.
+ mapping, orig = {}, [None]
+
+ def sdpa(module, query, key, value, attention_mask, **kwargs):
+ return "sdpa", None
+
+ def sliding(module, query, key, value, attention_mask, **kwargs):
+ return orig[0](module, query, key, value, attention_mask, **kwargs)
+
+ def patch_sliding():
+ if getattr(mapping["sdpa"], "_unsloth_gemma4_flash", False):
+ return
+ orig[0] = mapping["sdpa"]
+ sliding._unsloth_gemma4_flash = True
+ mapping["sdpa"] = sliding
+
+ mapping["sdpa"] = sdpa
+ patch_sliding()
+ router = LH._make_router(mapping["sdpa"])
+ mapping["sdpa"] = router
+ patch_sliding() # second model load
+ assert (
+ mapping["sdpa"] is router
+ and router._unsloth_large_head
+ and router._unsloth_original is sliding
+ )
+ q = torch.randn(1, 2, 4, 64)
+ assert mapping["sdpa"](None, q, q, q, None)[0] == "sdpa"
+
+
+def test_install_sits_beneath_the_packed_router(monkeypatch):
+ # A packed trainer built before the model load already installed the packed router; the route of a
+ # packed call must not depend on which of the two was installed first.
+ import sys
+ import types
+
+ from transformers.modeling_utils import ALL_ATTENTION_FUNCTIONS
+
+ def inner(module, query, key, value, attention_mask, **kwargs):
+ return "inner", None
+
+ packed = types.ModuleType("unsloth.utils.hf_packed_attention")
+ packed._ORIG_SDPA = [inner]
+
+ def packed_router(module, query, key, value, attention_mask, **kwargs):
+ return packed._ORIG_SDPA[0](module, query, key, value, attention_mask, **kwargs)
+
+ model = torch.nn.Linear(4, 4)
+ model.head_dim = 512
+ saved = ALL_ATTENTION_FUNCTIONS["sdpa"]
+ monkeypatch.setitem(sys.modules, "unsloth.utils.hf_packed_attention", packed)
+ monkeypatch.setattr(torch.cuda, "is_available", lambda: True)
+ monkeypatch.setattr(torch.version, "hip", None)
+ monkeypatch.setattr(LH, "_PATCHED", [False])
+ try:
+ ALL_ATTENTION_FUNCTIONS["sdpa"] = packed_router
+ assert LH.patch_large_head_attention(model) is True
+ assert ALL_ATTENTION_FUNCTIONS["sdpa"] is packed_router
+ assert packed._ORIG_SDPA[0]._unsloth_large_head
+ assert packed._ORIG_SDPA[0]._unsloth_original is inner
+ finally:
+ ALL_ATTENTION_FUNCTIONS["sdpa"] = saved
diff --git a/unsloth/kernels/large_head_attention.py b/unsloth/kernels/large_head_attention.py
new file mode 100644
index 00000000000..16cbf21b617
--- /dev/null
+++ b/unsloth/kernels/large_head_attention.py
@@ -0,0 +1,921 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.
+
+# Flash attention for head_dim > 256 (Gemma-4 global layers), where FA2 and SDPA's
+# flash / cuDNN backends stop. Tiling follows axolotl's flash_attn_d512.py (Apache-2.0,
+# https://github.com/axolotl-ai-cloud/axolotl); this one reads GQA kv heads in place and
+# takes any mask whose allowed keys per row are one interval [lo, hi], lo and hi
+# non-decreasing down the rows (causal, packed, right padding, sliding window).
+# UNSLOTH_HD512_ATTENTION=0 leaves SDPA untouched.
+
+import functools
+import os
+import sys
+
+import torch
+import triton
+import triton.language as tl
+
+__all__ = [
+ "large_head_attention",
+ "patch_large_head_attention",
+ "intervals_from_mask",
+ "LARGE_HEAD_ENGAGED",
+]
+
+# calls: kernel ran; fallback: a qualifying call went back to SDPA after an error.
+LARGE_HEAD_ENGAGED = {"calls": 0, "fallback": 0}
+_SUPPORTED_HEAD_DIMS = (512,)
+_INT32_MAX = 2**31 - 1
+
+
+def _fwd_configs():
+ return [
+ triton.Config({"BLOCK_M": bm, "BLOCK_N": bn}, num_warps = w, num_stages = s)
+ for bm, bn, w, s in (
+ (32, 32, 4, 1),
+ (32, 32, 4, 2),
+ (32, 16, 4, 1),
+ (16, 16, 4, 1),
+ )
+ ]
+
+
+def _bwd_configs():
+ return [
+ triton.Config({"BLOCK_M": bm, "BLOCK_N": bn}, num_warps = w, num_stages = 1)
+ for bm, bn, w in (
+ (16, 32, 4),
+ (16, 32, 8),
+ (32, 32, 8),
+ (16, 16, 4),
+ )
+ ]
+
+
[email protected](configs = _fwd_configs(), key = ["HEAD_DIM", "G"])
+def _lh_fwd(
+ Q,
+ K,
+ V,
+ O,
+ LSE,
+ LO,
+ HI,
+ qk_scale,
+ sqb,
+ sqh,
+ sqm,
+ skb,
+ skh,
+ skn,
+ svb,
+ svh,
+ svn,
+ sob,
+ soh,
+ som,
+ H,
+ G,
+ S,
+ HEAD_DIM: tl.constexpr,
+ BLOCK_M: tl.constexpr,
+ BLOCK_N: tl.constexpr,
+):
+ start_m = tl.program_id(0)
+ off_bh = tl.program_id(1)
+ off_b = (off_bh // H).to(tl.int64)
+ off_h = (off_bh % H).to(tl.int64)
+ off_hk = off_h // G
+ Q += off_b * sqb + off_h * sqh
+ K += off_b * skb + off_hk * skh
+ V += off_b * svb + off_hk * svh
+ O += off_b * sob + off_h * soh
+ LO += off_b * S
+ HI += off_b * S
+
+ offs_m = start_m * BLOCK_M + tl.arange(0, BLOCK_M)
+ offs_n = tl.arange(0, BLOCK_N)
+ offs_d = tl.arange(0, HEAD_DIM)
+ row_ok = offs_m < S
+ lo = tl.load(LO + offs_m, mask = row_ok, other = S)
+ hi = tl.load(HI + offs_m, mask = row_ok, other = -1)
+ # lo / hi are non-decreasing, so the block's key range is [lo[first], hi[last]].
+ n_start = (tl.load(LO + start_m * BLOCK_M) // BLOCK_N) * BLOCK_N
+ n_end = tl.load(HI + tl.minimum(start_m * BLOCK_M + BLOCK_M, S) - 1) + 1
+
+ q = tl.load(Q + offs_m[:, None] * sqm + offs_d[None, :], mask = row_ok[:, None], other = 0.0)
+ m_i = tl.full([BLOCK_M], float("-inf"), tl.float32)
+ l_i = tl.zeros([BLOCK_M], tl.float32)
+ acc = tl.zeros([BLOCK_M, HEAD_DIM], tl.float32)
+ for start_n in range(n_start, n_end, BLOCK_N):
+ cur = start_n + offs_n
+ col_ok = cur < S
+ k = tl.load(K + cur[None, :] * skn + offs_d[:, None], mask = col_ok[None, :], other = 0.0)
+ s = tl.dot(q, k) * qk_scale
+ keep = (cur[None, :] >= lo[:, None]) & (cur[None, :] <= hi[:, None])
+ s = tl.where(keep, s, float("-inf"))
+ m_new = tl.maximum(m_i, tl.max(s, 1))
+ # A row with no allowed key yet keeps m = -inf; exp2(-inf - -inf) would be NaN.
+ m_use = tl.where(m_new == float("-inf"), 0.0, m_new)
+ p = tl.exp2(s - m_use[:, None])
+ alpha = tl.exp2(m_i - m_use)
+ l_i = l_i * alpha + tl.sum(p, 1)
+ v = tl.load(V + cur[:, None] * svn + offs_d[None, :], mask = col_ok[:, None], other = 0.0)
+ acc = acc * alpha[:, None] + tl.dot(p.to(v.dtype), v)
+ m_i = m_new
+ l_safe = tl.where(l_i == 0.0, 1.0, l_i)
+ acc = acc / l_safe[:, None]
+ tl.store(
+ O + offs_m[:, None] * som + offs_d[None, :],
+ acc.to(O.dtype.element_ty),
+ mask = row_ok[:, None],
+ )
+ tl.store(LSE + off_bh.to(tl.int64) * S + offs_m, m_i + tl.log2(l_safe), mask = row_ok)
+
+
+def _lh_bwd_pre(
+ O,
+ DO,
+ DELTA,
+ sob,
+ soh,
+ som,
+ sdob,
+ sdoh,
+ sdom,
+ H,
+ S,
+ HEAD_DIM: tl.constexpr,
+ BLOCK_M: tl.constexpr,
+):
+ start_m = tl.program_id(0)
+ off_bh = tl.program_id(1)
+ off_b = (off_bh // H).to(tl.int64)
+ off_h = (off_bh % H).to(tl.int64)
+ offs_m = start_m * BLOCK_M + tl.arange(0, BLOCK_M)
+ offs_d = tl.arange(0, HEAD_DIM)
+ row_ok = offs_m < S
+ o = tl.load(
+ O + off_b * sob + off_h * soh + offs_m[:, None] * som + offs_d[None, :],
+ mask = row_ok[:, None],
+ other = 0.0,
+ )
+ do = tl.load(
+ DO + off_b * sdob + off_h * sdoh + offs_m[:, None] * sdom + offs_d[None, :],
+ mask = row_ok[:, None],
+ other = 0.0,
+ )
+ delta = tl.sum(o.to(tl.float32) * do.to(tl.float32), 1)
+ tl.store(DELTA + off_bh.to(tl.int64) * S + offs_m, delta, mask = row_ok)
+
+
[email protected](configs = _bwd_configs(), key = ["HEAD_DIM", "G"])
+def _lh_bwd_dq(
+ Q,
+ K,
+ V,
+ DO,
+ DQ,
+ LSE,
+ DELTA,
+ LO,
+ HI,
+ qk_scale,
+ sm_scale,
+ sqb,
+ sqh,
+ sqm,
+ skb,
+ skh,
+ skn,
+ svb,
+ svh,
+ svn,
+ sdob,
+ sdoh,
+ sdom,
+ sdqb,
+ sdqh,
+ sdqm,
+ H,
+ G,
+ S,
+ HEAD_DIM: tl.constexpr,
+ BLOCK_M: tl.constexpr,
+ BLOCK_N: tl.constexpr,
+):
+ start_m = tl.program_id(0)
+ off_bh = tl.program_id(1)
+ off_b = (off_bh // H).to(tl.int64)
+ off_h = (off_bh % H).to(tl.int64)
+ off_hk = off_h // G
+ Q += off_b * sqb + off_h * sqh
+ K += off_b * skb + off_hk * skh
+ V += off_b * svb + off_hk * svh
+ DO += off_b * sdob + off_h * sdoh
+ DQ += off_b * sdqb + off_h * sdqh
+ LO += off_b * S
+ HI += off_b * S
+
+ offs_m = start_m * BLOCK_M + tl.arange(0, BLOCK_M)
+ offs_n = tl.arange(0, BLOCK_N)
+ offs_d = tl.arange(0, HEAD_DIM)
+ row_ok = offs_m < S
+ lo = tl.load(LO + offs_m, mask = row_ok, other = S)
+ hi = tl.load(HI + offs_m, mask = row_ok, other = -1)
+ n_start = (tl.load(LO + start_m * BLOCK_M) // BLOCK_N) * BLOCK_N
+ n_end = tl.load(HI + tl.minimum(start_m * BLOCK_M + BLOCK_M, S) - 1) + 1
+
+ q = tl.load(Q + offs_m[:, None] * sqm + offs_d[None, :], mask = row_ok[:, None], other = 0.0)
+ do = tl.load(DO + offs_m[:, None] * sdom + offs_d[None, :], mask = row_ok[:, None], other = 0.0)
+ lse = tl.load(LSE + off_bh.to(tl.int64) * S + offs_m, mask = row_ok, other = 0.0)
+ delta = tl.load(DELTA + off_bh.to(tl.int64) * S + offs_m, mask = row_ok, other = 0.0)
+ dq = tl.zeros([BLOCK_M, HEAD_DIM], tl.float32)
+ for start_n in range(n_start, n_end, BLOCK_N):
+ cur = start_n + offs_n
+ col_ok = cur < S
+ k = tl.load(K + cur[:, None] * skn + offs_d[None, :], mask = col_ok[:, None], other = 0.0)
+ v = tl.load(V + cur[:, None] * svn + offs_d[None, :], mask = col_ok[:, None], other = 0.0)
+ s = tl.dot(q, tl.trans(k)) * qk_scale
+ keep = (cur[None, :] >= lo[:, None]) & (cur[None, :] <= hi[:, None])
+ p = tl.where(keep, tl.exp2(s - lse[:, None]), 0.0)
+ dp = tl.dot(do, tl.trans(v))
+ ds = p * (dp - delta[:, None])
+ dq += tl.dot(ds.to(k.dtype), k)
+ dq = dq * sm_scale
+ tl.store(
+ DQ + offs_m[:, None] * sdqm + offs_d[None, :],
+ dq.to(DQ.dtype.element_ty),
+ mask = row_ok[:, None],
+ )
+
+
[email protected](configs = _bwd_configs(), key = ["HEAD_DIM", "G"])
+def _lh_bwd_dkdv(
+ Q,
+ K,
+ V,
+ DO,
+ DK,
+ DV,
+ LSE,
+ DELTA,
+ LO,
+ HI,
+ QLO,
+ QHI,
+ qk_scale,
+ sm_scale,
+ sqb,
+ sqh,
+ sqm,
+ skb,
+ skh,
+ skn,
+ svb,
+ svh,
+ svn,
+ sdob,
+ sdoh,
+ sdom,
+ sdkb,
+ sdkh,
+ sdkn,
+ sdvb,
+ sdvh,
+ sdvn,
+ HKV,
+ G,
+ S,
+ HEAD_DIM: tl.constexpr,
+ BLOCK_M: tl.constexpr,
+ BLOCK_N: tl.constexpr,
+):
+ # One program owns a kv block of one kv head and loops over its G query heads,
+ # so GQA needs no atomics and no repeated K / V.
+ start_n = tl.program_id(0)
+ off_bkh = tl.program_id(1)
+ off_b = (off_bkh // HKV).to(tl.int64)
+ off_hk = (off_bkh % HKV).to(tl.int64)
+ H = HKV * G
+ K += off_b * skb + off_hk * skh
+ V += off_b * svb + off_hk * svh
+ DK += off_b * sdkb + off_hk * sdkh
+ DV += off_b * sdvb + off_hk * sdvh
+ LO += off_b * S
+ HI += off_b * S
+ QLO += off_b * S
+ QHI += off_b * S
+
+ offs_n = start_n * BLOCK_N + tl.arange(0, BLOCK_N)
+ offs_m = tl.arange(0, BLOCK_M)
+ offs_d = tl.arange(0, HEAD_DIM)
+ col_ok = offs_n < S
+ k = tl.load(K + offs_n[:, None] * skn + offs_d[None, :], mask = col_ok[:, None], other = 0.0)
+ v = tl.load(V + offs_n[:, None] * svn + offs_d[None, :], mask = col_ok[:, None], other = 0.0)
+ # Query rows attending any key of this block: [QLO[first key], QHI[last key]].
+ m_start = tl.load(QLO + start_n * BLOCK_N)
+ m_end = tl.load(QHI + tl.minimum(start_n * BLOCK_N + BLOCK_N, S) - 1) + 1
+ dk = tl.zeros([BLOCK_N, HEAD_DIM], tl.float32)
+ dv = tl.zeros([BLOCK_N, HEAD_DIM], tl.float32)
+ for g in range(G):
+ off_h = off_hk * G + g
+ Qh = Q + off_b * sqb + off_h * sqh
+ DOh = DO + off_b * sdob + off_h * sdoh
+ row_base = (off_b * H + off_h) * S
+ for start_m in range(m_start, m_end, BLOCK_M):
+ cur_m = start_m + offs_m
+ row_ok = cur_m < S
+ q = tl.load(
+ Qh + cur_m[:, None] * sqm + offs_d[None, :], mask = row_ok[:, None], other = 0.0
+ )
+ do = tl.load(
+ DOh + cur_m[:, None] * sdom + offs_d[None, :], mask = row_ok[:, None], other = 0.0
+ )
+ lse = tl.load(LSE + row_base + cur_m, mask = row_ok, other = 0.0)
+ delta = tl.load(DELTA + row_base + cur_m, mask = row_ok, other = 0.0)
+ lo = tl.load(LO + cur_m, mask = row_ok, other = S)
+ hi = tl.load(HI + cur_m, mask = row_ok, other = -1)
+ s = tl.dot(q, tl.trans(k)) * qk_scale
+ keep = (offs_n[None, :] >= lo[:, None]) & (offs_n[None, :] <= hi[:, None])
+ p = tl.where(keep, tl.exp2(s - lse[:, None]), 0.0)
+ dv += tl.dot(tl.trans(p.to(do.dtype)), do)
+ dp = tl.dot(do, tl.trans(v))
+ ds = p * (dp - delta[:, None])
+ dk += tl.dot(tl.trans(ds.to(q.dtype)), q)
+ dk = dk * sm_scale
+ tl.store(
+ DK + offs_n[:, None] * sdkn + offs_d[None, :],
+ dk.to(DK.dtype.element_ty),
+ mask = col_ok[:, None],
+ )
+ tl.store(
+ DV + offs_n[:, None] * sdvn + offs_d[None, :],
+ dv.to(DV.dtype.element_ty),
+ mask = col_ok[:, None],
+ )
+
+
+_LOG2E = 1.4426950408889634
+
+
+def _fwd_impl(q, k, v, lo, hi, scale):
+ B, H, S, D = q.shape
+ HKV = k.shape[1]
+ # (B, S, H, D) storage so the caller's transpose(1, 2).contiguous() is free.
+ o = torch.empty((B, S, H, D), device = q.device, dtype = q.dtype).transpose(1, 2)
+ lse = torch.empty((B, H, S), device = q.device, dtype = torch.float32)
+ with torch.cuda.device(q.device):
+ _lh_fwd[lambda meta: (triton.cdiv(S, meta["BLOCK_M"]), B * H)](
+ q,
+ k,
+ v,
+ o,
+ lse,
+ lo,
+ hi,
+ scale * _LOG2E,
+ q.stride(0),
+ q.stride(1),
+ q.stride(2),
+ k.stride(0),
+ k.stride(1),
+ k.stride(2),
+ v.stride(0),
+ v.stride(1),
+ v.stride(2),
+ o.stride(0),
+ o.stride(1),
+ o.stride(2),
+ H,
+ H // HKV,
+ S,
+ HEAD_DIM = D,
+ enable_fp_fusion = True,
+ )
+ return o, lse
+
+
+def _bwd_impl(do, q, k, v, o, lse, lo, hi, scale):
+ B, H, S, D = q.shape
+ HKV = k.shape[1]
+ G = H // HKV
+ if do.stride(-1) != 1:
+ do = do.contiguous()
+ dq = torch.empty_like(q)
+ dk = torch.empty_like(k)
+ dv = torch.empty_like(v)
+ delta = torch.empty((B, H, S), device = q.device, dtype = torch.float32)
+ positions = (
+ torch.arange(S, device = q.device, dtype = torch.int32).expand(lo.shape[0], S).contiguous()
+ )
+ # First query row whose interval ends at or after key j, last row starting at or before j.
+ qlo = torch.searchsorted(hi, positions, out_int32 = True)
+ qhi = torch.searchsorted(lo, positions, right = True, out_int32 = True) - 1
+ with torch.cuda.device(q.device):
+ _lh_bwd_pre[(triton.cdiv(S, 16), B * H)](
+ o,
+ do,
+ delta,
+ o.stride(0),
+ o.stride(1),
+ o.stride(2),
+ do.stride(0),
+ do.stride(1),
+ do.stride(2),
+ H,
+ S,
+ HEAD_DIM = D,
+ BLOCK_M = 16,
+ num_warps = 4,
+ enable_fp_fusion = True,
+ )
+ _lh_bwd_dkdv[lambda meta: (triton.cdiv(S, meta["BLOCK_N"]), B * HKV)](
+ q,
+ k,
+ v,
+ do,
+ dk,
+ dv,
+ lse,
+ delta,
+ lo,
+ hi,
+ qlo,
+ qhi,
+ scale * _LOG2E,
+ scale,
+ q.stride(0),
+ q.stride(1),
+ q.stride(2),
+ k.stride(0),
+ k.stride(1),
+ k.stride(2),
+ v.stride(0),
+ v.stride(1),
+ v.stride(2),
+ do.stride(0),
+ do.stride(1),
+ do.stride(2),
+ dk.stride(0),
+ dk.stride(1),
+ dk.stride(2),
+ dv.stride(0),
+ dv.stride(1),
+ dv.stride(2),
+ HKV,
+ G,
+ S,
+ HEAD_DIM = D,
+ enable_fp_fusion = True,
+ )
+ _lh_bwd_dq[lambda meta: (triton.cdiv(S, meta["BLOCK_M"]), B * H)](
+ q,
+ k,
+ v,
+ do,
+ dq,
+ lse,
+ delta,
+ lo,
+ hi,
+ scale * _LOG2E,
+ scale,
+ q.stride(0),
+ q.stride(1),
+ q.stride(2),
+ k.stride(0),
+ k.stride(1),
+ k.stride(2),
+ v.stride(0),
+ v.stride(1),
+ v.stride(2),
+ do.stride(0),
+ do.stride(1),
+ do.stride(2),
+ dq.stride(0),
+ dq.stride(1),
+ dq.stride(2),
+ H,
+ G,
+ S,
+ HEAD_DIM = D,
+ enable_fp_fusion = True,
+ )
+ return dq, dk, dv
+
+
+# Opaque custom ops keep torch.compile from tracing into Triton (no graph break, no
+# re-emission with fp fusion). The plain autograd.Function is the fallback for torch
+# builds without torch.library.custom_op.
+_HAS_CUSTOM_OP = (
+ hasattr(torch, "library")
+ and hasattr(torch.library, "custom_op")
+ and hasattr(torch.library, "register_autograd")
+)
+
+if _HAS_CUSTOM_OP:
+
+ @torch.library.custom_op("unsloth::large_head_attention_fwd", mutates_args = ())
+ def _fwd_op(
+ q: torch.Tensor,
+ k: torch.Tensor,
+ v: torch.Tensor,
+ lo: torch.Tensor,
+ hi: torch.Tensor,
+ scale: float,
+ ) -> tuple[torch.Tensor, torch.Tensor]:
+ return _fwd_impl(q, k, v, lo, hi, scale)
+
+ @_fwd_op.register_fake
+ def _fwd_op_fake(q, k, v, lo, hi, scale):
+ B, H, S, D = q.shape
+ o = q.new_empty((B, S, H, D)).transpose(1, 2)
+ return o, q.new_empty((B, H, S), dtype = torch.float32)
+
+ @torch.library.custom_op("unsloth::large_head_attention_bwd", mutates_args = ())
+ def _bwd_op(
+ do: torch.Tensor,
+ q: torch.Tensor,
+ k: torch.Tensor,
+ v: torch.Tensor,
+ o: torch.Tensor,
+ lse: torch.Tensor,
+ lo: torch.Tensor,
+ hi: torch.Tensor,
+ scale: float,
+ ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
+ return _bwd_impl(do, q, k, v, o, lse, lo, hi, scale)
+
+ @_bwd_op.register_fake
+ def _bwd_op_fake(do, q, k, v, o, lse, lo, hi, scale):
+ return torch.empty_like(q), torch.empty_like(k), torch.empty_like(v)
+
+ def _setup_context(ctx, inputs, output):
+ q, k, v, lo, hi, scale = inputs
+ o, lse = output
+ ctx.save_for_backward(q, k, v, o, lse, lo, hi)
+ ctx.scale = scale
+
+ def _backward(ctx, do, dlse):
+ q, k, v, o, lse, lo, hi = ctx.saved_tensors
+ dq, dk, dv = _bwd_op(do, q, k, v, o, lse, lo, hi, ctx.scale)
+ return dq, dk, dv, None, None, None
+
+ torch.library.register_autograd(
+ "unsloth::large_head_attention_fwd",
+ _backward,
+ setup_context = _setup_context,
+ )
+
+ def _attention(q, k, v, lo, hi, scale):
+ return _fwd_op(q, k, v, lo, hi, scale)[0]
+
+else:
+
+ class _LargeHeadAttention(torch.autograd.Function):
+ @staticmethod
+ def forward(ctx, q, k, v, lo, hi, scale):
+ o, lse = _fwd_impl(q, k, v, lo, hi, scale)
+ ctx.save_for_backward(q, k, v, o, lse, lo, hi)
+ ctx.scale = scale
+ return o
+
+ @staticmethod
+ def backward(ctx, do):
+ q, k, v, o, lse, lo, hi = ctx.saved_tensors
+ dq, dk, dv = _bwd_impl(do, q, k, v, o, lse, lo, hi, ctx.scale)
+ return dq, dk, dv, None, None, None
+
+ def _attention(q, k, v, lo, hi, scale):
+ return _LargeHeadAttention.apply(q, k, v, lo, hi, scale)
+
+
+def _dense_intervals(B, S, causal, device):
+ hi = (
+ torch.arange(S, device = device, dtype = torch.int32)
+ if causal
+ else torch.full((S,), S - 1, device = device, dtype = torch.int32)
+ )
+ lo = torch.zeros((B, S), device = device, dtype = torch.int32)
+ return lo, hi.expand(B, S).contiguous()
+
+
+def intervals_from_mask(
+ mask,
+ B,
+ S,
+ _rows = 1024,
+):
+ """(lo, hi) int32 [B, S] such that query i attends exactly keys lo[i]..hi[i], or None.
+
+ None unless every row of `mask` (bool, or additive float with 0 = keep) allows one
+ non-empty contiguous key range and both ends are non-decreasing down the rows. One
+ host sync; the verdict is cached on the mask so every layer sharing it reuses it.
+ Inference tensors have no version counter, so they are rescanned on every call.
+ """
+ if not torch.is_tensor(mask) or mask.dim() != 4 or mask.shape[1] != 1:
+ return None
+ if mask.shape[-2] != S or mask.shape[-1] != S or mask.shape[0] not in (1, B):
+ return None
+ if not (mask.dtype == torch.bool or mask.is_floating_point()):
+ return None
+ use_cache = not mask.is_inference()
+ key = (B, S, mask._version) if use_cache else None
+ cached = getattr(mask, "_unsloth_large_head_intervals", None) if use_cache else None
+ if cached is not None and cached[0] == key:
+ return cached[1]
+ Bm = mask.shape[0]
+ idx = torch.arange(S, device = mask.device, dtype = torch.int32)
+ lo = torch.empty((Bm, S), device = mask.device, dtype = torch.int32)
+ hi = torch.empty_like(lo)
+ ok = torch.ones((), device = mask.device, dtype = torch.bool)
+ # Row chunks keep the int32 temporaries at Bm * _rows * S elements.
+ for r0 in range(0, S, _rows):
+ m = mask[:, 0, r0 : r0 + _rows]
+ if m.dtype == torch.bool:
+ allowed = m
+ else:
+ allowed = m == 0
+ # Additive masks must only hold 0 or a value that zeroes the softmax weight.
+ ok = ok & ((allowed | (m <= torch.finfo(m.dtype).min / 2)).all())
+ lo_c = torch.where(allowed, idx, S).amin(-1)
+ hi_c = torch.where(allowed, idx, -1).amax(-1)
+ cnt = allowed.sum(-1, dtype = torch.int32)
+ ok = ok & (cnt > 0).all() & (cnt == hi_c - lo_c + 1).all()
+ lo[:, r0 : r0 + _rows] = lo_c
+ hi[:, r0 : r0 + _rows] = hi_c
+ ok = ok & (lo[:, 1:] >= lo[:, :-1]).all() & (hi[:, 1:] >= hi[:, :-1]).all()
+ result = None
+ if bool(ok.item()):
+ result = (lo.expand(B, S).contiguous(), hi.expand(B, S).contiguous())
+ if use_cache:
+ try:
+ mask._unsloth_large_head_intervals = (key, result)
+ except Exception:
+ pass
+ return result
+
+
+def large_head_attention(
+ q,
+ k,
+ v,
+ lo,
+ hi,
+ scale = None,
+):
+ """Attention over q [B, Hq, S, D], k / v [B, Hkv, S, D] where query i attends keys
+ lo[i]..hi[i] (int32 [B, S]). Returns [B, S, Hq, D] contiguous."""
+ if scale is None:
+ scale = q.shape[-1] ** -0.5
+ q, k, v = [x if x.stride(-1) == 1 else x.contiguous() for x in (q, k, v)]
+ out = _attention(
+ q, k, v, lo.to(torch.int32).contiguous(), hi.to(torch.int32).contiguous(), float(scale)
+ )
+ return out.transpose(1, 2)
+
+
[email protected]_cache(maxsize = 1)
+def _enabled():
+ return os.environ.get("UNSLOTH_HD512_ATTENTION", "1") != "0"
+
+
+def _shapes_qualify(query, key, value):
+ if query.dim() != 4 or key.dim() != 4 or value.dim() != 4:
+ return False
+ B, H, S, D = query.shape
+ if D not in _SUPPORTED_HEAD_DIMS or key.shape[-1] != D or value.shape[-1] != D:
+ return False
+ if key.shape[0] != B or value.shape[0] != B or key.shape[2] != S or value.shape[2] != S:
+ return False
+ HKV = key.shape[1]
+ if value.shape[1] != HKV or HKV == 0 or H % HKV != 0 or S < 2:
+ return False
+ if not (query.is_cuda and key.device == query.device and value.device == query.device):
+ return False
+ if query.dtype not in (torch.float16, torch.bfloat16):
+ return False
+ if key.dtype != query.dtype or value.dtype != query.dtype:
+ return False
+ return max(query.numel(), key.numel(), value.numel()) <= _INT32_MAX
+
+
+_SELF_CHECK = {}
+
+
+def _self_check(device, dtype):
+ """One small fwd + bwd against SDPA math per (device, dtype) before first use."""
+ key = (str(device), dtype)
+ if key in _SELF_CHECK:
+ return _SELF_CHECK[key]
+ passed = False
+ try:
+ # A local generator: torch.manual_seed would reseed every CUDA device's RNG.
+ gen = torch.Generator(device = device)
+ gen.manual_seed(0)
+ # Inference tensors cannot record autograd, even under enable_grad.
+ with torch.inference_mode(False), torch.enable_grad():
+ q, k, v, g = [
+ torch.randn(shape, device = device, dtype = dtype, generator = gen)
+ for shape in ((1, 4, 80, 512), (1, 2, 80, 512), (1, 2, 80, 512), (1, 80, 4, 512))
+ ]
+ q, k, v = [x.requires_grad_() for x in (q, k, v)]
+ lo, hi = _dense_intervals(1, 80, True, device)
+ out = large_head_attention(q, k, v, lo, hi, 512**-0.5)
+ grads = torch.autograd.grad(out, (q, k, v), g)
+ qf, kf, vf = [x.detach().float().requires_grad_() for x in (q, k, v)]
+ ref = torch.nn.functional.scaled_dot_product_attention(
+ qf,
+ kf.repeat_interleave(2, 1),
+ vf.repeat_interleave(2, 1),
+ is_causal = True,
+ ).transpose(1, 2)
+ ref_grads = torch.autograd.grad(ref, (qf, kf, vf), g.float())
+ passed = all(
+ torch.isfinite(a).all().item()
+ and ((a.float() - b).abs().max() <= 0.05 * b.abs().max() + 0.02).item()
+ for a, b in zip((out, *grads), (ref, *ref_grads))
+ )
+ except torch.cuda.OutOfMemoryError:
+ raise
+ except Exception as exc:
+ _warn_once(
+ f"Unsloth: head_dim 512 attention kernel failed its self check ({exc}); using SDPA."
+ )
+ passed = False
+ _SELF_CHECK[key] = passed
+ return passed
+
+
+_WARNED = set()
+
+
+def _warn_once(message):
+ if message in _WARNED:
+ return
+ _WARNED.add(message)
+ try:
+ from transformers.utils import logging as hf_logging
+ hf_logging.get_logger("unsloth").warning(message)
+ except Exception:
+ print(message)
+
+
+def _autocast_dtype_differs(query):
+ # SDPA follows autocast; the kernel computes in the input dtype.
+ try:
+ enabled = torch.is_autocast_enabled(query.device.type)
+ dtype = torch.get_autocast_dtype(query.device.type) if enabled else None
+ except (AttributeError, TypeError):
+ enabled = torch.is_autocast_enabled()
+ dtype = torch.get_autocast_gpu_dtype() if enabled else None
+ return enabled and dtype != query.dtype
+
+
+def _math_sdpa_requested():
+ """A disabled memory efficient backend or a math-first sdpa_kernel priority asks for SDPA itself."""
+ cuda = torch.backends.cuda
+ if not cuda.mem_efficient_sdp_enabled():
+ return True
+ order = list(getattr(torch._C, "_get_sdp_priority_order", list)())
+ from torch.nn.attention import SDPBackend
+
+ math, efficient = int(SDPBackend.MATH), int(SDPBackend.EFFICIENT_ATTENTION)
+ return math in order and efficient in order and order.index(math) < order.index(efficient)
+
+
+if hasattr(torch.compiler, "assume_constant_result"):
+ _math_sdpa_requested = torch.compiler.assume_constant_result(_math_sdpa_requested)
+
+
+def _make_router(original):
+ def _sdpa_large_head(
+ module,
+ query,
+ key,
+ value,
+ attention_mask,
+ dropout = 0.0,
+ scaling = None,
+ is_causal = None,
+ **kwargs,
+ ):
+ if (
+ _enabled()
+ and torch.is_tensor(query)
+ and query.dim() == 4
+ and query.shape[-1] > 256
+ and not dropout
+ # The kernel only reads the mask's structure, so a trainable mask would get no grad.
+ and not (torch.is_tensor(attention_mask) and attention_mask.requires_grad)
+ and kwargs.get("position_bias", None) is None
+ and kwargs.get("softcap", None) is None
+ and not kwargs.get("output_attentions", False)
+ and torch.version.hip is None
+ and _shapes_qualify(query, key, value)
+ and not _autocast_dtype_differs(query)
+ and not _math_sdpa_requested()
+ ):
+ B, S = query.shape[0], query.shape[2]
+ intervals = None
+ if attention_mask is None:
+ causal = is_causal if is_causal is not None else getattr(module, "is_causal", True)
+ intervals = _dense_intervals(B, S, bool(causal), query.device)
+ elif not torch.compiler.is_compiling():
+ # Reading a mask's structure needs a host sync; compiled graphs keep SDPA.
+ intervals = intervals_from_mask(attention_mask, B, S)
+ compiling = torch.compiler.is_compiling()
+ if compiling:
+ checked = _SELF_CHECK.get((str(query.device), query.dtype), False)
+ elif intervals is not None:
+ checked = _self_check(query.device, query.dtype)
+ if intervals is not None and checked:
+ scale = scaling if scaling is not None else query.shape[-1] ** -0.5
+ if compiling:
+ return large_head_attention(
+ query, key, value, *intervals, scale
+ ).contiguous(), None
+ try:
+ out = large_head_attention(query, key, value, *intervals, scale).contiguous()
+ LARGE_HEAD_ENGAGED["calls"] += 1
+ return out, None
+ except torch.cuda.OutOfMemoryError:
+ raise
+ except Exception as exc:
+ LARGE_HEAD_ENGAGED["fallback"] += 1
+ _SELF_CHECK[(str(query.device), query.dtype)] = False
+ _warn_once(
+ f"Unsloth: head_dim 512 attention kernel failed ({exc}); using SDPA."
+ )
+ return original(
+ module,
+ query,
+ key,
+ value,
+ attention_mask,
+ dropout = dropout,
+ scaling = scaling,
+ is_causal = is_causal,
+ **kwargs,
+ )
+
+ # Keep the wrapped router's sentinels (unsloth_zoo's gemma-4 sliding router checks its own), so a
+ # second model load does not wrap this router again and recurse between the two.
+ for name, value in vars(original).items() if hasattr(original, "__dict__") else ():
+ if name.startswith(("_unsloth_", "__unsloth_")):
+ setattr(_sdpa_large_head, name, value)
+ _sdpa_large_head._unsloth_large_head = True
+ _sdpa_large_head._unsloth_original = original
+ return _sdpa_large_head
+
+
+_PATCHED = [False]
+
+
+def patch_large_head_attention(model = None):
+ """Route transformers' `sdpa` attention through the large head kernel when a call
+ qualifies; everything else reaches the previous `sdpa` function unchanged. With a
+ model, only installs if one of its modules has head_dim > 256."""
+ if not _enabled() or not torch.cuda.is_available() or torch.version.hip is not None:
+ return False
+ if model is not None:
+ head_dims = (getattr(m, "head_dim", 0) for m in model.modules())
+ if not any(isinstance(d, int) and d in _SUPPORTED_HEAD_DIMS for d in head_dims):
+ return False
+ # Compiled graphs only read the cached verdict, so check before any tracing.
+ try:
+ targets = {
+ (p.device, p.dtype)
+ for p in model.parameters()
+ if p.is_cuda and p.dtype in (torch.float16, torch.bfloat16)
+ }
+ except Exception:
+ targets = set()
+ for device, dtype in targets:
+ _self_check(device, dtype)
+ try:
+ from transformers.modeling_utils import ALL_ATTENTION_FUNCTIONS
+ current = ALL_ATTENTION_FUNCTIONS["sdpa"]
+ except Exception:
+ return False
+ # Other wrappers (unsloth_zoo's gemma-4 sliding router) may sit on top of ours.
+ if _PATCHED[0] or getattr(current, "_unsloth_large_head", False):
+ return True
+ # unsloth/utils/hf_packed_attention.py sends packed rows (packed_seq_lengths) past head dim 256 to
+ # SDPA's efficient kernel over cu_seqlens. Sit beneath it whichever is installed first, so a packed
+ # call takes the same route as when a packed trainer is built after the model load.
+ packed = sys.modules.get("unsloth.utils.hf_packed_attention")
+ packed_orig = getattr(packed, "_ORIG_SDPA", None)
+ if isinstance(packed_orig, list) and packed_orig and packed_orig[0] is not None:
+ packed_orig[0] = _make_router(packed_orig[0])
+ else:
+ # Direct assignment: register() does not update the global mapping layers read.
+ ALL_ATTENTION_FUNCTIONS["sdpa"] = _make_router(current)
+ _PATCHED[0] = True
+ return True
diff --git a/unsloth/models/vision.py b/unsloth/models/vision.py
index f4111afdab1..d5574bba6e5 100644
--- a/unsloth/models/vision.py
+++ b/unsloth/models/vision.py
@@ -4466,6 +4466,13 @@ class FastBaseModel:
f"({type(_exc).__name__}: {_exc})."
)
_install_bnb_nf4_override()
+ # head_dim 512 layers (Gemma-4 globals) otherwise fall to SDPA's efficient / math kernels.
+ # Lazy: the kernel module defines Triton autotuners, which need a driver at import.
+ from ..kernels.large_head_attention import (
+ patch_large_head_attention as _patch_large_head_attention,
+ )
+
+ _patch_large_head_attention(model)
return _mark_requested_float32(model, user_float32), tokenizer
@staticmethod