lora-from-scratch: adapting a small language model to new text by training a small fraction of its weights

Abstract. A small pretrained transformer is adapted to source code by training two thin matrices per layer instead of its weights. The backward pass, the adapters and the merge are written by hand, and every figure below runs the same adapter files in plain JavaScript.

Loading model and adapters...

1Switching an adapter on

The base model has only read Python documentation. An adapter is a small file, a few hundred kilobytes at most, that changes how it writes. Figure 1 generates from one prompt and seed, so any difference between two runs is the adapter and nothing else. Each token is shaded by how unlikely the model found it.

Adapter

1 is the strength it was trained at; 0 is the plain base model.

Output

adapter off
Nothing generated yet. Press Generate with the adapter off, then choose Python source r=4 and press it again. Prompt and seed stay the same.

At the hovered token: the base model against the current adapter
base modelwith the adapter at this scale
Hover or click a token above.
Figure 1. One prompt, one seed, one adapter at a time. Switching the adapter or moving the scale regenerates immediately. Bars compare the next-token probabilities of the plain base model and the adapted model at the hovered position.

2Measuring it

Looking at samples proves little. Figure 3 scores three 128-token passages the model never trained on, computed in this page with the same forward pass that generated the text above.

Nine forward passes, a few seconds.
Figure 3. Held-out loss in nats per token (lower is better), one row per model and one column per passage, with each adapter at the scale chosen in Figure 1. The shaded cell in each adapter row is the passage it was trained for.

3Five experiments

Loading results...

Experiment 1 and 2: base model, full fine-tune and LoRA at three ranks

Figure 4. Loss against rank on the new domain. Dashed lines: the untuned base and a full fine-tune.
Figure 5. Trainable parameters against loss on the new domain.

Experiments 3 and 4: forgetting, and less data

Figure 6. Loss on the original Python documentation after tuning, as an increase over the untouched base.
Figure 7. LoRA r=4 trained on 10, 30 and 100 percent of the new-domain text, with the same number of steps.

Experiment 5: adapter swapping

Table 1. Every measured number behind the figures, read from docs/results.json. Losses are nats per token on held-out validation documents.

4Trying to break it

Three ways to misuse an adapter, and what the code does about each.

Figure 8. Push the scale to 2. The curves are Python measurements of the code adapter on the validation sets; the dashed vertical line follows the slider in Figure 1. The table scores the three passages in this page at scale 0, 1 and 2.
Press one. The page should refuse it with a plain message and keep using the adapter it had.
Figure 9. Two damaged adapter files built from the real ones. Shapes are checked before anything is attached, so a bad file cannot change the model. You can also load your own file in Figure 1.
Figure 10. Merge the adapter into the base weights, then take it out. Each row fingerprints every base weight with SHA-256. Subtracting the product can leave float32 rounding; restoring a saved copy is exact.

5Limits and parity

JavaScript against Python
Checking...