Leanpub Header

Skip to main content

Optimisation and Acceleration

Where inference time actually goes — and what each remedy really costs

Optimisation and Acceleration
This book is 100% completeLast updated on 2026-08-22

Quantise the weights. Batch the requests. Add a draft model. Prune what you can. Shorten the

cache. A long list of techniques and a short list of reasons.

This book measures the reason first: the machine streams memory at 22.54 GB/s and computes at

116.58 GFLOP/s, so anything performing fewer than 5.17 operations per byte is waiting. Decoding

performs 0.22. Every technique in the book is then measured against that one number — and

several turn out to be addressing the wrong side of it.

Minimum price

$16.00

$16.00

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
About

About

About the Book

Inference optimisation is the part of this field where the techniques are most numerous and the

reasoning behind them least examined. Each one is real, and they are usually applied in the

order someone heard of them — producing a serving system with several optimisations, no

instrumentation, and no way to say which one is helping.

This book measures the underlying ratio first and then measures every technique against it. The

reference machine was clocked at 22.54 GB/s of memory bandwidth and 116.58 GFLOP/s of

arithmetic, giving a ridge point of 5.17 operations per byte. Generating one token performs

0.22 — twenty-three times below it. That single fact explains why decoding is slow, why

batching works, why the cache dominates long contexts, and why a second model can run almost

for free.

Several results are not what the technique's reputation suggests:

- **Unstructured pruning saves nothing.** Zeroing 60% of the weights cost 5% of perplexity and

not one byte of memory, because a dense matrix full of zeros is the same size as one full of

numbers. A sparse format costs positions as well as values and does not pay until roughly two

thirds sparsity.

- **Structured pruning destroyed the model — and that is not the verdict.** Perplexity went from

146.02 to 2569.46, and fine-tuning afterwards brought a model with a *quarter* of its

feed-forward channels back to 162.68, within 11% of the original. Pruning is

prune-then-recover, and the intermediate number should never be evaluated.

- **Batch 2 is slower than batch 1.** A real dispatch discontinuity, kept and explained rather

than smoothed away — the throughput curve is not smooth, and a batch size chosen by reasoning

rather than measurement can land in a hole.

- **An unrelated draft model accepts nothing.** Speculative decoding was verified as producing

output *identical* to greedy decoding, and then measured against a control: an ordinary model

of similar size accepted 0.000 of its proposals at every draft length. "A small fast model" is

an incomplete description of what a draft must be.

- **Tensor parallelism exchanges almost no data** — between 0.002% and 0.036% of the weight

bytes each device reads — and is still confined to one machine, because the all-reduce is a

synchronisation point hit once per block per token.

- **The largest saving is work not done.** A request sharing a 1,024-token prompt prefix with an

earlier one was 72.6x faster, superlinearly, because prefill attention is quadratic.

- **Memory and compute can be pulled apart.** A mixture-of-experts model with 128 experts and

one active per token holds 70.2x the parameters of its dense baseline and performs exactly the

same arithmetic — so "smaller model" and "less work per token" stop being the same statement.

And the ones that confirm their reputation come with the numbers: 8-bit quantisation costs 0.0%

of perplexity at 4x compression; granularity governs the cliff below that, cutting 4-bit damage

from 8.6% to 3.2%; batch 32 gives 3.9x the throughput for 8.2x the per-step latency; and at

131,072 tokens the KV cache is twenty-three times the size of the model.

**On quantisation, and a companion book.** Quantisation has an entire book of its own —

*LLM Quantization: From the Bits Up*, by the same author, 104 pages building the subject from

the representation of a single number upward. Chapter 2 of this volume covers quantisation to

the full depth these certifications require, measured, and refers the derivations, the named

algorithms and bit-level packing onward for readers who want them. You do not need that book to

pass the exams or to use this one; Chapter 2 stands alone.

What you get:

- 12 chapters covering where inference time goes, quantisation, pruning, distillation, the KV

cache and its remedies, speculative decoding, batching and continuous batching, throughput

against latency, multi-device parallelism, prefix caching and chunked prefill,

mixture-of-experts, and a final chapter of limits.

- 66 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 and measurement scripts, so every table can be regenerated.

This volume covers the two heaviest domains on the NVIDIA NCP-GENL blueprint — Model

Optimization and GPU Acceleration — which together carry 31% of that exam.

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. Attention, the KV cache and the arithmetic of memory traffic

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.

Bundles

Bundles that include this book

Author

About the Author

Hatem M.

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

Contents
  • 1. Where the Time Actually Goes
    • Two quantities, measured on the machine
    • Prefill and decode have opposite intensities
    • Confirming it on the clock
    • What this predicts about every later chapter
  • 2. Making Each Weight Smaller
    • What quantisation is
    • The measurement
    • Why this is a speed-up and not only a saving
    • Weights are the easy case
  • 3. Removing Weights
    • Two kinds of removal
    • The measurement
    • Pruning is two operations, not one
    • Choosing what to remove
  • 4. Training a Smaller Model to Imitate
    • What a distribution carries that a label does not
    • The measurement
    • The parameter that decides the balance
    • Where distillation sits among the alternatives
  • 5. The Cache That Grows
    • Why the cache exists and what it costs
    • It is time, not only space
    • Sharing keys and values across heads
    • Keeping less of the past
    • Fewer bytes per entry
  • 6. Using the Idle Arithmetic
    • The idea
    • It changes nothing about the output
    • Everything depends on the draft being related
    • Choosing the draft length
  • 7. Sharing One Weight Read
    • Why it works at all
    • What it costs
    • The two numbers cannot both be optimised
    • Sequences do not finish together
  • 8. Choosing Which Number to Optimise
    • Three numbers, not one
    • The trade, from the measurements
    • The two workloads
    • What to measure, and how to report it
  • 9. When One Device Is Not Enough
    • What does not fit
    • Two ways to split
    • The exchange is small
    • Choosing a strategy
  • 10. Not Doing the Work Twice
    • Requests share more than they seem to
    • A long prefill blocks everything
    • The score matrix nobody needs to store
  • 11. When Memory Stops Tracking Compute
    • The construction
    • The divergence, computed
    • What this does to the rest of the volume
  • 12. Limits, and Where Each Exam Goes
    • The limits, and what causes each
    • What inference optimisation does reliably
    • What this book did not establish
    • The reverse map: your certification to these chapters

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.

Learn more about writing on Leanpub