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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
head_dim 512 attention: sm80+ only, no 256 MB autotune buffer, OOM falls back to SDPA, no per-call mask sync (#13977)
Daniel Han1574001bc1ec
diff --git a/tests/test_large_head_attention.py b/tests/test_large_head_attention.pyindex 87d2e6ff242..eae63d174cc 100644--- a/tests/test_large_head_attention.py+++ b/tests/test_large_head_attention.py@@ -31,6 +31,7 @@ def _load():LH = _load()+_SM80 = has_real_cuda() and torch.cuda.get_device_capability()[0] >= 8def _causal(S):@@ -199,7 +200,7 @@ def test_kernel_matches_reference(S, heads, kind):assert err.mean() <= 0.01 * b.abs().mean() + 2e-3[email protected](not has_real_cuda(), reason = "needs CUDA")[email protected](not _SM80, reason = "the router engages on sm80+ only")def test_router_engages_on_gemma4_global_call():calls, original = _sentinel()router = LH._make_router(original)@@ -214,7 +215,7 @@ def test_router_engages_on_gemma4_global_call():assert router(_Module(), q, kv, kv, left_pad[None, None])[0] == "original"[email protected](not has_real_cuda(), reason = "needs CUDA")[email protected](not _SM80, reason = "the router engages on sm80+ only")def test_router_defers_trainable_additive_mask():calls, original = _sentinel()router = LH._make_router(original)@@ -234,7 +235,7 @@ 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")[email protected](not _SM80, reason = "the router engages on sm80+ only")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)@@ -246,7 +247,7 @@ def test_self_check_leaves_global_rng_alone(monkeypatch):assert torch.equal(torch.cuda.get_rng_state(device), state)[email protected](not has_real_cuda(), reason = "needs CUDA")[email protected](not _SM80, reason = "the router engages on sm80+ only")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)@@ -254,7 +255,7 @@ def test_self_check_under_inference_mode():assert LH._self_check(device, dtype) is True[email protected](not has_real_cuda(), reason = "needs CUDA")[email protected](not _SM80, reason = "the router engages on sm80+ only")def test_patch_self_checks_so_compiled_graphs_use_the_kernel(monkeypatch):from transformers.modeling_utils import ALL_ATTENTION_FUNCTIONS@@ -286,7 +287,7 @@ def test_patch_self_checks_so_compiled_graphs_use_the_kernel(monkeypatch):assert LH._SELF_CHECK.get((str(device), dtype)) is True[email protected](not has_real_cuda(), reason = "needs CUDA")[email protected](not _SM80, reason = "the router engages on sm80+ only")def test_router_defers_autocast_dtype_change():calls, original = _sentinel()router = LH._make_router(original)@@ -301,7 +302,7 @@ def test_router_defers_autocast_dtype_change():assert LH.LARGE_HEAD_ENGAGED["calls"] == before + 1 and len(calls) == 1[email protected](not has_real_cuda(), reason = "needs CUDA")[email protected](not _SM80, reason = "the router engages on sm80+ only")def test_router_honours_math_sdpa_selection():from torch.nn.attention import SDPBackend, sdpa_kernel@@ -385,3 +386,113 @@ def test_install_sits_beneath_the_packed_router(monkeypatch):assert packed._ORIG_SDPA[0]._unsloth_original is innerfinally:ALL_ATTENTION_FUNCTIONS["sdpa"] = saved+++def test_autotune_skips_the_flush_buffer_benchmarker():+ # Triton's default benchmarker allocates a 256 MB L2 flush buffer, enough to OOM a model that fits.+ for kernel in (LH._lh_fwd, LH._lh_bwd_dq, LH._lh_bwd_dkdv):+ assert kernel.do_bench is LH._bench_in_place+++def test_self_check_oom_falls_back_to_sdpa(monkeypatch):+ def oom(*args, **kwargs):+ raise torch.cuda.OutOfMemoryError("CUDA out of memory.")++ monkeypatch.setattr(LH, "large_head_attention", oom)+ monkeypatch.setattr(LH, "_measured_faster_on", lambda device: True)+ key = (str(torch.device("cpu")), torch.float32)+ monkeypatch.setitem(LH._SELF_CHECK, key, None)+ LH._SELF_CHECK.pop(key)+ assert LH._self_check(torch.device("cpu"), torch.float32) is False+ assert LH._SELF_CHECK[key] is False++[email protected](not has_real_cuda(), reason = "needs CUDA")+def test_autotune_of_a_new_shape_adds_no_flush_buffer():+ dtype = _cuda_dtype()+ # G = 3 is not used elsewhere in this file, so this call autotunes fwd and bwd from scratch.+ q = torch.randn(1, 6, 40, 512, device = "cuda", dtype = dtype, requires_grad = True)+ k = torch.randn(1, 2, 40, 512, device = "cuda", dtype = dtype, requires_grad = True)+ v = torch.randn(1, 2, 40, 512, device = "cuda", dtype = dtype, requires_grad = True)+ lo, hi = LH._dense_intervals(1, 40, True, q.device)+ torch.cuda.synchronize()+ torch.cuda.reset_peak_memory_stats()+ base = torch.cuda.memory_allocated()+ LH.large_head_attention(q, k, v, lo, hi).float().sum().backward()+ torch.cuda.synchronize()+ assert torch.cuda.max_memory_allocated() - base < 32 * 1024**2++[email protected](not _SM80, reason = "the router engages on sm80+ only")+def test_router_falls_back_to_sdpa_on_kernel_oom(monkeypatch):+ calls, original = _sentinel()+ router = LH._make_router(original)+ dtype = _cuda_dtype()+ assert LH._self_check(torch.device("cuda", torch.cuda.current_device()), dtype) is True++ def oom(*args, **kwargs):+ raise torch.cuda.OutOfMemoryError("CUDA out of memory.")++ monkeypatch.setattr(LH, "_attention", oom)+ 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["fallback"]+ assert router(_Module(), q, kv, kv, None)[0] == "original"+ assert LH.LARGE_HEAD_ENGAGED["fallback"] == before + 1 and len(calls) == 1++[email protected](not _SM80, reason = "the router engages on sm80+ only")+def test_router_skips_the_mask_scan_when_it_cannot_use_the_result(monkeypatch):+ # The scan needs a host sync: inference masks would pay it on every layer, and a failed self+ # check makes the result unusable.+ def scan(*args, **kwargs):+ raise AssertionError("mask scanned")++ calls, original = _sentinel()+ router = LH._make_router(original)+ device, dtype = torch.device("cuda", torch.cuda.current_device()), _cuda_dtype()+ assert LH._self_check(device, dtype) is True+ monkeypatch.setattr(LH, "intervals_from_mask", scan)+ q = torch.randn(1, 4, 24, 512, device = "cuda", dtype = dtype)+ kv = torch.randn(1, 2, 24, 512, device = "cuda", dtype = dtype)+ with torch.inference_mode():+ mask = _causal(24).cuda()[None, None]+ assert router(_Module(), q, kv, kv, mask)[0] == "original"+ monkeypatch.setitem(LH._SELF_CHECK, (str(device), dtype), False)+ assert router(_Module(), q, kv, kv, _causal(24).cuda()[None, None])[0] == "original"+ assert len(calls) == 2+++def test_pre_ampere_keeps_sdpa_without_compiling(monkeypatch):+ def kernel(*args, **kwargs):+ raise AssertionError("kernel compiled on sm75")++ monkeypatch.setattr(LH, "large_head_attention", kernel)+ monkeypatch.setattr(torch.cuda, "get_device_capability", lambda device = None: (7, 5))+ key = (str(torch.device("cpu")), torch.float16)+ monkeypatch.setitem(LH._SELF_CHECK, key, None)+ LH._SELF_CHECK.pop(key)+ assert LH._self_check(torch.device("cpu"), torch.float16) is False+++def test_install_skipped_when_every_self_check_fails(monkeypatch):+ from transformers.modeling_utils import ALL_ATTENTION_FUNCTIONS++ class _Param:+ is_cuda = True+ device = torch.device("cuda", 0)+ dtype = torch.float16++ model = torch.nn.Linear(4, 4)+ model.head_dim = 512+ monkeypatch.setattr(model, "parameters", lambda: iter([_Param()]))+ monkeypatch.setattr(torch.cuda, "is_available", lambda: True)+ monkeypatch.setattr(torch.version, "hip", None)+ monkeypatch.setattr(LH, "_PATCHED", [False])+ monkeypatch.setattr(LH, "_self_check", lambda device, dtype: False)+ saved = ALL_ATTENTION_FUNCTIONS["sdpa"]+ try:+ assert LH.patch_large_head_attention(model) is False+ assert ALL_ATTENTION_FUNCTIONS["sdpa"] is saved+ finally:+ ALL_ATTENTION_FUNCTIONS["sdpa"] = saveddiff --git a/unsloth/kernels/large_head_attention.py b/unsloth/kernels/large_head_attention.pyindex 16cbf21b617..f031bcfbd3c 100644--- a/unsloth/kernels/large_head_attention.py+++ b/unsloth/kernels/large_head_attention.py@@ -361,6 +361,32 @@ def _lh_bwd_dkdv()+def _bench_in_place(kernel_call, quantiles = None):+ # Triton's default benchmarker allocates a 256 MB L2 flush buffer per autotune, enough to OOM a+ # model that fits. Each config here only rewrites the call's own outputs, so time it in place.+ kernel_call()+ start, end = torch.cuda.Event(enable_timing = True), torch.cuda.Event(enable_timing = True)+ start.record()+ kernel_call()+ end.record()+ end.synchronize()+ reps = max(1, min(25, int(10.0 / max(start.elapsed_time(end), 1e-3))))+ start.record()+ for _ in range(reps):+ kernel_call()+ end.record()+ end.synchronize()+ ms = start.elapsed_time(end) / reps+ return [ms] * len(quantiles) if quantiles else ms+++# Triton 3.2's autotune() does not forward do_bench; the attribute works on every version.+for _kernel in (_lh_fwd, _lh_bwd_dq, _lh_bwd_dkdv):+ _kernel._do_bench = _bench_in_place+ _kernel.do_bench = _bench_in_place+del _kernel++_LOG2E = 1.4426950408889634@@ -709,12 +735,24 @@ def _shapes_qualify(query, key, value):_SELF_CHECK = {}+def _measured_faster_on(device):+ # Timed faster than SDPA on sm80+ only. Pre-Ampere (T4) also lacks the shared memory for the+ # larger tiles and has no bf16, so it keeps SDPA without compiling or autotuning anything.+ try:+ return torch.cuda.get_device_capability(device)[0] >= 8+ except Exception:+ return False++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+ if not _measured_faster_on(device):+ _SELF_CHECK[key] = passed+ return passedtry:# A local generator: torch.manual_seed would reseed every CUDA device's RNG.gen = torch.Generator(device = device)@@ -742,9 +780,8 @@ def _self_check(device, dtype):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:- raiseexcept Exception as exc:+ # Includes OOM: the load worked before this check existed, so SDPA keeps it working._warn_once(f"Unsloth: head_dim 512 attention kernel failed its self check ({exc}); using SDPA.")@@ -824,13 +861,18 @@ def _make_router(original):):B, S = query.shape[0], query.shape[2]intervals = None+ compiling = torch.compiler.is_compiling()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.+ elif (+ not compiling+ and not (torch.is_tensor(attention_mask) and attention_mask.is_inference())+ and _SELF_CHECK.get((str(query.device), query.dtype), True)+ ):+ # Reading a mask's structure needs a host sync; compiled graphs keep SDPA. Inference+ # masks cannot cache the verdict across layers, so they keep SDPA too.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:@@ -846,7 +888,8 @@ def _make_router(original):LARGE_HEAD_ENGAGED["calls"] += 1return out, Noneexcept torch.cuda.OutOfMemoryError:- raise+ # SDPA below either fits or raises the same OOM the previous path would.+ LARGE_HEAD_ENGAGED["fallback"] += 1except Exception as exc:LARGE_HEAD_ENGAGED["fallback"] += 1_SELF_CHECK[(str(query.device), query.dtype)] = False@@ -897,8 +940,10 @@ def patch_large_head_attention(model = None):}except Exception:targets = set()- for device, dtype in targets:- _self_check(device, dtype)+ passed = [_self_check(device, dtype) for device, dtype in targets]+ if passed and not any(passed):+ # Every call would go back to SDPA, so leave it unwrapped.+ return Falsetry:from transformers.modeling_utils import ALL_ATTENTION_FUNCTIONScurrent = ALL_ATTENTION_FUNCTIONS["sdpa"]