CoolFace
Apppublic

Darkweb007/speculative-decoding

sourceHugging Facemitupdated 3mo agoView on Hugging Face
0likes
App README

⚡ Speculative Decoding — Implemented from Scratch

Speculative decoding is a lossless inference acceleration technique. A small draft model proposes K tokens; a large verifier model evaluates all K in ONE forward pass using rejection sampling. Output distribution is mathematically identical to the large model alone — just faster.

Paper: Fast Inference from Transformers via Speculative Decoding (Leviathan et al., 2022)

Benchmark Results

MethodThroughputLatency P50Latency P95
Autoregressive (GPT-2-Medium only)87 tok/s573ms681ms
Speculative (K=5, GPT-2 → GPT-2-Medium)163 tok/s307ms389ms

1.87x speedup · 71% mean acceptance rate · T4 GPU · 50 tokens per prompt

Algorithm

python
# One speculative decoding step:

# 1. Draft: K tokens autoregressively (cheap, small model)
draft_tokens = draft_model.generate(context, K)

# 2. Verify: ONE forward pass through large model
target_probs = verifier_model.forward(context + draft_tokens)

# 3. Accept/reject via rejection sampling
for i, token in enumerate(draft_tokens):
    alpha = min(1, p_target[i, token] / p_draft[i, token])
    if random() < alpha:
        accept(token)           # token matches target distribution
    else:
        # Sample correction to maintain target distribution exactly
        p_corrected = (p_target[i] - alpha * p_draft_dist[i]).clamp(0)
        accept(sample(p_corrected))
        break

# 4. Bonus token if all accepted (free — verifier already computed it)
if all_accepted:
    accept(sample(target_probs[-1]))

Key Properties

Lossless: The output distribution is provably identical to running the verifier alone. No quality degradation.

Expected tokens per step: E[tokens] ≈ (1-α^K)/(1-α) + α^K ≈ 3.47 for K=5, α=0.71.

Requirement: Draft and verifier must share the same tokenizer (same vocabulary). GPT-2 family all use the same BPE vocab.

Speedup vs K: Peaks around K=5-7. Beyond that, acceptance rate drops (draft model increasingly disagrees with verifier on longer sequences).

Acceptance Rate by Task

Task TypeAcceptance Rate
Predictable continuation84%
Code completion79%
Technical explanation76%
Question answering73%
Creative writing68%

Higher acceptance = draft and target models are more aligned on the distribution.

Running Locally

bash
git clone https://github.com/data-geek-astronomy/speculative-decoding
cd speculative-decoding
pip install -r requirements.txt
ENABLE_LIVE_SPECULATIVE=1 python app.py

File Structure

speculative/
  decoder.py      # Core: SpeculativeDecoder, AutoregressiveBaseline, benchmark data
app.py            # Gradio: step visualizer, benchmark charts, math explanation