okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
1306424be
main
1// The qwen35 host sampler's penalties (include/strata/qwen35/sampling.hpp), CPU only, no model. 2// #3911: a history token outside a short logit row (--draft-vocab N) must not write past the row. 3#include "strata/qwen35/sampling.hpp" 4 5#include <cstdio> 6#include <cstdlib> 7#include <cstring> 8#include <map> 9 10using strata::qwen35::Sampling ; 11using strata::qwen35::apply_penalties ; 12 13namespace { 14int checks = 0 ; 15void check (bool ok ,const char * label ) { 16++ checks ; 17if (!ok ) { std::fprintf (stderr ,"FAIL: %s\n" ,label ); std::exit (1 ); } 18} 19 20// The loop as it was before the guard (origin/main of the time), for the in-range comparison. 21void old_penalties (std::vector < float >& lg ,const Sampling & s ,const std::vector < int32_t >& hist ) { 22if (s .last_n != 0 && (s .repeat != 1.f || s .freq != 0.f || s .present != 0.f )) { 23 std::map < int ,int > cnt ; 24const size_t from = s .last_n < 0 || (size_t )s .last_n >=hist .size () ?0 :hist .size ()- (size_t )s .last_n ; 25for (size_t i = from ;i < hist .size ();++ i )++ cnt [hist [i ]]; 26for (const auto & [id ,c ] :cnt ) { 27float & v = lg [(size_t )id ]; 28if (s .repeat != 1.f )v = v > 0 ?v /s .repeat :v * s .repeat ; 29v -= (float )c * s .freq + s .present ; 30 } 31 } 32} 33 34bool same_bits (const std::vector < float >& a ,const std::vector < float >& b ) { 35return a .size ()== b .size ()&& std::memcmp (a .data (),b .data (),a .size ()* sizeof (float ))== 0 ; 36} 37}// namespace 38 39int main () { 40Sampling s ; 41s .repeat = 1.2f ;s .freq = 0.1f ;s .present = 0.05f ;s .last_n = -1 ; 42 43// a short row (a 64-token draft vocabulary) and a history with tokens inside it, past it and negative. The row's 44// storage past its size is filled with a marker first: a write past the row would change the marker. 45 { 46 std::vector < float > row (256 ,7.f ); 47row .resize (64 ); 48for (int i = 0 ;i < 64 ;++ i )row [(size_t )i ]= (float )i - 20.f ; 49const std::vector < float > before (row .begin (),row .end ()); 50const std::vector < int32_t > hist = {3 ,100 ,3 ,63 ,64 ,65 ,255 ,100000 ,-1 ,7 }; 51apply_penalties (row ,s ,hist ); 52for (int i = 64 ;i < 256 ;++ i )check (row .data ()[i ]== 7.f ,"nothing is written past the row" ); 53check (row .size ()== 64 ,"the row keeps its size" ); 54// tokens 3 (twice), 63 and 7 are in the row and are penalised; the others are skipped 55 std::vector < float > want = before ; 56for (int id : {3 ,7 ,63 }) { 57const int c = id == 3 ?2 :1 ; 58float & v = want [(size_t )id ]; 59v = v > 0 ?v /s .repeat :v * s .repeat ; 60v -= (float )c * s .freq + s .present ; 61 } 62check (same_bits (row ,want ),"the in-row tokens take the penalty, the rest of the row is untouched" ); 63 } 64 65// history tokens that are all outside the row: the row does not change 66 { 67 std::vector < float > row (16 ,1.5f ); 68const std::vector < float > before = row ; 69apply_penalties (row ,s , {16 ,17 ,1000 ,-5 }); 70check (same_bits (row ,before ),"only out-of-row tokens: no change" ); 71 } 72 73// a row of the whole vocabulary behaves exactly as before the guard, bit for bit, for every window and penalty mix 74 { 75const int V = 512 ; 76 std::vector < float > base ((size_t )V ); 77for (int i = 0 ;i < V ;++ i )base [(size_t )i ]= ((float ) ((i * 37 ) %101 )- 50.f ) /7.f ; 78 std::vector < int32_t > hist ; 79for (int i = 0 ;i < 300 ;++ i )hist .push_back ((i * 53 + 11 ) %V ); 80const float repeats []= {1.f ,1.1f ,1.5f }; 81const float freqs []= {0.f ,0.2f }; 82const float presents []= {0.f ,0.3f }; 83const int lasts []= {0 ,-1 ,1 ,64 ,300 ,5000 }; 84for (float r :repeats )for (float f :freqs )for (float p :presents )for (int n :lasts ) { 85Sampling t ; 86t .repeat = r ;t .freq = f ;t .present = p ;t .last_n = n ; 87 std::vector < float > a = base ,b = base ; 88apply_penalties (a ,t ,hist ); 89old_penalties (b ,t ,hist ); 90check (same_bits (a ,b ),"full-vocabulary row: identical to the unguarded loop" ); 91 } 92 } 93 94// no penalty configured: untouched even with a history 95 { 96Sampling t ; 97 std::vector < float > row (8 ,2.f ); 98const std::vector < float > before = row ; 99apply_penalties (row ,t , {1 ,2 ,99 }); 100check (same_bits (row ,before ),"no penalty: no change" ); 101 } 102 103// an empty row with a history does not crash 104 { 105 std::vector < float > row ; 106apply_penalties (row ,s , {0 ,1 ,2 }); 107check (row .empty (),"empty row" ); 108 } 109 110// #4590: the history a request's penalties start from. Default: the last prompt token only (the reply's tokens join it); 111// STRATA_PENALTY_PROMPT=1 (counts_prompt): the whole prompt, as before. 112 { 113using strata::qwen35::penalty_history_start ; 114const std::vector < int32_t > prompt = {5 ,12 ,12 ,7777 ,7778 ,50 ,51 }; 115check (!strata::kernels::penalty_counts_prompt (nullptr )&& !strata::kernels::penalty_counts_prompt ("0" )&& 116 strata::kernels::penalty_counts_prompt ("1" ),"the switch: unset and 0 reply only, 1 the old window" ); 117const std::vector < int32_t > h = penalty_history_start (prompt , false); 118check (h .size ()== 1 && h [0 ]== 51 ,"default: the history starts at the last prompt token" ); 119check (penalty_history_start (prompt , true)== prompt ,"opt-out: the whole prompt" ); 120check (penalty_history_start ({}, false).empty (),"empty prompt" ); 121// a penalty on the first pick: the task's token 12 is untouched by default, penalised with the old window 122Sampling t ; 123t .present = 1.5f ;t .last_n = 64 ; 124 std::vector < float > a (8000 ,0.f ),b (8000 ,0.f ); 125apply_penalties (a ,t ,h ); 126apply_penalties (b ,t ,penalty_history_start (prompt , true)); 127check (a [12 ]== 0.f && a [7777 ]== 0.f && a [51 ]== -1.5f ,"default: only the fed-back token is penalised" ); 128check (b [12 ]== -1.5f && b [7777 ]== -1.5f && b [51 ]== -1.5f ,"opt-out: the prompt's tokens are penalised" ); 129// a reply that repeats itself is still penalised: tokens written join the history 130 std::vector < int32_t > hr = h ; 131hr .push_back (900 );hr .push_back (900 ); 132 std::vector < float > c (8000 ,0.f ); 133t .freq = 0.5f ; 134apply_penalties (c ,t ,hr ); 135check (c [900 ]== - (1.5f + 2 * 0.5f )&& c [12 ]== 0.f ,"default: the reply's own repeats are still penalised" ); 136 } 137 138 std::printf ("qwen35_sampling_test: %d checks passed\n" ,checks ); 139return 0 ; 140}