mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-05 15:58:53 +00:00
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).
52 lines
2.8 KiB
C++
52 lines
2.8 KiB
C++
// 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 <cstdio>
|
|
|
|
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;
|
|
}
|