okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
24d897c06
main
1// Pictures in the kept-conversation and SCR bookkeeping (no GPU): every position of a picture's span carries the same 2// image token, so token equality is not picture equality. The matchers (prefix reuse, the slot a prompt takes back, 3// SCR's runs) compare a "keyed" copy of the tokens instead, where each span position is a negative id derived from the 4// picture's hash and the position's offset in its span: two spans agree only for the same picture, offset by offset, 5// and never with a real token or the pad. SCR moves or reads a picture's span whole (whole_spans); a slot keeps its 6// pictures in a .pics file beside its .bin (write / read). The engine's serve loop (ds4_main.cpp) uses these; 7// tests/dsv41/slot_test.cpp checks them alone. 8// 9// .pics layout (little endian): u64 magic "DS4PICS1", u64 count, then per picture i64 start, i64 len, u64 hash, 10// i32 width, i32 height; u64 magic 11#pragma once 12 13#include "strata/core/scr.hpp" 14 15#include <algorithm> 16#include <cstdint> 17#include <filesystem> 18#include <fstream> 19#include <string> 20#include <vector> 21 22namespace strata::ds4::picspans { 23 24struct Span { 25int64_t start = 0 ,len = 0 ; 26uint64_t hash = 0 ; 27int32_t width = 0 ,height = 0 ; 28}; 29 30constexpr uint64_t kMagic = 0x3153434950345344ull ;// "DS4PICS1" 31 32// the matcher's id of a picture's position `off` (negative: never a real token, the pad or -1) 33inline int32_t key (uint64_t hash ,int64_t off ) { 34uint64_t h = (hash ^ ((uint64_t )off * 0x9E3779B97F4A7C15ull ))* 0xBF58476D1CE4E5B9ull ; 35h ^=h >>31 ; 36return -2 - (int32_t ) (h & 0x3FFFFFFF ); 37} 38 39// `toks` with every span position replaced by its key (a span past the end is cut there) 40template < class T > 41std::vector < int32_t > keyed (const std::vector < T >& toks ,const std::vector < Span >& spans ) { 42 std::vector < int32_t > out (toks .begin (),toks .end ()); 43for (const Span & s :spans ) 44for (int64_t i = 0 ;i < s .len && s .start + i < (int64_t )out .size ();++ i ) 45if (s .start + i >=0 )out [(size_t ) (s .start + i )]= key (s .hash ,i ); 46return out ; 47} 48 49// each picture's span in a prompt: the run of image tokens from its start, up to the next picture's start 50template < class T > 51void measure (const std::vector < T >& ids ,int image_token , std::vector < Span >& spans ) { 52for (size_t k = 0 ;k < spans .size ();++ k ) { 53const int64_t lim = k + 1 < spans .size () ?spans [k + 1 ].start : (int64_t )ids .size (); 54int64_t n = 0 ; 55while (spans [k ].start + n < lim && (int )ids [(size_t ) (spans [k ].start + n )]== image_token )++ n ; 56spans [k ].len = n ; 57 } 58} 59 60// the spans after `n` positions were inserted into their prompt: map[i] is old position i's new one 61inline std::vector < Span > moved (const std::vector < Span >& spans ,const std::vector < int64_t >& map ) { 62 std::vector < Span > out ; 63for (Span s :spans ) { 64if (s .start < 0 || s .start >= (int64_t )map .size ())continue ; 65s .start = map [(size_t )s .start ]; 66out .push_back (s ); 67 } 68return out ; 69} 70 71// positions 0..n-1 after inserting a slot at each of `at` (sorted positions in the result): the new place of each 72inline std::vector < int64_t > insert_map (size_t n ,const std::vector < int64_t >& at ) { 73 std::vector < int64_t > map (n ); 74size_t k = 0 ; 75int64_t out = 0 ; 76for (size_t i = 0 ;i < n ;++ i ) { 77while (k < at .size ()&& at [k ]== out )++ k ,++ out ; 78map [i ]= out ++ ; 79 } 80return map ; 81} 82 83// SCR's runs cut so no picture is split: each run reuses [new_start, new_start + n - band) of the prompt and reads its 84// last `band` tokens again, and a picture's span must lie wholly in one or the other (or outside the run). A run that 85// starts inside a span starts after it; one whose kept part ends inside a span ends before it. Runs shorter than 86// `min_n` go, and every run's pad is worked out again (block 2: an even shift). `spans` are in the prompt's 87// coordinates (the plan's new_start's). 88inline void whole_spans (core::ScrPlan & plan ,const std::vector < Span >& spans ,int64_t band ,int64_t min_n ) { 89if (spans .empty ()|| plan .runs .empty ())return ; 90for (core::ScrRun & r :plan .runs ) { 91bool changed = true; 92while (changed && r .n > 0 ) { 93changed = false; 94for (const Span & s :spans ) { 95const int64_t e = s .start + s .len ; 96if (s .start < r .new_start && e > r .new_start ) {// starts inside a span: after it, on an even step 97const int64_t cut = (e - r .new_start + 1 ) /2 * 2 ; 98r .new_start += cut ,r .old_start += cut ,r .n -= cut ; 99changed = true; 100 } 101const int64_t keep_end = r .new_start + r .n - band ; 102if (r .n > 0 && s .start < keep_end && e > keep_end ) {// the kept part ends inside: before the span 103r .n = std::max < int64_t > (0 , (s .start + band - r .new_start ) /2 * 2 ); 104changed = true; 105 } 106 } 107 } 108 } 109plan .runs .erase (std::remove_if (plan .runs .begin (),plan .runs .end (), 110 [& ](const core::ScrRun & r ) {return r .n < min_n ; }), 111plan .runs .end ()); 112int64_t pads = 0 ; 113for (core::ScrRun & r :plan .runs ) { 114r .pad = ((r .old_start - (r .new_start + pads )) %2 + 2 ) %2 ; 115pads += r .pad ; 116r .at = r .new_start + pads ; 117 } 118} 119 120// written under a temporary name and renamed over `path` 121inline bool write (const std::string & path ,const std::vector < Span >& spans ) { 122const std::string tmp = path + ".tmp" ; 123 { 124 std::ofstream f (tmp , std::ios::binary | std::ios::trunc ); 125const uint64_t n = spans .size (); 126f .write ((const char * )& kMagic ,8 ); 127f .write ((const char * )& n ,8 ); 128for (const Span & s :spans ) { 129f .write ((const char * )& s .start ,8 ); 130f .write ((const char * )& s .len ,8 ); 131f .write ((const char * )& s .hash ,8 ); 132f .write ((const char * )& s .width ,4 ); 133f .write ((const char * )& s .height ,4 ); 134 } 135f .write ((const char * )& kMagic ,8 ); 136f .flush (); 137if (!f )return false; 138 } 139 std::error_code ec ; 140 std::filesystem::rename (tmp ,path ,ec ); 141return !ec ; 142} 143 144// false: damaged, cut short, or a span outside the slot's n tokens (no file: true, no pictures) 145inline bool read (const std::string & path ,int64_t n_tokens , std::vector < Span >& spans ) { 146spans .clear (); 147 std::ifstream f (path , std::ios::binary ); 148if (!f )return !std::filesystem::exists (path ); 149uint64_t m0 = 0 ,n = 0 ,m1 = 0 ; 150if (!f .read ((char * )& m0 ,8 )|| m0 != kMagic || !f .read ((char * )& n ,8 )|| n > 4096 )return false; 151spans .resize (n ); 152for (Span & s :spans ) 153if (!f .read ((char * )& s .start ,8 )|| !f .read ((char * )& s .len ,8 )|| !f .read ((char * )& s .hash ,8 )|| 154 !f .read ((char * )& s .width ,4 )|| !f .read ((char * )& s .height ,4 )|| s .start < 0 || s .len <=0 || 155s .start + s .len > n_tokens ) 156return false; 157return f .read ((char * )& m1 ,8 )&& m1 == kMagic ; 158} 159 160}// namespace strata::ds4::picspans