okami.mom/strata

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

waveringanaRestore the DeepSeek V4.1 engine and CPU tests removed upstream8ac8ce32e

main
4.5 KiB98 linesraw
1// The kept-conversation (slot) bookkeeping that needs no GPU: a slot's .meta file (identity, size, tokens, checkpoint
2// positions) and the choice of which kept conversation a prompt continues, and from where.  The engine's serve loop
3// (ds4_main.cpp) uses these; tests/dsv41/slot_test.cpp checks them alone.
4//
5// Slot .meta layout (little endian), "DS4META2":
6//   u64 magic, u64 identity length, identity bytes, u64 .bin bytes, u64 token count, i32 tokens[],
7//   u64 checkpoint count (0..4), i64 checkpoints[], u64 magic
8// The magic at both ends and the .bin size make a half-written or foreign file refused, never read.
9#pragma once
10
11#include <algorithm>
12#include <cstddef>
13#include <cstdint>
14#include <filesystem>
15#include <fstream>
16#include <string>
17#include <vector>
18
19namespace strata::ds4::slotmeta {
20
21constexpr uint64_t kMagic = 0x324154454d345344ull;   // "DS4META2"
22
23// Written under a temporary name and renamed over `path` (an interrupted write leaves no half file).
24inline bool write(const std::string& path, const std::string& identity, const std::vector<int32_t>& toks,
25                  uint64_t bytes, const std::vector<int64_t>& cks) {
26    const std::string tmp = path + ".tmp";
27    {
28        std::ofstream f(tmp, std::ios::binary | std::ios::trunc);
29        const uint64_t il = identity.size(), nt = toks.size(), nc = cks.size();
30        f.write((const char*) &kMagic, 8);
31        f.write((const char*) &il, 8);
32        f.write(identity.data(), (std::streamsize) il);
33        f.write((const char*) &bytes, 8);
34        f.write((const char*) &nt, 8);
35        f.write((const char*) toks.data(), (std::streamsize) (nt * sizeof(int32_t)));
36        f.write((const char*) &nc, 8);
37        f.write((const char*) cks.data(), (std::streamsize) (nc * sizeof(int64_t)));
38        f.write((const char*) &kMagic, 8);
39        f.flush();
40        if (!f) return false;
41    }
42    std::error_code ec;
43    std::filesystem::rename(tmp, path, ec);
44    return !ec;
45}
46
47// false: foreign (another identity), damaged, cut short, or not plausible (no tokens, more than max_ctx, a checkpoint
48// outside the tokens).
49inline bool read(const std::string& path, const std::string& identity, int64_t max_ctx, std::vector<int32_t>& toks,
50                 uint64_t& bytes, std::vector<int64_t>& cks) {
51    std::ifstream f(path, std::ios::binary);
52    uint64_t m0 = 0, il = 0, nt = 0, nc = 0, m1 = 0;
53    if (!f.read((char*) &m0, 8) || m0 != kMagic || !f.read((char*) &il, 8) || il != identity.size()) return false;
54    std::string id(il, '\0');
55    if (!f.read(id.data(), (std::streamsize) il) || id != identity) return false;
56    if (!f.read((char*) &bytes, 8) || !f.read((char*) &nt, 8) || nt == 0 || nt > (uint64_t) max_ctx) return false;
57    toks.resize(nt);
58    if (!f.read((char*) toks.data(), (std::streamsize) (nt * sizeof(int32_t))) || !f.read((char*) &nc, 8) || nc > 4)
59        return false;
60    cks.resize(nc);
61    if (!f.read((char*) cks.data(), (std::streamsize) (nc * sizeof(int64_t))) || !f.read((char*) &m1, 8) || m1 != kMagic)
62        return false;
63    for (int64_t c : cks)
64        if (c <= 0 || (uint64_t) c > nt) return false;
65    return true;
66}
67
68// The kept conversation `prompt` continues the furthest, and where: *len is the number of the slot's tokens that are
69// the prompt's (0: none).  A conversation the prompt continues whole comes back whole (len == its size); one the prompt
70// leaves part way - a retried prompt, a history re-rendered without the earlier reasoning - comes back at its latest
71// checkpoint the prompt still agrees with.  Only a match longer than `floor` (what the model itself can go back to)
72// counts.  The last prompt token is never covered (a prompt always reads its last token).  Ties keep the earlier slot.
73struct Cand {
74    const std::vector<int32_t>* tokens;
75    const std::vector<int64_t>* cks;
76};
77inline size_t pick(const std::vector<Cand>& slots, const std::vector<int>& prompt, size_t floor, size_t* len) {
78    *len = 0;
79    if (prompt.size() < 2) return SIZE_MAX;
80    size_t best = SIZE_MAX;
81    for (size_t i = 0; i < slots.size(); ++i) {
82        const std::vector<int32_t>& t = *slots[i].tokens;
83        const size_t lim = std::min(t.size(), prompt.size() - 1);
84        size_t c = 0;
85        while (c < lim && t[c] == prompt[c]) ++c;
86        size_t u = c == t.size() ? c : 0;
87        if (u == 0)
88            for (int64_t k : *slots[i].cks)
89                if ((size_t) k <= c && (size_t) k > u) u = (size_t) k;
90        if (u > *len && u > floor) {
91            best = i;
92            *len = u;
93        }
94    }
95    return best;
96}
97
98}  // namespace strata::ds4::slotmeta