diff --git a/CMakeLists.txt b/CMakeLists.txt index b93a5a4c..5460231b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1075,6 +1075,10 @@ if(STRATA_BUILD_TESTS OR STRATA_BUILD_CONVERSATION_TESTS) add_executable(message_boundary_test src/program/message_boundary_test.cpp) target_include_directories(message_boundary_test PRIVATE include) add_test(NAME message_boundary_test COMMAND message_boundary_test) + # a layer split's ring override and the lend slots sized with it: the RAM copy must keep every borrowed slot (CPU) + add_executable(split_ring_test src/program/split_ring_test.cpp) + target_include_directories(split_ring_test PRIVATE include) + add_test(NAME split_ring_test COMMAND split_ring_test) # the serve loop's conversation-cache retention policy (header-only, no library to link) add_executable(conv_cache_test src/program/conv_cache_test.cpp) target_include_directories(conv_cache_test PRIVATE include) diff --git a/docs/MULTI_GPU.md b/docs/MULTI_GPU.md index 5659f97e..5553ee50 100644 --- a/docs/MULTI_GPU.md +++ b/docs/MULTI_GPU.md @@ -127,6 +127,23 @@ The split with `--mmap-experts` catches up once the OS file cache holds the expe running; the resident copy is there from the first request and stays locked when other programs need the RAM. With `--pcie-frac 0 --adapt-every 0` the split's greedy output is the same with either mode. +**The prompt path's loan on a split and its streamed ring (`STRATA_SPLIT_RING`).** Under a split the prompt path borrows +the tail slots of each card's expert cache for its buffers (they are given back after the prompt), and the resident RAM +copy keeps those experts too, so a borrowed expert is not read back from the model file during a prompt. How many slots +are borrowed depends on the prompt path's streamed ring: a split uses 96 ring slots when at least 75% of the (layer, +expert) pairs sit in some card's cache, and `STRATA_SPLIT_RING=N` sets N slots (`0` goes back to the pinned-share rule). +The ring used to be chosen after the RAM copy's regions were sized, so the regions were sized for a different ring than +the prompt path then used: with `STRATA_SPLIT_RING=384` it borrowed 5,396 slots and the copy kept 4,662, and the +other 734 were read from the model file in every chunk. The ring is now chosen first (`Prefill::set_ring_override` +says it must be set before the buffers are counted). The start-up line `strata serve: lend sizing: the prompt path +borrows N slots (ring R slots at chunk C), K of them keep their experts in RAM too` shows both numbers, and a +`WARNING` follows when K is below N. Without a split, or without `--resident-experts`, nothing changes. With a split +the order of the two steps changed for every ring choice, the 96-slot rule included: its regions used to be sized for +the larger default ring, so there the copy probably kept more slots than were borrowed (not measured). The 5,396 / +4,662 figures are from one start on the test machine (2x RTX 3080 20 GB, UD-Q4_K_XL, `--layer-split 23`); +TODO-EVIDENCE (PREvidence: borrowed and kept slots before and after, with `STRATA_SPLIT_RING` unset and at 384, and the +prompt time of a long prompt, n and the A/A). + **A separate VRAM reserve for the later cards:** `--vram-reserve-later-mib N` (default: `--vram-reserve-mib`'s value). The card that drives the monitors needs more headroom than one that drives none; with the display on the last card, `--vram-reserve-mib 300 --vram-reserve-later-mib 1800` gives the first card's cache that VRAM. diff --git a/include/strata/program/split_ring.hpp b/include/strata/program/split_ring.hpp new file mode 100644 index 00000000..13489e16 --- /dev/null +++ b/include/strata/program/split_ring.hpp @@ -0,0 +1,36 @@ +#pragma once + +#include +#include + +// A layer split's prompt-path ring and the lend slots sized from it, kept apart from the engine so they can be tested +// on the CPU (header-only, like pipeline_gate.hpp). +// +// 1. split_ring_slots: the streamed-ring override a split chooses (STRATA_SPLIT_RING, else 96 when most experts are +// resident). The choice must be made BEFORE anything sizes the prompt path with Prefill::bytes_needed (which counts +// ring_slots * max_blob): the split's lend regions (the RAM copies of the experts the prompt path borrows) used to +// be sized first, with the default ring, so a larger ring borrowed more slots than the copy kept. +// 2. split_lend_slots: how many tail slots of a cache `need` bytes take - the scan's own arithmetic, used for the +// RAM-copy region and for the borrow itself so the two cannot disagree. +namespace strata::program { + +/// The ring override (slots) for a layer split, 0 = none (the pinned-share rule stays). +/// env STRATA_SPLIT_RING as read from the environment (nullptr = unset); 0 / non-numeric = the pinned-share rule +/// res_share the share of (layer, expert) pairs resident in some stage cache, 0..1 +inline int split_ring_slots(const char* env, double res_share) { + const int ring = env != nullptr ? std::atoi(env) : (res_share >= 0.75 ? 96 : 0); + return ring > 0 ? ring : 0; +} + +/// The number of tail slots of a cache of `slots` slots whose bytes cover `need`. `offsets` is the cache's per-slot +/// byte-offset prefix sum (slots + 1 entries, `bytes` = offsets[slots]) or nullptr when every slot is `max_blob` bytes. +inline int64_t split_lend_slots(int64_t slots, int64_t bytes, const uint64_t* offsets, uint64_t max_blob, uint64_t need) { + if (offsets != nullptr) { + int64_t k = 0; + while (k < slots && (uint64_t) (bytes - (int64_t) offsets[slots - k]) < need) ++k; + return k; + } + return max_blob == 0 ? 0 : (int64_t) ((need + max_blob - 1) / max_blob); +} + +} // namespace strata::program diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 78da2dba..3e143f53 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -58,6 +58,7 @@ #include "strata/core/mtp.hpp" #include "strata/prefill/prefill.hpp" #include "strata/core/native_dense.hpp" +#include "strata/program/split_ring.hpp" #include "strata/program/logits_selection.hpp" #include "strata/program/conv_cache.hpp" #include "strata/program/message_boundary.hpp" @@ -6088,6 +6089,24 @@ int main(int argc, char** argv) { : nullptr; if (why != nullptr) adapt_async_off(why); } + // #340 / STRATA_SPLIT_RING: a layer split's streamed-ring override, decided from the stage caches' residency. It + // has to be in place BEFORE the split's lend regions are sized (Prefill::bytes_needed counts the ring): they were + // sized with the default ring and the override applied afterwards, so a ring of 384 borrowed 5396 slots and the RAM + // copy kept 4662 (the rest streamed from the GGUF mmap on every chunk). Called once; later calls do nothing. + bool split_ring_decided = false; + auto decide_split_ring = [&]() { + if (split_ring_decided || !o.serve || !multi_gpu || host_res.empty()) return; + split_ring_decided = true; + int64_t res_n = 0; + for (const int32_t r : host_res) res_n += r >= 0; + const double res_share = (double) res_n / (double) host_res.size(); + const int ring = strata::program::split_ring_slots(std::getenv("STRATA_SPLIT_RING"), res_share); + if (ring > 0) { + strata::prefill::Prefill::set_ring_override(ring); + std::fprintf(stderr, "strata serve: layer split: %.0f%% of the experts resident, the prompt path's " + "streamed ring %d slots\n", 100.0 * res_share, ring); + } + }; if (o.resident_cpu_experts) { int64_t lend_from = -1; if (o.prefill_chunk > 0 && !o.no_prefill_borrow && d_res != nullptr && xcache.slots() > 0) { @@ -6119,6 +6138,7 @@ int main(int argc, char** argv) { // is spent on it. The single-GPU loan is untouched: without a split the lend region stays in // pin_cache_complement's own path, and no regions are set. std::vector>> stage_lends; + decide_split_ring(); // the ring the prompt path really uses, BEFORE bytes_needed sizes the regions below if (pf_borrow && d_res != nullptr && o.prefill_chunk > 0 && multi_gpu && !stages.empty()) { const int64_t bound = o.prefill_auto ? auto_ceiling : o.prefill_chunk; static const bool own_ok = [] { @@ -6140,14 +6160,10 @@ int main(int argc, char** argv) { if ((uint64_t) fb >= need + (3ull << 29)) continue; } else (void) cudaGetLastError(); } - int64_t k = 0; // its tail slots, as the scan's `cache_slots_for` counts them - if (xc.slot_offsets() != nullptr) { - while (k < xc.slots() && - (uint64_t) (xc.bytes() - (int64_t) xc.slot_offsets()[xc.slots() - k]) < need) ++k; - } else { - const int64_t blob = (int64_t) strata::kernels::cpu::expert_layout().max_blob; - k = (int64_t) ((need + (uint64_t) blob - 1) / (uint64_t) blob); - } + // its tail slots, as the scan's `cache_slots_for` counts them + int64_t k = strata::program::split_lend_slots( + xc.slots(), xc.bytes(), xc.slot_offsets(), + (uint64_t) strata::kernels::cpu::expert_layout().max_blob, need); k = std::min(k, xc.slots() - 128); // the scan's 128-slot floor if (o.prefill_auto) k = std::min(k, kAutoLendPct * xc.slots() / 100); // the auto cap if (k <= 0) continue; @@ -6298,14 +6314,8 @@ int main(int argc, char** argv) { // bytes -> slots for one cache: exact when it knows its per-slot offsets (a native pack's blobs differ // per layer), otherwise max_blob each. auto cache_slots_for = [&](const strata::core::ExpertCache& xc, uint64_t need) -> int64_t { - if (xc.slot_offsets() != nullptr) { // sized slots: from the end until they hold `need` - int64_t k = 0; - while (k < xc.slots() && - (uint64_t) (xc.bytes() - (int64_t) xc.slot_offsets()[xc.slots() - k]) < need) ++k; - return k; - } - const int64_t blob = (int64_t) strata::kernels::cpu::expert_layout().max_blob; - return (int64_t) ((need + (uint64_t) blob - 1) / (uint64_t) blob); + return strata::program::split_lend_slots(xc.slots(), xc.bytes(), xc.slot_offsets(), + (uint64_t) strata::kernels::cpu::expert_layout().max_blob, need); }; auto part_slots = [&](const PfPart& p, int64_t c) -> int64_t { return cache_slots_for(*p.cache, strata::prefill::Prefill::bytes_needed(g, *p.ses, c)); @@ -6320,18 +6330,8 @@ int main(int argc, char** argv) { // the 384-slot ring (sized for a card that streams nearly every expert of a chunk) only makes every stage's // loan bigger: 96 slots (0.1.30's ring here) when >= 75% of the (layer, expert) pairs are resident. One GPU // keeps the pinned-share rule. STRATA_SPLIT_RING=N: N slots on a split; 0: the pinned-share rule. - if (multi_gpu && !host_res.empty()) { - int64_t res_n = 0; - for (const int32_t r : host_res) res_n += r >= 0; - const double res_share = (double) res_n / (double) host_res.size(); - const char* v = std::getenv("STRATA_SPLIT_RING"); - const int ring = v ? std::atoi(v) : (res_share >= 0.75 ? 96 : 0); - if (ring > 0) { - strata::prefill::Prefill::set_ring_override(ring); - std::fprintf(stderr, "strata serve: layer split: %.0f%% of the experts resident, the prompt path's " - "streamed ring %d slots\n", 100.0 * res_share, ring); - } - } + // (decided above, before the lend regions were sized: a no-op here unless the resident RAM mode skipped it) + decide_split_ring(); std::vector pf_parts; // 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 @@ -6536,6 +6536,25 @@ int main(int argc, char** argv) { pf_parts[i].dev, (long long) (pf_parts[i].cache->slots() - pf_parts[i].first), (long long) pf_parts[i].cache->slots(), (double) part_bytes(pf_parts[i], pf_parts[i].first) / 1073741824.0); + // A split's RAM copy keeps the experts of the slots the prompt path borrows (the lend regions sized above): + // every borrowed expert not kept is streamed from the GGUF mmap during every chunk. Say both counts side + // by side - they used to differ whenever a ring override (STRATA_SPLIT_RING) was set, because the regions + // were sized with the default ring before the override applied. + if (multi_gpu && src.complement_ready()) { + int64_t borrowed = borrow != nullptr ? xcache.slots() - lend_first : 0; + for (size_t i = 1; i < pf_parts.size(); ++i) + if (pf_parts[i].first >= 0) borrowed += pf_parts[i].cache->slots() - pf_parts[i].first; + const int64_t kept = src.resident_lent_slots(); + std::fprintf(stderr, "strata serve: lend sizing: the prompt path borrows %lld slots (ring %lld slots at " + "chunk %lld), %lld of them keep their experts in RAM too\n", + (long long) borrowed, + (long long) strata::prefill::Prefill::ring_slots_for(o.prefill_chunk), + (long long) o.prefill_chunk, (long long) kept); + if (kept < borrowed) + std::fprintf(stderr, "strata serve: WARNING: %lld of the %lld borrowed slots have no RAM copy: " + "their experts are streamed from the model file during every chunk\n", + (long long) (borrowed - kept), (long long) borrowed); + } } else { std::fprintf(stderr, "strata serve: the prompt path allocates its own buffers (too few cache slots to borrow)\n"); } diff --git a/src/program/split_ring_test.cpp b/src/program/split_ring_test.cpp new file mode 100644 index 00000000..71fbd3d6 --- /dev/null +++ b/src/program/split_ring_test.cpp @@ -0,0 +1,84 @@ +#include "strata/program/split_ring.hpp" + +#include +#include +#include + +using namespace strata::program; + +#define CHECK(cond) \ + do { \ + if (!(cond)) { \ + std::printf("split_ring_test FAIL line %d: %s\n", __LINE__, #cond); \ + return 1; \ + } \ + ++checks; \ + } while (0) + +// a cache of `slots` slots whose blobs vary per layer like a native pack's (2.4 .. 3.6 MB), as the prefix sum the +// expert cache keeps +static std::vector make_offsets(int64_t slots) { + std::vector off((size_t) slots + 1, 0); + for (int64_t i = 0; i < slots; ++i) off[(size_t) i + 1] = off[(size_t) i] + 2400000ull + (uint64_t) (i % 13) * 100000ull; + return off; +} + +int main() { + int checks = 0; + + // 1. the ring the split chooses: the env wins, else 96 when most experts are resident, 0 = the pinned-share rule + CHECK(split_ring_slots(nullptr, 0.9) == 96); + CHECK(split_ring_slots(nullptr, 0.75) == 96); + CHECK(split_ring_slots(nullptr, 0.74) == 0); + CHECK(split_ring_slots("384", 0.9) == 384); + CHECK(split_ring_slots("384", 0.1) == 384); + CHECK(split_ring_slots("512", 0.9) == 512); + CHECK(split_ring_slots("0", 0.9) == 0); + CHECK(split_ring_slots("-3", 0.9) == 0); + CHECK(split_ring_slots("junk", 0.9) == 0); + + // 2. the lend count: uniform slots round up, sized slots walk from the end + CHECK(split_lend_slots(1000, 1000 * 100, nullptr, 100, 0) == 0); + CHECK(split_lend_slots(1000, 1000 * 100, nullptr, 100, 1) == 1); + CHECK(split_lend_slots(1000, 1000 * 100, nullptr, 100, 100) == 1); + CHECK(split_lend_slots(1000, 1000 * 100, nullptr, 100, 101) == 2); + CHECK(split_lend_slots(1000, 0, nullptr, 0, 500) == 0); // no blob size: nothing to count + { + const std::vector off = make_offsets(50); + const int64_t bytes = (int64_t) off.back(); + CHECK(split_lend_slots(50, bytes, off.data(), 3600000, 0) == 0); + const uint64_t last = off[50] - off[49]; + CHECK(split_lend_slots(50, bytes, off.data(), 3600000, last) == 1); + CHECK(split_lend_slots(50, bytes, off.data(), 3600000, last + 1) == 2); + CHECK(split_lend_slots(50, bytes, off.data(), 3600000, off[50]) == 50); + CHECK(split_lend_slots(50, bytes, off.data(), 3600000, off[50] + 1) == 50); // never past the cache + } + + // 3. the order: the RAM copy's lend region and the borrow must be sized with the SAME ring. The need of a chunk is + // its own buffers plus ring_slots * max_blob (Prefill::bytes_needed); the engine used to size the region with the + // default ring (96) and apply the STRATA_SPLIT_RING override afterwards, so a ring of 384 borrowed more slots + // than the region kept. + { + const int64_t slots = 6000; + const std::vector off = make_offsets(slots); + const int64_t bytes = (int64_t) off.back(); + const uint64_t max_blob = 3600000, buffers = 3000ull << 20; + auto need = [&](int ring) { return buffers + (uint64_t) ring * max_blob; }; + auto lend = [&](int ring) { return split_lend_slots(slots, bytes, off.data(), max_blob, need(ring)); }; + + const int default_ring = 96; + const int ring = split_ring_slots("384", 0.9); // STRATA_SPLIT_RING=384 + const int64_t borrowed = lend(ring); // what the prompt path borrows with the override in force + const int64_t kept_old_order = lend(default_ring); // the region sized before the override applied + const int64_t kept_new_order = lend(split_ring_slots("384", 0.9)); // decided first, then sized + CHECK(kept_old_order < borrowed); // the bug: borrowed experts without a RAM copy + CHECK(kept_new_order == borrowed); // the fix: the copy keeps every borrowed slot + // no override in play (production: STRATA_SPLIT_RING unset, ring 96 either way): nothing changes + CHECK(lend(split_ring_slots(nullptr, 0.5) > 0 ? split_ring_slots(nullptr, 0.5) : default_ring) == + kept_old_order); + // a bigger ring borrows monotonically more + CHECK(lend(96) <= lend(256) && lend(256) <= lend(384) && lend(384) <= lend(512)); + } + + std::printf("split_ring_test OK (%d checks)\n", checks); +}