What LoRA is
Full fine-tuning updates every weight, so each task needs its own copy of the model, and AdamW keeps two extra numbers per weight while training. LoRA (low-rank adaptation) freezes the pretrained weights and learns only a small correction to some of the weight matrices. For a frozen matrix , it adds the product of two thin matrices, () and (), with a rank much smaller than either side:
The bet, from Hu et al., is that the change fine-tuning makes has a low "intrinsic rank", so a rank- update captures most of it. Aghajanyan et al. had found that fine-tuning can be done in a random subspace of a few hundred dimensions with little loss. Hu et al. report that on GPT-3 175 B, LoRA cut the trainable parameters 10,000-fold and the GPU memory 3-fold compared with full fine-tuning.
This page runs peft's LoRA on SmolLM2-135M-Instruct: rank , , no dropout, 40 AdamW steps on eight text-to-SQL rows of b-mc2/sql-create-context (rows 0–8 without 6), with row 10 held out. Each row gives a CREATE TABLE statement and a question; the answer is the SQL query.
Where the adapters go
Every one of SmolLM2's 30 layers has seven linear projections: four in attention (q_proj, k_proj and v_proj produce queries, keys and values; o_proj mixes the heads back together) and three in the MLP (gate_proj, up_proj, down_proj). The Where the adapters go grid shows all 210, with their shapes. Here only q_proj (576 × 576) and v_proj (192 × 576, three key-value heads of 64) get adapters, peft's default for Llama-style models and the choice the LoRA paper made for GPT-3. The embeddings, norms, LM head and the other five projections stay frozen. Each adapter cell is shaded by at the selected step, so the grid shows which layers moved most.
The Trainable parameters bar counts what that costs: trainable weights, 0.34 % of the 134,975,808 in the adapted model. AdamW's two moments then take 921,600 numbers instead of the 269,030,016 that full fine-tuning would need.
Starting from zero
peft initialises randomly (Kaiming-uniform) and with zeros, so and the wrapped model starts exactly at the base model: the page checks that its first logits match with a largest difference of 0. Write for the gradient arriving at the layer's output and . Then
While is zero, 's gradient is zero too, so the first step moves only ; the One adapter panel records that moved by exactly 0 in that step and shows how far it has moved since. The panel draws , and their product for the selected layer and projection. Every checkpoint is recorded for layer 15; other layers only after the last step.
The factor , here , sets the size of the update. Hu et al. set to the first rank they try and do not tune it; the scaling saves retuning other hyperparameters when changes.
Training and memorising
The Training · 40 steps on 8 rows chart follows two losses: the batch's (mean cross-entropy over the answer tokens of all eight rows) and the held-out row's. The batch loss falls from 1.459 to 0.002. The held-out loss falls from 2.411 to its lowest, 1.107, at step 8, then climbs back to 2.471 by step 40: with 460,800 weights and eight rows, the adapters memorise the rows. The Held-out row 10 · loss per answer token strip shows the same per token, at step 0 and at the selected step.
Greedy replies shows what that means in text. At step 0 the instruct model answers the held-out question in prose ("Based on the code, there are 2 acting statuses …"). By step 5 it writes SQL, and by the end it reproduces training row 0's query exactly. Its held-out query never matches the dataset's SELECT COUNT(DISTINCT temporary_acting) FROM management. Eight examples teach the format, not the skill.
Merging back
Because the adapter is linear, it can be folded into the weight it modifies:
The Merging back panel shows a 64 × 64 corner of , of and of . peft's merge_and_unload() gives exactly this formula (largest difference 0). The merged model's logits on the held-out row match the adapted model's to , rounding in float32, and the frozen weights are unchanged after training. The adapters changed the logits by up to 32.4.
Merged, the model is a plain SmolLM2 again and runs at the base model's cost, which is the advantage Hu et al. claim over adapter layers: no extra latency at inference. Kept apart, the 460,800 adapter weights are a small file, and many tasks can share one base by swapping adapters.
Where LoRA came from
Houlsby et al. (2019) introduced adapters: small bottleneck layers inserted into each transformer block and trained while the rest stays frozen. They add computation at inference time, which LoRA avoids by merging. LoRA (Hu et al., 2022) became the default parameter-efficient method. The QLoRA tab keeps the frozen base in 4 bits, and the DoRA tab splits each weight into a length and a direction. Biderman et al. later compared LoRA with full fine-tuning on code and mathematics: LoRA learned less of the new domain but forgot less of what the base model could already do.
Papers
- LoRA: Low-Rank Adaptation of Large Language Models Introduces LoRA: frozen weights plus a trainable low-rank update B·A scaled by α/r, initialised at zero and mergeable after training.
- Intrinsic Dimensionality Explains the Effectiveness of Language Model Fine-Tuning Fine-tuning works in a random low-dimensional subspace of the weights, the observation behind low-rank updates.
- Parameter-Efficient Transfer Learning for NLP Adapters: small trainable bottleneck layers in a frozen transformer, the method LoRA set out to improve on.
- SmolLM2: When Smol Goes Big -- Data-Centric Training of a Small Language Model SmolLM2-135M-Instruct, the model this page adapts.
- Spider: A Large-Scale Human-Labeled Dataset for Complex and Cross-Domain Semantic Parsing and Text-to-SQL Task One of the two text-to-SQL sets that sql-create-context, the data on this page, is built from.
- Seq2SQL: Generating Structured Queries from Natural Language using Reinforcement Learning Introduces WikiSQL, the other source of sql-create-context.
- LoRA Learns Less and Forgets Less LoRA against full fine-tuning on code and mathematics: lower gains in the target domain, less forgetting outside it.