okami.mom/strata

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

Niko1221tests: cast ignored CUDA / HIP return values to (void)8aec2625d

main
6.2 KiB121 linesraw
1// The elastic K/V when the expert cache cannot give it slots (kv_grow_rules.hpp): the pools of a QSA state made elastic
2// at init map only their first cells; the K/V then grows from new VRAM (the take that returns 0, as generate.cpp's
3// kvg_ensure does when the cache has nothing to give) to the cells a long prompt needs and on to the whole context, and
4// every cell it maps is writable and reads back.  GPU with virtual memory needed; skips with 77 otherwise.
5#include "strata/core/geometry.hpp"
6#include "strata/core/kv_grow_rules.hpp"
7#include "strata/core/layer.hpp"
8#include "strata/core/layout.hpp"
9#include "strata/core/vmm.hpp"
10
11#include <cuda_runtime.h>
12
13#include <cstdint>
14#include <cstdio>
15#include <string>
16#include <vector>
17
18using namespace strata::core;
19
20namespace {
21int failed = 0;
22#define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #x); ++failed; } } while (0)
23
24// cells [0, cells) of one [page][kv_head][page_size][w] pool are its first pages, contiguous
25size_t pool_bytes(int64_t cells, int64_t page_size, int64_t heads, int64_t w) {
26    return (size_t) ((cells + page_size - 1) / page_size) * (size_t) (heads * page_size * w);
27}
28
29bool fill_and_check(const QsaState& st, const strata::kernels::QsaShapes& s, int64_t cells, uint8_t v) {
30    const int64_t scb = (s.head_dim / 64) * 2;
31    const std::pair<void*, int64_t> arrays[] = {{st.k_q, s.head_dim}, {st.v_q, s.head_dim}, {st.k_scale, scb}, {st.v_scale, scb}};
32    for (const auto& [p, w] : arrays) {
33        const size_t n = pool_bytes(cells, s.page_size, s.n_head_kv, w);
34        if (cudaMemset(p, v, n) != cudaSuccess) return false;
35        if (cudaDeviceSynchronize() != cudaSuccess) return false;
36        std::vector<uint8_t> h(4096);   // the last bytes: the far end of the mapped cells
37        if (cudaMemcpy(h.data(), (const uint8_t*) p + (n - h.size()), h.size(), cudaMemcpyDeviceToHost) != cudaSuccess) return false;
38        for (const uint8_t b : h)
39            if (b != v) return false;
40    }
41    return true;
42}
43}  // namespace
44
45int main(int argc, char** argv) {
46    if (cudaFree(nullptr) != cudaSuccess || !vmm_available()) {
47        std::printf("kv_grow_refused_test: no CUDA device with virtual memory - skipped\n");
48        return 77;
49    }
50    const ModelGeometry g;   // the model's QSA shapes
51    const strata::kernels::QsaShapes s = qsa_shapes(g);
52    const int64_t max_cells = 65536, init = 4096;
53    qsa_set_kv_int8(true);
54    qsa_set_kv_elastic(true, init);
55    CHECK(qsa_kv_elastic());
56    void* base = nullptr;
57    CHECK(cudaMalloc(&base, qsa_state_bytes(g, max_cells)) == cudaSuccess);
58    QsaState st;
59    CHECK(qsa_state_init(g, max_cells, base, st) > 0);
60    CHECK(st.kv_elastic >= 0 && st.k_q != nullptr && st.v_scale != nullptr);
61    CHECK(cudaDeviceSynchronize() == cudaSuccess);
62    const int64_t cells0 = qsa_kv_elastic_cells();
63    std::printf("kv_grow_refused_test: first mapping %lld of %lld cells (%.1f of %.1f MiB)\n", (long long) cells0,
64                (long long) max_cells, (double) qsa_kv_elastic_mapped_bytes() / 1048576.0,
65                (double) qsa_kv_elastic_full_bytes() / 1048576.0);
66    CHECK(cells0 >= init && cells0 < max_cells);
67    CHECK(fill_and_check(st, s, cells0, 0x11));
68
69    // the refused state of BLOCK 6: --expert-cache 100 (128 slots, the floor), pools elastic
70    const KvGrowStart r = kv_grow_start(qsa_kv_elastic(), true, true, true, 128, 128);
71    CHECK(r.on && !r.from_cache);
72    // a 20,000-token prompt (kvg_ensure(n + 256)): its cells are not mapped yet, then grown from new VRAM
73    const int64_t want = 20000 + 256, step = 8192;
74    const int64_t target = kv_grow_target(want, step, max_cells);
75    CHECK(target >= want);
76    CHECK(qsa_kv_elastic_need(target) > 0);
77    int64_t fresh = 0;
78    CHECK(qsa_kv_elastic_grow(target, [&] { ++fresh; return (VmmChunk) 0; }));
79    CHECK(fresh > 0 && qsa_kv_elastic_need(target) == 0);
80    const int64_t cells1 = qsa_kv_elastic_cells();
81    CHECK(cells1 >= target);
82    CHECK(fill_and_check(st, s, cells1, 0x22));
83    // --full-card (not in ctest: it takes every free byte of the card for a moment): with no new VRAM either, the grow
84    // reports false (generate.cpp's callers then end the request with their "no VRAM is left" line), the cells mapped
85    // so far stay mapped and usable, the device has no sticky error, and the grow succeeds once memory is free again
86    bool full_card = false;
87    for (int i = 1; i < argc; ++i) full_card = full_card || std::string(argv[i]) == "--full-card";
88    if (full_card) {
89        std::vector<void*> hog;
90        for (const size_t block : {(size_t) 256 << 20, (size_t) 2 << 20}) {
91            void* p = nullptr;
92            while (cudaMalloc(&p, block) == cudaSuccess) hog.push_back(p);
93            (void) cudaGetLastError();
94        }
95        size_t free_b = 0, total_b = 0;
96        (void) cudaMemGetInfo(&free_b, &total_b);
97        std::printf("kv_grow_refused_test: full card: %zu blocks taken, %.1f MiB free\n", hog.size(), (double) free_b / 1048576.0);
98        const bool grew = qsa_kv_elastic_grow(max_cells, [] { return (VmmChunk) 0; });
99        (void) cudaGetLastError();
100        std::printf("kv_grow_refused_test: full card: the grow to %lld cells returned %s; %lld cells mapped\n",
101                    (long long) max_cells, grew ? "true" : "false", (long long) qsa_kv_elastic_cells());
102        CHECK(!grew);
103        CHECK(qsa_kv_elastic_cells() >= cells1);
104        CHECK(cudaDeviceSynchronize() == cudaSuccess);
105        CHECK(fill_and_check(st, s, cells1, 0x44));
106        for (void* p : hog) (void)cudaFree(p);
107    }
108    // the whole context: every cell of --max-context can be mapped the same way
109    CHECK(qsa_kv_elastic_grow(max_cells, [] { return (VmmChunk) 0; }));
110    CHECK(qsa_kv_elastic_cells() >= max_cells && qsa_kv_elastic_need(max_cells) == 0);
111    CHECK(fill_and_check(st, s, max_cells, 0x33));
112    std::printf("kv_grow_refused_test: grown from new VRAM to %lld then %lld cells (%lld new chunks for the first grow)\n",
113                (long long) cells1, (long long) qsa_kv_elastic_cells(), (long long) fresh);
114    CHECK(cudaGetLastError() == cudaSuccess);
115    if (failed) {
116        std::fprintf(stderr, "kv_grow_refused_test: %d failed\n", failed);
117        return 1;
118    }
119    std::printf("kv_grow_refused_test: ok\n");
120    return 0;
121}