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
LoRA: decide the fused order once per call (#16282)
Daniel Hand6ab4f64ac3b
diff --git a/tests/test_lora_fused_passes.py b/tests/test_lora_fused_passes.pyindex 9587753f6b3..77bfc7ad51c 100644--- a/tests/test_lora_fused_passes.py+++ b/tests/test_lora_fused_passes.py@@ -400,6 +400,31 @@ def test_shared_lora_a_grads_have_the_parameter_layout(monkeypatch):assert dA.t().is_contiguous(), dA.stride()+def test_shared_lora_backward_can_be_recorded_for_a_second_derivative(monkeypatch):+ # create_graph runs the backward with grad on: no in-place write through sibling views of one buffer.+ _set(monkeypatch, True)+ X = torch.randn(10, H, dtype = torch.float64)+ As = (torch.randn(4, H, dtype = torch.float64).t(), torch.randn(3, H, dtype = torch.float64).t())+ Bs = (torch.randn(H, 4, dtype = torch.float64).t(), torch.randn(I, 3, dtype = torch.float64).t())+ dYs = tuple(torch.randn(10, n, dtype = torch.float64, requires_grad = True) for n in (H, I))+ with torch.enable_grad():+ d_As, d_Bs, dX = fast_lora._shared_lora_backward(X, dYs, As, Bs, 2.0, None)+ total = sum(t.square().sum() for t in (*d_As, *d_Bs, dX))+ got = torch.autograd.grad(total, dYs)++ def ref(*dYs):+ out = []+ for dY, A, B in zip(dYs, As, Bs):+ y_dB = dY @ B.t()+ out += [2.0 * X.t() @ y_dB, 2.0 * (A.t() @ X.t()) @ dY, 2.0 * y_dB @ A.t()]+ dX = out[2] + out[5]+ return sum(t.square().sum() for t in (out[0], out[3], out[1], out[4], dX))++ want = torch.autograd.grad(ref(*dYs), dYs)+ for g, w in zip(got, want):+ torch.testing.assert_close(g, w)++def test_shared_lora_a_grads_are_views_of_one_buffer(monkeypatch):# Each d_A is a row block of one [sum r, K] buffer: no per-projection copy into the parameter layout._set(monkeypatch, True)diff --git a/unsloth/kernels/fast_lora.py b/unsloth/kernels/fast_lora.pyindex 3d0ab6dcc5f..c3036909935 100644--- a/unsloth/kernels/fast_lora.py+++ b/unsloth/kernels/fast_lora.py@@ -335,59 +335,43 @@ def _add_lora_dX(dX, y_dB, A, S, mask, p):dX.add_(_dropout_replay(y_dB @ A.t(), mask, p, S))-def _dense(t):- # Dynamo bans reading `layout` in a compiled backward, so traced code checks is_sparse / is_sparse_csr.- if t.is_sparse or t.is_sparse_csr:- return False- return torch.compiler.is_compiling() or t.layout == torch.strided--def _shared_XA(X, As, dtype):# One X @ [A_1; A_2; ...].t() instead of one pass over X per projection; per-projection column views.# Compressed sparse adapters cannot be concatenated: one product each, as before.- if not all(_dense(A) for A in As):+ # Eager only: a strided layout is neither sparse nor compressed sparse.+ if not all(A.layout == torch.strided for A in As):return [torch.matmul(X, A.to(dtype).t()) for A in As]- XA = torch.matmul(X, torch.cat([A.to(dtype) for A in As], 0).t())- views, start = [], 0- for A in As:- views.append(XA[:, start : start + A.shape[0]])- start += A.shape[0]- return views+ XA = torch.matmul(X, torch.cat([A if A.dtype is dtype else A.to(dtype) for A in As], 0).t())+ return XA.split_with_sizes([A.shape[0] for A in As], 1)def _shared_lora_backward(X, dYs, As, Bs, S, dX_out):# LoRA grads of projections sharing X and S: X is read once for all d_A and A @ X.t(), and dX starts# as S * dB @ A.t() for the base GEMMs to accumulate onto. Each d_A.t() is a row block of one buffer.A_rows = torch.cat([A.t() for A in As], 0)- AX = A_rows @ X.t()- d_Bs, start = [], 0- for dY, B in zip(dYs, Bs):- r = B.shape[0]- d_B = torch.empty_like(B)- d_B.addmm_(AX[start : start + r], dY, alpha = S, beta = 0)- d_Bs.append(d_B)- start += r- del AX- if _is_compiling():- # Traced, Inductor plans the buffers; slice writes would only add scatter copies.+ rs = [B.shape[0] for B in Bs]+ d_Bs = []+ for AX_, dY, B in zip((A_rows @ X.t()).split_with_sizes(rs, 0), dYs, Bs):+ d_Bs.append(torch.empty_like(B).addmm_(AX_, dY, alpha = S, beta = 0))+ # The last view keeps the whole A @ X.t() buffer alive otherwise.+ del AX_+ if _is_compiling() or torch.is_grad_enabled():+ # Traced, Inductor plans the buffers; slice writes would only add scatter copies. A recorded+ # backward (create_graph) cannot write through sibling split views either.dB = torch.cat([dY @ B.t() for dY, B in zip(dYs, Bs)], 1)else:dB = dYs[0].new_empty((X.shape[0], A_rows.shape[0]))- start = 0- for dY, B in zip(dYs, Bs):- r = B.shape[0]- dB[:, start : start + r].addmm_(dY, B.t(), beta = 0)- start += r+ for dB_, dY, B in zip(dB.split_with_sizes(rs, 1), dYs, Bs):+ dB_.addmm_(dY, B.t(), beta = 0)dA = dB.new_empty((A_rows.shape[0], X.shape[1]))dA.addmm_(dB.t(), X, alpha = S, beta = 0)- d_As, start = [], 0- for B in Bs:- r = B.shape[0]- d_As.append(dA[start : start + r].t())- start += r+ d_As = [dA_.t() for dA_ in dA.split_with_sizes(rs, 0)]del dA- if dX_out is None:+ if dX_out is None and _is_compiling():dX = torch.addmm(dB.new_zeros(1), dB, A_rows, beta = 0, alpha = S)+ elif dX_out is None:+ # Eager, a fresh buffer (beta 0 never reads it) skips the fill the traced form needs.+ dX = dB.new_empty((dB.shape[0], A_rows.shape[1])).addmm_(dB, A_rows, beta = 0, alpha = S)else:dX = dX_outaddmm_(dX, dB, A_rows, alpha = S, beta = 0)@@ -408,15 +392,32 @@ def _lora_traced_ok(*lora_weights, X = None):def _mlp_fused_ok(gateW, gateW_quant, upW, upW_quant, downW, downW_quant, dtype):# All or none: the forward and backward of one module use one order.- return all(- _lora_fused_ok(W, Q, dtype)- for W, Q in ((gateW, gateW_quant), (upW, upW_quant), (downW, downW_quant))+ return (+ _lora_fused_ok(gateW, gateW_quant, dtype)+ and _lora_fused_ok(upW, upW_quant, dtype)+ and _lora_fused_ok(downW, downW_quant, dtype))def _qkv_fused_ok(QW, QW_quant, KW, KW_quant, VW, VW_quant, dtype):- return all(- _lora_fused_ok(W, Q, dtype) for W, Q in ((QW, QW_quant), (KW, KW_quant), (VW, VW_quant))+ return (+ _lora_fused_ok(QW, QW_quant, dtype)+ and _lora_fused_ok(KW, KW_quant, dtype)+ and _lora_fused_ok(VW, VW_quant, dtype)+ )+++def _record_fused(ctx, fused_dtype):+ # The weights passed _lora_fused_ok at this dtype in the forward; the backward reuses that answer.+ ctx.fused_dtype = fused_dtype+ ctx.fused_compiling = _is_compiling()+++def _fused_reused(ctx, dtype):+ # Same weights, dtype and compile state as the forward's check: the same answer, without rerunning it.+ return (+ dtype is getattr(ctx, "fused_dtype", None)+ and getattr(ctx, "fused_compiling", None) == _is_compiling())@@ -515,11 +516,26 @@ class LoRA_MLP(torch.autograd.Function):# Eager only: traced, the shared intermediate fails the block compile (saved output aliasing).if fused and not _is_compiling() and gateA is not None and upA is not None:gateXA, upXA = _shared_XA(X.view(-1, X.shape[-1]), (gateA, upA), dtype)- e = matmul_lora(X, gateW, gateW_quant, gateA, gateB, gateS, XA = gateXA, fused = fused)- g = matmul_lora(X, upW, upW_quant, upA, upB, upS, XA = upXA, fused = fused)+ checked = dtype if fused else None+ e = matmul_lora(+ X,+ gateW,+ gateW_quant,+ gateA,+ gateB,+ gateS,+ XA = gateXA,+ fused = fused,+ fused_checked = checked,+ )+ g = matmul_lora(+ X, upW, upW_quant, upA, upB, upS, XA = upXA, fused = fused, fused_checked = checked+ )del gateXA, upXAh = _forward_function(e, g)- i = matmul_lora(h, downW, downW_quant, downA, downB, downS, fused = fused)+ i = matmul_lora(+ h, downW, downW_quant, downA, downB, downS, fused = fused, fused_checked = checked+ )masks = (None, None, None)else:fused = False@@ -554,6 +570,7 @@ class LoRA_MLP(torch.autograd.Function):ctx.dropout_p = dropout_p or (0.0, 0.0, 0.0)ctx.biases = (gateBias is not None, upBias is not None, downBias is not None)ctx.fused = fused+ _record_fused(ctx, dtype if fused else None)ctx.global_buffer = plainreturn i@@ -604,8 +621,9 @@ class LoRA_MLP(torch.autograd.Function):# Decided on the stored weights, before the FP8 down weight below is converted to a dense one.# The forward's order; a handcrafted or older ctx without `fused` keeps the base-then-LoRA order.- fused = getattr(ctx, "fused", False) and _mlp_fused_ok(- gateW, gateW_quant, upW, upW_quant, downW, downW_quant, dY.dtype+ fused = getattr(ctx, "fused", False) and (+ _fused_reused(ctx, dY.dtype)+ or _mlp_fused_ok(gateW, gateW_quant, upW, upW_quant, downW, downW_quant, dY.dtype))if downW.dtype == torch.float8_e4m3fn:# Dequantize like gate / up: matmul_lora would send the FP8 weight to fp8_linear, quantizing dY to FP8.@@ -622,6 +640,7 @@ class LoRA_MLP(torch.autograd.Function):downS,use_global_buffer = getattr(ctx, "global_buffer", True),fused = fused,+ fused_checked = dY.dtype if fused else None,)else:# Only the LoRA branch saw the dropped out h, so only its gradient goes through the mask.@@ -976,9 +995,16 @@ class LoRA_QKV(torch.autograd.Function):and VA is not None):QXA, KXA, VXA = _shared_XA(X_for_matmul, (QA, KA, VA), dtype)- Q = matmul_lora(X_for_matmul, QW, QW_quant, QA, QB, QS, XA = QXA, fused = fused)- K = matmul_lora(X_for_matmul, KW, KW_quant, KA, KB, KS, XA = KXA, fused = fused)- V = matmul_lora(X_for_matmul, VW, VW_quant, VA, VB, VS, XA = VXA, fused = fused)+ checked = dtype if fused else None+ Q = matmul_lora(+ X_for_matmul, QW, QW_quant, QA, QB, QS, XA = QXA, fused = fused, fused_checked = checked+ )+ K = matmul_lora(+ X_for_matmul, KW, KW_quant, KA, KB, KS, XA = KXA, fused = fused, fused_checked = checked+ )+ V = matmul_lora(+ X_for_matmul, VW, VW_quant, VA, VB, VS, XA = VXA, fused = fused, fused_checked = checked+ )del QXA, KXA, VXAmasks = (None, None, None)else:@@ -1025,6 +1051,7 @@ class LoRA_QKV(torch.autograd.Function):ctx.dropout_p = dropout_p or (0.0, 0.0, 0.0)ctx.biases = (QBias is not None, KBias is not None, VBias is not None)ctx.fused = fused+ _record_fused(ctx, dtype if fused else None)return Q, K, V@staticmethod@@ -1067,7 +1094,10 @@ class LoRA_QKV(torch.autograd.Function):if (getattr(ctx, "fused", False)and QS == KS == VS- and _qkv_fused_ok(QW, QW_quant, KW, KW_quant, VW, VW_quant, dQ.dtype)+ and (+ _fused_reused(ctx, dQ.dtype)+ or _qkv_fused_ok(QW, QW_quant, KW, KW_quant, VW, VW_quant, dQ.dtype)+ )and dQ.dtype == dK.dtype == dV.dtype):(d_QA, d_KA, d_VA), (d_QB, d_KB, d_VB), dX = _shared_lora_backward(@@ -1079,7 +1109,11 @@ class LoRA_QKV(torch.autograd.Function):X if ctx.inplace and not _is_compiling() else None,)for W_, W_quant_, dY_ in ((QW, QW_quant, dQ), (KW, KW_quant, dK), (VW, VW_quant, dV)):- W_ = fast_dequantize(W_.t(), W_quant_, dtype = dY_.dtype)+ W_ = (+ W_.t()+ if W_quant_ is None+ else fast_dequantize(W_.t(), W_quant_, dtype = dY_.dtype)+ )addmm_(dX, dY_, W_.t())del W_return (@@ -1268,10 +1302,13 @@ class LoRA_W(torch.autograd.Function):bias = None,):dtype = X.dtype- fused = False+ fused = fused_w = Falseif dropout_p is None and bias is None:fused = _lora_traced_ok(A, B, X = X)- XW = matmul_lora(X, W, W_quant, A, B, S, fused = fused)+ fused_w = fused and _lora_fused_ok(W, W_quant, dtype)+ XW = matmul_lora(+ X, W, W_quant, A, B, S, fused = fused_w, fused_checked = dtype if fused_w else None+ )mask = Noneelse:Xd, mask = _dropout_forward(X, dropout_p or 0.0)@@ -1286,6 +1323,7 @@ class LoRA_W(torch.autograd.Function):ctx.dropout_p = dropout_pctx.has_bias = bias is not Nonectx.fused = fused+ _record_fused(ctx, dtype if fused_w else None)return XW@staticmethod@@ -1317,9 +1355,16 @@ class LoRA_W(torch.autograd.Function):del Xdd_bias = _lora_bias_grad(dY, S) if ctx.has_bias else None- if getattr(ctx, "fused", False) and _lora_fused_ok(W, W_quant, dY.dtype):+ if getattr(ctx, "fused", False) and (+ _fused_reused(ctx, dY.dtype) or _lora_fused_ok(W, W_quant, dY.dtype)+ ):# dX starts as the rank-r term; the base GEMM accumulates onto it in its epilogue.- dX = torch.addmm(y_dB.new_zeros(1), y_dB, A.t(), beta = 0, alpha = S)+ if _is_compiling():+ dX = torch.addmm(y_dB.new_zeros(1), y_dB, A.t(), beta = 0, alpha = S)+ else:+ dX = y_dB.new_empty((y_dB.shape[0], A.shape[0])).addmm_(+ y_dB, A.t(), beta = 0, alpha = S+ )W = fast_dequantize(W.t(), W_quant, dtype = dY.dtype)addmm_(dX, dY, W.t())del Wdiff --git a/unsloth/kernels/utils.py b/unsloth/kernels/utils.pyindex cc094ced4bf..69030e330da 100644--- a/unsloth/kernels/utils.py+++ b/unsloth/kernels/utils.py@@ -1863,15 +1863,19 @@ def fast_linear_forward(return out+_is_functorch_wrapped_tensor = getattr(torch._C._functorch, "is_functorch_wrapped_tensor", None)++def _functorch_wrapped(*tensors):# vmap / grad transforms wrap tensors; an in-place write from them into a plain buffer is not allowed.- check = getattr(torch._C._functorch, "is_functorch_wrapped_tensor", None)+ check = _is_functorch_wrapped_tensorreturn check is not None and any(check(t) for t in tensors)def _lora_first_addmm(X, Wt, XA, Bt, alpha):# alpha * XA @ Bt into a fresh buffer (beta 0 never reads it), then the base GEMM accumulates onto it.- if _functorch_wrapped(X, Wt, XA, Bt):+ # Wrapped tensors only exist inside an active torch.func transform.+ if _functorch_active() and _functorch_wrapped(X, Wt, XA, Bt):out = torch.addmm(XA.new_zeros(1), XA, Bt, beta = 0, alpha = alpha)return torch.addmm(out, X, Wt)out = torch.empty((X.shape[0], Wt.shape[1]), dtype = X.dtype, device = X.device)@@ -2057,10 +2061,12 @@ def matmul_lora(use_global_buffer = True,XA = None,fused = True,+ fused_checked = None,):# XA: X @ A.t() already computed by the caller (shared across projections reading the same X).# fused = False: the caller's module needs the base-then-LoRA order (dropout, lora_bias, a weight# converted before this call); _lora_fused_ok decides the rest.+ # fused_checked: the dtype the caller already ran _lora_fused_ok(W, W_quant, dtype) for, True.dtype = X.dtypeif X.dim() == 3:@@ -2097,8 +2103,13 @@ def matmul_lora(# batching rule, and a traced beta=0 addmm under jvp crashes, so keep the base-then-LoRA order there.and not (_is_compiling() and (_functorch_active() or _forward_ad_active()))# A DTensor operand's placements would be lost in the plain accumulator (W is checked below).- and not any(isinstance(t, _LORA_DTENSOR) for t in (X, A, B, XA))- and _lora_fused_ok(W, W_quant, dtype)+ and not (+ isinstance(X, _LORA_DTENSOR)+ or isinstance(A, _LORA_DTENSOR)+ or isinstance(B, _LORA_DTENSOR)+ or isinstance(XA, _LORA_DTENSOR)+ )+ and (fused_checked is dtype or _lora_fused_ok(W, W_quant, dtype))# A caller's buffer must already be the output and must not share storage with a base operand.and (out is None@@ -2110,15 +2121,18 @@ def matmul_lora()):if XA is None:- XA = torch_matmul(X, A.t().to(dtype))- B = B.t().to(dtype)+ XA = torch_matmul(X, A.t() if A.dtype is dtype else A.t().to(dtype))+ B = B.t() if B.dtype is dtype else B.t().to(dtype)if out is not None:# Eager only: a traced caller buffer cannot be checked for aliasing (_shares_storage). Dequantize# first: the caller's buffer may share storage with the quant state (absmax, code, ...).- W = fast_dequantize(W, W_quant, use_global_buffer = use_global_buffer, dtype = dtype)+ if W_quant is not None:+ W = fast_dequantize(W, W_quant, use_global_buffer = use_global_buffer, dtype = dtype)out = _accumulated(addmm_(out, XA, B, alpha = s, beta = 0), out.addmm_(X, W.t()))else:- W = fast_dequantize(W, W_quant, use_global_buffer = use_global_buffer, dtype = dtype)+ # Unquantized, fast_dequantize returns W as is (FP8 never takes this branch).+ if W_quant is not None:+ W = fast_dequantize(W, W_quant, use_global_buffer = use_global_buffer, dtype = dtype)if not _is_compiling():out = _lora_first_addmm(X, W.t(), XA, B, s)elif (