SarvamStack

    Week 6–7

    Fine-tuning

    Adapt a base model to your domain. QLoRA makes it possible on consumer GPUs.

    Pause & reflect

    Before you read on, answer this in your head — or on paper:

    You've used prompting and RAG. They work for a lot of cases.

    When might they fail? What kind of behaviour or knowledge is hard to "program" with prompts? What would you need to "bake in" to the model itself?

    Take about 40 seconds. No skipping — thinking first makes the next part stick.

    Use fine-tuning when prompting + RAG can't reliably achieve what you need: consistent output format, domain jargon, specific tone, or behaviours that few-shot examples don't capture. Fine-tuning writes knowledge into the weights. Production systems often combine fine-tuning with RAG.

    Before you continue: Full fine-tuning a 7B model needs ~112 GB VRAM. Why? What are you storing in memory during training?Pause 30s. Then read on.

    Memory at scale

    Full fine-tuning needs ~16 bytes per parameter (weights + gradients + optimizer states). A 7B model ≈ 112 GB VRAM; 70B ≈ 1.1 TB. QLoRA keeps the base in 4-bit and trains only LoRA adapters — a 7B model fits on a single 24 GB GPU. Techniques like activation checkpointing and 8-bit Adam reduce memory further.

    Catastrophic forgetting

    Full fine-tuning directly changes pre-trained weights. If training goes wrong, the model can forget prior knowledge — a phenomenon called catastrophic forgetting. LoRA/QLoRA train separate adapters; the base stays frozen, so you preserve existing skills while adding new ones. You can also remove adapters to revert.

    QLoRA in a nutshell

    Quantized Low-Rank Adaptation

    Keep the base model quantized (4-bit). Train only small LoRA adapters — low-rank matrices that sit beside the frozen weights. Saves 70%+ VRAM. A 7B model fine-tunes on a single 24 GB GPU.

    The workflow

    1. Prepare data: instruction–response pairs (or chat format).
    2. Load base model in 4-bit, attach LoRA adapters.
    3. Train on your data (often via Colab for GPU access).
    4. Merge adapters into base (optional) or serve adapters separately.

    Cost and time

    Week 7 of the course includes an optional ~$10 run for a fuller dataset. A cheaper dataset keeps spend minimal. Training a 7B on Colab: roughly 1–3 hours for a few thousand examples.

    DPO: preference alignment

    Beyond instruction fine-tuning, Direct Preference Optimization (DPO) aligns models with human preferences. You use triples: instruction + chosen response + rejected response. The model learns to prefer the chosen output. Fewer samples than instruction tuning, but captures subjective quality — helpfulness, style, safety. Use when you need "better" not just "correct."

    Test your understanding

    Don't reveal yet. Think for ~30 seconds first — what would you try?

    You need the model to output JSON with exact keys {name, score, reason}. Prompting sometimes gives extra keys or wrong format. RAG doesn't apply (no external docs). What should you do?

    Fine-tune vs RAG: quick comparison

    Fine-tune

    • Stable domain, custom behaviour
    • Needs training data
    • Update = retrain
    • Knowledge in weights

    RAG

    • Changing data, fast iteration
    • No training
    • Update = re-index
    • Knowledge in retrieval

    Data format

    Fine-tuning needs instruction–response pairs. Chat format: {messages: [{role, content}]}. Or alpaca-style: instruction + output. Use distilabel or similar to generate synthetic data with an LLM if you have few examples.

    Check your understanding

    ?

    What does QLoRA stand for and why is it useful?

    ?

    When is fine-tuning worth it over prompting + RAG?

    ?

    What are LoRA adapters?