05. Capstone 4: Efficient Inference#
Level: Practitioner · Reading time: 20 min · Exercise: 25 h Prerequisites: Quantization, A Minimal Evaluation Harness
In one sentence: After this project you have taken one small open model, measured its latency, memory and quality, made it smaller with quantization, and reported the trade-off in a table that says what was measured on which machine.
Why this matters#
A model that scores well and does not fit on the machine is not a feature. Quantization opens with the arithmetic. A model with 7 billion parameters needs 14 GB for its weights in 16-bit, so it does not load on a laptop with 8 GB. In 4-bit the same weights need 3.5 GB.
After every compression the questions are the same. What did it cost in quality? Did it make anything faster? Answering them is an engineering job with a fixed shape: measure, compress, measure again, compare.
The goal#
Take one small open model, a file from a model hub such as Hugging Face. Measure its latency, memory and quality. Compress it, with quantization at minimum and pruning or distillation as optional stages. Then report the trade-off between size, speed and quality, in numbers that another engineer can check.
Two tracks#
| Track | Model | Hardware | What it teaches |
|---|---|---|---|
| A | A small open classifier, or another small model you can load | Any laptop | The whole procedure, end to end, with nothing hidden |
| B | A small open-weights language model | A laptop that can load it. A GPU only for training-based stages | Where the money is: quantizing the weights and the KV cache |
Do track A first. It takes 8 to 12 hours and works through the five deliverables in one pass; the milestones below are track B's. Then do track B. Both run on a laptop. Numbers measured on a CPU are real numbers. They are not GPU numbers, and the report says so.
What hardware is needed#
| Task | Laptop, CPU only | GPU |
|---|---|---|
| Measure latency, memory and quality of a small model | Yes | Needed only for GPU latency numbers |
| Post-training quantization of the weights | Yes | Not needed |
| Check the quality of the compressed model | Yes, more slowly | Faster |
| Quantization-aware training, pruning with fine-tuning, distillation | Only for a tiny model | Needed |
| Speedups that depend on special low-bit hardware | No | The matching hardware |
Start track B at a billion parameters or fewer. Weight memory is parameters times bytes. A billion parameters take about 2 GB in 16-bit, 1 GB in 8-bit, and 0.5 GB in 4-bit, before the cache and the overhead.
One pitfall from the quantization chapter decides what you can promise. A smaller file is not a faster model unless the inference engine, the program that loads the model and runs it, has a fast kernel for that format. Which engine supports which format changes quickly. As of 2026-11-24, check the documentation of the engine you pick. Inference names four engines. Efficient LLM Inference and Deployment covers the serving side.
Deliverables#
- A baseline table. Parameter count, size, latency, peak memory and quality, before any compression.
- A quality evaluation. A fixed task set, and for a language model a held-out text for perplexity.
- Variants. Float, 8-bit and 4-bit. Optionally a pruned or a distilled model.
- The final table. Size, latency, memory and quality for every variant, with the hardware named.
- A report of one page. The trade-off, what was not comparable, and what you recommend.
Milestones#
Milestone 1: measure the baseline (4 to 6 hours)#
Read Neural Network Basics for Efficiency for latency against throughput, FLOPs, parameter counting and memory, and Why Efficiency Matters for latency and energy budgets.
- Parameter count recorded. Weight memory computed as parameters times bytes, and checked against the file size. Any gap is explained.
- Latency measured after warm-up, at least 20 times, one input at a time. Reported as a median and a 95th percentile. For a language model: time to first token and tokens per second, at a fixed prompt length and output length.
- Peak memory during one inference, recorded.
- Hardware, engine and their versions, written down.
- Track B: the size of the cache of past tokens at your prompt length, estimated and set beside the weights. Inference shows how to bound a model's speed from memory bandwidth and that cache. Per token, the cache takes
2 × layers × heads × head size × bytes per value.
No chapter shows how to time a call. Read time.perf_counter() before and after one call, discard three warm-up calls, and report the median and the 95th percentile of the rest. For peak memory, start tracemalloc before the call and read its peak after it. A deep-learning framework allocates mostly outside it: use its own peak counter and name it.
Milestone 2: build the quality evaluation first (3 to 5 hours)#
Read A Minimal Evaluation Harness and Evaluating Language Models.
- A fixed set of at least 50 tasks with answers a program can check, or a labeled test set for a classifier. For a language model, fifty arithmetic questions in the harness's task format will do.
- Track B: a held-out text for perplexity, beside the task set.
- The baseline score, with a standard error.
- A written limit: the largest quality loss you accept.
The fourth pitfall of the quantization chapter is judging quality by one prompt or by perplexity alone. Its fix is to run an evaluation set against the float baseline.
Milestone 3: quantize (6 to 8 hours)#
Read Quantization, and the compression levers in Inference.
- Float, 8-bit and 4-bit variants exist as files you can load.
- The 4-bit variant uses per-channel or group scales. The report notes what changed from one scale per tensor.
- Sensitive layers stay in 16 bits: embeddings, the first and the last layer, the final normalization.
- Track B: only the weights are quantized. Activations stay in floating point, which is the quantization chapter's choice for language models on one machine.
- The size arithmetic is checked again for each variant.
Follow the chapter's procedure: fix a float baseline, compute the scales, run a few hundred representative inputs to record each activation's range, quantize, and evaluate on real tasks.
Milestone 4 (optional): prune or distill (6 to 10 hours)#
Read Pruning and Sparsity and Knowledge Distillation.
- One pruning pass, with the quality recorded after each step, and the report saying which kind of sparsity you produced.
- Or one distillation run, with the student evaluated on real labels and not on agreement with its teacher.
- The new rows added to the table.
Milestone 5: measure again and report (4 to 6 hours)#
Read Quality Attributes of ML Components.
- The final table: variant, size, latency as median and 95th percentile, peak memory, quality with its standard error, hardware.
- The dominated variants named, those worse on every attribute, and the choice made among the rest.
- For each speedup, or its absence, a stated reason.
- Every number measured on one machine, or the differences named.
You are done when#
- The baseline was measured before any compression.
- The quality evaluation existed before any compression, with a written limit.
- Three variants were measured: float, 8-bit and 4-bit.
- The size arithmetic matches the measured file sizes, or the gap is explained.
- Latency is reported as a median and a 95th percentile over at least 20 runs.
- The report names the hardware, the engine and the date for every number.
Effort, tools and cost#
Track A takes 8 to 12 hours. Track B takes 17 to 25 hours. You need Python 3.10 or newer, NumPy (the quantization chapter's snippet uses it), the library that loads your model, and for track B an inference engine that supports the quantized format.
The project is free on a laptop. A rented GPU costs money. You need one for the optional stage, for GPU latency numbers, and for any training-based compression. Building Your Own lists prices for one GPU type, with their date. Check current prices before you budget.
Stretch goals#
- Quantization-aware training, if post-training quantization lost more quality than your limit allows (Quantization).
- Mixed precision per layer. Cheap or memory-bound layers stay in 16 or 32 bits, and compute-bound layers go lower. The same chapter.
- Smoothing activation outliers into the weights, the idea the quantization chapter describes from a course project.
- A smaller cache of past tokens, through the attention variants and cache levers in Inference.
- Energy, measured beside latency (Why Efficiency Matters).
- A microcontroller, if the classifier of track A is small enough (TinyML: Microcontrollers and TinyEngine).
Pitfalls#
- Measuring once, or cold. Fix: warm-up, then at least 20 runs, median and 95th percentile.
- Numbers from different machines or engines. Fix: one machine, one engine, or say what differs.
- One prompt, or perplexity alone. Fix: a task set beside perplexity.
- Assuming smaller means faster. Fix: check the engine's kernel support for the format.
- One scale at 4 bits. Fix: per-channel scales, and group scales below 8 bits.
- Every layer at the same width. Fix: sensitive layers stay in 16 bits.
- Forgetting the cache of past tokens. With long inputs it can outweigh the weights.
- A different evaluation set for each variant. Fix: one set, fixed before the first compression.
- An average without a spread. Fix: the 95th percentile and the standard error.
- A number without its date and versions. Fix: hardware, engine and date in every table.
What the practitioners say#
- Quantization is the workhorse. Song Han calls it very widely used in industry, and 8-bit mature (Quantization).
- Post-training first. His order: try post-training quantization, and move to quantization-aware training only when the evaluation shows a real loss. The same chapter.
- Generation is limited by memory. The inference chapter of part 10 bounds a model's speed from memory bandwidth (Inference).
- Argue on the front. Kästner's method for quality attributes: cut the design space with hard constraints, and argue only among the designs left (Quality Attributes of ML Components).
Try it#
The project is the exercise. Start with one hour:
- Pick the smallest model you already use.
- Write down its parameter count, and compute its weight memory at 16 bits.
- Time one inference 20 times, after three warm-up runs. Write down the median.
You are done with the first hour when you have three numbers on paper: parameters, bytes and milliseconds.
Key takeaways#
- Measure before you compress: latency, memory and quality, on named hardware.
- Build the quality evaluation first, with a written limit on the loss.
- Try post-training quantization first. Go finer in granularity before you go to retraining.
- A smaller file is a faster model only if the engine has a fast kernel for the format.
- Report the trade-off as a table with a spread, a date and a machine.
- Both tracks run on a laptop. A GPU is for training-based stages and for GPU numbers.
Self-check#
- Why must the quality evaluation exist before the first compression?
- A 4-bit model loads in a quarter of the memory and runs at the same speed as the 16-bit model. What is the first explanation to check?
- Why report a task set beside perplexity?
- Which layers stay in 16 bits?
- What two latency numbers does a language model need that a classifier does not?
- Why measure latency at least 20 times after warm-up?
Show answers
1. Without a fixed evaluation and a written limit, there is no number to compare a variant against. The temptation is then to judge by one prompt. 2. Whether the inference engine has a fast kernel for that format. Without one, the compression saved memory only. 3. Perplexity is one average over held-out text. The quantization chapter counts it alone as a way of judging quality wrongly, and asks for an evaluation set on real tasks. 4. Embeddings, the first and the last layer, and the final normalization. They are the sensitive ones, so they stay in higher precision. 5. Time to first token, and tokens per second. Each is measured at a fixed prompt length and output length. 6. The first runs are cold and slower. A median and a 95th percentile over many runs describe a distribution. One run does not.Sources#
- Part 09: Why Efficiency Matters, Neural Network Basics for Efficiency, Pruning and Sparsity, Quantization, Knowledge Distillation, TinyML: Microcontrollers and TinyEngine, Efficient LLM Inference and Deployment
- Part 10: Inference, Evaluating Language Models, Building Your Own: A Roadmap Through the Assignments
- Part 03: A Minimal Evaluation Harness
- Part 07: Quality Attributes of ML Components
Next#
→ Capstone 5: Train a Small Language Model, for building the model instead of compressing it.