1. Theoretical Motivation & Foundations
Auto-regressive language generation requires sequentially reading all model weights from GPU DRAM for every single generated token. Speculative decoding bypasses this memory-bandwidth bottleneck by leveraging a fast draft model (or multiple parallel draft prediction heads) to propose a sequence of K speculative tokens. Because the large target model is compute-bound when processing K tokens in parallel, it can verify all K candidate tokens in a single forward execution step with virtually no additional latency compared to generating a single token. A rigorous modified rejection sampling protocol guarantees that the combined output distribution is mathematically identical to running the large target model independently. If candidate tokens are accepted with probability α, the expected number of emitted tokens per target model forward step becomes (1 - α^(K+1)) / (1 - α), delivering 2.0x to 3.5x wall-clock speedups without any loss of generation quality. This module explores linear drafting, tree-based verification with custom attention masks (SpecInfer), and multi-head drafting architectures like Medusa and EAGLE.
2. Mathematical Formulations & Derivations
The governing analytical formulations and proof frameworks for this module:
3. From-Scratch Reference Implementation
Executable, production-tested reference code without magic libraries:
# Speculative Decoding Rejection Sampling Simulator
import random
def speculative_step(target_logits, draft_logits, vocab_size=5):
# Mock probability distributions
p = [math.exp(x) for x in target_logits]
p = [x / sum(p) for x in p]
q = [math.exp(x) for x in draft_logits]
q = [x / sum(q) for x in q]
# Draft model proposes token
sampled_draft = random.choices(range(vocab_size), weights=q)[0]
acceptance_prob = min(1.0, p[sampled_draft] / q[sampled_draft])
if random.random() < acceptance_prob:
return sampled_draft, True, p[sampled_draft]
else:
# Sample from normalized residual distribution
residual = [max(0.0, p[i] - q[i]) for i in range(vocab_size)]
res_sum = sum(residual)
if res_sum > 0:
residual_probs = [x / res_sum for x in residual]
replacement = random.choices(range(vocab_size), weights=residual_probs)[0]
else:
replacement = random.choices(range(vocab_size), weights=p)[0]
return replacement, False, p[replacement]
random.seed(42)
t_log = [1.2, 4.5, 0.3, -1.0, 2.1]
d_log = [1.0, 4.1, 0.5, -0.8, 1.9]
tok, accepted, prob = speculative_step(t_log, d_log)
print(f'Token: {tok} | Accepted: {accepted} | Target Probability: {prob:.4f}')
4. Systems Complexity & Memory Footprint
Speculative decoding converts idle GPU compute during auto-regressive generation into wall-clock speedup. Because target model verification processes draft tokens in parallel using high-intensity matrix math, inference latency is slashed by up to 60% while maintaining lossless output fidelity.
5. Canonical Literature & Primary Research
Original research papers and foundational texts recommended for advanced study:
- Leviathan, Y., Kalman, M., & Matias, Y. (2023). Fast Inference from Transformers via Speculative Decoding. ICML.
- Chen, C., et al. (2023). Accelerating Large Language Model Decoding with Speculative Sampling. arXiv:2302.01318.
- Cai, T., et al. (2024). Medusa: Simple LLM Inference Acceleration Framework with Multiple Decoding Heads. arXiv:2401.10774.
- Li, Y., et al. (2024). EAGLE: Speculative Sampling Requires Rethinking Feature Uncertainty. ICML.