From e77f1dfc3fa15d91e7b4eb868f7f134912b1fa6c Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Sun, 18 Oct 2026 03:21:24 +0200 Subject: [PATCH 1/3] Prefill planner: price the host (RAM page cache, displaced experts, drive) when choosing chunk and ring (#2824) The auto chunk scan only tested the GPU cache. A lent slot's expert comes back from the RAM copy or the model file for the length of a prompt, and every chunk streams the whole file set; past the page cache that set is re-read per chunk (28-45 GB per 29K prompt on 2x RTX 3060 / 32 GB / IQ2_XS at 8192-token chunks, ~1 GB at 4096). prefill_plan.hpp is the pure model (file set, miss share fitted to #2824, additive stream + compute + drive time); the serve scan and the generate path refine the VRAM pick with it, after the RAM copy exists. A set that fits the page cache keeps today's pick. STRATA_PREFILL_HOST_PLAN=0 turns it off; --prefill N is untouched. --- CMakeLists.txt | 5 + include/strata/program/prefill_plan.hpp | 152 ++++++++++++++++++++ src/program/generate.cpp | 177 +++++++++++++++++++++++- tests/program/prefill_plan_test.cpp | 96 +++++++++++++ 4 files changed, 427 insertions(+), 3 deletions(-) create mode 100644 include/strata/program/prefill_plan.hpp create mode 100644 tests/program/prefill_plan_test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 9591fe3f..3a265785 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -293,6 +293,11 @@ if(STRATA_BUILD_TESTS) target_include_directories(split_rules_test PRIVATE include) add_test(NAME split_rules_test COMMAND split_rules_test) + # the prompt planner's host-memory model (#2824): header-only, no GPU + add_executable(prefill_plan_test tests/program/prefill_plan_test.cpp) + target_include_directories(prefill_plan_test PRIVATE include) + add_test(NAME prefill_plan_test COMMAND prefill_plan_test) + # the default CPU prefill share is not armed when every expert is resident (#1595): header-only rule, no GPU add_executable(share_rules_test tests/prefill/share_rules_test.cpp) target_include_directories(share_rules_test PRIVATE include) diff --git a/include/strata/program/prefill_plan.hpp b/include/strata/program/prefill_plan.hpp new file mode 100644 index 00000000..60d71741 --- /dev/null +++ b/include/strata/program/prefill_plan.hpp @@ -0,0 +1,152 @@ +#pragma once + +#include +#include +#include +#include +#include +#include + +// The prompt path's chunk, streamed ring and expert-cache loan, priced against the HOST as well as the GPU (#2824). +// +// The automatic chunk scan only asks whether the loan fits the GPU cache. But a lent slot's expert is not in VRAM +// during the prompt: it comes from the RAM copy when the copy kept it, else from the model file, and a chunk of +// stream_all_min_tokens or more streams EVERY expert the GPU does not hold, once per chunk. When the file set +// (the experts neither the GPU cache nor the RAM copy hold, plus the displaced ones with no RAM backup) is bigger than +// the RAM the OS can keep file pages in, the set cycles through the page cache and each chunk reads most of it again: +// 2x RTX 3060 / 32 GB / IQ2_XS read 28-45 GB for one 29K prompt at 8192-token chunks (file set 15.1 GiB against a +// 14.5 GiB page cache) and 0.9-1.4 GB at 4096 (11.6 GiB). +// +// This is the pure model (no engine, no GPU) so it can be tested on the CPU. The engine builds the Options (a chunk x +// ring grid, each with the bytes it lends and the bytes of those the RAM copy keeps) and `choose` returns the one with +// the least predicted prompt time; a FlexGen-style joint choice of placement (what is lent) and I/O schedule (how many +// passes over the file set): when the set can never fit, f is ~1 for every option and the fewest passes (the biggest +// chunk) wins, so nothing is "rejected" and nothing is shrunk blindly. +// +// Time per prompt of N tokens in n = ceil(N / chunk) chunks: +// n * (chunk-compute + stream(chunk) / h2d) + file_reads / ssd, file_reads = n * f(W / page_cache) * W +// The streaming term is ADDITIVE with the compute term: the 12 GB-card 32K Q2_0 prompt measured 791 / 878 / 973 tok/s at +// 4096 / 6144 / 8192, which is 1,275 tok/s of compute plus ~1.9 s of streaming per chunk (a 20 GB stream at ~10 GB/s). +namespace strata::program::prefill_plan { + +constexpr double kGiB = 1073741824.0; + +struct Host { + uint64_t page_cache = 0; ///< bytes the OS can keep file pages in while a prompt runs (0 = reads are unbuffered) + double h2d_gbps = 12.0; ///< host -> device, GB/s (the engine's probe) + double ssd_gbps = 0.45; ///< file tier reads, GB/s (rotational ~0.15, SATA ~0.45, NVMe ~2.5) + double compute_tok_s = 1200; ///< prompt compute without streaming, tokens/s (only scales the printed seconds) + double ring_loss = 0.05; ///< prompt time lost between a full ring and the smallest one (#583: 17 vs 199 slots) +}; + +struct Tier { + uint64_t experts_total = 0; ///< every routed expert of the pack + uint64_t gpu_cache = 0; ///< bytes of all GPU expert caches (lent slots included) + uint64_t file_tier = 0; ///< experts neither a GPU cache nor the RAM copy holds (the decode-time file set) + uint64_t extra_file = 0; ///< other file pages a prompt touches (PLE rows) +}; + +struct Option { + int64_t chunk = 0; + int64_t ring = 0; ///< the ring slots the prompt path will really lay out + int64_t ring_full = 0; ///< the largest ring on offer (0 = unknown / not adjustable) + uint64_t lend_bytes = 0; ///< cache bytes the loan displaces + uint64_t lend_kept = 0; ///< ...of which the RAM copy keeps a copy +}; + +struct Cost { + Option opt; + double file_set = 0; ///< bytes the prompt path reads through the file tier, one chunk + double miss = 0; ///< share of that set each chunk fetches from the drive + double read_bytes = 0; ///< drive bytes per prompt (steady state: the set already warm) + double seconds = 0; ///< predicted prompt time + int64_t chunks = 0; + bool fits = false; ///< the set sits in the page cache with room to spare +}; + +/// The share of a chunk's file set that comes from the drive, from r = file_set / page_cache. Fitted to #2824: +/// r 0.80 (4096 chunks) read ~1 GB per prompt, r 0.92 (6144) 1.2-2.8 GB, r 1.04 (8192) 28-45 GB over four chunks +/// (a miss share of 0.47-0.75). Below 0.80 the set stays cached; the ramp to 1.0 is the page cache's imperfect LRU on +/// a cyclic scan (some pages survive); well past 1 nearly everything misses. +inline double miss_share(double r) { + if (r <= 0.80) return 0.0; + if (r <= 1.00) return 0.15 * (r - 0.80) / 0.20; + if (r <= 1.05) return 0.15 + 0.50 * (r - 1.00) / 0.05; + return std::min(1.0, 0.65 + 0.35 * (r - 1.05) / 0.25); +} + +inline Cost price(const Host& h, const Tier& t, int64_t tokens, const Option& o) { + Cost c; + c.opt = o; + c.chunks = o.chunk > 0 ? (tokens + o.chunk - 1) / o.chunk : 1; + const uint64_t unbacked = o.lend_bytes > o.lend_kept ? o.lend_bytes - o.lend_kept : 0; + c.file_set = (double) t.file_tier + (double) unbacked + (double) t.extra_file; + const double r = h.page_cache > 0 ? c.file_set / (double) h.page_cache : 1e9; // unbuffered: nothing is cached + c.miss = h.page_cache > 0 ? miss_share(r) : 1.0; + c.fits = h.page_cache > 0 && r <= 0.80; + c.read_bytes = (double) c.chunks * c.file_set * c.miss; + // a chunk streams every expert the GPU does not hold: the loan returns to that set for the length of the prompt + const double stream = std::max(0.0, (double) t.experts_total - (double) t.gpu_cache + (double) o.lend_bytes); + double ring_pen = 1.0; + if (o.ring_full > 16 && o.ring < o.ring_full) + ring_pen = 1.0 + h.ring_loss * (1.0 - (double) (o.ring - 16) / (double) (o.ring_full - 16)); + const double compute = (double) tokens / h.compute_tok_s * ring_pen; + c.seconds = compute + (double) c.chunks * stream / (h.h2d_gbps * 1e9) + c.read_bytes / (h.ssd_gbps * 1e9); + return c; +} + +struct Pick { + Cost best, baseline; + bool changed = false; +}; + +/// The option with the least predicted time. `baseline` (today's choice) is kept unless the pick saves at least +/// `min_gain` of its time AND at least `min_save_s` seconds: a plan that fits the page cache is never touched, and +/// near-ties keep the established behaviour. +inline Pick choose(const Host& h, const Tier& t, int64_t tokens, const std::vector