Int3 merge f-0291-bw-next (4c5a55491); generate.cpp: req_df2 and draft_long both kept

This commit is contained in:
Niko1221 committed 2026-11-01 09:37:10 +01:00
commit de856c7b8c
4 files changed
+149 -13

No files matched your search

+97 -7
View File
@@ -175,6 +175,7 @@ bool window_slot_copy_main(bool into_slot, int slot, int64_t upto, bool kv_only,
#include <stdexcept>
#include <string>
#include <set>
#include <unordered_map>
#include <vector>
// logprobs=K / allowed=...: one `LP` line for the token just emitted, from `ver`'s head
@@ -927,6 +928,11 @@ struct Options {
/// of the MTP layer, which stays loaded as the fallback. Needs --mtp, --spec and a native pack; a missing pack falls back to the MTP.
std::string draft, draft_model;
double draft_conf = 0.0; ///< --draft-conf P: with --draft dflash2 the window ends where the product of the selector's probabilities falls below P (replaces --spec-min-p)
/// --draft auto (serve): both drafters stay current and each request picks one (its key draft=mtp|dflash2, else draft_rule); the CLI treats auto as dflash2
bool draft_auto = false;
std::string draft_rule = "df2"; ///< --draft-rule df2|mtp|learn: what a request without a draft= key gets
int draft_mtp_spec = 4; ///< --draft-mtp-spec N: the widest window of a request that drafts with the MTP layer under --draft auto
double draft_mtp_min_p = -1.0; ///< --draft-mtp-min-p P: the MTP requests' --spec-min-p under --draft auto (-1 = --spec-min-p)
int draft_cap = 0; ///< --draft-cap N: the widest verify window with --draft dflash2 (0 = by card class, see where the drafter loads)
/// A control vector on the residual stream (strata/kernels/cvec.hpp), with llama.cpp's flags: the
/// `experimental-speed-projection` profile passes `--control-vector-scaled FILE:1.0 --control-vector-layer-range
@@ -1086,6 +1092,8 @@ void usage() {
" --draft mtp|dflash2 the draft source (default mtp); dflash2 needs --draft-model PACK (tools/dflash2_pack.py)\n"
" and keeps the MTP layer loaded as the fallback (opt-in, Flash-Next, CUDA / HIP)\n"
" --draft-conf P with --draft dflash2: end the window where the product of the selector's probabilities falls below P (default 0 = --spec-min-p)\n"
" --draft auto (serve) both drafters stay current; a request picks one: draft=mtp|dflash2, else --draft-rule df2|mtp|learn;\n"
" MTP requests use --draft-mtp-spec N (default 4) and --draft-mtp-min-p P\n"
" --draft-cap N the widest verify window with --draft dflash2 (default by card: 5 on Strix Halo / iGPU, --spec as given elsewhere)\n"
" --control-vector-scaled FILE:SCALE[,...] a control vector GGUF on the residual stream (llama.cpp's\n"
" format; --control-vector FILE = scale 1). --serve: requests switch it (cvec=0|1)\n"
@@ -3488,11 +3496,15 @@ int main(int argc, char** argv) {
else if (a == "--mtp-draft-vocab") o.mtp_draft_vocab = next("--mtp-draft-vocab");
else if (a == "--draft") {
o.draft = next("--draft");
if (o.draft == "auto") { o.draft = "dflash2"; o.draft_auto = true; }
if (o.draft != "dflash2" && o.draft != "mtp") {
std::fprintf(stderr, "strata generate: --draft takes mtp or dflash2\n");
std::fprintf(stderr, "strata generate: --draft takes mtp, dflash2 or auto\n");
std::exit(2);
}
}
else if (a == "--draft-rule") o.draft_rule = next("--draft-rule");
else if (a == "--draft-mtp-spec") o.draft_mtp_spec = std::max(2, std::atoi(next("--draft-mtp-spec")));
else if (a == "--draft-mtp-min-p") o.draft_mtp_min_p = std::clamp(std::atof(next("--draft-mtp-min-p")), 0.0, 1.0);
else if (a == "--draft-model") o.draft_model = next("--draft-model");
else if (a == "--draft-conf") o.draft_conf = std::clamp(std::atof(next("--draft-conf")), 0.0, 1.0);
else if (a == "--draft-cap") o.draft_cap = std::max(0, std::atoi(next("--draft-cap")));
@@ -12768,12 +12780,23 @@ int main(int argc, char** argv) {
std::string line;
int64_t rounds = 0;
const int S = o.spec;
const int S_mtp = o.mtp_max_t > 0 ? std::min(o.mtp_max_t, S) : S; // the MTP's windows; suffixes go up to S
const int S_mtp_wide = o.mtp_max_t > 0 ? std::min(o.mtp_max_t, S) : S; // the MTP's windows; suffixes go up to S
// --draft auto: a request that drafts with the MTP layer has its own widest window (the MTP loses to itself above 4 on Blackwell), the DFlash2 requests S_mtp_wide
const int S_mtp_narrow = o.draft_auto ? std::min(S_mtp_wide, std::max(2, o.draft_mtp_spec)) : S_mtp_wide;
int S_mtp = S_mtp_wide; // this request's (set when it starts)
int S_look = S; // --draft auto: the widest window a prompt-lookup draft may make for this request (the MTP requests keep the MTP's width, as without DFlash2)
if (use_mtp && S_mtp < S) mtp.set_max_drafts(S_mtp - 1);
// --pipeline-windows 2 loads the drafter with 8 rows: the serial loop's chain keeps its own length
else if (use_mtp && mtp.max_t() > S_mtp) mtp.set_max_drafts(S_mtp - 1);
strata::spec::SuffixDrafter sfx(std::max(1, o.suffix_draft), 64, (size_t) o.max_context + 4096);
strata::spec::DraftPolicy policy(S); // MTP or lookup window, learned over the whole process
strata::spec::DraftPolicy policy_idle(S); // --draft auto: the other drafter's learned costs; swapped in with it when a request uses the other drafter
bool policy_is_df2 = use_df2; // which drafter's costs `policy` holds (the engine's first drafter; swapped with policy_idle when a request uses the other one)
struct DraftBandit { double tps[2] = {0, 0}; int n[2] = {0, 0}; int64_t reqs = 0; }; // [1] = DFlash2, [0] = MTP: decode tok/s of finished requests (--draft-rule learn)
std::unordered_map<uint64_t, DraftBandit> bandit;
static const bool draft_log = std::getenv("STRATA_DRAFT_LOG") != nullptr; // one stderr line per request: drafter, tokens, rounds, ms, upkeep ms
static const bool draft_keep_off = std::getenv("STRATA_DRAFT_KEEP") != nullptr && std::atoi(std::getenv("STRATA_DRAFT_KEEP")) == 0; // diagnostic: do not keep the idle drafter current
int64_t draft_req_count = 0;
const bool spec_cost = [] { const char* v = std::getenv("STRATA_SPEC_COST"); return v != nullptr && v[0] == '1'; }(); // cost-aware MTP window (A/B arm)
strata::spec::DraftPolicy::ChainState chain_st;
int chain_rows = 1; // the window the cost-aware rule kept in the MTP chain just drafted
@@ -14334,6 +14357,8 @@ int main(int argc, char** argv) {
// tuning keys (setup's calibration measures settings without restarting the engine): the PCIe share of
// the missed experts and the draft-probability floor, for this request only
double req_pcie_frac = o.pcie_frac, req_spec_min_p = o.spec_min_p;
std::string req_draft; // draft=mtp|dflash2 (--draft auto): which drafter this request uses
bool req_min_p_set = false;
double req_pipe = -1.0; // --prefill-pipe for this request (setup's calibration of a layer split)
int req_aux_cpus = -1; // aux_cpus=0|1: --aux-cpus for this request (-1 = the start-up value)
// #2228: img_keys=<hex>,<hex>,... (GENI): each picture's key from the server (its normalized bytes' hash),
@@ -14380,7 +14405,8 @@ int main(int argc, char** argv) {
else if (key == "penalty_present") req_penalty_present = fv;
else if (key == "seed") req_seed = std::strtoull(tok.c_str() + eq + 1, nullptr, 10);
else if (key == "pcie_frac") { req_pcie_frac = std::clamp((double) fv, 0.0, 1.0); req_pcie_set = true; }
else if (key == "spec_min_p") req_spec_min_p = std::clamp((double) fv, 0.0, 1.0);
else if (key == "spec_min_p") { req_spec_min_p = std::clamp((double) fv, 0.0, 1.0); req_min_p_set = true; }
else if (key == "draft") req_draft = tok.substr(eq + 1);
else if (key == "pipe") req_pipe = std::max(0.0, (double) fv);
else if (key == "aux_cpus") req_aux_cpus = std::atoi(tok.c_str() + eq + 1) != 0 ? 1 : 0;
else if (key == "img_keys") {
@@ -14399,6 +14425,7 @@ int main(int argc, char** argv) {
}
}
strata::prefill::Prefill::set_pipe_request(req_pipe); // every request sets it: none carries over
bool req_df2 = use_df2; // --draft auto: this request's drafter, decided once the prompt is parsed (below)
std::vector<float> req_bias;
std::string bias_error;
if (req_bias_present && (o.batch > 0 ||
@@ -14420,6 +14447,31 @@ int main(int argc, char** argv) {
continue;
}
const int64_t n = (int64_t) ids.size();
// --draft auto: this request's drafter. Without the DFlash2 drafter loaded every request uses the MTP, as before.
uint64_t bandit_key = 0;
if (use_df2 && o.draft_auto) {
const std::string& d = !req_draft.empty() ? req_draft : o.draft_rule;
if (d == "mtp") req_df2 = false;
else if (d == "dflash2" || d == "df2") req_df2 = true;
else if (d == "learn") { // what this kind of request (its first prompt tokens: the system prompt of the app) did best so far, with some exploring
bandit_key = 1469598103934665603ull;
for (int64_t i = 1; i < std::min<int64_t>(n, 33); ++i) bandit_key = (bandit_key ^ (uint64_t) ids[(size_t) i]) * 1099511628211ull;
DraftBandit& b = bandit[bandit_key];
const int64_t r = b.reqs++;
if (b.n[1] < 2) req_df2 = true;
else if (b.n[0] < 2) req_df2 = false;
else {
const bool df_better = b.tps[1] >= 0.99 * b.tps[0]; // DFlash2 unless the MTP is clearly ahead
const int every = std::fabs(b.tps[1] / b.tps[0] - 1.0) < 0.04 ? 6 : 12; // the other one is tried now and then
req_df2 = (r % every == every - 1) ? !df_better : df_better;
}
}
if (!req_df2 && !req_min_p_set && o.draft_mtp_min_p >= 0.0) req_spec_min_p = o.draft_mtp_min_p;
if (req_df2 != policy_is_df2) { std::swap(policy, policy_idle); policy_is_df2 = req_df2; }
S_mtp = req_df2 ? S_mtp_wide : S_mtp_narrow;
S_look = req_df2 ? S : std::min(S, S_mtp_narrow);
if (use_mtp) mtp.set_max_drafts(S_mtp - 1);
}
if (req_prefill_only && (geni || o.batch > 0 || n < 2)) {
std::printf("ERR prefill=1 needs a text prompt of at least 2 tokens and no --batch\n");
continue;
@@ -16584,6 +16636,18 @@ int main(int argc, char** argv) {
// what the session holds once this request is done: the prompt read so far, then every committed token
std::vector<int32_t> consumed;
consumed.reserve((size_t) (n + max_new + S));
double keep_ms = 0; // --draft auto: host time spent queueing the idle drafter's upkeep
bool keep_pending = false;
auto keep_sync = [&]() -> bool { // the queued upkeep is done: the verifier may rewrite the taps / residual rows now
if (!keep_pending) return true;
keep_pending = false;
#if defined(STRATA_HAVE_DFLASH2)
if (use_df2 && !df2.context_wait(err)) return false;
#endif
if (use_mtp && cudaStreamSynchronize(mtp.stream()) != cudaSuccess) { err = "the MTP upkeep failed"; return false; }
return true;
};
const int64_t rounds_at_start = rounds;
for (int64_t i = 0; i < p; ++i) consumed.push_back((int32_t) ids[(size_t) i]);
// #4590: where the penalty window may start in `consumed` (see pen_prompt above): the end of the prompt without its
// last token, which joins the window as the fed-back head `x`.
@@ -17324,7 +17388,7 @@ int main(int argc, char** argv) {
while (T < S_mtp && dprob[(size_t) T - 1] >= (float) req_spec_min_p) ++T;
}
#if defined(STRATA_HAVE_DFLASH2)
if (use_df2 && o.draft_conf > 0.0 && !first_window) { // --draft-conf in serve as in the CLI loop: the chance that every draft so far holds
if (req_df2 && o.draft_conf > 0.0 && !first_window) { // --draft-conf in serve as in the CLI loop: the chance that every draft so far holds
T = 1;
double chain = 1.0;
while (T < S_mtp && (chain *= (double) dprob[(size_t) T - 1]) >= o.draft_conf) ++T;
@@ -17334,7 +17398,7 @@ int main(int argc, char** argv) {
// a repeat of earlier context (prompt lookup) where the MTP's own first guess agrees: the policy takes it
// when its expected tokens per ms, from the measured acceptance and window costs, beat the MTP window's
if (o.suffix_draft > 0 && !first_window) {
const int k = sfx.propose(S - 1, sbuf.data());
const int k = sfx.propose(S_look - 1, sbuf.data());
sfx_match = sfx.last_match();
// a probabilistic window must not condition the source on the draw: the policy copy fixed at the request's start
// chooses, and the lookup is taken without asking whether the MTP's first guess agrees
@@ -17358,7 +17422,7 @@ int main(int argc, char** argv) {
int csrc = -1;
const int nt = chain_tail(sfx, drafts.data(), T - 1, ctail);
chain_n = strata::spec::propose_from_sources(lookup_src, ctail.data(), nt, T - 1,
std::min(o.lookup_chain, S - T), o.lookup_chain_min,
std::min(o.lookup_chain, S_look - T), o.lookup_chain_min,
cbuf.data(), &cm, &csrc);
// the policy keeps the chained tokens that pay at this machine's measured window costs
if (chain_n > 0 && !chain_fixed) {
@@ -17383,6 +17447,7 @@ int main(int argc, char** argv) {
: from_sfx ? sbuf[(size_t) i - 1]
: drafts[(size_t) i - 1];
for (int i = 0; i < chain_n; ++i) window[(size_t) (T_mtp + i)] = cbuf[(size_t) i];
if (!keep_sync()) { std::printf("ERR %s\n", err.c_str()); return 1; }
drive.d.layers = 0;
drive.d.experts = 0;
drive.d.failed = false;
@@ -17624,6 +17689,19 @@ int main(int argc, char** argv) {
for (int i = 0; i <= a; ++i) consumed.push_back(window[(size_t) i]);
#if defined(STRATA_HAVE_DFLASH2)
if (use_df2) df2.note_rows(window.data(), a + 1, p);
// --draft auto: the drafter this request does not use keeps up with the commit (its context / its K/V), so a later request can use it
if (use_df2 && o.draft_auto && !merged && !draft_keep_off) {
const Clock::time_point tk0 = Clock::now();
// queued on the idle drafter's own stream, overlapped with the other's draft; keep_sync() below waits before the next window rewrites the taps / rows
if (!req_df2) {
if (!df2.context_async(ver.tap_dev(), a + 1, p, err)) { if (adapt_thr.joinable()) adapt_thr.join(); std::printf("ERR %s\n", err.c_str()); return 1; }
keep_pending = true;
} else if (use_mtp) {
if (!mtp.prefill(ver.final_R_all(), outv.data(), a + 1, p, err, false)) { if (adapt_thr.joinable()) adapt_thr.join(); std::printf("ERR %s\n", err.c_str()); return 1; }
keep_pending = true;
}
keep_ms += std::chrono::duration<double, std::milli>(Clock::now() - tk0).count();
}
#endif
if (!merged) { // (a merged window's rows are prompt tokens, not drafts)
draft_offered += T - 1;
@@ -17652,7 +17730,7 @@ int main(int argc, char** argv) {
generation = {}; // a skipped or failed production must not expose an older q view
const GatedDraftLimit draft_limit(mtp, gated_proposal, S_mtp, p + a + 1, o.max_context);
const bool drafted = !use_mtp || eos || produced_n >= max_new ||
(use_df2 ? draft_df2(a) : mtp.draft_long(T, outv.data(), p, a, drafts.data(), err, dprob.data(), (float) req_spec_min_p,
(req_df2 ? draft_df2(a) : mtp.draft_long(T, outv.data(), p, a, drafts.data(), err, dprob.data(), (float) req_spec_min_p,
nullptr, gated_proposal ? &generation : nullptr));
{
const Clock::time_point tw3 = Clock::now();
@@ -17716,7 +17794,19 @@ int main(int argc, char** argv) {
x = outv[(size_t) a];
p += a + 1;
}
if (!keep_sync()) { std::printf("ERR %s\n", err.c_str()); return 1; }
const double decode_ms = std::chrono::duration<double, std::milli>(Clock::now() - d0).count();
if (use_df2 && o.draft_auto && bandit_key != 0 && produced_n >= 48 && decode_ms > 0.0) {
DraftBandit& b = bandit[bandit_key];
const int w = req_df2 ? 1 : 0;
const double tps = 1000.0 * (double) produced_n / decode_ms;
b.tps[w] = b.n[w] == 0 ? tps : 0.65 * b.tps[w] + 0.35 * tps;
++b.n[w];
}
if (draft_log && use_df2)
std::fprintf(stderr, "strata draft: request %lld drafter=%s window<=%d tokens=%lld rounds=%lld decode_ms=%.1f keep_ms=%.1f (%.3f per round)\n",
(long long) ++draft_req_count, req_df2 ? "dflash2" : "mtp", S_mtp, (long long) produced_n, (long long) (rounds - rounds_at_start),
decode_ms, keep_ms, rounds > rounds_at_start ? keep_ms / (double) (rounds - rounds_at_start) : 0.0);
// the last commit (set_commit_async): the session is complete before anything reads or copies it
if (!ver.wait_commit(err)) {
exit_if_gpu_fatal(err);