okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
6ca60e9a0
main
1// src/program/conv_cache_test.cpp - the conversation cache's retention policy: the shared root survives, 2// the leaves rotate by least recent use. 3// 4// The scenarios, in the serve loop's terms (generate.cpp): checkpoints are a prefix chain - stamps[0] is 5// the root, the deepest point every request so far has shared (the end of the system prompt, in practice). 6// A stamp advances on creation, when a new checkpoint lands on an existing length, and when a request 7// mounts through the checkpoint. 8// 9// 1. one conversation over its budget: the oldest leaf leaves - with fresh stamps in creation order this 10// is exactly the previous first-in-first-out behaviour; 11// 2. the root survives arbitrarily many newer checkpoints however old its stamp is; 12// 3. a mount advances a checkpoint's stamp and the rotation then takes a different leaf; 13// 4. a one-slot budget has no room for root and leaf, so the pin is off and the oldest leaves (the 14// newest point stays, as before); 15// 5. the policy is pure: the same stamps, the same victim. 16#include "strata/program/conv_cache.hpp" 17 18#include <cstdint> 19#include <cstdio> 20#include <vector> 21 22using strata::program::conv_cache::eviction_victim ; 23using strata::program::conv_cache::restore_fits ; 24using strata::program::conv_cache::spread_victim ; 25 26namespace { 27int g_fail = 0 ; 28void check (bool ok ,const char * what ) { 29 std::printf (" %-66s %s\n" ,what ,ok ?"ok" :"FAIL" ); 30if (!ok )++ g_fail ; 31} 32}// namespace 33 34// a conversation of `turns` turns of `turn` tokens each after a `root`-token system prompt, a checkpoint at every turn's 35// end and at most `cap` kept; returns how many tokens a request whose prompt differs `back` tokens before the end reuses 36int64_t reuse_after_edit (bool spread ,int64_t root ,int64_t turn ,int turns ,int64_t cap ,int64_t back ) { 37 std::vector < int64_t > lens = {root }; 38 std::vector < uint64_t > stamps = {1 }; 39for (int t = 1 ;t <=turns ;++ t ) { 40lens .push_back (root + t * turn ); 41stamps .push_back (uint64_t (t + 1 )); 42while ((int64_t )lens .size ()> cap ) { 43const size_t v = spread ?spread_victim (lens .data (),stamps .data (),lens .size (),cap ,nullptr ,nullptr ) 44 :eviction_victim (stamps .data (),stamps .size (),cap ); 45lens .erase (lens .begin ()+ (std::ptrdiff_t )v ); 46stamps .erase (stamps .begin ()+ (std::ptrdiff_t )v ); 47 } 48 } 49const int64_t edit = lens .back ()- back ; 50int64_t reuse = 0 ; 51for (int64_t l :lens ) 52if (l <=edit )reuse = l ; 53return reuse ; 54} 55 56int main () { 57 std::printf ("conv_cache_test\n" ); 58 {// --prompt-cache-spread: 200 turns of 1,000 tokens after a 9,525-token system prompt, 6 checkpoints 59const int64_t end = 9525 + 200 * 1000 ; 60const int64_t lru6 = reuse_after_edit (false,9525 ,1000 ,200 ,6 ,6000 ); 61const int64_t spread6 = reuse_after_edit (true,9525 ,1000 ,200 ,6 ,6000 ); 62 std::printf (" an edit 6K before the end: least recent use reuses %lld, spread %lld of %lld\n" , (long long )lru6 , 63 (long long )spread6 , (long long )end ); 64check (lru6 == 9525 ,"least recent use: an edit 6K back falls through to the root" ); 65check (end - spread6 < 4 * 6000 ,"spread: an edit 6K back reads less than 24K again" ); 66const int64_t spread42 = reuse_after_edit (true,9525 ,1000 ,200 ,6 ,42000 ); 67check (end - spread42 < 4 * 42000 ,"spread: an edit 42K back reads less than 168K again" ); 68check (reuse_after_edit (true,9525 ,1000 ,200 ,6 ,500 )== end - 1000 ,"spread: the turn before is still kept" ); 69 } 70 { 71const std::vector < int64_t > lens = {100 ,200 ,300 ,400 }; 72const std::vector < uint64_t > stamps = {1 ,2 ,3 ,4 }; 73const bool pinned []= {false, true, true, false}; 74check (spread_victim (lens .data (),stamps .data (),4 ,3 ,nullptr ,pinned )== 75eviction_victim (stamps .data (),4 ,3 ,nullptr ,pinned ), 76"spread: every candidate pinned - the plain policy" ); 77check (spread_victim (lens .data (),stamps .data (),2 ,1 ,nullptr ,nullptr )== 0 , 78"spread: two items, one slot - the plain policy" ); 79 } 80 { 81const std::vector < uint64_t > stamps = {1 ,2 ,3 ,4 ,5 ,6 ,7 };// chat A: root + six turns, one over 82check (eviction_victim (stamps .data (),stamps .size (),6 )== 1 , 83"over budget with fresh stamps: the oldest leaf leaves (the FIFO it was)" ); 84 } 85 { 86// chat B mounts through the root long after it was created; its stamp stays ancient 87const std::vector < uint64_t > stamps = {1 ,100 ,101 ,102 ,103 ,104 ,105 }; 88const size_t v = eviction_victim (stamps .data (),stamps .size (),6 ); 89check (v == 1 ,"the root's stale stamp never makes it the victim" ); 90check (v != 0 ,"the root - the shared prefix - is never the victim" ); 91 } 92 { 93// the same chain after mounting checkpoint 1: its stamp jumps, the rotation moves to the next leaf 94const std::vector < uint64_t > stamped = {1 ,8 ,3 ,4 ,5 ,6 ,7 }; 95check (eviction_victim (stamped .data (),stamped .size (),6 )== 2 , 96"after a mount, the rotation takes the next least recently used leaf" ); 97 } 98 { 99const std::vector < uint64_t > stamps = {1 ,90 ,91 ,92 }; 100check (eviction_victim (stamps .data (),stamps .size (),2 )== 1 , 101"a two-slot budget keeps root + newest and drops the leaf in between" ); 102check (eviction_victim (stamps .data (),stamps .size (),1 )== 0 , 103"a one-slot budget has no pin: the oldest leaves, the newest point stays" ); 104 } 105 { 106const std::vector < uint64_t > stamps = {7 ,3 ,3 ,5 }; 107const size_t a = eviction_victim (stamps .data (),stamps .size (),4 ); 108const size_t b = eviction_victim (stamps .data (),stamps .size (),4 ); 109check (a == 1 && b == 1 ,"ties among equally stale leaves: the earlier index, deterministically" ); 110 } 111 {// --prompt-cache-tail: the tail checkpoint goes first; never the root, never the newest item 112const std::vector < uint64_t > st = {1 ,2 ,3 ,4 ,5 ,6 ,7 }; 113const bool t2 [7 ]= {false, false, false, true, false, false, false}; 114check (eviction_victim (st .data (),st .size (),6 ,t2 )== 3 ,"a stale tail checkpoint leaves before the oldest leaf" ); 115const bool t3 [7 ]= {false, false, false, false, false, false, true}; 116check (eviction_victim (st .data (),st .size (),6 ,t3 )== 1 ,"the newest item, even a tail, is not the victim" ); 117const bool t4 [7 ]= {true, false, false, false, false, false, false}; 118check (eviction_victim (st .data (),st .size (),6 ,t4 )== 1 ,"the root is never the victim, tail flag or not" ); 119check (eviction_victim (st .data (),st .size (),6 ,nullptr )== 1 ,"no tail flags: the plain policy" ); 120 } 121 {// pin=N: a pinned shared prefix is never the victim, however stale its stamp 122const std::vector < uint64_t > st = {1 ,2 ,30 ,4 ,5 ,6 ,7 }; 123const bool pin1 [7 ]= {false, true, false, false, false, false, false}; 124check (eviction_victim (st .data (),st .size (),6 ,nullptr ,pin1 )== 3 , 125"the oldest checkpoint is pinned: the next least recently used leaves" ); 126check (eviction_victim (st .data (),st .size (),6 ,nullptr ,nullptr )== 1 ,"no pin flags: the plain policy" ); 127const bool pin2 [7 ]= {false, true, false, true, false, false, false}; 128check (eviction_victim (st .data (),st .size (),6 ,nullptr ,pin2 )== 4 ,"two pins: the third oldest leaves" ); 129const bool tail3 [7 ]= {false, false, false, true, false, false, false}; 130check (eviction_victim (st .data (),st .size (),6 ,tail3 ,pin2 )== 4 ,"a pinned item is not taken as the tail either" ); 131const bool all [4 ]= {false, true, true, true}; 132const std::vector < uint64_t > s4 = {1 ,9 ,3 ,5 }; 133check (eviction_victim (s4 .data (),s4 .size (),4 ,nullptr ,all )== 2 ,"everything pinned: the oldest pin leaves, never the root" ); 134check (eviction_victim (s4 .data (),s4 .size (),1 ,nullptr ,all )== 0 ,"a one-slot budget keeps no pin: the oldest leaves" ); 135 } 136 {// #2409: a restore is refused only for a card that must create its copy stream and has no room for it 137constexpr uint64_t need = strata::program::conv_cache::kRestoreFirstUseMib <<20 ; 138const uint64_t loaded []= {300ull <<20 ,104ull <<20 };// a loaded card: far under --vram-reserve-mib 139const uint8_t ready []= {1 ,1 }; 140check (restore_fits (loaded ,ready ,2 ,need ),"cards with their stream already: any amount free admits (the #2409 review's case)" ); 141const uint8_t later_new []= {1 ,0 }; 142check (restore_fits (loaded ,later_new ,2 ,need ),"a card that must create its stream, with 104 MiB free: admitted" ); 143const uint64_t full []= {300ull <<20 ,20ull <<20 }; 144check (!restore_fits (full ,later_new ,2 ,need ),"a card that must create its stream, with 20 MiB free: reread instead" ); 145check (restore_fits (full ,ready ,2 ,need ),"the same full card with its stream: admitted" ); 146const uint64_t edge []= {need ,need - 1 }; 147const uint8_t none []= {0 ,0 }; 148check (!restore_fits (edge ,none ,2 ,need ),"one byte under the need refuses" ); 149check (restore_fits (edge ,none ,1 ,need ),"exactly the need admits" ); 150check (restore_fits (nullptr ,none ,2 ,need )&& restore_fits (full ,nullptr ,2 ,need )&& restore_fits (full ,none ,0 ,need )&& 151restore_fits (full ,none ,2 ,0 ), 152"no sample, no cards, no need: do not refuse" ); 153 } 154 std::printf (g_fail ?"FAIL\n" :"PASS\n" ); 155return g_fail ?1 :0 ; 156}