You will learn:

Chapter 8 showed that fine-tuning every weight is expensive: the weights, their gradients and the optimizer state all have to fit in memory, and a narrow task may not need all of that freedom. LoRA (low-rank adaptation) (Hu et al. 2022) freezes the model and learns a small correction to each of its big matrices instead. Writing a large matrix as the product of two thin ones is an old trick: recommender systems, for example, factor the matrix of user ratings into a user part and an item part (Koren et al. 2009). LoRA uses the same trick on a different object. What is factored is not the data but the update to a frozen weight.

9.1 The idea

A linear layer in the model multiplies its input by a weight matrix \(W\). LoRA leaves \(W\) alone and adds a correction that is the product of two thin matrices:

\[ W' = W + \frac{\alpha}{r}\, B\,A, \qquad B \in \mathbb{R}^{d_\text{out} \times r}, \quad A \in \mathbb{R}^{r \times d_\text{in}} . \]

The number \(r\), the rank, is chosen small (we start with 16) compared with \(d_\text{in}\) and \(d_\text{out}\), which are in the thousands. A product of an \(r\)-column and an \(r\)-row matrix can only change the layer’s output within an \(r\)-dimensional space, so the update is low-rank however large the layer is. Figure 9.1 shows one matrix of Qwen3-4B in two equivalent views.

Two panels. Left: W-prime equals frozen W plus a thin tall B times a thin flat A, scaled by alpha over r. Right: the input splits into a path through frozen W and a path through A then B; the two outputs are summed. A dashed box around A and B notes that their product is never built.
Figure 9.1: One weight matrix with a LoRA adapter (Qwen3-4B’s gate_proj, rank 16). Left: the adapted weight is the frozen W plus the product of two thin matrices scaled by α/r, which is what merging produces. Right: what training runs. The input goes through W and, separately, through A then B, and the outputs are added, so the full 9,728 × 2,560 product is never built in memory. Only r = 16 numbers pass between A and B, and the trained matrices hold 196,608 weights, against 24.9 million in W. The thin matrices are drawn wider than their true proportion.

Three details are worth knowing.

  • Why it is cheap. During training the input is multiplied by \(A\) first and then by \(B\), so only a vector of \(r\) numbers sits between them. The big update matrix \(BA\) is never formed. Only \(A\) and \(B\) have gradients and optimizer state, which is why LoRA’s memory cost in Chapter 8 was small.
  • Why it starts as the identity. \(B\) is initialized to zero and \(A\) to small random values (PEFT’s default is the same initialization as an ordinary linear layer: uniform in \(\pm 1/\sqrt{d_\text{in}}\), about \(\pm 0.02\) for our matrices; other schemes exist, some of which start from the weights’ singular components). At step 0 the correction \(BA\) is exactly zero, so the model begins as the base model. \(A\) must not be zero too: with \(B = 0\) the gradient of \(B\) depends on \(A\)’s output, so a zero \(A\) would give no gradient to start from.
  • What \(\alpha/r\) does. It scales the correction. We set \(\alpha = 2r\), so the scale is 2 at every rank we try; changing \(\alpha\) is, to a first approximation, like changing the learning rate of the adapter.

The bet LoRA makes is that the change a task needs, \(W' - W\), is of low rank. It is a bet, not a fact: the training chapter tests it by looking at the singular values of the update the training produces.

9.2 Where the adapters go

We put an adapter on every linear layer of the transformer blocks, which PEFT calls target_modules="all-linear". Figure 9.2 marks them on one decoder layer: four in attention and three in the feed-forward block.

A decoder layer inside a dashed box labelled 36 times. Seven boxes are highlighted: q, k, v and o projections in the attention part, and gate, up and down projections in the feed-forward part. The embeddings, the two RMSNorms and the output head are grey.
Figure 9.2: One decoder layer of Qwen3-4B, repeated 36 times. The seven linear layers LoRA adapts are green: q, k, v and o in attention, and gate, up and down in the feed-forward block. The embeddings, the norms, the output head and the parts with no weights have no adapter.

That is \(36 \times 7 = 252\) adapters, the number of modules Chapter 5 counted. The embeddings, the normalization scales and the output head are not adapted, and stay as they were.

9.3 What it costs

A matrix with \(d_\text{in}\) inputs and \(d_\text{out}\) outputs has \(d_\text{in}\, d_\text{out}\) weights; its adapter has \(r\,(d_\text{in} + d_\text{out})\). We can compute the cost for Qwen3-4B from the model’s configuration, and check the total against what PEFT reports once it has wrapped the real model.

Code
#HEAVY
import math

import pandas as pd
from transformers import AutoConfig

from convfinqa.artifacts import artifact, get


@artifact(
  "lora_checks",
  deps=["train_val_split"],
  files=["configs/lora_checks.json"],
  heavy=True
)
def build_lora_checks(out, deps, files):
    import json

    from convfinqa.formatting import prompt_messages
    from convfinqa.adapters import lora_checks

    cfg = files["configs/lora_checks.json"].json()
    conv = next(iter(deps["train_val_split"].jsonl("val.jsonl")))
    result = lora_checks(
        cfg["model"], prompt_messages(conv, 0),
        r=cfg["r"],
        alpha=cfg["alpha"],
        b_std=cfg["b_std"],
        seed=cfg["seed"],
        memory_cap_gb=cfg["memory_cap_gb"],
    )
    (out / "checks.json").write_text(json.dumps(result))

checks = get("lora_checks").json("checks.json")

config = AutoConfig.from_pretrained("Qwen/Qwen3-4B-Instruct-2507")

hidden = config.hidden_size
ff = config.intermediate_size
layers = config.num_hidden_layers
q_out = config.num_attention_heads * config.head_dim
kv_out = config.num_key_value_heads * config.head_dim

projections = {  # name: (inputs, outputs)
    "q_proj": (hidden, q_out),
    "k_proj": (hidden, kv_out),
    "v_proj": (hidden, kv_out),
    "o_proj": (q_out, hidden),
    "gate_proj": (hidden, ff),
    "up_proj": (hidden, ff),
    "down_proj": (ff, hidden),
}

r = 16
rows = {
    name: {
        "shape (in → out)": f"{i:,} → {o:,}",
        "full weights": f"{i * o:,}",
        f"adapter weights, r={r}": f"{r * (i + o):,}",
        "adapter / full": f"{r * (i + o) / (i * o):.1%}",
        "largest useful rank": f"{min(i, o):,}",
        "break-even rank": f"{round(i * o / (i + o)):,}",
    }
    for name, (i, o) in projections.items()
}
pd.DataFrame(rows).T
shape (in → out) full weights adapter weights, r=16 adapter / full largest useful rank break-even rank
q_proj 2,560 → 4,096 10,485,760 106,496 1.0% 2,560 1,575
k_proj 2,560 → 1,024 2,621,440 57,344 2.2% 1,024 731
v_proj 2,560 → 1,024 2,621,440 57,344 2.2% 1,024 731
o_proj 4,096 → 2,560 10,485,760 106,496 1.0% 2,560 1,575
gate_proj 2,560 → 9,728 24,903,680 196,608 0.8% 2,560 2,027
up_proj 2,560 → 9,728 24,903,680 196,608 0.8% 2,560 2,027
down_proj 9,728 → 2,560 24,903,680 196,608 0.8% 2,560 2,027
Code
per_layer = sum(r * (i + o) for i, o in projections.values())
total = per_layer * layers
print(
    f"adapter weights per layer: {per_layer:,};  × {layers} layers = {total:,}"
)
peft = checks["bf16"]
print(f"PEFT's count of trainable parameters:   {peft['trainable_params']:,}")
print(
    f"adapted modules: {peft['adapted_modules']}   "
    f"share of the model: {total / checks['base_params']:.2%}"
)
assert total == peft["trainable_params"]\
    and peft["adapted_modules"] == layers * len(projections)
print(f"saved as fp32, the adapter is about {total * 4 / 1e6:.0f} MB")
adapter weights per layer: 917,504;  × 36 layers = 33,030,144
PEFT's count of trainable parameters:   33,030,144
adapted modules: 252   share of the model: 0.82%
saved as fp32, the adapter is about 132 MB
Code
largest = {name: min(i, o) for name, (i, o) in projections.items()}
break_even = {
    name: i * o / (i + o) for name, (i, o) in projections.items()
}
wide = max(largest.values())
narrow = min(largest.values())
assert sorted(set(largest.values())) == [narrow, wide]
assert [n for n, v in largest.items() if v == narrow] == ["k_proj", "v_proj"]
assert all(be > 10 * r for be in break_even.values())

At rank 16 the adapters hold 33.0 million weights, 0.82% of the model, and the count equals PEFT’s. Two columns of the table matter for choosing a rank.

  • Largest useful rank. The product \(BA\) can never have a rank above \(\min(d_\text{in}, d_\text{out})\). At that rank the update can be any matrix of the layer’s shape: 2,560 for most matrices and 1,024 for \(k\) and \(v\). A rank beyond it adds weights and no new updates.
  • Break-even rank. Where \(r\,(d_\text{in}+d_\text{out}) = d_\text{in}\, d_\text{out}\), the adapter has as many weights as the matrix it adapts: between 731 and 2,027 here. Above that LoRA is not smaller than full fine-tuning, so useful ranks sit far below it. Our \(r = 16\) is 0.6% of the largest useful rank for most matrices, and 1.6% for \(k\) and \(v\).

Even at the largest rank, LoRA would not be the same as full fine-tuning: the embeddings and norms stay frozen, and the update is learned through the two factors, which behaves differently from training \(W\) directly.

9.4 Two checks on the real model

The claims above are about the model we use, so we test them on it. The artifact above loaded Qwen3-4B twice, in bf16 and in fp32, wrapped it with PEFT at rank 16 and \(\alpha = 32\), and ran one prompt, the first turn of a validation conversation (1348 tokens), through the original model, the wrapped one and the merged one.

It starts as the base model. With \(B = 0\) the logits of the wrapped model and of the original must be the same:

Code
for precision in ("bf16", "fp32"):
    print(
        f"{precision}: largest difference between the wrapped and the "
        f"original model's logits: {checks[precision]['step0_max_abs_diff']}"
    )
assert all(checks[p]["step0_max_abs_diff"] == 0.0 for p in ("bf16", "fp32"))
bf16: largest difference between the wrapped and the original model's logits: 0.0
fp32: largest difference between the wrapped and the original model's logits: 0.0

The logits are identical, not merely close: adding a correction of exactly zero changes nothing, in any precision.

Merging, in fp32 and in bf16. After training, the correction can be folded into the weights, \(W \leftarrow W + \tfrac{\alpha}{r} BA\), so the model runs with no extra layers and no extra cost. To test that on a model whose adapter actually does something, we give \(B\) random values (small ones, with a standard deviation of 0.01), which moves the logits, and compare the output with the adapter kept separate against the output after merging it. We do it twice, with the model in fp32 and in bf16:

Code
merge_table = pd.DataFrame({
    precision: {
        "largest logit change caused by the adapter":
            checks[precision]["adapter_effect_max_abs"],
        "largest difference, merged vs separate":
            checks[precision]["merge_max_abs_diff"],
        "same top token, merged vs separate":
            f"{checks[precision]['merge_top1_agreement']:.1%}",
        "same top token, adapter vs base model":
            f"{checks[precision]['adapter_top1_agreement']:.1%}",
    }
    for precision in ("fp32", "bf16")
})
print(
    f"{checks['n_positions']} positions; the logits of the original model "
    f"go up to about {checks['bf16']['logit_scale']:.0f}"
)
merge_table
1348 positions; the logits of the original model go up to about 52
fp32 bf16
largest logit change caused by the adapter 14.689929 15.125
largest difference, merged vs separate 0.001925 15.28125
same top token, merged vs separate 100.0% 95.7%
same top token, adapter vs base model 95.2% 93.2%
Code
fp32, bf16 = checks["fp32"], checks["bf16"]
assert fp32["merge_max_abs_diff"] < 0.01 * fp32["adapter_effect_max_abs"]\
    and fp32["merge_top1_agreement"] == 1.0

In fp32, merging is the same function. The largest difference between the merged and the separate adapter is 0.002 in the logits, against 14.7 for the effect of the adapter itself, and the top token is the same at every position: the difference is float rounding.

In bf16, it is not. The largest difference is 15.3, as large as the adapter’s own effect, and the merged model picks a different top token at 4% of the positions. One likely reason: the merged weights \(W + \tfrac{\alpha}{r}BA\) are rounded to bf16 when they are stored, which discards the low bits of a small correction, and the separate adapter keeps them. Another is probably that a random \(B\) is a large, arbitrary perturbation of the model, which makes its output sensitive to every small rounding, so a trained adapter, smaller and with structure, may merge better. We have not measured that.

What does keep the adapter’s exact effect is not to merge it into bf16 weights: either leave it separate, a bf16 base model with an fp32 adapter, which is how we train it and PEFT’s default, or merge and keep the whole model in fp32 (twice the memory). Merging and then casting to bf16 stores rounded weights, which is the case we measured, so a merged bf16 model has to be evaluated before it is relied on. Chapter 13 does that on the trained adapter.

9.5 Rank, alpha and what we leave out

  • Rank is capacity: a rank-\(r\) update can change each layer’s output in only \(r\) directions. Too small and the model cannot fit the task; beyond what the task needs, more rank only adds weights. How much the task needs is an empirical question, and we answer it with a rank sweep, below and above 16 (Chapter 17).
  • Alpha sets the size of the correction. Keeping \(\alpha = 2r\) keeps the scale fixed at 2 whatever the rank, so rank is the only thing the sweep changes.
  • QLoRA (Dettmers et al. 2023) stores the frozen base weights in 4 bits, to save memory, and trains the adapters in higher precision on top. We do not need it: the 4B model’s bf16 weights take 8 GB, which the machine holds with room to spare.
NoteUnder the hood

The two views of Figure 9.1 compute the same function in exact arithmetic, but they are not interchangeable in practice. Training runs the right-hand view, because it never builds the big matrix. Merging builds \(W + \tfrac{\alpha}{r}BA\) once, which is what makes inference free of extra cost. A merge cannot be undone exactly: once the correction is added to bf16 weights the original \(W\) cannot be recovered, so we keep the adapter file and not only the merged model. In fp32 the adapter of the main run is about 132 MB, against 8 GB for the model.

TipFurther reading
  • The LoRA paper (Hu et al. 2022) introduces the method and the low-rank hypothesis it rests on.
  • QLoRA (Dettmers et al. 2023) combines it with a 4-bit base model.
  • Low-rank factorization for recommendation (Koren et al. 2009) is the older use of the same product-of-thin-matrices idea.