Course study · RL for speculative decoding
Learning when to stop drafting.
A small draft model guesses tokens and a big target model checks them all in one pass. After each guess, an RL agent decides: guess one more, or stop and verify. The output stays exactly the target's; only the speed changes. I compared four RL algorithms against fixed rules, in a simulator and live on the GPU.
Live speedup
1.75×
SmolLM2 with the Value Iteration policy: 68.4 tok/s against 39.2 for the target alone.
Over the best baseline
+6%
live, on both model pairs, against HuggingFace's confidence-threshold rule.
CUDA graphs
1.44 → 0.25
draft cost relative to the target. Without graphs, speculation could not win at all.
Simulator ranking
1.00
rank correlation with the live run on Qwen (0.94 on SmolLM2), at 8–14% optimistic.
Setup
Code on GitHub- GPU
- RTX 3050 Laptop, 6 GB, CUDA graphs
- Model pairs
- Qwen2.5 0.5B → 1.5B, SmolLM2 135M → 1.7B
- Prompts
- 498 chat, code and maths prompts: 399 to train, 99 to test
- Decoding
- Greedy, answers up to 128 tokens, up to 8 drafts per round
- Team
- Course project at Thapar Institute, with Vaibhav Sundriyal and Angad Shukla
The problem
Speculation as a decision process.
Draft too few tokens and the target is called too often. Draft too many and most get rejected. The agent sees how confident the draft is and chooses each step.
- State
- Tokens drafted s, latest draft confidence p, entropy H, chance all drafts survive q, recent acceptance rate a
- Actions
- CONTINUE (draft one more) or STOP (verify). STOP is forced at 8 drafts
- Reward
- On STOP: tokens gained − λ(1 + s·c). Zero on CONTINUE
- c
- Cost of one draft step relative to one target pass, measured on the GPU
- λ
- Price of a target call in tokens, set by a Dinkelbach update so the agent maximises tokens per second
Measured cost of a draft step
RTX 3050 Laptop GPUWithout CUDA graphs, a one-token step was limited by how fast the CPU launches kernels, so the small model was nearly as slow as the big one. Recording each forward pass as a CUDA graph is what made speculation pay.
| Pair | Draft step | Target verify | c | c without CUDA graphs | Draft match (chat / code / maths) |
|---|---|---|---|---|---|
| Qwen2.5 0.5B → 1.5B | 10.7 ms | 26.5 ms | 0.40 | 0.81 | 78 / 92 / 92% |
| SmolLM2 135M → 1.7B | 6.6 ms | 26.7 ms | 0.25 | 1.44 | 73 / 95 / 83% |
Algorithms
Four ways to learn the same choice.
All four solve the same problem on the 399 training prompts and are scored on 99 unseen ones. Baselines: a fixed draft length, and HuggingFace's rule of stopping when draft confidence falls below 0.4.
Value Iteration, model-based.
Bins the state into 80 cells (drafts × q), counts rewards and transitions from the logged outputs, and solves the Bellman equation in about 8 sweeps. It never plays the simulator. 1.5 s to train.
Q-learning, model-free.
Same 80 cells, a Q-table learned from 10,000 simulated answers with ε-greedy exploration. Same values as Value Iteration, estimated from noisy samples. 8 s to train.
DQN, function approximation.
A 5→64→64→2 network on all five continuous features, with a replay buffer and a target network. Sees the latest token's confidence and entropy, which the table cannot. About 3 minutes to train.
REINFORCE, policy gradient.
Learns action probabilities directly. Each draft round is one short episode, so returns have low variance. About 2.5 minutes to train.
What each agent learned
Policy on the test prompts

Results
Every agent beat every baseline.
Live: 30 test prompts × 3 runs, every method on the same CUDA-graph runners, method order rotated per prompt. Speedup is against the target model generating alone.
Live, wall clock
Tokens per second| Method | Qwen tok/s | Qwen speedup | SmolLM2 tok/s | SmolLM2 speedup |
|---|---|---|---|---|
| Target alone | 39.3 | 1.00× | 39.2 | 1.00× |
| Best fixed draft length | 54.3 | 1.38× | 61.4 | 1.57× |
| Threshold p < 0.4 (HuggingFace) | 55.7 | 1.42× | 64.6 | 1.65× |
| Value Iteration | 59.3 | 1.51× | 68.4 | 1.75× |
| REINFORCE | 58.8 | 1.50× | 66.7 | 1.70× |
| DQN | 57.9 | 1.47× | 66.4 | 1.70× |
| Q-learning | 57.7 | 1.47× | 65.4 | 1.67× |


