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.

Nightly (#299)

Daniel Han7e1c6a62e26a
diff --git a/unsloth/kernels/__init__.py b/unsloth/kernels/__init__.py
index 8ff255e4a45..cb04377b1a2 100644
--- a/unsloth/kernels/__init__.py
+++ b/unsloth/kernels/__init__.py
@@ -30,4 +30,4 @@ from .fast_lora import (
apply_lora_qkv,
apply_lora_o,
)
-from .utils import fast_dequantize, fast_gemv, QUANT_STATE, fast_linear_forward
+from .utils import fast_dequantize, fast_gemv, QUANT_STATE, fast_linear_forward, matmul_lora
diff --git a/unsloth/kernels/fast_lora.py b/unsloth/kernels/fast_lora.py
index 6568bba6819..aba44f02145 100644
--- a/unsloth/kernels/fast_lora.py
+++ b/unsloth/kernels/fast_lora.py
@@ -13,32 +13,7 @@
# limitations under the License.
import torch
-from .utils import fast_dequantize, QUANT_STATE, get_lora_parameters
-
-
-def matmul_lora(X, W, W_quant, A, B, s, out = None):
- dtype = X.dtype
- W = fast_dequantize(W.t(), W_quant)
-
- if X.dim() == 3:
- batch, seq_len, d = X.shape
- X = X.view(-1, X.shape[-1])
- reshape = True
- else:
- reshape = False
- pass
-
- out = torch.matmul(X, W, out = out)
- if W_quant is not None: del W
-
- if A is not None:
- # LoRA is enabled
- A, B = A.t(), B.t()
- out += (X @ A.to(dtype)) @ (s * B.to(dtype))
- pass
-
- return out.view(batch, seq_len, -1) if reshape else out
-pass
+from .utils import fast_dequantize, QUANT_STATE, get_lora_parameters, matmul_lora
class LoRA_MLP(torch.autograd.Function):
diff --git a/unsloth/kernels/utils.py b/unsloth/kernels/utils.py
index 76f0a9854a7..1f2085df5ae 100644
--- a/unsloth/kernels/utils.py
+++ b/unsloth/kernels/utils.py
@@ -182,8 +182,8 @@ pass
def fast_linear_forward(proj, X, temp_lora = None, out = None):
W, W_quant, lora_A, lora_B, lora_S = get_lora_parameters(proj)
-
bsz, q_len, in_dim = X.shape
+ if q_len != 1: return matmul_lora(X, W, W_quant, lora_A, lora_B, lora_S)
if W_quant is None:
out = torch.matmul(X, W.t(), out = out)
@@ -203,7 +203,7 @@ def fast_linear_forward(proj, X, temp_lora = None, out = None):
lora_A._fast_lora = lora_A.to(dtype)
lora_B._fast_lora = lora_B.to(dtype)
pass
-
+
if bsz == 1:
out = out.view(out_dim)
temp_lora = torch.mv(lora_A._fast_lora, X.ravel(), out = temp_lora)
@@ -218,3 +218,28 @@ def fast_linear_forward(proj, X, temp_lora = None, out = None):
return out
pass
+
+
+def matmul_lora(X, W, W_quant, A, B, s, out = None):
+ dtype = X.dtype
+ W = fast_dequantize(W.t(), W_quant)
+
+ if X.dim() == 3:
+ batch, seq_len, d = X.shape
+ X = X.view(-1, X.shape[-1])
+ reshape = True
+ else:
+ reshape = False
+ pass
+
+ out = torch.matmul(X, W, out = out)
+ if W_quant is not None: del W
+
+ if A is not None:
+ # LoRA is enabled
+ A, B = A.t(), B.t()
+ out += (X @ A.to(dtype)) @ (s * B.to(dtype))
+ pass
+
+ return out.view(batch, seq_len, -1) if reshape else out
+pass