okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
5af104b51
main
1// src/kernels/kv_steps_parity.cpp - the batched K/V append (#783 PR-d): kv_append_q8_steps / kv_append_q4_steps for 2// n = 1..8 cells in one launch against n calls of the single-cell append, memcmp on every pool byte. 3// 4// Cells land at scattered positions through a non-identity page table; the K/V rows are random with small, large and 5// all-zero groups. Three layouts: INT8 K and V, Q4_0 K and V, and the K8V4 hybrid's folded call (the K pool passed as 6// both halves, one plane). GPU, synthetic, no model. 7#define DPCT_PROFILING_ENABLED 8#include <sycl/sycl.hpp> 9#include <dpct/dpct.hpp> 10#include "strata/kernels/kv_q4.hpp" 11#include "strata/kernels/kv_q8.hpp" 12#include "strata/kernels/qsa.hpp" 13 14#include <algorithm> 15#include <cstdio> 16#include <cstdlib> 17#include <cstring> 18#include <random> 19#include <vector> 20 21namespace k= strata::kernels; 22 23namespace { 24int g_fail = 0 ; 25void ck (dpct::err0 e ,const char * w ) { 26} 27template < typename T > 28T * dalloc (size_t n ) { 29T * p = nullptr ; 30ck (DPCT_CHECK_ERROR (p = (T * )sycl::malloc_device ( 31n * sizeof (T )+ 64 , dpct::get_in_order_queue ())), 32"malloc" ); 33ck (DPCT_CHECK_ERROR ( 34 (dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()).memset (p ,0 ,n * sizeof (T )+ 64 ).wait ()), 35"memset" ); 36return p ; 37} 38template < typename T > 39std::vector < T > fetch (const T * d ,size_t n ) { 40 std::vector < T > h (n ); 41ck (DPCT_CHECK_ERROR ((dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()) 42 .memcpy (h .data (),d ,n * sizeof (T )) 43 .wait ()), 44"d2h" ); 45return h ; 46} 47template < typename T > 48void zero (T * d ,size_t n ) { 49ck (DPCT_CHECK_ERROR ( 50 (dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()).memset (d ,0 ,n * sizeof (T )+ 64 ).wait ()), 51"zero" ); 52} 53bool same (const void * a ,const void * b ,size_t bytes ) {return std::memcmp (a ,b ,bytes )== 0 ; } 54}// namespace 55 56int main () { 57 k::QsaShapes s = k::qsa_real_shapes (); 58s .page_size = 64 ; 59const int H = (int )s .n_head_kv ,D = (int )s .head_dim ,P = (int )s .page_size ,G = D / k::KV_Q8_GROUP ; 60const int pages = 8 ,cells = pages * P ; 61 std::mt19937 rng (783 ); 62 std::normal_distribution < float > nd (0.f ,1.f ); 63 std::vector < int32_t > table (pages ); 64for (int i = 0 ;i < pages ;++ i )table [i ]= (i * 5 + 3 ) %pages ; 65int32_t * d_table = dalloc < int32_t > (pages ); 66/* 67DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, assuming in 68the original code the source host memory is pageable memory. If the memory 69is not pageable, call wait() on event return by memcpy API to ensure 70synchronization behavior. 71*/ 72ck (DPCT_CHECK_ERROR ( 73 (dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()).memcpy (d_table ,table .data (),pages * 4 ).wait ()), 74"table" ); 75 76const size_t q8n = (size_t )cells * H * D ,q8s = (size_t )cells * H * G ,q4n = (size_t )cells * H * D ;// q4: <= 144 B per cell-head 77int8_t * kq1 = dalloc < int8_t > (q8n ),* vq1 = dalloc < int8_t > (q8n ),* kq2 = dalloc < int8_t > (q8n ),* vq2 = dalloc < int8_t > (q8n ); 78uint16_t * ks1 = dalloc < uint16_t > (q8s ),* vs1 = dalloc < uint16_t > (q8s ),* ks2 = dalloc < uint16_t > (q8s ),* vs2 = dalloc < uint16_t > (q8s ); 79uint8_t * k41 = dalloc < uint8_t > (q4n ),* v41 = dalloc < uint8_t > (q4n ),* k42 = dalloc < uint8_t > (q4n ),* v42 = dalloc < uint8_t > (q4n ); 80uint8_t * h41 = dalloc < uint8_t > (q4n ),* h42 = dalloc < uint8_t > (q4n ); 81int8_t * hq1 = dalloc < int8_t > (q8n ),* hq2 = dalloc < int8_t > (q8n ); 82uint16_t * hs1 = dalloc < uint16_t > (q8s ),* hs2 = dalloc < uint16_t > (q8s ); 83 84int total_cases = 0 ; 85for (int n = 1 ;n <=8 ;++ n ) { 86for (int round = 0 ;round < 3 ;++ round ) { 87zero (kq1 ,q8n );zero (vq1 ,q8n );zero (kq2 ,q8n );zero (vq2 ,q8n ); 88zero (ks1 ,q8s );zero (vs1 ,q8s );zero (ks2 ,q8s );zero (vs2 ,q8s ); 89zero (k41 ,q4n );zero (v41 ,q4n );zero (k42 ,q4n );zero (v42 ,q4n );zero (h41 ,q4n );zero (h42 ,q4n ); 90zero (hq1 ,q8n );zero (hq2 ,q8n );zero (hs1 ,q8s );zero (hs2 ,q8s ); 91 std::vector < int > positions (cells ); 92for (int i = 0 ;i < cells ;++ i )positions [i ]= i ; 93 std::shuffle (positions .begin (),positions .end (),rng ); 94 std::vector < float > hk ((size_t )n * H * D ),hv (hk .size ()); 95for (int t = 0 ;t < n ;++ t ) { 96const float scale = (t %3 == 0 ) ?1e-3f : (t %5 == 0 ?40.f :1.f ); 97for (int i = 0 ;i < H * D ;++ i ) { 98hk [(size_t )t * H * D + i ]= nd (rng )* scale ; 99hv [(size_t )t * H * D + i ]= nd (rng )* scale ; 100 } 101if (t %4 == 1 ) std::fill (hk .begin ()+ (size_t )t * H * D ,hk .begin ()+ (size_t )t * H * D + 64 ,0.f ); 102 } 103 std::vector < int32_t > hstep ((size_t )n * k::kStepCount ,0 ); 104for (int t = 0 ;t < n ;++ t ) { 105hstep [(size_t )t * k::kStepCount + k::kStepPos ]= positions [t ]; 106hstep [(size_t )t * k::kStepCount + 1 ]= positions [t ]+ 1 ; 107 } 108float * d_k = dalloc < float > (hk .size ()),* d_v = dalloc < float > (hv .size ()); 109int32_t * d_step = dalloc < int32_t > (hstep .size ()); 110/* 111DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, 112assuming in the original code the source host memory is pageable 113memory. If the memory is not pageable, call wait() on event return 114by memcpy API to ensure synchronization behavior. 115*/ 116ck (DPCT_CHECK_ERROR ((dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()).memcpy ( 117d_k ,hk .data (),hk .size ()* 4 ).wait ()), 118"k" ); 119/* 120DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, 121assuming in the original code the source host memory is pageable 122memory. If the memory is not pageable, call wait() on event return 123by memcpy API to ensure synchronization behavior. 124*/ 125ck (DPCT_CHECK_ERROR ((dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()).memcpy ( 126d_v ,hv .data (),hv .size ()* 4 ).wait ()), 127"v" ); 128/* 129DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, 130assuming in the original code the source host memory is pageable 131memory. If the memory is not pageable, call wait() on event return 132by memcpy API to ensure synchronization behavior. 133*/ 134ck (DPCT_CHECK_ERROR ((dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()).memcpy ( 135d_step ,hstep .data (),hstep .size ()* 4 ).wait ()), 136"step" ); 137 138for (int t = 0 ;t < n ;++ t ) { 139const int32_t * st = d_step + (size_t )t * k::kStepCount ; 140const float * kc = d_k + (size_t )t * H * D ; 141const float * vc = d_v + (size_t )t * H * D ; 142 k::kv_append_q8_step (kq1 ,vq1 ,ks1 ,vs1 ,d_table ,st ,kc ,vc ,s ,nullptr ); 143 k::kv_append_q4_step (k41 ,v41 ,d_table ,st ,kc ,vc ,s ,nullptr ); 144 k::kv_append_q8_step (hq1 ,hq1 ,hs1 ,hs1 ,d_table ,st ,kc ,kc ,s ,nullptr );// the hybrid's folded K call 145 k::kv_append_q4_step (h41 ,h41 ,d_table ,st ,vc ,vc ,s ,nullptr );// ... and its folded V call 146 } 147 k::kv_append_q8_steps (kq2 ,vq2 ,ks2 ,vs2 ,d_table ,d_step , k::kStepCount ,d_k ,d_v ,H * D ,n ,s ,nullptr ,nullptr ); 148 k::kv_append_q4_steps (k42 ,v42 ,d_table ,d_step , k::kStepCount ,n ,d_k ,d_v ,s ,nullptr ); 149 k::kv_append_q8_steps (hq2 ,hq2 ,hs2 ,hs2 ,d_table ,d_step , k::kStepCount ,d_k ,d_k ,H * D ,n ,s ,nullptr ,nullptr ); 150 k::kv_append_q4_steps (h42 ,h42 ,d_table ,d_step , k::kStepCount ,n ,d_v ,d_v ,s ,nullptr ); 151ck (DPCT_CHECK_ERROR ( 152 dpct::get_current_device ().queues_wait_and_throw ()), 153"sync" ); 154 155struct Chk {const char * name ;bool ok ; }; 156const Chk chks []= { 157 {"INT8 K/V codes" ,same (fetch (kq1 ,q8n ).data (),fetch (kq2 ,q8n ).data (),q8n )&& same (fetch (vq1 ,q8n ).data (),fetch (vq2 ,q8n ).data (),q8n )}, 158 {"INT8 K/V scales" ,same (fetch (ks1 ,q8s ).data (),fetch (ks2 ,q8s ).data (),q8s * 2 )&& same (fetch (vs1 ,q8s ).data (),fetch (vs2 ,q8s ).data (),q8s * 2 )}, 159 {"Q4_0 K/V" ,same (fetch (k41 ,q4n ).data (),fetch (k42 ,q4n ).data (),q4n )&& same (fetch (v41 ,q4n ).data (),fetch (v42 ,q4n ).data (),q4n )}, 160 {"hybrid K (folded INT8)" ,same (fetch (hq1 ,q8n ).data (),fetch (hq2 ,q8n ).data (),q8n )&& same (fetch (hs1 ,q8s ).data (),fetch (hs2 ,q8s ).data (),q8s * 2 )}, 161 {"hybrid V (folded Q4_0)" ,same (fetch (h41 ,q4n ).data (),fetch (h42 ,q4n ).data (),q4n )}, 162 }; 163for (const Chk & c :chks ) { 164++ total_cases ; 165if (!c .ok ) { 166 std::printf (" *** n=%d round %d: %s differ from the per-cell appends ***\n" ,n ,round ,c .name ); 167++ g_fail ; 168 } 169 } 170 sycl::free (d_k , dpct::get_in_order_queue ()); 171 sycl::free (d_v , dpct::get_in_order_queue ()); 172 sycl::free (d_step , dpct::get_in_order_queue ()); 173 } 174 } 175 std::printf ("kv_steps: %d checks, %d failures\n" ,total_cases ,g_fail ); 176if (g_fail )return 1 ; 177 std::printf ("kv_steps_parity OK\n" ); 178return 0 ; 179}