Interview's MateAI Engineering
Interview's Mate

AI Engineering

LLMs, RAG, agents & evals

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
AI Engineering topics
Lesson 16/ 160
01. LLM Fundamentals

Fine-tuning with LoRA

Low-rank adapters for efficient specialization without full weight updates.

AdvancedAdapters · Rank3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidHands-onQuiz (3)Practice

In plain words

Instead of retraining the whole giant model, you train a tiny add-on that nudges it toward your task.

Think of it like…

Putting a clip-on lens on a camera instead of rebuilding the camera: the body stays untouched, the add-on changes the output for one purpose, and you can keep a drawer of different lenses and swap them in seconds.

Where you’ve seen it

A legal-tech startup serves 40 law firms, each wanting its own drafting style. They keep one base model in GPU memory and a 120 MB LoRA adapter per firm; each request loads or selects the right adapter (multi-LoRA serving). Training an adapter on 3,000 example documents takes about two hours on one GPU, where full fine-tuning would have required a multi-GPU node and a separate 14 GB copy per firm.

Words you’ll hear

Adapter
The small trainable low-rank matrices added alongside frozen weights.
Rank (r)
Inner dimension of the adapter; higher means more capacity and more parameters.
Alpha (α)
Scaling factor for the adapter's contribution, applied as α/r.
Target modules
Which layers get adapters, commonly the attention projections (q, k, v, o) and sometimes the MLP.

Visual diagram

Loading interactive diagram

Theory & deep dive

The idea and the numbers

Research on fine-tuning found that the weight changes needed to adapt a model have low intrinsic rank, so the update ΔW = AB can be represented with far fewer parameters than the full matrix. For a 4,096 × 4,096 matrix (16.8 million parameters), a rank-16 adapter has 16 × (4,096 + 4,096) ≈ 131,000 parameters, under 1 percent. Applied to the attention projections of every layer, a 7B model gets roughly 10 to 40 million trainable parameters.

Because the base weights are frozen, you don't store gradients or optimiser state for them, which cuts training memory dramatically; the adapter is saved as a file of a few tens to hundreds of megabytes. A is initialised randomly and B at zero, so training starts exactly at the base model's behaviour. At inference you can keep the adapter separate (to swap tasks) or merge it into W, which adds no extra latency.

Typical configuration

LoraConfig(r=16, lora_alpha=32, target_modules=["q_proj","k_proj","v_proj","o_proj"], lora_dropout=0.05, task_type="CAUSAL_LM") with a learning rate around 1e-4 to 2e-4, which is higher than full fine-tuning because only the adapters learn.

Key takeaway: Low-rank updates: ~1% of the parameters, frozen base, mergeable or swappable adapters.

When to use LoRA, and what to watch

LoRA suits changing behaviour: style and tone, a fixed output format, domain terminology, classification or routing heads, or distilling a larger model's outputs on a task. It's a poor way to add large amounts of new factual knowledge; retrieval (RAG) is better for facts that change or must be cited. Try prompting and few-shot examples first, then LoRA when prompts become long or inconsistent, and full fine-tuning only when adapters can't reach the quality needed.

Quality depends mostly on data: a few thousand clean, diverse, correctly formatted examples beat tens of thousands of noisy ones. Tune rank, alpha, target modules and learning rate, hold out an evaluation set, and compare against the base model with the best prompt. Watch for overfitting (the adapter memorises training examples), catastrophic drift on tasks outside the fine-tuning data, and licence or terms-of-service limits on using model outputs as training data. Serving frameworks such as vLLM and LoRAX can host many adapters on one base model.

Decision order

Need current facts with citations: RAG. Need a consistent style or format: prompt, then LoRA. Need a whole new capability or domain at scale: continued pretraining or full fine-tuning.

Key takeaway: Behaviour and style: yes. Fresh facts: use retrieval. Data quality beats quantity.

How it works, step by step

Step 1 of 4
Data

1. Prepare a clean dataset

Collect a few thousand diverse input-output examples in the model's chat or instruction format; hold out an eval set.

3,000 train · 300 eval, consistent format
Key numbers at this step
Quality
over quantity

When to use it & trade-offs

What it is

LoRA (Low-Rank Adaptation) fine-tunes a model by freezing its original weights and training small adapter matrices instead. For a weight matrix W of size d × k, LoRA learns two thin matrices A (d × r) and B (r × k) with a small rank r (often 8 to 64), and uses W + (α/r)·AB at run time. Only A and B are trained, usually well under 1 percent of the parameters, so training needs far less GPU memory and the result is a small file you can swap or merge. LoRA is the best-known parameter-efficient fine-tuning (PEFT) method.

Why it matters: Full fine-tuning of a multi-billion-parameter model needs weights, gradients and optimiser state in memory, which is several times the model size. LoRA makes specialising a model, for a house style, a domain vocabulary, a fixed output format or a classification head, feasible on one GPU and cheap to store per customer or task. Knowing when LoRA is the right tool, and when prompting or retrieval is, is a common design question.

When to reach for it

  • A house writing style or fixed output format
  • Per-customer or per-task adapters on one base model
  • Distilling a stronger model's outputs for a narrow task

When to avoid or be careful

  • Using LoRA to teach rapidly changing facts
  • Training on a few hundred inconsistent examples
  • Skipping a comparison against a well-prompted base model

LoRA vs full fine-tuning vs prompting

LoRA adapters
Best for: Style, format and task specialisation on a budget

Strengths: Small, cheap to train, swappable, mergeable.

Watch out: Less capacity than full fine-tuning; not for fresh facts.

Full fine-tuning
Best for: Large behaviour changes with lots of data

Strengths: Maximum flexibility.

Watch out: Heavy compute and memory; a full model copy per variant.

Prompting / RAG
Best for: First attempt and knowledge that changes

Strengths: No training; instant updates.

Watch out: Longer prompts; less consistent behaviour.

Interview signals & numbers to know

When this topic comes up

  • “Fine-tune on a single GPU”
  • “We need a different style per customer”
  • “LoRA vs full fine-tuning”
  • PEFT
  • Multi-adapter serving

Benchmarks & rules of thumb

< 1% -
typical share of parameters trained
8 – 64 -
common ranks
≈ 131K -
params in a rank-16 adapter for a 4,096² matrix

Common mistakes & follow-ups

Common interview mistakes

  • Fine-tuning to inject product documentation, then finding answers go stale after the next release.
  • Using a huge rank on a tiny dataset and memorising the training set.
  • Comparing the tuned model with an un-prompted base model, which overstates the gain.

Likely interviewer follow-ups

  • How does LoRA reduce training memory?
  • When would you choose RAG over fine-tuning?
  • How can one server host dozens of customer-specific adapters?
Interview tip: Give the parameter arithmetic (rank-16 on a 4,096² matrix ≈ 131K params vs 16.8M). Concrete numbers make the "low-rank" idea memorable.
Loading hands-on exercise

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Fine-tuning with LoRA with generative AI interview Q&A and hands-on practice.

Open Generative AI Q&A →Tech Learn Q&A
Previous topic
Multimodal Inputs
Next topic
QLoRA