Macalinao StudioLearnAll enriched lessons ↗
College path · 7 sectionsSwitch to high school ↗

College / LONGER LESSON lora

LoRA: learn a small change to a large model

Trace a low-rank update, count trainable parameters, explain initialization and distinguish merging from model compression.

Preparation: Vectors, matrix multiplication and gradients; fine-tuning basics.

Plan 25–40 minutes with the activities; this is a study estimate, not a timed requirement. Both paths have complete narration. Your notes and progress are kept separately for each level.

Section 01 / 07

Change the model through a smaller path

College path. Trace a low-rank update, count trainable parameters, explain initialization and distinguish merging from model compression. Preparation: Vectors, matrix multiplication and gradients; fine-tuning basics. Move between Signal Room, Workbench and Field Notes without losing your section. Pause before each worked solution.

Fine-tuning adapts a trained model to examples from a task or domain. Full fine-tuning may change many weights. Low-rank adaptation, called LoRA, instead freezes selected base weight matrices and learns an added update through two smaller matrices. The base model is still needed. An adapter is not usually a complete independent language model, and fewer trainable numbers do not mean that fewer base weights participate in the forward computation. The method changes where learning is allowed, rather than replacing the entire network with a small one.

For a linear map W of shape output dimension by input dimension, write y=Wx+sBAx. A maps input coordinates into an r-dimensional space, and B maps back to the output dimension. The update BA has rank at most r. This constrains the update, not the rank of the original W or the whole nonlinear network.

Worked example. Think of keeping a large reference map and learning a compact set of corrections. In our numerical example, the base map is the two-dimensional identity. The adapter first measures the difference between the two input coordinates, then sends that one number into two output corrections. Base and correction are added.

y = Wx + sBAx; rank(BA) ≤ r

The displayed notation records the process described in the example. Keep its assumptions and units attached to the result.

Try it yourself. Does a rank-one adapter force the whole model to have rank one?

Pause the narration and explain your reasoning before continuing to the worked solution.

Reveal worked solution

No. The added matrix has rank at most one. The frozen base matrix may have much higher rank, and a complete model contains many other operations.

Section 02 / 07

Follow the shapes and count the parameters

Shapes prevent a common misunderstanding: two small matrices can describe an update to a large matrix without storing a separately trainable value at every location. If the base matrix has d output rows and k input columns, it stores d times k weights. The adapter stores r times k values in A and d times r in B. Rank is a design choice. Increasing it expands the possible update space, but also increases trainable state and can change optimization and overfitting behavior.

A has shape r by k, B has shape d by r, and BA has shape d by k. The adapter count is r(k+d), excluding optional biases and other trainable modules. Savings occur when this count is much smaller than dk. Choosing a rank near the dimensions can remove that advantage; low-rank is not synonymous with always smaller.

Worked example. A 1,024 by 1,024 base matrix contains 1,048,576 weights. With rank eight, A and B together contain 16,384 trainable values, one sixty-fourth as many. Rank sixteen doubles that count. The large base weights remain stored; the comparison concerns the parameters trained in this selected layer.

Base count = dk; adapter count = r(k+d)

The displayed notation records the process described in the example. Keep its assumptions and units attached to the result.

Try it yourself. If rank doubles while input and output dimensions stay fixed, what happens to the adapter parameter count?

Pause the narration and explain your reasoning before continuing to the worked solution.

Reveal worked solution

It doubles. That does not automatically double quality or total device memory: the rest of the model and runtime allocations also matter.

Section 03 / 07

Compute the adapter before adding it

Use the input vector [2,1], base matrix equal to the identity, and A equal to the row [1,-1]. A compresses the input into one number: two minus one, which is one. Let B be the column [2,1]. It expands that number into the correction [2,1]. With scale one half, the correction becomes [1,0.5]. Adding it to the unchanged base output [2,1] gives [3,1.5]. Each step is visible, so you can distinguish the original computation from the learned change.

Associativity gives B(Ax)=(BA)x. Computing the small intermediate can avoid materializing the dense update during adapter execution. It does not remove Wx. Our two-dimensional rank-one fixture is separate from the 1,024-dimensional parameter budget; changing the budget rank does not resize these fixed toy matrices.

Worked example. Workbench lets you change the first entry of B and the adapter scale. At B=[2,1] and scale 0.5, both the separate-path and explicitly merged-matrix calculations produce [3,1.5]. At scale zero, the output returns to [2,1] for every B setting. The base matrix itself has not changed.

Ax=[1]; B(Ax)=[2,1]; Wx+0.5B(Ax)=[3,1.5]

The displayed notation records the process described in the example. Keep its assumptions and units attached to the result.

Try it yourself. If scale becomes zero, does that prove the adapter matrices are all zero?

Pause the narration and explain your reasoning before continuing to the worked solution.

Reveal worked solution

No. The scale can disable a nonzero update. Distinguish stored parameters from the contribution they make under a particular configuration.

Section 04 / 07

Initialization determines the first learning step

A common LoRA initialization makes the added update zero by setting one factor to zero and the other to random values. This starts the network at the base model’s output while leaving a path for learning. Setting both factors to zero is different: with an ordinary differentiable loss, the initial gradients of both can vanish because each factor’s gradient depends on the other. Zero initial output does not require every trainable value to be zero. This distinction explains why a seemingly harmless initialization can prevent adaptation from starting.

For scale one, output gradient g, and input x, derivatives are dL/dB=g(Ax) transpose and dL/dA=B transpose g x transpose. With B=0 and nonzero A, the first derivative can be nonzero while the second is zero initially. After B changes, A can receive a gradient. The exact initialization can differ among variants.

