← All courses ← Course Lesson 17 / 18
Part V · Using and extending models

17. Making models cheap: quantization, LoRA, MoE, speculative decoding

The techniques that determine whether a model costs $10 or $10,000 per million tokens to serve — and how a 7B model ends up running on a laptop.

By the end of this lesson you can

  • Explain quantization, the formats in use, and what it costs in quality
  • Explain LoRA's low-rank decomposition and compute its parameter savings
  • Explain mixture-of-experts and the distinction between total and active parameters
  • Explain speculative decoding and why it is mathematically lossless

Quantization

Weights are trained in 16-bit floats. They do not need to stay there. Quantization stores them in fewer bits, mapping the original range onto a coarse grid.

FormatBytes per parameter70B model weightsTypical quality
fp16 / bf162140 GBreference
int8170 GBessentially indistinguishable
int40.535 GBsmall but measurable loss

Why this helps so much: from Lesson 11, decode is memory-bandwidth-bound. Halving the bytes read per token roughly halves the time per token. You get both a memory saving and a speedup, which is unusual.

Two approaches. Post-training quantization (GPTQ, AWQ) converts an existing model using a small calibration set — minutes to hours, no retraining. Quantization-aware training simulates the rounding during training so the model adapts to it; better results, much more expensive.

The subtlety that makes 4-bit work at all is outliers. A small number of activation dimensions have magnitudes far larger than the rest, and naively quantizing them destroys quality. Modern methods handle those channels separately, or quantize in small blocks with per-block scales rather than one scale for a whole tensor.

Rounding to a grid

Set the bit width and watch a weight distribution snap to the available levels.

LoRA and parameter-efficient fine-tuning

Full fine-tuning of a 70B model means updating all 70B weights and storing optimizer state for each — hundreds of gigabytes of GPU memory. Low-Rank Adaptation avoids nearly all of it.

The observation: the change a fine-tune makes to a weight matrix is empirically low-rank. So freeze the original W and learn a product of two thin matrices:

W' = W + BA    where B is [d, r], A is [r, d], r ≪ d

For a 4096×4096 matrix, full fine-tuning trains 16.8M parameters. With rank 8, LoRA trains 2 × 4096 × 8 = 65,536 — about 0.4%. Memory drops accordingly, since optimizer state is only needed for the trainable parameters.

Practical consequences:

  • Adapters are tiny (megabytes), so you can keep dozens per base model and swap them per request.
  • BA can be merged into W after training, giving exactly zero added inference latency.
  • QLoRA combines a 4-bit frozen base with LoRA adapters trained in higher precision, which is what makes fine-tuning a 70B model on a single GPU possible.

The limit: LoRA adapts behaviour and style well. It is a poor tool for injecting large amounts of new factual knowledge — retrieval is the right instrument for that.

Low-rank decomposition

Set d and r to compare full fine-tuning against LoRA parameter counts.

Mixture of experts

In a dense model, every parameter participates in every token. MoE breaks that assumption.

Replace each feed-forward network with N parallel FFNs ("experts") plus a small router. For each token, the router picks the top k experts (commonly 2 of 8, or 8 of 256) and only those run. The result is weighted by the router's scores.

So a model can hold, say, 8× the parameters while doing roughly the same arithmetic per token. This is the total versus active parameters distinction: a model advertised as 47B total / 13B active has 47B parameters worth of capacity and 13B parameters worth of per-token compute.

The costs are real:

  • All experts must be in memory even though only a few run, so VRAM tracks total parameters, not active ones.
  • Routing must be load-balanced or a few experts get all the traffic; an auxiliary balancing loss is standard.
  • Training is less stable, and distributed serving involves substantial cross-device communication.

MoE is why some very capable models are surprisingly fast: you are paying inference cost for a much smaller model than the parameter count suggests.

Expert routing

Send tokens through a router and watch which experts activate, and the load balance across them.

Speculative decoding

Decode is sequential and bandwidth-bound: one token per full read of the weights, with the GPU's arithmetic units mostly idle. Speculative decoding exploits that idle capacity.

  1. A small, fast draft model generates the next k tokens cheaply — say 5.
  2. The large model processes all 5 candidates in a single forward pass, in parallel, and computes what it would have predicted at each position.
  3. Accept the longest prefix where the draft matches what the large model would have sampled; reject the rest and continue from there.

The remarkable property, given the right acceptance rule, is that the output distribution is identical to running the large model alone. It is not an approximation — it is pure latency optimisation, typically 2–3× on easy text where the draft agrees often.

It works because verifying 5 tokens costs nearly the same as generating 1: both require reading all the weights once, and the extra arithmetic is free capacity. Variants include Medusa (extra prediction heads instead of a separate draft model) and n-gram lookup drafting for repetitive text.

Lesson in one breath

Quantization stores weights in fewer bits, cutting memory and speeding up bandwidth-bound decode. LoRA trains small low-rank update matrices instead of full weights. MoE routes each token to a few experts, decoupling capacity from per-token cost. Speculative decoding drafts several tokens cheaply and verifies them in one pass.

Practice

Answers are checked in your browser and saved to this device. Get one wrong and you can retry as many times as you like.

Exercise 1compute it

A 13-billion-parameter model is quantized from fp16 to int4. How many gigabytes do the weights occupy afterwards? (Use 1 GB = 10⁹ bytes; one decimal place.)

Exercise 2compute it

A weight matrix is 4096 × 4096. Using LoRA with rank 16, how many parameters are trained for it? Give the answer in thousands.

Exercise 3one answer

A mixture-of-experts model is described as 47B total parameters, 13B active. What does 'active' mean?

Exercise 4one answer

Why does speculative decoding not change the model's output distribution?

Exercise 5select all that apply

Which statements are true? Select all.

Exercise 6type the term

What is the name of the small component in a mixture-of-experts layer that decides which experts each token goes to?

Done with this lesson?

A lesson counts as complete once it is marked read and every exercise is solved.

Tip: press and to move between lessons.