← Back to blog

Guide /

Fine-tuning a small language model with LoRA

What a LoRA adapter actually changes, a tiny runnable example, and how to decide whether fine-tuning helped.

Updated

A small silver insert beside a large graphite matrix and a transparent glass frame.
concept artwork · AI-assisted

Suppose you want a small language model to answer technical questions in a very particular format. It usually knows the subject, but sometimes adds a paragraph you did not ask for, forgets a field, or ignores your examples. Fine-tuning might help. First, though, you need a way to tell whether it helped more than a better prompt would.

LoRA makes this experiment more manageable by learning a compact adjustment to selected model weights. Think of it as fitting an adjustable attachment to a machine: the original parts stay fixed while you learn how to change their output. Let’s make that idea concrete before touching a training script.

What the adapter changes

For one linear layer, the original computation is y = Wx. LoRA adds a second path:

y = Wx + (alpha / r) BAx

W: d × k, frozen
A: r × k, trainable
B: d × r, trainable

Here, x is the input vector and r is the adapter’s rank. Instead of learning a separate adjustment for every entry of W, the adapter learns two smaller matrices whose product has rank at most r. The two outputs are added. This is the formulation in Section 4.1 of the original LoRA paper.

For a 4,096 × 4,096 weight matrix, the full matrix contains 16,777,216 parameters. With rank 8, the two adapter matrices contain 65,536 parameters together: about 0.39% of that matrix’s size. That is a count for this one matrix. It is not a whole-model memory estimate or a promise about training speed; activations, other trainable components and the training setup still matter.

The attachment analogy also has a limit. An adapter does not simply store a list of facts or examples. Its learned matrices alter the computation for inputs passing through the selected layers.

A tiny example you can run

No GPU is needed to see the arithmetic. Use a two-dimensional identity matrix as the frozen layer, then add a rank-one update:

const W = [[1, 0], [0, 1]];
const A = [[1, -1]];
const B = [[0.1], [-0.2]];
const x = [2, 1];
// alpha / r = 1

The base path returns [2, 1]. The adapter first computes Ax = [1], then BAx = [0.1, -0.2]. Adding the paths gives [2.1, 0.8].

Download the complete example and run:

node lora-toy.mjs

I ran it with Node 24.21.0 and no external packages. It returned the values above and passed assertions that the original matrix remained unchanged and that multiplying by the merged matrix W + BA produced the same result, within numerical tolerance. It also checks the parameter counts.

This is fixed matrix arithmetic, not an adapter training run. It shows where the adjustment enters the computation; it cannot tell you which rank will work for your dataset or whether a language model will improve.

Choose the task before the rank

A useful first task is narrow enough to score. “Answer better” is difficult to evaluate. “Produce a valid JSON object with these three fields and an answer supported by the supplied passage” gives you separate checks for format and factual support.

Build a small reviewed dataset with that consistent input/output contract. Remove duplicates and split by the source that must remain unseen: document, customer, author or collection period, depending on the task. Keep the final test separate before trying prompts or adapters. The evaluation checklist explains why splitting individual rows can be misleading.

Then test the base model with a sensible prompt and a few examples. Save that baseline. If it already solves the problem, you have learned something useful without running training. If it fails, inspect the failures: missing knowledge, unclear instructions and inconsistent target answers call for different fixes.

Fine-tuning deserves a concrete hypothesis, such as “consistent examples will improve output formatting without increasing unsupported claims.” That hypothesis tells you what to measure later.

Keep the first training experiment small

Before a long run, inspect a few tokenized examples. Check that the model’s chat template is applied once, that the answer survives truncation, and that the loss is computed on the tokens you intend. A low loss on the wrong target is still the wrong experiment.

Choose adapter targets from the actual model architecture. Names such as q_proj and v_proj are common examples, not universal module names. The PEFT LoRA guide explains rank, scaling and adapter configuration. In the original formulation, the scale is alpha / r; alternative variants can use different scaling rules, so record the setting.

Start with one reviewed configuration. Confirm that a short run changes the adapter weights and produces a loadable checkpoint before comparing many ranks. Save the base-model revision, tokenizer, library versions, data split, seed and configuration alongside the adapter. Someone loading it later needs to know which base it belongs to.

Decide whether to keep it

Compare the base model and adapted model on the same untouched examples, with the same evaluation rules. Report format validity separately from answer correctness. Read cases where the adapter improved, cases where it regressed, and cases where both models confidently failed.

Training loss answers whether optimization reduced the training objective. Your task evaluation answers whether the behavior became more useful. Those are different observations.

A practical next step is simple: write twenty representative examples and define a scoring rule before choosing hardware. If you cannot explain what improvement would look like, changing the rank will not resolve that uncertainty.

Source paper

Prepared with AI assistance. Sources and a worked example are provided so you can check the reasoning.

Contact

Elgün · AI Researcher

Let's talk

Good questions welcome. Citations optional.

Academic CV