okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
f7e24459b
main
1// src/core/dflash2_ring_test.cpp - the drafter's ring log and rebuild (Dflash2Drafter::begin_request), against the ring an uninterrupted update sequence left. 2// 3// dflash2_ring_test PACK [device] 4// 5// Context rows are fed with deterministic pseudo-random taps (a function of the position, so any lineage can be refed), for lineage A up to a point P, the ring is 6// copied out, then the update goes on far enough to wrap the ring (and, in the other scenarios, another lineage takes over). begin_request must then rebuild the 7// window the first draft reads, [n - 2047, resume), and the K/V bytes of every rebuilt slot must equal, bit for bit, those of the copy taken at P: 8// 1 rewind : A fed to P + 600, rewound to P (a repeated or rewound prefix) -> rebuild from the live log 9// 2 parked : A fed to P, B fed (different tokens and taps), A resumed at P -> swap from the stash, rebuild 10// 3 branch : A fed to P + 700, a request branches at P - 300 with other tokens, then A's old branch resumes at P -> stash copy (shared chunks, copy on write) 11// 4 off : STRATA_DF2_RING_LOG=0 (env set before the run): the same rewind finds the window blank (the old behaviour), reported not asserted 12// 5 evicted : a stash too small to keep A: the resume is cold (missing rows reported, *cold set) 13// Exit 0 when every assertion holds. 14#include "strata/core/dflash2.hpp" 15 16#include <cuda_runtime.h> 17 18#include <cmath> 19#include <cstdint> 20#include <cstdio> 21#include <cstdlib> 22#include <cstring> 23#include <string> 24#include <vector> 25 26using strata::core::Dflash2Drafter ; 27 28namespace { 29int g_fail = 0 ; 30void check (bool ok ,const char * what ) { 31 std::printf (" %-100s %s\n" ,what ,ok ?"ok" :"FAIL" ); 32if (!ok )++ g_fail ; 33} 34 35constexpr int kTap = 12800 ;// 5 taps x 2560 36 37float tap_value (uint32_t lineage ,int64_t pos ,int i ) { 38uint32_t x = (uint32_t )pos * 2654435761u ^ (uint32_t )i * 40503u ^lineage * 97531u ; 39x ^=x >>15 ;x *=2246822519u ;x ^=x >>13 ;x *=3266489917u ;x ^=x >>16 ; 40return ((float ) (x & 0xFFFF ) /32768.0f - 1.0f )* (1.0f + 0.25f * (float ) (i %7 )); 41} 42 43// feeds the rows [from, to) of `lineage` (tokens: token_of(lineage, pos)) as context updates, 256 rows a call, and notes the tokens 44int32_t token_of (uint32_t lineage ,int64_t pos ) {return (int32_t ) (1000 + ((uint32_t )pos * 7919u + lineage * 104729u ) %50000u ); } 45 46bool feed (Dflash2Drafter & d ,float * dev ,uint32_t lineage ,int64_t from ,int64_t to , std::string & err ) { 47 std::vector < float > h ((size_t )256 * kTap ); 48for (int64_t p0 = from ;p0 < to ;p0 += 256 ) { 49const int64_t n = std::min < int64_t > (256 ,to - p0 ); 50for (int64_t r = 0 ;r < n ;++ r ) 51for (int i = 0 ;i < kTap ;++ i )h [(size_t )r * kTap + (size_t )i ]= tap_value (lineage ,p0 + r ,i ); 52if (cudaMemcpy (dev ,h .data (), (size_t )n * kTap * 4 ,cudaMemcpyHostToDevice )!= cudaSuccess ) {err = "upload" ;return false; } 53if (!d .context (dev ,n ,p0 ,err ))return false; 54 std::vector < int32_t > tk ((size_t )n ); 55for (int64_t r = 0 ;r < n ;++ r )tk [(size_t )r ]= token_of (lineage ,p0 + r ); 56d .note_rows (tk .data (),n ,p0 ); 57 } 58return true; 59} 60 61std::vector < int32_t > ids_of (uint32_t lineage ,int64_t n ) { 62 std::vector < int32_t > v ((size_t )n ); 63for (int64_t p = 0 ;p < n ;++ p )v [(size_t )p ]= token_of (lineage ,p ); 64return v ; 65} 66 67// bytes of slot s of layer l: K and V, 8 heads x 128 floats 68bool slot_equal (const std::vector < float >& a ,const std::vector < float >& b ,int slot ) { 69const size_t per = (size_t )Dflash2Drafter ::kRing * 8 * 128 ; 70for (int l = 0 ;l < 5 ;++ l ) 71for (int kv = 0 ;kv < 2 ;++ kv ) 72for (int h = 0 ;h < 8 ;++ h ) { 73const size_t off = ((size_t )l * 2 + (size_t )kv )* per + ((size_t )h * Dflash2Drafter ::kRing + (size_t )slot )* 128 ; 74if (std::memcmp (& a [off ],& b [off ],128 * 4 )!= 0 )return false; 75 } 76return true; 77} 78 79// every slot of the positions [lo, hi) holds the row `ref` has (same bytes), tagged with the drafter's current epoch and that position 80int count_bad (Dflash2Drafter & d ,const std::vector < float >& ref ,int64_t lo ,int64_t hi ,int * wrong_tag ) { 81 std::vector < float > now ; 82 std::vector < int64_t > tags ; 83 std::string err ; 84if (!d .debug_ring (now ,tags ,err )) { std::printf ("debug_ring: %s\n" ,err .c_str ());return 1 <<20 ; } 85int bad = 0 ; 86* wrong_tag = 0 ; 87for (int64_t p = lo ;p < hi ;++ p ) { 88const int slot = (int ) (p %Dflash2Drafter ::kRing ); 89if ((tags [(size_t )slot ]& 0xFFFFFFFFll )!= p || tags [(size_t )slot ]< 0 ) {++* wrong_tag ;++ bad ;continue ; } 90if (!slot_equal (now ,ref ,slot ))++ bad ; 91 } 92return bad ; 93} 94}// namespace 95 96int main (int argc ,char ** argv ) { 97if (argc < 2 ) { std::printf ("usage: dflash2_ring_test PACK [device]\n" );return 2 ; } 98const int device = argc > 2 ? std::atoi (argv [2 ]) :0 ; 99const bool log_off_run = std::getenv ("STRATA_DF2_RING_LOG" )!= nullptr && std::getenv ("STRATA_DF2_RING_LOG" )[0 ]== '0' ; 100cudaSetDevice (device ); 101 std::string err ; 102const int64_t P = 4227 ,N = 4232 ;// the resumed prefix and the prompt (the agent prompt's numbers) 103float * dev = nullptr ; 104cudaMalloc ((void ** )& dev , (size_t )256 * kTap * 4 ); 105 std::printf ("dflash2_ring_test%s\n" ,log_off_run ?" (STRATA_DF2_RING_LOG=0)" :"" ); 106 107auto fresh = [& ](Dflash2Drafter & d )-> bool { 108if (!d .load (argv [1 ],device ,err )) { std::printf ("load: %s\n" ,err .c_str ());return false; } 109return true; 110 }; 111 std::vector < float > ref ; 112 std::vector < int64_t > reftags ; 113 {// ---- 1 rewind 114 std::printf ("1 rewind (fed to %lld + 600, rewound to %lld, prompt %lld)\n" , (long long )P , (long long )P , (long long )N ); 115Dflash2Drafter d ; 116if (!fresh (d ))return 2 ; 117if (!feed (d ,dev ,1 ,0 ,P ,err )) { std::printf ("feed: %s\n" ,err .c_str ());return 2 ; } 118if (!d .debug_ring (ref ,reftags ,err ))return 2 ; 119if (!feed (d ,dev ,1 ,P ,P + 600 ,err ))return 2 ; 120const std::vector < int32_t > ids = ids_of (1 ,N ); 121bool cold = false; 122d .begin_request (ids .data (),N ,P ,& cold ); 123int wt = 0 ; 124const int bad = count_bad (d ,ref ,N - 2047 ,P ,& wt ); 125 std::printf (" rebuilt %lld rows in %.1f ms, missing %lld, slots wrong %d (wrong tag %d), cold %d\n" , (long long )d .last_rebuilt ,d .last_ms , (long long )d .last_missing ,bad ,wt , (int )cold ); 126if (log_off_run ) { 127check (d .last_rebuilt == 0 ,"log off: nothing is rebuilt (the pre-log behaviour)" ); 128check (bad > 0 ,"log off: the overwritten part of the window is blank (the defect this fixes)" ); 129 }else { 130check (bad == 0 ,"every slot of [n - 2047, resume) holds its row, bitwise the original update's" ); 131check (d .last_rebuilt == (P + 600 - Dflash2Drafter ::kRing )- (N - 2047 ),"only the overwritten slots were rebuilt (the others were already right)" ); 132check (!cold ,"not cold" ); 133 } 134// the ring must go on working: feed the prompt's remaining rows and a decode row, no stray tags 135if (!feed (d ,dev ,1 ,P ,N ,err ))return 2 ; 136 } 137if (log_off_run ) { 138 std::printf (g_fail == 0 ?"dflash2_ring_test: ok\n" :"dflash2_ring_test: FAILED\n" ); 139return g_fail == 0 ?0 :1 ; 140 } 141 {// ---- 2 parked 142 std::printf ("2 parked (A to %lld, B to 3000, A resumed at %lld)\n" , (long long )P , (long long )P ); 143Dflash2Drafter d ; 144if (!fresh (d ))return 2 ; 145if (!feed (d ,dev ,1 ,0 ,P ,err ))return 2 ; 146 {// B takes over properly: a request for B from scratch 147const std::vector < int32_t > idsB = ids_of (2 ,3000 ); 148bool cold = false; 149d .begin_request (idsB .data (),3000 ,0 ,& cold ); 150if (!feed (d ,dev ,2 ,0 ,3000 ,err ))return 2 ; 151 } 152const std::vector < int32_t > ids = ids_of (1 ,N ); 153bool cold = true; 154d .begin_request (ids .data (),N ,P ,& cold ); 155int wt = 0 ; 156const int bad = count_bad (d ,ref ,N - 2047 ,P ,& wt ); 157 std::printf (" stash hits %lld, rebuilt %lld rows in %.1f ms (swap %d), slots wrong %d (wrong tag %d), stash %.1f MiB\n" , (long long )d .ring_stats .stash_hits , 158 (long long )d .last_rebuilt ,d .last_ms , (int )d .last_swapped ,bad ,wt , (double )d .ring_stash_bytes () /1048576.0 ); 159check (d .ring_stats .stash_hits == 1 && d .last_swapped ,"the parked lineage came back from the stash" ); 160check (bad == 0 ,"every slot of [n - 2047, resume) holds its row, bitwise the original update's" ); 161check (d .last_rebuilt == P - (N - 2047 ),"the whole window below resume was rebuilt (a new epoch)" ); 162check (!cold ,"not cold" ); 163 } 164 {// ---- 3 branch 165 std::printf ("3 branch (A to %lld, a request at %lld with other tokens, the old branch resumed at %lld)\n" , (long long ) (P + 700 ), (long long ) (P - 300 ), (long long )P ); 166Dflash2Drafter d ; 167if (!fresh (d ))return 2 ; 168if (!feed (d ,dev ,1 ,0 ,P + 700 ,err ))return 2 ; 169// reference at P: the ring of an uninterrupted feed to P is `ref` (same lineage, same taps) 170 {// a request that shares A up to P - 300 and then goes its own way (lineage 3 after that point) 171 std::vector < int32_t > ids = ids_of (1 ,P - 300 ); 172for (int64_t p = P - 300 ;p < P - 300 + 400 ;++ p )ids .push_back (token_of (3 ,p )); 173bool cold = false; 174d .begin_request (ids .data (), (int64_t )ids .size (),P - 300 ,& cold ); 175if (!feed (d ,dev ,3 ,P - 300 ,P - 300 + 400 ,err ))return 2 ;// (rows after the branch point: lineage 3's taps) 176 } 177const std::vector < int32_t > ids = ids_of (1 ,N ); 178bool cold = true; 179d .begin_request (ids .data (),N ,P ,& cold ); 180int wt = 0 ; 181const int bad = count_bad (d ,ref ,N - 2047 ,P ,& wt ); 182 std::printf (" stash hits %lld, rebuilt %lld rows, missing %lld, slots wrong %d (wrong tag %d)\n" , (long long )d .ring_stats .stash_hits , (long long )d .last_rebuilt , 183 (long long )d .last_missing ,bad ,wt ); 184check (d .ring_stats .stash_hits >=1 ,"the old branch was kept (copy on write) and came back" ); 185check (bad == 0 ,"the old branch's rows are bitwise the original update's, not the new branch's" ); 186check (!cold ,"not cold" ); 187 } 188 {// ---- 5 evicted 189 std::printf ("5 evicted (stash budget 0)\n" ); 190setenv ("STRATA_DF2_LOG_MIB" ,"0" ,1 ); 191Dflash2Drafter d ; 192if (!fresh (d ))return 2 ; 193unsetenv ("STRATA_DF2_LOG_MIB" ); 194if (!feed (d ,dev ,1 ,0 ,P ,err ))return 2 ; 195const std::vector < int32_t > idsB = ids_of (2 ,3000 ); 196bool cold = false; 197d .begin_request (idsB .data (),3000 ,0 ,& cold ); 198if (!feed (d ,dev ,2 ,0 ,3000 ,err ))return 2 ; 199const std::vector < int32_t > ids = ids_of (1 ,N ); 200cold = false; 201d .begin_request (ids .data (),N ,P ,& cold ); 202 std::printf (" stash hits %lld, rebuilt %lld, missing %lld, cold %d\n" , (long long )d .ring_stats .stash_hits , (long long )d .last_rebuilt , (long long )d .last_missing , (int )cold ); 203check (cold && d .last_missing > 0 && d .last_rebuilt == 0 ,"no log of the lineage: cold, the missing rows are reported" ); 204 } 205 std::printf (g_fail == 0 ?"dflash2_ring_test: ok\n" :"dflash2_ring_test: FAILED\n" ); 206return g_fail == 0 ?0 :1 ; 207}