okami.mom/strata

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

Niko1221Merge f-strix-c4 00807c9b3 into f-p4-int (with two or more requests at once on Windows integrated AMD, the pinned RAM copy of experts is capped so every slot fits)47d46ccce

main
4.8 KiB66 linesraw
1// src/core/uma_cache_test.cpp - the RAM reserve of an integrated GPU's auto expert cache (no GPU needed).
2#include "strata/core/uma_cache.hpp"
3
4#include <cstdio>
5
6static int fails = 0;
7#define CHECK(c) do { if (!(c)) { std::printf("FAIL line %d: %s\n", __LINE__, #c); ++fails; } } while (0)
8
9int main() {
10    using namespace strata::core;
11    constexpr uint64_t GiB = 1ull << 30;
12    // the larger of 8 GiB and 8% of the RAM
13    CHECK(uma_desktop_reserve_bytes(32 * GiB, nullptr) == 8 * GiB);
14    CHECK(uma_desktop_reserve_bytes(64 * GiB, nullptr) == 8 * GiB);          // 8% = 5.1 GiB
15    CHECK(uma_desktop_reserve_bytes(128 * GiB, nullptr) > 10 * GiB);         // 8% = 10.2 GiB
16    CHECK(uma_desktop_reserve_bytes(128 * GiB, nullptr) < 11 * GiB);
17    CHECK(uma_desktop_reserve_bytes(0, nullptr) == 8 * GiB);                 // RAM unknown: the floor
18    // the person's own figure, 0 included
19    CHECK(uma_desktop_reserve_bytes(128 * GiB, "0") == 0);
20    CHECK(uma_desktop_reserve_bytes(128 * GiB, "4") == 4 * GiB);
21    CHECK(uma_desktop_reserve_bytes(128 * GiB, "2.5") == 2 * GiB + GiB / 2);
22    CHECK(uma_desktop_reserve_bytes(128 * GiB, "") > 10 * GiB);              // empty: not set
23    CHECK(uma_desktop_reserve_bytes(64 * GiB, "x") == 8 * GiB);              // not a number: ignored
24    CHECK(uma_desktop_reserve_bytes(64 * GiB, "-3") == 8 * GiB);
25    // the room
26    CHECK(uma_cache_room_bytes(100 * GiB, 10 * GiB) == 90 * GiB);
27    CHECK(uma_cache_room_bytes(10 * GiB, 10 * GiB) == 0);
28    CHECK(uma_cache_room_bytes(5 * GiB, 10 * GiB) == 0);
29    CHECK(uma_cache_room_bytes(5 * GiB, 0) == 5 * GiB);
30    // G16: the Windows HIP floor of the auto cache's reserve is for a discrete card of 15 GiB or more only
31    CHECK(wddm_auto_reserve_mib(false, 20 * GiB, 700) == 2560);              // a 20 GB discrete card (#1376): the floor
32    CHECK(wddm_auto_reserve_mib(false, 15 * GiB, 700) == 2560);              // 15 GiB: the floor (#1709)
33    CHECK(wddm_auto_reserve_mib(false, 16 * GiB - 1, 700) == 2560);          // a 16 GB card reports just under 16
34    CHECK(wddm_auto_reserve_mib(false, 8 * GiB, 700) == 700);                // a smaller card keeps its own sizing
35    CHECK(wddm_auto_reserve_mib(false, 15 * GiB - 1, 700) == 700);
36    CHECK(wddm_auto_reserve_mib(true, 94027ull << 20, 700) == 700);          // a Strix Halo (94,027 MiB pool): no floor
37    CHECK(wddm_auto_reserve_mib(true, 20 * GiB, 700) == 700);                // integrated wins whatever the size
38    CHECK(wddm_auto_reserve_mib(false, 20 * GiB, 3000) == 3000);             // a larger reserve is never lowered
39    CHECK(wddm_auto_reserve_mib(true, 94027ull << 20, 3000) == 3000);
40    // the pinned complement cap with batch slots (UD-Q4_K_XL, 131,072 tokens: 1.55 GiB of pinned K/V a session, the main one and each slot)
41    {
42        const uint64_t kv = GiB * 155 / 100;
43        const uint64_t budget38 = 38 * GiB;
44        auto capped = [&](int sessions) { const uint64_t c = uma_pinned_complement_cap_bytes(kv * (uint64_t) sessions); return c < budget38 ? c : budget38; };
45        // the starts that worked keep their complement: 1 session (solo) 4.62 GiB, 3 (2 slots) 12.47, 4 (3 slots) 16.40
46        CHECK(capped(1) > 4 * GiB + GiB * 62 / 100);
47        CHECK(capped(3) >= 12 * GiB + GiB * 47 / 100);
48        CHECK(capped(4) >= 16 * GiB + GiB * 40 / 100);
49        // 4 slots (5 sessions): 22.75 - 7.75 = 15.0 GiB, the largest budget that started (job strix-20261112-1937-7dbd)
50        CHECK(capped(5) >= 14 * GiB + GiB * 99 / 100 && capped(5) <= 15 * GiB + GiB / 100);
51        CHECK(capped(5) < 18 * GiB);                                          // 18 failed
52        // many slots: the K/V alone is over the level (15 sessions x 1.55 GiB = 23.25 > 22.75): the cap is 0 = no RAM copy, and the budget handed to the pin is 1 byte
53        // (0 would mean "no limit"): a start with every expert outside the GPU cache read from the SSD, never an error, never a wrapped (negative) size
54        CHECK(uma_pinned_complement_cap_bytes(30 * GiB) == 0);
55        CHECK(uma_pinned_complement_cap_bytes(kv * 15) == 0);
56        CHECK(uma_pinned_complement_budget_bytes(30 * GiB) == 1);
57        CHECK(uma_pinned_complement_budget_bytes(kv * 15) == 1);
58        CHECK(uma_pinned_complement_budget_bytes(~0ull) == 1);                // the largest input
59        CHECK(uma_pinned_complement_cap_bytes(0) == GiB * 2275 / 100);        // no session at all: the whole level
60        CHECK(uma_pinned_complement_cap_bytes(kv * 9) > 8 * GiB + GiB * 50 / 100);   // 8 slots + the main session (9 x 1.55 = 13.95): 8.8 GiB, still a copy
61        CHECK(uma_pinned_complement_cap_bytes(kv * 14) > 0);                  // 14 sessions: 1.05 GiB left
62        CHECK(uma_pinned_complement_budget_bytes(kv * 5) == uma_pinned_complement_cap_bytes(kv * 5));   // above zero the budget is the cap itself
63    }
64    if (fails == 0) std::printf("uma_cache_test: all checks passed\n");
65    return fails == 0 ? 0 : 1;
66}