diff --git a/include/strata/prefill/prefill.hpp b/include/strata/prefill/prefill.hpp index f0182700..b3526f5b 100644 --- a/include/strata/prefill/prefill.hpp +++ b/include/strata/prefill/prefill.hpp @@ -83,6 +83,21 @@ public: /// 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); + /// The same without the streamed ring: what the chunk's own buffers cost. The auto chunk scan sizes the chunk + /// first and hands the ring what the chunk leaves over, so it needs the chunk priced on its own. + static uint64_t bytes_needed_no_ring(const core::ModelGeometry& g, const core::SessionState& ss, int64_t chunk); + + /// The streamed ring's byte budget as a slot count for this pack (the measured slot count x Q2_0's blob, over + /// max_blob, never past ring_cap()): + /// 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(); + + /// 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 + /// Monitor tab shows the pair the run really got, not what it asked for. + static int64_t ring_slots_for(int64_t chunk); + /// Positions [pos0, pos0 + n) holding `tokens`; `ss.ple_prev` must be the two tokens before pos0 (oldest /// first, -1 for none) and is advanced to the last two of these. bool run(const int64_t* tokens, int64_t n, int64_t pos0, std::string& err); diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cpp index 82cea8a1..4f2654b0 100644 --- a/src/prefill/prefill.cpp +++ b/src/prefill/prefill.cpp @@ -110,6 +110,14 @@ double g_pinned_share = 1.0; // 8192-token chunks: 96 slots 1153 tok/s, 384 1294 (the next layer's experts arrive during its attention half) - // and 96 when a large share goes through host copies (IQ3_S on 64 GB, a third unpinned: 96 slots 1216, 256 1070 - // the host copies are the limit and the bigger ring only takes cache slots). STRATA_PREFILL_RING overrides. +// +// Those are slots on the pack they were measured on, and a slot is one WHOLE expert blob - a Q2_0 blob is +// 1,382,400 B, so 384 slots is 506 MiB. On a pack with bigger blobs the same slot count is a different amount of +// memory: 1,912 MiB on Q8_0 (5,222,400 B), which is more than half of an 8 GB card's expert cache and what held +// that card at a 1,024-token chunk when its own buffers would have fitted 6,144. Measured on the 4-way rig, one +// env var and nothing else: chunk 1,024 -> 6,144, prefill 87 -> 402 tok/s. So the budget is kept in BYTES and the +// 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 // #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 @@ -138,6 +146,22 @@ inline bool fused_ring() { // the largest ring: 512 slots; 1024 with the Q2_0 pack's fused experts (P3's smaller buffers, measured there) - the // native packs' fused layers were measured at 512 inline int ring_cap() { return fused_ring() && !strata::kernels::cpu::expert_layout().native ? RING_MAX : 512; } +// The ring's budget in bytes: the measured slot counts above, at the blob size they were measured with. It is +// bytes and not slots because a slot is one whole blob and the blob is the pack's - see the note above. +inline constexpr uint64_t Q2_0_BLOB = 1382400ull; // the blob of the pack the ring was tuned on +inline uint64_t ring_bytes() { + const uint64_t slots = fused_ring() ? 1024ull : 384ull; // #136: a fused ring holds two layers' experts + return (g_pinned_share >= 0.9 ? slots : slots / 4) * Q2_0_BLOB; +} +// ...and what that buys on THIS pack, never past ring_cap(): the slot count `init` lays out, and what the auto +// chunk scan treats as a full ring. A pack whose blobs are larger than Q2_0's gets fewer slots for the same +// bytes, which is the point - the ring competes with the expert cache for the same VRAM. +inline int ring_budget_slots() { + const int64_t per = MAXBLOB(); + const int64_t n = per > 0 ? (int64_t) (ring_bytes() / (uint64_t) per) : 0; + const int cap = ring_cap(); + return (int) (n <= 0 ? 0 : (n > cap ? cap : n)); +} inline int ring_slots(size_t T) { const char* v = std::getenv("STRATA_PREFILL_RING"); #if defined(STRATA_USE_HIP) @@ -149,8 +173,11 @@ inline int ring_slots(size_t T) { }(); if (!v && g_ring_override <= 0 && wmma) return (int64_t) T >= stream_all_min() ? 96 : STAGE; #endif - const int pinned_ring = fused_ring() ? 1024 : 384; - const int r = v ? std::atoi(v) : g_ring_override > 0 ? g_ring_override : (g_pinned_share >= 0.9 ? pinned_ring : 96); + // 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_budget_slots() : 96; + const int r = v ? std::atoi(v) : g_ring_override > 0 ? g_ring_override : 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; @@ -1317,7 +1344,15 @@ uint64_t Prefill::bytes_needed(const core::ModelGeometry& g, const core::Session o.take((size_t) GEMM_SCRATCH, ok); o.take(GEMM_WS, ok); auto f = [&](size_t n) { o.take(n, ok); }; - f(T * N); f(T * D); f(T * D); o.take(T * D, ok); f(T * LR); o.take(T * LR, ok); + // `carve`'s order, buffer for buffer: emb, R, xn, grs, xn16, lo, lo16, gated, inj, mixed, mixed_bf, mixed_h, + // bo. This counted `xn` unconditionally (carve takes it only under STRATA_GR_UNFUSED) and never counted + // `grs`. Net over-count T*(D-HC)*4 bytes: 42 MB at a 1024-token chunk, 252 MB (48 Q8_0 slots) at 6144 - the + // prompt path was told it had less room than it did. Safe - the direction is over-estimating, and `take` + // still bounds-checks - but it under-sizes every loan, so every chunk the scan picks is one step smaller. + f(T * N); f(T * D); + if (gr_unfused()) f(T * D); + f(T * HC); + o.take(T * D, ok); f(T * LR); o.take(T * LR, ok); f(T * D); f(T * HC); f(T * N); o.take(T * N, ok); o.take(T * N, ok); f(T * N); if (bf16x2_hc()) { o.take(T * D, ok); o.take(T * LR, ok); } if (bf16x2()) o.take(T * N, ok); @@ -1345,6 +1380,14 @@ uint64_t Prefill::bytes_needed(const core::ModelGeometry& g, const core::Session return o.used + (8u << 20); // alignment slack } +uint64_t Prefill::bytes_needed_no_ring(const core::ModelGeometry& g, const core::SessionState& ss, int64_t chunk) { + 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_slots_for(int64_t chunk) { return ring_slots((size_t) chunk); } + namespace { const core::WeightRef* need(const core::LayerView& v, const char* suffix, std::string& err) {