okami.mom/strata

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

Niko1221Merge f-0213-pr5531 (9947e7d6f): community PR #5531 by pjgmobile for #5327 - on Linux the expert arena loads again when free pages, droppable file cache or a reserved hugetlb pool can serve a RAM shortfall (their measure: 1M ctx 47 -> 124 tok/s); review fixes: mlocked file pages are not droppable, stricter hugetlb credit (keeps #4051's refusal); plain warning text, stdout only with --serve; normal starts unchanged (tokens and output identical)3289d8280

main
18.5 KiB218 linesraw
1#include "strata/core/ple_residency.hpp"
2
3#include <cstdio>
4#include <cstdlib>
5#include <limits>
6
7using namespace strata::core;
8namespace {
9int checks = 0;
10void check(bool ok, const char* label) {
11    ++checks;
12    if (!ok) { std::fprintf(stderr, "FAIL: %s\n", label); std::exit(1); }
13}
14constexpr uint64_t GiB = 1ull << 30;
15}
16
17int main() {
18    const uint64_t table = 26ull * GiB + 870ull * (1ull << 20);   // IQ3_S's table, 26.8 GiB
19    const uint64_t arena = 51ull * GiB + 100ull * (1ull << 20);   // IQ3_S's 51.1 GiB of experts
20    // the owner PC, 2026-10-26: 45.2 GiB available when the table is opened, 63 GiB total.  The 0.2.7 first policy
21    // (table + 8 GiB) admitted it and the expert load then failed with error 1453.  Not admitted now.
22    check(!ple_resident_admit(45ull * GiB + (1ull << 28), table, arena, 8 * GiB), "64 GB PC with the IQ3_S arena: not admitted");
23    check(!ple_resident_admit(40ull * GiB, table, arena, 8 * GiB), "40 GiB: not admitted");
24    // STRATA_DEFAULTS=026: the policy does not run (v0.2.6 had none); an explicit STRATA_PLE_AUTO_RAM wins either way
25    check(ple_auto_ram_allowed(nullptr, false), "policy on by default");
26    check(!ple_auto_ram_allowed(nullptr, true), "policy off under STRATA_DEFAULTS=026");
27    check(ple_auto_ram_allowed("1", true), "STRATA_PLE_AUTO_RAM=1 wins over 026");
28    check(!ple_auto_ram_allowed("0", false), "STRATA_PLE_AUTO_RAM=0 is the old off switch");
29    check(!ple_auto_ram_allowed("0", true), "STRATA_PLE_AUTO_RAM=0 under 026");
30    // a PC that holds the table, the arena and 8 GiB
31    check(ple_resident_admit(table + arena + 8 * GiB, table, arena, 8 * GiB), "exactly table + arena + headroom is admitted");
32    check(!ple_resident_admit(table + arena + 8 * GiB - 1, table, arena, 8 * GiB), "one byte below is not");
33    check(ple_resident_admit(240ull * GiB, table, arena, 8 * GiB), "a 256 GB box: admitted");
34    // a smaller model's arena on a mid PC (Q2_0: 34 GiB arena): 26.8 + 34 + 8 = 68.8
35    check(ple_resident_admit(80ull * GiB, table, 34ull * GiB, 8 * GiB), "Q2_0 on 96 GB: admitted");
36    check(!ple_resident_admit(60ull * GiB, table, 34ull * GiB, 8 * GiB), "Q2_0 with 60 GiB available: not admitted");
37    // unknowns fail closed
38    check(!ple_resident_admit({}, table, arena, 8 * GiB), "unknown telemetry");
39    check(!ple_resident_admit(240ull * GiB, 0, arena, 8 * GiB), "unknown table size");
40    check(!ple_resident_admit(240ull * GiB, table, 0, 8 * GiB), "unknown arena size");
41    check(!ple_resident_admit(std::numeric_limits<uint64_t>::max(), table, std::numeric_limits<uint64_t>::max(), 8 * GiB),
42          "sizes cannot overflow");
43    check(!ple_resident_admit(0, table, arena, 0), "zero available");
44    // #4518: after the load, does the table's lock leave the automatic conversation cache no room?  (min free 2560 MiB, cache 40% of the
45    // rest, 8192 MiB at most, 1024 MiB at least)
46    {
47        const int64_t floor_mib = 2560;
48        check(!ple_lock_blocks_cache(std::nullopt, table, floor_mib), "unknown telemetry: leave the lock");
49        check(!ple_lock_blocks_cache(40ull * GiB, 0, floor_mib), "unknown table size: leave the lock");
50        check(ple_lock_blocks_cache(4ull * GiB, table, floor_mib), "the reporter: 4 GiB left with the table locked (cache off), 30 GiB without");
51        check(ple_lock_blocks_cache(10ull * GiB, table, floor_mib), "10 GiB left: cache 1.9 GiB with the lock, 8 GiB without");
52        check(!ple_lock_blocks_cache(40ull * GiB, table, floor_mib), "40 GiB left: the cache is already at its 8 GiB cap");
53        check(!ple_lock_blocks_cache(23ull * GiB, table, floor_mib), "23 GiB: the cap is just reached with the lock");
54        check(ple_lock_blocks_cache(22ull * GiB, table, floor_mib), "22 GiB: the lock costs the cache some of its 8 GiB");
55        check(!ple_lock_blocks_cache(1ull * GiB, 1ull << 20, floor_mib), "a tiny table cannot make a cache");
56        check(!ple_lock_blocks_cache(std::numeric_limits<uint64_t>::max(), table, floor_mib), "no overflow");
57    }
58    // #4846 (owner 11-01): RAM still short at the arena after the wait and the give-back: the --mmap-experts source, never a refusal,
59    // never registering anyway.  A hand-set --ple-io ram or a shared arena file is respected (warn only).
60    {
61        const uint64_t need = arena + (1ull << 20);   // the arena and one blob
62        const uint64_t none = std::numeric_limits<uint64_t>::max();   // no cgroup limit
63        check(arena_ram_verdict(need, need, none, true) == ArenaRamVerdict::Load, "RAM exactly there: load");
64        check(arena_ram_verdict(need, 240ull * GiB, none, true) == ArenaRamVerdict::Load, "plenty: load");
65        check(arena_ram_verdict(need, need - 1, none, true) == ArenaRamVerdict::FallBack, "one byte short: file source");
66        check(arena_ram_verdict(need, 20ull * GiB, none, true) == ArenaRamVerdict::FallBack, "20 GiB of 51: file source");
67        check(arena_ram_verdict(need, 20ull * GiB, none, false) == ArenaRamVerdict::Warn, "20 GiB of 51, fixed by hand: warn only");
68        check(arena_ram_verdict(need, 240ull * GiB, 30ull * GiB, true) == ArenaRamVerdict::FallBack, "a 30 GiB container limit: file source, not a refusal");
69        check(arena_ram_verdict(need, 240ull * GiB, 30ull * GiB, false) == ArenaRamVerdict::Refuse, "a 30 GiB container limit, fixed by hand: as before");
70        check(arena_ram_verdict(need, 240ull * GiB, need, true) == ArenaRamVerdict::Load, "a limit of exactly the arena: load");
71        check(arena_ram_verdict(0, 0, 0, true) == ArenaRamVerdict::Load, "an unpriced arena loads");
72        check(arena_ram_fallback_allowed(nullptr, false, false), "fallback on by default");
73        check(!arena_ram_fallback_allowed(nullptr, true, false), "a hand-set --ple-io ram is kept");
74        check(!arena_ram_fallback_allowed(nullptr, false, true), "a shared arena file is kept");
75        check(!arena_ram_fallback_allowed("0", false, false), "STRATA_ARENA_RAM_FALLBACK=0 is the old behaviour");
76        check(arena_ram_fallback_allowed("1", false, false), "STRATA_ARENA_RAM_FALLBACK=1");
77        check(arena_ram_fallback_allowed("", false, false), "an empty value is unset");
78        check(!arena_ram_fallback_allowed("1", true, false), "the user's --ple-io ram wins over the env value");
79    }
80    // #5327: a shortfall the kernel can serve by dropping the box's own file cache loads through it (0.1.55's
81    // warn-and-proceed), one it cannot (anonymous RAM: #4051's host) keeps the #4846 fallback
82    {
83        const uint64_t need = 31ull * GiB + 687000000ull;   // the reporter's Q2_0 arena: 31.64 GiB
84        check(arena_shortfall_served_by_file_cache(need, 1ull * GiB, 38ull * GiB),
85              "12.9 GiB available, the model's files hot in the cache: load through the file cache");
86        check(!arena_shortfall_served_by_file_cache(need, 300ull << 20, 6ull * GiB),
87              "#4051's host: an exiting engine's anonymous RAM is not the cache: keep the fallback");
88        check(arena_shortfall_served_by_file_cache(need, need, 0), "free pages alone cover it");
89        check(!arena_shortfall_served_by_file_cache(need, 0, need - 1), "one byte short even with the cache");
90        check(!arena_shortfall_served_by_file_cache(0, 0, 0), "an unpriced arena is not this decision");
91        check(arena_shortfall_served_by_file_cache(std::numeric_limits<uint64_t>::max(), 5, std::numeric_limits<uint64_t>::max()),
92              "no overflow");
93        check(arena_shortfall_served_by_file_cache(need, 1ull * GiB, 6ull * GiB, 32ull * GiB),
94              "a reserved hugetlb pool that holds the whole arena: load");
95        check(!arena_shortfall_served_by_file_cache(need, 1ull * GiB, 6ull * GiB, need - 1),
96              "a pool one page short serves none of it (MAP_HUGETLB is all-or-nothing): keep the fallback");
97        uint64_t free_b = 1, drop_b = 1, huge_b = 1;
98        check(meminfo_free_and_droppable(
99                  "MemTotal:       1000 kB\nMemFree:          10 kB\nMemAvailable:    100 kB\nBuffers:           5 kB\n"
100                  "Cached:         400 kB\nSwapCached:       0 kB\nActive(file):   200 kB\nDirty:            2 kB\n"
101                  "Shmem:          30 kB\nSReclaimable:    15 kB\nHugePages_Total: 16384\nHugePages_Free:  16384\n"
102                  "Hugepagesize:    2048 kB\n",
103                  free_b, drop_b, &huge_b) && free_b == 10 * 1024 && drop_b == (400 + 5 + 15 - 30 - 2) * 1024 &&
104                  huge_b == 16384ull * 2048 * 1024,
105              "free, droppable and huge pages parsed (Shmem and Dirty are not droppable)");
106        check(meminfo_free_and_droppable("MemFree: 1 kB\nCached: 2 kB\n", free_b, drop_b) && free_b == 1024 && drop_b == 2 * 1024,
107              "Buffers/SReclaimable/Shmem/Dirty are optional");
108        check(!meminfo_free_and_droppable("MemFree: 1 kB\n", free_b, drop_b), "no Cached line: unknown, no credit");
109        check(!meminfo_free_and_droppable("Cached: 1 kB\n", free_b, drop_b), "no MemFree line: unknown, no credit");
110        // the loopholes a review of #5531 found: locked file pages (the PLE table's mlock) sit in Cached but the kernel cannot
111        // drop them (MemAvailable leaves them out); reserved huge pages are counted free but spoken for; only a 2 MiB pool
112        // is the arena's pool
113        check(meminfo_free_and_droppable("MemFree: 10 kB\nCached: 4000 kB\nBuffers: 5 kB\nMlocked: 3000 kB\nDirty: 2 kB\nShmem: 30 kB\n",
114                                         free_b, drop_b) &&
115                  drop_b == (4000 + 5 - 30 - 2 - 3000) * 1024,
116              "Mlocked pages are not droppable");
117        check(meminfo_free_and_droppable("MemFree: 1 kB\nCached: 100 kB\nMlocked: 5000 kB\n", free_b, drop_b) && drop_b == 0,
118              "Mlocked above the cache floors at 0");
119        check(meminfo_free_and_droppable("MemFree: 1 kB\nCached: 1 kB\nHugePages_Free: 16384\nHugePages_Rsvd: 16000\nHugepagesize: 2048 kB\n",
120                                         free_b, drop_b, &huge_b) &&
121                  huge_b == 384ull * 2048 * 1024,
122              "reserved huge pages are not free for the arena");
123        check(meminfo_free_and_droppable("MemFree: 1 kB\nCached: 1 kB\nHugePages_Free: 16\nHugePages_Rsvd: 20\nHugepagesize: 2048 kB\n",
124                                         free_b, drop_b, &huge_b) &&
125                  huge_b == 0,
126              "more reserved than free: none");
127        check(meminfo_free_and_droppable("MemFree: 1 kB\nCached: 1 kB\nHugePages_Free: 64\nHugepagesize: 1048576 kB\n",
128                                         free_b, drop_b, &huge_b) &&
129                  huge_b == 0,
130              "a 1 GiB default pool is not the 2 MiB pool the arena maps");
131        // #4051's other shape: an engine still exiting holds the PLE table mlocked.  Cached has it, MemAvailable does not.
132        {
133            const uint64_t need_x = 47ull * GiB;
134            check(meminfo_free_and_droppable("MemFree: " + std::to_string(2 * GiB / 1024) + " kB\nCached: " + std::to_string(55 * GiB / 1024) +
135                                             " kB\nMlocked: " + std::to_string(27 * GiB / 1024) + " kB\n",
136                                             free_b, drop_b) &&
137                      !arena_shortfall_served_by_file_cache(need_x, free_b, drop_b),
138                  "a locked PLE table in the cache does not serve the arena: keep the fallback");
139        }
140    }
141    // --ram-keep-gib W (R5070): ONE setting for both RAM decisions at the start.  The arena is used only if W GiB stay available after it;
142    // otherwise the RAM copy leaves max(W, the safety headroom: 8 GiB Windows, 4 GiB elsewhere), unless STRATA_RESIDENT_HEADROOM_GIB is set.
143    {
144        const uint64_t need = 46ull * GiB + 860ull * (1ull << 20) + 2660000ull;   // IQ3_S: 46.84 GiB of experts and the spare blob
145        const uint64_t none = std::numeric_limits<uint64_t>::max();
146        const uint64_t win = fallback_safety_headroom(true), lin = fallback_safety_headroom(false);
147        check(win == 8 * GiB && lin == 4 * GiB, "safety headroom: 8 GiB Windows, 4 GiB elsewhere");
148        check(ram_keep_from_gib(0.0) == 0 && ram_keep_from_gib(-3.0) == 0 && ram_keep_from_gib(8.0) == 8 * GiB, "W in GiB");
149        check(ram_keep_from_gib(1e9) == kRamKeepMax, "a typo cannot ask for more than 1 TiB");
150        check(arena_need_with_keep(need, 0) == need && arena_need_with_keep(need, 8 * GiB) == need + 8 * GiB, "need plus W");
151        check(arena_need_with_keep(none - 5, 100) == none, "need plus W cannot overflow");
152        // W = 0 is the behaviour before the setting: the arena exactly when arena_ram_verdict says Load, the copy leaves the safety headroom
153        const double frees[] = {10.0, 24.0, 30.0, 36.0, 40.0, 44.6, 46.5, 48.0, 48.9, 49.1, 50.0, 54.9, 60.0, 100.0};
154        for (const double f : frees) {
155            const uint64_t avail = (uint64_t) (f * (double) GiB);
156            for (const uint64_t safety : {win, lin}) {
157                const RamKeepPlan p0 = ram_keep_plan(need, avail, 0, std::nullopt, safety);
158                check(p0.arena == (arena_ram_verdict(need, avail, none, true) == ArenaRamVerdict::Load), "W=0: the arena whenever it fits, as before");
159                check(p0.headroom == safety, "W=0: the safety headroom");
160                check(p0.budget == (p0.arena ? 0 : (avail > safety + GiB ? avail - safety : 0)), "W=0: the budget is what is available less the safety headroom");
161                uint64_t prev_budget = none;
162                bool prev_arena = true;
163                for (const uint64_t w : {0ull, 5ull * GiB, 7ull * GiB, 8ull * GiB, 16ull * GiB}) {
164                    const RamKeepPlan p = ram_keep_plan(need, avail, w, std::nullopt, safety);
165                    check(p.arena == (avail >= need + w), "the arena only if W GiB stay available after it");
166                    if (p.arena) check(avail - need >= w, "an arena start leaves at least W free");
167                    check(p.headroom == (w > safety ? w : safety), "the headroom is max(W, safety)");
168                    if (!p.arena && p.budget > 0) check(avail - p.budget >= p.headroom, "a RAM copy leaves at least the headroom free");
169                    check(!p.arena || prev_arena, "a larger W never turns the fallback into the arena");
170                    check(p.budget <= prev_budget || prev_arena, "a larger W never grows the copy");
171                    prev_arena = p.arena;
172                    prev_budget = p.arena ? none : p.budget;
173                }
174                // the environment variable wins over both W and the safety headroom, even when smaller
175                const RamKeepPlan e = ram_keep_plan(need, avail, 8 * GiB, std::optional<uint64_t>(2 * GiB), safety);
176                check(e.headroom == 2 * GiB, "STRATA_RESIDENT_HEADROOM_GIB still wins");
177                check(e.arena == (avail >= need + 8 * GiB), "the environment does not move the arena decision");
178            }
179        }
180        // the owner's PC (63.1 GB, 12 GB card, IQ3_S): 48.9 GiB free at the start
181        const uint64_t f489 = (uint64_t) (48.9 * (double) GiB), f446 = (uint64_t) (44.6 * (double) GiB);
182        check(ram_keep_plan(need, f489, 0, std::nullopt, win).arena, "48.9 GiB free, W=0: the arena");
183        check(!ram_keep_plan(need, f489, 8 * GiB, std::nullopt, win).arena, "48.9 GiB free, W=8: not the arena (2 GiB left)");
184        const RamKeepPlan w8 = ram_keep_plan(need, f489, 8 * GiB, std::nullopt, win);
185        check(w8.headroom == 8 * GiB && w8.budget > 39 * GiB && w8.budget < 41 * GiB, "48.9 GiB free, W=8: a 40.9 GiB RAM copy, 8 GiB left");
186        const RamKeepPlan w5 = ram_keep_plan(need, f489, 5 * GiB, std::nullopt, win);
187        check(!w5.arena && w5.headroom == 8 * GiB, "48.9 GiB free, W=5 on Windows: 48.9 < 46.8 + 5, so the copy, and the safety headroom of 8 GiB wins over 5");
188        check(ram_keep_plan(need, f489, 5 * GiB, std::nullopt, lin).headroom == 5 * GiB, "W=5 on Linux: the copy leaves 5 GiB");
189        check(!ram_keep_plan(need, f446, 0, std::nullopt, win).arena && ram_keep_plan(need, f446, 0, std::nullopt, win).budget > 36 * GiB,
190              "44.6 GiB free, W=0: the fallback with a 36.6 GiB copy");
191    }
192    // A HARD memory limit just above the arena (R5070, found on l620: a 49 GiB cgroup limit, the 46.84 GiB arena chosen, the engine killed by the
193    // cgroup OOM killer at the start).  With the fallback allowed the arena needs its headroom under the limit too.
194    {
195        const uint64_t need = 46ull * GiB + 860ull * (1ull << 20) + 2660000ull;   // IQ3_S: 46.84 GiB of experts and the spare blob
196        const uint64_t head4 = 4 * GiB, head8 = 8 * GiB;
197        auto verdict = [&](uint64_t limit, uint64_t keep, uint64_t headroom, bool fallback) {
198            return arena_ram_verdict(arena_hard_limit_need(need, keep, headroom, fallback), ~uint64_t{0}, limit, fallback);
199        };
200        check(arena_hard_limit_need(need, 0, head4, true) == need + head4, "fallback allowed: the arena needs its headroom under the limit");
201        check(arena_hard_limit_need(need, 8 * GiB, head4, true) == need + 8 * GiB, "the RAM to keep counts when it is larger than the headroom");
202        check(arena_hard_limit_need(need, 0, head4, false) == need, "fallback not allowed: the comparison stays as it was");
203        check(arena_hard_limit_need(need, 8 * GiB, head4, false) == need + 8 * GiB, "fallback not allowed: the RAM to keep is still asked");
204        check(verdict(49ull * GiB, 0, head4, true) == ArenaRamVerdict::FallBack, "49 GiB limit, 4 GiB headroom: the copy, not an arena that the OOM killer ends");
205        check(verdict(49ull * GiB, 0, head8, true) == ArenaRamVerdict::FallBack, "49 GiB limit, 8 GiB headroom (Windows): the copy");
206        check(verdict(51ull * GiB, 0, head4, true) == ArenaRamVerdict::Load, "51 GiB limit, 4 GiB headroom: the arena");
207        check(verdict(need + head4, 0, head4, true) == ArenaRamVerdict::Load, "a limit of exactly arena + headroom: the arena");
208        check(verdict(need + head4 - 1, 0, head4, true) == ArenaRamVerdict::FallBack, "one byte under: the copy");
209        check(verdict(need - 1, 0, head4, true) == ArenaRamVerdict::FallBack, "a limit under the arena: the copy, as before");
210        check(verdict(49ull * GiB, 0, head4, false) == ArenaRamVerdict::Load, "fallback not allowed, 49 GiB limit: as before (no new refusal)");
211        check(verdict(45ull * GiB, 0, head4, false) == ArenaRamVerdict::Refuse, "fallback not allowed, limit under the arena: refused as before");
212        check(verdict(60ull * GiB, 8 * GiB, head4, true) == ArenaRamVerdict::Load, "60 GiB limit, 8 GiB to keep: the arena");
213        check(verdict(54ull * GiB, 8 * GiB, head4, true) == ArenaRamVerdict::FallBack, "54 GiB limit, 8 GiB to keep: the copy");
214        check(verdict(std::numeric_limits<uint64_t>::max(), 0, head4, true) == ArenaRamVerdict::Load, "no limit: the arena");
215    }
216    std::printf("ple_residency_test: %d checks passed\n", checks);
217    return 0;
218}