From 3949737f1a933061764814983d1acd12dda98f55 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Sat, 31 Oct 2026 22:11:27 +0100 Subject: [PATCH 1/5] Trial fix (belongs to f-0291-bw-dense): fused_gr.cu ldg_v4 PTX asm and gr_v3 --resources used cuda-only names on HIP; plain load / no-op on HIP --- src/kernels/cuda/fused_gr.cu | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/kernels/cuda/fused_gr.cu b/src/kernels/cuda/fused_gr.cu index 96438447ad..54389dae11 100644 --- a/src/kernels/cuda/fused_gr.cu +++ b/src/kernels/cuda/fused_gr.cu @@ -736,9 +736,13 @@ constexpr int PR = LR + HC; // partial rows per (token, s // The same loads, the same values, the same arithmetic in the same order: bitwise the kernels without it (gr_v3_screen). // ld.global.nc as a volatile asm so the compiler cannot sink the load back to its first use, behind the barrier. __device__ __forceinline__ uint4 ldg_v4(const uint4* p) { +#if defined(__HIPCC__) + return *p; // (PTX does not exist on HIP; the plain load) +#else uint4 v; asm volatile("ld.global.nc.v4.u32 {%0,%1,%2,%3}, [%4];" : "=r"(v.x), "=r"(v.y), "=r"(v.z), "=r"(v.w) : "l"(p)); return v; +#endif } constexpr int TQ3 = N / 8 / 32; // uint4 weight chunks per lane in one stream's slice (10) @@ -2801,6 +2805,10 @@ static bool fused_gr_read_multi_slice(const FusedGrArgs* a, int n_tok, float* xn void fused_gr_v3_pf(int mask) { g_v3_pf_force = mask; } // The bench only: registers, spills, shared memory and resident blocks a multiprocessor of the v3 kernels (T = 4) on the current card, one line each. void fused_gr_v3_print_resources() { +#if defined(__HIPCC__) + std::printf("gr_v3 resources: not printed on HIP\n"); + return; +#else auto one = [](const char* name, const void* f, size_t dyn) { cudaFuncAttributes a; int blocks = 0; @@ -2822,6 +2830,7 @@ void fused_gr_v3_print_resources() { one("up T4 pf1 (batched)", (const void*) gr_up_v3_kernel<1, 4, true, 1>, 0); one("up T4 pf2 (weights)", (const void*) gr_up_v3_kernel<1, 4, true, 2>, 0); one("up T4 pf3", (const void*) gr_up_v3_kernel<1, 4, true, 3>, 0); +#endif } bool fused_gr_read_multi(const FusedGrArgs* a, int n_tok, float* xn_scratch, void* stream, unsigned long long* stamp_buf, int stamp_i0) { From 1ab4c2ce467a82dd0c2a31e3d06a72221f2c585b Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Sat, 31 Oct 2026 23:07:24 +0100 Subject: [PATCH 2/5] serve: --draft auto keeps both drafters current; a request picks the MTP or DFlash2 (draft= key, --draft-rule), own window width and policy per drafter --- src/program/generate.cpp | 59 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 54 insertions(+), 5 deletions(-) diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 1c1ce6c0eb..73126d2fb6 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -917,6 +917,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 @@ -1073,6 +1078,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" @@ -3278,11 +3285,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"))); @@ -12418,12 +12429,20 @@ 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) 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) + 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 @@ -13962,6 +13981,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=,,... (GENI): each picture's key from the server (its normalized bytes' hash), @@ -14008,7 +14029,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") { @@ -14027,6 +14049,17 @@ int main(int argc, char** argv) { } } strata::prefill::Prefill::set_pipe_request(req_pipe); // every request sets it: none carries over + // --draft auto: this request's drafter. Without the DFlash2 drafter loaded every request uses the MTP, as before. + bool req_df2 = use_df2; + 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; + 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; + if (use_mtp) mtp.set_max_drafts(S_mtp - 1); + } std::vector req_bias; std::string bias_error; if (req_bias_present && (o.batch > 0 || @@ -16132,6 +16165,8 @@ 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 consumed; consumed.reserve((size_t) (n + max_new + S)); + double keep_ms = 0; // --draft auto: time spent keeping the idle drafter current + const int64_t rounds_at_start = rounds; for (int64_t i = 0; i < p; ++i) consumed.push_back((int32_t) ids[(size_t) i]); const char* finish = "length"; const Clock::time_point d0 = Clock::now(); @@ -16869,7 +16904,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; @@ -17156,6 +17191,16 @@ 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(); + if (!req_df2) { + if (!df2.context(ver.tap_dev(), a + 1, p, err)) { if (adapt_thr.joinable()) adapt_thr.join(); std::printf("ERR %s\n", err.c_str()); return 1; } + } else if (use_mtp) { + if (!mtp.prefill(ver.final_R_all(), outv.data(), a + 1, p, err)) { if (adapt_thr.joinable()) adapt_thr.join(); std::printf("ERR %s\n", err.c_str()); return 1; } + } + keep_ms += std::chrono::duration(Clock::now() - tk0).count(); + } #endif if (!merged) { // (a merged window's rows are prompt tokens, not drafts) draft_offered += T - 1; @@ -17184,7 +17229,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(T, outv.data(), p, a, drafts.data(), err, dprob.data(), (float) req_spec_min_p, + (req_df2 ? draft_df2(a) : mtp.draft(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(); @@ -17249,6 +17294,10 @@ int main(int argc, char** argv) { p += a + 1; } const double decode_ms = std::chrono::duration(Clock::now() - d0).count(); + 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)) { std::printf("ERR %s\n", err.c_str()); From 7a498a05b4d244cad1d1ac00a8395ad3303afaac Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Sat, 31 Oct 2026 23:29:51 +0100 Subject: [PATCH 3/5] serve --draft auto: the idle drafter's upkeep is queued on its own stream and waited for before the next window (DFlash2 context_async / context_wait, MTP prefill without sync) --- include/strata/core/dflash2.hpp | 5 +++++ src/core/dflash2.cpp | 25 +++++++++++++++++++++++++ src/program/generate.cpp | 21 ++++++++++++++++++--- 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/include/strata/core/dflash2.hpp b/include/strata/core/dflash2.hpp index c536b76a6d..770de18280 100644 --- a/include/strata/core/dflash2.hpp +++ b/include/strata/core/dflash2.hpp @@ -59,6 +59,10 @@ public: void note_rows(const int32_t* toks, int64_t n, int64_t pos0); /// Context rows without a draft: `n` rows of taps (device, [n][5 * n_embd]) for positions pos0 ... pos0 + n - 1. bool context(const float* taps_dev, int64_t n, int64_t pos0, std::string& err); + /// The same without the wait: the rows are queued on the drafter's stream (n <= kRows) and a later context_wait() (or the next draft / context call, which + /// waits first) finds them done. The taps must stay as they are until then. --draft auto keeps the idle drafter current this way, overlapped with the other's draft. + bool context_async(const float* taps_dev, int64_t n, int64_t pos0, std::string& err); + bool context_wait(std::string& err); /// One round: the new context rows (n_new in 1..8: the committed window rows, taps device [n_new][5 * n_embd], positions pos0 ...), /// then the block for `anchor` (the last committed token, at pos0 + n_new). `drafts` / `probs` get kDrafts entries (the /// selector's pick and its probability). The taps pointer must be the same device buffer on every call (graphs are captured on it). @@ -142,6 +146,7 @@ private: int64_t have_ctx_ = 0; std::vector hist_; ///< the token of every position the ring was filled for (serve) int64_t ring_hi_ = 0; ///< one past the highest position written in this epoch + bool ctx_pending_ = false; ///< context_async() queued rows nobody has waited for yet int32_t epoch_ = 0; ///< stamped into the ring tags: a new epoch makes every older slot invalid int64_t* tag_ = nullptr; ///< per ring slot: (epoch << 32 | position) of the row it holds, -1 none cudaEvent_t ev_a_ = nullptr, ev_b_ = nullptr; diff --git a/src/core/dflash2.cpp b/src/core/dflash2.cpp index 8c0839cd0c..04d4a8a169 100644 --- a/src/core/dflash2.cpp +++ b/src/core/dflash2.cpp @@ -514,6 +514,7 @@ void Dflash2Drafter::note_rows(const int32_t* toks, int64_t n, int64_t pos0) { } bool Dflash2Drafter::context(const float* taps, int64_t n, int64_t pos0, std::string& err) { + if (!context_wait(err)) return false; const OnDevice on_device(device_); const int tap_n = (int) taps_.size() * N_; for (int64_t r0 = 0; r0 < n; r0 += kRows) { @@ -530,8 +531,32 @@ bool Dflash2Drafter::context(const float* taps, int64_t n, int64_t pos0, std::st return true; } +bool Dflash2Drafter::context_async(const float* taps, int64_t n, int64_t pos0, std::string& err) { + if (n < 1 || n > kRows) { err = "dflash2: context_async rows out of range"; return false; } + if (!context_wait(err)) return false; // h_meta_ is read by the queued copy: the previous call must be done + const OnDevice on_device(device_); + for (int i = 0; i < (int) n; ++i) h_meta_[i] = (int32_t) (pos0 + i); + h_meta_[18] = epoch_; + std::atomic_thread_fence(std::memory_order_seq_cst); + kernels::copy_i32_from_mapped(d_meta_, m_meta_, 32, cs_); + if (!record_context(taps, (int) n, cs_, err)) return false; + ctx_pending_ = true; + have_ctx_ = std::max(have_ctx_, pos0 + n); + ring_hi_ = std::max(ring_hi_, pos0 + n); + return true; +} + +bool Dflash2Drafter::context_wait(std::string& err) { + if (!ctx_pending_) return true; + ctx_pending_ = false; + const OnDevice on_device(device_); + if (cudaStreamSynchronize(cs_) != cudaSuccess) { err = std::string("dflash2: ") + cudaGetErrorString(cudaGetLastError()); return false; } + return true; +} + bool Dflash2Drafter::draft(const float* taps, int n_new, int64_t pos0, int32_t anchor, int32_t* drafts, float* probs, std::string& err) { if (!bound_) { err = "dflash2: not bound"; return false; } + if (!context_wait(err)) return false; if (n_new < 1 || n_new > kRows) { err = "dflash2: draft rows out of range"; return false; } const OnDevice on_device(device_); const Clock::time_point t0 = Clock::now(); diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 73126d2fb6..e3831a4311 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -16165,7 +16165,17 @@ 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 consumed; consumed.reserve((size_t) (n + max_new + S)); - double keep_ms = 0; // --draft auto: time spent keeping the idle drafter current + 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]); const char* finish = "length"; @@ -16953,6 +16963,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; @@ -17194,10 +17205,13 @@ int main(int argc, char** argv) { // --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(ver.tap_dev(), a + 1, p, err)) { if (adapt_thr.joinable()) adapt_thr.join(); std::printf("ERR %s\n", err.c_str()); return 1; } + 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)) { if (adapt_thr.joinable()) adapt_thr.join(); std::printf("ERR %s\n", err.c_str()); return 1; } + 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(Clock::now() - tk0).count(); } @@ -17293,6 +17307,7 @@ 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(Clock::now() - d0).count(); 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", From 1db96cda5f3997be8b5e94ba4611318b7b831f22 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Sat, 31 Oct 2026 23:32:24 +0100 Subject: [PATCH 4/5] serve --draft auto: --draft-rule learn picks per kind of request (first prompt tokens) the drafter with the better measured decode speed, with exploring --- src/program/generate.cpp | 46 ++++++++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/src/program/generate.cpp b/src/program/generate.cpp index e3831a4311..7895cac6ea 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -172,6 +172,7 @@ bool window_slot_copy_main(bool into_slot, int slot, int64_t upto, bool kv_only, #include #include #include +#include #include // logprobs=K / allowed=...: one `LP` line for the token just emitted, from `ver`'s head @@ -12440,6 +12441,8 @@ int main(int argc, char** argv) { 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 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; @@ -14049,17 +14052,7 @@ int main(int argc, char** argv) { } } strata::prefill::Prefill::set_pipe_request(req_pipe); // every request sets it: none carries over - // --draft auto: this request's drafter. Without the DFlash2 drafter loaded every request uses the MTP, as before. - bool req_df2 = use_df2; - 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; - 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; - if (use_mtp) mtp.set_max_drafts(S_mtp - 1); - } + bool req_df2 = use_df2; // --draft auto: this request's drafter, decided once the prompt is parsed (below) std::vector req_bias; std::string bias_error; if (req_bias_present && (o.batch > 0 || @@ -14081,6 +14074,30 @@ 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(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; + 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; @@ -17309,6 +17326,13 @@ int main(int argc, char** argv) { } if (!keep_sync()) { std::printf("ERR %s\n", err.c_str()); return 1; } const double decode_ms = std::chrono::duration(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), From 4c5a55491240cd616300e1cfdb2e2187e0ea3dee Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Sun, 1 Nov 2026 03:28:12 +0100 Subject: [PATCH 5/5] IQ4_XS 5-column interleaved MMVQ runs as 6 columns with 5 stored (STRATA_IL_PAD, bitwise); --draft auto: MTP requests keep the MTP's prompt-lookup width --- src/kernels/cuda/native_mmvq.cu | 28 ++++++++++++++++++++++------ src/program/generate.cpp | 6 ++++-- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/kernels/cuda/native_mmvq.cu b/src/kernels/cuda/native_mmvq.cu index 0f2bdf7dc0..3bcb440f5d 100644 --- a/src/kernels/cuda/native_mmvq.cu +++ b/src/kernels/cuda/native_mmvq.cu @@ -2151,7 +2151,9 @@ __global__ void native_mmvq_il_pf_kernel(const typename I::F::Block* __restrict_ } } -template +// NOUT < NC (BW-NEXT, STRATA_IL_PAD): the kernel of NC columns computes a column that is not wanted and stores only the first NOUT; each column's value is +// its own sum, so the stored ones are bitwise those of the NOUT-column kernel. 5 columns of IQ4_XS run as 6 (the 5-column instantiation is 15-40% slower). +template __launch_bounds__(4 * WARP) __global__ void native_mmvq_il_kernel(const typename I::F::Block* __restrict__ w, const int* __restrict__ xq, const float* __restrict__ xd, float* __restrict__ y, int n_in, int n_out) { @@ -2199,7 +2201,7 @@ __global__ void native_mmvq_il_kernel(const typename I::F::Block* __restrict__ w s += acc[2][i][c]; s += acc[3][i][c]; s = warp_sum(s); - if (lane == 0 && row0 + i < n_out) y[std::size_t(c) * n_out + row0 + i] = s; + if (c < NOUT && lane == 0 && row0 + i < n_out) y[std::size_t(c) * n_out + row0 + i] = s; } } } @@ -2222,10 +2224,10 @@ int il_pf_for(int type, int ncols, int n_out, int* rows) { return t >> 4; } // one (type, columns, rows) launch: today's kernel, or the variant il_pf_for names for a type that has them -template +template void launch_il_one(unsigned blocks, const typename I::F::Block* w, const int* xq, const float* xd, float* y, int n_in, int n_out, cudaStream_t s, int pf) { - if constexpr (I::HAS_PF) { + if constexpr (I::HAS_PF && NOUT == NC) { switch (pf) { case 6: native_mmvq_il_pf_kernel<<>>(w, xq, xd, y, n_in, n_out); return; case 2: native_mmvq_il_pf_kernel<<>>(w, xq, xd, y, n_in, n_out); return; @@ -2235,7 +2237,13 @@ void launch_il_one(unsigned blocks, const typename I::F::Block* w, const int* xq default: break; } } - native_mmvq_il_kernel<<>>(w, xq, xd, y, n_in, n_out); + native_mmvq_il_kernel<<>>(w, xq, xd, y, n_in, n_out); +} + +// STRATA_IL_PAD (bit 0: IQ4_XS 5 columns as 6; default on, 0 off): bitwise, see native_mmvq_il_kernel +int il_pad() { + static const int v = [] { const char* e = std::getenv("STRATA_IL_PAD"); return e != nullptr ? std::atoi(e) : 1; }(); + return v; } template @@ -2255,8 +2263,16 @@ void launch_il(const void* weights, const void* x_il, float* y, int n_in, int n_ #undef STRATA_IL_CASE // 147-verify: 5..8 columns (the cp = 8 copy) at 1 or 2 rows a warp; launch_il_rows maps 4 rows to 2 there #define STRATA_IL_CASE(N) case N: if constexpr (R <= 2) launch_il_one(blocks, w, xq, xd, y, n_in, n_out, s, pf); break; - STRATA_IL_CASE(5) STRATA_IL_CASE(6) STRATA_IL_CASE(7) STRATA_IL_CASE(8) + STRATA_IL_CASE(6) STRATA_IL_CASE(7) STRATA_IL_CASE(8) #undef STRATA_IL_CASE + case 5: + if constexpr (R <= 2) { + if constexpr (std::is_same_v) { + if ((il_pad() & 1) != 0) { launch_il_one(blocks, w, xq, xd, y, n_in, n_out, s, 0); break; } + } + launch_il_one(blocks, w, xq, xd, y, n_in, n_out, s, pf); + } + break; default: throw std::invalid_argument("native_mmvq_il requires 1 <= ncols <= 8"); } } diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 7895cac6ea..cdb238b946 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -12434,6 +12434,7 @@ int main(int argc, char** argv) { // --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); @@ -14096,6 +14097,7 @@ int main(int argc, char** argv) { 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)) { @@ -16941,7 +16943,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 @@ -16960,7 +16962,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) {