Runbooks/ LLM Inference Runbook Track D · Scale and speed Deep readQuick learn
  1. 00 Start
  2. /
  3. 01 Tokens
  4. 02 Anatomy
  5. 03 Journey
  6. /
  7. 04 Hardware
  8. 05 Phases
  9. 06 KV cache
  10. /
  11. 07 Attention
  12. 08 Position
  13. 09 Precision
  14. 10 Cache ops
  15. /
  16. 11 Many GPUs
  17. 12 Engines
  18. 13 Decode loop
  19. /
  20. 14 Planning
  21. 15 Production
Document 13 · Quick learn Masks · Penalties · Temperature · Truncation · Draw

One vectorof scores

Everything user-facing happens to one vector of scores — and the one technique that makes a single answer arrive faster works only in the regime where batching does not.

“A weighted raffle, redrawn every second. Temperature is how hard you shake the drum.”
2.80×
speculation at 80% acceptance
0.89×
at 20% — genuinely slower
−∞
how constrained decoding actually works
128,256 scores
recomputed from scratch every step
How a token is choseneverything user-facing happens to one vector

Stage 9 of the journey — the moment 128,256 scores become one integer. It is user-facing, it is cheap, and it is where three quite different capabilities live.

128,256 logits

One raw score per vocabulary entry, recomputed from scratch every step.

Masks

Grammar constraints and banned tokens set to −∞.

Penalties

Repetition, presence, frequency — adjust tokens already used.

Temperature

Divide every score by T. Flatten above 1, sharpen below.

Truncation

Softmax, then top-k or top-p.

Draw one

At T = 0 this collapses to taking the largest.

The order matters

A grammar mask applied after truncation could leave nothing legal to sample from. Masks first, then penalties, then temperature, then truncation, then the draw.

The analogy — a weighted raffle, redrawn every second

Every ticket in the vocabulary is in the drum, always — 128,256 of them — and the model’s job is to decide how many copies of each. Masks confiscate certain tickets entirely. Penalties reduce the copies of tickets already drawn. Temperature is how vigorously you shake the drum.

Top-p is removing the least-likely tickets before drawing — and removing however many it takes, which is why it adapts

Settings that actually get used

Extraction

T 0

Deterministic at the sampling layer
Code

T 0.2

Just enough variation to escape a loop
Chat

T 0.7, top-p 0.9

The common default
Creative

T 0.9–1.1

And do not set top-k and top-p together
Several tokens per tripthe one technique that makes a single answer arrive faster

Batching makes a server handle more people. It does nothing for how fast any one answer arrives. Speculative decoding is the technique that does — and it works by exploiting exactly the thing that makes decode slow.

Draft four

A model a twentieth of the size guesses four tokens, one at a time.

Four cheap passes ≈ a fifth of one target pass
Verify all four

The target checks them in a single forward pass.

That is prefill on four tokens — and prefill is cheap
Accept a prefix

From the start until the first disagreement. The rest is discarded.

The target supplies the correction, so you always move forward by at least one
Lossless

The acceptance rule preserves the exact output distribution.

2–3× reported on T5-XXL with identical outputs
Why checking four tokens costs almost the same as one

This is the part that is not obvious and the part interviewers want. Checking several tokens at once is exactly what prefill does, and prefill is cheap per token because the model reads its weights once and processes many positions with them.

So one loop costs one target pass — which you were going to pay anyway — plus four cheap draft passes. You paid 1.2 units and got up to four tokens
expected tokens = ( 1 − ak+1 ) ÷ ( 1 − a )  ·  cost = 1 + k/r
a = acceptance rate · k = tokens drafted · r = size ratio · speed-up is the ratio of the two
a = 0.8, k = 4, r = 20

3.36 tokens for 1.20 passes.

2.80× — the case it was designed for
a = 0.2, k = 4, r = 10

1.25 tokens for 1.40 passes.

0.89× — you have made the system 11% slower

When it does not help, and why

Small batch — spare compute is free

The GPU is stalled waiting on memory.

  • One user, latency matters: excellent — the designed case
  • Small batch, interactive chat: usually a clear win
  • Predictable output (code, JSON, edits): drafts very well
Speculation spends
spare compute
to save
memory trips.

Large batch — there is none to spend

