okami.mom/strata

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

Niko1221Merge f-ci-green-0215: the four red GitHub checks (cmake-cpu on MSVC, py-tools on a CRLF checkout and on macOS, the Windows snapshot time)b749503db

main
6.8 KiB148 linesraw
1// ConversationBuffer on the segment pool (STRATA_PARK_SEGPOOL_MIB): a real-sized 2K and 64K K/V image alternating, a pool smaller than
2// the request, and the buffers' bytes with the pool on and off.  Host only; the OS side is a fake that counts calls.
3#include "strata/core/conversation_buffer.hpp"
4
5#include <cstdio>
6#include <cstdlib>
7#include <cstring>
8#include <memory>
9#include <vector>
10#ifdef _WIN32
11#include <malloc.h>
12#endif
13
14using namespace strata::core;
15
16namespace {
17// 2 MiB aligned memory from the C library: MSVC's C runtime has no std::aligned_alloc (its free cannot release it), so _aligned_malloc there
18void* aligned_2m(size_t bytes) {
19#ifdef _WIN32
20    return _aligned_malloc(bytes, size_t(2) << 20);
21#else
22    return std::aligned_alloc(size_t(2) << 20, bytes);
23#endif
24}
25void aligned_free(void* p) {
26#ifdef _WIN32
27    _aligned_free(p);
28#else
29    std::free(p);
30#endif
31}
32
33int checks = 0;
34void check(bool value, const char* description) {
35    ++checks;
36    if (!value) { std::fprintf(stderr, "FAIL: %s\n", description); std::exit(1); }
37}
38constexpr size_t MiB = size_t(1) << 20;
39
40struct FakeOs final : SegmentPoolOs {
41    void* reserve(size_t bytes) override { return aligned_2m(bytes); }
42    void unreserve(void* base, size_t) override { aligned_free(base); }
43    bool commit(void*, size_t) override { return true; }
44    bool populate(void*, size_t) override { return true; }
45    void release(void* p, size_t n) override { std::memset(p, 0, n); }
46};
47
48// One conversation's K/V host image at Qwen3.8-Flash-Next's int8 sizes (13 layers: 12 QSA + the draft layer; two K/V heads of 256):
49// k and v cells x 512 B, the two scale buffers cells x 32 B, the pooled indexer rows (one 128-float row per 128 tokens)
50struct Image {
51    std::vector<ConversationBuffer> buffers;   // 5 per layer
52    size_t bytes = 0;
53    explicit Image(size_t tokens, uint8_t tag, bool fill = true) {
54        const size_t cells = (tokens + 63) / 64 * 64;
55        const size_t sizes[5] = {cells * 512, cells * 512, cells * 32, cells * 32, (tokens / 128 + 1) * 128 * 4};
56        buffers.resize(13 * 5);
57        for (size_t layer = 0; layer < 13; ++layer)
58            for (size_t part = 0; part < 5; ++part) {
59                ConversationBuffer& b = buffers[layer * 5 + part];
60                b.resize_for_overwrite(sizes[part]);
61                bytes += sizes[part];
62                if (fill)
63                    b.visit(0, b.size(), [&](uint8_t* p, size_t n, size_t at) {
64                        std::memset(p, int(tag + layer * 5 + part), n);
65                        (void) at;
66                        return true;
67                    });
68            }
69    }
70    bool intact(uint8_t tag) const {
71        for (size_t i = 0; i < buffers.size(); ++i) {
72            bool ok = true;
73            buffers[i].visit(0, buffers[i].size(), [&](const uint8_t* p, size_t n, size_t) {
74                for (size_t k = 0; k < n; k += 997) ok = ok && p[k] == uint8_t(tag + i);
75                return ok;
76            });
77            if (!ok) return false;
78        }
79        return true;
80    }
81};
82}
83
84int main() {
85    FakeOs os;
86    // pool off: every segment is a plain allocation (nothing pooled), the sizes are what they were
87    {
88        check(segment_pool() == nullptr, "no pool is installed");
89        Image img(61225, 1);
90        check(img.intact(1) && img.bytes > 700 * MiB && img.bytes < 900 * MiB, "a 64K image is about 0.83 GB (13 layers)");
91    }
92    SegmentPool pool(os, 1024 * MiB);
93    check(pool.active() && segment_pool_install(&pool), "a 1 GiB pool is installed");
94    // a 2K and a 64K conversation alternating: the second 64K image is served entirely from the pool
95    {
96        auto a = std::make_unique<Image>(1931, 10);          // 2K
97        const uint64_t after_2k = pool.hits();
98        check(after_2k > 0 && pool.live_requested() > 0, "the 2K image's large buffers come from the pool");
99        check(pool.live_requested() < a->bytes, "its small buffers (scales, pooled rows) stay on the heap");
100        auto z = std::make_unique<Image>(61225, 20);         // new 64K
101        check(z->intact(20) && a->intact(10), "both images keep their bytes");
102        const size_t live_both = pool.live_requested();
103        const uint64_t misses_first = pool.misses();
104        z.reset();                                           // the 64K image goes (evicted): its ranges are free and warm
105        check(pool.live_requested() < live_both && pool.idle_bytes() > 0, "an evicted image's bytes go back to the pool");
106        auto w = std::make_unique<Image>(1931, 30);          // another 2K
107        check(w->intact(30), "a 2K image takes some of the freed ranges");
108        auto z2 = std::make_unique<Image>(61225, 40);        // the 64K conversation again
109        check(z2->intact(40) && a->intact(10) && w->intact(30), "the second 64K image and the 2K images are intact");
110        check(pool.misses() == misses_first, "the second 64K image took every large buffer from the pool: no new miss");
111        z2.reset(); w.reset(); a.reset();
112        check(pool.live_requested() == 0, "everything is given back");
113    }
114    // growth by appending (a continued conversation) and rewind
115    {
116        ConversationBuffer b;
117        b.resize(3 * MiB, 7);
118        const size_t live = pool.live_requested();
119        b.resize(3 * MiB + 5, 9);   // within the segment's spare capacity or a new small segment
120        std::vector<uint8_t> tail(5);
121        check(b.read(tail.data(), 3 * MiB, 5) && tail[0] == 9 && tail[4] == 9, "append keeps working on a pooled buffer");
122        b.resize(1 * MiB);
123        check(b.size() == 1 * MiB && pool.live_requested() >= live, "a shortened buffer keeps its segment");
124        ConversationBuffer c = b;   // a copy is its own storage
125        check(c == b, "a copy equals its source");
126        ConversationBuffer d = std::move(b);
127        check(d.size() == 1 * MiB && b.size() == 0, "a move leaves the source empty");
128    }
129    check(pool.live_requested() == 0, "no pooled byte is left after the buffers are gone");
130    segment_pool_uninstall_for_test();
131    // a pool smaller than the request: the pool takes what it can, the rest is the heap, and every byte is right
132    {
133        SegmentPool small(os, 256 * MiB);
134        segment_pool_install(&small);
135        {
136            Image big(61225, 50);
137            check(big.intact(50), "a 64K image over a 256 MiB pool keeps its bytes");
138            check(small.hits() > 0 && small.misses() > 0, "the pool served part of it and missed the rest");
139            check(small.live_requested() <= 256 * MiB, "the pool never holds more than its size");
140            Image other(8000, 60);
141            check(other.intact(60) && big.intact(50), "an 8K image beside it, both intact");
142        }
143        check(small.live_requested() == 0, "all pooled bytes given back");
144        segment_pool_uninstall_for_test();
145    }
146    std::printf("conversation_buffer_pool_test: %d checks passed\n", checks);
147    return 0;
148}