okami.mom/strata

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

waveringanadsv41: conversation slots and SCR keep conversations with pictures - spans matched by picture hash (picspans::keyed), a slot's pictures in a .pics file, SCR moves or reads a picture's span whole24d897c06

main
10.6 KiB209 linesraw
1// The kept conversations' bookkeeping without a GPU (src/dsv41/ds4_slotmeta.hpp): the .meta file's round trip and
2// every way it is refused, and which kept conversation a prompt continues, and from where.
3//   ds4_slot_test            exit 0: all passed
4#include "ds4_slotmeta.hpp"
5#include "ds4_picspans.hpp"
6
7#include <cstdio>
8#include <cstring>
9#include <filesystem>
10#include <string>
11#include <vector>
12
13namespace sm = strata::ds4::slotmeta;
14namespace fs = std::filesystem;
15
16static int fails = 0;
17#define CHECK(c)                                                           \
18    do {                                                                   \
19        if (!(c)) {                                                        \
20            std::printf("FAIL line %d: %s\n", __LINE__, #c);               \
21            ++fails;                                                       \
22        }                                                                  \
23    } while (0)
24
25static std::vector<int32_t> seq(int a, int n) {
26    std::vector<int32_t> v((size_t) n);
27    for (int i = 0; i < n; ++i) v[(size_t) i] = a + i;
28    return v;
29}
30static std::vector<int> as_int(const std::vector<int32_t>& v) { return std::vector<int>(v.begin(), v.end()); }
31
32static void patch(const std::string& p, size_t off, uint8_t b) {
33    std::FILE* f = std::fopen(p.c_str(), "r+b");
34    std::fseek(f, (long) off, SEEK_SET);
35    std::fwrite(&b, 1, 1, f);
36    std::fclose(f);
37}
38
39static void meta_tests(const fs::path& dir) {
40    const std::string id = "strata-ds4-session-2 engine=test", path = (dir / "slot-1.meta").string();
41    const std::vector<int32_t> toks = seq(100, 50);
42    const std::vector<int64_t> cks = {40, 33, 7};
43    CHECK(sm::write(path, id, toks, 123456789ull, cks));
44    CHECK(!fs::exists(path + ".tmp"));   // (renamed over, no half file left)
45    std::vector<int32_t> t;
46    std::vector<int64_t> c;
47    uint64_t bytes = 0;
48    CHECK(sm::read(path, id, 1000, t, bytes, c));
49    CHECK(t == toks && c == cks && bytes == 123456789ull);
50    // no checkpoints is valid (a slot of a state that had none)
51    CHECK(sm::write(path, id, toks, 5, {}));
52    CHECK(sm::read(path, id, 1000, t, bytes, c) && c.empty() && bytes == 5);
53    // another model's / engine's identity: refused, never read
54    CHECK(!sm::read(path, id + "x", 1000, t, bytes, c));
55    CHECK(!sm::read(path, "other", 1000, t, bytes, c));
56    // more tokens than the context: refused
57    CHECK(!sm::read(path, id, 49, t, bytes, c));
58    CHECK(sm::read(path, id, 50, t, bytes, c));
59    // a missing file
60    CHECK(!sm::read((dir / "none.meta").string(), id, 1000, t, bytes, c));
61    // every cut length, a damaged magic at either end
62    CHECK(sm::write(path, id, toks, 9, cks));
63    const size_t full = (size_t) fs::file_size(path);
64    for (size_t keep = 0; keep < full; ++keep) {
65        fs::copy_file(path, dir / "cut.meta", fs::copy_options::overwrite_existing);
66        fs::resize_file(dir / "cut.meta", keep);
67        if (sm::read((dir / "cut.meta").string(), id, 1000, t, bytes, c)) {
68            std::printf("FAIL: a .meta cut to %zu of %zu bytes was read\n", keep, full);
69            ++fails;
70            break;
71        }
72    }
73    fs::copy_file(path, dir / "bad0.meta", fs::copy_options::overwrite_existing);
74    patch((dir / "bad0.meta").string(), 0, 0x55);
75    CHECK(!sm::read((dir / "bad0.meta").string(), id, 1000, t, bytes, c));
76    fs::copy_file(path, dir / "bad1.meta", fs::copy_options::overwrite_existing);
77    patch((dir / "bad1.meta").string(), full - 1, 0x55);
78    CHECK(!sm::read((dir / "bad1.meta").string(), id, 1000, t, bytes, c));
79    // a checkpoint outside the conversation, a zero one, more than four
80    CHECK(sm::write(path, id, toks, 9, {51}));
81    CHECK(!sm::read(path, id, 1000, t, bytes, c));
82    CHECK(sm::write(path, id, toks, 9, {0}));
83    CHECK(!sm::read(path, id, 1000, t, bytes, c));
84    CHECK(sm::write(path, id, toks, 9, {4, 3, 2, 1}));
85    CHECK(sm::read(path, id, 1000, t, bytes, c) && c.size() == 4);
86    CHECK(sm::write(path, id, toks, 9, {1, 2, 3, 4, 5}));
87    CHECK(!sm::read(path, id, 1000, t, bytes, c));
88    // no tokens at all
89    CHECK(sm::write(path, id, {}, 9, {}));
90    CHECK(!sm::read(path, id, 1000, t, bytes, c));
91    // an unwritable place: false, not a crash
92    CHECK(!sm::write((dir / "nodir" / "x.meta").string(), id, toks, 1, {}));
93}
94
95static size_t pick(const std::vector<std::vector<int32_t>>& toks, const std::vector<std::vector<int64_t>>& cks,
96                   const std::vector<int>& prompt, size_t floor, size_t* len) {
97    std::vector<sm::Cand> c;
98    for (size_t i = 0; i < toks.size(); ++i) c.push_back({&toks[i], &cks[i]});
99    return sm::pick(c, prompt, floor, len);
100}
101
102static void pick_tests() {
103    size_t len = 99;
104    const std::vector<int32_t> a = seq(1000, 3000), b = seq(5000, 2000);
105    // the next turn of A: A whole, then new tokens
106    std::vector<int32_t> a_turn = a;
107    for (int32_t x : seq(9000, 40)) a_turn.push_back(x);
108    CHECK(pick({a, b}, {{}, {}}, as_int(a_turn), 0, &len) == 0 && len == a.size());
109    CHECK(pick({b, a}, {{}, {}}, as_int(a_turn), 0, &len) == 1 && len == a.size());
110    // nothing shared: none, len 0
111    CHECK(pick({a, b}, {{}, {}}, as_int(seq(7777, 100)), 0, &len) == SIZE_MAX && len == 0);
112    // a prompt equal to the slot's tokens: the last token is still read, so the slot is not taken whole
113    CHECK(pick({a}, {{}}, as_int(a), 0, &len) == SIZE_MAX && len == 0);
114    CHECK(pick({a}, {{2900}}, as_int(a), 0, &len) == 0 && len == 2900);
115    // a shorter prompt than the slot (a prefix of it): taken at a checkpoint within it, never past it
116    std::vector<int32_t> head(a.begin(), a.begin() + 2000);
117    CHECK(pick({a}, {{2900, 1500}}, as_int(head), 0, &len) == 0 && len == 1500);
118    // a retried prompt: A's prompt end (2950) is a checkpoint; the new prompt agrees up to 2960 then differs
119    std::vector<int32_t> retry(a.begin(), a.begin() + 2960);
120    for (int32_t x : seq(8000, 30)) retry.push_back(x);
121    CHECK(pick({a}, {{2950, 2000, 1000}}, as_int(retry), 0, &len) == 0 && len == 2950);
122    // diverging before the latest checkpoint: the latest one it still agrees with (the anchor)
123    std::vector<int32_t> early(a.begin(), a.begin() + 1200);
124    for (int32_t x : seq(8000, 30)) early.push_back(x);
125    CHECK(pick({a}, {{2950, 2000, 1000}}, as_int(early), 0, &len) == 0 && len == 1000);
126    // diverging before every checkpoint: none
127    std::vector<int32_t> first(a.begin(), a.begin() + 500);
128    for (int32_t x : seq(8000, 30)) first.push_back(x);
129    CHECK(pick({a}, {{2950, 2000, 1000}}, as_int(first), 0, &len) == SIZE_MAX && len == 0);
130    // the floor: what the model itself can go back to wins unless the slot goes further
131    CHECK(pick({a}, {{}}, as_int(a_turn), 3000, &len) == SIZE_MAX && len == 0);
132    CHECK(pick({a}, {{}}, as_int(a_turn), 2999, &len) == 0 && len == 3000);
133    // two slots, the longer match wins; a tie keeps the earlier one
134    std::vector<int32_t> a_short(a.begin(), a.begin() + 1000);
135    CHECK(pick({a_short, a}, {{}, {}}, as_int(a_turn), 0, &len) == 1 && len == 3000);
136    CHECK(pick({a, a}, {{}, {}}, as_int(a_turn), 0, &len) == 0 && len == 3000);
137    // too short a prompt
138    CHECK(pick({a}, {{}}, {1000}, 0, &len) == SIZE_MAX && len == 0);
139    CHECK(pick({a}, {{}}, {}, 0, &len) == SIZE_MAX && len == 0);
140    // an empty slot list
141    CHECK(pick({}, {}, as_int(a_turn), 0, &len) == SIZE_MAX && len == 0);
142}
143
144// pictures: a span is matched by its picture's hash, SCR never splits one, a slot's .pics round trip
145static void picspans_tests(const fs::path& dir) {
146    namespace ps = strata::ds4::picspans;
147    const int IMG = 9000;
148    std::vector<int> ids = {1, 2, IMG, IMG, IMG, IMG, 3, 4, IMG, IMG, IMG, 5};
149    std::vector<ps::Span> sp = {{2, 0, 111, 28, 14}, {8, 0, 222, 14, 14}};
150    ps::measure(ids, IMG, sp);
151    CHECK(sp[0].len == 4 && sp[1].len == 3);
152    const auto k1 = ps::keyed(ids, sp);
153    CHECK(k1[0] == 1 && k1[6] == 3 && k1[11] == 5);
154    for (int i = 2; i < 6; ++i) CHECK(k1[(size_t) i] < -1 && k1[(size_t) i] != 201);
155    CHECK(k1[2] != k1[3]);                      // (each offset its own id)
156    std::vector<ps::Span> other = sp;
157    other[1].hash = 333;                        // the same tokens, another second picture
158    const auto k2 = ps::keyed(ids, other);
159    size_t c = 0;
160    while (c < k1.size() && k1[c] == k2[c]) ++c;
161    CHECK(c == 8);                              // shared up to the swapped picture's span, not past it
162    CHECK(ps::keyed(ids, sp) == k1);            // the same picture: the same ids
163    // insert_map: 0..4 with slots inserted at 1 and 4 of the result
164    CHECK((ps::insert_map(5, {1, 4}) == std::vector<int64_t>{0, 2, 3, 5, 6}));
165    // whole_spans: a run (kept part [100, 400 - 32)) that starts inside a span [90, 110) and whose kept part ends
166    // inside [350, 380): it starts after the first, its kept part ends before the second
167    strata::core::ScrPlan plan;
168    plan.runs.push_back({100, 50, 300, 0, 100});
169    ps::whole_spans(plan, {{90, 20, 1, 0, 0}, {350, 30, 2, 0, 0}}, 32, 64);
170    CHECK(plan.runs.size() == 1);
171    if (!plan.runs.empty()) {
172        const auto& r = plan.runs[0];
173        CHECK(r.new_start == 110 && r.old_start == 60 && r.pad == 0 && r.at == 110);
174        CHECK(r.new_start + r.n - 32 <= 350 && r.n % 2 == 0);
175    }
176    // a run left too short goes; the next one's pad is worked out again
177    plan.runs = {{100, 51, 100, 1, 101}, {300, 400, 300, 0, 301}};
178    ps::whole_spans(plan, {{95, 60, 3, 0, 0}}, 32, 64);
179    CHECK(plan.runs.size() == 1 && plan.runs[0].new_start == 300 && plan.runs[0].pad == 0 && plan.runs[0].at == 300);
180    // no pictures: the plan is untouched
181    plan.runs = {{100, 51, 100, 1, 101}};
182    ps::whole_spans(plan, {}, 32, 64);
183    CHECK(plan.runs.size() == 1 && plan.runs[0].pad == 1 && plan.runs[0].at == 101);
184    // .pics round trip; a span past the slot's tokens, a damaged end, or a cut file are refused; no file: no pictures
185    const std::string pf = (dir / "slot-1.pics").string();
186    CHECK(ps::write(pf, sp));
187    std::vector<ps::Span> got;
188    CHECK(ps::read(pf, 12, got) && got.size() == 2 && got[1].start == 8 && got[1].len == 3 && got[1].hash == 222 &&
189          got[0].width == 28);
190    CHECK(!ps::read(pf, 10, got));
191    patch(pf, fs::file_size(pf) - 1, 0x00);
192    CHECK(!ps::read(pf, 12, got));
193    fs::resize_file(pf, 20);
194    CHECK(!ps::read(pf, 12, got));
195    CHECK(ps::read((dir / "none.pics").string(), 12, got) && got.empty());
196}
197
198int main() {
199    const fs::path dir = fs::temp_directory_path() / "ds4-slot-test";
200    std::error_code ec;
201    fs::remove_all(dir, ec);
202    fs::create_directories(dir);
203    meta_tests(dir);
204    pick_tests();
205    picspans_tests(dir);
206    fs::remove_all(dir, ec);
207    std::printf(fails ? "FAILED: %d\n" : "slot_test: all passed\n", fails);
208    return fails ? 1 : 0;
209}