Past the ridge point the GPU is already compute-saturated.

  • Very large batch, throughput matters: neutral or actively harmful
  • Verification is now real work competing with everyone’s tokens
  • High temperature, free prose: acceptance collapses
  • And the draft model eats KV cache, which costs concurrency
Four ways to get a draftcompare them on losslessness, not headline speed
A separate small model

A twentieth to a fiftieth of the target.

Lossless. Hosts a second model, which eats cache
Must share the target’s tokeniser
Medusa

Extra prediction heads on the target itself.

No second model. Greedy-only guarantee — the distribution is preserved only at temperature 0
EAGLE

Drafts from the target’s own internal features.

Lossless. 2.7–3.5× reported on a 70B
n-gram / prompt lookup

Copies repeated spans out of the prompt.

Free — no model at all. Only works when the output echoes the input: edits, summarise-this, code rewriting
The comparison axis that separates people

Compare them on whether the output distribution is provably unchanged — not on the headline speed-up. A method that is lossless only at temperature 0 is a different product from one that is lossless everywhere.

And the first filter on any separate drafter is the tokeniser, ahead of size: different vocabularies mean the draft’s token ids are meaningless to the target
Making invalid output impossibleshape, guaranteed. Truth, not.
The model emits {

Only a quotation mark or a closing brace is legal JSON here.

Mask everything else

Every other token’s score set to −∞ before sampling.

It literally cannot

Emit invalid JSON. A hard constraint on the logits, not a nudge.

From a state machine

Compiled once from your schema, so the per-token cost is tiny.

Four approaches, and only two are designs

Ask politely in the prompt

Guarantee: none. Cost: free.

When: never, if the output is parsed by code
Ask, then retry on failure

Guarantee: eventually, usually.

Cost: wasted generations, unbounded tail latency
When: a fallback, not a design
Constrained from a schema

Guarantee: the output parses, always.

Cost: a compiled state machine; tiny per token
When: any structured output consumed by code
Constrained from a grammar

Guarantee: the output matches the grammar.

Cost: same mechanism, more compilation
When: SQL, a DSL, anything with real syntax
It guarantees shape, not truth

The output parses; the values may still be wrong. And over-constraining can hurt quality by forcing the model down low-probability paths.

SGLang additionally compresses runs where only one continuation is legal — if the schema forces the next seven characters, there is nothing to sample and it emits them in one go
Answer it out loudthe ninety-second version, then five questions
“At the top of the model every vocabulary entry gets a score, and everything user-facing happens to that one vector: masks, penalties, temperature, truncation, then a draw. Temperature reshapes the distribution and top-p truncates its tail, which is why top-p beats top-k — the set adapts to how confident the model is. Speculative decoding gets several tokens per trip: a small model guesses four, the target verifies all four in one pass, which costs almost nothing because verification is prefill-shaped and decode is memory-bound. It is lossless and reported at 2–3 times — but it spends spare compute, so it pays at small batch and can be harmful at large. And constrained decoding masks illegal tokens to minus infinity from a compiled state machine, which guarantees shape, not truth.”

Five questions, and the spine of each answer

Architect

Explain speculative decoding in thirty seconds.

  • A small fast model guesses the next few tokens
  • The big model checks all of them in one pass — almost free, because verification is prefill-shaped and decode is memory-bound
  • Correct guesses kept, everything after the first wrong one discarded, and the target supplies the correction
  • So you always move forward by at least one
The output is identical to normal decoding — the acceptance rule is designed to preserve the distribution. 2–3× on T5-XXL with no retraining, which is why it shipped rather than staying a curiosity.
Architect

What if the draft model is wrong most of the time?

  • You still get one token per loop from the correction — you never go backwards
  • But you paid for the draft passes and gained almost nothing
  • A drafter with a poor acceptance rate is worse than no drafter
  • a=0.8 k=4 r=20 → 3.36/1.20 = 2.8×. a=0.2 k=4 r=10 → 1.25/1.40 = 0.89×
So the thing to measure before deploying is the acceptance rate on real traffic, not the speed-up in a paper.
Eng manager

Someone wants speculative decoding across the fleet.

  • What batch size do we actually run at? That decides whether this helps or hurts
  • It trades spare compute for fewer memory trips. Past the ridge point (~150 on an H100) there is no spare compute
  • On a busy fleet it can be neutral or actively harmful
Counter-proposal rather than rejection: scope it to the low-concurrency path — the interactive or premium tier. Then check the drafter shares the tokeniser and account for the KV cache it eats. And start with an n-gram drafter if the workload echoes its input, because that one is free.
Architect

What is the difference between temperature and top-p?

  • Temperature reshapes the whole distribution — divides every logit by T before the softmax
  • Top-p truncates — it reshapes nothing, it cuts off the tail
  • Top-p beats top-k because the set adapts to confidence: certain → keep one, unsure → open up
One practical note: setting both top-k and top-p is usually a mistake — they compose, whichever is stricter wins, and you get behaviour neither person intended.
Architect

How would you guarantee valid JSON output?

  • Constrained decoding. Compile the schema into a state machine
  • At each step mask out every token that would make the output invalid — logits to −∞ before sampling
  • Just opened a brace? Only a quotation mark or a closing brace survives the mask
  • Compiled once and reused, so the per-token cost is tiny
It guarantees shape, not truth: the output parses, the values may be wrong. And over-constraining can hurt quality by forcing low-probability paths.
Fast answersthe nine that come up most

QHow big should the draft model be?

Small enough that several draft passes cost far less than one target pass, and capable enough to agree often — typically a twentieth to a fiftieth. Both extremes fail. And the hard constraint comes first: it must share the target’s tokeniser.

QWhy must draft and target share a tokeniser?

They have to agree on what a token is before they can agree on what comes next. Different vocabularies mean the draft’s token ids are meaningless to the target. This is the first filter, ahead of size.

QCan I use it with a big batch?

You can, and the benefit shrinks and can turn negative. A large batch has already spent the spare compute. Measure it on your own traffic, and consider scoping it to a low-concurrency route.

QWhat is a realistic acceptance rate?

It depends entirely on content and temperature, which is why the honest answer is to measure it. Code, JSON and edits draft very well; free prose at high temperature drafts badly. Most engines expose it as a metric — watch it, because if it drifts down the feature quietly starts costing you.

QDoes temperature 0 make the model deterministic?

At the sampling layer, yes. End to end, not quite: floating-point reduction order can vary with batch composition and parallel configuration, which can occasionally flip a near-tie. Worth knowing before you promise bit-identical reproducibility.

QWhat do repetition penalties actually do?

Reduce the score of tokens that have already appeared, before temperature and truncation. Useful against loops and easy to over-apply — a strong penalty stops the model repeating words it legitimately needs, like a variable name. Turn it off entirely for structured output.

QDoes constrained decoding slow things down?

Barely. The state machine is compiled once and at each step yields a set of legal tokens that becomes a mask over a vector you were producing anyway. If you see real slowdown, the grammar is probably being recompiled per request rather than cached.

QCan I combine constrained and speculative decoding?

In principle yes, and it interacts: the draft has to respect the same constraints or its guesses will be rejected by the mask. Engines handle this to varying degrees. Test rather than assume — the failure mode is a quietly collapsed acceptance rate, not an error.

QWhat is the one thing to remember?

All three capabilities operate on the same vector of logits — and speculative decoding pays only when the GPU has spare compute, which is the opposite of the regime where batching pays. Knowing which operating point you are in is the whole decision.

Speculative decoding and batching are for opposite operating points. Batching serves more people and does nothing for one answer’s speed; speculation speeds up one answer by spending spare compute — which exists at small batch and does not past the ridge point. So “turn it on across the fleet” is the wrong shape of proposal, and the right one is to scope it to the low-concurrency tier and measure the acceptance rate on real traffic. At 80% acceptance it is 2.8×; at 20% it is 0.89× — eleven per cent slower.

Where this goes next

Questions to ask them

What sampling settings do you run, and are they per-route or global?

Is speculative decoding on? What acceptance rate do you actually observe?

What batch size do you run at — is there spare compute for speculation to use?

Do you use constrained decoding for structured output, or parse-and-retry?

Has anyone measured whether a tight schema changed output quality?

Is temperature 0 used anywhere you rely on reproducibility?

← 12 · Engines Read the full chapter → 14 · Planning →