LLM Inference Acceleration in Practice: How Speculative Decoding Works and What Drives Its Performance
This article explains why token-by-token autoregressive decoding is inherently slow and how speculative decoding (the draft-and-verify paradigm) increases the number of tokens produced per weight read. It walks through the standard workflow and pseudocode, the three variables that determine real-world gains (acceptance rate, draft length, and where the system bottleneck sits), the engineering pieces required for production (draft model alignment, sampling consistency, batching and KV cache management, cache rollback, observability), and how speculative decoding combines with quantization, paged caching, and continuous batching. A reusable method for measuring your own tokens/s is included.
1. Starting from the Question “How Many Tokens per Second?”
When deploying a dense large language model with billions of parameters, the question teams ask each other most often is: how many tokens/s can generation reach on a single consumer-grade GPU? The question looks simple, but it has no single answer, because the result is shaped jointly by weight precision, memory bandwidth, context length, batch size, sampling strategy, and how the inference framework schedules work.
A typical pattern: with the same model on the same GPU, streaming output for a single request may reach only a few dozen tokens per second, while overall server throughput under high-concurrency batching can be several times higher. The former is constrained by the latency of token-by-token serial decoding; the latter amortizes the cost of reading weights across many requests. Understanding the difference between the two is the prerequisite for discussing any acceleration approach.
Note that this article does not benchmark any specific model or product. It uses “generation speed of a 27B-class dense model on a single GPU” as generic technical background, and discusses the inference acceleration ideas widely adopted in the industry.
2. Why Token-by-Token Decoding Is Inherently Slow
The math of autoregressive generation is simple:
P(x1..xn) = Π P(xi | x1..x(i-1))
It requires that token i cannot be computed until all of the previous i-1 tokens have been produced. This has two consequences:
- No parallelism: one forward pass confirms only one position, leaving compute units largely idle.
- Bandwidth bound: every token generated requires moving all weights from memory into the compute units once. Decoding is a classic memory-bound scenario with very low arithmetic intensity, so the GPU’s peak compute is almost never used.
This also explains why “doubling the parameter count does not necessarily halve the speed” — what really determines speed is the ratio of weight bytes read to memory bandwidth. So the core direction for acceleration is not making matrix multiplications faster, but making a single weight read produce more tokens.
3. Speculative Decoding: From “Confirm One by One” to “Verify in Batch”
The idea mentioned in the original text is generally known in the industry as speculative decoding, or the draft-and-verify paradigm. Its logic resembles a retrieval system: first recall a batch of candidates with a cheap method, then decide on all of them at once with an expensive method.
The standard flow:
- Use a much smaller draft model to autoregressively generate n candidate tokens;
- Append these n candidates to the confirmed sequence and run one forward pass on the large model to obtain distributions at n+1 positions;
- Compare from left to right: if the token at position k is accepted, then all of the first k candidates are essentially free, and the distribution at position k+1 has already been computed, so one more token can be produced for free;
- If a position is rejected, resample a token from the corrected distribution at that position, discard all subsequent candidates, and start the next round.
Illustrative pseudocode:
# Skeleton of one speculative decoding rounddraft = [small_model.sample() for _ in range(n)] # draft stage: serial but cheaplogits = big_model.forward(prefix + draft) # verify stage: one parallel forward passaccepted = 0for i in range(n):if accept(draft[i], logits[i]): # common acceptance criterionaccepted += 1else:breakbonus = sample_from(logits[accepted]) # correction at rejection, or extra token at the endnew_tokens = draft[:accepted] + [bonus]
The key is step 2: the n+1 positions share the same weight read, so the marginal cost of verification is far lower than generating n times one by one. When the acceptance rate is high, this is effectively trading one large-model call for multiple tokens — which is where the jump from a few dozen tokens per second to something close to a hundred comes from.
4. Three Variables That Determine the Actual Gain
The “40 t/s optimized to nearly 120” figure from the original text is an order-of-magnitude reference, not a universal promise. Real gains depend on:
- Acceptance rate α: the closer the draft distribution is to the large model’s, the higher the acceptance rate. If both models come from the same source and share the same tokenizer and domain data, the acceptance rate is usually solid; it drops noticeably across domains or when the prompt style changes abruptly.
- Draft length n: a larger n raises the theoretical ceiling, but once a rejection happens midway, everything after it is wasted, and drafting itself takes time. There is an optimal n that needs to be measured per task.
- Hardware and bottleneck location: if the large model is already running under high-concurrency batching with compute saturated, the gain from speculative decoding is compressed; conversely, the gain is most obvious in low-concurrency online chat scenarios with strict latency requirements.
A common rough intuition is that the expected number of tokens produced per round is about (1 - α^(n+1)) / (1 - α), and only after subtracting draft overhead do you get the net speedup. The more reliable engineering practice is to measure end-to-end output tokens per second and time-to-first-token directly.
5. What Must Be in Place for Production
Getting this mechanism to run stably in production requires more than the principle:
- Choosing and aligning the draft model: the tokenizer, vocabulary, and special tokens must match the target model, otherwise the distributions in the verification stage cannot be compared position by position.
- Sampling consistency: greedy decoding, temperature, top-p, and related parameters must be semantically consistent on both the draft and verify sides, otherwise the acceptance decision becomes distorted.
- Batching and memory management: with concurrent requests, different requests have different accepted lengths, causing ragged batches. Mature implementations introduce continuous batching and paged key-value cache management to reduce idle spinning and fragmentation.
- Key-value cache rollback: the cache entries corresponding to rejected candidates must be truncated correctly. This is critical for correctness and the easiest place to introduce bugs.
- Observability: it is advisable to turn acceptance rate, average accepted length, draft time share, and end-to-end throughput into monitoring metrics, and observe them continuously with logging and alerting, rather than looking at a single benchmark number.
6. How It Relates to Other Acceleration Techniques
Speculative decoding does not exclude other optimizations; real systems often stack them: weight quantization and low-precision compute reduce bandwidth pressure; paged cache management improves memory utilization; continuous batching improves concurrent throughput; operator fusion and efficient attention implementations reduce the constant overhead of a single forward pass. On model inference services such as Baidu AI Cloud, these capabilities are typically offered under product categories like general inference acceleration, elastic scaling, and monitoring and alerting, and users can combine and tune them according to their own latency and cost targets.
7. How to Evaluate Your Own “tokens/s”
A reusable evaluation method:
- Fix input and output lengths and sampling parameters, and measure single concurrency and multiple concurrency separately;
- Record both time-to-first-token and overall output rate, avoiding reporting a single number;
- Run one round with speculative decoding on and one with it off, and compute the net speedup rather than the theoretical value;
- Cover long-context scenarios and observe how key-value cache growth affects the rate;
- Measure the acceptance rate with real business corpora, not with a general-purpose Q&A set.
8. Conclusion
The value of speculative decoding is that it shifts the optimization dimension: instead of pursuing a faster single forward pass, it makes one expensive weight read carry the confirmation work for more tokens. It restructures generation from purely serial into “cheap recall + parallel verification,” which in low-concurrency online scenarios often delivers several times the effective speedup. But it is not a free lunch: the gain depends heavily on the acceptance rate, draft length, and where the system bottleneck sits, and must be validated and continuously tuned against end-to-end metrics.
