mirror of
https://github.com/iafiscal1212/diffuse-cpp.git
synced 2026-12-04 08:38:08 +00:00
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:
1 parent
740e8366e9
commit
e9d830652e
7 files changed
+532
-6
No files matched your search
+84
-6
@@ -26,8 +26,13 @@ static void print_usage(const char * prog) {
|
||||
fprintf(stderr, " --seed INT Random seed (default: 42)\n");
|
||||
fprintf(stderr, " --tokens IDs Comma-separated pre-tokenized input\n");
|
||||
fprintf(stderr, " --bind-cores Bind threads to CPU cores (reduces jitter)\n");
|
||||
fprintf(stderr, "\nSpeculative decoding:\n");
|
||||
fprintf(stderr, " --draft PATH Draft model GGUF (enables speculative decoding)\n");
|
||||
fprintf(stderr, " --spec-k INT Speculative lookahead (default: 4)\n");
|
||||
fprintf(stderr, " --draft-threads INT Threads for draft model (default: 4)\n");
|
||||
fprintf(stderr, "\nExample:\n");
|
||||
fprintf(stderr, " %s -m thrombia-32b-q4.gguf --tokens 151644,8948,198,... -n 512 -t 12\n", prog);
|
||||
fprintf(stderr, " %s -m thrombia-32b-q4.gguf --draft qwen2.5-0.5b-q4.gguf --spec-k 5 -t 12\n", prog);
|
||||
}
|
||||
|
||||
static std::vector<int32_t> parse_tokens(const char * str) {
|
||||
@@ -43,10 +48,13 @@ static std::vector<int32_t> parse_tokens(const char * str) {
|
||||
|
||||
int main(int argc, char ** argv) {
|
||||
std::string model_path;
|
||||
std::string draft_path;
|
||||
std::string prompt;
|
||||
std::vector<int32_t> input_tokens;
|
||||
int max_tokens = 256;
|
||||
int n_threads = 4;
|
||||
int draft_threads = 4;
|
||||
int spec_k = 4;
|
||||
float temperature = 0.0f;
|
||||
float top_p = 0.9f;
|
||||
int top_k = 40;
|
||||
@@ -58,12 +66,18 @@ int main(int argc, char ** argv) {
|
||||
for (int i = 1; i < argc; i++) {
|
||||
if (strcmp(argv[i], "-m") == 0 && i + 1 < argc) {
|
||||
model_path = argv[++i];
|
||||
} else if (strcmp(argv[i], "--draft") == 0 && i + 1 < argc) {
|
||||
draft_path = argv[++i];
|
||||
} else if (strcmp(argv[i], "-p") == 0 && i + 1 < argc) {
|
||||
prompt = argv[++i];
|
||||
} else if (strcmp(argv[i], "-n") == 0 && i + 1 < argc) {
|
||||
max_tokens = atoi(argv[++i]);
|
||||
} else if (strcmp(argv[i], "-t") == 0 && i + 1 < argc) {
|
||||
n_threads = atoi(argv[++i]);
|
||||
} else if (strcmp(argv[i], "--draft-threads") == 0 && i + 1 < argc) {
|
||||
draft_threads = atoi(argv[++i]);
|
||||
} else if (strcmp(argv[i], "--spec-k") == 0 && i + 1 < argc) {
|
||||
spec_k = atoi(argv[++i]);
|
||||
} else if (strcmp(argv[i], "--temp") == 0 && i + 1 < argc) {
|
||||
temperature = atof(argv[++i]);
|
||||
} else if (strcmp(argv[i], "--top-p") == 0 && i + 1 < argc) {
|
||||
@@ -118,14 +132,80 @@ int main(int argc, char ** argv) {
|
||||
#ifdef _OPENMP
|
||||
omp_set_num_threads(n_threads);
|
||||
#endif
|
||||
// Set environment for child threads (GGML uses pthreads internally)
|
||||
setenv("OMP_PROC_BIND", "close", 0); // don't override if already set
|
||||
setenv("OMP_PROC_BIND", "close", 0);
|
||||
setenv("OMP_PLACES", "cores", 0);
|
||||
setenv("GOMP_CPU_AFFINITY", "0-1023", 0);
|
||||
fprintf(stderr, "Thread binding: enabled (%d threads, close/cores)\n", n_threads);
|
||||
}
|
||||
|
||||
// Load model
|
||||
// ── Speculative decoding path ───────────────────────────────
|
||||
if (!draft_path.empty()) {
|
||||
fprintf(stderr, "=== SPECULATIVE DECODING MODE ===\n");
|
||||
fprintf(stderr, "Loading target model: %s\n", model_path.c_str());
|
||||
diffuse_model * target_model = diffuse_model_load(model_path, n_threads);
|
||||
if (!target_model) {
|
||||
fprintf(stderr, "Failed to load target model\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
fprintf(stderr, "Loading draft model: %s\n", draft_path.c_str());
|
||||
diffuse_model * draft_model = diffuse_model_load(draft_path, draft_threads);
|
||||
if (!draft_model) {
|
||||
fprintf(stderr, "Failed to load draft model\n");
|
||||
diffuse_model_free(target_model);
|
||||
return 1;
|
||||
}
|
||||
|
||||
const auto & t_hp = diffuse_model_hparams(target_model);
|
||||
const auto & d_hp = diffuse_model_hparams(draft_model);
|
||||
int n_ctx = (int)input_tokens.size() + max_tokens + spec_k + 4;
|
||||
|
||||
fprintf(stderr, "Target: %dL, %d heads, vocab=%d\n",
|
||||
(int)t_hp.n_layer, (int)t_hp.n_head, (int)t_hp.n_vocab);
|
||||
fprintf(stderr, "Draft: %dL, %d heads, vocab=%d\n",
|
||||
(int)d_hp.n_layer, (int)d_hp.n_head, (int)d_hp.n_vocab);
|
||||
fprintf(stderr, "Prompt: %d tokens, max_generate: %d, K=%d\n",
|
||||
(int)input_tokens.size(), max_tokens, spec_k);
|
||||
|
||||
diffuse_context * target_ctx = diffuse_context_new(target_model, n_ctx, n_threads);
|
||||
diffuse_context * draft_ctx = diffuse_context_new(draft_model, n_ctx, draft_threads);
|
||||
|
||||
ar_spec_params sparams;
|
||||
sparams.K = spec_k;
|
||||
|
||||
ar_spec_stats stats;
|
||||
|
||||
auto result = ar_speculative_generate(
|
||||
target_ctx, draft_ctx, input_tokens, max_tokens, sparams,
|
||||
[](int32_t token, int pos) -> bool {
|
||||
fprintf(stderr, "\r generating... %d tokens", pos);
|
||||
return true;
|
||||
},
|
||||
&stats);
|
||||
|
||||
fprintf(stderr, "\n\nGenerated %d token IDs:\n", (int)result.size());
|
||||
for (size_t i = 0; i < result.size(); i++) {
|
||||
if (i > 0) printf(",");
|
||||
printf("%d", result[i]);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
fprintf(stderr, "\nSpeculative stats:\n");
|
||||
fprintf(stderr, " Acceptance rate: %.1f%% (%d/%d)\n",
|
||||
100.0f * stats.acceptance_rate(),
|
||||
stats.total_accepted, stats.total_draft_tokens);
|
||||
fprintf(stderr, " Tokens/target_call: %.2f\n",
|
||||
stats.tokens_per_target_call());
|
||||
fprintf(stderr, " Fast rejects: %d\n", stats.total_fast_rejects);
|
||||
|
||||
diffuse_context_free(target_ctx);
|
||||
diffuse_context_free(draft_ctx);
|
||||
diffuse_model_free(target_model);
|
||||
diffuse_model_free(draft_model);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ── Standard AR decoding path ───────────────────────────────
|
||||
fprintf(stderr, "Loading model...\n");
|
||||
diffuse_model * model = diffuse_model_load(model_path, n_threads);
|
||||
if (!model) {
|
||||
@@ -155,10 +235,8 @@ int main(int argc, char ** argv) {
|
||||
|
||||
auto result = ar_generate(ctx, input_tokens, max_tokens, sparams,
|
||||
[](int32_t token, int pos) -> bool {
|
||||
// Streaming: print token IDs as they come
|
||||
// (decode externally with tokenizer)
|
||||
fprintf(stderr, "\r generating... %d tokens", pos);
|
||||
return true; // continue
|
||||
return true;
|
||||
});
|
||||
|
||||
fprintf(stderr, "\n\nGenerated %d token IDs:\n", (int)result.size());
|
||||
|
||||
Reference in new issue
Block a user