// Pictures in the kept-conversation and SCR bookkeeping (no GPU): every position of a picture's span carries the same // image token, so token equality is not picture equality. The matchers (prefix reuse, the slot a prompt takes back, // SCR's runs) compare a "keyed" copy of the tokens instead, where each span position is a negative id derived from the // picture's hash and the position's offset in its span: two spans agree only for the same picture, offset by offset, // and never with a real token or the pad. SCR moves or reads a picture's span whole (whole_spans); a slot keeps its // pictures in a .pics file beside its .bin (write / read). The engine's serve loop (ds4_main.cpp) uses these; // tests/dsv41/slot_test.cpp checks them alone. // // .pics layout (little endian): u64 magic "DS4PICS1", u64 count, then per picture i64 start, i64 len, u64 hash, // i32 width, i32 height; u64 magic #pragma once #include "strata/core/scr.hpp" #include #include #include #include #include #include namespace strata::ds4::picspans { struct Span { int64_t start = 0, len = 0; uint64_t hash = 0; int32_t width = 0, height = 0; }; constexpr uint64_t kMagic = 0x3153434950345344ull; // "DS4PICS1" // the matcher's id of a picture's position `off` (negative: never a real token, the pad or -1) inline int32_t key(uint64_t hash, int64_t off) { uint64_t h = (hash ^ ((uint64_t) off * 0x9E3779B97F4A7C15ull)) * 0xBF58476D1CE4E5B9ull; h ^= h >> 31; return -2 - (int32_t) (h & 0x3FFFFFFF); } // `toks` with every span position replaced by its key (a span past the end is cut there) template std::vector keyed(const std::vector& toks, const std::vector& spans) { std::vector out(toks.begin(), toks.end()); for (const Span& s : spans) for (int64_t i = 0; i < s.len && s.start + i < (int64_t) out.size(); ++i) if (s.start + i >= 0) out[(size_t) (s.start + i)] = key(s.hash, i); return out; } // each picture's span in a prompt: the run of image tokens from its start, up to the next picture's start template void measure(const std::vector& ids, int image_token, std::vector& spans) { for (size_t k = 0; k < spans.size(); ++k) { const int64_t lim = k + 1 < spans.size() ? spans[k + 1].start : (int64_t) ids.size(); int64_t n = 0; while (spans[k].start + n < lim && (int) ids[(size_t) (spans[k].start + n)] == image_token) ++n; spans[k].len = n; } } // the spans after `n` positions were inserted into their prompt: map[i] is old position i's new one inline std::vector moved(const std::vector& spans, const std::vector& map) { std::vector out; for (Span s : spans) { if (s.start < 0 || s.start >= (int64_t) map.size()) continue; s.start = map[(size_t) s.start]; out.push_back(s); } return out; } // positions 0..n-1 after inserting a slot at each of `at` (sorted positions in the result): the new place of each inline std::vector insert_map(size_t n, const std::vector& at) { std::vector map(n); size_t k = 0; int64_t out = 0; for (size_t i = 0; i < n; ++i) { while (k < at.size() && at[k] == out) ++k, ++out; map[i] = out++; } return map; } // SCR's runs cut so no picture is split: each run reuses [new_start, new_start + n - band) of the prompt and reads its // last `band` tokens again, and a picture's span must lie wholly in one or the other (or outside the run). A run that // starts inside a span starts after it; one whose kept part ends inside a span ends before it. Runs shorter than // `min_n` go, and every run's pad is worked out again (block 2: an even shift). `spans` are in the prompt's // coordinates (the plan's new_start's). inline void whole_spans(core::ScrPlan& plan, const std::vector& spans, int64_t band, int64_t min_n) { if (spans.empty() || plan.runs.empty()) return; for (core::ScrRun& r : plan.runs) { bool changed = true; while (changed && r.n > 0) { changed = false; for (const Span& s : spans) { const int64_t e = s.start + s.len; if (s.start < r.new_start && e > r.new_start) { // starts inside a span: after it, on an even step const int64_t cut = (e - r.new_start + 1) / 2 * 2; r.new_start += cut, r.old_start += cut, r.n -= cut; changed = true; } const int64_t keep_end = r.new_start + r.n - band; if (r.n > 0 && s.start < keep_end && e > keep_end) { // the kept part ends inside: before the span r.n = std::max(0, (s.start + band - r.new_start) / 2 * 2); changed = true; } } } } plan.runs.erase(std::remove_if(plan.runs.begin(), plan.runs.end(), [&](const core::ScrRun& r) { return r.n < min_n; }), plan.runs.end()); int64_t pads = 0; for (core::ScrRun& r : plan.runs) { r.pad = ((r.old_start - (r.new_start + pads)) % 2 + 2) % 2; pads += r.pad; r.at = r.new_start + pads; } } // written under a temporary name and renamed over `path` inline bool write(const std::string& path, const std::vector& spans) { const std::string tmp = path + ".tmp"; { std::ofstream f(tmp, std::ios::binary | std::ios::trunc); const uint64_t n = spans.size(); f.write((const char*) &kMagic, 8); f.write((const char*) &n, 8); for (const Span& s : spans) { f.write((const char*) &s.start, 8); f.write((const char*) &s.len, 8); f.write((const char*) &s.hash, 8); f.write((const char*) &s.width, 4); f.write((const char*) &s.height, 4); } f.write((const char*) &kMagic, 8); f.flush(); if (!f) return false; } std::error_code ec; std::filesystem::rename(tmp, path, ec); return !ec; } // false: damaged, cut short, or a span outside the slot's n tokens (no file: true, no pictures) inline bool read(const std::string& path, int64_t n_tokens, std::vector& spans) { spans.clear(); std::ifstream f(path, std::ios::binary); if (!f) return !std::filesystem::exists(path); uint64_t m0 = 0, n = 0, m1 = 0; if (!f.read((char*) &m0, 8) || m0 != kMagic || !f.read((char*) &n, 8) || n > 4096) return false; spans.resize(n); for (Span& s : spans) if (!f.read((char*) &s.start, 8) || !f.read((char*) &s.len, 8) || !f.read((char*) &s.hash, 8) || !f.read((char*) &s.width, 4) || !f.read((char*) &s.height, 4) || s.start < 0 || s.len <= 0 || s.start + s.len > n_tokens) return false; return f.read((char*) &m1, 8) && m1 == kMagic; } } // namespace strata::ds4::picspans