From 652f5fbd35fe340ae0c55f75f30c474ae451ce98 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Sat, 14 Nov 2026 03:38:17 +0100 Subject: [PATCH] generate: the elastic K/V grows from new VRAM when the expert cache cannot give it slots With --kv-grow accepted at session init, the K/V pools hold physical memory only for their first 16,384 cells. The start of the elastic K/V then switched growth off completely when the expert cache could not give slots (no VMM range, no residency table or expert source, or a cache top at or under its floor of 128 slots, as with --expert-cache 100). The pools stayed at their first cells, and a longer prompt wrote past them: an illegal memory access. Now the K/V grows whenever its pools are elastic. The cache gives it slots only when it can; otherwise every grow takes new VRAM, as it already does when the cache is at its floor, and the K/V is not trimmed back (there is no cache slot to return). Nothing changes when the cache can give slots. kv_grow_rules.hpp holds the decision and the grow target. Tests: kv_grow_rules_test (CPU) and kv_grow_refused_test (GPU: elastic pools grown from new VRAM to a 20,000-token prompt's cells and to the whole context, every mapped cell writable). --- CMakeLists.txt | 4 + cmake/strata_gpu_engine.cmake | 6 + include/strata/core/kv_grow_rules.hpp | 39 ++++++ src/program/generate.cpp | 166 ++++++++++++++------------ tests/core/kv_grow_refused_test.cpp | 95 +++++++++++++++ tests/core/kv_grow_rules_test.cpp | 51 ++++++++ 6 files changed, 285 insertions(+), 76 deletions(-) create mode 100644 include/strata/core/kv_grow_rules.hpp create mode 100644 tests/core/kv_grow_refused_test.cpp create mode 100644 tests/core/kv_grow_rules_test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index b1307e5ece..60c0563aa8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -314,6 +314,10 @@ if(STRATA_BUILD_TESTS) add_executable(reader_room_test tests/core/reader_room_test.cpp) target_include_directories(reader_room_test PRIVATE include) add_test(NAME reader_room_test COMMAND reader_room_test) + # the elastic K/V once the expert cache is known: it always grows when the pools are elastic, from the cache only when it can give (no GPU) + add_executable(kv_grow_rules_test tests/core/kv_grow_rules_test.cpp) + target_include_directories(kv_grow_rules_test PRIVATE include) + add_test(NAME kv_grow_rules_test COMMAND kv_grow_rules_test) # the wait before READY on an integrated AMD GPU (the counter reader and the wait, faked clock; no GPU) add_executable(kfd_settle_test tests/core/kfd_settle_test.cpp) target_include_directories(kfd_settle_test PRIVATE include) diff --git a/cmake/strata_gpu_engine.cmake b/cmake/strata_gpu_engine.cmake index 6cb4e3f8c4..9d2674cfa2 100644 --- a/cmake/strata_gpu_engine.cmake +++ b/cmake/strata_gpu_engine.cmake @@ -325,6 +325,12 @@ if(STRATA_ENABLE_CUDA OR STRATA_ENABLE_HIP) target_link_libraries(vmm_test PRIVATE strata_engine) add_test(NAME vmm_test COMMAND vmm_test) set_tests_properties(vmm_test PROPERTIES TIMEOUT 60) + # the elastic K/V's pools when the expert cache cannot give them slots: they grow from new VRAM to a long prompt's + # cells and to the whole context, every mapped cell writable (GPU with virtual memory; skips with 77 without one) + add_executable(kv_grow_refused_test tests/core/kv_grow_refused_test.cpp) + target_link_libraries(kv_grow_refused_test PRIVATE strata_engine) + add_test(NAME kv_grow_refused_test COMMAND kv_grow_refused_test) + set_tests_properties(kv_grow_refused_test PROPERTIES TIMEOUT 60 SKIP_RETURN_CODE 77) # The resident RAM mode with a layer split: the copy keeps each stage's lend region, and the experts the # prompt path borrows are served from RAM (the file source reads none of them). The device calls are # wrapped - conversation_transfer_test's pattern - and `pin = false` keeps the copy pageable, so this diff --git a/include/strata/core/kv_grow_rules.hpp b/include/strata/core/kv_grow_rules.hpp new file mode 100644 index 0000000000..8fd3ada071 --- /dev/null +++ b/include/strata/core/kv_grow_rules.hpp @@ -0,0 +1,39 @@ +// include/strata/core/kv_grow_rules.hpp - the elastic K/V (--kv-grow) once the expert cache is known. +// +// When --kv-grow is accepted at session init, every QSA pool lives in its own VMM range with physical memory for its +// first cells only (16,384 by default; layer.cpp). From then on a request may only run once its cells are mapped, so the +// K/V must be able to grow for the whole run. It grows in two ways: the expert cache gives it the chunks of slots just +// below the prompt path's loan (the experts there are given up), and when the cache has nothing to give, new VRAM. +// The cache can give slots only with a VMM range of its own, a residency table, an expert source to compute the given-up +// experts from, and a top above its floor; without one of those the K/V still grows, from new VRAM alone. (Before, the +// whole elastic K/V was switched off in that case while its pools stayed at their first cells, and a longer prompt wrote +// past them: an illegal memory access.) +#pragma once + +#include +#include + +namespace strata::core { + +struct KvGrowStart { + bool on = false; ///< the K/V grows before a request that needs more cells (the pools are elastic) + bool from_cache = false; ///< ... and the expert cache gives it slots; false: new VRAM only, the cache stays whole +}; + +/// `pools_elastic`: the session's pools were made elastic at init (qsa_kv_elastic()). `cache_vmm`: the expert cache +/// is one VMM range; `residency`: a residency table on the device and the host; `source`: an expert source; `top`: the +/// slot the K/V may take room below (the prompt path's loan starts there); `floor`: the slots the cache always keeps. +inline KvGrowStart kv_grow_start(bool pools_elastic, bool cache_vmm, bool residency, bool source, int64_t top, + int64_t floor) { + KvGrowStart s; + s.on = pools_elastic; + s.from_cache = pools_elastic && cache_vmm && residency && source && top > floor; + return s; +} + +/// The cells one grow maps for a request that needs `cells`: whole steps of `step` cells, at most the context. +inline int64_t kv_grow_target(int64_t cells, int64_t step, int64_t max_context) { + return std::min(max_context, (cells + step - 1) / step * step); +} + +} // namespace strata::core diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 98173e7277..2c80f678cf 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -55,6 +55,7 @@ #include "strata/core/on_device.hpp" #include "strata/core/peer_experts.hpp" #include "strata/core/layer.hpp" +#include "strata/core/kv_grow_rules.hpp" #include "strata/core/layout.hpp" #include "strata/core/session.hpp" #include "strata/core/weights.hpp" @@ -10470,7 +10471,8 @@ int main(int argc, char** argv) { // addresses move, so every captured graph stays valid. The context is never smaller: --max-context cells always // fit, the cache simply holds fewer experts while a long one is live. struct KvGrow { - bool on = false; + bool on = false; // the pools are elastic: every request's cells are mapped before it runs + bool from_cache = false; // ... and the expert cache gives them its slots (else new VRAM only) int64_t top = 0, lo = 0; // slots [lo, top) hold no expert; their whole chunks may be with the K/V int64_t floor = 128; // the cache keeps at least these slots int64_t step = 8192; // the K/V grows in whole steps of cells @@ -10478,13 +10480,21 @@ int main(int argc, char** argv) { std::vector spare; // out of the cache, not (yet) in the K/V int64_t grows = 0, trims = 0, fresh = 0, evicted = 0, refilled = 0; } kvg; + // Pools made elastic at session init map only their first cells, so they grow whenever the session is elastic: a + // cache that cannot give slots (no VMM range, residency table or expert source, or a top at or under the floor) + // only means the K/V grows from new VRAM (kv_grow_rules.hpp). Before, such a start left the pools at their first + // cells and a longer prompt wrote past them (an illegal memory access). auto kvg_start = [&](int64_t top) { - kvg.on = strata::core::qsa_kv_elastic() && xcache.vmm_range() != nullptr && d_res != nullptr && - !host_res.empty() && srcp != nullptr && top > kvg.floor; + const strata::core::KvGrowStart s = strata::core::kv_grow_start( + strata::core::qsa_kv_elastic(), xcache.vmm_range() != nullptr, d_res != nullptr && !host_res.empty(), + srcp != nullptr, top, kvg.floor); + kvg.on = s.on; + kvg.from_cache = s.from_cache; if (!kvg.on) return; kvg.top = kvg.lo = top; kvg.cells = strata::core::qsa_kv_elastic_cells(); if (const char* v = std::getenv("STRATA_KV_GROW_STEP"); v != nullptr && std::atoll(v) > 0) kvg.step = std::atoll(v); + if (!kvg.from_cache) return; // tests: the cache keeps this many slots (its size: the K/V grows into new VRAM only) if (const char* v = std::getenv("STRATA_KV_GROW_FLOOR"); v != nullptr && std::atoll(v) > 0) kvg.floor = std::atoll(v); std::fprintf(stderr, "strata generate: elastic K/V: %lld of %lld cells in VRAM (%.2f of %.2f GiB); the expert " @@ -10498,85 +10508,89 @@ int main(int argc, char** argv) { // the adaptive tier's swaps (a swap in flight could still be writing a slot given up here). auto kvg_ensure = [&](int64_t cells, const std::function& quiesce) -> bool { if (!kvg.on || cells <= kvg.cells) return true; - const int64_t target = std::min(o.max_context, (cells + kvg.step - 1) / kvg.step * kvg.step); + const int64_t target = strata::core::kv_grow_target(cells, kvg.step, o.max_context); const int64_t need = strata::core::qsa_kv_elastic_need(target); if (need == 0) { kvg.cells = strata::core::qsa_kv_elastic_cells(); return true; } quiesce(); - strata::core::VmmRange& r = *xcache.vmm_range(); - const uint64_t G = strata::core::vmm_granularity(); - std::vector owner; // slot -> residency index - // The slots given up are the ones just below the prompt path's loan, and the loan is most of the cache: they - // hold experts of middling heat. So an expert there that is hotter than the coldest one in the loan region - // moves into that one's slot (a device copy), and the coldest is given up instead - the cache then holds what - // a smaller cache would. Heat: the adaptive tier's routing counts, then the profile's rank. - std::vector rank; - std::vector> cold; // the loan region's residents, hottest first - auto heat = [&](int32_t i) -> float { - const float u = drive.d.usage.empty() ? 0.0f : drive.d.usage[(size_t) i]; - return u * 1048576.0f - (float) rank[(size_t) i]; - }; - const auto& lay = strata::kernels::cpu::expert_layout(); int64_t gave = 0, moved = 0; - auto freeable = [&]() -> int64_t { // mapped chunks wholly inside [lo, top): a chunk a live slot shares stays - const int64_t c0 = (int64_t) ((xcache.slot_offset(kvg.lo) + G - 1) / G); - const int64_t c1 = (int64_t) (xcache.slot_offset(kvg.top) / G); - int64_t k = 0; - for (int64_t c = c0; c < c1; ++c) k += r.mapped(c) ? 1 : 0; - return k; - }; - while ((int64_t) kvg.spare.size() + freeable() < need && kvg.lo > kvg.floor) { - if (owner.empty()) { - owner.assign((size_t) xcache.slots(), -1); - for (size_t i = 0; i < host_res.size(); ++i) - if (host_res[i] >= 0 && host_res[i] < xcache.slots()) owner[(size_t) host_res[i]] = (int32_t) i; - rank.assign(host_res.size(), (int32_t) profile.size()); - for (size_t r = 0; r < profile.size(); ++r) - rank[(size_t) profile[r].first * (size_t) g.n_expert + (size_t) profile[r].second] = (int32_t) r; - for (size_t i = 0; i < host_res.size(); ++i) - if (host_res[i] >= kvg.top) cold.emplace_back(heat((int32_t) i), (int32_t) i); - std::sort(cold.begin(), cold.end(), [](const auto& a, const auto& b) { return a.first > b.first; }); - } - --kvg.lo; - ++gave; - if (const int32_t oi = owner[(size_t) kvg.lo]; oi >= 0) { - const int64_t layer = oi / g.n_expert; - int32_t vi = -1; - if (!cold.empty() && cold.back().first < heat(oi)) { - vi = cold.back().second; - const int32_t vs = host_res[(size_t) vi]; - nib::guard_raw(layer, "the elastic K/V's slot move (kvg_ensure)"); - if (xcache.slot_offset(vs + 1) - xcache.slot_offset(vs) < lay.blob_bytes(layer) || - cudaMemcpy(xcache.device_slot(vs), xcache.device_slot((int32_t) kvg.lo), - (size_t) lay.blob_bytes(layer), cudaMemcpyDeviceToDevice) != cudaSuccess) - vi = -1; - else { - cold.pop_back(); - host_res[(size_t) oi] = vs; - host_res[(size_t) vi] = strata::core::kNotResident; - ++moved; + // the expert cache gives the K/V slots only when kvg_start found it able to (kvg.from_cache); otherwise the + // K/V grows from new VRAM alone (grow_take below), as it does when the cache is at its floor + if (kvg.from_cache) { + strata::core::VmmRange& r = *xcache.vmm_range(); + const uint64_t G = strata::core::vmm_granularity(); + std::vector owner; // slot -> residency index + // The slots given up are the ones just below the prompt path's loan, and the loan is most of the cache: they + // hold experts of middling heat. So an expert there that is hotter than the coldest one in the loan region + // moves into that one's slot (a device copy), and the coldest is given up instead - the cache then holds what + // a smaller cache would. Heat: the adaptive tier's routing counts, then the profile's rank. + std::vector rank; + std::vector> cold; // the loan region's residents, hottest first + auto heat = [&](int32_t i) -> float { + const float u = drive.d.usage.empty() ? 0.0f : drive.d.usage[(size_t) i]; + return u * 1048576.0f - (float) rank[(size_t) i]; + }; + const auto& lay = strata::kernels::cpu::expert_layout(); + auto freeable = [&]() -> int64_t { // mapped chunks wholly inside [lo, top): a chunk a live slot shares stays + const int64_t c0 = (int64_t) ((xcache.slot_offset(kvg.lo) + G - 1) / G); + const int64_t c1 = (int64_t) (xcache.slot_offset(kvg.top) / G); + int64_t k = 0; + for (int64_t c = c0; c < c1; ++c) k += r.mapped(c) ? 1 : 0; + return k; + }; + while ((int64_t) kvg.spare.size() + freeable() < need && kvg.lo > kvg.floor) { + if (owner.empty()) { + owner.assign((size_t) xcache.slots(), -1); + for (size_t i = 0; i < host_res.size(); ++i) + if (host_res[i] >= 0 && host_res[i] < xcache.slots()) owner[(size_t) host_res[i]] = (int32_t) i; + rank.assign(host_res.size(), (int32_t) profile.size()); + for (size_t r = 0; r < profile.size(); ++r) + rank[(size_t) profile[r].first * (size_t) g.n_expert + (size_t) profile[r].second] = (int32_t) r; + for (size_t i = 0; i < host_res.size(); ++i) + if (host_res[i] >= kvg.top) cold.emplace_back(heat((int32_t) i), (int32_t) i); + std::sort(cold.begin(), cold.end(), [](const auto& a, const auto& b) { return a.first > b.first; }); + } + --kvg.lo; + ++gave; + if (const int32_t oi = owner[(size_t) kvg.lo]; oi >= 0) { + const int64_t layer = oi / g.n_expert; + int32_t vi = -1; + if (!cold.empty() && cold.back().first < heat(oi)) { + vi = cold.back().second; + const int32_t vs = host_res[(size_t) vi]; + nib::guard_raw(layer, "the elastic K/V's slot move (kvg_ensure)"); + if (xcache.slot_offset(vs + 1) - xcache.slot_offset(vs) < lay.blob_bytes(layer) || + cudaMemcpy(xcache.device_slot(vs), xcache.device_slot((int32_t) kvg.lo), + (size_t) lay.blob_bytes(layer), cudaMemcpyDeviceToDevice) != cudaSuccess) + vi = -1; + else { + cold.pop_back(); + host_res[(size_t) oi] = vs; + host_res[(size_t) vi] = strata::core::kNotResident; + ++moved; + } } + if (vi < 0) host_res[(size_t) oi] = strata::core::kNotResident; + ++kvg.evicted; } - if (vi < 0) host_res[(size_t) oi] = strata::core::kNotResident; - ++kvg.evicted; } - } - // a device-to-device cudaMemcpy does not wait for the copy: the moves still read the slots given up, and a - // chunk unmapped under a copy faults it (the error then sticks to whatever runs next) - if (gave > 0 && cudaDeviceSynchronize() != cudaSuccess) { - std::fprintf(stderr, "strata: moving experts for the K/V failed: %s\n", cudaGetErrorString(cudaGetLastError())); - return false; - } - { - const int64_t c0 = (int64_t) ((xcache.slot_offset(kvg.lo) + G - 1) / G); - const int64_t c1 = (int64_t) (xcache.slot_offset(kvg.top) / G); - for (int64_t c = c0; c < c1; ++c) - if (r.mapped(c)) - if (const strata::core::VmmChunk h = r.unmap(c)) kvg.spare.push_back(h); - } - if (gave > 0 && - cudaMemcpy(d_res, host_res.data(), host_res.size() * sizeof(int32_t), cudaMemcpyHostToDevice) != cudaSuccess) { - std::fprintf(stderr, "strata: the residency table upload failed: %s\n", cudaGetErrorString(cudaGetLastError())); - return false; + // a device-to-device cudaMemcpy does not wait for the copy: the moves still read the slots given up, and a + // chunk unmapped under a copy faults it (the error then sticks to whatever runs next) + if (gave > 0 && cudaDeviceSynchronize() != cudaSuccess) { + std::fprintf(stderr, "strata: moving experts for the K/V failed: %s\n", cudaGetErrorString(cudaGetLastError())); + return false; + } + { + const int64_t c0 = (int64_t) ((xcache.slot_offset(kvg.lo) + G - 1) / G); + const int64_t c1 = (int64_t) (xcache.slot_offset(kvg.top) / G); + for (int64_t c = c0; c < c1; ++c) + if (r.mapped(c)) + if (const strata::core::VmmChunk h = r.unmap(c)) kvg.spare.push_back(h); + } + if (gave > 0 && + cudaMemcpy(d_res, host_res.data(), host_res.size() * sizeof(int32_t), cudaMemcpyHostToDevice) != cudaSuccess) { + std::fprintf(stderr, "strata: the residency table upload failed: %s\n", cudaGetErrorString(cudaGetLastError())); + return false; + } } int64_t fresh = 0; auto grow_take = [&]() -> strata::core::VmmChunk { @@ -10608,7 +10622,7 @@ int main(int argc, char** argv) { // A request that needs far fewer cells than the K/V holds gives the rest back: the slots refill with the // profile's hottest experts the GPU does not hold. Run on a quiet device (as kvg_ensure's `quiesce`). auto kvg_trim = [&](int64_t cells, int64_t hold) -> bool { // `hold`: the cells a parked conversation could need - if (!kvg.on) return true; + if (!kvg.on || !kvg.from_cache) return true; // new VRAM only: nothing to hand back to the cache // #1128: a parked conversation comes back through kvg_ensure's grow, so a K/V that fell to a short request's // size is grown again (its experts shuffled again) after every short request in between: the K/V keeps the // longest parked conversation's cells while it is parked diff --git a/tests/core/kv_grow_refused_test.cpp b/tests/core/kv_grow_refused_test.cpp new file mode 100644 index 0000000000..bac9a92c00 --- /dev/null +++ b/tests/core/kv_grow_refused_test.cpp @@ -0,0 +1,95 @@ +// The elastic K/V when the expert cache cannot give it slots (kv_grow_rules.hpp): the pools of a QSA state made elastic +// at init map only their first cells; the K/V then grows from new VRAM (the take that returns 0, as generate.cpp's +// kvg_ensure does when the cache has nothing to give) to the cells a long prompt needs and on to the whole context, and +// every cell it maps is writable and reads back. GPU with virtual memory needed; skips with 77 otherwise. +#include "strata/core/geometry.hpp" +#include "strata/core/kv_grow_rules.hpp" +#include "strata/core/layer.hpp" +#include "strata/core/layout.hpp" +#include "strata/core/vmm.hpp" + +#include + +#include +#include +#include + +using namespace strata::core; + +namespace { +int failed = 0; +#define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #x); ++failed; } } while (0) + +// cells [0, cells) of one [page][kv_head][page_size][w] pool are its first pages, contiguous +size_t pool_bytes(int64_t cells, int64_t page_size, int64_t heads, int64_t w) { + return (size_t) ((cells + page_size - 1) / page_size) * (size_t) (heads * page_size * w); +} + +bool fill_and_check(const QsaState& st, const strata::kernels::QsaShapes& s, int64_t cells, uint8_t v) { + const int64_t scb = (s.head_dim / 64) * 2; + const std::pair arrays[] = {{st.k_q, s.head_dim}, {st.v_q, s.head_dim}, {st.k_scale, scb}, {st.v_scale, scb}}; + for (const auto& [p, w] : arrays) { + const size_t n = pool_bytes(cells, s.page_size, s.n_head_kv, w); + if (cudaMemset(p, v, n) != cudaSuccess) return false; + if (cudaDeviceSynchronize() != cudaSuccess) return false; + std::vector h(4096); // the last bytes: the far end of the mapped cells + if (cudaMemcpy(h.data(), (const uint8_t*) p + (n - h.size()), h.size(), cudaMemcpyDeviceToHost) != cudaSuccess) return false; + for (const uint8_t b : h) + if (b != v) return false; + } + return true; +} +} // namespace + +int main() { + if (cudaFree(nullptr) != cudaSuccess || !vmm_available()) { + std::printf("kv_grow_refused_test: no CUDA device with virtual memory - skipped\n"); + return 77; + } + const ModelGeometry g; // the model's QSA shapes + const strata::kernels::QsaShapes s = qsa_shapes(g); + const int64_t max_cells = 65536, init = 4096; + qsa_set_kv_int8(true); + qsa_set_kv_elastic(true, init); + CHECK(qsa_kv_elastic()); + void* base = nullptr; + CHECK(cudaMalloc(&base, qsa_state_bytes(g, max_cells)) == cudaSuccess); + QsaState st; + CHECK(qsa_state_init(g, max_cells, base, st) > 0); + CHECK(st.kv_elastic >= 0 && st.k_q != nullptr && st.v_scale != nullptr); + CHECK(cudaDeviceSynchronize() == cudaSuccess); + const int64_t cells0 = qsa_kv_elastic_cells(); + std::printf("kv_grow_refused_test: first mapping %lld of %lld cells (%.1f of %.1f MiB)\n", (long long) cells0, + (long long) max_cells, (double) qsa_kv_elastic_mapped_bytes() / 1048576.0, + (double) qsa_kv_elastic_full_bytes() / 1048576.0); + CHECK(cells0 >= init && cells0 < max_cells); + CHECK(fill_and_check(st, s, cells0, 0x11)); + + // the refused state of BLOCK 6: --expert-cache 100 (128 slots, the floor), pools elastic + const KvGrowStart r = kv_grow_start(qsa_kv_elastic(), true, true, true, 128, 128); + CHECK(r.on && !r.from_cache); + // a 20,000-token prompt (kvg_ensure(n + 256)): its cells are not mapped yet, then grown from new VRAM + const int64_t want = 20000 + 256, step = 8192; + const int64_t target = kv_grow_target(want, step, max_cells); + CHECK(target >= want); + CHECK(qsa_kv_elastic_need(target) > 0); + int64_t fresh = 0; + CHECK(qsa_kv_elastic_grow(target, [&] { ++fresh; return (VmmChunk) 0; })); + CHECK(fresh > 0 && qsa_kv_elastic_need(target) == 0); + const int64_t cells1 = qsa_kv_elastic_cells(); + CHECK(cells1 >= target); + CHECK(fill_and_check(st, s, cells1, 0x22)); + // the whole context: every cell of --max-context can be mapped the same way + CHECK(qsa_kv_elastic_grow(max_cells, [] { return (VmmChunk) 0; })); + CHECK(qsa_kv_elastic_cells() >= max_cells && qsa_kv_elastic_need(max_cells) == 0); + CHECK(fill_and_check(st, s, max_cells, 0x33)); + std::printf("kv_grow_refused_test: grown from new VRAM to %lld then %lld cells (%lld new chunks for the first grow)\n", + (long long) cells1, (long long) qsa_kv_elastic_cells(), (long long) fresh); + CHECK(cudaGetLastError() == cudaSuccess); + if (failed) { + std::fprintf(stderr, "kv_grow_refused_test: %d failed\n", failed); + return 1; + } + std::printf("kv_grow_refused_test: ok\n"); + return 0; +} diff --git a/tests/core/kv_grow_rules_test.cpp b/tests/core/kv_grow_rules_test.cpp new file mode 100644 index 0000000000..727a20935a --- /dev/null +++ b/tests/core/kv_grow_rules_test.cpp @@ -0,0 +1,51 @@ +// The elastic K/V once the expert cache is known (strata/core/kv_grow_rules.hpp): pools made elastic always grow, from +// the cache's slots only when the cache can give them, else from new VRAM; a grow covers the request, never past the context. +#include "strata/core/kv_grow_rules.hpp" + +#include + +using namespace strata::core; + +namespace { +int failed = 0; +#define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #x); ++failed; } } while (0) +} // namespace + +int main() { + // pools not elastic (no --kv-grow, or it was turned off at init): nothing to grow + CHECK(!kv_grow_start(false, true, true, true, 9000, 128).on); + CHECK(!kv_grow_start(false, true, true, true, 9000, 128).from_cache); + // every condition met: grows from the cache's slots + CHECK(kv_grow_start(true, true, true, true, 9000, 128).on); + CHECK(kv_grow_start(true, true, true, true, 9000, 128).from_cache); + // the refused states: the pools are elastic, so the K/V still grows (new VRAM), the cache gives nothing + const KvGrowStart floor_hit = kv_grow_start(true, true, true, true, 128, 128); // --expert-cache 100 -> 128 slots + CHECK(floor_hit.on && !floor_hit.from_cache); + CHECK(kv_grow_start(true, true, true, true, 0, 128).on); // a loan that starts at slot 0 + CHECK(!kv_grow_start(true, true, true, true, 0, 128).from_cache); + CHECK(kv_grow_start(true, false, true, true, 9000, 128).on); // no VMM range for the cache + CHECK(!kv_grow_start(true, false, true, true, 9000, 128).from_cache); + CHECK(kv_grow_start(true, true, false, true, 9000, 128).on); // no residency table + CHECK(!kv_grow_start(true, true, false, true, 9000, 128).from_cache); + CHECK(kv_grow_start(true, true, true, false, 9000, 128).on); // no expert source + CHECK(!kv_grow_start(true, true, true, false, 9000, 128).from_cache); + CHECK(kv_grow_start(true, true, true, true, 129, 128).from_cache); // one slot above the floor + // the grow target: whole steps, covering the request, at most the context (never a smaller context) + CHECK(kv_grow_target(20256, 8192, 131072) == 24576); + CHECK(kv_grow_target(16384, 8192, 131072) == 16384); + CHECK(kv_grow_target(16385, 8192, 131072) == 24576); + CHECK(kv_grow_target(1, 8192, 131072) == 8192); + CHECK(kv_grow_target(130000, 8192, 131072) == 131072); + CHECK(kv_grow_target(131072 + 256, 8192, 131072) == 131072); + for (int64_t c = 1; c <= 131072; c += 997) { + const int64_t t = kv_grow_target(c, 8192, 131072); + CHECK(t >= c && t <= 131072 && t % 8192 == 0); + } + for (int64_t c = 1; c <= 100000; c += 991) CHECK(kv_grow_target(c, 8192, 100000) >= c); + if (failed) { + std::fprintf(stderr, "kv_grow_rules_test: %d failed\n", failed); + return 1; + } + std::printf("kv_grow_rules_test: ok\n"); + return 0; +}