okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
b90297368
main
1// src/kernels/verify_batch_parity.cpp - the verify window's one-launch forms (the hyper-connection write, the PLE post-ops) against the per-token launches they 2// replace, bitwise. 3// 4// build/verify_batch_parity 5// 6// 1. native_gr_post_multi against n native_gr_post calls (the hyper-connection write before the PLE block). 7// 2. native_ple_postops_batch_snap (STRATA_DF_PLE) against the per-token loop of the verify window: per token 8// native_ple_postops, ple_history_advance and a copy of the history (the commit's snapshot); the results, the final 9// history and every snapshot, for windows of 2..8 tokens. 10// Skipped where the engine switches the batched PLE post-ops off itself (an engine built with CUDA 12.8 on a compute 11// capability 12.0 card: sm120_series.hpp ple_batch_resolve, the rule the prompt path uses; STRATA_PLE_BATCH=1 runs it anyway). 12// GPU, synthetic, no model. 13#include "strata/kernels/f16_bits.hpp" 14#include "strata/kernels/kv_q4.hpp" 15#include "strata/kernels/kv_stream.hpp" 16#include "strata/kernels/native_gr_postops.hpp" 17#include "strata/kernels/native_ple_postops.hpp" 18#include "strata/kernels/ngram.hpp" 19#include "strata/kernels/ple.hpp" 20#include "strata/kernels/qsa.hpp" 21#include "strata/core/sm120_series.hpp" 22 23#include <cuda_runtime.h> 24 25#include <cstdint> 26#include <cstdio> 27#include <cstdlib> 28#include <cstring> 29#include <random> 30#include <vector> 31 32namespace k= strata::kernels; 33 34namespace { 35 36int g_fail = 0 ; 37 38void ck (cudaError_t e ,const char * what ) { 39if (e != cudaSuccess ) { 40 std::printf ("CUDA: %s: %s\n" ,what ,cudaGetErrorString (e )); 41 std::exit (2 ); 42 } 43} 44 45// The set-up copies and fills run on the legacy default stream, which does NOT order work on the non-blocking stream 46// the kernels run on (and cudaMemset / a pageable cudaMemcpy may return before the device has finished them): each 47// one is completed here, or a kernel could read its inputs - or have its outputs overwritten - while they land. 48template < typename T > 49T * dalloc (size_t n ,int fill = 0 ) { 50T * p = nullptr ; 51ck (cudaMalloc (& p ,n * sizeof (T )+ 64 ),"malloc" ); 52ck (cudaMemset (p ,fill ,n * sizeof (T )+ 64 ),"memset" ); 53ck (cudaDeviceSynchronize (),"memset sync" ); 54return p ; 55} 56 57template < typename T > 58std::vector < T > read (const T * d ,size_t n ) { 59 std::vector < T > h (n ); 60ck (cudaMemcpy (h .data (),d ,n * sizeof (T ),cudaMemcpyDeviceToHost ),"read" ); 61return h ; 62} 63 64template < typename T > 65T * upload (const std::vector < T >& h ) { 66T * d = dalloc < T > (h .size ()); 67ck (cudaMemcpy (d ,h .data (),h .size ()* sizeof (T ),cudaMemcpyHostToDevice ),"upload" ); 68ck (cudaDeviceSynchronize (),"upload sync" ); 69return d ; 70} 71 72void report (const char * what ,bool ok ) { 73 std::printf ("%-64s %s\n" ,what ,ok ?"bitwise equal" :"DIFFERENT" ); 74if (!ok )++ g_fail ; 75} 76 77// ---- 1. the hyper-connection write 78void gr_post (cudaStream_t s ) { 79const int N = 2560 ,HC = 4 ; 80 std::mt19937 rng (12 ); 81 std::normal_distribution < float > nd (0.f ,1.f ); 82for (int n : {2 ,4 ,8 }) { 83 std::vector < float > r ((size_t )n * HC * N ),bo ((size_t )n * N ),inj ((size_t )n * HC ); 84for (auto & x :r )x = nd (rng ); 85for (auto & x :bo )x = nd (rng ); 86for (auto & x :inj )x = 3.f * nd (rng ); 87float * r0 = upload (r ); 88float * r1 = upload (r ); 89float * d_bo = upload (bo ); 90float * d_inj = upload (inj ); 91for (int t = 0 ;t < n ;++ t ) 92 k::native_gr_post (r0 + (size_t )t * HC * N ,d_bo + (size_t )t * N ,d_inj + (size_t )t * HC , 93r0 + (size_t )t * HC * N ,N ,HC ,s ); 94 k::native_gr_post_multi (r1 ,d_bo ,d_inj ,r1 ,N ,HC ,n , (long long )HC * N ,N ,HC ,s ); 95ck (cudaStreamSynchronize (s ),"gr post" ); 96char what [128 ]; 97 std::snprintf (what ,sizeof what ,"native_gr_post_multi, %d tokens (in place)" ,n ); 98report (what ,read (r0 ,r .size ())== read (r1 ,r .size ())); 99cudaFree (r0 );cudaFree (r1 );cudaFree (d_bo );cudaFree (d_inj ); 100 } 101} 102 103// ---- 2. the PLE post-ops of a window, with the commit's history snapshots 104void ple (cudaStream_t s ) { 105constexpr int N = k::NG_N_EMBD ,D = k::NG_HC_DIM ,HS = k::NG_HIST * k::NG_HC_DIM ; 106 std::mt19937 rng (13 ); 107 std::normal_distribution < float > nd (0.f ,1.f ); 108 std::vector < float > nk (D ),nq (D ),nc (D ); 109for (auto * v : {& nk ,& nq ,& nc }) 110for (auto & x :* v )x = 1.f + 0.1f * nd (rng ); 111 std::vector < uint16_t > conv ((size_t )4 * D ); 112for (auto & x :conv )x = k::f16_from_f32 (0.3f * nd (rng )); 113 k::PleWeights w ; 114w .norm_key = upload (nk ); 115w .norm_query = upload (nq ); 116w .norm_conv = upload (nc ); 117w .conv1d_f16 = upload (conv ); 118 std::vector < float > hist0 ((size_t )HS ); 119for (auto & x :hist0 )x = nd (rng ); 120for (int n : {2 ,3 ,4 ,8 }) { 121 std::vector < float > key ((size_t )n * D ),hid ((size_t )n * D ),val ((size_t )n * N ); 122for (auto & x :key )x = 4.f * nd (rng ); 123for (auto & x :hid )x = nd (rng ); 124for (auto & x :val )x = nd (rng ); 125// per token, as the verify window's loop: native_ple_postops (ple_block_projected's buffers), the history 126// advance and the snapshot copy 127float * key0 = upload (key ); 128float * hid0 = upload (hid ); 129float * val0 = upload (val ); 130float * hist_a = upload (hist0 ); 131float * snap_a = dalloc < float > ((size_t )n * HS ); 132float * q = dalloc < float > (D ),* nrm = dalloc < float > (D ),* g = dalloc < float > (4 ),* gd = dalloc < float > (D ); 133float * cv = dalloc < float > (D ); 134for (int t = 0 ;t < n ;++ t ) { 135 k::NativePlePostopsBuffers b {q ,nrm ,g ,gd ,nrm ,cv ,hid0 + (size_t )t * D }; 136 k::native_ple_postops (key0 + (size_t )t * D ,hid0 + (size_t )t * D ,val0 + (size_t )t * N ,hist_a ,w ,b ,s ); 137 k::ple_history_advance (hist_a ,nrm ,s ); 138ck (cudaMemcpyAsync (snap_a + (size_t )t * HS ,hist_a , (size_t )HS * 4 ,cudaMemcpyDeviceToDevice ,s ),"snap" ); 139 } 140// the window's one pass 141float * key1 = upload (key ); 142float * hid1 = upload (hid ); 143float * val1 = upload (val ); 144float * hist_b = upload (hist0 ); 145float * snap_b = dalloc < float > ((size_t )n * HS ); 146float * qn = dalloc < float > ((size_t )n * D ),* gated = dalloc < float > ((size_t )n * D ),* gate = dalloc < float > ((size_t )n * 4 ); 147 k::native_ple_postops_batch_snap (key1 ,hid1 ,val1 ,hist_b ,w ,qn ,gated ,gate ,n ,snap_b ,s ); 148ck (cudaStreamSynchronize (s ),"ple" ); 149char what [128 ]; 150 std::snprintf (what ,sizeof what ,"native_ple_postops_batch_snap, %d tokens: results" ,n ); 151report (what ,read (hid0 ,hid .size ())== read (hid1 ,hid .size ())); 152 std::snprintf (what ,sizeof what ,"native_ple_postops_batch_snap, %d tokens: history" ,n ); 153report (what ,read (hist_a , (size_t )HS )== read (hist_b , (size_t )HS )); 154 std::snprintf (what ,sizeof what ,"native_ple_postops_batch_snap, %d tokens: snapshots" ,n ); 155report (what ,read (snap_a , (size_t )n * HS )== read (snap_b , (size_t )n * HS )); 156for (float * p : {key0 ,hid0 ,val0 ,hist_a ,snap_a ,q ,nrm ,g ,gd ,cv ,key1 ,hid1 ,val1 ,hist_b ,snap_b ,qn ,gated , 157gate }) 158cudaFree (p ); 159 } 160} 161 162}// namespace 163 164int main () { 165cudaDeviceProp prop {}; 166int dev = 0 ; 167ck (cudaGetDevice (& dev ),"device" ); 168ck (cudaGetDeviceProperties (& prop ,dev ),"props" ); 169 std::printf ("verify_batch_parity: %s (sm_%d%d)\n" ,prop .name ,prop .major ,prop .minor ); 170cudaStream_t s = nullptr ; 171ck (cudaStreamCreateWithFlags (& s ,cudaStreamNonBlocking ),"stream" ); 172gr_post (s ); 173// the engine's own rule for the batched PLE post-ops (prefill.cpp ple_batch_enabled, same toolkit arguments): off for a CUDA 12.8 build on a 12.0 card unless STRATA_PLE_BATCH says otherwise 174#if defined(CUDART_VERSION )&& !defined(STRATA_USE_HIP )&& !defined(__HIPCC__ ) 175constexpr int kRt = CUDART_VERSION ; 176constexpr bool kHip = false; 177#else 178constexpr int kRt = 0 ;// a HIP build has no CUDA toolkit version: the engine passes 0 and hip = true 179constexpr bool kHip = true; 180#endif 181if (strata::ple_batch_resolve (std::getenv ("STRATA_PLE_BATCH" ),kRt ,kHip ,prop .major ,prop .minor )) 182ple (s ); 183else 184 std::printf ("SKIP native_ple_postops_batch_snap: built with CUDA 12.8 for RTX 50: the batched PLE post-ops are switched off in this build " 185"(STRATA_PLE_BATCH=1 runs the step anyway)\n" ); 186 std::printf ("verify_batch_parity: %s\n" ,g_fail ?"FAIL" :"PASS" ); 187return g_fail ?1 :0 ; 188}