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.

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 GPU

Without 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.

PairDraft stepTarget verifycc without CUDA graphsDraft match (chat / code / maths)
Qwen2.5 0.5B → 1.5B10.7 ms26.5 ms0.400.8178 / 92 / 92%
SmolLM2 135M → 1.7B6.6 ms26.7 ms0.251.4473 / 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
MethodQwen tok/sQwen speedupSmolLM2 tok/sSmolLM2 speedup
Target alone39.31.00×39.21.00×
Best fixed draft length54.31.38×61.41.57×
Threshold p < 0.4 (HuggingFace)55.71.42×64.61.65×
Value Iteration59.31.51×68.41.75×
REINFORCE58.81.50×66.71.70×
DQN57.91.47×66.41.70×
Q-learning57.71.47×65.41.67×

Live, by task

Value Iteration against the threshold rule
TaskQwen, Value IterationQwen, thresholdSmolLM2, Value IterationSmolLM2, threshold
Chat1.31×1.25×1.48×1.39×
Code1.61×1.55×2.03×1.96×
Maths1.60×1.45×1.81×1.69×

Simulator

99 unseen prompts

The 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.

MethodQwen speedupQwen tokens/callSmolLM2 speedupSmolLM2 tokens/call
Fixed k = 11.34×1.881.48×1.84
Fixed k = 31.50×3.311.79×3.10
Fixed k = 41.50×3.891.81×3.58
Fixed k = 81.33×5.521.63×4.78
Threshold p < 0.41.53×5.171.91×4.43
Value Iteration1.66×3.882.02×3.84
Q-learning1.59×4.771.94×4.42
DQN1.63×4.782.01×4.10
REINFORCE1.64×4.362.01×3.95

Across training seeds

Simulator speedup, mean ± std over 3 seeds
AlgorithmQwenSmolLM2
Value Iteration1.657 (deterministic)2.019 (deterministic)
REINFORCE1.649 ± 0.0052.011 ± 0.008
DQN1.623 ± 0.0132.015 ± 0.005
Q-learning1.586 ± 0.0081.928 ± 0.022

Price of a target call

Value Iteration, Qwen

Too 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 value0.50.70.91.01.11.21.4
Sim speedup1.47×1.60×1.66×1.66×1.64×1.61×1.34×
Drafts per round6.45.13.93.32.92.51.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.

  • Greedy decoding only
  • Small models on a 6 GB GPU
  • Speedups depend on the hardware's c
  • Simulator is 8–14% optimistic
  • Live run uses one seed per algorithm
  • 83–97% token-exact (fp16 near-ties)

Hiring for backend, forward-deployed or GPU work? Let’s talk.

saxena.shikhar1808@gmail.com