From f6e4cd689964f9526bc12f8395c425c5253d0225 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Fri, 23 Oct 2026 17:47:43 +0200 Subject: [PATCH 01/13] dsv41: chunked prompt for a single card (--prefill-chunk N), the expert cache learns from chunks and catches up after a prompt, chunk buffers fall back by halving, CLI feeds a whole prompt The layer-major chunked prompt path only ran with --threads on / --tp > 1. --prefill-chunk N (N > 0) now enables it on one card too (kChunkedPromptDefault stays false until the decode gate is signed off). A chunk's routing bumps the layers' heat and CPU-lane hits, and cache_settle() runs the swap rule in rounds after the prompt so the decode that follows finds the hot experts in VRAM (8K prompt: 1699 swaps, 5.6 s). If the chunk buffers do not fit, the chunk is halved down to 256 rows, then windows of 16 with one line saying so. ds4_main: --window 0 (default) feeds a whole prompt when the chunk path is on. --- src/dsv41/ds4_main.cpp | 5 ++- src/dsv41/ds4_model.cu | 91 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 92 insertions(+), 4 deletions(-) diff --git a/src/dsv41/ds4_main.cpp b/src/dsv41/ds4_main.cpp index dca4e18c..9fedb893 100644 --- a/src/dsv41/ds4_main.cpp +++ b/src/dsv41/ds4_main.cpp @@ -736,7 +736,7 @@ int spec_eval(strata::ds4::Model& m, const std::string& ids_path, int n, int win int main(int argc, char** argv) { strata::ds4::Options o; std::string ids_s, dump, eos = "1"; - int n = 0, window = 16; + int n = 0, window = 0; // (0: the model's feed unit - a whole prompt on the chunked path; 16 for the scoring runs) bool spec = true; float temp = 0.0f; // the CLI's sampling (0: greedy) unsigned long long cli_seed = 1; @@ -834,6 +834,8 @@ int main(int argc, char** argv) { } if (!o.vision_only) std::fprintf(stderr, "%s", m.describe().c_str()); if (serve_mode) return serve(m, eos, spec, o.dspark_conf); + const int window_given = window; + if (window == 0) window = 16; // (the scoring / profiling runs; the plain run below takes the model's feed unit) if (!prof_ids.empty()) return profile_run(m, prof_ids, window); if (!spec_ids.empty()) return spec_eval(m, spec_ids, n, window, o.dspark_conf); if (!score_ids.empty()) return score(m, score_ids, score_out.empty() ? score_ids + ".tsv" : score_out, window, std::atoi(eos.c_str())); @@ -887,6 +889,7 @@ int main(int argc, char** argv) { } std::FILE* df = dump.empty() ? nullptr : std::fopen(dump.c_str(), "wb"); std::vector lg; + if (window_given == 0) window = df != nullptr ? 16 : std::max(16, std::min(m.feed_unit(), 1 << 30)); const double t0 = now_ms(); for (size_t a = 0; a < ids.size();) { const int T = (int) std::min((size_t) window, ids.size() - a); diff --git a/src/dsv41/ds4_model.cu b/src/dsv41/ds4_model.cu index 154a8af4..8c4c0cbb 100644 --- a/src/dsv41/ds4_model.cu +++ b/src/dsv41/ds4_model.cu @@ -791,6 +791,7 @@ struct Model::Impl { unsigned win_seq = 0; // the windows the threads ran (the hand-offs' seq) // the chunked prompt path (threads mode): chunk tokens a pass, runs shorter than chunk_min in windows int chunk = 0, chunk_min = 512; + static constexpr bool kChunkedPromptDefault = false; // flip once the chunk buffers are elastic (decode unchanged) static constexpr int kFlowChunkMin = 2; // chunk_min with every device's layers on the flow (see load) // --prefill-chunk at most (route() runs in kVlRows-row slabs). The default stays kVlRows: a 16384-token chunk // reads a 16K prompt's experts once instead of twice (V100: 461.8 -> 321.3 s, 736 -> 506 GB read) but its buffers @@ -1829,7 +1830,13 @@ struct Model::Impl { // prompt path (its own thread a GPU) as asked for - also where the experts' blocks took the groups' width down // to 1 (a 256-value type's: Maya's quants), which left every prompt in windows of 16 par = o.threads || tp > 1; - prompt_par = par || o.tp > 1; + // the chunked (layer-major) prompt path: with --tp / --threads as before; --prefill-chunk N (N > 0) or + // kChunkedPromptDefault bring it to a single card's default run too (a host thread a GPU for the prompt only; + // decode stays as asked). --prefill-chunk 0 keeps the windows of 16 (DS4_WINDOWS=1 the same). Off by default + // until a prompt costs the decode nothing: its buffers hold VRAM the windowed path gives to experts + prompt_par = par || o.tp > 1 || + ((o.prefill_chunk > 0 || (o.prefill_chunk < 0 && kChunkedPromptDefault)) && + std::getenv("DS4_WINDOWS") == nullptr); const bool tp_all = tp > 1 && tp == (int) devs.size(); for (size_t i = 0; i < devs.size();) { Group G; @@ -2413,7 +2420,54 @@ struct Model::Impl { } // the chunked prompt path's buffers (before the experts: they take what is left) - if (chunk > 0 && !alloc_chunks(err)) return false; + if (chunk > 0) { + // recommend, never force: a chunk the cards cannot hold in buffers is halved (down to 256 rows) and then the + // prompt runs in windows of 16, said in one line - the load only fails for a reason that is not this + std::vector na0, sc0; + for (Dev& d : devs) { + na0.push_back(d.allocs.size()); + sc0.push_back(d.scratch); + } + const size_t nh = host_allocs.size(); + std::string aerr; + while (chunk > 0 && !alloc_chunks(aerr)) { + for (size_t di = 0; di < devs.size(); ++di) { + Dev& d = devs[di]; + cudaSetDevice(d.id); + cudaDeviceSynchronize(); + if (di < chunks.size() && chunks[di]) { + if (chunks[di]->blas) cublasDestroy(chunks[di]->blas); + if (chunks[di]->cs) cudaStreamDestroy(chunks[di]->cs); + } + while (d.allocs.size() > na0[di]) { + cudaFree(d.allocs.back()); + d.allocs.pop_back(); + } + d.scratch = sc0[di]; + } + while (host_allocs.size() > nh) { + cudaFreeHost(host_allocs.back()); + host_allocs.pop_back(); + } + chunks.clear(); + const int was = chunk; + chunk = chunk > 512 ? chunk / 2 / 16 * 16 : 0; + if (chunk > 0) { + eg_cap = (size_t) chunk; + std::fprintf(stderr, "ds4: the prompt path's buffers for chunks of %d rows did not fit (%s): trying %d\n", + was, aerr.c_str(), chunk); + } else { + prompt_par = false; + eg_cap = (size_t) kWin; + tap_cap = kWin; + std::fprintf(stderr, "ds4: the prompt path's buffers did not fit (%s): prompts run in windows of %d " + "tokens (slower on long prompts; more free GPU memory or a lower --reserve-mib " + "brings the chunks back)\n", aerr.c_str(), kWin); + } + aerr.clear(); + } + if (chunk > 0 && aerr.empty()) err.clear(); + } // the CPU lane's experts read in place (decided above): how much of them the load reads ahead, from what VRAM // now leaves over if (file_experts) { @@ -5267,6 +5321,31 @@ struct Model::Impl { } } + // after a chunked prompt: the expert cache catches up with what the prompt picked at once - a window prompt gets + // its swaps one tick at a time over its hundreds of windows, a chunked one has a single tick - so the decode that + // follows finds the hot experts in VRAM. Rounds of the ordinary swap rule (the hottest CPU-lane pick of each layer + // in, its coldest VRAM expert out) until nothing is worth moving or DS4_SETTLE_MS (6000) is spent + void cache_settle() { + if (!o.expert_cache || cache_ths.empty()) return; + static const double budget = env_f("DS4_SETTLE_MS", 6000.0f); + if (budget <= 0.0) return; + const double t0 = now_ms(); + const int64_t s0 = st.swaps; + while (now_ms() - t0 < budget) { + for (Layer& l : L) { + if (l.mtp || l.spare_vram < 0 || l.swap_pending) continue; + l.cpu_hits.clear(); + for (int e = 0; e < l.n_exp; ++e) + if (l.slot[(size_t) e] < 0 && l.heat[(size_t) e] >= 2.0f) l.cpu_hits.push_back(e); + } + cache_tick_swaps(0); + if (sw_inflight == 0) break; // (nothing queued: every layer's best CPU-lane expert is not hotter than its coldest VRAM one) + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } + if (o.verbose && st.swaps > s0) + std::fprintf(stderr, "ds4: expert cache: %lld swaps after the prompt (%.0f ms)\n", (long long) (st.swaps - s0), + now_ms() - t0); + } void cache_tick(int T) { cache_tick_swaps(T); store_sync(); @@ -6352,7 +6431,12 @@ struct Model::Impl { uint32_t* pr = prof.data() + (size_t) l.il * g.n_exp; for (int e = 0; e < l.n_exp; ++e) { const int v = L0.vs_pin[e]; - pr[e] += (uint32_t) (b[(size_t) v + 1] - b[(size_t) v]); + const int cnt = b[(size_t) v + 1] - b[(size_t) v]; + pr[e] += (uint32_t) cnt; + if (cnt > 0) { // (the expert cache learns from a chunked prompt as from windows: heat, and the CPU lane's hits) + l.heat[(size_t) e] += (float) cnt; + if (l.slot[(size_t) e] < 0) l.cpu_hits.push_back(e); + } } st.experts += (int64_t) T * l.k_exp; } @@ -7216,6 +7300,7 @@ struct Model::Impl { pos += n; st.tokens += n; cache_tick(n); + cache_settle(); const int nr = std::min(T_last, tap_cap); if (!seed_drafter(pos - nr, nr, 0, err)) return false; if (snap_slot >= 0) { // checkpoint `snap_slot` from what the layers kept (the drafter's blocks: as they are - From d2411af9fdbdfb28f169b2c10ee7bd4f36d9d762 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Fri, 23 Oct 2026 18:04:51 +0200 Subject: [PATCH 02/13] dsv41: i-quant CPU-lane entries take a whole window's tokens of an expert (cap 16, was 8) for the exact multi-token rows; the chunk staging ring is 64 MB where the cards hold 90% of the experts (decode after a chunked prompt back to the windowed path's speed) --- src/dsv41/ds4_model.cu | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/src/dsv41/ds4_model.cu b/src/dsv41/ds4_model.cu index 8c4c0cbb..07219aaf 100644 --- a/src/dsv41/ds4_model.cu +++ b/src/dsv41/ds4_model.cu @@ -2667,6 +2667,22 @@ struct Model::Impl { return (size_t) chunk * ((size_t) g.hc * g.dim * 4 + 8 * 4 + topk_stride() * 4 + cand_stride() * 4); } + // the staging ring's size (MB) for the experts a chunk streams from the CPU lane: 400 where a good part of them are + // streamed; 64 where the cards hold at least 90% of the experts (a 4-card box) - the ring's VRAM is then worth more + // as expert slots, and the decode after a prompt keeps the windowed path's speed (R9700 x4: 29.6 -> 31.7 tok/s) + size_t ring_mb_auto() const { + double exp_b = 0, other_b = 0, room = 0; + for (const auto& kt : pk.t) { + const std::string& n = kt.first; + if (n.find(".engram.embed.") != std::string::npos || n.rfind("vision.", 0) == 0 || n == "embed.weight" || + (!o.dspark && n.rfind("mtp.", 0) == 0)) + continue; + (n.find(".ffn.experts.") != std::string::npos ? exp_b : other_b) += (double) kt.second.bytes; + } + for (const Dev& d : devs) + room += std::max(0.0, (double) d.free0 - (double) ((size_t) o.reserve_mib << 20) - 1.5 * (1 << 30)) * o.vram_experts; + return exp_b > 0 && exp_b - std::max(0.0, room - other_b) <= 0.1 * exp_b ? 64 : 400; + } bool alloc_chunks(std::string& err) { chunks.resize(devs.size()); const int D = g.dim, H = g.hc, HD = g.hd; @@ -2725,7 +2741,7 @@ struct Model::Impl { return false; } if (stride > 0) { // DS4_RING_MB (400) of slots in kParts batches - const size_t mb = std::getenv("DS4_RING_MB") ? (size_t) std::atoi(std::getenv("DS4_RING_MB")) : 400; + const size_t mb = std::getenv("DS4_RING_MB") ? (size_t) std::atoi(std::getenv("DS4_RING_MB")) : ring_mb_auto(); c.ring_stride = stride; c.ring_n = std::max(Chunk::kParts, (int) ((mb << 20) / stride) / Chunk::kParts * Chunk::kParts); if (!dalloc(d, c.ring, (size_t) c.ring_n * stride, err)) return false; @@ -4834,8 +4850,14 @@ struct Model::Impl { // pieces; the down bands). yd[slot] gets each row's expert output. struct Ent { int e = 0, n = 0, slot0 = 0; - int tok[kc::kExl3CpuMaxN] = {}; + int tok[kEntMax] = {}; }; + // tokens an entry holds: the i-quant GGUF experts take a whole window's tokens of an expert in one call (the CPU + // lane's exact multi-token rows, 7+ tokens an expert: decoded once for all), the others (MXFP4, EXL3) up to 8 + static constexpr int kEntMax = kWin > kc::kExl3CpuMaxN ? kWin : kc::kExl3CpuMaxN; + static int ent_cap(const Layer& l) { + return l.lay.fmt == gg::kFmtMXFP4 && !gg::mx_experts(l.lay) ? kEntMax : kc::kExl3CpuMaxN; + } void run_nodes(int n, const std::function* f) { for (int X = 0; X < NH; ++X) pools[(size_t) X]->start(n, f[X]); pools[0]->help(); @@ -4982,8 +5004,8 @@ struct Model::Impl { f[X] = [&, X](int j) { const Ent& en = ents[(size_t) (ord != nullptr ? ord[j / GB] : j / GB)]; const int r0 = X * FFh + (j % GB) * RB, r1 = std::min(X * FFh + FFh, r0 + RB); - const void* ap[gg::kMxCpuMaxN]; - float* hp[gg::kMxCpuMaxN]; + const void* ap[kEntMax]; + float* hp[kEntMax]; for (int i = 0; i < en.n; ++i) { ap[i] = x_act(en, i); hp[i] = c_xhd.data() + (size_t) (en.slot0 + i) * FF + (size_t) X * FFh; // (part X's rows of h) @@ -5002,8 +5024,8 @@ struct Model::Impl { f[X] = [&, X](int j) { const Ent& en = ents[(size_t) (ord != nullptr ? ord[j / DB] : j / DB)]; const int r0 = X * Eh + (j % DB) * RB, r1 = std::min(X * Eh + Eh, r0 + RB); - const void* hp[gg::kMxCpuMaxN]; - float* yp[gg::kMxCpuMaxN]; + const void* hp[kEntMax]; + float* yp[kEntMax]; for (int i = 0; i < en.n; ++i) { hp[i] = aq + (size_t) (kWin + en.slot0 + i) * E * 4; yp[i] = c_yd.data() + (size_t) (en.slot0 + i) * E + (size_t) X * Eh; // (part X's rows of it) @@ -5675,7 +5697,7 @@ struct Model::Impl { en.e = picks[a].first; en.slot0 = nslots; en.n = 0; - while (a < picks.size() && picks[a].first == en.e && en.n < kc::kExl3CpuMaxN) { + while (a < picks.size() && picks[a].first == en.e && en.n < ent_cap(l)) { const int t = picks[a].second / kPicks, i = picks[a].second % kPicks; en.tok[en.n++] = t; tok_of[nslots] = t; @@ -8086,11 +8108,11 @@ struct Model::Impl { } ++nvex; } else { - for (size_t a = 0; a < pe.size(); a += kc::kExl3CpuMaxN) { + for (size_t a = 0; a < pe.size(); a += (size_t) ent_cap(l)) { Ent en; en.e = e; en.slot0 = nslots; - en.n = (int) std::min(kc::kExl3CpuMaxN, pe.size() - a); + en.n = (int) std::min((size_t) ent_cap(l), pe.size() - a); for (int i = 0; i < en.n; ++i) { en.tok[i] = pe[a + (size_t) i].first; cpu_rows.push_back({pe[a + (size_t) i].first, h_w[pe[a + (size_t) i].first * kPicks + pe[a + (size_t) i].second]}); From b43b0bb518cd2baa80898a74567a0d09a48175c0 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Fri, 23 Oct 2026 18:06:26 +0200 Subject: [PATCH 03/13] dsv41: kEntMax declared before Ent --- src/dsv41/ds4_model.cu | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dsv41/ds4_model.cu b/src/dsv41/ds4_model.cu index 07219aaf..91278f20 100644 --- a/src/dsv41/ds4_model.cu +++ b/src/dsv41/ds4_model.cu @@ -4848,13 +4848,13 @@ struct Model::Impl { // in four rounds (the gate / up split-K pieces, each preparing its two input blocks of every token; each band's // sums / codebook / Hadamard / swiglu and that block of the down input, which both nodes then read whole; the down // pieces; the down bands). yd[slot] gets each row's expert output. + static constexpr int kEntMax = kWin > kc::kExl3CpuMaxN ? kWin : kc::kExl3CpuMaxN; struct Ent { int e = 0, n = 0, slot0 = 0; int tok[kEntMax] = {}; }; // tokens an entry holds: the i-quant GGUF experts take a whole window's tokens of an expert in one call (the CPU // lane's exact multi-token rows, 7+ tokens an expert: decoded once for all), the others (MXFP4, EXL3) up to 8 - static constexpr int kEntMax = kWin > kc::kExl3CpuMaxN ? kWin : kc::kExl3CpuMaxN; static int ent_cap(const Layer& l) { return l.lay.fmt == gg::kFmtMXFP4 && !gg::mx_experts(l.lay) ? kEntMax : kc::kExl3CpuMaxN; } From 4cca3fac20a64e46c002610b6bfb23f8cb4737d7 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Fri, 23 Oct 2026 18:44:21 +0200 Subject: [PATCH 04/13] dsv41: --spec-eval honours --dspark off --- src/dsv41/ds4_main.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/dsv41/ds4_main.cpp b/src/dsv41/ds4_main.cpp index dca4e18c..0b288e84 100644 --- a/src/dsv41/ds4_main.cpp +++ b/src/dsv41/ds4_main.cpp @@ -682,7 +682,7 @@ int profile_run(strata::ds4::Model& m, const std::string& ids_path, int window) // ---- Strata H100 addition: DSpark acceptance log --------------------------------------------------------------- // --spec-eval IDS.json --n N: each segment is a prompt: fed from reset, then N greedy tokens decoded with the drafter, every // draft verified (--dspark-conf 0); DS4_ROUND_LOG=file gets a line a round (segment name, drafts, accepted, confidences). -int spec_eval(strata::ds4::Model& m, const std::string& ids_path, int n, int window, float conf_min) { +int spec_eval(strata::ds4::Model& m, const std::string& ids_path, int n, int window, float conf_min, bool spec) { std::vector segs; std::string err; if (!read_score_ids(ids_path, segs, err)) { @@ -712,7 +712,7 @@ int spec_eval(strata::ds4::Model& m, const std::string& ids_path, int n, int win bool stop = false; while (emitted < n && !stop) { int off = 0, acc = 0; - if (!round(m, tok, n - emitted, true, conf_min, 0.0f, 1.0f, 1, 0.0f, 1, counter, got, off, acc, err)) { + if (!round(m, tok, n - emitted, spec, conf_min, 0.0f, 1.0f, 1, 0.0f, 1, counter, got, off, acc, err)) { std::fprintf(stderr, "strata-dsv41: %s\n", err.c_str()); return 1; } @@ -835,7 +835,7 @@ int main(int argc, char** argv) { if (!o.vision_only) std::fprintf(stderr, "%s", m.describe().c_str()); if (serve_mode) return serve(m, eos, spec, o.dspark_conf); if (!prof_ids.empty()) return profile_run(m, prof_ids, window); - if (!spec_ids.empty()) return spec_eval(m, spec_ids, n, window, o.dspark_conf); + if (!spec_ids.empty()) return spec_eval(m, spec_ids, n, window, o.dspark_conf, spec); if (!score_ids.empty()) return score(m, score_ids, score_out.empty() ? score_ids + ".tsv" : score_out, window, std::atoi(eos.c_str())); if (!enc_rgb.empty()) { // the vision tower alone: a picture's span rows (BF16 [rows][dim]) into --dump-image From 96a47b79d5109ab074203e0d39e0fb7bda70e88a Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Fri, 23 Oct 2026 19:14:44 +0200 Subject: [PATCH 05/13] dsv41: after a chunked prompt the RAM store is filled with the prompt's hottest CPU-lane experts (a window prompt fills it through the CPU lane; the chunk path streams past it) --- src/dsv41/ds4_model.cu | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/dsv41/ds4_model.cu b/src/dsv41/ds4_model.cu index 91278f20..f4ccb36a 100644 --- a/src/dsv41/ds4_model.cu +++ b/src/dsv41/ds4_model.cu @@ -5364,6 +5364,24 @@ struct Model::Impl { if (sw_inflight == 0) break; // (nothing queued: every layer's best CPU-lane expert is not hotter than its coldest VRAM one) std::this_thread::sleep_for(std::chrono::milliseconds(1)); } + if (store && env_f("DS4_SETTLE_STORE", 1.0f) != 0.0f) { // the RAM store too: a window prompt fills it through the CPU + // lane's picks, a chunked one streams past it - the hottest CPU-lane experts of the prompt into its free slots + std::vector>> hot; + for (const Layer& l : L) { + if (l.mtp) continue; + for (int e = 0; e < l.n_exp; ++e) + if (l.slot[(size_t) e] < 0 && l.heat[(size_t) e] >= 4.0f) hot.push_back({l.heat[(size_t) e], {l.il, e}}); + } + std::sort(hot.begin(), hot.end(), [](const auto& a, const auto& b) { return a.first > b.first; }); + std::vector> list; + for (const auto& h : hot) { + if (list.size() >= store->slots_allocated()) break; + list.push_back(h.second); + } + const double ts = now_ms(); + const size_t n = store->preload(list, 0); + if (o.verbose) std::fprintf(stderr, "ds4: RAM store: %zu hot experts read after the prompt (%.0f ms)\n", n, now_ms() - ts); + } if (o.verbose && st.swaps > s0) std::fprintf(stderr, "ds4: expert cache: %lld swaps after the prompt (%.0f ms)\n", (long long) (st.swaps - s0), now_ms() - t0); From dbf3bb2e643c769df89c96c891787afda3cba22c Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Fri, 23 Oct 2026 19:24:01 +0200 Subject: [PATCH 06/13] dsv41: the layer-major run's RAM is given back before the cache catch-up and the store fill --- src/dsv41/ds4_model.cu | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/dsv41/ds4_model.cu b/src/dsv41/ds4_model.cu index f4ccb36a..1b521555 100644 --- a/src/dsv41/ds4_model.cu +++ b/src/dsv41/ds4_model.cu @@ -7340,6 +7340,10 @@ struct Model::Impl { pos += n; st.tokens += n; cache_tick(n); + if (lm_end_.on) { // (the layer cache's RAM back before the store and the swaps want it) + lm_end(); + lm_end_.on = false; + } cache_settle(); const int nr = std::min(T_last, tap_cap); if (!seed_drafter(pos - nr, nr, 0, err)) return false; From dc352b839b04ac617a2fcbf9dab1979a7a05941b Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Fri, 23 Oct 2026 19:36:53 +0200 Subject: [PATCH 07/13] dsv41: say how the chunk staging ring was sized --- src/dsv41/ds4_model.cu | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/dsv41/ds4_model.cu b/src/dsv41/ds4_model.cu index 1b521555..b1d78f1e 100644 --- a/src/dsv41/ds4_model.cu +++ b/src/dsv41/ds4_model.cu @@ -2681,7 +2681,11 @@ struct Model::Impl { } for (const Dev& d : devs) room += std::max(0.0, (double) d.free0 - (double) ((size_t) o.reserve_mib << 20) - 1.5 * (1 << 30)) * o.vram_experts; - return exp_b > 0 && exp_b - std::max(0.0, room - other_b) <= 0.1 * exp_b ? 64 : 400; + const bool fit = exp_b > 0 && exp_b - std::max(0.0, room - other_b) <= 0.1 * exp_b; + if (o.verbose) + std::fprintf(stderr, "ds4: staging ring: experts %.1f GB, VRAM room %.1f GB, dense %.1f GB -> %d MB\n", exp_b / 1e9, + room / 1e9, other_b / 1e9, fit ? 64 : 400); + return fit ? 64 : 400; } bool alloc_chunks(std::string& err) { chunks.resize(devs.size()); From 0beae0bca6b500c0a147aea3071f24644de25d8b Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Fri, 23 Oct 2026 19:44:54 +0200 Subject: [PATCH 08/13] dsv41: the store fill after a chunked prompt reaches the store's whole capacity, not only its allocated slots --- src/dsv41/ds4_model.cu | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dsv41/ds4_model.cu b/src/dsv41/ds4_model.cu index b1d78f1e..79487716 100644 --- a/src/dsv41/ds4_model.cu +++ b/src/dsv41/ds4_model.cu @@ -5379,7 +5379,7 @@ struct Model::Impl { std::sort(hot.begin(), hot.end(), [](const auto& a, const auto& b) { return a.first > b.first; }); std::vector> list; for (const auto& h : hot) { - if (list.size() >= store->slots_allocated()) break; + if (list.size() >= store->slots_max()) break; list.push_back(h.second); } const double ts = now_ms(); From 41856f659443940eafb92984c11ba865d0b9f5bb Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Fri, 23 Oct 2026 19:45:21 +0200 Subject: [PATCH 09/13] dsv41: staging ring 64 MB when the experts are within 30% of fitting the cards (R9700 x4 plain 2.00: ratio 1.16) --- src/dsv41/ds4_model.cu | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/dsv41/ds4_model.cu b/src/dsv41/ds4_model.cu index 79487716..8d773f2c 100644 --- a/src/dsv41/ds4_model.cu +++ b/src/dsv41/ds4_model.cu @@ -2668,7 +2668,7 @@ struct Model::Impl { } // the staging ring's size (MB) for the experts a chunk streams from the CPU lane: 400 where a good part of them are - // streamed; 64 where the cards hold at least 90% of the experts (a 4-card box) - the ring's VRAM is then worth more + // streamed; 64 where the cards hold most of the experts (they are within 30% of fitting) (a 4-card box) - the ring's VRAM is then worth more // as expert slots, and the decode after a prompt keeps the windowed path's speed (R9700 x4: 29.6 -> 31.7 tok/s) size_t ring_mb_auto() const { double exp_b = 0, other_b = 0, room = 0; @@ -2681,7 +2681,7 @@ struct Model::Impl { } for (const Dev& d : devs) room += std::max(0.0, (double) d.free0 - (double) ((size_t) o.reserve_mib << 20) - 1.5 * (1 << 30)) * o.vram_experts; - const bool fit = exp_b > 0 && exp_b - std::max(0.0, room - other_b) <= 0.1 * exp_b; + const bool fit = exp_b > 0 && exp_b <= 1.3 * std::max(1.0, room - other_b); // (the cards hold most of them: R9700 x4 1.16, 2 x 3090 ~3, one card 5+) if (o.verbose) std::fprintf(stderr, "ds4: staging ring: experts %.1f GB, VRAM room %.1f GB, dense %.1f GB -> %d MB\n", exp_b / 1e9, room / 1e9, other_b / 1e9, fit ? 64 : 400); From f7d70728412484827507bdd6ec7715d9b7d2e077 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Fri, 23 Oct 2026 19:58:22 +0200 Subject: [PATCH 10/13] dsv41: DS4_STORE_PREGROW=1 (map the whole store arena before the first kernel; cuMemHostRegister mid-decode hangs on Linux) --- src/dsv41/ds4_store.hpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/dsv41/ds4_store.hpp b/src/dsv41/ds4_store.hpp index 3c7b923a..643ef376 100644 --- a/src/dsv41/ds4_store.hpp +++ b/src/dsv41/ds4_store.hpp @@ -93,6 +93,12 @@ public: } io_.reset(new IoQueue(std::max(1, c.threads))); grow(); // (the first chunk now: whether the GPU can map it is known from the start) + // DS4_STORE_PREGROW=1: the whole arena now. cuMemHostRegister (the GPU mapping of a chunk) taken while a kernel of + // the decode is running waited on it for good on Linux (H100, driver 580): registering all chunks before the first + // kernel avoids it + if (std::getenv("DS4_STORE_PREGROW") != nullptr) + while (grow()) { + } return true; } ~ExpertStore() { From fb321d62cb59ab19897c1083a55a0751cec4fe8a Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Fri, 23 Oct 2026 19:59:13 +0200 Subject: [PATCH 11/13] dsv41: the layer-major (chunked) prompt is the default on every run: chunks of 1024 for the single-card default run, staging ring 16 MB where the cards hold most of the experts --- src/dsv41/ds4_model.cu | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/dsv41/ds4_model.cu b/src/dsv41/ds4_model.cu index 8d773f2c..32b9e614 100644 --- a/src/dsv41/ds4_model.cu +++ b/src/dsv41/ds4_model.cu @@ -791,7 +791,7 @@ struct Model::Impl { unsigned win_seq = 0; // the windows the threads ran (the hand-offs' seq) // the chunked prompt path (threads mode): chunk tokens a pass, runs shorter than chunk_min in windows int chunk = 0, chunk_min = 512; - static constexpr bool kChunkedPromptDefault = false; // flip once the chunk buffers are elastic (decode unchanged) + static constexpr bool kChunkedPromptDefault = true; // the layer-major prompt is the default (gate: KL 0.1685 vs 0.1686, 8K prompt 6x, decode within 2% - ds4 notes-dsv41-r97) static constexpr int kFlowChunkMin = 2; // chunk_min with every device's layers on the flow (see load) // --prefill-chunk at most (route() runs in kVlRows-row slabs). The default stays kVlRows: a 16384-token chunk // reads a 16K prompt's experts once instead of twice (V100: 461.8 -> 321.3 s, 736 -> 506 GB read) but its buffers @@ -1880,7 +1880,8 @@ struct Model::Impl { o.vram_experts; file_experts = exp_b - std::max(0.0, room - other_b) + (double) kRamMargin > (double) ram_avail(); } - chunk = prompt_par ? (o.prefill_chunk >= 0 ? o.prefill_chunk : file_experts ? kVlRows : 2048) : 0; + const bool own_default = !(par || o.tp > 1); // (the single-card default run the chunked path newly takes: chunks of 1024 - 8K prompt 370 tok/s on 4 x R9700, 214 at 8192; the threads / tp runs keep theirs) + chunk = prompt_par ? (o.prefill_chunk >= 0 ? o.prefill_chunk : own_default ? 1024 : file_experts ? kVlRows : 2048) : 0; const char* dc = std::getenv("DS4_CHUNK"); // (tests: down to 16, a short prompt in several chunks) if (dc != nullptr) chunk = prompt_par ? std::atoi(dc) : 0; if (const char* v = std::getenv("DS4_CHUNK_MIN")) chunk_min = std::max(1, std::atoi(v)); @@ -2668,7 +2669,7 @@ struct Model::Impl { } // the staging ring's size (MB) for the experts a chunk streams from the CPU lane: 400 where a good part of them are - // streamed; 64 where the cards hold most of the experts (they are within 30% of fitting) (a 4-card box) - the ring's VRAM is then worth more + // streamed; 16 where the cards hold most of the experts (they are within 30% of fitting) (a 4-card box) - the ring's VRAM is then worth more // as expert slots, and the decode after a prompt keeps the windowed path's speed (R9700 x4: 29.6 -> 31.7 tok/s) size_t ring_mb_auto() const { double exp_b = 0, other_b = 0, room = 0; @@ -2681,11 +2682,11 @@ struct Model::Impl { } for (const Dev& d : devs) room += std::max(0.0, (double) d.free0 - (double) ((size_t) o.reserve_mib << 20) - 1.5 * (1 << 30)) * o.vram_experts; - const bool fit = exp_b > 0 && exp_b <= 1.3 * std::max(1.0, room - other_b); // (the cards hold most of them: R9700 x4 1.16, 2 x 3090 ~3, one card 5+) + const bool fit = exp_b > 0 && exp_b <= 1.3 * std::max(1.0, room - other_b); // (R9700 x4: 1.16 -> ring 16: decode -1.8% after a prompt, 64: -4.4%, 400: -4.6%) if (o.verbose) std::fprintf(stderr, "ds4: staging ring: experts %.1f GB, VRAM room %.1f GB, dense %.1f GB -> %d MB\n", exp_b / 1e9, - room / 1e9, other_b / 1e9, fit ? 64 : 400); - return fit ? 64 : 400; + room / 1e9, other_b / 1e9, fit ? 16 : 400); + return fit ? 16 : 400; } bool alloc_chunks(std::string& err) { chunks.resize(devs.size()); From c35710e388bdfbf3498587ec326836c5d37dd81a Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Fri, 23 Oct 2026 19:59:29 +0200 Subject: [PATCH 12/13] dsv41: store arena pre-grown by default on Linux when the GPU maps it --- src/dsv41/ds4_store.hpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/dsv41/ds4_store.hpp b/src/dsv41/ds4_store.hpp index 643ef376..aa0d2b8d 100644 --- a/src/dsv41/ds4_store.hpp +++ b/src/dsv41/ds4_store.hpp @@ -93,10 +93,15 @@ public: } io_.reset(new IoQueue(std::max(1, c.threads))); grow(); // (the first chunk now: whether the GPU can map it is known from the start) - // DS4_STORE_PREGROW=1: the whole arena now. cuMemHostRegister (the GPU mapping of a chunk) taken while a kernel of + // DS4_STORE_PREGROW=1 (default on Linux with the GPU mapping, =0 off): the whole arena now. cuMemHostRegister (the GPU mapping of a chunk) taken while a kernel of // the decode is running waited on it for good on Linux (H100, driver 580): registering all chunks before the first // kernel avoids it - if (std::getenv("DS4_STORE_PREGROW") != nullptr) +#if defined(__linux__) + const bool pregrow = cfg_.map_chunk && !(std::getenv("DS4_STORE_PREGROW") != nullptr && std::atoi(std::getenv("DS4_STORE_PREGROW")) == 0); // (Linux: on with the GPU mapping) +#else + const bool pregrow = std::getenv("DS4_STORE_PREGROW") != nullptr && std::atoi(std::getenv("DS4_STORE_PREGROW")) != 0; +#endif + if (pregrow) while (grow()) { } return true; From f1cb66d09e664fc1e4503b9959ebaf3825f6f180 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Fri, 23 Oct 2026 20:03:24 +0200 Subject: [PATCH 13/13] dsv41: strata-dsv41 --version and a 'ds4: build ' line with --verbose (commit from git at configure, a BUILD_ID file in an archive, or -DSTRATA_BUILD_ID) --- CMakeLists.txt | 12 ++++++++++++ src/dsv41/ds4_main.cpp | 12 +++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 80ba5890..c7fcd7de 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -778,6 +778,18 @@ if(STRATA_ENABLE_CUDA OR STRATA_ENABLE_HIP) add_executable(strata-dsv41 src/dsv41/ds4_main.cpp src/dsv41/ds4_model.cu src/dsv41/ds4_kernels.cu src/dsv41/ds4_vision.cu src/dsv41/ds4_gguf.cu src/dsv41/ds4_gguf_cpu.cpp src/dsv41/ds4_iq.cu src/dsv41/ds4_iq_cpu.cpp src/dsv41/ds4_tq.cu src/dsv41/ds4_tq_cpu.cpp src/dsv41/ds4_gg_cpu.cpp src/dsv41/ds4_packinfo.cpp) + # the commit the binary was built from, readable with --version (set -DSTRATA_BUILD_ID=... for a tree without .git) + if(NOT STRATA_BUILD_ID AND EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/BUILD_ID) # (a git archive has no .git: the packer writes the commit here) + file(STRINGS ${CMAKE_CURRENT_SOURCE_DIR}/BUILD_ID STRATA_BUILD_ID LIMIT_COUNT 1) + endif() + if(NOT STRATA_BUILD_ID) + execute_process(COMMAND git -C ${CMAKE_CURRENT_SOURCE_DIR} rev-parse --short=9 HEAD OUTPUT_VARIABLE STRATA_BUILD_ID + OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET) + endif() + if(NOT STRATA_BUILD_ID) + set(STRATA_BUILD_ID unknown) + endif() + set_source_files_properties(src/dsv41/ds4_main.cpp PROPERTIES COMPILE_DEFINITIONS "DS4_BUILD_ID=\"${STRATA_BUILD_ID}\"") target_include_directories(strata-dsv41 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include ${_strata_gpu_include_directories} ${CMAKE_CURRENT_SOURCE_DIR}/third_party/ggml) target_link_libraries(strata-dsv41 PRIVATE strata_dsv41_kernels ${_strata_gpu_runtime_target} ${_strata_gpu_blas_target}) diff --git a/src/dsv41/ds4_main.cpp b/src/dsv41/ds4_main.cpp index 420fa93f..3378403b 100644 --- a/src/dsv41/ds4_main.cpp +++ b/src/dsv41/ds4_main.cpp @@ -735,6 +735,10 @@ int spec_eval(strata::ds4::Model& m, const std::string& ids_path, int n, int win } } // namespace +#ifndef DS4_BUILD_ID +#define DS4_BUILD_ID "unknown" +#endif + int main(int argc, char** argv) { strata::ds4::Options o; std::string ids_s, dump, eos = "1"; @@ -794,7 +798,13 @@ int main(int argc, char** argv) { } else if (a == "--temperature") temp = (float) std::atof(val("--temperature").c_str()); else if (a == "--seed") cli_seed = std::strtoull(val("--seed").c_str(), nullptr, 10); - else if (a == "--verbose" || a == "-v") o.verbose = true; + else if (a == "--verbose" || a == "-v") { + o.verbose = true; + std::fprintf(stderr, "ds4: build %s (%s)\n", DS4_BUILD_ID, STRATA_VERSION); + } else if (a == "--version") { + std::printf("strata-dsv41 %s build %s\n", STRATA_VERSION, DS4_BUILD_ID); + return 0; + } else if (a == "--ids") ids_s = val("--ids"); else if (a == "--n") n = std::atoi(val("--n").c_str()); else if (a == "--window") window = std::max(1, std::atoi(val("--window").c_str())); // (> 16: the chunked prompt path)