Agentic AI Engineering/Fine-tuning & Local Models
Lesson 2 / 4 · 22 min · 0 / 4 exercises
Course contents
Concept · 4 hands-on exercises

LoRA & QLoRA

Parameter-efficient fine-tuning: train small adapters instead of the whole model. How LoRA works, what QLoRA adds, the knobs that matter and the current tooling.

Full fine-tuning updates every weight. That needs memory for the weights, their gradients and the optimizer's state, often several times the model's size, and every task produces a complete new copy of the model. LoRA (low-rank adaptation) freezes the base model and learns a small update for selected weight matrices instead. The update to a d × d matrix is written as the product of two thin matrices, B (d × r) and A (r × d), with a rank r far smaller than d. That works because the changes fine-tuning makes tend to have low intrinsic rank. You train a tiny fraction of the parameters, and the result is an adapter file you load on top of a shared base, so one base model can serve many task adapters. Serving engines such as vLLM can load several LoRA adapters over one base at once.

Explore: How few parameters LoRA trainsExercise 1 of 4
Hidden size (d)
Rank (r)
W: d × d (frozen)+B · A: (d × r)(r × d)
16.8Mparams in one full matrix
131.1kparams LoRA trains for it
0.78%share you train

Across 32 layers × 4 matrices: 16.8M trainable parameters instead of 2147.5M.

Move the controls and watch what changes.

Quick checkExercise 2 of 4

At hidden size 4,096, what happens to the share you train when you double the rank from 16 to 32?

Two more settings shape a LoRA run. Alpha scales how strongly the adapter's update is applied (a common default is alpha equal to r, or twice r). Target modules decide which matrices get adapters: the attention projections, the MLP layers, or every linear layer (all-linear in PEFT). Adapting more matrices adds capacity and memory; the explorer's per-layer slider shows how fast it adds up.

QLoRA: fine-tuning on one GPU

QLoRA pushes the memory floor down further: it quantizes the frozen base model to 4-bit precision (a format called NF4) and trains the LoRA adapters in higher precision on top. The original QLoRA paper (2023) fine-tuned a 65-billion-parameter model on a single 48 GB GPU with quality close to 16-bit fine-tuning on its benchmarks. In practice, models in the 7B to 13B range become trainable on one consumer GPU, which is why QLoRA is the default way most people fine-tune open-weight models.

The knobs that matter, in order

  1. Data quality. A few hundred to a few thousand clean, deduplicated, correctly formatted examples beat ten times as many scraped ones. Hold out 10% for evaluation.
  2. Learning rate. For LoRA, starting points around 1e-4 to 2e-4 are common. Too high and the model forgets what it knew; too low and nothing changes.
  3. Epochs. One to three passes. More usually memorizes the training set.
  4. Rank and alpha. r between 8 and 64 covers most tasks; raise it only if the evals show the adapter can't fit the task.
  5. Sequence length and target modules. Long enough for your real inputs; adapters on all linear layers if memory allows.
Match them upExercise 3 of 4

Match each knob to what it controls.

Pick one on the left, then its partner on the right.

Knob
Controls
0 of 5 matched
Spot the problemsExercise 4 of 4

A colleague's first training run looks great to them. Find the three lines that say it probably isn't.

train.log0 of 3 found

The tooling, as of November 2026

Hugging Face's PEFT (0.21) implements LoRA and its variants, and TRL (1.17) provides trainers for SFT, DPO and GRPO. Unsloth (release 2026.11) wraps the same ideas with memory and speed optimizations, free notebooks, GGUF export and the no-code Unsloth Studio. On Apple silicon, mlx_lm.lora trains LoRA or QLoRA adapters, or full fine-tunes with --fine-tune-type full. A minimal TRL run:

train.py: LoRA SFT with TRL and PEFT
from datasets import load_dataset
from peft import LoraConfig
from trl import SFTConfig, SFTTrainer

data = load_dataset("json", data_files={"train": "train.jsonl", "eval": "eval.jsonl"})

trainer = SFTTrainer(
    model="google/gemma-4-E4B-it",
    train_dataset=data["train"],
    eval_dataset=data["eval"],
    peft_config=LoraConfig(r=16, lora_alpha=32, target_modules="all-linear", task_type="CAUSAL_LM"),
    args=SFTConfig(output_dir="acme-triage-lora", num_train_epochs=2, learning_rate=2e-4,
                   eval_strategy="epoch", logging_steps=10),
)
trainer.train()
trainer.save_model()          # writes the adapter, not a full copy of the model
Check your understanding
Q1. What does LoRA actually train?
Q2. What does QLoRA add on top of LoRA?
· Work through the 4 remaining exercises above (stuck? each one can show you the answer).
· Score 100% on the quiz.
Vocabulary in this lesson

Plain-English definitions from the AI Coding Dictionary.