Highlights

Contents

Part V
05

LoRA as a small correction

Specialization need not update every weight. LoRA adds a small factored correction. Explain its capacity, scale, initialization, and gradient path before assuming it removes the cost of training.

THE AGENT AND ITS TRAINING PATHDefine tools01Run the loop02Measure tasks03Choose training04Build LoRA05Count adapters06Serialize traces07Train and validate08Generate tasks09Check traces10Compare models11Prepare inference12

#Section 27Full fine-tuning

A weight needs more company in training than in inference: its gradient and the optimizer’s running statistics. A model that fits for generation can fail to fit for training.

Updating all parameters and compute cost

Full fine-tuning makes all selected parameters trainable, normally all of them. The forward pass predicts, backpropagation computes gradients, and the optimizer updates weights. The forward pass is a full decoder computation however small the dataset.

Memory cost and optimizer-state cost

An optimizer state is information retained between updates. Adam stores running gradient statistics alongside trainable parameters; those tensors add memory beyond the weights.

Activations and temporary buffers add further memory. Mixed precision can also retain a high-precision copy of weights.

PARAMETER STATE PAYLOAD, WITH EXPLICIT PRECISION ASSUMPTIONSfull FP32 Adam648,159,232 bytesfrozen BF16 + FP32 adapter Adam90,719,232 bytesactivations still cost memoryFinch-24; r = 8 on all seven projections; buffers excluded
Figure 5.25Computed Finch-24 parameter-state payload under two stated conventions. Full training uses FP32 weights, gradients, and Adam moments; LoRA uses BF16 frozen weights and FP32 adapter state. Neither row includes activations.

#Section 28Parameter-efficient fine-tuning

The stockroom task needs a small tool vocabulary and a consistent protocol. Updating every feature may be unnecessary, and storing a full model copy per specialization is expensive.

PEFT, adapters, and why LoRA exists

Parameter-efficient fine-tuning, or PEFT, trains a subset of parameters or a small added set. An adapter is a trainable component attached to a fixed model. PEFT methods differ in what they change; the term is not a synonym for LoRA.

Low-rank adaptation, or LoRA, adds a factored correction to a weight matrix and keeps the original fixed. Hu and colleagues introduced it in 2021 as a useful restriction on adaptation, not a proof that all updates are low rank. LoRA paper.

Training still runs the base forward pass, and gradients still flow through frozen layers to reach earlier adapters. LoRA removes base gradients and base optimizer state, but activations and most decoder compute remain. Its memory saving is easier to predict than its speedup.

#Section 29LoRA intuition

A projection already encodes useful language patterns, and we want it to adapt to tool calls without replacing it. Write the effective matrix as the old one plus a learned correction.

Frozen base weights and low-rank updates

Frozen weights take part in the forward pass but get no optimizer updates. A low-rank update expresses change through fewer independent directions than a general dense matrix. For a projection from width dind_{\mathrm{in}} to doutd_{\mathrm{out}}, LoRA learns factors AA and BB instead of every entry.

W′=W+ΔW,ΔW=sBA.W' = W + \Delta W, \qquad \Delta W = sBA.

Read this as the effective weight is the frozen weight plus correction sBAsBA. With column vectors, WW is d_out × d_in; libraries may store the transpose for the same map.

Why low rank can be enough

Adapting a trained representation may need far fewer directions than learning it from scratch. A small rank can still touch every output coordinate; it does not mean only a few rows change, nor that the capacity suffices for every task.

ONE PROJECTION, A FROZEN PATH AND A TRAINABLE CORRECTIONxW frozenA trainableBalpha/r+yWx(alpha/r) B(Ax)gradients update A and B; W still carries the input
Figure 5.26A schematic projection adds a trained correction to the frozen path. Grids show shapes at a small schematic size, not Finch-24's full dimensions.
Figure 5.27Move across the sheets or use arrow keys. The base stays fixed while a correction separates; this illustrates adaptation, not a literal matrix layout. Open illustration

#Section 30Low-rank factorization

A matrix with many entries can describe a change built from one shared feature. What matters is how many independent directions the update can express, not how many entries are nonzero.

Matrices A and B, rank r, and shape intuition

