AI-ENGINEERING
    advanced

    Fine-Tuning & LoRA

    AI Engineer path · lesson 98 of 124
    The idea
    Full fine-tuning is repainting a house — every surface, massive cost. LoRA is installing removable wallpaper in the key rooms: a thin low-rank diff stored separately, snapped on top of the frozen original. Same look, a fraction of the cost, and you can swap rooms (tasks) without repainting.
    How to use
    Move the LoRA rank slider to see how parameter count and the fraction of full fine-tuning cost change with rank.

    Key insight

    Fine-tuning adapts a pretrained model to your task. The challenge: updating all parameters of a 70B model costs millions of dollars and weeks of GPU time. Parameter-efficient methods like LoRA solve this by updating only a tiny fraction of weights.

    100%
    Full FT trainable params
    < 1%
    LoRA trainable params
    comparable or better
    GPT-3 LoRA vs full FT
    The third figure is the one worth sourcing, because “LoRA is nearly as good” is usually asserted without one. Hu et al. (2021): for GPT-3, LoRA achieves comparable or better performance than full finetuning on several tasks using only ~4.7M trainable parameters — 0.0027% of full finetuning. The earlier adapter work points the same way: Houlsby et al. (2019) came within 0.4% of full finetuning on GLUE using 3% of the trainable parameters, though their extra layers add steps to the forward pass and therefore inference latency — which is exactly what LoRA avoids by merging back into the original weights.
    The PEFT ladder
    🟢
    Step 1: Prompt engineering: Zero labeled data required. Try this first.
    🟡
    Step 2: Few-shot / RAG: Dynamic context injection. Great for knowledge-heavy tasks.
    🟠
    Step 3: LoRA / QLoRA fine-tuning: 50–100 examples to find out whether it helps; a few hundred to a few thousand in practice. Style, format, domain-specific tasks.
    🔴
    Step 4: Full fine-tuning: 10k+ examples. Task very distant from pretraining distribution.
    Key insight
    Fine-tuning is expensive. The decision to fine-tune should come AFTER prompting and RAG have been exhausted. At FAANG, models are fine-tuned when inference-time techniques saturate at ~90% of target quality.

    The LoRA decomposition

    Schematic

    Complete the update applied to a frozen weight matrix.

    Tap a piece, then tap where it goes — or drag it. Tap the tray to take one back.

    never updated

    +

    d × r
    r × k
    3 pieces left to place

    Test your knowledge

    20 questions
    Question 1 of 20
    Score: 0

    LoRA stands for…

    The short version

    Full fine-tuning is repainting a house — every surface, massive cost. LoRA is installing removable wallpaper in the key rooms: a thin low-rank "diff" stored separately, snapped on top of the frozen original. Same look, a fraction of the cost, and you can swap rooms (tasks) without repainting.

    In more depth

    Fine-Tuning & LoRA

    Fine-tuning adapts a pretrained model to a target task by updating its weights on task-specific data. But updating all parameters of a 70B model is prohibitively expensive.

    LoRA: Low-Rank Adaptation

    LoRA freezes the original weight matrix W ∈ ℝ^{d×k} and injects a trainable low-rank update:

    W' = W + ΔW = W + BA

    where B ∈ ℝ^{d×r} and A ∈ ℝ^{r×k}, with rank r ≪ min(d, k).

    Trainable params per layer: 2·r·d (vs. d² for full FT).

    For d=4096, r=8: 65,536 vs. 16,777,216 — 256× fewer.

    At inference, merge: W' = W + BA → zero latency overhead.

    QLoRA

    QLoRA (Dettmers et al. 2023) combines LoRA with 4-bit NormalFloat quantization of the base model, enabling 65B fine-tuning on a single 48GB GPU.

    When to fine-tune?

    Prompting first: if a well-engineered prompt solves the task, stop here.
    RAG: if the task requires fresh or private knowledge.
    Fine-tuning: when you have 50–100+ labeled examples (a few hundred to a few thousand for PEFT; tens of thousands to millions for full finetuning), a stable task, or need sub-300ms latency (no long few-shot context).

    Interview Questions

    Meta
    Google
    Anthropic
    Cohere
    OpenAI
    Databricks
    Scale AI
    Hugging Face
    Meta
    Microsoft
    Google
    Anthropic
    Cohere
    NVIDIA
    Together AI
    Modal

    Love this visualization?

    Sign up to track your progress, earn achievements, use the AI assistant, and access your personal learning dashboard.

    100% free • No credit card required