okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
6ca60e9a0
main
1// Actual gated proposal packet contract, CPU-only; hand-specified expectations. 2// Includes immutable q bits, root rescue on all branches, NULL versus budget, and invalid packets. 3// Does not validate a GPU execution or main's final window copy/commit loop. 4#include "strata/core/gated_proposal.hpp" 5#include "strata/core/spec_proposal.hpp" 6#include <algorithm> 7#include <array> 8#include <cmath> 9#include <cstdint> 10#include <cstdio> 11#include <cstring> 12#include <limits> 13#include <stdexcept> 14#include <string> 15 16namespace { 17namespace ap= strata::core::gated; 18namespace ac= strata::core::proposal; 19constexpr int A = 7 ,B = 4097 ,C = 8190 ,V = 8192 ; 20constexpr float theta = .5f ; 21using Row = std::array < int32_t ,ap::kStride > ; 22using Rows = std::array < int32_t ,ap::kRows * ap::kStride > ; 23using Floats = std::array < float ,ap::kRows > ; 24using Drafts = std::array < int32_t ,ap::kRows > ; 25int checks = 0 ; 26void need (bool ok ,const char * msg ) {++ checks ;if (!ok )throw std::runtime_error (msg );} 27uint32_t bits (float f ) {uint32_t n ;std::memcpy (& n ,& f ,4 );return n ;} 28Row row (float a = .625f ,float b = .25f ,float c = .125f ) { 29Row r ;r .fill (-1 ); 30// IDs/padding deliberately unlike target order. Padding must survive masking. 31for (int i = 0 ;i < ap::kEntries ;++ i )r [ap::kEntries + i ]= (int32_t )(0x3e800000u + (uint32_t )i ); 32const int ids []= {C ,A ,B };const float probs []= {c ,a ,b }; 33for (int i = 0 ;i < 3 ;++ i ){r [i ]= ids [i ];std::memcpy (r .data ()+ ap::kEntries + i ,& probs [i ],4 );} 34return r ; 35} 36void put (Rows & rows ,int n ,const Row & r ) {std::memcpy (rows .data ()+ n * ap::kStride ,r .data (),sizeof (r ));} 37void expect_mask (const int32_t * actual ,const Row & original ,float a ,float b ,float c ) { 38Row expected = original ;const float p []= {c ,a ,b }; 39for (int i = 0 ;i < 3 ;++ i )std::memcpy (expected .data ()+ ap::kEntries + i ,& p [i ],4 ); 40need (std::memcmp (actual ,expected .data (),sizeof (expected ))== 0 ,"mask differs in retained bits / IDs / padding / +0" ); 41} 42struct Expected {int T ,nq ,offers ,attempts ,failed ;bool suffix ,nulltail ;}; 43void packet (const ap::Packet & p ,Expected x ) { 44need (!p .error ,"unexpected packet error" ); 45need (p .T == x .T && p .n_q == x .nq && p .offers == x .offers && p .attempts == x .attempts && p .failed_index == x .failed && p .from_sfx == x .suffix && p .null_tail == x .nulltail ,"packet fields differ from hand-written expectation" ); 46need (p .T == p .offers + 1 ,"one terminal output after offered prefix" ); 47need (ac::rowkind (p .T - 1 ,p .T ,p .n_q )== (p .null_tail ?ac::RowKind ::NullTail :ac::RowKind ::PlainBonus ),"AA packet / Hubble terminal rowkind mismatch" ); 48} 49struct Fixture { 50Rows raw {};Drafts d {};Floats scores {}; 51Fixture (){raw .fill (-1 );d .fill (-777 );scores .fill (std::numeric_limits < float > ::quiet_NaN ());} 52void set (int j ,const Row & r ,int id ,float score ){put (raw ,j ,r );d [j ]= id ;scores [j ]= score ;} 53bool call (ap::Packet & out ,int generated ,int budget ,const ap::RootChoice & root = {},bool initial = false,int capacity = 6 ) const { 54const Rows saved = raw ; const Drafts ds = d ; const Floats ss = scores ; 55const bool ok = ap:: make_packet ( out , raw . data (), d . data (), scores . data (), generated , budget , capacity , V , theta , initial , root ); 56need (std:: memcmp ( raw . data (), saved . data (), sizeof raw ) == 0 , "raw q mutated" ); 57need (std:: memcmp ( d . data (), ds . data (), sizeof d ) == 0 , "raw drafts mutated" ); 58need (std:: memcmp ( scores . data (), ss . data (), sizeof scores ) == 0 , "own-score mutated" ); 59need ( ok == ( out . error == nullptr ), "return/error inconsistent" ); 60return ok ; 61} 62}; 63void positive ( const char * name , const Fixture & f ,int n ,int budget ,ap::RootChoice root ,Expected x ) { 64ap::Packet p ; need ( f . call ( p , n , budget , root ), name ); packet ( p , x ); 65std:: printf ( "{\"case\":\"%s\",\"passed\":true}\n" , name ); 66} 67void tests () { 68const Row raw = row ();Row masked {}; const Row saved = raw ; 69need (ap:: mask_row ( raw . data (), masked . data (), theta , -1 , V ), "mask H" ); 70expect_mask ( masked . data (), raw , .625f , 0 , 0 ); // exact GPU T1_NULL mu 71need (ap:: mask_row ( raw . data (), masked . data (), theta , B , V ), "mask UNION" ); 72expect_mask ( masked . data (), raw , .625f , .25f , 0 ); // GPU UNION mu, sum=.875 !=1 73need (std:: memcmp ( raw . data (), saved . data (), sizeof raw ) == 0 , "mask modified raw" ); 74const Row edge = row ( .5f ,std:: nextafter ( .5f , 0.0f ), 0x1p-25f ); 75need (ap:: mask_row ( edge . data (), masked . data (), theta , -1 , V ), "mask inclusive edge" ); 76expect_mask ( masked . data (), edge , .5f , 0 , 0 ); 77const Row above = row (std:: nextafter ( .5f , 1.0f ), .25f , .25f - 0x1p-24f ); 78need (ap:: mask_row ( above . data (), masked . data (), theta , -1 , V ), "mask above edge" ); 79expect_mask ( masked . data (), above ,std:: nextafter ( .5f , 1.0f ), 0 , 0 ); 80const Row none = row ( .375f , .375f , .25f ); 81need (ap:: mask_row ( none . data (), masked . data (), theta , -1 , V ), "mask zero measure" ); 82expect_mask ( masked . data (), none , 0 , 0 , 0 ); 83const Row negzero = row ( 1.0f , -0.0f , 0.0f ); 84need (ap:: mask_row ( negzero . data (), masked . data (), 0.0f , -1 , V ), "theta zero mask" ); 85expect_mask ( masked . data (), negzero , 1.0f , -0.0f , 0.0f ); 86need ( bits (ap:: mass_at ( masked . data (), B )) == bits ( -0.0f ), "retained negative zero bits changed" ); 87 88ap::Packet p ; 89need (ap:: make_packet ( p , nullptr , nullptr , nullptr , 0 , 3 , 6 , V , theta ,true,{}), "initial no buffers" ); 90packet ( p ,{ 1 , 0 , 0 , 0 , -1 ,false,false}); 91need (ap:: make_packet ( p , nullptr , nullptr , nullptr , 0 , 0 , 6 , V , theta ,false,{}), "zero producer budget" ); 92packet ( p ,{ 1 , 0 , 0 , 0 , -1 ,false,false}); 93 94Fixture firstnull ; firstnull . set ( 0 , raw , B , .25f ); 95positive ( "root_NULL" , firstnull , 1 , 3 ,{}, { 1 , 1 , 0 , 1 , 0 ,false,true}); 96Fixture allzero ; allzero . set ( 0 , none , B , .375f ); 97positive ( "all_zero_mu_NULL" , allzero , 1 , 3 ,{}, { 1 , 1 , 0 , 1 , 0 ,false,true}); 98Fixture one ; one . set ( 0 , raw , A , .625f ); 99positive ( "actual_max_steps_1_is_bonus" , one , 1 , 1 ,{}, { 2 , 1 , 1 , 1 , -1 ,false,false}); 100need (! one . call ( p , 1 , 3 ), "incomplete generated1/budget3 must not become bonus" ); 101Fixture later ; later . set ( 0 , raw , A , .625f ); later . set ( 1 , raw , C , .125f ); 102positive ( "second_attempt_NULL" , later , 2 , 3 ,{}, { 2 , 2 , 1 , 2 , 1 ,false,true}); 103need ( later . call ( p , 2 , 3 ), "NULL packet" ); 104need ( p . offers == 1 && p . failed_index == 1 && p . T - 1 == 1 , "failed D excluded from offered window input span" ); 105// Packet contains no token array. These counts exclude failed D from the 106// intended copy span; actual main window construction remains integration work. 107expect_mask ( p . mu . data () + ap:: kStride , raw , .625f , 0 , 0 ); 108Fixture full ; full . set ( 0 , raw , A , .625f ); full . set ( 1 , raw , A , .625f ); full . set ( 2 , raw , A , .625f ); 109positive ( "actual_max_steps_3_bonus" , full , 3 , 3 ,{}, { 4 , 3 , 3 , 3 , -1 ,false,false}); 110need (! full . call ( p , 3 , 4 ), "generated3/budget4 must not guess exhausted budget" ); 111 112const ap::RootChoice E {true, B , 6 }; 113positive ( "UNION_low_D_suffix" , firstnull , 1 , 3 , E , { 6 , 1 , 5 , 1 , -1 ,true,false}); 114need ( firstnull . call ( p , 1 , 3 , E ), "UNION rescue" ); expect_mask ( p . mu . data (), raw , .625f , .25f , 0 ); 115const Row union_root_snapshot = [](){Row r = row ();Row out ; need (ap:: mask_row ( r . data (), out . data (), theta , B , V ), "snapshot union" ); return out ;}(); 116Fixture high ; high . set ( 0 , raw , A , .625f ); 117need ( high . call ( p , 1 , 1 , E ), "UNION D!=s offer" ); packet ( p ,{ 2 , 1 , 1 , 1 , -1 ,false,false}); 118need (std:: memcmp ( p . mu . data (), union_root_snapshot . data (), sizeof ( Row )) == 0 , "UNION must exist on MTP branch" ); 119Fixture otherlow ; otherlow . set ( 0 , raw , C , .125f ); 120need ( otherlow . call ( p , 1 , 3 , E ), "UNION D!=s NULL" ); packet ( p ,{ 1 , 1 , 0 , 1 , 0 ,false,true}); 121need (std:: memcmp ( p . mu . data (), union_root_snapshot . data (), sizeof ( Row )) == 0 , "UNION must exist on NULL branch" ); 122// Later row's B is NOT rescued by the root set. 123Fixture next ; next . set ( 0 , raw , A , .625f ); next . set ( 1 , raw , B , .25f ); 124need ( next . call ( p , 2 , 3 , E ), "later H only" ); packet ( p ,{ 2 , 2 , 1 , 2 , 1 ,false,true}); 125expect_mask ( p . mu . data (), raw , .625f , .25f , 0 ); 126expect_mask ( p . mu . data () + ap:: kStride , raw , .625f , 0 , 0 ); 127// Eligibility false despite populated rescue fields must mean plain H. 128positive ( "ineligible_rescue_ignored" , firstnull , 1 , 3 ,{false, B , 6 }, { 1 , 1 , 0 , 1 , 0 ,false,true}); 129 130// Frozen source choice must not inspect invalid discarded later materialization. 131// A future row's poison may be present, but only root is consumed if suffix wins. 132Fixture eager = firstnull ; 133positive ( "suffix_ignores_later_D_score_q" , eager , 3 , 3 , E , { 6 , 1 , 5 , 1 , -1 ,true,false}); 134Fixture stopped = firstnull ; 135positive ( "NULL_ignores_later_materialization" , stopped , 3 , 3 ,{}, { 1 , 1 , 0 , 1 , 0 ,false,true}); 136Fixture edgeoffer ; edgeoffer . set ( 0 , edge , A , .5f ); 137positive ( "inclusive_D_at_theta" , edgeoffer , 1 , 1 ,{}, { 2 , 1 , 1 , 1 , -1 ,false,false}); 138Fixture edgenull ; edgenull . set ( 0 , edge , B ,std:: nextafter ( .5f , 0.0f )); 139positive ( "D_just_below_theta_NULL" , edgenull , 1 , 3 ,{}, { 1 , 1 , 0 , 1 , 0 ,false,true}); 140 141Fixture bad = one ; bad . scores [ 0 ] = std:: nextafter ( .625f , 1.0f ); 142need (! bad . call ( p , 1 , 1 ), "own-score mismatch must fail" ); 143bad = one ; bad . d [ 0 ] = 123 ; 144need (! bad . call ( p , 1 , 1 ), "D outside q support must fail" ); 145bad = one ; bad . raw [ 1 ] = bad . raw [ 0 ]; 146need (! bad . call ( p , 1 , 1 ), "duplicate support must fail" ); 147bad = one ;float nan = std::numeric_limits < float > :: quiet_NaN ();std:: memcpy ( bad . raw . data () + ap:: kEntries + 1 , & nan , 4 ); 148need (! bad . call ( p , 1 , 1 ), "nonfinite q must fail" ); 149need (! one . call ( p , 1 , 0 ), "n_generated exceeds actual budget" ); 150need (! one . call ( p , 1 , 1 ,{},true), "initial stale attempts" ); 151need (! one . call ( p , 1 , 1 ,{true, V , 6 }), "out-of-vocab rescue" ); 152need (! one . call ( p , 1 , 1 ,{true, B , 7 }), "suffix width exceeds capacity" ); 153need (! one . call ( p , 1 , 6 ), "budget cannot fill terminal slot" ); 154need (!ap:: make_packet ( p , nullptr , nullptr , nullptr , 0 , 1 , 6 , V , theta ,false,{}), "missing expected attempt" ); 155need (!ap:: make_packet ( p , nullptr , nullptr , nullptr , 1 , 1 , 6 , V , theta ,false,{}), "null buffers" ); 156need (!ap:: mask_row ( raw . data (), masked . data (), nan , -1 , V ), "NaN theta" ); 157need (!ap:: mask_row ( raw . data (), masked . data (), -.1f , -1 , V ), "negative theta" ); 158// Concrete producer/context boundaries: budget is not inferred from count. 159// The failed row may be the LAST physically generated row at actual budget. 160Fixture last_attempt ; last_attempt . set ( 0 , raw , A , .625f ); last_attempt . set ( 1 , raw , B , .25f ); 161need ( last_attempt . call ( p , 2 , 2 ,{},false, 3 ), "last producer attempt failed at exact budget" ); 162packet ( p ,{ 2 , 2 , 1 , 2 , 1 ,false,true}); 163expect_mask ( p . mu . data () + ap:: kStride , raw , .625f , 0 , 0 ); 164// Zero remaining proposal budget has NO attempt, regardless of stale bytes. 165Fixture poison ; 166need ( poison . call ( p , 0 , 0 ,{},false, 1 ), "capacity1 zero producer budget" ); 167packet ( p ,{ 1 , 0 , 0 , 0 , -1 ,false,false}); 168need (std:: all_of ( p . mu . begin (), p . mu . end (),[](int32_t word ){ return word == 0 ;}), "zero-budget packet retained old mu" ); 169need (! last_attempt . call ( p , 1 , 0 ,{},false, 1 ), "actual generated row cannot be relabeled zero budget" ); 170// If a caller has already materialized a failed row at the final target 171// capacity, it must preserve NULL or explicitly reject this packet. Passing 172// min(generated,room-1) here would erase evidence and evade this contract. 173Fixture edge_failed ; edge_failed . set ( 0 , raw , B , .25f ); 174const bool edge_ok = edge_failed . call ( p , 1 , 1 ,{},false, 1 ); 175if ( edge_ok ) packet ( p ,{ 1 , 1 , 0 , 1 , 0 ,false,true}); 176else need ( p . error != nullptr , "capacity failure must be explicit, never bonus" ); 177// A subsequent successful call must clear every stale error/count/mode field. 178need ( one . call ( p , 1 , 1 ), "packet reset after errors" ); packet ( p ,{ 2 , 1 , 1 , 1 , -1 ,false,false}); 179} 180} // namespace 181int main () { 182try { tests ();std:: printf ( "{\"summary\":\"gated_proposal_test\",\"checks\":%d,\"passed\":true,\"GPU_executed\":false,\"actual_main_window_copy_tested\":false}\n" , checks ); return 0 ;} 183catch ( const std::exception & e ){std:: fprintf ( stderr , "gated_proposal_test FAILED after %d checks: %s\n" , checks , e . what ()); return 1 ;} 184}