โ ๐ณ Decoding Algorithms
Advanced
Grammar-Constrained Decoding
Constrained decoding masks every token the grammar cannot accept before sampling, using an automaton compiled from a regex, JSON schema or context-free grammar. This page covers how the mask is computed fast enough to hide under a 5 ms decode step, why per-step masking does not sample from the model conditioned on the grammar, and how it interacts with speculation.
Get full Premium access ยท โน2,000 / $25
Every worked answer and derivation in all 12 topics, every concept page, and the full practice-test bank.
6 months ยท One payment ยท No auto-renewal
RELATED CONCEPTS
PRACTICE THIS IN REAL QUESTIONS
Speculative & Constrained DecodingDerive the expected speedup of speculative decoding, prove it does not change the output distribution, and tell me when it stops paying.โSpeculative & Constrained DecodingWhat do temperature, top-k and top-p each do to the next-token distribution, and in what order does a serving engine apply them?โImplement-from-Scratch CodingImplement batched top-p sampling with temperature. It must handle ties at the cutoff, p = 1, temperature 0 and masked tokens.โImplement-from-Scratch CodingImplement the lossless speculative sampling verifier for a batch with ragged draft lengths, and prove it matches the target.โKV Cache & GPU MemoryWith parallel sampling at n=8, a 2K-token prompt and 500 output tokens each, how many KV blocks does copy-on-write save?โSpeculative & Constrained DecodingHow does grammar-constrained decoding build the allowed-token mask at every step, and what does that cost per step at batch 128?โ