4 · Transformers & LLMs

13. Inference, Decoding and Hallucination

Sampling parameters, KV caching, quantisation and why models state falsehoods confidently.

10 min read · 3 MCQs

Decoding strategies

Greedy decoding takes the argmax and is repetitive. Temperature flattens or sharpens the distribution; top-k and top-p (nucleus) restrict sampling to the plausible mass. Low temperature for extraction and code, higher for ideation.

Making inference cheap

The KV cache stores past keys and values so each new token costs one step instead of recomputing the prefix. Quantisation to int8 or 4-bit shrinks weights with modest quality loss; batching, paged attention and speculative decoding raise throughput further.

Hallucination

The model samples plausible continuations; it has no truth oracle and no memory of provenance. Grounding in retrieved documents, requiring citations, constraining output schemas and verifying with tools are the practical mitigations — not stern prompting.

Chapter quiz

3 questions · pass mark 75%
  1. 1. Top-p (nucleus) sampling selects from…

  2. 2. The KV cache exists to…

  3. 3. The most reliable mitigation for hallucination is…

Answer every question to submit. Progress for ai-13 is saved in this browser.