From e0a36b356b0db8d6b4bd57b752b639419faf3d45 Mon Sep 17 00:00:00 2001 From: Daniel Han-Chen Date: Fri, 26 Jan 2024 19:30:18 +1100 Subject: [PATCH] Update fast_lora.py --- unsloth/kernels/fast_lora.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/unsloth/kernels/fast_lora.py b/unsloth/kernels/fast_lora.py index 5d7ad1feac..8dbac0d28b 100644 --- a/unsloth/kernels/fast_lora.py +++ b/unsloth/kernels/fast_lora.py @@ -134,20 +134,20 @@ class LoRA_MLP(torch.autograd.Function): # h, DW_f, DW_dfg = DW, e, g # Down projection LoRA weights - d_downA = h.t() @ dY @ downB.t() - d_downB = downA.t() @ h.t() @ dY + d_downA = h.t() @ (dY @ downB.t()) + d_downB = (downA.t() @ h.t()) @ dY d_downA *= downS d_downB *= downS # Up projection LoRA weights - d_upA = X.t() @ DW_f @ upB.t() - d_upB = upA.t() @ X.t() @ DW_f + d_upA = X.t() @ (DW_f @ upB.t()) + d_upB = (upA.t() @ X.t()) @ DW_f d_upA *= upS d_upB *= upS # Gate projection LoRA weights - d_gateA = X.t() @ DW_dfg @ gateB.t() - d_gateB = gateA.t() @ X.t() @ DW_dfg + d_gateA = X.t() @ (DW_dfg @ gateB.t()) + d_gateB = (gateA.t() @ X.t()) @ DW_dfg d_gateA *= gateS d_gateB *= gateS @@ -156,15 +156,15 @@ class LoRA_MLP(torch.autograd.Function): # (D @ W.T * f) @ U.T upW = fast_dequantize(upW.t(), upW_quant) # (D @ W.T * f) @ (U.T + B.T @ A.T) - dX = torch.matmul(DW_f, upW.t()) + dX = torch.matmul(DW_f, upW.t(), out = X) del upW dX += upS * (DW_f @ upB.to(dtype).t() @ (upA.to(dtype).t())) # And add the derivative for the gate projection gateW = fast_dequantize(gateW.t(), gateW_quant) - new_dX = DW_dfg @ gateW.t() + gateS * (DW_dfg @ gateB.to(dtype).t() @ (gateA.to(dtype).t())) + dX += DW_dfg @ gateW.t() del gateW - dX += new_dX + dX += gateS * (DW_dfg @ gateB.to(dtype).t() @ (gateA.to(dtype).t())) # gateW, gateW_quant, gateA, gateB, gateS, # upW, upW_quant, upA, upB, upS, @@ -178,7 +178,7 @@ pass from transformers.models.llama.modeling_llama import logger def apply_lora_mlp(self, X): - logger.warning_once("Hello!3") + logger.warning_once("Hello!2") # gate = self.gate_proj(X) # up = self. up_proj(X) # h = torch.nn.functional.silu(gate) * up