Lecțiile acestui traseu sunt publicate deocamdată doar în engleză, așa că lecția de mai jos apare în engleză. Restul — module, quiz și materiale — funcționează la fel.
Speculative Decoding and Distillation
Vei putea să:
You can explain how a small draft model lets a large model emit several tokens per forward pass without changing its output distribution, estimate the speed-up from acceptance rate and draft cost, and say when distilling a smaller student is the better investment than speeding up the teacher.
Getting more tokens per weight read
Module 1 said decode is bound by streaming the weights once per token. Speculative decoding attacks that constraint head-on: if a forward pass of the big model could verify several candidate tokens at once instead of producing one, each expensive weight read would yield several tokens. Distillation attacks the problem from the other side: make the model that serves most of the traffic smaller so every weight read is cheaper. Both are mainstream now; knowing when each applies is the skill.
Speculative decoding: draft, verify, accept
The scheme described in "Fast Inference from Transformers via Speculative Decoding" by Leviathan, Kalman and Matias (and independently by Chen and colleagues at DeepMind) works like this:
- A cheap draft model generates k tokens autoregressively — fast, because it is small.
- The target model runs one forward pass over the prompt plus all k drafted tokens. Because this is prefill-like (many tokens at once), it costs about the same as producing a single token.
- Walk through the draft tokens left to right. Each is accepted with probability min(1, p_target / p_draft) — the ratio of what the big model would have assigned to what the small one did. At the first rejection, sample a corrected token from an adjusted distribution and discard the rest.
- If all k were accepted, the target pass also gives one bonus token for free.
The crucial property is that the accepted sequence has exactly the target model's output distribution. Speculation changes speed, never quality, which is why it is safe to switch on without re-evaluating the model. The Hugging Face "Assisted Generation" post shows the same mechanism implemented in Transformers, where you can try it with any pair of models that share a tokenizer (since Transformers 4.46, Universal Assisted Generation also pairs models with different tokenizers).
Where the speed-up comes from, and where it vanishes
Expected tokens per target pass depend on the acceptance rate a and the draft length k: with independent acceptance it is roughly (1 − a^(k+1)) / (1 − a). Two things eat into that:
- Draft cost. The draft model's k passes are not free. If the drafter is a tenth of the target's cost, drafting 8 tokens costs almost a full target pass.
- Batch interaction. Speculation helps when the target is bandwidth-bound and has idle compute — single-user or low-concurrency serving. At large batch sizes the target is already compute-bound, the "free" verification is no longer free, and rejected tokens are wasted work. Several deployments see speculation slow down a saturated server.
A worked example: target cost 1 per pass, draft cost 0.1 per token, k = 4, a = 0.8. Expected accepted tokens ≈ (1 − 0.8⁵)/(1 − 0.8) ≈ 3.36 per round. Cost per round ≈ 1 + 0.4 = 1.4. Tokens per unit cost ≈ 2.4, versus 1.0 without speculation — a 2.4× gain. Drop the acceptance rate to 0.4 (a drafter that is a poor match, or creative open-ended text) and the gain shrinks to about 1.65/1.4 ≈ 1.2×, barely worth the complexity. Acceptance is highest on predictable text: code, structured output, retrieval-heavy answers that quote their sources.
Variants reduce draft cost further. Medusa and EAGLE attach small extra heads to the target model so no separate drafter is needed; n-gram and prompt-lookup methods draft by copying text that already appears in the context, which works surprisingly well for editing and summarisation tasks.
Distillation: train a smaller model to imitate a larger one
Hinton, Vinyals and Dean's "Distilling the Knowledge in a Neural Network" framed the idea: a student is trained not on hard labels but on the teacher's full output distribution, softened with a temperature, so it learns which wrong answers the teacher considers almost right. For language models the modern versions range from logit-matching on the same text to sequence-level distillation, where the teacher simply generates a large synthetic dataset and the student is fine-tuned on it. Many of the strongest small open models were produced this way, and the technique is also how teams get a domain-specific 1B model that matches a general 70B model on their narrow task.
The trade-off is scope. A distilled student is as good as the teacher on the distribution it was distilled on. Off-distribution it degrades faster than the teacher, and nothing in the serving system will warn you. Distillation also needs an evaluation set and training infrastructure, whereas speculation is a serving-time switch.
Choosing between them
- Latency problem for a model you must keep: speculative decoding.
- Cost problem on a narrow, well-defined task with stable traffic: distil a student, then serve it (possibly with speculation on top).
- Throughput problem on a saturated server: neither; look at batching, quantization and hardware first.
What is still unsettled
How to keep draft models aligned as target models are updated, how speculation interacts with batching schedulers, and whether self-speculative methods (early-exit layers, extra heads) beat separate drafters in production are all active areas. Reported speed-ups in papers are typically single-request numbers; treat them as ceilings.
What to remember
- Speculative decoding verifies k draft tokens in one target pass and preserves the target's output distribution exactly; it changes speed, not quality.
- Gain ≈ expected accepted tokens ÷ (1 + draft cost); it collapses when acceptance is low or the server is already compute-bound.
- Distillation trains a cheaper student from the teacher's outputs or distribution; it wins on narrow, stable tasks and quietly degrades off-distribution.
- Match the tool to the bottleneck: latency → speculation, unit cost on a narrow task → distillation, saturated throughput → neither.
Materiale selectate
1.6hMunca de profunzime se întâmplă aici — lecția este briefingul tău.
În direct pe aiqbench
Subiectul acestei lecții în restul site-ului, astăzi.
În știri, ultimele 30 de zile
- GPU decoder keeps pace with a superconducting qubit clock on a continuous stream, with NVIDIA NVQLink interconnectAlice & Bob ·
- DeepSeek Opens Source Production-Level Ascend Inference Code36Kr ·
- NVIDIA and Cerebras Partner to Accelerate AI Agent Inference with Two-Stage Compute Architecture36Kr ·
Mergi mai departe
- Care model merită?Preț, viteză și hardware alături de capabilitate.
- Întreabă AI despre această lecțieDeschide Întreabă AI cu o întrebare despre această lecție, cu răspuns din arhiva de știri, cu surse.
- Skill-uri de agent pentru această muncăSkill-uri gata făcute pentru agenți AI de programare, verificate și evaluate.
Testează-ți cunoștințele
5 întrebări în 100 de secunde, pe o pagină separată. Citește mai întâi tot ce este mai sus.
Testează-ți cunoștințele →Misiunea modulului
30–40 minBuild a tiny speculative-decoding simulator in a spreadsheet or 30 lines of Python: assume the target model costs 1 unit per forward pass, the draft model costs 0.1 unit per token, the draft proposes k tokens, and each proposed token is accepted independently with probability a. For k in {2, 4, 8} and a in {0.2, 0.5, 0.7, 0.9}, compute expected tokens produced per unit of cost relative to plain decoding. Highlight the combinations that are slower than not speculating at all and explain why in two sentences.
Se încarcă lucrarea ta…