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.

Fix 3D tensor support for bitsandbytes 8-bit matmul in forward pass

Fizza-Mukhtar9116aa8dfd3a
diff --git a/unsloth/kernels/fast_lora.py b/unsloth/kernels/fast_lora.py
index 16b679d0054..fbb18c3a15d 100644
--- a/unsloth/kernels/fast_lora.py
+++ b/unsloth/kernels/fast_lora.py
@@ -383,12 +383,12 @@ class LoRA_QKV(torch.autograd.Function):
# TorchInductor/AOTAutograd fails on 3D tensors during backward,
# so we explicitly flatten the sequence dimension.
orig_shape = X.shape
+ X_for_matmul = X
if X.dim() == 3:
- X = X.view(-1, X.shape[-1])
-
- Q = matmul_lora(X, QW, QW_quant, QA, QB, QS)
- K = matmul_lora(X, KW, KW_quant, KA, KB, KS)
- V = matmul_lora(X, VW, VW_quant, VA, VB, VS)
+ X_for_matmul = X.view(-1, X.shape[-1])
+ Q = matmul_lora(X_for_matmul, QW, QW_quant, QA, QB, QS)
+ K = matmul_lora(X_for_matmul, KW, KW_quant, KA, KB, KS)
+ V = matmul_lora(X_for_matmul, VW, VW_quant, VA, VB, VS)
# Restore original shape after matmul
if len(orig_shape) == 3: