Skip to content

← Parameter-Efficient FT step 1 of 4

Medium Research

LoRA Update

LoRA, or Low-Rank Adaptation, represents a weight correction as the product of two smaller matrices. During adaptation, the base weights stay fixed while the smaller matrices are trained. Here, compute a forward pass using supplied parameters; do not perform a training update.

Your task

Implement lora_forward(x, W, A, B, alpha, r):

  • x is a floating-point vector of shape (d_in,).
  • W has shape (d_out, d_in) and contains the base weights.
  • A has shape (r, d_in) and maps the input to r features.
  • B has shape (d_out, r) and maps those features to the output width.
  • r is a positive integer matching the inner dimensions of A and B.
  • alpha is a scalar controlling the correction's strength.

Use matrix-times-vector orientation, with W @ x as the base output:

scale = alpha / r
output = W @ x + scale * (B @ (A @ x))

Return a vector of shape (d_out,). The scale applies only to the correction, not to W @ x. Use ordinary division, with no rounding of alpha / r. Add no bias, activation or dropout, and preserve all inputs.

Equivalently, form delta_W = scale * (B @ A) and return (W + delta_W) @ x. Both forms describe the same calculation; the first avoids constructing a full (d_out, d_in) correction matrix.

Example

For x = [2, 3], an identity W, A = [[1, 0]], B = [[1], [0]], alpha = 1 and r = 1, the base output is [2, 3]. The correction is [2, 0], so return [4, 3].

When alpha = 0, return W @ x. The input and output widths can differ, so the base path is not generally an identity or a direct addition of x.

What does low rank mean?

The matrix B @ A has rank at most r; it need not have rank exactly r. The two factors contain r * (d_in + d_out) parameters instead of d_in * d_out for an unrestricted correction. Choosing a sufficiently small r reduces the number of trainable parameters. This describes the correction, not the rank of the full effective matrix W + delta_W.

Background: Hu et al., LoRA: Low-Rank Adaptation of Large Language Models.