Live, by task
Value Iteration against the threshold rule| Task | Qwen, Value Iteration | Qwen, threshold | SmolLM2, Value Iteration | SmolLM2, threshold |
|---|---|---|---|---|
| Chat | 1.31× | 1.25× | 1.48× | 1.39× |
| Code | 1.61× | 1.55× | 2.03× | 1.96× |
| Maths | 1.60× | 1.45× | 1.81× | 1.69× |


Simulator
99 unseen promptsThe simulator replays logged model outputs and counts time in target calls. It ranks the methods the same way as the live run, but reads 8–14% high because it skips per-step overhead.
| Method | Qwen speedup | Qwen tokens/call | SmolLM2 speedup | SmolLM2 tokens/call |
|---|---|---|---|---|
| Fixed k = 1 | 1.34× | 1.88 | 1.48× | 1.84 |
| Fixed k = 3 | 1.50× | 3.31 | 1.79× | 3.10 |
| Fixed k = 4 | 1.50× | 3.89 | 1.81× | 3.58 |
| Fixed k = 8 | 1.33× | 5.52 | 1.63× | 4.78 |
| Threshold p < 0.4 | 1.53× | 5.17 | 1.91× | 4.43 |
| Value Iteration | 1.66× | 3.88 | 2.02× | 3.84 |
| Q-learning | 1.59× | 4.77 | 1.94× | 4.42 |
| DQN | 1.63× | 4.78 | 2.01× | 4.10 |
| REINFORCE | 1.64× | 4.36 | 2.01× | 3.95 |




Across training seeds
Simulator speedup, mean ± std over 3 seeds| Algorithm | Qwen | SmolLM2 |
|---|---|---|
| Value Iteration | 1.657 (deterministic) | 2.019 (deterministic) |
| REINFORCE | 1.649 ± 0.005 | 2.011 ± 0.008 |
| DQN | 1.623 ± 0.013 | 2.015 ± 0.005 |
| Q-learning | 1.586 ± 0.008 | 1.928 ± 0.022 |


Price of a target call
Value Iteration, QwenToo cheap and the agent drafts long rounds that get rejected; too dear and it stops after one token. The best speedup sits at the λ the Dinkelbach update picked.
| λ, × tuned value | 0.5 | 0.7 | 0.9 | 1.0 | 1.1 | 1.2 | 1.4 |
|---|---|---|---|---|---|---|---|
| Sim speedup | 1.47× | 1.60× | 1.66× | 1.66× | 1.64× | 1.61× | 1.34× |
| Drafts per round | 6.4 | 5.1 | 3.9 | 3.3 | 2.9 | 2.5 | 1.0 |


Takeaways
What the agents taught me.
Six things this study showed.
Planning beats sampling when the model is cheap.
Value Iteration beat Q-learning on the same 80 states (1.66× against 1.59× on Qwen) and trained 5× faster. With an exact count-based model of the logs, there was nothing to gain from learning by trial.
The agents rediscovered the known optimal rule.
Value Iteration learned to stop when the chance all drafts survive drops below about 0.7. That is the threshold shape SpecDec++ proves optimal; nobody told it.
They read the task from confidence alone.
Agents drafted about twice as long on code and maths as on chat without being told the task. The biggest gain over the threshold rule was on chat, where knowing when to stop matters most.
Fewer tokens per call, more tokens per second.
The RL agents got fewer tokens per target call than the threshold rule but were faster, because they stop before a likely rejection and waste fewer draft steps.
Kernel launch overhead decides if speculation works.
Without CUDA graphs, the 135M draft model was slower than its 1.7B target on this laptop. The CPU launching kernels was the limit, not model size.
A comparison found a bug.
Q-learning first scored 1.28×. Its table, next to Value Iteration's, showed that the impossible CONTINUE at 8 drafts stayed at 0 and taught 'always continue'. Masking it fixed both Q-learning and DQN.
Limits
What this doesn't cover.
Where these numbers stop applying.
Hiring for backend, forward-deployed or GPU work? Let’s talk.
saxena.shikhar1808@gmail.com