okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
1306424be
main
1// #4590: which tokens the repeat / frequency / presence penalties count. Header-only, CPU, no model. 2// 3// Default: the current reply only. The prompt (the new task's digits, a quoted earlier answer, earlier assistant turns) 4// is not in the window; the last prompt token, which the first decode step feeds in, is. STRATA_PENALTY_PROMPT=1 is the 5// old window: the last penalty_last_n tokens of prompt + reply. 6#include "strata/kernels/sampler.hpp" 7 8#include <cstdio> 9#include <cstdlib> 10#include <vector> 11 12using strata::kernels::penalty_counts_prompt ; 13using strata::kernels::penalty_rows ; 14using strata::kernels::penalty_window_from ; 15 16namespace { 17int checks = 0 ; 18void check (bool ok ,const char * label ) { 19++ checks ; 20if (!ok ) { std::fprintf (stderr ,"FAIL: %s\n" ,label ); std::exit (1 ); } 21} 22 23constexpr int kH = 64 ;// penalty_last_n 64 (the server's default window) 24constexpr int32_t kTaskTok = 12 ;// a token of the new task ("12"), in the last 64 prompt tokens 25constexpr int32_t kOldAnswer = 7777 ;// a token of an earlier assistant turn, quoted near the end of the prompt 26 27// A request's prompt: filler, an earlier assistant answer (kOldAnswer tokens), then the new task (kTaskTok), then the 28// assistant header whose last token is the fed-back head x. 29std::vector < int32_t > prompt () { 30 std::vector < int32_t > ids ; 31for (int i = 0 ;i < 300 ;++ i )ids .push_back (1000 + i ); 32for (int i = 0 ;i < 20 ;++ i )ids .push_back (kOldAnswer + (i %3 )); 33for (int i = 0 ;i < 10 ;++ i )ids .push_back (kTaskTok ); 34for (int i = 0 ;i < 4 ;++ i )ids .push_back (50 + i ); 35return ids ; 36} 37 38// The row the engine stages for the pick after `reply` has written its tokens: the tokens `consumed` holds when decode 39// began (the prompt without its last token) followed by the reply, read from penalty_window_from(). 40std::vector < int32_t > row_after (const std::vector < int32_t >& ids ,const std::vector < int32_t >& reply ,bool counts_prompt ) { 41 std::vector < int32_t > consumed (ids .begin (),ids .end ()- 1 ); 42const size_t from = penalty_window_from (consumed .size (),counts_prompt ); 43const int32_t x = ids .back (); 44// window = [x, reply...]: the single row of the last one is "the pick after the last reply token" 45 std::vector < int32_t > window = {x }; 46window .insert (window .end (),reply .begin (),reply .end ()); 47const int T = (int )window .size (); 48 std::vector < int32_t > rows ((size_t )T * kH ); 49penalty_rows (consumed .data ()+ from , (int64_t ) (consumed .size ()- from ),window .data (),T ,kH ,rows .data ()); 50return std::vector < int32_t > (rows .end ()- kH ,rows .end ());// the last row 51} 52 53int count_of (const std::vector < int32_t >& row ,int32_t tok ) { 54int c = 0 ; 55for (int32_t t :row )c += t == tok ; 56return c ; 57} 58int filled (const std::vector < int32_t >& row ) { 59int c = 0 ; 60for (int32_t t :row )c += t >=0 ; 61return c ; 62} 63}// namespace 64 65int main () { 66// ---- the switch: unset, empty, 0 and anything but "1" is the default (reply only) 67check (!penalty_counts_prompt (nullptr ),"env unset: reply only" ); 68check (!penalty_counts_prompt ("" ),"env empty: reply only" ); 69check (!penalty_counts_prompt ("0" ),"env 0 (the old opt-in value): reply only" ); 70check (!penalty_counts_prompt ("2" ),"env 2: reply only" ); 71check (penalty_counts_prompt ("1" ),"env 1: the old window" ); 72 73const std::vector < int32_t > ids = prompt (); 74const int32_t x = ids .back (); 75 76// ---- first pick (nothing written yet) 77 { 78const std::vector < int32_t > r = row_after (ids , {}, false); 79check (filled (r )== 1 && r [kH - 1 ]== x ,"default: the first pick counts only the fed-back head x" ); 80check (count_of (r ,kTaskTok )== 0 && count_of (r ,kOldAnswer )== 0 ,"default: no prompt task / earlier-answer token" ); 81const std::vector < int32_t > o = row_after (ids , {}, true); 82check (filled (o )== kH && o [kH - 1 ]== x ,"opt-out: a full window of prompt tokens ends in x" ); 83check (count_of (o ,kTaskTok )== 10 ,"opt-out: the new task's token is in the window (the old behaviour)" ); 84 } 85 86// ---- mid reply: the reply's own tokens are counted in both modes, a loop still gets penalised 87 { 88const std::vector < int32_t > reply = {900 ,901 ,900 ,902 ,900 }; 89const std::vector < int32_t > r = row_after (ids ,reply , false); 90check (filled (r )== 1 + (int )reply .size (),"default: the window is x + the reply" ); 91check (count_of (r ,900 )== 3 && count_of (r ,901 )== 1 && count_of (r ,902 )== 1 ,"default: reply tokens counted" ); 92check (count_of (r ,kTaskTok )== 0 && count_of (r ,kOldAnswer )== 0 ,"default: still no prompt token mid reply" ); 93const std::vector < int32_t > o = row_after (ids ,reply , true); 94check (count_of (o ,900 )== 3 && count_of (o ,kTaskTok )> 0 ,"opt-out: reply and prompt tokens both counted" ); 95 } 96 97// ---- a long reply: the window slides over the reply alone, the prompt never comes back 98 { 99 std::vector < int32_t > reply ; 100for (int i = 0 ;i < 200 ;++ i )reply .push_back (2000 + (i %7 )); 101const std::vector < int32_t > r = row_after (ids ,reply , false); 102check (filled (r )== kH ,"default: a reply longer than the window fills it" ); 103bool prompt_token = false; 104for (int32_t t :r )prompt_token |= (t >=0 && t < 2000 ); 105check (!prompt_token ,"default: after 63 reply tokens no prompt token (not even x) is in the window" ); 106const std::vector < int32_t > o = row_after (ids ,reply , true); 107check (filled (o )== kH && count_of (o ,kTaskTok )== 0 ,"opt-out: the same long reply has pushed the prompt out too" ); 108 } 109 110// ---- a restored conversation: the session comes back from the conversation cache or a saved slot with its state, but 111// the history is built from the request's ids and the tokens written since, so it is the same as a cold read of the 112// same ids. A second turn that quotes the first answer in the prompt does not carry the first reply's window over. 113 { 114const std::vector < int32_t > cold = row_after (ids , {900 ,901 }, false); 115const std::vector < int32_t > restored = row_after (ids , {900 ,901 }, false); 116check (cold == restored ,"restored session: same window as a cold read of the same ids" ); 117 std::vector < int32_t > next = ids ;// turn 2: the prompt is turn 1's prompt + its reply + a new task 118next .insert (next .end (), {900 ,901 ,902 ,99 }); 119const std::vector < int32_t > r2 = row_after (next , {}, false); 120check (filled (r2 )== 1 && r2 [kH - 1 ]== 99 ,"turn 2 default: turn 1's reply tokens in the prompt are not counted" ); 121 } 122 123 std::printf ("penalty_window_test: %d checks passed\n" ,checks ); 124return 0 ; 125}