Skip to main content

LoRA

Customer framing: A customer needed to adapt one open-weight model to several narrow tasks without a GPU cluster or a full model copy per task. Here is the architecture and the tradeoffs.

Official docs:


How it works​

Freeze the pretrained weight W. Next to it, add two thin matrices: A (r Γ— d) and B (d Γ— r). The layer's output is the frozen path plus a scaled update path.

Output: what the frozen weight produces, plus (alpha / r) Γ— B applied to A applied to the input.

One linear layer with LoRA: the input goes through the frozen weight matrix W and, in parallel, through two small trained matrices A and B of rank 8 and a scale of alpha over r; the two paths are summed to give the outputOne linear layer with LoRA: the input goes through the frozen weight matrix W and, in parallel, through two small trained matrices A and B of rank 8 and a scale of alpha over r; the two paths are summed to give the output

Figure FT2-1: One linear layer with LoRA. Grey is frozen, amber is trained, blue is fixed arithmetic, green is the output. Only the amber path gets gradients.

Worked example: a 4096 Γ— 4096 matrix holds ~16.8M weights. At r = 8, A and B add 4096 Γ— 8 Γ— 2 β‰ˆ 65k trainable weights, about 0.4%.

B starts at zero, so the update is zero on step one and the model begins identical to the base.

Why it's cheaper​

LoRA adds parameters but cuts what is trained. Frozen weights need no gradients and no optimizer state, and those are most of the training memory, so memory drops sharply.

Inference is a different story. LoRA does not make the model faster. Merged into W, it costs exactly what the base costs. Unmerged, it adds a small overhead per request but stays small and swappable.

β†’ See Deployment: merge or keep separate


Key knobs​

KnobControlsTypicalHigher means
Rank (r)Capacity of the update8 to 64Learns more, costs more memory, risks overfitting
Alpha (lora_alpha)How strongly the update applies (scale = alpha / r)Often 2 Γ— rA larger step away from the base
Target modulesWhich weight matrices get an adapterAll linear layersMore capacity, more parameters
Learning rateStep size for A and B1e-4 to 2e-4Faster learning, less stable
DropoutRegularisation on the update path0.05Less overfitting on small datasets
Target modulesResult
Attention query and value onlyCheapest. Fine for light style changes; the original paper's setting.
All linear layers (attention + MLP)Usually best; the QLoRA paper's finding. The common default.
Norms and biasesSkip. Tiny, and rarely worth an adapter.
Best practice

Data quality matters more than any hyperparameter. Fix the dataset before tuning r.


QLoRA​

QLoRA is LoRA with the frozen base stored in 4-bit (NF4). The adapters stay in 16-bit. Two extras keep memory down further: double quantization (the quantization constants are themselves quantized) and paged optimizers (optimizer state spills to CPU memory during spikes instead of crashing).

LoRAQLoRA
Base precision16-bit4-bit (NF4)
7B base weight memory~14 GB~4 GB
Speed per stepBaselineRoughly 20–30% slower: weights are dequantized on the fly
QualityBaselineVery close to LoRA
Typical GPU for 7B24 GB or moreA single 16 GB card
Use whenThe base fits in 16-bit on your GPUsIt doesn't

β†’ See Quantization for the same memory trade-off at serving time.

Order of operations​

Amber is the only stage that updates weights. Dashed is optional: merge only when you need a standalone model.

StageFailure if done wrong
Load baseWrong revision: the adapter only works on the exact base it was trained on
FreezeBase weights drift: you are doing full fine-tuning by accident
AttachWrong target modules: too little capacity, or wasted memory
TrainOverfitting: watch held-out loss, not training loss
Save or mergeMerging into 4-bit weights: reload the base in 16-bit and merge into that

Deployment: merge or keep separate​

Merge into the baseKeep as a separate adapter
LatencySame as the baseSmall overhead per request
Models in memoryOne full model per taskOne base shared by many adapters
ArtifactGigabytes per taskMegabytes per adapter
SwappingRedeploy the modelChoose the adapter per request
Use whenOne task, latency-criticalSeveral tasks or tenants on one GPU pool

Multi-LoRA serving keeps one base model in GPU memory and applies a different adapter per request. vLLM supports it natively. For Meridian, each task (answer drafting, ticket triage) gets its own adapter, and every on-prem tenant shares one base model.

β†’ See Sizing a deployment


Limits​

LimitWhat to do
Poor at teaching large amounts of new knowledgePut knowledge in RAG; use LoRA for behaviour
Rank and target modules need tuning per taskStart at the typical values; change one knob per run against a fixed eval set
An adapter only works on the exact base it was trained onVersion the adapter with its base model; retrain when the base changes