Level 4 · advanced · ~35 h
Speculative decoding with a draft model: acceptance and latency study
Implement speculative decoding with a small draft model and a large target model, derive the acceptance criterion, and measure the latency-accuracy tradeoff across draft sizes and temperature settings.
Problem statement
- 01Implement speculative decoding with a small draft model and a large target model, derive the acceptance criterion, and measure the latency-accuracy tradeoff across draft sizes and temperature settings.
Real-world motivation
- 01Speculative decoding preserves the target distribution exactly while reducing latency; implementation mistakes break this guarantee silently.
Learning objectives
- 01Implement lossless speculative sampling
- 02Measure acceptance rates
- 03Benchmark wall-clock speedups
Technical architecture
- 01draft model proposes k tokens
- 02target model verifies in one forward pass
- 03resampling from adjusted distributions on rejection
Mathematical foundations
- 01accept with probability $\min(1,p_{target}(x)/p_{draft}(x))$
- 02on rejection sample from $\mathrm{normalize}(\max(0,p-q))$
Dataset and sources
- 01WikiText-103 prompts
- 02Alpaca instruction prompts
Step-by-step tasks
- 01Implement the exact speculative sampling algorithm
- 02Verify output distribution matches target-only sampling statistically
- 03Sweep draft length k and draft model size
- 04Benchmark tokens per second at batch 1 and batch 8
- 05Analyze acceptance rate versus token position
Implementation requirements
- 01Use two causal LMs with the same tokenizer
- 02Use KV caches for both models
- 03Log acceptance per step
Testing requirements
- 01Distribution test against target-only sampling
- 02Check no quality regression on greedy decoding
- 03Verify KV cache consistency after rejection
Evaluation metrics
- 01acceptance rate
- 02tokens per second
- 03speedup factor
- 04distribution divergence
Expected outputs
- 01implementation
- 02latency benchmark
- 03acceptance analysis
- 04report
Common failure modes
- 01approximate acceptance criterion changes the distribution
- 02draft model too slow negates gains
- 03KV cache not rolled back on rejection
Stretch goals
- 01add adaptive draft length
- 02study tree-based speculation
Research extensions
- 01derive expected speedup from acceptance rate
- 02study optimal draft-target size ratios
GitHub submission requirements
- 01Code with tests
- 02Distribution verification
- 03Latency tables
- 04Report with derivation
Completion rubric
- 01sampling algorithm exact
- 02distribution verified
- 03speedup measured
- 04acceptance analyzed
- 05cache handling correct
Submit when every rubric item is satisfied and your repository contains runnable code plus a reproduction command.