The rank rr is the width of the adapter's intermediate representation. AA is r × d_in, mapping the input to rr features; BB is d_out × r, expanding them back. Their product is d_out × d_in with rank at most rr.

A DENSE UPDATE CAN BE STORED AS TWO THIN FACTORSdelta W: 4 x 416 entries=B: 4 x 1xA: 1 x 44 + 4 = 8 trainable entriesrank 1one shareddirectionsaving parameters also restricts the possible update
Figure 5.28An illustrative 4 × 4 rank-one correction uses a 4 × 1 and a 1 × 4 factor. The computed count falls from 16 dense entries to 8 factor entries.

Read actual projection shapes: grouped-query keys and values are narrower than queries, so their adapter costs differ.

#Section 31LoRA forward pass

Follow the two paths in the figure: the original projection and a small learned correction.

Base projection, adapter branch, and combining outputs

y=Wx+sB(Ax).y = Wx + sB(Ax).

Read this as the output is the base projection plus the scaled adapter projection. Computing AxAx first avoids building a dense BABA during unmerged inference.

A RANK-ONE CORRECTION YOU CAN MULTIPLY BY HANDA12xx21=4Ax = 1 x 2 + 2 x 1B34x 4 =1216scale = alpha/r = 2base output = [2, 1]sum = [26, 33]
Figure 5.29Every intermediate value in this illustrative rank-one forward pass is computed. The identity base matrix makes its contribution visible.

Training only adapter parameters

Base output and correction must match in shape, device, and dtype. Check rectangular projections, which reveal transpose bugs that square matrices can hide.

The optimizer gets only the factors and any explicitly chosen extras. Freezing WW stops updates to WW, not gradient flow through it; gradients still pass to the input and upstream adapters.

#Section 32LoRA scaling

Doubling rank changes both the number of features and how their output is scaled. A comparison that silently changes the effective scale confuses capacity with update size.

Rank, alpha, and alpha/r

The alpha setting scales the correction. In the original convention, scale is alpha divided by rank.

Read this as the correction multiplier is alpha over rank. Alpha is a scale setting, not rank or learning rate, and it changes both the forward correction and the gradients through it.

Controlling update magnitude

Training moves both factors, so scale alone does not predict correction size. Judge rank and scale together using validation.

FIXED ALPHA 16: DOUBLING RANK HALVES THE MULTIPLIER s = α / rr = 4s = 16 / 4 = 4r = 8s = 16 / 8 = 2r = 16s = 16 / 16 = 1adapter featuresscaleorange: the Finch-24 adapter; learned correction norms need not follow these ratios
Figure 5.30Computed scales for three ranks at alpha 16. More adapter features come with a smaller multiplier, so rank and scale must be reported together.

Some methods divide by r\sqrt r instead. Load the convention used in training: applying the wrong one changes the function even when the tensors load cleanly. PEFT records these options in its LoRA configuration reference.

#Section 33LoRA initialization

Attaching an adapter to a trained model should leave its outputs unchanged at first. A random correction would shift behavior before the first step and muddy the baseline comparison.

Initializing A, initializing B, and base equivalence

Initialization sets parameters before training. The common LoRA choice is random nonzero AA and zero BB, so BA = 0 and W + sBA = W at attachment. Other schemes exist; this chapter uses this one.

It also shapes the first backward pass. With B=0B=0, the gradient for AA contains BB and is zero, while the gradient for BB is nonzero because AA is. If both start at zero, both gradients are zero and the adapter never starts learning.

ZERO B STARTS WITH THE BASE FUNCTION, NOT A ZERO MODELA randomnonzero features000000000B = 0adapter output = 0W + scale BAinitially equals Wfirst backward pass: B can get a gradient; A initially gets zero
Figure 5.31Schematic factor grids show a zero correction at attachment time. Random-grid shades are illustrative; every entry of the zero factor is exactly zero.

Check attachment by comparing base and adapted logits in evaluation mode with the same numerics. Dropout, changed precision, or a modified template can cause differences unrelated to the correction, so disable stochastic layers and record the tolerance.

BA=0BA=0 makes the functions equal mathematically, but measure anyway: replacement-module bugs can transpose weights, double the scale, or alter a bias. Section 69 applies the same check to merging, where floating-point arithmetic adds more differences.