// The kept conversations' bookkeeping without a GPU (src/dsv41/ds4_slotmeta.hpp): the .meta file's round trip and // every way it is refused, and which kept conversation a prompt continues, and from where. // ds4_slot_test exit 0: all passed #include "ds4_slotmeta.hpp" #include "ds4_picspans.hpp" #include #include #include #include #include namespace sm = strata::ds4::slotmeta; namespace fs = std::filesystem; static int fails = 0; #define CHECK(c) \ do { \ if (!(c)) { \ std::printf("FAIL line %d: %s\n", __LINE__, #c); \ ++fails; \ } \ } while (0) static std::vector seq(int a, int n) { std::vector v((size_t) n); for (int i = 0; i < n; ++i) v[(size_t) i] = a + i; return v; } static std::vector as_int(const std::vector& v) { return std::vector(v.begin(), v.end()); } static void patch(const std::string& p, size_t off, uint8_t b) { std::FILE* f = std::fopen(p.c_str(), "r+b"); std::fseek(f, (long) off, SEEK_SET); std::fwrite(&b, 1, 1, f); std::fclose(f); } static void meta_tests(const fs::path& dir) { const std::string id = "strata-ds4-session-2 engine=test", path = (dir / "slot-1.meta").string(); const std::vector toks = seq(100, 50); const std::vector cks = {40, 33, 7}; CHECK(sm::write(path, id, toks, 123456789ull, cks)); CHECK(!fs::exists(path + ".tmp")); // (renamed over, no half file left) std::vector t; std::vector c; uint64_t bytes = 0; CHECK(sm::read(path, id, 1000, t, bytes, c)); CHECK(t == toks && c == cks && bytes == 123456789ull); // no checkpoints is valid (a slot of a state that had none) CHECK(sm::write(path, id, toks, 5, {})); CHECK(sm::read(path, id, 1000, t, bytes, c) && c.empty() && bytes == 5); // another model's / engine's identity: refused, never read CHECK(!sm::read(path, id + "x", 1000, t, bytes, c)); CHECK(!sm::read(path, "other", 1000, t, bytes, c)); // more tokens than the context: refused CHECK(!sm::read(path, id, 49, t, bytes, c)); CHECK(sm::read(path, id, 50, t, bytes, c)); // a missing file CHECK(!sm::read((dir / "none.meta").string(), id, 1000, t, bytes, c)); // every cut length, a damaged magic at either end CHECK(sm::write(path, id, toks, 9, cks)); const size_t full = (size_t) fs::file_size(path); for (size_t keep = 0; keep < full; ++keep) { fs::copy_file(path, dir / "cut.meta", fs::copy_options::overwrite_existing); fs::resize_file(dir / "cut.meta", keep); if (sm::read((dir / "cut.meta").string(), id, 1000, t, bytes, c)) { std::printf("FAIL: a .meta cut to %zu of %zu bytes was read\n", keep, full); ++fails; break; } } fs::copy_file(path, dir / "bad0.meta", fs::copy_options::overwrite_existing); patch((dir / "bad0.meta").string(), 0, 0x55); CHECK(!sm::read((dir / "bad0.meta").string(), id, 1000, t, bytes, c)); fs::copy_file(path, dir / "bad1.meta", fs::copy_options::overwrite_existing); patch((dir / "bad1.meta").string(), full - 1, 0x55); CHECK(!sm::read((dir / "bad1.meta").string(), id, 1000, t, bytes, c)); // a checkpoint outside the conversation, a zero one, more than four CHECK(sm::write(path, id, toks, 9, {51})); CHECK(!sm::read(path, id, 1000, t, bytes, c)); CHECK(sm::write(path, id, toks, 9, {0})); CHECK(!sm::read(path, id, 1000, t, bytes, c)); CHECK(sm::write(path, id, toks, 9, {4, 3, 2, 1})); CHECK(sm::read(path, id, 1000, t, bytes, c) && c.size() == 4); CHECK(sm::write(path, id, toks, 9, {1, 2, 3, 4, 5})); CHECK(!sm::read(path, id, 1000, t, bytes, c)); // no tokens at all CHECK(sm::write(path, id, {}, 9, {})); CHECK(!sm::read(path, id, 1000, t, bytes, c)); // an unwritable place: false, not a crash CHECK(!sm::write((dir / "nodir" / "x.meta").string(), id, toks, 1, {})); } static size_t pick(const std::vector>& toks, const std::vector>& cks, const std::vector& prompt, size_t floor, size_t* len) { std::vector c; for (size_t i = 0; i < toks.size(); ++i) c.push_back({&toks[i], &cks[i]}); return sm::pick(c, prompt, floor, len); } static void pick_tests() { size_t len = 99; const std::vector a = seq(1000, 3000), b = seq(5000, 2000); // the next turn of A: A whole, then new tokens std::vector a_turn = a; for (int32_t x : seq(9000, 40)) a_turn.push_back(x); CHECK(pick({a, b}, {{}, {}}, as_int(a_turn), 0, &len) == 0 && len == a.size()); CHECK(pick({b, a}, {{}, {}}, as_int(a_turn), 0, &len) == 1 && len == a.size()); // nothing shared: none, len 0 CHECK(pick({a, b}, {{}, {}}, as_int(seq(7777, 100)), 0, &len) == SIZE_MAX && len == 0); // a prompt equal to the slot's tokens: the last token is still read, so the slot is not taken whole CHECK(pick({a}, {{}}, as_int(a), 0, &len) == SIZE_MAX && len == 0); CHECK(pick({a}, {{2900}}, as_int(a), 0, &len) == 0 && len == 2900); // a shorter prompt than the slot (a prefix of it): taken at a checkpoint within it, never past it std::vector head(a.begin(), a.begin() + 2000); CHECK(pick({a}, {{2900, 1500}}, as_int(head), 0, &len) == 0 && len == 1500); // a retried prompt: A's prompt end (2950) is a checkpoint; the new prompt agrees up to 2960 then differs std::vector retry(a.begin(), a.begin() + 2960); for (int32_t x : seq(8000, 30)) retry.push_back(x); CHECK(pick({a}, {{2950, 2000, 1000}}, as_int(retry), 0, &len) == 0 && len == 2950); // diverging before the latest checkpoint: the latest one it still agrees with (the anchor) std::vector early(a.begin(), a.begin() + 1200); for (int32_t x : seq(8000, 30)) early.push_back(x); CHECK(pick({a}, {{2950, 2000, 1000}}, as_int(early), 0, &len) == 0 && len == 1000); // diverging before every checkpoint: none std::vector first(a.begin(), a.begin() + 500); for (int32_t x : seq(8000, 30)) first.push_back(x); CHECK(pick({a}, {{2950, 2000, 1000}}, as_int(first), 0, &len) == SIZE_MAX && len == 0); // the floor: what the model itself can go back to wins unless the slot goes further CHECK(pick({a}, {{}}, as_int(a_turn), 3000, &len) == SIZE_MAX && len == 0); CHECK(pick({a}, {{}}, as_int(a_turn), 2999, &len) == 0 && len == 3000); // two slots, the longer match wins; a tie keeps the earlier one std::vector a_short(a.begin(), a.begin() + 1000); CHECK(pick({a_short, a}, {{}, {}}, as_int(a_turn), 0, &len) == 1 && len == 3000); CHECK(pick({a, a}, {{}, {}}, as_int(a_turn), 0, &len) == 0 && len == 3000); // too short a prompt CHECK(pick({a}, {{}}, {1000}, 0, &len) == SIZE_MAX && len == 0); CHECK(pick({a}, {{}}, {}, 0, &len) == SIZE_MAX && len == 0); // an empty slot list CHECK(pick({}, {}, as_int(a_turn), 0, &len) == SIZE_MAX && len == 0); } // pictures: a span is matched by its picture's hash, SCR never splits one, a slot's .pics round trip static void picspans_tests(const fs::path& dir) { namespace ps = strata::ds4::picspans; const int IMG = 9000; std::vector ids = {1, 2, IMG, IMG, IMG, IMG, 3, 4, IMG, IMG, IMG, 5}; std::vector sp = {{2, 0, 111, 28, 14}, {8, 0, 222, 14, 14}}; ps::measure(ids, IMG, sp); CHECK(sp[0].len == 4 && sp[1].len == 3); const auto k1 = ps::keyed(ids, sp); CHECK(k1[0] == 1 && k1[6] == 3 && k1[11] == 5); for (int i = 2; i < 6; ++i) CHECK(k1[(size_t) i] < -1 && k1[(size_t) i] != 201); CHECK(k1[2] != k1[3]); // (each offset its own id) std::vector other = sp; other[1].hash = 333; // the same tokens, another second picture const auto k2 = ps::keyed(ids, other); size_t c = 0; while (c < k1.size() && k1[c] == k2[c]) ++c; CHECK(c == 8); // shared up to the swapped picture's span, not past it CHECK(ps::keyed(ids, sp) == k1); // the same picture: the same ids // insert_map: 0..4 with slots inserted at 1 and 4 of the result CHECK((ps::insert_map(5, {1, 4}) == std::vector{0, 2, 3, 5, 6})); // whole_spans: a run (kept part [100, 400 - 32)) that starts inside a span [90, 110) and whose kept part ends // inside [350, 380): it starts after the first, its kept part ends before the second strata::core::ScrPlan plan; plan.runs.push_back({100, 50, 300, 0, 100}); ps::whole_spans(plan, {{90, 20, 1, 0, 0}, {350, 30, 2, 0, 0}}, 32, 64); CHECK(plan.runs.size() == 1); if (!plan.runs.empty()) { const auto& r = plan.runs[0]; CHECK(r.new_start == 110 && r.old_start == 60 && r.pad == 0 && r.at == 110); CHECK(r.new_start + r.n - 32 <= 350 && r.n % 2 == 0); } // a run left too short goes; the next one's pad is worked out again plan.runs = {{100, 51, 100, 1, 101}, {300, 400, 300, 0, 301}}; ps::whole_spans(plan, {{95, 60, 3, 0, 0}}, 32, 64); CHECK(plan.runs.size() == 1 && plan.runs[0].new_start == 300 && plan.runs[0].pad == 0 && plan.runs[0].at == 300); // no pictures: the plan is untouched plan.runs = {{100, 51, 100, 1, 101}}; ps::whole_spans(plan, {}, 32, 64); CHECK(plan.runs.size() == 1 && plan.runs[0].pad == 1 && plan.runs[0].at == 101); // .pics round trip; a span past the slot's tokens, a damaged end, or a cut file are refused; no file: no pictures const std::string pf = (dir / "slot-1.pics").string(); CHECK(ps::write(pf, sp)); std::vector got; CHECK(ps::read(pf, 12, got) && got.size() == 2 && got[1].start == 8 && got[1].len == 3 && got[1].hash == 222 && got[0].width == 28); CHECK(!ps::read(pf, 10, got)); patch(pf, fs::file_size(pf) - 1, 0x00); CHECK(!ps::read(pf, 12, got)); fs::resize_file(pf, 20); CHECK(!ps::read(pf, 12, got)); CHECK(ps::read((dir / "none.pics").string(), 12, got) && got.empty()); } int main() { const fs::path dir = fs::temp_directory_path() / "ds4-slot-test"; std::error_code ec; fs::remove_all(dir, ec); fs::create_directories(dir); meta_tests(dir); pick_tests(); picspans_tests(dir); fs::remove_all(dir, ec); std::printf(fails ? "FAILED: %d\n" : "slot_test: all passed\n", fails); return fails ? 1 : 0; }