Merge f-150-planner (8253477bf) into f-split-int2

This commit is contained in:
Niko1221 committed 2026-10-18 11:13:37 +02:00
commit fd46e34d5c
4 files changed
+514 -4

No files matched your search

+205 -4
View File
@@ -77,6 +77,7 @@
#include "strata/prefill/share_rules.hpp"
#include "strata/platform/host_limits.hpp"
#include "strata/program/split_ring.hpp"
#include "strata/program/prefill_plan.hpp"
#include "strata/program/draft_kv_plan.hpp"
#include "strata/program/message_boundary.hpp"
#include "strata/spec/draft_policy.hpp"
@@ -1350,6 +1351,24 @@ void disk_wait_threads(std::FILE* f) {
}
#endif
// #2824: what the drive under `path` reads at, GB/s, for the prompt planner (it prices the file set a chunk re-reads). Linux:
// rotational disk 0.15, NVMe 2.5, other 0.45 (SATA); elsewhere 1.5. STRATA_SSD_GBPS overrides. `why` says where it came from.
double drive_read_gbps(const std::string& path, std::string& why) {
if (const char* e = std::getenv("STRATA_SSD_GBPS"); e != nullptr && std::atof(e) > 0.0) {
why = "STRATA_SSD_GBPS";
return std::atof(e);
}
#if !defined(_WIN32)
std::string dev;
const int rot = on_rotational_disk(path, dev);
if (rot == 1) { why = dev + " is a rotational disk"; return 0.15; }
if (rot == 0 && dev.rfind("nvme", 0) == 0) { why = dev + " is NVMe"; return 2.5; }
if (rot == 0) { why = dev + " is a SATA-class SSD (assumed)"; return 0.45; }
#endif
why = "assumed";
return 1.5;
}
// --pipeline-windows: what the watchdog prints about the pipelined decode loop (set while it runs)
struct PlDiag {
strata::core::Verifier* v[2][2] = {};
@@ -1952,6 +1971,7 @@ static bool pcie_auto_split_ok() {
// median would still follow a disturbance that lasts through half the bursts). Each burst takes ~10 ms on an x16
// PCIe 4 link, so the probe takes no longer than the one 1 GiB burst did. `samples`, when given, gets every
// burst's reading for the log.
double g_last_h2d_gbps = -1.0; // the last probe's reading, for the prompt planner (#2824)
double probe_pcie_h2d_gbps(std::string* samples = nullptr) {
constexpr size_t kBytes = 256ull << 20;
constexpr int kBursts = 4;
@@ -2007,6 +2027,7 @@ double probe_pcie_h2d_gbps(std::string* samples = nullptr) {
if (!ok) (void) cudaGetLastError();
cudaFree(d);
cudaFreeHost(h);
if (bw > 0.0) g_last_h2d_gbps = bw;
return bw;
}
@@ -7247,6 +7268,14 @@ int main(int argc, char** argv) {
const int64_t rounded = per > std::numeric_limits<int64_t>::max() - 255 ? per : ((per + 255) / 256) * 256;
return std::min(max_chunk, rounded);
};
// #2824: the chunk a request of `tokens` reads in when the host-aware plan chose `plan` (< cap, the buffers' chunk = what the VRAM
// scan picked). A prompt that fits one chunk of `cap` keeps ONE chunk, as before (a single pass reads each expert once, so the
// page cache cannot thrash, and splitting it would only add a pass); a longer one reads in n = ceil(tokens / plan) chunks of
// EQUAL size (no tiny tail), at most `cap`.
auto planned_chunk = [&](int64_t tokens, int64_t cap, int64_t plan) -> int64_t {
if (plan <= 0 || plan >= cap || tokens <= 0) return equal_chunk(tokens, cap);
return strata::program::prefill_plan::balanced_chunk(tokens, cap, plan);
};
// The prompt path's chunk and the slots it borrows for its buffers: the requested chunk halved until it fits,
// or with --prefill auto the largest chunk whose buffers take at most kAutoLendPct % of the slots (a
// lent slot's expert is streamed during the prompt and refilled after it; measured on a 12 GB card, 32K Q2_0
@@ -7403,6 +7432,109 @@ int main(int argc, char** argv) {
}
return 0;
};
// ---- #2824: THE PROMPT PLAN PRICES THE HOST. The scans above ask whether a loan fits the GPU cache. But a lent
// slot's expert is not in VRAM for the length of the prompt: it comes from the RAM copy if the copy kept it, else from
// the model file, and a chunk streams every expert the GPU does not hold once per chunk. When that file set is bigger
// than the RAM the OS keeps file pages in, it cycles through the page cache and each chunk reads most of it again
// (2x RTX 3060, 32 GB, IQ2_XS, 29K prompt: 8192-token chunks read 28-45 GB, 4096 read ~1 GB). So the auto pick is
// refined by prefill_plan (include/strata/program/prefill_plan.hpp): the chunk x ring grid at or below the VRAM pick,
// each priced with the expert bytes its loan displaces without a RAM backup, the RAM left for the page cache, the
// measured PCIe rate and a drive-speed class, and the least predicted prompt time wins. A set that fits the page cache
// keeps today's pick untouched. Advisory: --prefill N and STRATA_PREFILL_RING are the operator's, and
// STRATA_PREFILL_HOST_PLAN=0 turns this off. No weights or experts change; only the chunk shape (rounding).
// fits_vram(c) whether every participant can lend chunk c at the ring currently set
// lend_of(c, bytes, slots) the cache bytes and slots the loan of chunk c takes at the ring currently set
// ring_full(c) the largest ring slot count on offer for chunk c
// restore() puts the ring state back as the VRAM pick left it
// Returns the chunk to use (== `chunk` when nothing changes); the ring state is set for it.
auto host_plan_chunk = [&](int64_t chunk, uint64_t gpu_cache_bytes, int64_t cache_slots,
const std::function<bool(int64_t)>& fits_vram,
const std::function<void(int64_t, uint64_t&, int64_t&)>& lend_of,
const std::function<int64_t(int64_t)>& ring_full,
const std::function<void()>& restore, int64_t tokens_hint = 0) -> int64_t {
namespace pp = strata::program::prefill_plan;
const char* off = std::getenv("STRATA_PREFILL_HOST_PLAN");
if (!o.prefill_auto || chunk <= 0 || srcp != &src || (off != nullptr && off[0] == '0') ||
!strata::prefill::Prefill::ring_bytes_enabled())
return chunk;
const auto& lay = strata::kernels::cpu::expert_layout();
uint64_t total = 0;
for (int64_t l = 0; l < g.n_layers; ++l) total += (uint64_t) lay.blob_bytes(l) * (uint64_t) g.n_expert;
if (total == 0 || gpu_cache_bytes >= total) return chunk; // the GPU holds every expert: no file tier
const auto hm = strata::core::sample_host_memory();
if (!hm) return chunk;
const uint64_t slack = 1200ull << 20; // MemAvailable overstates what the cache can hold (~1 GiB on the #2824 box)
pp::Host host;
host.page_cache = src.unbuffered() ? 0 : (hm->available > slack ? hm->available - slack : 0);
host.h2d_gbps = g_last_h2d_gbps > 0.0 ? g_last_h2d_gbps : 12.0;
std::string why;
host.ssd_gbps = drive_read_gbps(o.native_preset.empty() ? o.pack : o.native_preset, why); // where the experts live
if (const char* e = std::getenv("STRATA_PREFILL_PLAN_TOKS"); e != nullptr && std::atof(e) > 0.0)
host.compute_tok_s = std::atof(e);
const uint64_t ram_copy = src.complement_ready() ? src.resident_bytes() : 0;
const int64_t kept_slots = src.complement_ready() ? src.resident_lent_slots() : 0;
const uint64_t slot_avg = cache_slots > 0 ? gpu_cache_bytes / (uint64_t) cache_slots : 0;
const uint64_t kept_bytes = (uint64_t) std::max<int64_t>(0, kept_slots) * slot_avg; // the copy's lend region
pp::Tier tier;
tier.experts_total = total;
tier.gpu_cache = gpu_cache_bytes;
const uint64_t ram_outside_gpu = ram_copy > kept_bytes ? ram_copy - kept_bytes : 0;
tier.file_tier = total > gpu_cache_bytes + ram_outside_gpu ? total - gpu_cache_bytes - ram_outside_gpu : 0;
tier.extra_file = o.ple_gguf.empty() ? 0 : (350ull << 20); // PLE rows the prompt touches (#2824: ~0.35 GiB)
const int64_t tokens = tokens_hint > 0 ? tokens_hint
: std::max<int64_t>(chunk, std::min<int64_t>(std::max<int64_t>(o.max_context, 4096), 32768));
std::vector<pp::Option> opts;
auto add = [&](int64_t c, int64_t ring_req) {
strata::prefill::Prefill::set_ring_budget((int) ring_req, 0);
if (!fits_vram(c)) return;
pp::Option op;
op.chunk = c;
op.ring = strata::prefill::Prefill::ring_slots_for(c);
op.ring_full = ring_full(c);
uint64_t lb = 0;
int64_t ls = 0;
lend_of(c, lb, ls);
op.lend_bytes = lb;
op.lend_kept = std::min<uint64_t>(lb, kept_bytes);
for (const pp::Option& e : opts) if (e.chunk == op.chunk && e.ring == op.ring) return;
opts.push_back(op);
};
// today's pick first, priced under the ring state it left
restore();
pp::Option base;
base.chunk = chunk;
base.ring = strata::prefill::Prefill::ring_slots_for(chunk);
base.ring_full = ring_full(chunk);
{
uint64_t lb = 0;
int64_t ls = 0;
lend_of(chunk, lb, ls);
base.lend_bytes = lb;
base.lend_kept = std::min<uint64_t>(lb, kept_bytes);
}
opts.push_back(base);
for (int64_t c = chunk; c >= 512; c -= 256) {
const int64_t full = ring_full(c);
add(c, (int) full);
if (full > 32) add(c, (int) (full / 2));
if (full > 16) add(c, 16);
}
pp::Pick pick = pp::choose(host, tier, tokens, opts, 0);
std::fprintf(stderr, "%s\n", pp::describe(pick, host, tokens).c_str());
std::fprintf(stderr, "strata: prefill plan inputs: %.1f GiB experts, %.1f GiB in the GPU caches, %.1f GiB RAM copy "
"(%lld lend slots kept), file tier %.1f GiB; %.1f GiB available for the page cache%s; PCIe %.1f GB/s; "
"drive %.2f GB/s (%s); a %lld-token prompt\n",
(double) total / pp::kGiB, (double) gpu_cache_bytes / pp::kGiB, (double) ram_copy / pp::kGiB,
(long long) kept_slots, (double) tier.file_tier / pp::kGiB, (double) host.page_cache / pp::kGiB,
src.unbuffered() ? " (the file tier reads unbuffered: none)" : "", host.h2d_gbps, host.ssd_gbps, why.c_str(),
(long long) tokens);
if (!pick.changed) {
restore();
return chunk;
}
strata::prefill::Prefill::set_ring_budget((int) pick.best.opt.ring, 0);
return pick.best.opt.chunk;
};
// ---- THE ELASTIC K/V (--kv-grow; vmm.hpp). Allocated for the whole --max-context up front, the K/V of a 262K
// context (3.35 GiB with the drafter's at int8) takes VRAM the expert cache could hold more experts in. Now the K/V pools hold physical memory only for the cells
// the requests reach. When a request needs more, slots just below the prompt path's loan give up their experts
@@ -8015,6 +8147,7 @@ int main(int argc, char** argv) {
// (decided above, before the lend regions were sized: a no-op here unless the resident RAM mode skipped it)
decide_split_ring();
std::vector<PfPart> pf_parts;
int64_t pf_plan_chunk = 0; // #2824: the host-aware plan's chunk for prompts longer than o.prefill_chunk (0: none)
// a cache too small to lend the prompt path its buffers would make it allocate them on top - on a card
// whose cache already filled its reserve, that is the over-subscription the auto sizing avoids - so the
// chunk is the largest one EVERY participant can lend (a smaller chunk only reads slower)
@@ -8106,7 +8239,7 @@ int main(int argc, char** argv) {
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);
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
// 32 GB card's split to 512 tokens: prompts 6.2x slower, decode the same). Named when it bites, so the
// regression is one log line: each stage that cannot fund the chunk CUDA0 alone would read in.
@@ -8121,7 +8254,42 @@ int main(int argc, char** argv) {
small >= chunk ? 0 : (int) ring_room(chunk, nullptr, strata::prefill::Prefill::ring_cap_for(small)),
small);
}
if (chunk > 0 && alone > chunk) {
// #2824: the VRAM pick, priced against the host (RAM left for the page cache, the displaced experts with no RAM
// backup, the drive): a smaller chunk / ring when the file set the prompt re-reads outgrows the page cache.
// The RAM copy is built by now, so its bytes and the lend slots it kept are known.
const int64_t chunk_vram = chunk;
if (chunk > 0 && o.prefill_auto && strata::prefill::Prefill::ring_bytes_enabled()) {
const int64_t small0 = old_pick(nullptr);
const int64_t cap0 = strata::prefill::Prefill::ring_cap_for(small0);
const int64_t ring0 = small0 >= chunk ? 0 : ring_room(chunk, nullptr, cap0);
auto restore_ring = [&] { strata::prefill::Prefill::set_ring_budget((int) ring0, small0); };
restore_ring();
uint64_t gc_bytes = 0;
int64_t gc_slots = 0;
for (const PfPart& p : pf_parts) {
gc_bytes += (uint64_t) std::max<int64_t>(0, (int64_t) p.cache->bytes());
gc_slots += p.cache->slots();
}
chunk = host_plan_chunk(
chunk, gc_bytes, gc_slots, [&](int64_t c) { return fits(c, true, nullptr); },
[&](int64_t c, uint64_t& bytes, int64_t& slots) {
bytes = 0;
slots = 0;
for (const PfPart& p : pf_parts) {
const int64_t k = part_slots(p, c);
if (k <= 0) continue;
slots += k;
bytes += part_bytes(p, (int32_t) (p.cache->slots() - k));
}
},
[&](int64_t c) { return ring_room(c, nullptr, cap0); }, restore_ring);
}
// the buffers stay sized for today's chunk; the plan's smaller chunk applies to prompts longer than it (planned_chunk)
if (chunk > 0 && chunk != chunk_vram) {
pf_plan_chunk = chunk;
chunk = chunk_vram;
}
if (chunk > 0 && alone > chunk_vram) {
for (size_t i = 1; i < pf_parts.size(); ++i) {
const PfPart& p = pf_parts[i];
if (fits_one(p, alone, o.prefill_auto)) continue;
@@ -8136,7 +8304,7 @@ int main(int argc, char** argv) {
std::fprintf(stderr, "strata serve: WARNING: prompt chunk %lld tokens, not %lld: CUDA%d (%s) "
"has %lld expert-cache slots, and a %lld-token chunk borrows %lld of them "
"(it must keep 128%s) - prompts read slower than on CUDA0 alone (#448)\n",
(long long) chunk, (long long) alone, dev, prop.name,
(long long) chunk_vram, (long long) alone, dev, prop.name,
(long long) p.cache->slots(), (long long) alone,
(long long) part_slots(p, alone), pct.c_str());
// the helper tiers start at CUDA1 without a split (and are enabled in order)
@@ -8151,9 +8319,15 @@ int main(int argc, char** argv) {
// the RESOLVED ring, not the room offered: ring_slots() clamps to [16, RING_MAX] and a chunk
// under stream_all_min() gets STAGE, and STRATA_PREFILL_RING overrides both. This is what
// `init` lays out, and what the INFO line reports to the Monitor tab.
{
std::fprintf(stderr, "strata serve: prompt chunk auto: %lld tokens, a %lld-slot ring\n",
(long long) chunk,
(long long) strata::prefill::Prefill::ring_slots_for(chunk));
if (pf_plan_chunk > 0)
std::fprintf(stderr, "strata serve: prompts longer than %lld tokens read in equal chunks of at most %lld "
"(the host-aware plan); shorter ones in one chunk\n",
(long long) chunk, (long long) pf_plan_chunk);
}
else if (chunk != o.prefill_chunk)
std::fprintf(stderr, "strata serve: prompt chunk %lld -> %lld tokens so its buffers fit in "
"every expert cache\n", (long long) o.prefill_chunk, (long long) chunk);
@@ -12781,7 +12955,7 @@ int main(int argc, char** argv) {
const auto t_ln = Clock::now();
// what this segment needs, capped by the configured chunk: a request lends only what its own
// prompt needs, so a large chunk costs a short prompt nothing
const int64_t want_full = equal_chunk(tokens, o.prefill_chunk);
const int64_t want_full = planned_chunk(tokens, o.prefill_chunk, pf_plan_chunk);
// #340: a short enough request reads in the stages' own S-token chunks (nothing lent)
const int64_t want_seg = o.prefill_head_alias ? o.prefill_chunk
: split_small > 0 && tokens <= split_small_max ? std::min(want_full, split_small)
@@ -15139,7 +15313,34 @@ int main(int argc, char** argv) {
uint64_t borrow_bytes = 0;
if (!o.no_prefill_borrow && !host_res.empty() && d_res != nullptr) {
int64_t chunk = o.prefill_chunk;
const int64_t chunk_in = chunk;
int64_t k = plan_lend(chunk); // auto: the largest chunk that fits; fixed: halved to fit
if (k > 0 && o.prefill_auto && n_batched > 1024) { // #2824: priced against the host, for THIS prompt's length
const int64_t vram_chunk = chunk;
chunk = host_plan_chunk(
chunk, (uint64_t) std::max<int64_t>(0, (int64_t) xcache.bytes()), xcache.slots(),
[&](int64_t c) {
const int64_t kk = lend_slots(c);
return kk + 128 <= xcache.slots() && kk * 100 <= kAutoLendPct * xcache.slots();
},
[&](int64_t c, uint64_t& bytes, int64_t& slots) {
slots = lend_slots(c);
bytes = bytes_from_slots(slots);
},
[&](int64_t) { return strata::prefill::Prefill::ring_cap_for(4096); },
[&] { int64_t c2 = chunk_in; (void) plan_lend(c2); }, n_batched);
if (chunk != vram_chunk) {
// the whole prompt fits today's chunk: one pass, keep it (the plan's ring state goes back with it)
if (request_chunk(n_batched, std::numeric_limits<int64_t>::max() / 2) <= vram_chunk) {
chunk = vram_chunk;
int64_t c2 = chunk_in;
(void) plan_lend(c2);
} else {
chunk = std::min(vram_chunk, planned_chunk(n_batched, vram_chunk, chunk)); // equal chunks, no tail
}
k = lend_slots(chunk);
}
}
const int64_t request_sized = equal_chunk(n_batched, chunk);
if (k > 0 && request_sized < chunk) { // no bigger than this prompt segment needs
chunk = request_sized;