Chapter 14: Fine-tuning & Adaptation Techniques¶
Teach pre-trained models new behaviors with your data—when to fine-tune, instruction datasets, supervised fine-tuning loops, parameter-efficient methods (LoRA, QLoRA, adapters, IA3), preference data (DPO), and rigorous evaluation.
Metadata¶
| Field | Value |
|---|---|
| Track | Practitioner |
| Time | 8 hours |
| Prerequisites | Chapters 1–13 (especially Chapter 11: LLMs and Chapter 13: RAG) |
Learning Objectives¶
- Decide when to fine-tune vs prompt vs RAG, with cost / latency / quality trade-offs
- Prepare instruction datasets (formatting, splits, token budgets, response masking)
- Run a supervised fine-tuning (SFT) loop with masked loss and early stopping
- Implement LoRA from scratch and apply PEFT methods (QLoRA, adapters, prefix tuning, IA3)
- Use preference data: RLHF and DPO concepts with a NumPy DPO loss
- Evaluate adapted models rigorously and plan deployment via a model registry
What's Included¶
Notebooks¶
| Notebook | Description |
|---|---|
01_fine_tuning_basics.ipynb | Decision tree, dataset prep, SFT loop, evaluation basics |
02_peft_lora.ipynb | LoRA math and NumPy implementation, QLoRA, adapters, prefix, IA3, merging |
03_advanced_adaptation.ipynb | Instruction tuning, RLHF/DPO, eval, forgetting, registry, capstone |
Scripts¶
config.py— Chapter config, dataset paths, optional-framework flagsdataset_utils.py— Instruction formatting, splits, tokenization budgets, response maskingtraining_utils.py— Tiny SFT loop helpers, loss masking, schedules, early stoppingpeft_utils.py— NumPy LoRA adapter (rank, alpha, scaling), merge / serve helpers
Exercises¶
- Problem Set 1 (notebook) — Format an instruction dataset, token budgets, loss masking, choose hyperparameters, FT vs RAG, tiny SFT loop
- Problem Set 2 (notebook) — Implement LoRA forward, parameter-efficiency ratios, merge adapters, DPO loss in NumPy, win-rate eval, registry entry
- Solutions — In
exercises/solutions/(notebooks andsolutions.pyfor CI)
Diagrams (Mermaid)¶
fine_tuning_spectrum.mermaid,lora_architecture.mermaid,training_pipeline.mermaid
Read Online¶
- 14.1 Introduction — When to fine-tune, dataset prep, SFT loop, evaluation basics
- 14.2 Intermediate — LoRA math + NumPy implementation, QLoRA, adapters, IA3, merging
- 14.3 Advanced — Instruction tuning, DPO, evaluation, forgetting, registry, capstone
Or try the code in the Playground.
How to Use This Chapter¶
Quick Start
Follow these steps to get coding in minutes.
1. Clone and install dependencies
git clone https://github.com/luigipascal/berta-chapters.git
cd berta-chapters
pip install -r requirements.txt
2. Navigate to the chapter
3. (Optional) Install the heavy framework extras (GPU helpful)
4. Launch Jupyter
GitHub Folder
All chapter materials live in: chapters/chapter-14-fine-tuning-and-adaptation/
Created by Luigi Pascal Rondanini | Generated by Berta AI