liveBook · Manning

Low-rank adaptation

As explained in Transformers in Action

A low-rank adapter is a parameter-efficient fine-tuning component that represents a weight update through low-rank factors.

Low-rank adaptation (LoRA) fine-tunes a pretrained transformer without updating its original weights. It learns a compact update for selected layers, reducing trainable parameters while preserving the pretrained model. section 8.3 section 8.3.1

For an original weight matrix W₀, LoRA represents the task-specific update ΔW as BA, giving W₀ + ΔW = W₀ + BA. With B ∈ ℝᵈˣʳ and A ∈ ℝʳˣᵏ, the rank r is smaller than both matrix dimensions, so the update remains low-rank. section 8.3.1

LoRA’s practical design choices include:

  • Freeze W₀ and train only the factor matrices A and B. section 8.3.1
  • Insert adapters into selected transformer layers, including attention projections and feed-forward projections. section 8.3.1
  • Treat rank as a hyperparameter: larger ranks can represent richer updates, while smaller ranks emphasize parameter efficiency. section 8.3.1
  • Use LoRA as a baseline, then compare DoRA, QLoRA, QA-LoRA, or LQ-LoRA when convergence, memory, or quantization constraints dominate. section 8.3.7

Illustrative matrix decomposition

A simplified NumPy example uses singular value decomposition to construct B and A whose product approximates a matrix W. This demonstrates rank-r approximation; actual LoRA training learns B and A rather than computing them with SVD. section 8.3.1

Listing 8.11 Illustrative example of LoRA
def lora_decomposition(W, r):
    B = np.zeros((W.shape[0], r)) #1
    A = np.random.normal(0, 1, (r, W.shape[1]))

    U, S, Vt = np.linalg.svd(W, full_matrices=False) #2
    B[:, :r] = U[:, :r] * np.sqrt(S[:r])
    A[:r, :] = np.sqrt(S[:r])[:, np.newaxis] * Vt[:r, :]

    W_approx = B @ A #3

    return B, A, W_approx

d = 6   #4
W = np.random.randint(0, 10, size=(d, d)) #5

r = 5  # Rank for the decomposition #6

B, A, W_approx = lora_decomposition(W, r) #7

Inspect the executable decomposition that creates B and A, forms W_approx = B @ A, and applies a chosen rank to a sample matrix. section 8.3.1

A library-based setup can configure rank, target modules, scaling, gradient checkpointing, and randomization through Unsloth’s PEFT interface. The demonstrated configuration targets Qwen3-4B-Base with a rank of 32. section 8.3.1

How Transformers in Action covers it

How deep it goes
Explains frozen weights, factor dimensions, rank constraints, SVD intuition, and library configuration, but not the training loop.
Examples
Uses NumPy SVD decomposition, matrix visualizations, and an Unsloth configuration targeting Qwen3-4B-Base.
What it assumes
Assumes transformer layers and matrix dimensions, while teaching low-rank factorization and parameter-efficient updates.
What you can do after
Apply it via an Unsloth PEFT interface by selecting a rank and transformer target modules.
How it connects
Connects low-rank updates with transformer architecture, parameter-efficient fine-tuning, quantization, DoRA, QLoRA, QA-LoRA, and LQ-LoRA.

Also explained in

Compare how 9 books explain low-rank adaptation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Transformers in Action. The section links point to the parts of the book it draws on.