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
Fused LoRA: plain Linear projections after merge_and_unload / unload no longer crash (#15119)
Daniel Hand8c28b71858d
diff --git a/tests/test_fused_lora_peft_fallbacks.py b/tests/test_fused_lora_peft_fallbacks.pyindex 29a5e59ce26..c9e41396bc0 100644--- a/tests/test_fused_lora_peft_fallbacks.py+++ b/tests/test_fused_lora_peft_fallbacks.py@@ -360,3 +360,28 @@ def test_routing_compiles_without_graph_breaks(monkeypatch):model.base_model.disable_adapter_layers()check(X.detach(), grad = False) # fusedcheck(X.detach().requires_grad_(), grad = True) # PEFT++[email protected]("device,dtype,path", CASES)[email protected]("unload", ["merge_and_unload", "unload"])[email protected]("grad_mode", ["no_grad", "enable_grad"])+def test_unloaded_plain_linear_projections_run_the_fused_paths(+ monkeypatch, device, dtype, path, unload, grad_mode+):+ # merge_and_unload() / unload() swap the LoRA layers back to plain nn.Linear while the fused+ # apply_qkv / apply_o / MLP forwards stay installed on the attention and MLP modules, so the+ # routing guards must accept a projection with no lora_A / lora_B (forward / generate after merging).+ model, _ = _peft_block(device, dtype, 0.0)+ block = getattr(model, unload)()+ assert type(block.o_proj) is torch.nn.Linear+ assert "lora_A" not in block.o_proj._modules+ X = torch.randn(2, 5, H, device = device, dtype = dtype)+ ctx = torch.no_grad if grad_mode == "no_grad" else torch.enable_grad+ with ctx():+ fused = PATHS[path][0](block)(X)+ ref = PATHS[path][1](block)(X)+ tol = {} if path == "mlp" else dict(rtol = 0, atol = 0)+ for a, b in zip(+ fused if isinstance(fused, tuple) else (fused,), ref if isinstance(ref, tuple) else (ref,)+ ):+ torch.testing.assert_close(a, b, **tol)diff --git a/unsloth/kernels/fast_lora.py b/unsloth/kernels/fast_lora.pyindex 60e863ec2d6..8b71fc21ccf 100644--- a/unsloth/kernels/fast_lora.py+++ b/unsloth/kernels/fast_lora.py@@ -66,8 +66,15 @@ def _forward_fake_quant_linears():def _has_activation_fake_quantizer(proj):# Fused LoRA skips per-layer activation fake quantizers; explicit children, as proj.modules() is slow per call.+ # A plain nn.Linear (after merge_and_unload / unload) has no lora_A / lora_B: check the layer itself.forward_fake_quant_linears = _forward_fake_quant_linears()- for module in (getattr(proj, "base_layer", proj), *proj.lora_A.values(), *proj.lora_B.values()):+ modules = proj._modules+ lora_A, lora_B = modules.get("lora_A"), modules.get("lora_B")+ for module in (+ modules.get("base_layer", proj),+ *(() if lora_A is None else lora_A.values()),+ *(() if lora_B is None else lora_B.values()),+ ):if module._modules.get("activation_fake_quantizer") is not None or isinstance(module, forward_fake_quant_linears):