okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
104a8485e
main
1// src/kernels/gr_multi_parity.cpp - the multi-token hyper-connection read and write (#783 PR-g: gr_read_multi, 2// gr_write_multi and the exact-T BF16 projections under them) against the single-token calls, memcmp. 3// 4// Real geometry (n_embd 2560, hc 4, hc_lr 320), the pinned native MMVF path, every T = 1..8, with and without an 5// injection weight (a layer read vs the final mixer), and the write both out of place and in place (R_out == R). 6// Each output of the multi call must equal the token-by-token one bit for bit. GPU, synthetic, no model. 7#include "strata/kernels/gr.hpp" 8 9#include <cuda_runtime.h> 10 11#include <cstdint> 12#include <cstdio> 13#include <cstdlib> 14#include <cstring> 15#include <random> 16#include <vector> 17 18namespace k= strata::kernels; 19 20namespace { 21void ck (cudaError_t e ,const char * w ) { 22if (e != cudaSuccess ) { 23 std::fprintf (stderr ,"%s: %s\n" ,w ,cudaGetErrorString (e )); 24 std::exit (2 ); 25 } 26} 27template < typename T > 28T * up (const std::vector < T >& h ) { 29void * p = nullptr ; 30ck (cudaMalloc (& p ,h .size ()* sizeof (T )+ 64 ),"malloc" ); 31ck (cudaMemcpy (p ,h .data (),h .size ()* sizeof (T ),cudaMemcpyHostToDevice ),"h2d" ); 32return (T * )p ; 33} 34template < typename T > 35T * dev (size_t n ) { 36void * p = nullptr ; 37ck (cudaMalloc (& p ,n * sizeof (T )+ 64 ),"malloc" ); 38ck (cudaMemset (p ,0 ,n * sizeof (T )+ 64 ),"memset" ); 39return (T * )p ; 40} 41template < typename T > 42std::vector < T > down (const T * d ,size_t n ) { 43 std::vector < T > h (n ); 44ck (cudaMemcpy (h .data (),d ,n * sizeof (T ),cudaMemcpyDeviceToHost ),"d2h" ); 45return h ; 46} 47uint16_t bf16_of (float f ) { 48uint32_t u ; 49 std::memcpy (& u ,& f ,4 ); 50return (uint16_t ) (u >>16 ); 51} 52}// namespace 53 54int main () { 55const long long n_embd = 2560 ,hc = 4 ,hc_lr = 320 ,hc_dim = hc * n_embd ; 56const float eps = 1e-6f ; 57 std::mt19937 rng (783 ); 58 std::normal_distribution < float > gauss (0.f ,1.f ); 59 k::gr_set_native_mmvf (true); 60 61 std::vector < float > w_norm ((size_t )hc_dim ); 62for (auto & x :w_norm )x = 1.0f + 0.1f * gauss (rng ); 63 std::vector < uint16_t > w_down ((size_t ) (hc_lr * hc_dim )),w_up ((size_t ) (hc_dim * hc_lr )),w_inj ((size_t ) (hc * hc_dim )); 64for (auto & x :w_down )x = bf16_of (gauss (rng )* 0.05f ); 65for (auto & x :w_up )x = bf16_of (gauss (rng )* 0.05f ); 66for (auto & x :w_inj )x = bf16_of (gauss (rng )* 0.02f ); 67float * d_norm = up (w_norm ); 68uint16_t * d_down = up (w_down ),* d_up = up (w_up ),* d_inj = up (w_inj ); 69 70const k::GrShapes sh {n_embd ,hc ,hc_lr }; 71void * ws_raw = nullptr ; 72ck (cudaMalloc (& ws_raw , k::gr_workspace_bytes (sh )),"ws" ); 73 k::GrWorkspace ws ; 74 k::gr_workspace_init (sh ,ws_raw ,ws ); 75 76int cases = 0 ,bad = 0 ; 77for (int T = 1 ;T <=8 ;++ T ) { 78 std::vector < float > R ((size_t )T * hc_dim ); 79for (int t = 0 ;t < T ;++ t ) 80for (long long c = 0 ;c < hc ;++ c ) 81for (long long d = 0 ;d < n_embd ;++ d )R [(size_t )t * hc_dim + c * n_embd + d ]= gauss (rng )* (float ) (1 << (2 * c )); 82float * d_R = up (R ); 83float * d_xn = dev < float > ((size_t )T * hc_dim ); 84float * d_lo = dev < float > ((size_t )T * hc_lr ); 85float * d_g = dev < float > ((size_t )T * hc_dim ); 86for (int with_inj = 0 ;with_inj < 2 ;++ with_inj ) { 87float * d_mixed_m = dev < float > ((size_t )T * n_embd ); 88float * d_mixed_s = dev < float > ((size_t )T * n_embd ); 89float * d_inject_m = dev < float > ((size_t )T * hc ); 90float * d_inject_s = dev < float > ((size_t )T * hc ); 91const uint16_t * wi = with_inj ?d_inj :nullptr ; 92 k::gr_read_multi (d_R ,d_norm ,d_down ,d_up ,wi ,eps ,sh ,ws ,d_xn ,d_lo ,d_g ,d_mixed_m , 93with_inj ?d_inject_m :nullptr ,T ,nullptr ); 94for (int t = 0 ;t < T ;++ t ) 95 k::gr_read (d_R + (size_t )t * hc_dim ,d_norm ,d_down ,d_up ,wi ,eps ,sh ,ws ,d_mixed_s + (size_t )t * n_embd , 96with_inj ?d_inject_s + (size_t )t * hc :nullptr ,nullptr ); 97ck (cudaDeviceSynchronize (),"sync" ); 98bool ok = std::memcmp (down (d_mixed_m , (size_t )T * n_embd ).data (),down (d_mixed_s , (size_t )T * n_embd ).data (), 99 (size_t )T * n_embd * 4 )== 0 ; 100if (with_inj ) 101ok = ok && std::memcmp (down (d_inject_m , (size_t )T * hc ).data (),down (d_inject_s , (size_t )T * hc ).data (), 102 (size_t )T * hc * 4 )== 0 ; 103++ cases ; 104if (!ok ) { 105 std::printf (" *** gr_read_multi T=%d %s differs from gr_read ***\n" ,T ,with_inj ?"with injection" :"final mixer" ); 106++ bad ; 107 } 108cudaFree (d_mixed_m );cudaFree (d_mixed_s );cudaFree (d_inject_m );cudaFree (d_inject_s ); 109 } 110 111// the write: R_out[t] = R[t] + block_out[t] * 2 sigmoid(inject[t] / hc) 112 std::vector < float > bo ((size_t )T * n_embd ),inj ((size_t )T * hc ); 113for (auto & x :bo )x = gauss (rng ); 114for (auto & x :inj )x = gauss (rng )* 3.0f ; 115float * d_bo = up (bo ); 116float * d_in = up (inj ); 117for (int inplace = 0 ;inplace < 2 ;++ inplace ) { 118float * d_Rm = up (R ); 119float * d_Rs = up (R ); 120float * d_om = inplace ?d_Rm :dev < float > ((size_t )T * hc_dim ); 121float * d_os = inplace ?d_Rs :dev < float > ((size_t )T * hc_dim ); 122 k::gr_write_multi (d_Rm ,d_bo ,d_in ,sh ,d_om ,T ,nullptr ); 123for (int t = 0 ;t < T ;++ t ) 124 k::gr_write (d_Rs + (size_t )t * hc_dim ,d_bo + (size_t )t * n_embd ,d_in + (size_t )t * hc ,sh , 125d_os + (size_t )t * hc_dim ,nullptr ); 126ck (cudaDeviceSynchronize (),"sync" ); 127const bool ok = std::memcmp (down (d_om , (size_t )T * hc_dim ).data (),down (d_os , (size_t )T * hc_dim ).data (), 128 (size_t )T * hc_dim * 4 )== 0 ; 129++ cases ; 130if (!ok ) { 131 std::printf (" *** gr_write_multi T=%d %s differs from gr_write ***\n" ,T ,inplace ?"in place" :"out of place" ); 132++ bad ; 133 } 134cudaFree (d_Rm );cudaFree (d_Rs ); 135if (!inplace ) {cudaFree (d_om );cudaFree (d_os ); } 136 } 137cudaFree (d_R );cudaFree (d_xn );cudaFree (d_lo );cudaFree (d_g );cudaFree (d_bo );cudaFree (d_in ); 138 } 139 std::printf ("gr_multi: %d cases, %d failures\n" ,cases ,bad ); 140if (bad )return 1 ; 141 std::printf ("gr_multi_parity OK\n" ); 142return 0 ; 143}