Phase 3: Speculative decoding with draft model

Add speculative decoding engine that uses a small draft model (e.g.
Qwen2.5-0.5B) to propose K candidate tokens, verified in one batch
by the target model (e.g. Qwen2.5-32B). Greedy mode (temp=0) only.

Key features:
- Fast-reject: skip target batch when d1 mismatches (saves ~40% of batches)
- KV cache rollback after verification (accepted prefix kept)
- Separate threadpools: target=high priority, draft=normal
- ar_forward_batch: new API for multi-token forward without cache reset

New files: src/ar-speculative.{h,cpp}
CLI flags: --draft, --spec-k, --draft-threads

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Carmen EstebanandClaude Opus 4.6 committed 2026-04-05 18:42:33 +02:00
1 parent 740e8366e9
commit e9d830652e
7 files changed
+532 -6

No files matched your search

+37
View File
@@ -115,4 +115,41 @@ std::vector<int32_t> ar_generate(
const ar_sampler_params & params,
ar_token_callback callback = nullptr);
// ═══════════════════════════════════════════════════════════════
// ── Speculative Decoding API ────────────────────────────────────
// ═══════════════════════════════════════════════════════════════
struct ar_spec_params {
int K = 4; // speculative lookahead (draft tokens per round)
};
struct ar_spec_stats {
int total_draft_tokens = 0;
int total_accepted = 0;
int total_generated = 0;
int total_target_batches = 0;
int total_target_decodes = 0;
int total_fast_rejects = 0;
double prefill_ms = 0;
double decode_ms = 0;
float acceptance_rate() const {
return total_draft_tokens > 0
? (float)total_accepted / total_draft_tokens : 0.0f;
}
float tokens_per_target_call() const {
int calls = total_target_batches + total_target_decodes;
return calls > 0 ? (float)total_generated / calls : 1.0f;
}
};
std::vector<int32_t> ar_speculative_generate(
diffuse_context * target_ctx,
diffuse_context * draft_ctx,
const std::vector<int32_t> & prompt_tokens,
int max_tokens,
const ar_spec_params & params,
ar_token_callback callback = nullptr,
ar_spec_stats * stats_out = nullptr);
// end of diffuse.h