okami.mom/strata

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

Niko1221Merge the expert cache fix for a part-way shrink: with --vram-elastic, a refused shrink no longer leaves experts listed in unmapped slots28e8d0e05

main
8.0 KiB150 linesraw
1// #533: the segmented expert cache (--vram-elastic).  A shrink gives the last segments' VRAM back to the driver and
2// keeps every slot left exactly as it was (same address, same bytes); a grow maps them again at the same addresses.
3// Uniform and sized slots.  Needs a CUDA device with virtual memory management (exits 77 without one).
4#include "strata/core/expert_cache.hpp"
5#include "strata/core/expert_cache_refuse.hpp"
6#include "strata/core/residency_trim.hpp"
7
8#include <cuda_runtime.h>
9
10#include <cstdio>
11#include <string>
12#include <utility>
13#include <vector>
14
15namespace {
16int fail(const char* what, const std::string& err = "") {
17    std::fprintf(stderr, "FAIL %s %s\n", what, err.c_str());
18    return 1;
19}
20
21size_t free_vram() {
22    size_t f = 0, t = 0;
23    cudaMemGetInfo(&f, &t);
24    return f;
25}
26
27// slot i holds the byte (i * 7 + 1) everywhere
28bool fill(strata::core::ExpertCache& c, int64_t from, int64_t to, int64_t blob) {
29    std::vector<uint8_t> b((size_t) blob);
30    for (int64_t i = from; i < to; ++i) {
31        std::fill(b.begin(), b.end(), (uint8_t) (i * 7 + 1));
32        if (cudaMemcpy(c.device_slot((int32_t) i), b.data(), (size_t) blob, cudaMemcpyHostToDevice) != cudaSuccess)
33            return false;
34    }
35    return true;
36}
37
38bool check(const strata::core::ExpertCache& c, int64_t to, int64_t blob) {
39    std::vector<uint8_t> b((size_t) blob);
40    for (int64_t i = 0; i < to; ++i) {
41        if (cudaMemcpy(b.data(), c.device_slot((int32_t) i), (size_t) blob, cudaMemcpyDeviceToHost) != cudaSuccess)
42            return false;
43        for (const uint8_t x : b)
44            if (x != (uint8_t) (i * 7 + 1)) return false;
45    }
46    return true;
47}
48}  // namespace
49
50int main() {
51    int n = 0;
52    if (cudaGetDeviceCount(&n) != cudaSuccess || n == 0) {
53        std::puts("no CUDA device: skipped");
54        return 77;
55    }
56    constexpr int64_t kBlob = 1 << 20, kSlots = 96, kSeg = 8ll << 20;
57    for (int sized = 0; sized < 2; ++sized) {
58        strata::core::ExpertCache c;
59        c.set_segment_bytes(kSeg);
60        std::string err;
61        const bool ok = sized ? c.open_sized(std::vector<int64_t>(kSlots, kBlob), 4, 64, err)
62                              : c.open(kSlots, 4, 64, kBlob, err);
63        if (!ok) {
64            // #1475: a HIP build has no segmented cache ("--vram-elastic ... is CUDA-only for now"): a skip, not a failure
65            if (err.find("virtual memory management") != std::string::npos ||
66                err.find("CUDA-only") != std::string::npos) {
67                std::printf("%s: skipped\n", err.c_str());
68                return 77;
69            }
70            return fail("open", err);
71        }
72        if (!c.segmented() || c.slots() != kSlots || c.mapped_bytes() < kSlots * kBlob) return fail("open sizes");
73        uint8_t* const first = c.device_slot(0);
74        if (!fill(c, 0, kSlots, kBlob)) return fail("fill");
75        const size_t before = free_vram();
76        // keep 30 MiB: rounded up to whole 8 MiB segments = 32 MiB = 32 slots
77        if (!c.shrink(30ll << 20, err)) return fail("shrink", err);
78        if (c.slots() != 32 || c.mapped_bytes() != 32ll << 20 || c.bytes() != 32ll << 20)
79            return fail("shrunk sizes");
80        if (c.full_slots() != kSlots || c.full_bytes() != kSlots * kBlob) return fail("full sizes");
81        const size_t freed = free_vram() - before;
82        if (freed + (8u << 20) < (size_t) (64ll << 20)) {   // ~64 MiB back to the driver (WDDM rounds a little)
83            std::fprintf(stderr, "freed %zu bytes\n", freed);
84            return fail("VRAM not given back");
85        }
86        if (c.device_slot(0) != first || !check(c, 32, kBlob)) return fail("slots kept after the shrink");
87        // grow to 70 MiB: whole segments only, 64 MiB = 64 slots
88        if (!c.grow(70ll << 20, err) || c.slots() != 64) return fail("grow part", err);
89        if (!c.grow(1ll << 40, err) || c.slots() != kSlots || c.mapped_bytes() < kSlots * kBlob)
90            return fail("grow all", err);
91        if (c.device_slot(0) != first || !check(c, 32, kBlob)) return fail("slots kept after the grow");
92        if (!fill(c, 32, kSlots, kBlob) || !check(c, kSlots, kBlob)) return fail("the grown slots");
93        if (c.slots_within(0) != 0 || c.slots_within(kBlob * 3 + 5) != 3 || c.bytes_of(5) != 5 * kBlob)
94            return fail("slots_within / bytes_of");
95        c.close();
96        if (c.valid() || c.slots() != 0) return fail("close");
97    }
98    // a shrink the driver refuses part way (strata/core/expert_cache_refuse.hpp): what was given back before the refusal
99    // is gone, the rest keeps its bytes, the residency entries past it are taken (strata/core/residency_trim.hpp), and a
100    // grow maps everything again.  12 segments of 8 MiB; a shrink to 30 MiB walks segments 11 down to 4.
101    {
102        strata::core::ExpertCache c;
103        c.set_segment_bytes(kSeg);
104        std::string err;
105        if (!c.open(kSlots, 4, 64, kBlob, err) || !fill(c, 0, kSlots, kBlob)) return fail("refusal: open", err);
106        std::vector<int32_t> res((size_t) kSlots);
107        for (int64_t i = 0; i < kSlots; ++i) res[(size_t) i] = (int32_t) i;   // expert i in slot i
108        // (a) the release of segment 9 refused after its unmap: segments 11 and 10 are gone, 9 is unmapped, 72 slots stay
109        strata::core::ec_test::refuse_segment_call = [](const char* call, int64_t seg) {
110            return seg == 9 && std::string(call) == "release";
111        };
112        const bool ok_a = c.shrink(30ll << 20, err);
113        strata::core::ec_test::refuse_segment_call = nullptr;
114        if (ok_a || err.empty()) return fail("refusal (a): the shrink reported no error");
115        if (c.slots() != 72 || c.mapped_bytes() != 72ll << 20) {
116            std::fprintf(stderr, "slots %lld mapped %lld\n", (long long) c.slots(), (long long) c.mapped_bytes());
117            return fail("refusal (a): an unmapped segment is still counted");
118        }
119        if (!check(c, 72, kBlob)) return fail("refusal (a): the slots kept");
120        std::vector<std::pair<int32_t, int32_t>> ev;
121        if (strata::core::evict_slots_past(res, c.slots(), ev) != 24 || ev.size() != 24 || ev.front() != std::make_pair(72, 72) ||
122            res[71] != 71 || res[72] != strata::core::kNotResident || res[95] != strata::core::kNotResident)
123            return fail("refusal (a): the residency entries past the mapped slots");
124        // (b) the unmap of segment 6 refused: 8 and 7 go, 6 stays mapped, 56 slots stay
125        strata::core::ec_test::refuse_segment_call = [](const char* call, int64_t seg) {
126            return seg == 6 && std::string(call) == "unmap";
127        };
128        const bool ok_b = c.shrink(30ll << 20, err);
129        strata::core::ec_test::refuse_segment_call = nullptr;
130        if (ok_b || c.slots() != 56 || c.mapped_bytes() != 56ll << 20 || !check(c, 56, kBlob))
131            return fail("refusal (b): a segment the driver kept mapped");
132        if (strata::core::evict_slots_past(res, c.slots(), ev) != 16 || ev.size() != 40 || res[55] != 55 ||
133            res[56] != strata::core::kNotResident)
134            return fail("refusal (b): the residency entries past the mapped slots");
135        // a grow maps all of it again, segment 9's kept handle included, and every slot holds what is written to it
136        if (!c.grow(1ll << 40, err) || c.slots() != kSlots || c.mapped_bytes() < kSlots * kBlob) return fail("refusal: grow", err);
137        if (!check(c, 56, kBlob) || !fill(c, 56, kSlots, kBlob) || !check(c, kSlots, kBlob)) return fail("refusal: the grown slots");
138        // a clean shrink after it, then close
139        if (!c.shrink(30ll << 20, err) || c.slots() != 32) return fail("refusal: a later shrink", err);
140        c.close();
141        if (c.valid()) return fail("refusal: close");
142    }
143    // the default (no segment size): one allocation, shrink refused
144    strata::core::ExpertCache d;
145    std::string err;
146    if (!d.open(8, 2, 4, kBlob, err) || d.segmented() || d.shrink(0, err)) return fail("unsegmented", err);
147    std::puts("expert cache segments: shrink gives VRAM back and keeps the slots, grow maps them again, a refused shrink "
148              "counts only what stays mapped");
149    return 0;
150}