Optimizing LLM Inference With Speculative Decoding

Generative AI
Date:November 27, 2026
Topic:
Optimizing LLM Inference With Speculative Decoding
⏱ 3 min read

You're serving a 7B parameter model. Users complain about latency. You've tried quantization, batching, and KV cache optimization. Throughput is decent, but time-to-first-token still stings. The bottleneck isn't compute—it's memory bandwidth. Every token generated requires a full model forward pass, moving gigabytes of weights from VRAM to compute units. What if you could skip most of those passes?

The Core Idea: Guess and Verify

Speculative decoding separates generation into two stages: a fast draft model predicts multiple tokens, then the target model verifies them in a single forward pass. If the draft matches the target's distribution, you accept multiple tokens for the price of one large model call. If not, you reject and fall back. The math is brutal: a 10x smaller draft model runs 10x faster. Even with a 50% acceptance rate, you cut latency nearly in half.

"

Speculative decoding is the only technique that reduces latency without degrading model quality—it changes how you sample, not what you sample.

— Charlie Chen, LLM Systems Researcher

Architecture: Draft Model Selection

The draft model must be fast and reasonably aligned with the target. Three proven approaches exist: a distilled version of the target model (best quality, highest training cost), a smaller open model like TinyLlama or Mamba (zero training, moderate alignment), or a n-gram language model (instant, weak alignment). For production, distilled drafts hit 60-80% acceptance rates. TinyLlama-1.1B drafting for Llama-3-70B achieves ~45%. Choose based on your training budget and latency SLA.

python
class SpeculativeDecoder:
    def __init__(self, target_model, draft_model, max_draft_tokens=4):
        self.target = target_model
        self.draft = draft_model
        self.gamma = max_draft_tokens
    
    def generate(self, input_ids, max_new_tokens=100):
        while len(input_ids) < max_new_tokens:
            # Draft phase: generate gamma tokens fast
            draft_tokens = self.draft.generate(input_ids, self.gamma)
            
            # Verify phase: single forward pass on target
            target_logits = self.target.forward(input_ids + draft_tokens)
            
            # Accept/reject using modified rejection sampling
            accepted = self._verify(draft_tokens, target_logits)
            input_ids.extend(accepted)
            
            if len(accepted) < self.gamma:
                # Resample rejection token from target distribution
                input_ids.append(self._sample(target_logits[-1]))
        return input_ids

KV Cache Mechanics

The draft model needs its own KV cache. During verification, the target model processes the full sequence (prompt + draft tokens) in one forward pass. This means the target's KV cache grows by the number of accepted tokens only. Rejected tokens never enter the target cache. Implementation detail: allocate draft cache separately, then slice target cache to accepted length after verification. This avoids cache pollution and keeps memory footprint predictable.

MetricStandard DecodingSpeculative (γ=4)Speedup
Time per token (ms)45182.5x
VRAM read/token (GB)145.62.5x
Acceptance rateN/A62%
Quality (MT-Bench)8.418.41Identical
💡
TipTune gamma (draft tokens per step) based on acceptance rate. Higher gamma increases potential speedup but lowers acceptance. Sweet spot is usually 3-5 for distilled drafts, 2-3 for off-the-shelf small models.

Rejection Sampling Without Quality Loss

The verification step uses a modified rejection sampling algorithm that guarantees the output distribution exactly matches the target model. For each draft token, compute acceptance probability: min(1, p_target / (c * p_draft)) where c is a constant ensuring p_target <= c * p_draft everywhere. If rejected, resample from the corrected distribution: (p_target - c * p_draft) / (1 - c * p_draft). This proves speculative decoding is a zero-quality-loss optimization—it's mathematically identical to sampling from the target directly.

⚠️
WarningDon't implement rejection sampling from scratch. Use vetted libraries like Hugging Face's `generate` with `assistant_model` or vLLM's speculative decoding module. Subtle bugs in acceptance logic silently corrupt output distributions.

Production Checklist

Deploying speculative decoding requires infrastructure changes. First, colocate draft and target models on the same GPU to avoid PCIe transfers—use CUDA graphs for both. Second, pad draft sequences to fixed length for continuous batching compatibility. Third, monitor acceptance rate per request; drops below 30% indicate draft model drift or distribution shift. Fourth, set a hard latency timeout: if verification exceeds standard decoding time, fall back immediately.


✦

Next Steps

Start with a distilled draft model if you have training compute—it pays off in acceptance rate. Otherwise, plug in TinyLlama or a Mamba variant today. Measure baseline latency, enable speculative decoding with gamma=4, and track acceptance rate. If you hit >50%, you've just cut user-facing latency in half without touching model weights. That's the highest ROI optimization in LLM serving right now.

Share𝕏 Twitterin LinkedInin Whatsapp