okami.mom/strata

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

waveringanaconversation cache: an NVMe tier keeps what the RAM budget evicts and what is parked at exitf7257bce7

main
8.0 KiB167 linesraw
1// CPU-only tests for the NVMe tier of the conversation cache (include/strata/core/conversation_disk.hpp): what it
2// keeps, finds, reads back and drops, and that the RAM tier hands it what it evicts.
3// Built with -DSTRATA_BUILD_CONVERSATION_TESTS=ON; no CUDA, no model.
4#include "strata/core/conversation_disk.hpp"
5
6#include <chrono>
7#include <cstdio>
8#include <cstdlib>
9#include <filesystem>
10#include <fstream>
11
12using namespace strata::core;
13namespace fs = std::filesystem;
14
15namespace {
16
17int checks = 0;
18void check(bool ok, const char* label) {
19    ++checks;
20    if (!ok) { std::fprintf(stderr, "FAIL: %s\n", label); std::exit(1); }
21}
22
23std::vector<uint8_t> bytes_of(size_t n, uint8_t seed) {
24    std::vector<uint8_t> v(n);
25    for (size_t i = 0; i < n; ++i) v[i] = uint8_t(seed + i * 7);
26    return v;
27}
28
29ConversationBuffer pattern(size_t n, uint8_t seed) {
30    ConversationBuffer b;
31    b.resize(n);
32    b.visit(0, n, [&](uint8_t* p, size_t c, size_t at) {
33        for (size_t i = 0; i < c; ++i) p[i] = uint8_t(seed + (at + i) * 13);
34        return true;
35    });
36    return b;
37}
38
39// a checkpoint over tokens base, base+1, ... (count of them)
40ConversationCheckpoint checkpoint(int32_t base, size_t count, uint8_t seed) {
41    ConversationCheckpoint c;
42    for (size_t i = 0; i < count; ++i) c.ids.push_back(base + int32_t(i));
43    c.gdn = bytes_of(4099, seed);
44    c.ple = bytes_of(77, seed + 1);
45    c.tails = bytes_of(301, seed + 2);
46    c.dead = bytes_of(64, seed + 3);
47    c.block_pos = bytes_of(8, seed + 4);
48    c.used = 42 + seed;
49    return c;
50}
51
52// one conversation: tokens base.. of `live` length, checkpoints at each of `at`
53SavedConversation conversation(int32_t base, size_t live, std::vector<size_t> at, uint8_t seed) {
54    SavedConversation s;
55    for (size_t i = 0; i < s.geometry.size(); ++i) s.geometry[i] = int64_t(100 + i);
56    s.layer_lo = 0; s.layer_hi = 48;
57    s.live = checkpoint(base, live, seed);
58    for (size_t n : at) s.checkpoints.push_back(checkpoint(base, n, uint8_t(seed + n)));
59    ConversationKv kv;
60    kv.format = 2; kv.cells = 1024; kv.heads = 2; kv.head_dim = 256; kv.page_size = 64; kv.pooled_rows = 9; kv.idx_dim = 128;
61    kv.k = pattern(65536 + seed, seed);
62    kv.v = pattern(4096, uint8_t(seed + 10));
63    kv.k_scale = pattern(512, uint8_t(seed + 20));
64    kv.v_scale = pattern(512, uint8_t(seed + 30));
65    kv.pooled = pattern(9 * 128 * 4, uint8_t(seed + 40));
66    s.kv.push_back(std::move(kv));
67    return s;
68}
69
70std::vector<int32_t> prompt(int32_t base, size_t count) {
71    std::vector<int32_t> p;
72    for (size_t i = 0; i < count; ++i) p.push_back(base + int32_t(i));
73    return p;
74}
75
76size_t files_in(const fs::path& dir, const char* ext) {
77    size_t n = 0;
78    for (const auto& e : fs::directory_iterator(dir)) n += e.path().extension() == ext;
79    return n;
80}
81
82}   // namespace
83
84int main() {
85    const fs::path dir = fs::temp_directory_path() / ("strata-conv-disk-test-" + std::to_string(
86        std::chrono::steady_clock::now().time_since_epoch().count()));
87    const SessionFileIdentity id{0x1111222233334444ull, 0x5555666677778888ull};
88    std::string error;
89
90    // two conversations are kept, each found by the prompt that continues it, and read back whole
91    {
92        ConversationDisk disk(dir.string(), 1ull << 30);
93        check(disk.load() == 0, "an empty folder holds nothing");
94        const SavedConversation a = conversation(1000, 900, {100, 600}, 1);
95        const SavedConversation b = conversation(5000, 400, {100}, 2);
96        check(disk.put(a, id, error) && disk.put(b, id, error), "both are written");
97        check(disk.size() == 2 && files_in(dir, ".kvs") == 2 && files_in(dir, ".idx") == 2, "a file and an index each");
98        auto m = disk.best(prompt(1000, 950), {}, true);
99        check(m.tokens == 900 && m.live, "the prompt that continues a is matched to a's live state");
100        m = disk.best(prompt(1000, 650), {}, true);
101        check(m.tokens == 600 && !m.live, "a shorter prompt is matched to a's checkpoint");
102        check(disk.best(prompt(9000, 500), {}, true).tokens == 0, "an unrelated prompt matches nothing");
103        check(disk.best(prompt(1000, 950), {}, false).tokens == 0, "another control vector matches nothing");
104        SavedConversation back;
105        m = disk.best(prompt(5000, 450), {}, true);
106        check(disk.take(m.index, id, back, error), "b is read back");
107        check(back.live.ids == b.live.ids && back.live.gdn == b.live.gdn && back.checkpoints.size() == 1 &&
108              back.kv.size() == 1 && back.kv[0].k.size() == b.kv[0].k.size(), "b comes back as it was written");
109        check(disk.size() == 1 && files_in(dir, ".kvs") == 1 && files_in(dir, ".idx") == 1, "a taken entry leaves the disk");
110    }
111
112    // a restart finds what an earlier run wrote, without reading the states; stray files go
113    {
114        std::ofstream(dir / "conv-000000000099.kvs.tmp") << "half";
115        std::ofstream(dir / "conv-000000000098.kvs") << "no index";
116        ConversationDisk disk(dir.string(), 1ull << 30);
117        check(disk.load() == 1, "the remaining conversation is found again");
118        check(disk.best(prompt(1000, 950), {}, true).tokens == 900, "and matched as before");
119        check(!fs::exists(dir / "conv-000000000099.kvs.tmp") && !fs::exists(dir / "conv-000000000098.kvs"),
120              "a half-written file and a file without its index are removed");
121        SessionFileIdentity other = id;
122        other.model ^= 1;
123        SavedConversation back;
124        check(!disk.take(0, other, back, error) && !error.empty(), "another model's engine cannot read it");
125        check(disk.size() == 0 && files_in(dir, ".kvs") == 0, "and a file that does not read is not kept");
126    }
127
128    // the same conversation a turn later replaces its earlier copy; over budget the oldest goes
129    {
130        ConversationDisk disk(dir.string(), 1ull << 30);
131        check(disk.put(conversation(1000, 900, {100, 600}, 1), id, error), "turn 1 is written");
132        check(disk.put(conversation(1000, 1400, {100, 600, 1200}, 1), id, error), "turn 2 is written");
133        check(disk.size() == 1 && disk.best(prompt(1000, 1500), {}, true).tokens == 1400, "turn 2 supersedes turn 1");
134        disk.clear();
135        check(disk.put(conversation(7000, 900, {100}, 4), id, error), "one of the size below is written");
136        const uint64_t one = disk.bytes();
137        disk.clear();
138        ConversationDisk small(dir.string(), one * 2 + one / 2);
139        check(small.put(conversation(1000, 900, {100}, 1), id, error) &&
140              small.put(conversation(5000, 900, {100}, 2), id, error) &&
141              small.put(conversation(9000, 900, {100}, 3), id, error), "three are written");
142        check(small.size() == 2 && small.bytes() <= one * 2 + one / 2, "the budget keeps two");
143        check(small.best(prompt(1000, 950), {}, true).tokens == 0 && small.best(prompt(9000, 950), {}, true).tokens == 900,
144              "the least recently written left");
145        check(small.clear() == 2 && files_in(dir, ".kvs") == 0 && files_in(dir, ".idx") == 0, "clear removes every file");
146    }
147
148    // the RAM tier hands each conversation it evicts to the spill, and nothing else
149    {
150        ConversationCache ram(1ull << 30, 1);
151        std::vector<std::vector<int32_t>> spilled;
152        ram.set_spill([&](const SavedConversation& s) { spilled.push_back(s.live.ids); });
153        check(ram.put(conversation(1000, 900, {100}, 1)), "a is parked");
154        check(ram.put(conversation(5000, 900, {100}, 2)), "b is parked, evicting a");
155        check(spilled.size() == 1 && spilled[0] == prompt(1000, 900), "a was spilled");
156        check(ram.put(conversation(5000, 1300, {100, 900}, 2)) && spilled.size() == 1,
157              "b's next turn supersedes b without a spill");
158        ram.clear();
159        check(spilled.size() == 1, "clear spills nothing");
160        check(ram.put(conversation(1000, 900, {100}, 1)) && ram.spill_all() == 1 && spilled.size() == 2 && ram.size() == 0,
161              "spill_all hands over every parked conversation");
162    }
163
164    fs::remove_all(dir);
165    std::printf("conversation_disk_test: %d checks passed\n", checks);
166    return 0;
167}