AI system design

LoRA and QLoRA: understand what becomes smaller

By Anup RaiReviewed

Parameter-efficient fine-tuning (PEFT) adapts a model by training a subset of parameters or additional small components. Low-Rank Adaptation (LoRA) freezes a base weight matrix and learns an additive update represented by two smaller matrices. QLoRA trains adapters through a frozen, quantized base model.

Estimate four costs separately. Weight storage holds the model parameters. Trainable state holds the gradients and optimizer information used to update trainable parameters. Activation memory holds intermediate values needed during training. Serving cost depends on the resulting model and how it handles production traffic.

Start with the memory problem

A team needs task-specific versions of one base model for several document workflows. Keeping separate full checkpoints increases storage and deployment work. LoRA can share a base and store smaller task updates. Whether this is worthwhile depends on quality and the serving engine's adapter support.

Memory component Ordinary LoRA QLoRA
Frozen base weights Remain present, often at higher precision Stored in a supported low-bit representation
Base gradients and optimizer state Not needed for frozen weights Not needed for frozen weights
Adapter weights, gradients, optimizer Required Required; not necessarily four-bit
Activations and temporary buffers Still required Still required

This is why an adapter with very few parameters can still run out of GPU memory on a long training sequence.

LoRA Mechanics

Let x be the input column vector and W the layer's original weight matrix. LoRA adds a correction computed with two trainable matrices, A and B. The rank setting r is the width of the intermediate vector. The scaling parameter α, read as alpha, sets the multiplier α/r applied to the correction before it is added to the original output.

h = W x + (α/r) B A x
W: d_out × d_in       A: r × d_in       B: d_out × r
  1. Compute the ordinary frozen projection W x.
  2. Project the input into r dimensions using A.
  3. Project back to the output dimension using B.
  4. Scale the update and add it to the frozen projection.
  5. Update A and B during training, leaving W unchanged.

For W = 4096 × 4096 and r = 16, the base has 16,777,216 weights. The adapter has 16 × 4096 + 4096 × 16 = 131,072, or 0.78125% as many. This is a single-layer parameter comparison, not a prediction of whole-model training memory or speed.

Architecture / visual model
flowchart LR X[Input x] --> W[Frozen W] X --> A[Trainable A: reduce dimension] A --> B[Trainable B: restore dimension] B --> S[Scale by alpha over rank] W --> P[Add] S --> P P --> Y[Output h]
Read diagram source
flowchart LR
  X[Input x] --> W[Frozen W]
  X --> A[Trainable A: reduce dimension]
  A --> B[Trainable B: restore dimension]
  B --> S[Scale by alpha over rank]
  W --> P[Add]
  S --> P
  P --> Y[Output h]

In the original recipe, A starts with small random values and B starts at zero. The correction is initially zero, so the layer starts with the base model's behavior. During the first update, the nonzero output of A gives the optimizer a signal for changing B. Once B changes, the optimizer can also get a signal for changing A. If both matrices started at zero, the correction would give neither matrix a first-step gradient. α = 2r is one possible setting. Rank, scaling, initialization, learning rate, and target layers interact. Original LoRA formulation.

Select target layers deliberately

Adapting only selected attention projections reduces state. Including attention output and feed-forward projections gives the update more places to change behavior at greater cost. Architecture names differ; inspect the actual modules rather than assuming every model contains q_proj and v_proj.

Run a small comparison with the same data and evaluation: narrow target set versus all supported linear layers, then a rank sweep if needed. For mixture-of-experts models, fused expert parameters and serving support introduce additional compatibility questions. Verify the installed version's PEFT configuration; examples from a development branch may require a newer package than your environment.

QLoRA: separate storage precision from training precision

QLoRA stores the base weights in low precision and trains the LoRA adapters. In the original recipe, a layer reads its packed weights and reconstructs their approximate values in BF16 for the matrix multiplication. It adds the adapter's output and sends the result to the next layer. When the trainer calculates gradients, it treats the base weights as fixed and uses the gradients for the adapter weights to update A and B. The base still participates in the calculation. The optimizer only needs update state for the trainable weights.

Ingredient Purpose Limitation to explain
NF4 Nonuniform four-bit representation designed around normally distributed weights Distribution assumptions and block scaling matter
Double quantization Stores the block scales themselves in a smaller format The trainer must reconstruct the scales before using them to reconstruct the weights
Paged optimizers Can move optimizer-state pages between GPU and host memory when memory pressure rises Those transfers cost time, and some workloads can still run out of memory

The QLoRA paper demonstrated adaptation of a 65B model on one 48 GB GPU with its recipe. Treat that as a documented experiment, not a promise that any 70B workload fits with arbitrary context and batch size. Computation dtype and adapter dtype are separate from the stored base dtype.

Advanced Variants

Method What changes When to investigate
DoRA Separates weight magnitude and direction; applies low-rank adaptation to direction A measured quality gap may justify extra state and computation
VeRA Freezes shared random low-rank matrices and learns scaling vectors Very small task-specific trainable state is valuable
Rank-stabilized LoRA Uses α/√r scaling rather than α/r Testing larger ranks requires stable update scaling

None is a universal replacement for ordinary LoRA. Compare target quality, retained capabilities, export compatibility, and serving support. Primary descriptions: DoRA, VeRA, rank-stabilized LoRA.

Multi-LoRA Serving

A compatible engine can select an adapter for each request while sharing a base. Define the adapter identity as part of routing and authorization; a customer must not choose another customer's private adapter merely by naming it.

  1. Pin the base revision, tokenizer, adapter revision, target modules, and supported rank.
  2. Distinguish adapters stored on disk, cached in CPU memory, and resident on the GPU.
  3. Measure cold loading and eviction under realistic tenant traffic.
  4. Batch only combinations supported by the runtime and capacity plan.
  5. Roll back the adapter and its configuration together.

Merging W + (α/r)BA can produce a fixed dense layer where supported. It simplifies a single-model deployment but sacrifices easy adapter switching. Quantized merges can introduce additional rounding or require conversion; evaluate the actual exported artifact. Consult the current vLLM LoRA serving documentation before relying on specific limits.

Interview practice

  1. Why can a frozen model still consume training memory? Its weights, forward computations, required activations, and temporary buffers remain; only selected gradient and optimizer state disappear.
  2. Does rank 16 mean sixteen trainable parameters? No. The two matrices contain r(d_in + d_out) parameters for one adapted linear layer.
  3. Does QLoRA train four-bit adapters? Not inherently. It quantizes the frozen base; adapter and computation dtypes are separate settings.
  4. Why not always maximize rank? More capacity costs memory and can overfit. The useful rank depends on data, target layers, and the task.
  5. Can one base serve a thousand adapters? Storage may be feasible, but active residency, loading, kernel limits, traffic skew, and tail latency determine serving capacity.
  6. What could break an adapter after a base upgrade? Changed weights, tokenizer/template, dimensions, or module definitions. Compatibility and behavior both need revalidation.

Recall card and closing

LoRA limits which weights training can change. QLoRA also reduces the storage used by the frozen base. Before serving multiple adapters, test compatibility with the exact base revision and measure request latency when an adapter must be loaded and when it is already on the GPU. Quantization.

Your notes

Write the decision you would make and the uncertainty you would investigate next. Saved only in this browser.

Explore the diagram