okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
db137d917
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#define DPCT_PROFILING_ENABLED 8#include <sycl/sycl.hpp> 9#include <dpct/dpct.hpp> 10#include "strata/kernels/gr.hpp" 11#include "strata/core/geometry.hpp" 12 13#include <cstdint> 14#include <cstdio> 15#include <cstdlib> 16#include <cstring> 17#include <random> 18#include <vector> 19 20namespace k= strata::kernels; 21 22namespace { 23void ck (dpct::err0 e ,const char * w ) { 24} 25template < typename T > 26T * up (const std::vector < T >& h ) { 27void * p = nullptr ; 28ck (DPCT_CHECK_ERROR ( 29p = (void * )sycl::malloc_device (h .size ()* sizeof (T )+ 64 , 30 dpct::get_in_order_queue ())), 31"malloc" ); 32/* 33DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, assuming in 34the original code the source host memory is pageable memory. If the memory 35is not pageable, call wait() on event return by memcpy API to ensure 36synchronization behavior. 37*/ 38ck (DPCT_CHECK_ERROR ((dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()).memcpy ( 39p ,h .data (),h .size ()* sizeof (T )).wait ()), 40"h2d" ); 41return (T * )p ; 42} 43template < typename T > 44T * dev (size_t n ) { 45void * p = nullptr ; 46ck (DPCT_CHECK_ERROR (p = (void * )sycl::malloc_device ( 47n * sizeof (T )+ 64 , dpct::get_in_order_queue ())), 48"malloc" ); 49ck (DPCT_CHECK_ERROR ( 50 (dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()).memset (p ,0 ,n * sizeof (T )+ 64 ).wait ()), 51"memset" ); 52return (T * )p ; 53} 54template < typename T > 55std::vector < T > down (const T * d ,size_t n ) { 56 std::vector < T > h (n ); 57ck (DPCT_CHECK_ERROR ((dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()) 58 .memcpy (h .data (),d ,n * sizeof (T )) 59 .wait ()), 60"d2h" ); 61return h ; 62} 63uint16_t bf16_of (float f ) { 64uint32_t u ; 65 std::memcpy (& u ,& f ,4 ); 66return (uint16_t ) (u >>16 ); 67} 68}// namespace 69 70int main () { 71const long long n_embd = strata::core::kCompiledGeometry .n_embd ,hc = strata::core::kCompiledGeometry .hc ,hc_lr = strata::core::kCompiledGeometry .hc_lr ,hc_dim = hc * n_embd ;// the compiled shape (Q4-SYCL) 72const float eps = 1e-6f ; 73 std::mt19937 rng (783 ); 74 std::normal_distribution < float > gauss (0.f ,1.f ); 75 k::gr_set_native_mmvf (true); 76 77 std::vector < float > w_norm ((size_t )hc_dim ); 78for (auto & x :w_norm )x = 1.0f + 0.1f * gauss (rng ); 79 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 )); 80for (auto & x :w_down )x = bf16_of (gauss (rng )* 0.05f ); 81for (auto & x :w_up )x = bf16_of (gauss (rng )* 0.05f ); 82for (auto & x :w_inj )x = bf16_of (gauss (rng )* 0.02f ); 83float * d_norm = up (w_norm ); 84uint16_t * d_down = up (w_down ),* d_up = up (w_up ),* d_inj = up (w_inj ); 85 86const k::GrShapes sh {n_embd ,hc ,hc_lr }; 87void * ws_raw = nullptr ; 88ck (DPCT_CHECK_ERROR ( 89ws_raw = (void * )sycl::malloc_device (k::gr_workspace_bytes (sh ), 90 dpct::get_in_order_queue ())), 91"ws" ); 92 k::GrWorkspace ws ; 93 k::gr_workspace_init (sh ,ws_raw ,ws ); 94 95int cases = 0 ,bad = 0 ; 96for (int T = 1 ;T <=8 ;++ T ) { 97 std::vector < float > R ((size_t )T * hc_dim ); 98for (int t = 0 ;t < T ;++ t ) 99for (long long c = 0 ;c < hc ;++ c ) 100for (long long d = 0 ;d < n_embd ;++ d )R [(size_t )t * hc_dim + c * n_embd + d ]= gauss (rng )* (float ) (1 << (2 * c )); 101float * d_R = up (R ); 102float * d_xn = dev < float > ((size_t )T * hc_dim ); 103float * d_lo = dev < float > ((size_t )T * hc_lr ); 104float * d_g = dev < float > ((size_t )T * hc_dim ); 105for (int with_inj = 0 ;with_inj < 2 ;++ with_inj ) { 106float * d_mixed_m = dev < float > ((size_t )T * n_embd ); 107float * d_mixed_s = dev < float > ((size_t )T * n_embd ); 108float * d_inject_m = dev < float > ((size_t )T * hc ); 109float * d_inject_s = dev < float > ((size_t )T * hc ); 110const uint16_t * wi = with_inj ?d_inj :nullptr ; 111 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 , 112with_inj ?d_inject_m :nullptr ,T ,nullptr ); 113for (int t = 0 ;t < T ;++ t ) 114 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 , 115with_inj ?d_inject_s + (size_t )t * hc :nullptr ,nullptr ); 116ck (DPCT_CHECK_ERROR ( 117 dpct::get_current_device ().queues_wait_and_throw ()), 118"sync" ); 119bool ok = std::memcmp (down (d_mixed_m , (size_t )T * n_embd ).data (),down (d_mixed_s , (size_t )T * n_embd ).data (), 120 (size_t )T * n_embd * 4 )== 0 ; 121if (with_inj ) 122ok = ok && std::memcmp (down (d_inject_m , (size_t )T * hc ).data (),down (d_inject_s , (size_t )T * hc ).data (), 123 (size_t )T * hc * 4 )== 0 ; 124++ cases ; 125if (!ok ) { 126 std::printf (" *** gr_read_multi T=%d %s differs from gr_read ***\n" ,T ,with_inj ?"with injection" :"final mixer" ); 127++ bad ; 128 } 129 sycl::free (d_mixed_m , dpct::get_in_order_queue ()); 130 sycl::free (d_mixed_s , dpct::get_in_order_queue ()); 131 sycl::free (d_inject_m , dpct::get_in_order_queue ()); 132 sycl::free (d_inject_s , dpct::get_in_order_queue ()); 133 } 134 135// the write: R_out[t] = R[t] + block_out[t] * 2 sigmoid(inject[t] / hc) 136 std::vector < float > bo ((size_t )T * n_embd ),inj ((size_t )T * hc ); 137for (auto & x :bo )x = gauss (rng ); 138for (auto & x :inj )x = gauss (rng )* 3.0f ; 139float * d_bo = up (bo ); 140float * d_in = up (inj ); 141for (int inplace = 0 ;inplace < 2 ;++ inplace ) { 142float * d_Rm = up (R ); 143float * d_Rs = up (R ); 144float * d_om = inplace ?d_Rm :dev < float > ((size_t )T * hc_dim ); 145float * d_os = inplace ?d_Rs :dev < float > ((size_t )T * hc_dim ); 146 k::gr_write_multi (d_Rm ,d_bo ,d_in ,sh ,d_om ,T ,nullptr ); 147for (int t = 0 ;t < T ;++ t ) 148 k::gr_write (d_Rs + (size_t )t * hc_dim ,d_bo + (size_t )t * n_embd ,d_in + (size_t )t * hc ,sh , 149d_os + (size_t )t * hc_dim ,nullptr ); 150ck (DPCT_CHECK_ERROR ( 151 dpct::get_current_device ().queues_wait_and_throw ()), 152"sync" ); 153const bool ok = std::memcmp (down (d_om , (size_t )T * hc_dim ).data (),down (d_os , (size_t )T * hc_dim ).data (), 154 (size_t )T * hc_dim * 4 )== 0 ; 155++ cases ; 156if (!ok ) { 157 std::printf (" *** gr_write_multi T=%d %s differs from gr_write ***\n" ,T ,inplace ?"in place" :"out of place" ); 158++ bad ; 159 } 160 sycl::free (d_Rm , dpct::get_in_order_queue ()); 161 sycl::free (d_Rs , dpct::get_in_order_queue ()); 162if (!inplace ) { 163 sycl::free (d_om , dpct::get_in_order_queue ()); 164 sycl::free (d_os , dpct::get_in_order_queue ()); 165 } 166 } 167 sycl::free (d_R , dpct::get_in_order_queue ()); 168 sycl::free (d_xn , dpct::get_in_order_queue ()); 169 sycl::free (d_lo , dpct::get_in_order_queue ()); 170 sycl::free (d_g , dpct::get_in_order_queue ()); 171 sycl::free (d_bo , dpct::get_in_order_queue ()); 172 sycl::free (d_in , dpct::get_in_order_queue ()); 173 } 174 std::printf ("gr_multi: %d cases, %d failures\n" ,cases ,bad ); 175if (bad )return 1 ; 176 std::printf ("gr_multi_parity OK\n" ); 177return 0 ; 178}