Module 3 · Efficient fine-tuning

LoRA, QLoRA and DoRA in practice

7 min read

LoRA has become the default fine-tuning method: little memory, small files, quality close to full fine-tuning. Its variants QLoRA and DoRA promise even less memory or a bit more quality. This lesson explains the mechanism of each, their settings, and measures them on Forge — with results that do not always confirm the promises.

The LoRA principle

A linear layer computes y = W·x, with a d × k matrix W (for example 896 × 896). Fine-tuning amounts to learning a modification ΔW. LoRA's hypothesis (Low-Rank Adaptation, Hu et al., 2021): this modification has low rank, and can be written as the product of two small matrices:

y = W·x + (α / r) · B·A·x        A : r × k (initialisée au hasard), B : d × r (initialisée à ZÉRO)

(A is initialized randomly, B to zero.) With r = 16, instead of 896 × 896 = 802,816 parameters, you train 16 × (896 + 896) = 28,672 — 28 times fewer. B is initialized to zero: at the start, the adapter changes nothing, and the model starts exactly where the base model was. W stays frozen.

In Forge, the configuration fits in a few lines:

def lora_config(r: int = 16, alpha: int = 32, dropout: float = 0.05, dora: bool = False):
    from peft import LoraConfig

    # « all-linear » : toutes les projections linéaires (attention ET MLP), sauf la tête de sortie
    return LoraConfig(r=r, lora_alpha=alpha, lora_dropout=dropout, target_modules="all-linear",
                      use_dora=dora, task_type="CAUSAL_LM")
SettingRoleCommon value
r (rank)Adapter capacity8 to 64
lora_alphaScale of the modification (α/r)Often 2 × r; Forge keeps 32 by default
target_modulesAdapted layersall-linear: attention and MLP
lora_dropoutRegularization0 to 0.1

Adapting all linear layers rather than only the attention projections (the setting of the first publications) generally gives better results for a modest cost: it is today's recommended choice.

QLoRA: the base model in 4 bits

QLoRA (Dettmers et al., 2023) applies LoRA to a base model quantized to 4 bits:

if method == "qlora":  # poids gelés en 4 bits (NF4), adaptateurs LoRA entraînés en 16/32 bits
    quantization = BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_quant_type="nf4",
                                      bnb_4bit_use_double_quant=True, bnb_4bit_compute_dtype=dtype)
  • NF4 (NormalFloat 4): a 4-bit format whose 16 levels are distributed according to a normal law, like a network's weights.
  • Double quantization: the quantization scale constants are themselves quantized, to save a few hundred more MB on a large model.
  • Compute dtype: weights are stored in 4 bits but dequantized on the fly to 16 or 32 bits for each computation.

The adapters stay in full precision. This is what makes it possible to fine-tune a 70 B on a single GPU.

DoRA: magnitude and direction

DoRA (Weight-Decomposed Low-Rank Adaptation, Liu et al., 2024) decomposes each weight into a magnitude (a vector) and a direction (a normalized matrix), and applies LoRA only to the direction, the magnitude being learned separately. The idea: get closer to how full fine-tuning modifies weights. In PEFT, it is an option: use_dora=True.

The measured comparison

Qwen2.5-0.5B-Instruct, 1,179 examples, 2 epochs, r = 16, same GPU:

MethodTrained parametersDurationPeak memoryValidationTestInference (unmerged)
LoRA8.80 M10.2 min2.87 GB1.000.65155 ms/message
QLoRA (NF4)8.80 M11.6 min1.66 GB0.950.53153 ms/message
DoRA9.10 M18.1 min2.87 GB1.000.61465 ms/message

What these numbers say — and what they don't:

  • QLoRA divides memory by 1.7, not by 4: on a small model, the (non-quantized) embedding matrix and the buffers weigh heavily (lesson 1.3). On an 8 B or a 70 B, the gain approaches a factor of 4. It is slower: each computation starts with a dequantization.
  • QLoRA scores 0.53 on the test set, versus 0.65 for LoRA: the gap is significant on the same examples (paired test, lesson 5.1) and falls outside the range observed between LoRA seeds (0.65 to 0.74, lesson 3.3). On this small model, quantizing the frozen weights to 4 bits cost quality; on large models, published work generally shows a much smaller gap.
  • DoRA brings nothing measurable here, costs 75% more training time, and triples latency as long as the adapter is not merged. DoRA's published gains exist, but they are on the order of one to two points: invisible under the variance of a small test set.

The rank

The rank sets the adapter's capacity. On Forge, same data, 2 epochs:

RankTrained parametersPeak memoryDurationTest
r = 42.20 M (0.44%)2.79 GB10.0 min0.58
r = 168.80 M (1.75%)2.87 GB10.2 min0.65 (0.65 and 0.74 with other seeds)
r = 6435.19 M (6.65%)3.18 GB10.3 min0.61

Multiplying the rank by 16 changes almost nothing in memory or duration — the frozen weights dominate — and nothing measurable in quality: the gaps stay below the variance between seeds. For a narrow task like triage, a low rank is enough; a high rank is justified for tasks that require learning a lot (new domain, new language), and only if measurement confirms it. A limitation of this experiment, worth knowing: α stayed fixed at 32 in all three runs, so the α/r scale was 8, 2 and 0.5. Changing r therefore also changed the effective size of the updates — two variables at once. For a clean comparison, keep α proportional to r, or enable rsLoRA (use_rslora=True in PEFT), which divides by √r instead of r to make the scale less sensitive to rank.

Classic pitfalls

  • Forgetting to enable adapter training when loading a quantized model by hand: TRL and PEFT do it when you pass peft_config to the trainer; in manual code, prepare_model_for_kbit_training is required.
  • Merging a QLoRA adapter into the 4-bit model: the merge is done into the 16-bit base model (lesson 5.3), otherwise rounding degrades the result.
  • Comparing methods on a single run: the variance from one training run to another can exceed the gap between methods.

Key takeaways

  • LoRA adds a rank-r B·A to each linear layer; B starts at zero, W stays frozen.
  • Target all linear layers; r from 8 to 64, α ≈ 2r.
  • QLoRA: NF4 base, full-precision adapters; reduced memory gain on a small model, and slower training.
  • DoRA: much higher cost, invisible gain here; merge before serving.
  • A comparison of methods on a single run proves nothing: measure several seeds.

Practice

  1. Compute the number of LoRA parameters of Qwen2.5-0.5B for r = 16 from its layer dimensions, and check against the output of examples/03_sft_lora.py.
  2. Run examples/03_sft_lora.py qlora and compare the peak memory with your estimate from lesson 1.3.
  3. Train LoRA with target_modules=["q_proj", "v_proj"] only: what happens to the parameter count and the test score?

Resources