Worked example. Use identity W, input [2,1], target [0,0], and loss one half of the squared output length. With A=[1,-1] and B=[0,0], output gradient is [2,1] and Ax is one. B’s gradient is [2,1], so a learning step is possible. If A is also zero, both factor gradients vanish.

At B=0: ∇B L=g(Ax)ᵀ; ∇A L=0

The displayed notation records the process described in the example. Keep its assumptions and units attached to the result.

Try it yourself. Why is zero B with nonzero A different from zeroing both?

Pause the narration and explain your reasoning before continuing to the worked solution.

Reveal worked solution

Nonzero A supplies a signal for B’s gradient. With both zero, each factor blocks the other’s initial gradient in this simple bilinear parameterization.

Section 05 / 07

Rank, scaling and target modules are separate choices

LoRA is a family of configurations rather than one universal recipe. Decide which weight matrices receive adapters, choose a rank, and choose a scaling convention. The original formulation commonly scales the update by alpha divided by rank. Other variants use different rules. Increasing rank while keeping alpha fixed therefore changes both capacity and scaling under that convention. Data quality, training duration and target modules also affect results. A larger adapter is not automatically a better adaptation, especially when examples are narrow or noisy.

Record rank, alpha, scaling variant, target-module names, dropout, initialization and any additionally trained parameters. The original study explored selected attention projections; current implementations can target other linear modules. Comparisons should distinguish changing capacity from changing update magnitude. The Workbench scale is a direct scalar, not an implicit rank-normalized hyperparameter.

Worked example. With original scale alpha divided by r, alpha sixteen and rank eight give scale two. Holding alpha sixteen while raising rank to sixteen gives scale one. If you intend to compare ranks at constant scale, you must account for that difference. Our budget slider changes only the count, while the scale slider changes the small fixture.

Original scaling convention: s = alpha/r

The displayed notation records the process described in the example. Keep its assumptions and units attached to the result.

Try it yourself. Does doubling rank at fixed alpha necessarily keep the original LoRA update scale unchanged?

Pause the narration and explain your reasoning before continuing to the worked solution.

Reveal worked solution

No. Under alpha divided by rank, the scale halves. Name the convention before interpreting a rank comparison.

Section 06 / 07

Merge carefully and keep the adapter identity

For a compatible linear layer, the adapter can be merged into a dense weight matrix by adding the scaled update. The separate and merged paths then represent the same mathematical map under the stated arithmetic. This can remove the separate adapter branch at inference. It does not make the dense base matrix smaller. Keeping adapters separate can support switching tasks without storing a full dense copy for every task. Deployment choices must preserve which base checkpoint, adapter and scaling settings belong together.

Merge equivalence assumes the intended adapter behavior at inference, including disabled dropout and compatible numerical formats. Quantized storage introduces reconstruction and possible re-quantization error, so a merge into a low-bit format needs fresh evaluation. Two individually useful adapters are not guaranteed to compose safely by simple addition.

Worked example. Our identity base plus the rank-one update at scale one half yields merged matrix [[2,-1],[0.5,0.5]]. Multiplying by [2,1] gives [3,1.5], exactly matching the separate route in real arithmetic. The merged matrix still has four entries. Adapter parameter savings concern learning and packaging, not deletion of the base computation.

W_merged = W + sBA

The displayed notation records the process described in the example. Keep its assumptions and units attached to the result.

Try it yourself. Why must an adapter record which base checkpoint it expects?

Pause the narration and explain your reasoning before continuing to the worked solution.

Reveal worked solution

Its learned correction is relative to that model’s representations and weights. Matching tensor shapes alone does not prove that another checkpoint will behave correctly.

Section 07 / 07

Evaluate adaptation rather than assuming improvement

Use held-out examples to test whether adaptation solves the intended task. Compare against the unchanged base and a relevant fine-tuning baseline. Include general behavior you want to retain, not only the narrow training domain. Track overfitting, data leakage and failures on unusual inputs. Count trainable parameters separately from peak training memory and measured serving latency. Save Workbench predictions and results, then label which conclusions came from arithmetic and which would require training and evaluation on actual data.

A reproducible adapter package includes base revision, target modules, rank, scale, tokenizer and preprocessing, training data provenance, hyperparameters and evaluation protocol. Small loss on training examples is not evidence of broad generalization. Evaluate merged and unmerged forms when deployment changes numerical formats.

Worked example. Start with the fixture baseline, save its output, and set scale zero. Then restore scale and change the budget rank from eight to sixteen. The budget doubles while the fixed toy output stays unchanged. Explain why neither operation demonstrates a task-quality improvement, even though both have precisely testable numerical effects.

Parameter arithmetic, numerical equivalence and task quality are separate evidence

The displayed notation records the process described in the example. Keep its assumptions and units attached to the result.

Try it yourself. What does agreement between merged and separate outputs establish?

Pause the narration and explain your reasoning before continuing to the worked solution.

Reveal worked solution

It verifies a numerical identity for the specified matrices. It does not establish that a trained adapter is useful, generalizes, or preserves unrelated capabilities.

Final checkpoint. For a d by k matrix, how many parameters do rank-r factors contain? Option 1: r times (d plus k). Option 2: d times k times r. Option 3: Only r.

Pause and choose an answer before continuing.

Reveal worked solution

Option 1 is correct. A has r times k entries and B has d times r, excluding other trained modules.

LONGER POST-TRAINING LESSONS

Follow demonstrations, rewards and preferences

Worked examples, local narration and connected experiments at your level.

Continue the learning path

Sources and scope

Original worked examples and teaching text, grounded in these primary references. Numerical fixtures explain mechanisms; model quality and hardware performance require separate measurements. Both levels share key examples, with different explanations and checkpoints.

Reset this college path?

This removes only this path’s notes, progress and experiments from this browser.