- 1. When to Change the Model
- Three levers, and what each one moves
- What adaptation actually does
- The measurement this book insists on
- The setup used throughout this book
- 2. What a Straightforward Adaptation Costs
- The run
- Both abilities, every fifty steps
- What moved
- 3. Training Fewer Parameters
- The construction
- The first measurement
- The error in my own experiment
- What LoRA is actually for
- 4. The Fix That Changes the Trade
- Freezing part of the model
- Learning rate, measured
- The other family
- Putting the four together
- 5. Teaching a Behaviour
- What instruction tuning is
- The choice nobody notices making
- The measurement
- Data, and why quantity is the wrong question
- 6. Teaching a Preference
- Comparisons instead of demonstrations
- Direct preference optimisation, built
- The measurement
- Where preference training goes wrong
- 7. The Dataset Is the Decision
- How much data
- What duplicates do
- What contradiction does
- 8. When One Adaptation Erases Another
- Two tasks, deliberately close
- The measurement
- Order, and the fix
- What an adapted model needs measured
- 9. When the Model Does Not Fit
- What actually consumes the memory
- Gradient accumulation
- Quantising the frozen base
- 10. Limits, and Where Each Exam Goes
- The limits, and what causes each
- What adaptation does reliably
- What this book did not establish
- The reverse map: your certification to these chapters
Adaptation and Fine-Tuning
What it costs to teach a model something new — built, broken and measured from scratch
Use LoRA because it forgets less. Keep the learning rate low. Mix in some general data. Stop
early. Four pieces of advice everyone repeats and almost nobody quantifies.
This book measures each one — and two turn out to be doing something other than what they are
credited with. LoRA's retention is governed by a scaling constant, not by its parameter
efficiency. Early stopping bounds the damage without separating it from the gain. Every
experiment reports two numbers: the new task, and the capability you were not trying to
change.
Minimum price
$19.00
$29.00
You pay
Author earns
About
About the Book
Fine-tuning is the option teams reach for when prompting is not enough, and the one they
understand least. The advice around it is unusually confident and unusually unquantified.
So adaptation is attempted, the model gets better at the new task, and whether it got worse
at everything else is a question nobody measures — because measuring it means deliberately
evaluating a capability you were not trying to improve.
That is the design decision behind this book: **every experiment reports two numbers.** The
domain the model was adapted to, and the general text it started from. The second is the one
production teams do not collect, and it is where the entire cost of adaptation appears.
Nothing here is described rather than built. Low-rank adaptation is implemented from scratch —
frozen base weights, a low-rank pair on every attention projection, initialised so the adapted
model is exactly the base model before training begins. Direct preference optimisation is
implemented from scratch, with a frozen reference model. Quantised training, gradient
accumulation, loss masking and multi-task interference are all measured on the same base
model, so the comparisons differ only in the variable under test.
What gets measured:
- **The trade happens immediately, not gradually.** Probed every fifty steps, the first 8% of
training captured essentially the whole domain gain — perplexity from 19,952,720 to 8.18 —
and inflicted most of the forgetting, taking general perplexity from 256 to 5,111. There is
no stopping point at which the domain is adapted and the general ability is intact.
- **LoRA's reputation does not survive measurement — and neither did my first conclusion.**
The initial sweep showed full fine-tuning beating LoRA on both axes. That sweep tied the
scaling constant to the rank, so two variables moved together. Holding rank fixed and
varying only the scale reversed it: at a low scale LoRA retained general ability *better*
than full fine-tuning, while training 1.68% of the parameters. Both sweeps are in the book,
because the mistake is more instructive than the result.
- **Mixing 12.5% general data into each batch** cost 0.6% of the domain gain and returned
general perplexity from 6,245 to 236 — better than the base model's own 256. No method that
merely restricts the update can end above where it started.
- **Preference training costs a different order of magnitude from demonstration.** Teaching by
comparison reached perfect preference accuracy for 2.8–9.1% of general perplexity, where
supervised adaptation cost 2,339%.
- **A second adaptation erases the first.** A model at accuracy 1.000 on one task scored 0.000
after fifty steps on another — erasure rather than decline, because a discrete learned
behaviour has no partial version. Interleaving the two gave 1.000 on both, and better
general text than either order.
- **Data volume is the wrong question.** Eight examples — one per fact — reached 1.000, and
thirty-two times as much taught nothing further. A quarter of the labels could contradict
the rest with no measurable loss; at half, the failure was per-fact rather than uniform,
which spot-checking misses.
- **Quantisation costs the base model, not the adaptation.** Four-bit base weights cost 8.3%
of what the model already knew and left the adapted result unchanged, because the adapter is
trained afterwards in full precision and fits itself to the quantised base.
What you get:
- 10 chapters covering the adapt-or-retrieve decision, full fine-tuning and forgetting,
parameter-efficient methods, mitigations, instruction tuning, preference training, dataset
design, multi-task interference and evaluation, memory-constrained training, and a final
chapter of limits.
- 54 original practice questions, tagged by certification and by difficulty, where every
option is explained — not just why the right answer is right, but why each wrong answer is
wrong, because on these exams the distractors are where the teaching is.
- A reverse map from each certification to the chapters that serve it, in reading order.
- All the code — the transformer, LoRA, DPO, the quantiser — so every table can be regenerated.
Written to the published objectives of NVIDIA NCP-GENL, NVIDIA NCA-GENL, AWS Certified
Generative AI Developer – Professional, Databricks Certified Generative AI Engineer, and
NVIDIA NCP Agentic AI. Objectives were checked in August 2026; confirm the current blueprint
with the certifying body before you sit.
You need to be able to read Python. Transformers, gradients, low-rank factorisation and
perplexity are built up from the point where they are needed, and nothing in this volume
assumes you have read any other book.
Every question is original, written from published exam objectives. Nothing is reproduced
from, or based on recollection of, any live examination.
Feedback
Author
About the Author
Hatem M. is a programmer and technical author whose work focuses on modern C++, large language models, and AI systems.
His books combine first-principles explanations with complete implementations and reproducible experiments. They include C++ Algorithmic Mastery, an eight-volume series on algorithms and problem solving; Build an LLM Inference Engine in C++, which constructs a GPT-style inference engine from scratch; LLM Quantization: From the Bits Up, which develops the theory and practice of neural network quantization from the bit level upward; and C++ Autopsy, a forensic investigation of ten subtle C++ bugs that compiled successfully, ran correctly, and still produced the wrong answers.
Contents
Table of Contents
The Leanpub 60 Day 100% Happiness Guarantee
Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.
See full terms...
Earn $8 on a $10 Purchase, and $16 on a $20 Purchase
We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.
(Yes, some authors have already earned much more than that on Leanpub.)
In fact, authors have earned over $15 million writing, publishing and selling on Leanpub.
Learn more about writing on Leanpub
Free Updates. DRM Free.
If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).
Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.
Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.
Learn more about Leanpub's ebook formats and where to read them
Write and Publish on Leanpub
You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!
Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.
Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.