Implement speculative decoding with two GPT-2 models on CPU
CPU · your Macnotebook150 minrequiredGoal · Build draft-verify-accept decoding with gpt2 as draft and gpt2-medium as target, measure acceptance rate, and verify losslessness statistically.
- 1Load gpt2 (draft) and gpt2-medium (target) in float32 on CPU; write a plain autoregressive sampler for the target as the baseline with a fixed seed.
- 2Write draft(prompt_ids, gamma) that samples gamma tokens from the draft model and returns the tokens and their probabilities q.
- 3Run the target once over prompt + draft tokens and gather target probabilities p at each draft position.
- 4Implement the acceptance loop; accept with probability min(1, p/q); on first rejection sample from normalize(max(0, p - q)); if all accepted sample the bonus token from the last target distribution.
- 5Generate 200 tokens for five prompts with gamma in [2, 4, 8]; record acceptance rate and mean accepted tokens per verification step.
- 6Losslessness check; for one fixed short prompt, sample the next token 5,000 times with plain target sampling and 5,000 times with speculative sampling and compare the two empirical distributions with a chi-square test or total-variation distance.
- 7Time the baseline and speculative runs on CPU and compute tokens per second for each gamma.
- Deliverable
- A notebook with the implementation, an acceptance-rate table per gamma and the distribution comparison.
- Check
- Total-variation distance between the two empirical next-token distributions is under 0.05; acceptance rate is between 0.5 and 0.9 and mean accepted tokens per step rises with gamma but with diminishing returns.