okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
fe57a0cb5
main
1// Batched windows (Engine::step) against one sequence at a time: each conversation's GEN greedy tokens alone (one row a 2// window, no drafts), then the conversations on lanes decoding together, one row per lane a window; each lane's tokens 3// must equal its conversation's alone. For 1, 2, 3 and 6 lanes (prompts reused round-robin): the window time and the 4// total tokens a second. With FNX_BATCH_DRAFTS (lookup or 1) the same again with drafts in the batched windows 5// (BatchDecoder: several rows a lane, each lane cut back to what it accepts, lanes leaving as they finish), greedy and 6// equal to alone; then a sampled run at 3 lanes, which must only run (its tokens differ by chance). bin/batch_test and 7// the lab's "batch" command. 8#pragma once 9#include <algorithm> 10#include <chrono> 11#include <cstdio> 12#include <random> 13#include <vector> 14 15#include "../src/batch.hpp" 16#include "../src/engine.hpp" 17#include "../src/session.hpp" 18 19inline int batch_check (const fnx::Model & m , fnx::Engine & e ,const fnx::Mtp & mtp ,int gen ,const std::vector < std::vector < int >>& prompts ) { 20e .mtp_attach (mtp ); 21const std::vector < int > eos {248044 ,248046 }; 22 fnx::Sampling greedy ; 23// alone: one row a window 24 std::vector < std::vector < int >> solo (prompts .size ()); 25 { 26 fnx::Session s (e ,4 ,7 ,eos ); 27s .speculate = false; 28for (size_t i = 0 ;i < prompts .size ();++ i ) { 29e .reset (); 30 fnx::GenStats st ; 31s .read (prompts [i ],st ); 32s .generate (gen ,greedy , [& ](int t ) {solo [i ].push_back (t ); },nullptr ,st ); 33 } 34 } 35e .reset (); 36 std::vector < int > lanes {0 }; 37while ((int )lanes .size ()< 6 )lanes .push_back (e .add_lane ()); 38// a lane's tokens against its conversation's alone: every token of the reference up to its first end-of-reply (past 39// it the plain batch keeps decoding), none missing: a lane that ends early or emits nothing does not match 40auto matches = [& ](const std::vector < int >& got ,const std::vector < int >& ref ) { 41size_t want = ref .size (); 42for (size_t j = 0 ;j < ref .size ();++ j ) 43if (ref [j ]== 248044 || ref [j ]== 248046 ) { 44want = j + 1 ; 45break ; 46 } 47return got .size () >=want && std::equal (ref .begin (),ref .begin ()+ (long )want ,got .begin ()); 48 }; 49bool ok = true; 50for (int n : {1 ,2 ,3 ,6 }) { 51 std::vector < int > use (lanes .begin (),lanes .begin ()+ n ),x (n ); 52 std::vector < std::vector < int >> got (n ); 53for (int r = 0 ;r < n ;++ r ) { 54e .use_lane (use [r ]); 55e .reset (); 56 fnx::Session s (e ,4 ,7 ,eos ,248045 ,0 ,use [r ]); 57 fnx::GenStats st ; 58s .read (prompts [r %prompts .size ()],st ); 59x [r ]= prompts [r %prompts .size ()].back (); 60 } 61double ms = 0 ; 62int windows = 0 ; 63while ((int )got [0 ].size ()< gen ) { 64const auto t0 = std::chrono::steady_clock::now (); 65const std::vector < int > o = e .step (use ,x ); 66ms += std::chrono::duration < double , std::milli > (std::chrono::steady_clock::now ()- t0 ).count (); 67++ windows ; 68for (int r = 0 ;r < n ;++ r ) { 69got [r ].push_back (o [r ]); 70x [r ]= o [r ]; 71 } 72 } 73int same = 0 ; 74for (int r = 0 ;r < n ;++ r )same += matches (got [r ],solo [r %prompts .size ()]); 75 std::printf ("%d lanes: %d windows, %.2f ms a window (first one captures its graph), %.1f tokens/s in all; %d of %d lanes match alone\n" , 76n ,windows ,ms /windows ,n * windows * 1000.0 /ms ,same ,n ); 77ok = ok && same == n ; 78 } 79 std::printf ("%s\n" ,ok ?"batched windows give each lane its tokens alone" :"MISMATCH" ); 80if (fnx::batch_drafts ()== fnx::BatchDrafts ::off )return ok ?0 :1 ; 81bool dok = true; 82 fnx::BatchDecoder dec (e ,4 ,15 ,eos , fnx::batch_drafts ()); 83// the lanes on their prompts, each lane's next row its prompt's last token 84auto start = [& ](int n ,const fnx::Sampling & s , std::vector < fnx::BatchLane >& bl ) { 85bl .assign ((size_t )n , fnx::BatchLane {}); 86for (int r = 0 ;r < n ;++ r ) { 87e .use_lane (lanes [(size_t )r ]); 88e .reset (); 89 fnx::Session sess (e ,4 ,7 ,eos ,248045 ,0 ,lanes [(size_t )r ]); 90 fnx::GenStats st ; 91const std::vector < int >& p = prompts [r %prompts .size ()]; 92sess .read (p ,st ); 93bl [(size_t )r ].start (lanes [(size_t )r ],s , std::mt19937_64 (1000 + r ),p ); 94 } 95 }; 96// windows until every lane has gen tokens or ended its reply: the tokens per lane, windows, rows, ms 97auto run = [& ](std::vector < fnx::BatchLane >& bl , std::vector < std::vector < int >>& got ,int & windows ,int & rows ,double & ms ) { 98const int n = (int )bl .size (); 99got .assign ((size_t )n , {}); 100 std::vector < bool > ended ((size_t )n , false); 101windows = rows = 0 ; 102ms = 0 ; 103for (;;) { 104 std::vector < fnx::BatchLane *> ls ; 105 std::vector < int > idx ,limit ; 106for (int r = 0 ;r < n ;++ r ) 107if (!ended [(size_t )r ]&& (int )got [(size_t )r ].size ()< gen ) { 108ls .push_back (& bl [(size_t )r ]); 109idx .push_back (r ); 110limit .push_back (gen - (int )got [(size_t )r ].size ()); 111 } 112if (ls .empty ())break ; 113 fnx::BatchDecoder ::fit (ls ); 114const auto t0 = std::chrono::steady_clock::now (); 115const auto out = dec .window (ls ,limit , [& ](int i ,int t ) {got [(size_t )idx [(size_t )i ]].push_back (t ); }); 116ms += std::chrono::duration < double , std::milli > (std::chrono::steady_clock::now ()- t0 ).count (); 117++ windows ; 118rows += dec .last_rows ; 119for (size_t i = 0 ;i < out .size ();++ i ) 120if (out [i ].eos )ended [(size_t )idx [i ]]= true; 121 } 122 }; 123const char * mode = fnx::batch_drafts ()== fnx::BatchDrafts ::lookup ?"lookup drafts" :"lookup drafts and the draft layer's chain" ; 124for (int n : {1 ,2 ,3 ,6 }) { 125 std::vector < fnx::BatchLane > bl ; 126start (n ,greedy ,bl ); 127 std::vector < std::vector < int >> got ; 128int windows = 0 ,rows = 0 ; 129double ms = 0 ; 130run (bl ,got ,windows ,rows ,ms ); 131int same = 0 ; 132size_t tokens = 0 ; 133long offered = 0 ,accepted = 0 ; 134for (int r = 0 ;r < n ;++ r ) { 135same += matches (got [(size_t )r ],solo [r %prompts .size ()]); 136tokens += got [(size_t )r ].size (); 137offered += bl [(size_t )r ].offered ; 138accepted += bl [(size_t )r ].accepted ; 139 } 140 std::printf ("%d lanes with %s: %d windows, %.1f rows and %.2f ms a window, %.1f tokens/s in all, drafts %ld of %ld; %d of %d " 141"lanes match alone\n" , 142n ,mode ,windows , (double )rows /windows ,ms /windows ,tokens * 1000.0 /ms ,accepted ,offered ,same ,n ); 143dok = dok && same == n ; 144 } 145 {// sampled (prod's values): it must run; nothing to compare 146 fnx::Sampling s ; 147s .temperature = 0.6f ; 148s .top_p = 0.95f ; 149s .pen_present = 1.0f ; 150 std::vector < fnx::BatchLane > bl ; 151start (3 ,s ,bl ); 152 std::vector < std::vector < int >> got ; 153int windows = 0 ,rows = 0 ; 154double ms = 0 ; 155run (bl ,got ,windows ,rows ,ms ); 156bool in_vocab = true; 157size_t tokens = 0 ; 158for (const auto & g :got ) { 159tokens += g .size (); 160for (int t :g )in_vocab = in_vocab && t >=0 && t < m .vocab ; 161 } 162 std::printf ("3 lanes sampled (temperature 0.6, top_p 0.95, presence 1.0): %d windows, %.1f rows a window, %.1f tokens/s in all%s\n" , 163windows , (double )rows /windows ,tokens * 1000.0 /ms ,in_vocab ?"" :": A TOKEN OUT OF THE VOCABULARY" ); 164dok = dok && in_vocab ; 165 } 166for (int i :lanes ) { 167e .use_lane (i ); 168e .reset (); 169 } 170 std::printf ("%s\n" ,dok ?"batched windows with drafts give each lane its tokens alone" :"MISMATCH with drafts" ); 171ok = ok && dok ; 172return ok ?0 :1 ; 173}