TorchedUp
LearnBetaProblemsSystem DesignSoonPremium
TorchedUp
LearnBetaProblemsSystem DesignSoonPremium
←

149. LoRA Forward

Medium

Implement the LoRA (Low-Rank Adaptation) forward pass. Instead of training the full weight matrix W, LoRA freezes W and learns a low-rank update B @ A (where A and B are skinny matrices of rank r).

Signature: def lora_forward(x: np.ndarray, W: np.ndarray, A: np.ndarray, B: np.ndarray, alpha: float, r: int) -> np.ndarray

Shapes:

  • x: (batch, in)
  • W: (out, in) — frozen base weight
  • A: (r, in) — down-projection
  • B: (out, r) — up-projection

Returns: the frozen base output plus the low-rank update — shape (batch, out). The update routes x through A (down-projection to rank r) and B (up-projection back to out), scaled by alpha / r. See the math reference below.

Math

y=xW⊤+rα​xA⊤B⊤

Asked at

NumPy

import numpy as np

 

def lora_forward(...):

    pass

🔒

Premium problem

Free accounts include problems #1–20. Upgrade to unlock the editor, hidden test cases, and reference solutions for every problem.

Upgrade to PremiumBack to problems

Already premium?