Zero Labs
Industry News / AI

Why Your LLM Should Be Guessing: Breaking the Sequential Curse

Source · Towards AI By Kyouma45

From the publisher’s feed, collected in the ZeroLabs newsroom.

Paper-explained Series 3Imagine speeding up your LLM by 2X or even 3X instantly — without retraining a single parameter, without changing your architecture, and critically, without sacrificing a single drop of generation quality.It sounds like a “free lunch” that shouldn’t exist in deep learning, but it does. The bottleneck of modern AI isn’t always how smart your model is; it’s how fast it can “think.” If you are tired of watching tokens generate one… by… one, Speculative Decoding is the solution to your problem.Welcome to your one-shot guide to the algorithm that breaks the sequential speed limit of Large Language Models. Let’s dive in.The Bottleneck: Autoregressive InferenceStandard large models, such as Transformers, are autoregressive. This means they generate text one token at a time. To generate K tokens, you must run the model K times in a serial fashion. If a model takes 100 milliseconds to generate a token, a 20-token sentence takes a full 2 seconds.Engineers have tried various methods to speed-up:Distillation: Training a smaller “student” model to mimic a larger “teacher”.Pruning: Pruning less important connections in the neural network.Quantization: Reducing the precision of the model weights (e.g., from 16-bit to 4-bit).While effective, these methods often require expensive re-training or architecture changes, and critically, they often degrade the model’s output quality.Enter Speculative Decoding: Drafter and TargetSpeculative Decoding is different. It accelerates the existing model (the Target Model) without changing its weights or outputs. It relies on the observation that some tokens are “easier” to predict than others and don’t require the full power of a massive model.The Core ConceptThe system uses two models:The Drafter (M_q): A smaller, faster, and cheaper approximation model.The Target (M_p): The large, powerful, and slower model we actually want to use.Analogy: Imagine a Master Chef (Target) and a Sous-chef (Drafter). The Sous-chef quickly chops vegetables and pre-plates the food (drafts tokens). The Master Chef then reviews the plates. If the work is up to their standard, they approve it immediately (parallel verification). If the Sous-chef made a mistake, the Master Chef fixes it and takes over.Memory Bound vs. Compute BoundTo understand why this works, we must distinguish between two types of computational bottlenecks:Memory Bound: The speed is limited by how fast data (model weights) can be moved from memory to the processor.Compute Bound: The speed is limited by how fast the processor can perform arithmetic operations.Inference on large Transformers is typically memory bandwidth bound. The expensive part is loading the massive model weights into the chip, not doing the actual math.When the Target model generates one token, it reads all its weights. When it verifies 5 tokens in parallel, it still reads its weights only once. By checking the Drafter’s guesses in parallel, we convert a memory-bound operation into a compute-bound operation, utilizing arithmetic resources that were sitting idle.The Algorithm in Action: When the Drafter is WrongLet’s look at the mechanics. The Drafter predicts a short sequence of tokens (a speculative prefix), say 5 tokens. The Target model processes all 5 guesses in parallel to check their probabilities.Scenario: The Drafter predicts 5 tokens: [The, cat, sat, on, the].The Target model verifies them.Token 1 (“The”): Correct (Accepted).Token 2 (“cat”): Correct (Accepted).Token 3 (“sat”): Wrong (Rejected).What happens?Because the models are autoregressive, if token 3 is wrong, tokens 4 and 5 are automatically invalid because they were based on a wrong history.The system keeps “The” and “cat”.It discards “sat”, “on”, and “the”.The Target model resamples a correct token for position 3 using its own distribution.This ensures the output remains identical to what the Target would have produced alone.Even in this failure case, we generated 3 valid tokens (2 accepted + 1 corrected) in a single run of the Target model, tripling the speed of that step.The Mathematics of VerificationHow do we decide if a token is “Right” or “Wrong” when the models output probabilities, not absolutes? We use a specific rejection sampling scheme.Let q(x) be the probability from the Drafter and p(x) be the probability from the Target.Case 1: q(x) The Target thinks this token is more likely (or equally likely) than the Drafter did. We accept the token.Case 2: q(x) > p(x): The Drafter was overconfident. We reject the token with probability 1 — (p(x)/q(x))If rejected, we sample a new token from an adjusted distribution:This clever math guarantees that the final output distribution of the system is exactly p(x), maintaining the Target model’s quality perfectly.The “Sweet Spot”: Why not predict 100 tokens?If the Drafter is fast, why not guess 100 tokens ahead?Diminishing Returns: The acceptance rate (α) is rarely 100%. The probability of accepting a sequence of length γ drops exponentially as γ increases.Overhead: Running the Drafter takes time. The cost of running the system is roughly the cost of the Drafter times γ, plus one run of the Target.Wasted Compute: If you guess 100 tokens and the 2nd one is wrong, you wasted the compute for 98 predictions.The expected number of tokens generated per step is:Where α is the acceptance rate and γ is the number of speculative tokens.The Sweet Spot: Empirically, a lookahead γ of 3 to 7 tokens is usually optimal. This balances the cost of the Drafter against the probability of rejection.Selecting a Drafter & RisksHow to Pick a DrafterTokenizer: The Drafter and Target must use the same tokenizer so the token IDs match.Size Ratio: The Drafter should be significantly smaller (e.g., 100x smaller). A cost ratio (c) of around 0.05 (Drafter takes 5% of Target’s time) is ideal.Distribution Alignment: They should be trained on similar data so the acceptance rate (α) is high.Risks: When is it Dangerous?Speculative Decoding is not guaranteed to be faster.If the acceptance rate (α) drops too low or the Drafter is too slow, the system can become slower than standard decoding.Specifically, if the time spent generating guesses and failing to verify them exceeds the time of just running the Target model normally, you lose performance. However, since valid Drafters are usually tiny, this risk is minimal in well-tuned setups.Real-World SpeedupIn the original paper, the authors demonstrated significant improvements using T5-XXL (11B parameters) as the Target and T5-Small (77M parameters) as the Drafter:Speedup: 2X to 3X faster inference.Quality: Identical outputs to the baseline.Even using a trivial n-gram model (which just looks up the most common next words) as a Drafter resulted in a ~1.25X speedup, proving the robustness of the technique.Modern VariantsSince the original paper, several variants have emerged to further optimize this process:Medusa: Instead of a separate Drafter model, Medusa adds multiple “heads” to the Target model itself. These heads attempt to predict the next few tokens simultaneously during the single pass, removing the latency of calling a separate model.Lookahead Decoding (Jacobi Decoding): This method doesn’t use a Drafter at all. It uses the Target model itself to guess ahead by treating the sequence generation as a system of equations (Jacobi iteration), refining multiple future tokens in parallel steps.EAGLE: A more advanced technique that uses a lightweight feature-level drafter (often a single layer) that operates on the internal embeddings of the Target model rather than just the token outputs, improving the accuracy (α) of the guesses.Link to original paper: https://arxiv.org/pdf/2312.00752Until next time folks…El Psy CongrooWhy Your LLM Should Be Guessing: Breaking the Sequential Curse was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.