okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-kvgrow-guard: --kv-grow no longer faults on a long prompt when the expert cache is at its floor513fd0cff

main
2.8 KiB51 linesraw
1// The elastic K/V once the expert cache is known (strata/core/kv_grow_rules.hpp): pools made elastic always grow, from
2// the cache's slots only when the cache can give them, else from new VRAM; a grow covers the request, never past the context.
3#include "strata/core/kv_grow_rules.hpp"
4
5#include <cstdio>
6
7using namespace strata::core;
8
9namespace {
10int failed = 0;
11#define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #x); ++failed; } } while (0)
12}  // namespace
13
14int main() {
15    // pools not elastic (no --kv-grow, or it was turned off at init): nothing to grow
16    CHECK(!kv_grow_start(false, true, true, true, 9000, 128).on);
17    CHECK(!kv_grow_start(false, true, true, true, 9000, 128).from_cache);
18    // every condition met: grows from the cache's slots
19    CHECK(kv_grow_start(true, true, true, true, 9000, 128).on);
20    CHECK(kv_grow_start(true, true, true, true, 9000, 128).from_cache);
21    // the refused states: the pools are elastic, so the K/V still grows (new VRAM), the cache gives nothing
22    const KvGrowStart floor_hit = kv_grow_start(true, true, true, true, 128, 128);   // --expert-cache 100 -> 128 slots
23    CHECK(floor_hit.on && !floor_hit.from_cache);
24    CHECK(kv_grow_start(true, true, true, true, 0, 128).on);   // a loan that starts at slot 0
25    CHECK(!kv_grow_start(true, true, true, true, 0, 128).from_cache);
26    CHECK(kv_grow_start(true, false, true, true, 9000, 128).on);   // no VMM range for the cache
27    CHECK(!kv_grow_start(true, false, true, true, 9000, 128).from_cache);
28    CHECK(kv_grow_start(true, true, false, true, 9000, 128).on);   // no residency table
29    CHECK(!kv_grow_start(true, true, false, true, 9000, 128).from_cache);
30    CHECK(kv_grow_start(true, true, true, false, 9000, 128).on);   // no expert source
31    CHECK(!kv_grow_start(true, true, true, false, 9000, 128).from_cache);
32    CHECK(kv_grow_start(true, true, true, true, 129, 128).from_cache);   // one slot above the floor
33    // the grow target: whole steps, covering the request, at most the context (never a smaller context)
34    CHECK(kv_grow_target(20256, 8192, 131072) == 24576);
35    CHECK(kv_grow_target(16384, 8192, 131072) == 16384);
36    CHECK(kv_grow_target(16385, 8192, 131072) == 24576);
37    CHECK(kv_grow_target(1, 8192, 131072) == 8192);
38    CHECK(kv_grow_target(130000, 8192, 131072) == 131072);
39    CHECK(kv_grow_target(131072 + 256, 8192, 131072) == 131072);
40    for (int64_t c = 1; c <= 131072; c += 997) {
41        const int64_t t = kv_grow_target(c, 8192, 131072);
42        CHECK(t >= c && t <= 131072 && t % 8192 == 0);
43    }
44    for (int64_t c = 1; c <= 100000; c += 991) CHECK(kv_grow_target(c, 8192, 100000) >= c);
45    if (failed) {
46        std::fprintf(stderr, "kv_grow_rules_test: %d failed\n", failed);
47        return 1;
48    }
49    std::printf("kv_grow_rules_test: ok\n");
50    return 0;
51}