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.
This commit is contained in:
1 parent
efa888b143
commit
e77f1dfc3f
4 files changed
+427
-3
No files matched your search
+174
-3
@@ -76,6 +76,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"
|
||||
@@ -1349,6 +1350,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] = {};
|
||||
@@ -1951,6 +1970,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;
|
||||
@@ -2006,6 +2026,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;
|
||||
}
|
||||
|
||||
@@ -7380,6 +7401,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.pack, why);
|
||||
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
|
||||
@@ -8066,7 +8190,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.
|
||||
@@ -8081,7 +8205,37 @@ 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);
|
||||
}
|
||||
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;
|
||||
@@ -8096,7 +8250,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)
|
||||
@@ -14981,7 +15135,24 @@ 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) 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;
|
||||
|
||||
Reference in new issue
Block a user