From 56501a38db9128c520049cb8e4284e97ced1fa66 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Sun, 4 Oct 2026 10:10:54 +0200 Subject: [PATCH] #583: the byte-budget ring only where it gains; short prompts keep 0.1.39's ring - a prompt that fits 0.1.39's auto chunk keeps 0.1.39's ring (Prefill::set_ring_budget's small_max): 4K prompts were 1-17% slower with the smaller ring (Coder -15%, IQ3_S -5%, IQ3_XXS -2%; 5 interleaved pairs each); - ring slots are given up for a bigger chunk only where 0.1.39's rule held the chunk under 6144 (ring_cap_for); from 6144 on the ring keeps its 0.1.39 size (the Coder 6144/384 -> 7936/199 was -12% at 32K); - the unpinned arm's cap stays 96 (the byte budget made it ~49 on IQ3_S); - the scan sets its own ring budget instead of set_ring_override, so a layer split's 96-slot override (#340) is no longer clobbered by the scan. RTX 5070 vs STRATA_RING_BYTES=0: 4K -0.7..+1.1% (bit-identical); 32K IQ3_XXS +18.5% (cache 1500) / +9.4%, Coder +3.1%, IQ3_S +0.2%. --- docs/DETAILS.md | 10 ++-- include/strata/prefill/prefill.hpp | 8 ++++ src/prefill/prefill.cpp | 35 ++++++++++++-- src/program/generate.cpp | 77 +++++++++++++++++++----------- 4 files changed, 96 insertions(+), 34 deletions(-) diff --git a/docs/DETAILS.md b/docs/DETAILS.md index 62563f85..ec8b4fc4 100644 --- a/docs/DETAILS.md +++ b/docs/DETAILS.md @@ -1033,9 +1033,13 @@ the document, +0.4% on the chat. Details: `bench/results/2026-09-27-esp/`. - **Prompts** are processed in chunks of up to 8,192 tokens (`--prefill auto`; 32,768 opt-in) with the experts streamed to the GPU over PCIe. Since engine 0.1.39b (#583) the streamed ring is sized in bytes for the pack (a native pack with bigger blobs gets fewer ring slots and keeps more cache slots) and `--prefill auto` picks the - largest chunk that keeps that ring full: an RTX 5070 with IQ3_XXS and a partly pinned arena reads a 32K prompt - 14-26% faster (1,045 -> 1,190 tok/s at a 1,500-slot cache, -> 1,315 at 2,400; a 4K prompt 1-4% slower). Q2_0 - keeps its 384-slot ring. It changes the bits of a long prompt against 0.1.39 (the experts go through a different + largest chunk that keeps that ring full. It only gives up ring slots where 0.1.39's rule held the chunk at 4,096 + tokens or less; from 6,144 on it keeps 0.1.39's ring and only looks for a bigger chunk beside it (shrinking the + ring there measured slower: the Coder -12%), and a prompt that fits 0.1.39's chunk keeps 0.1.39's ring (one + chunk: a smaller ring only slowed it). RTX 5070, 32K prompts against `STRATA_RING_BYTES=0`, 2-3 interleaved + rounds: IQ3_XXS +18.5% at a 1,500-slot cache and +9% in another memory state, the Coder +3%, IQ3_S unchanged; 4K + prompts unchanged (-0.7% to +1.1%, bit-identical). Q2_0 (a fixed `--prefill 2048`) is not affected. It changes + the bits of a long prompt against 0.1.39 (the experts go through a different mix of cached and streamed groups). Measured quality, teacher-forced over the next 2,001 tokens of a long document against the FP16 prompt path (IQ3_XXS, `--prefill auto`): 8K prompt KL 0.042 (0.1.39: 0.054), top-1 agreement 93.5% (92.2%); 32K prompt KL 0.020 (0.019), top-1 95.3% (95.0%) - the same band as before. `STRATA_RING_BYTES=0` diff --git a/include/strata/prefill/prefill.hpp b/include/strata/prefill/prefill.hpp index ed633fd1..15df6e4e 100644 --- a/include/strata/prefill/prefill.hpp +++ b/include/strata/prefill/prefill.hpp @@ -79,6 +79,9 @@ public: /// #340: the streamed ring's slot count for chunks that stream every expert, instead of the pinned-share rule /// (0 = that rule). Set before any `bytes_needed`/`init` (both count the ring); STRATA_PREFILL_RING still wins. static void set_ring_override(int slots); + /// #583: the auto chunk scan's byte-budget ring, for chunks above `small_max` (0.1.39's auto chunk: a prompt that + /// fits it keeps 0.1.39's ring). 0 slots = none. A layer split's set_ring_override and STRATA_PREFILL_RING win. + static void set_ring_budget(int slots, int64_t small_max); /// Device bytes `init` needs for a chunk of `chunk` tokens (what a borrowed region must hold). static uint64_t bytes_needed(const core::ModelGeometry& g, const core::SessionState& ss, int64_t chunk); @@ -92,6 +95,11 @@ public: /// what the auto chunk scan treats as a full ring. A slot is one whole blob, so a pack with bigger blobs than /// Q2_0's gets fewer of them for the same bytes - 384 on Q2_0, 199 on a 2.54 MiB-blob IQ3_S pack. static int64_t ring_max_slots(); + /// 0.1.39's ring for this PC (1024 fused / 384 pinned / 96), within ring_cap(). + static int64_t ring_default_slots(); + /// #583: the ring the auto scan keeps full, given the chunk 0.1.39's rule picked: its byte budget where that rule's + /// chunk was small (< 6144), else 0.1.39's ring (measured: shrinking it for a bigger chunk lost there). + static int64_t ring_cap_for(int64_t old_chunk); /// What the ring actually resolves to for a chunk of `chunk` tokens, after the override, STRATA_PREFILL_RING /// and the pinned-share rule - the slot count `init` lays out. The engine reports it on its INFO line so the diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cpp index 6af1ea55..e76fb6dd 100644 --- a/src/prefill/prefill.cpp +++ b/src/prefill/prefill.cpp @@ -119,6 +119,11 @@ double g_pinned_share = 1.0; // slot count is derived from the pack (`ring_budget_slots`); Q2_0 still resolves to exactly 1024 (fused) and 384, // so the pack all of this was tuned on does not move. int g_ring_override = 0; // #340: set by a layer split (Prefill::set_ring_override); 0 = the rule below +// #583 (0.1.39b): the auto chunk scan's byte-budget ring (Prefill::set_ring_budget), for requests whose chunk is above +// g_ring_small_max - the chunk 0.1.39's rule would have picked. A prompt that fits that chunk keeps 0.1.39's ring: +// on one chunk the smaller ring only slowed it (RTX 5070, 4K prompts: Coder -15%, IQ3_S -5%, IQ3_XXS -2%). +int g_ring_budget = 0; +int64_t g_ring_small_max = 0; // #136: the fused experts (STRATA_PF_FUSED=1) launch on a batch of a layer's streamed experts at once, so the ring // should hold a whole layer's (~460 of 512 on Q2_0): with 384 slots a layer's last batch waits for slots its own // first batch frees. Measured on the 5070, Q2_0, the 4K / 32K code-agent prompts (one run each): fused at 384 slots @@ -184,8 +189,11 @@ inline int ring_slots(size_t T) { // the byte budget as this pack's slots: 1024 fused / 384 not on Q2_0, fewer on a pack with bigger blobs. The // unpinned arm stays a slot count (96): it was measured where the host copies are the limit, and there the ring // is not what is competing for VRAM. - const int pinned_ring = g_pinned_share >= 0.9 ? (ring_bytes_on() ? ring_budget_slots() : fused_ring() ? 1024 : 384) : 96; - const int r = v ? std::atoi(v) : g_ring_override > 0 ? g_ring_override : pinned_ring; + // 0.1.39's ring (1024 fused / 384 pinned, 96 when a large share goes through host copies), and the #583 byte + // budget the auto scan chose for chunks past the size 0.1.39's rule would have picked (set_ring_budget) + const int pinned_ring = g_pinned_share >= 0.9 ? (fused_ring() ? 1024 : 384) : 96; + const bool budget = ring_bytes_on() && g_ring_budget > 0 && (int64_t) T > g_ring_small_max; + const int r = v ? std::atoi(v) : g_ring_override > 0 ? g_ring_override : budget ? g_ring_budget : pinned_ring; if (v && r == STAGE) return STAGE; // Explicit opt-in to routed-only staging, including large chunks. const int big = r < 16 ? 16 : r > ring_cap() ? ring_cap() : r; return (int64_t) T >= stream_all_min() ? big : STAGE; @@ -1341,6 +1349,10 @@ bool Prefill::set_peer(core::PeerExperts* peer, int64_t cap_rows, std::string& e void Prefill::set_pinned_share(double share) { g_pinned_share = share; } void Prefill::set_ring_override(int slots) { g_ring_override = slots > 0 ? slots : 0; } +void Prefill::set_ring_budget(int slots, int64_t small_max) { + g_ring_budget = slots > 0 ? slots : 0; + g_ring_small_max = small_max > 0 ? small_max : 0; +} double Prefill::pinned_share() { return g_pinned_share; } uint64_t Prefill::bytes_needed(const core::ModelGeometry& g, const core::SessionState& ss, int64_t chunk) { @@ -1396,7 +1408,24 @@ uint64_t Prefill::bytes_needed_no_ring(const core::ModelGeometry& g, const core: return bytes_needed(g, ss, chunk) - (uint64_t) ring_slots((size_t) chunk) * (uint64_t) MAXBLOB(); } -int64_t Prefill::ring_max_slots() { return (int64_t) ring_budget_slots(); } +int64_t Prefill::ring_default_slots() { + const int r = g_pinned_share >= 0.9 ? (fused_ring() ? 1024 : 384) : 96; + return (int64_t) std::min(r, ring_cap()); +} + +int64_t Prefill::ring_cap_for(int64_t old_chunk) { + // 0.1.39b (#583, measured on the RTX 5070): giving up ring slots for a bigger chunk paid where 0.1.39's ring held + // the chunk at 4096 or less (IQ3_XXS 32K prompts +14% to +26%: 4096/384 -> 6656/56) and lost where 0.1.39 already + // read 6144-token chunks (the Coder: 6144/384 -> 7936/199, 32K -12%). From kKeepRingChunk on the ring keeps its + // 0.1.39 size and the scan only looks for a bigger chunk next to it (IQ3_XXS unpinned 6144/96 -> 6912/96: +9%). + constexpr int64_t kKeepRingChunk = 6144; + return old_chunk >= kKeepRingChunk ? ring_default_slots() : ring_max_slots(); +} + +// the unpinned arm keeps its measured 96 (the PR's rule; the byte budget would have been ~49 slots on IQ3_S) +int64_t Prefill::ring_max_slots() { + return g_pinned_share >= 0.9 ? (int64_t) ring_budget_slots() : (int64_t) std::min(96, ring_cap()); +} int64_t Prefill::ring_slots_for(int64_t chunk) { return ring_slots((size_t) chunk); } diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 6d98693b..819ffc84 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -4335,15 +4335,22 @@ int main(int argc, char** argv) { }; auto plan_lend = [&](int64_t& chunk) -> int64_t { auto slots_for = lend_slots; - if (o.prefill_auto && !strata::prefill::Prefill::ring_bytes_enabled()) { // STRATA_RING_BYTES=0: 0.1.39's list + // 0.1.39's list with its ring (also the size up to which a prompt keeps that ring under #583) + auto old_rule = [&]() -> int64_t { + strata::prefill::Prefill::set_ring_budget(0, 0); static constexpr int64_t kAutoChunks[] = {32768, 16384, 8192, 6144, 4096, 3072, 2048, 1024, 512, 256}; for (const int64_t c : kAutoChunks) { // above 8192: only when asked for, and only when a prompt of the context can use it if (c > 8192 && (c > o.prefill_auto_max || c > o.max_context)) continue; const int64_t k = slots_for(c); - if (k + 128 <= xcache.slots() && k * 100 <= kAutoLendPct * xcache.slots()) { chunk = c; return k; } + if (k + 128 <= xcache.slots() && k * 100 <= kAutoLendPct * xcache.slots()) return c; } return 0; + }; + if (o.prefill_auto && !strata::prefill::Prefill::ring_bytes_enabled()) { // STRATA_RING_BYTES=0: 0.1.39's list + const int64_t c = old_rule(); + if (c > 0) { chunk = c; return slots_for(c); } + return 0; } if (o.prefill_auto) { // the default since 0.1.39b (#583) // The chunk and the ring are one budget, and the ring is the better buy. Measured on this 4-way @@ -4356,7 +4363,8 @@ int main(int argc, char** argv) { // one falls back to the old rule, which takes the largest chunk whose ring clears kRingMin (the // value at or below which ring_slots() returns STAGE and streaming is off). constexpr int64_t kRingMin = 16; - const int64_t ring_max = strata::prefill::Prefill::ring_max_slots(); + const int64_t small = old_rule(); // 0.1.39's chunk (and its ring for the prompts that fit it) + const int64_t ring_max = strata::prefill::Prefill::ring_cap_for(small); const int64_t budget = std::min(xcache.slots() - 128, kAutoLendPct * xcache.slots() / 100); // The room is a BYTE budget. A ring slot is max_blob bytes (`carve` lays out one whole blob each), // while these cache slots hold their own layer's blob, which is smaller than max_blob unless the cache @@ -4376,7 +4384,7 @@ int main(int argc, char** argv) { return biggest_chunk(auto_ceiling, [&](int64_t t) -> bool { const int64_t room = room_of(t); if (room < floor) return false; - strata::prefill::Prefill::set_ring_override((int) room); + strata::prefill::Prefill::set_ring_budget((int) room, 0); // ring_slots() now returns `room` - unless STRATA_PREFILL_RING overrides it, in which case the // chunk has to fit THAT ring, which is the pre-fix rule and the A/B arm const int64_t k = slots_for(t); @@ -4384,14 +4392,16 @@ int main(int argc, char** argv) { }); }; int64_t c = scan(ring_max); - if (c == 0) c = scan(kRingMin); - if (c > 0) { - // the probe left the override on its last trial; put it back on the chunk that won - strata::prefill::Prefill::set_ring_override((int) room_of(c)); - chunk = c; - return slots_for(c); + if (c == 0 && ring_max < strata::prefill::Prefill::ring_default_slots()) c = scan(kRingMin); + // a prompt that fits 0.1.39's chunk keeps 0.1.39's ring (one chunk: the smaller ring only slowed it) + if (small >= c) { // the scan bought nothing: 0.1.39's choice + strata::prefill::Prefill::set_ring_budget(0, 0); + if (small > 0) { chunk = small; return slots_for(small); } + return 0; } - return 0; + strata::prefill::Prefill::set_ring_budget((int) room_of(c), small); + chunk = c; + return slots_for(c); } for (int64_t c = chunk; c >= 256; c /= 2) { const int64_t k = slots_for(c); @@ -4581,8 +4591,8 @@ int main(int argc, char** argv) { // than it is, and a chunk whose ring only fitted after that discount was refused outright - the scan // stopped one step short while the chunk it did take had room to spare for the cap-sized ring. const int64_t kBlob = (int64_t) strata::kernels::cpu::expert_layout().max_blob; - auto ring_room = [&](int64_t c, const PfPart* only) -> int64_t { - int64_t room = strata::prefill::Prefill::ring_max_slots(); + auto ring_room = [&](int64_t c, const PfPart* only, int64_t cap) -> int64_t { + int64_t room = cap; for (const PfPart& p : pf_parts) { if (only != nullptr && &p != only) continue; // clamped at 0: a cache under 128 slots has a negative lend budget, and `slots - budget` is @@ -4600,33 +4610,38 @@ int main(int argc, char** argv) { // ring still at its full byte budget (ring_room is capped at ring_max, so that is room == ring_max), // under the operator's ceiling (auto_ceiling keeps a bare `auto` at 8192, #282). Failing that, the // largest chunk whose ring clears kRingMin. + // 0.1.39's list with its ring: the chunk it would pick, and the size up to which a prompt keeps that ring + auto old_pick = [&](const PfPart* only) -> int64_t { + strata::prefill::Prefill::set_ring_budget(0, 0); + static constexpr int64_t kAutoChunks[] = {32768, 16384, 8192, 6144, 4096, 3072, 2048, 1024, 512, 256}; + for (const int64_t c : kAutoChunks) { + if (c > 8192 && (c > o.prefill_auto_max || c > o.max_context)) continue; // #282, as plan_lend + if (fits(c, true, only)) return c; + } + return 0; + }; auto scan = [&](const PfPart* only) -> int64_t { if (!o.prefill_auto) { // an explicit --prefill is the operator's number, and a loan of it only has to fit for (int64_t c = o.prefill_chunk; c >= 256; c /= 2) if (fits(c, false, only)) return c; return 0; } - if (!strata::prefill::Prefill::ring_bytes_enabled()) { // STRATA_RING_BYTES=0: 0.1.39's list - static constexpr int64_t kAutoChunks[] = {32768, 16384, 8192, 6144, 4096, 3072, 2048, 1024, 512, 256}; - for (const int64_t c : kAutoChunks) { - if (c > 8192 && (c > o.prefill_auto_max || c > o.max_context)) continue; // #282, as plan_lend - if (fits(c, true, only)) return c; - } - return 0; - } + if (!strata::prefill::Prefill::ring_bytes_enabled()) return old_pick(only); // STRATA_RING_BYTES=0 + const int64_t small = old_pick(only); + const int64_t cap = strata::prefill::Prefill::ring_cap_for(small); auto probe = [&](int64_t floor) -> int64_t { return biggest_chunk(auto_ceiling, [&](int64_t t) -> bool { - const int64_t room = ring_room(t, only); + const int64_t room = ring_room(t, only, cap); if (room < floor) return false; - strata::prefill::Prefill::set_ring_override((int) room); + strata::prefill::Prefill::set_ring_budget((int) room, 0); // ring_slots() now returns `room` - unless STRATA_PREFILL_RING overrides it, in which // case the chunk has to fit THAT ring, which is the pre-fix rule and the A/B arm return fits(t, true, only); }); }; - int64_t c = probe(strata::prefill::Prefill::ring_max_slots()); - if (c == 0) c = probe(kRingMin); - return c; + int64_t c = probe(cap); + if (c == 0 && cap < strata::prefill::Prefill::ring_default_slots()) c = probe(kRingMin); + return std::max(c, small); // never a smaller chunk than 0.1.39's }; const int64_t chunk = scan(nullptr); // #448: a small card in a layer split caps every stage's chunk (an RTX 3080's 512-slot cache held a @@ -4635,8 +4650,14 @@ int main(int argc, char** argv) { const int64_t alone = pf_parts.size() > 1 ? scan(&pf_parts[0]) : chunk; // both scans left the ring override on their own last trial: put it back on the chunk that won, which // is what `init` lays out and what the INFO line reports - if (chunk > 0 && o.prefill_auto && strata::prefill::Prefill::ring_bytes_enabled()) - strata::prefill::Prefill::set_ring_override((int) ring_room(chunk, nullptr)); + if (chunk > 0 && o.prefill_auto && strata::prefill::Prefill::ring_bytes_enabled()) { + // a prompt that fits 0.1.39's chunk keeps 0.1.39's ring (one chunk: the smaller ring only slowed + // it); the byte-budget ring is for the chunks past it + const int64_t small = old_pick(nullptr); + strata::prefill::Prefill::set_ring_budget( + small >= chunk ? 0 : (int) ring_room(chunk, nullptr, strata::prefill::Prefill::ring_cap_for(small)), + small); + } if (chunk > 0 && alone > chunk) { for (size_t i = 1; i < pf_parts.size(); ++i) { const PfPart& p = pf_parts[i];