okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
24d897c06
main
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 ); 27for (int i = 0 ;i < n ;++ i )v [(size_t )i ]= a + i ; 28return 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 ) { 40const std::string id = "strata-ds4-session-2 engine=test" ,path = (dir /"slot-1.meta" ).string (); 41const std::vector < int32_t > toks = seq (100 ,50 ); 42const std::vector < int64_t > cks = {40 ,33 ,7 }; 43CHECK (sm::write (path ,id ,toks ,123456789ull ,cks )); 44CHECK (!fs::exists (path + ".tmp" ));// (renamed over, no half file left) 45 std::vector < int32_t > t ; 46 std::vector < int64_t > c ; 47uint64_t bytes = 0 ; 48CHECK (sm::read (path ,id ,1000 ,t ,bytes ,c )); 49CHECK (t == toks && c == cks && bytes == 123456789ull ); 50// no checkpoints is valid (a slot of a state that had none) 51CHECK (sm::write (path ,id ,toks ,5 , {})); 52CHECK (sm::read (path ,id ,1000 ,t ,bytes ,c )&& c .empty ()&& bytes == 5 ); 53// another model's / engine's identity: refused, never read 54CHECK (!sm::read (path ,id + "x" ,1000 ,t ,bytes ,c )); 55CHECK (!sm::read (path ,"other" ,1000 ,t ,bytes ,c )); 56// more tokens than the context: refused 57CHECK (!sm::read (path ,id ,49 ,t ,bytes ,c )); 58CHECK (sm::read (path ,id ,50 ,t ,bytes ,c )); 59// a missing file 60CHECK (!sm::read ((dir /"none.meta" ).string (),id ,1000 ,t ,bytes ,c )); 61// every cut length, a damaged magic at either end 62CHECK (sm::write (path ,id ,toks ,9 ,cks )); 63const size_t full = (size_t ) fs::file_size (path ); 64for (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 ); 67if (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 ; 70break ; 71 } 72 } 73 fs::copy_file (path ,dir /"bad0.meta" , fs::copy_options::overwrite_existing ); 74patch ((dir /"bad0.meta" ).string (),0 ,0x55 ); 75CHECK (!sm::read ((dir /"bad0.meta" ).string (),id ,1000 ,t ,bytes ,c )); 76 fs::copy_file (path ,dir /"bad1.meta" , fs::copy_options::overwrite_existing ); 77patch ((dir /"bad1.meta" ).string (),full - 1 ,0x55 ); 78CHECK (!sm::read ((dir /"bad1.meta" ).string (),id ,1000 ,t ,bytes ,c )); 79// a checkpoint outside the conversation, a zero one, more than four 80CHECK (sm::write (path ,id ,toks ,9 , {51 })); 81CHECK (!sm::read (path ,id ,1000 ,t ,bytes ,c )); 82CHECK (sm::write (path ,id ,toks ,9 , {0 })); 83CHECK (!sm::read (path ,id ,1000 ,t ,bytes ,c )); 84CHECK (sm::write (path ,id ,toks ,9 , {4 ,3 ,2 ,1 })); 85CHECK (sm::read (path ,id ,1000 ,t ,bytes ,c )&& c .size ()== 4 ); 86CHECK (sm::write (path ,id ,toks ,9 , {1 ,2 ,3 ,4 ,5 })); 87CHECK (!sm::read (path ,id ,1000 ,t ,bytes ,c )); 88// no tokens at all 89CHECK (sm::write (path ,id , {},9 , {})); 90CHECK (!sm::read (path ,id ,1000 ,t ,bytes ,c )); 91// an unwritable place: false, not a crash 92CHECK (!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 , 96const std::vector < int >& prompt ,size_t floor ,size_t * len ) { 97 std::vector < sm::Cand > c ; 98for (size_t i = 0 ;i < toks .size ();++ i )c .push_back ({& toks [i ],& cks [i ]}); 99return sm::pick (c ,prompt ,floor ,len ); 100} 101 102static void pick_tests () { 103size_t len = 99 ; 104const 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 ; 107for (int32_t x :seq (9000 ,40 ))a_turn .push_back (x ); 108CHECK (pick ({a ,b }, {{}, {}},as_int (a_turn ),0 ,& len )== 0 && len == a .size ()); 109CHECK (pick ({b ,a }, {{}, {}},as_int (a_turn ),0 ,& len )== 1 && len == a .size ()); 110// nothing shared: none, len 0 111CHECK (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 113CHECK (pick ({a }, {{}},as_int (a ),0 ,& len )== SIZE_MAX && len == 0 ); 114CHECK (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 ); 117CHECK (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 ); 120for (int32_t x :seq (8000 ,30 ))retry .push_back (x ); 121CHECK (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 ); 124for (int32_t x :seq (8000 ,30 ))early .push_back (x ); 125CHECK (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 ); 128for (int32_t x :seq (8000 ,30 ))first .push_back (x ); 129CHECK (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 131CHECK (pick ({a }, {{}},as_int (a_turn ),3000 ,& len )== SIZE_MAX && len == 0 ); 132CHECK (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 ); 135CHECK (pick ({a_short ,a }, {{}, {}},as_int (a_turn ),0 ,& len )== 1 && len == 3000 ); 136CHECK (pick ({a ,a }, {{}, {}},as_int (a_turn ),0 ,& len )== 0 && len == 3000 ); 137// too short a prompt 138CHECK (pick ({a }, {{}}, {1000 },0 ,& len )== SIZE_MAX && len == 0 ); 139CHECK (pick ({a }, {{}}, {},0 ,& len )== SIZE_MAX && len == 0 ); 140// an empty slot list 141CHECK (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 ) { 146namespace ps= strata::ds4::picspans; 147const 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 ); 151CHECK (sp [0 ].len == 4 && sp [1 ].len == 3 ); 152const auto k1 = ps::keyed (ids ,sp ); 153CHECK (k1 [0 ]== 1 && k1 [6 ]== 3 && k1 [11 ]== 5 ); 154for (int i = 2 ;i < 6 ;++ i )CHECK (k1 [(size_t )i ]< -1 && k1 [(size_t )i ]!= 201 ); 155CHECK (k1 [2 ]!= k1 [3 ]);// (each offset its own id) 156 std::vector < ps::Span > other = sp ; 157other [1 ].hash = 333 ;// the same tokens, another second picture 158const auto k2 = ps::keyed (ids ,other ); 159size_t c = 0 ; 160while (c < k1 .size ()&& k1 [c ]== k2 [c ])++ c ; 161CHECK (c == 8 );// shared up to the swapped picture's span, not past it 162CHECK (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 164CHECK ((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 ; 168plan .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 ); 170CHECK (plan .runs .size ()== 1 ); 171if (!plan .runs .empty ()) { 172const auto & r = plan .runs [0 ]; 173CHECK (r .new_start == 110 && r .old_start == 60 && r .pad == 0 && r .at == 110 ); 174CHECK (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 177plan .runs = {{100 ,51 ,100 ,1 ,101 }, {300 ,400 ,300 ,0 ,301 }}; 178 ps::whole_spans (plan , {{95 ,60 ,3 ,0 ,0 }},32 ,64 ); 179CHECK (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 181plan .runs = {{100 ,51 ,100 ,1 ,101 }}; 182 ps::whole_spans (plan , {},32 ,64 ); 183CHECK (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 185const std::string pf = (dir /"slot-1.pics" ).string (); 186CHECK (ps::write (pf ,sp )); 187 std::vector < ps::Span > got ; 188CHECK (ps::read (pf ,12 ,got )&& got .size ()== 2 && got [1 ].start == 8 && got [1 ].len == 3 && got [1 ].hash == 222 && 189got [0 ].width == 28 ); 190CHECK (!ps::read (pf ,10 ,got )); 191patch (pf , fs::file_size (pf )- 1 ,0x00 ); 192CHECK (!ps::read (pf ,12 ,got )); 193 fs::resize_file (pf ,20 ); 194CHECK (!ps::read (pf ,12 ,got )); 195CHECK (ps::read ((dir /"none.pics" ).string (),12 ,got )&& got .empty ()); 196} 197 198int main () { 199const 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 ); 203meta_tests (dir ); 204pick_tests (); 205picspans_tests (dir ); 206 fs::remove_all (dir ,ec ); 207 std::printf (fails ?"FAILED: %d\n" :"slot_test: all passed\n" ,fails ); 208return fails ?1 :0 ; 209}