okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
6ca60e9a0
main
1// Explicit mu/NULL verifier against a CPU oracle; CUDA/HIP, no model or tracing. 2// Equal finite logits at three known IDs, all other logits -inf, give p=1/3 3// at each ID independently of an exp implementation. A top-k1 fixture gives 4// exact p=delta. Thus no test-only inspection API or SamplerTraceRow is needed. 5// These finite per-token checks do not claim arbitrary-logit bitwise equality. 6#include "strata/core/spec_prob.hpp" 7#include "strata/core/spec_proposal.hpp" 8#include "strata/kernels/sampler.hpp" 9#include <cuda_runtime.h> 10#include <algorithm> 11#include <array> 12#include <cmath> 13#include <cstdint> 14#include <cstdio> 15#include <cstring> 16#include <limits> 17#include <stdexcept> 18#include <string> 19#include <utility> 20#include <vector> 21 22namespace { 23constexpr int V = 8192 ,MaxT = 3 ,A = 7 ,B = 4097 ,C = 8190 ; 24constexpr int Entries = strata::core::kSpecQEntries ,Stride = strata::core::kSpecQStride ; 25constexpr int Error = strata::core::proposal::kProposalSamplerErrorToken ; 26using Mass = std::array < float ,3 > ; 27constexpr std::array < int ,3 > IDs {{A ,B ,C }}; 28struct Result {int token ;bool accepted ;}; 29struct Case { 30const char * name ;int T ,nq ;std::vector < int32_t > drafts ; 31 std::vector < Mass > mu ,raw ;int discarded = B ; 32bool error = false,legacy_equal = false; 33}; 34void require (bool ok ,const char * message ) {if (!ok )throw std::runtime_error (message );} 35void cu (cudaError_t code ,const char * message ) { 36if (code != cudaSuccess )throw std::runtime_error (std::string (message )+ ": " + cudaGetErrorString (code )); 37} 38float uniform (uint64_t seed ,uint64_t counter ) { 39uint32_t c0 = (uint32_t )counter ,c1 = (uint32_t )(counter >>32 ),c2 = (uint32_t )seed ,c3 = (uint32_t )(seed >>32 ); 40for (int i = 0 ;i < 10 ;++ i ) { 41const uint64_t x = 0x9E3779B9u * (uint64_t )c0 ,y = 0xBB67AE85u * (uint64_t )c2 ; 42const uint32_t n0 = (uint32_t )(y >>32 )^c1 ^(uint32_t )i ,n2 = (uint32_t )(x >>32 )^c3 ; 43c0 = n0 ;c1 = (uint32_t )y ;c2 = n2 ;c3 = (uint32_t )x ; 44 } 45return (float )(c0 >>8 )* (1.0f /16777216.0f ); 46} 47double mass (const Mass & q ,int id ) { 48for (int i = 0 ;i < 3 ;++ i )if (IDs [i ]== id )return q [i ]; 49return 0 ; 50} 51int choose (const std::array < double ,3 >& weights ,float u ,bool normalize ) { 52const double sum = weights [0 ]+ weights [1 ]+ weights [2 ];require (sum > 0 ,"empty reachable law" ); 53double cum = 0 ;int last = -1 ; 54for (int i = 0 ;i < 3 ;++ i ) { 55if (normalize && weights [i ]<=0 )continue ; 56last = IDs [i ];cum += normalize ?weights [i ]/sum :weights [i ]; 57if ((double )u < cum )return IDs [i ]; 58 } 59require (last >=0 ,"empty support" );return last ; 60} 61Result reference (const Case & c ,int row ,uint64_t seed ,const Mass * alternate = nullptr ) { 62const uint64_t pos = 365 + (uint64_t )row ; 63const std::array < double ,3 > p = c .error ?std::array < double ,3 > {{1 ,0 ,0 }}:std::array < double ,3 > {{1.0 /3 ,1.0 /3 ,1.0 /3 }}; 64const bool last = row == c .T - 1 ,null = last && c .nq == c .T ; 65if (last && !null )return {choose (p ,uniform (seed ,pos ),false),false}; 66const int d = null ?-1 :c .drafts [row ];const bool point = !null && row >=c .nq ; 67const Mass q = alternate ?* alternate :(row < c .nq ?c .mu [row ]:Mass {{0 ,0 ,0 }}); 68double pd = 0 ;for (int i = 0 ;i < 3 ;++ i )if (IDs [i ]== d )pd = p [i ]; 69if (!null ) { 70const double qd = point ?1 :mass (q ,d ); 71if (!(qd > 0 )|| !std::isfinite (qd ))return {Error ,false}; 72if (pd > 0 && (double )uniform (seed ,pos |strata::core::kSpecCtrAccept )* qd < pd )return {d ,true}; 73 } 74 std::array < double ,3 > weights {}; 75for (int i = 0 ;i < 3 ;++ i )weights [i ]= point ?(IDs [i ]== d ?0 :p [i ]):std::max (0.0 ,p [i ]- mass (q ,IDs [i ])); 76if (weights [0 ]+ weights [1 ]+ weights [2 ]<=0 )return {Error ,false}; 77return {choose (weights ,uniform (seed ,pos |strata::core::kSpecCtrResid ),true),false}; 78} 79struct Device { 80float * logits = nullptr ;int * out = nullptr ;int32_t * draft = nullptr ;int32_t * mu = nullptr ; 81Device () { 82cu (cudaMalloc ((void ** )& logits ,MaxT * V * sizeof (float )),"logits" ); 83cu (cudaMalloc ((void ** )& out ,MaxT * sizeof (int )),"out" ); 84cu (cudaMalloc ((void ** )& draft ,(MaxT - 1 )* sizeof (int32_t )),"draft" ); 85cu (cudaMalloc ((void ** )& mu ,MaxT * Stride * sizeof (int32_t )),"mu" ); 86 } 87 ~Device (){cudaDeviceSynchronize ();cudaFree (logits );cudaFree (out );cudaFree (draft );cudaFree (mu );} 88}; 89std::vector < Case > cases () { 90const Mass raw {{.625f ,.25f ,.125f }},m {{.625f ,0 ,0 }},root {{.625f ,.25f ,0 }}; 91return { 92 {"T1 NULL" ,1 ,1 ,{}, {m },{raw }}, 93 {"zero mu NULL" ,1 ,1 ,{}, {Mass {{0 ,0 ,0 }}},{Mass {{.375f ,.375f ,.25f }}}}, 94 {"offer then NULL" ,2 ,2 ,{A },{m ,m },{raw ,raw }}, 95 {"two offers then NULL" ,3 ,3 ,{A ,A },{m ,m ,m },{raw ,raw ,raw }}, 96 {"plain budget" ,1 ,0 ,{}, {},{}}, 97 {"offer reject and bonus" ,2 ,1 ,{A },{m },{raw }}, 98 {"mu then delta then bonus" ,3 ,1 ,{A ,B },{m },{raw }}, 99 {"point then bonus" ,2 ,0 ,{A },{},{}}, 100 {"UNION low-q rescue" ,2 ,1 ,{B },{root },{raw }}, 101 {"UNION other draw rejection" ,2 ,1 ,{A },{root },{raw }}, 102 {"UNION NULL" ,1 ,1 ,{}, {root },{raw },C }, 103 {"inclusive threshold NULL" ,1 ,1 ,{}, {Mass {{.5f ,0 ,0 }}},{Mass {{.5f ,.25f ,.25f }}}}, 104 {"zero residual sentinel" ,1 ,1 ,{}, {Mass {{1 ,0 ,0 }}},{Mass {{1 ,0 ,0 }}},-1 ,true}, 105 {"legacy full-q parity" ,3 ,2 ,{A ,B },{raw ,raw },{raw ,raw },-1 ,false,true}, 106 {"missing offered mu sentinel" ,2 ,1 ,{B },{m },{raw }} 107 }; 108} 109int run () { 110Device d ;long long mismatches = 0 ,rows = 0 ,accepts = 0 ,rejects = 0 ,null_same = 0 ,plain_witness = 0 ,fullq_witness = 0 ,union_witness = 0 ; 111const uint64_t seeds []= {0 ,1 ,2 ,3 ,7 ,11 ,17 ,29 ,41 ,67 ,101 ,777 ,2026101600ULL ,2026101700ULL ,0x123456789abcdef0ULL ,3728620368707436339ULL }; 112for (const Case & c :cases ()) { 113require ((int )c .drafts .size ()== c .T - 1 && (int )c .mu .size ()== c .nq ,"bad fixture" ); 114 std::vector < float > logits (c .T * V ,- std::numeric_limits < float > ::infinity ()); 115for (int t = 0 ;t < c .T ;++ t ){logits [t * V + A ]= 0 ;if (!c .error ){logits [t * V + B ]= 0 ;logits [t * V + C ]= 0 ;}} 116 std::vector < int32_t > packed (c .T * Stride ,-1 );const int permutation []= {2 ,0 ,1 }; 117for (int t = 0 ;t < c .nq ;++ t )for (int i = 0 ;i < Entries ;++ i ) { 118const float q = i < 3 ?c .mu [t ][permutation [i ]]:0 ; 119if (i < 3 )packed [t * Stride + i ]= IDs [permutation [i ]]; 120 std::memcpy (& packed [t * Stride + Entries + i ],& q ,sizeof (q )); 121 } 122cu (cudaMemcpy (d .mu ,packed .data (),packed .size ()* sizeof (int32_t ),cudaMemcpyHostToDevice ),"copy mu" ); 123if (!c .drafts .empty ())cu (cudaMemcpy (d .draft ,c .drafts .data (),c .drafts .size ()* sizeof (int32_t ),cudaMemcpyHostToDevice ),"copy drafts" ); 124for (uint64_t seed :seeds ) { 125 strata::kernels::SamplerParams p ;p .top_k = c .error ?1 :3 ;p .top_p = 1 ;p .min_p = 0 ;p .temperature = 1 ;p .seed = seed ;p .counter = 365 ; 126const int32_t * drafts = c .drafts .empty ()?nullptr :d .draft ; 127cu (cudaMemcpy (d .logits ,logits .data (),logits .size ()* sizeof (float ),cudaMemcpyHostToDevice ),"copy logits" ); 128require (strata::kernels::sample_tokens_proposal (d .logits ,c .T ,V ,nullptr ,0 ,p ,drafts ,d .mu ,c .nq ,d .out ,nullptr ),"proposal Split refused" ); 129cu (cudaDeviceSynchronize (),"proposal sync" );std::vector < int > out (c .T ); 130cu (cudaMemcpy (out .data (),d .out ,c .T * sizeof (int ),cudaMemcpyDeviceToHost ),"copy out" ); 131for (int t = 0 ;t < c .T ;++ t ) { 132const auto ref = reference (c ,t ,seed );++ rows ; 133if (out [t ]!= ref .token ){++ mismatches ;std::fprintf (stderr ,"%s seed=%llu row=%d: got=%d expected=%d\n" ,c .name ,(unsigned long long )seed ,t ,out [t ],ref .token );} 134const bool null = t == c .T - 1 && c .nq == c .T ; 135if (t < c .T - 1 && ref .token != Error )(ref .accepted ?accepts :rejects )++ ; 136if (null && !c .error ) { 137if (out [t ]== c .discarded )++ null_same ; 138const std::array < double ,3 > target {{1.0 /3 ,1.0 /3 ,1.0 /3 }}; 139if (ref .token != choose (target ,uniform (seed ,365 + t ),false))++ plain_witness ; 140if (ref .token != reference (c ,t ,seed ,& c .raw [t ]).token )++ fullq_witness ; 141if (std::strcmp (c .name ,"UNION NULL" )== 0 ) { 142const Mass old {{.625f ,0 ,0 }}; 143if (ref .token != reference (c ,t ,seed ,& old ).token )++ union_witness ; 144 } 145// NULL is terminal even when residual==discarded D. T1 has 146// zero offered inputs; no dtok slot exists for its raw D. 147if (c .T == 1 && out [t ]== c .discarded )require (c .drafts .empty (),"NULL candidate leaked into offered inputs" ); 148 } 149 } 150if (c .legacy_equal ) { 151cu (cudaMemcpy (d .logits ,logits .data (),logits .size ()* sizeof (float ),cudaMemcpyHostToDevice ),"legacy logits" ); 152require (strata::kernels::sample_tokens_spec (d .logits ,c .T ,V ,nullptr ,0 ,p ,drafts ,d .mu ,c .nq ,d .out ,nullptr ),"legacy Split refused" ); 153cu (cudaDeviceSynchronize (),"legacy sync" );std::vector < int > old (c .T ); 154cu (cudaMemcpy (old .data (),d .out ,c .T * sizeof (int ),cudaMemcpyDeviceToHost ),"legacy output" ); 155require (old == out ,"explicit proposal call changed legacy full-q result" ); 156 } 157 } 158 } 159require (accepts > 0 && rejects > 0 && null_same > 0 ,"missing accept/reject/NULL-equality coverage" ); 160require (plain_witness > 0 && fullq_witness > 0 && union_witness > 0 ,"fixed seeds did not discriminate incorrect laws" ); 161 std::printf ("spec_proposal_parity: %lld rows, %lld mismatches; accepts=%lld rejects=%lld NULL==rawD=%lld; ordinary/fullq/omitted-rescue witnesses=%lld/%lld/%lld\n" ,rows ,mismatches ,accepts ,rejects ,null_same ,plain_witness ,fullq_witness ,union_witness ); 162return mismatches ?1 :0 ; 163} 164}// namespace 165int main () { 166try {return run ();}catch (const std::exception & e ){std::fprintf (stderr ,"spec_proposal_parity: %s\n" ,e .what ());return 1 ;} 167}