okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
564d0abeb
main
1// sycl/src/kernels/tq_sycl_parity.cpp - the Gyro trellis experts (TQ2_T 144, TQK6 145, TQK7 146, Hadamard-folded) on the 2// SYCL kernels, against real tensors of a Gyro GGUF. 3// 4// tq_sycl_parity <pack-dir> <model.gguf> [layer ...] 5// tq_sycl_parity --synthetic [--hadamard] GU/DOWN ... (random trellis codes and scales, e.g. 145/146 144/144) 6// tq_sycl_parity --bench GU/DOWN [groups [entries-per-group [iterations]]] (native_expert_grouped on distinct experts in VRAM) 7// 8// For one expert of each layer (the gate/up/down rows read from the GGUF, the Hadamard signs and block from the pack): 9// (a) float reference: fp16-codebook weights, x and h rotated in double, SwiGLU in double. 10// (b) the CPU engine path (native_quant_act, native_gu_rows, native_quant_h, native_down_rows). 11// (c) the GPU path (hadamard_rows, quantize_q8_1_rows, native_expert_grouped) with 3 entries in one group. 12// Checks: (c) against (a) and (b) within the bound every expert type gets (relative L1 < 3e-2); the GPU Hadamard rotation 13// against the CPU's; the gate/up rows and the down rows of (c) BIT FOR BIT against a host emulation of the kernels' 14// integer and float operations (the int8 codebook, the q8_1 blocks and the h the device produced are read back, the 15// lane-strided sums and the xor tree are replayed); the prompt-path dequantizers (iq_dequant_f32 / _f16) against the 16// fp16 codebook, bit for bit. Exit 0 only if all hold. 17#include "strata/artifact/gguf_reader.hpp" 18#include "strata/kernels/cpu/native_expert.hpp" 19#include "strata/kernels/cpu/expert_layout.hpp" 20#include "strata/kernels/cpu/hadamard.hpp" 21#include "strata/kernels/cpu/tq_cpu.hpp" 22#include "strata/kernels/iq_kernels.hpp" 23#include "ggml-common-tq.h" 24#include "../../../src/kernels/cpu/tq_states.hpp" 25#include "ggml.h" 26 27#include <sycl/sycl.hpp> 28#include <dpct/dpct.hpp> 29 30#include <algorithm> 31#include <chrono> 32#include <cmath> 33#include <cstdio> 34#include <cstdlib> 35#include <cstring> 36#include <random> 37#include <string> 38#include <vector> 39 40#define __device__ 41#include "../../../src/kernels/cuda/tq_lut_i8.cuh" 42#undef __device__ 43 44namespace cpu= strata::kernels::cpu; 45namespace { 46constexpr int NT = 3 ; 47constexpr int64_t H = 2560 ,FF = 640 ; 48constexpr int LN = 8 ;// STRATA_EXPERT_LANES 49 50double rel (const std::vector < float >& a ,const std::vector < float >& b ) { 51double n = 0 ,d = 0 ; 52for (size_t i = 0 ;i < a .size ();++ i ) {n += std::fabs ((double )a [i ]- b [i ]);d += std::fabs ((double )b [i ]); } 53return n / (d + 1e-30 ); 54} 55float h2f (uint16_t b ) {return cpu::tq_detail::tq_half_to_float (b ); } 56int kbits (int t ) {return t == 144 ?8 :t == 145 ?6 :7 ; } 57size_t bsize (int t ) {return 2 + 4 * (size_t )kbits (t ); } 58 59// the 32 trellis states of block b (qs after the fp16 scale) 60void states (int t ,const uint8_t * blk ,uint32_t * st ) { 61if (t == 144 ) cpu::tq_detail::tq_states < 8 > (blk + 2 ,st ); 62else if (t == 145 ) cpu::tq_detail::tq_states < 6 > (blk + 2 ,st ); 63else cpu::tq_detail::tq_states < 7 > (blk + 2 ,st ); 64} 65// float dequantization of one row of n values with the fp16 codebook 66void dq_row (int t ,const uint8_t * row ,int64_t n ,float * y ) { 67for (int64_t b = 0 ;b < n /128 ;++ b ) { 68const uint8_t * blk = row + b * bsize (t ); 69const float d = h2f ((uint16_t ) (blk [0 ] | (blk [1 ] <<8 ))); 70uint32_t st [32 ]; 71states (t ,blk ,st ); 72for (int s = 0 ;s < 32 ;++ s ) { 73const uint32_t x = (st [s ]& TQ_STATE_MASK )* 0x9e3779b1u ; 74const uint32_t pa = x >>21 ,pb = (x >>10 )& 2047u ; 75y [b * 128 + 4 * s + 0 ]= d * h2f (tq_lut_f16 [2 * pa ]); 76y [b * 128 + 4 * s + 1 ]= d * h2f (tq_lut_f16 [2 * pa + 1 ]); 77y [b * 128 + 4 * s + 2 ]= d * h2f (tq_lut_f16 [2 * pb ]); 78y [b * 128 + 4 * s + 3 ]= d * h2f (tq_lut_f16 [2 * pb + 1 ]); 79 } 80 } 81} 82// host replay of the kernels' dot of one row against one q8_1 row (36-byte blocks): lane `sub` of LN accumulates calls 83// sub, sub+LN, ... in order, then the xor tree (o = LN/2 .. 1) 84float emu_dot (int t ,const uint8_t * row ,const uint8_t * q8 ,int64_t n ) { 85const int nb = (int ) (n /128 ); 86float s [LN ]= {}; 87for (int sub = 0 ;sub < LN ;++ sub ) 88for (int k = sub ;k < nb * 8 ;k += LN ) { 89const int kbx = k /8 ,lane = k %8 ; 90const uint8_t * blk = row + (size_t )kbx * bsize (t ); 91uint32_t st [32 ]; 92states (t ,blk ,st ); 93int sumi = 0 ; 94const uint8_t * b8 = q8 + (size_t ) (kbx * 4 + lane /2 )* 36 ; 95for (int i = 0 ;i < 4 ;++ i ) { 96const uint32_t x = (st [4 * lane + i ]& TQ_STATE_MASK )* 0x9e3779b1u ; 97const uint16_t p0 = tq_lut_i8 [x >>21 ],p1 = tq_lut_i8 [(x >>10 )& 2047u ]; 98const int8_t w [4 ]= {(int8_t ) (p0 & 0xff ), (int8_t ) (p0 >>8 ), (int8_t ) (p1 & 0xff ), (int8_t ) (p1 >>8 )}; 99const int8_t * a = (const int8_t * ) (b8 + 4 + 16 * (lane & 1 )+ 4 * i ); 100for (int j = 0 ;j < 4 ;++ j )sumi += (int )w [j ]* (int )a [j ]; 101 } 102const uint16_t dsb = (uint16_t ) (b8 [0 ] | (b8 [1 ] <<8 )); 103const float d = h2f ((uint16_t ) (blk [0 ] | (blk [1 ] <<8 ))); 104volatile float tmp = h2f (dsb )* TQ_LUT_I8_SCALE ; 105volatile float v = d * tmp ; 106v = v * (float )sumi ; 107volatile float acc = s [sub ]; 108acc = acc + v ; 109s [sub ]= acc ; 110 } 111for (int o = LN /2 ;o > 0 ;o >>=1 ) { 112float nx [LN ]; 113for (int l = 0 ;l < LN ;++ l )nx [l ]= s [l ]+ s [l ^o ]; 114 std::memcpy (s ,nx ,sizeof s ); 115 } 116return s [0 ]; 117} 118 119template < class T > T * dmalloc (size_t n ) {return (T * ) sycl::malloc_device ((n ?n :1 )* sizeof (T ), dpct::get_in_order_queue ()); } 120template < class T > void to_dev (T * d ,const void * h ,size_t n ) { dpct::get_in_order_queue ().memcpy (d ,h ,n ).wait (); } 121template < class T > void from_dev (void * h ,const T * d ,size_t n ) { dpct::get_in_order_queue ().memcpy (h ,d ,n ).wait (); } 122 123int check_layer (const cpu::NativeFmt & f ,const std::vector < uint8_t >& blob ,int seed ,const std::string & label ) { 124int failures = 0 ; 125 sycl::queue * s = & dpct::get_in_order_queue (); 126// (a) float reference (rows dequantized with the fp16 codebook) 127 std::vector < float > G ((size_t )FF * H ),U ((size_t )FF * H ),D ((size_t )H * FF ); 128for (int64_t r = 0 ;r < FF ;++ r ) { 129dq_row (f .gu_type ,blob .data ()+ r * f .gu_row ,H ,G .data ()+ r * H ); 130dq_row (f .gu_type ,blob .data ()+ f .up_off + r * f .gu_row ,H ,U .data ()+ r * H ); 131 } 132for (int64_t r = 0 ;r < H ;++ r )dq_row (f .d_type ,blob .data ()+ f .down_off + r * f .d_row ,FF ,D .data ()+ r * FF ); 133 std::mt19937 rng (11 + seed ); 134 std::normal_distribution < float > nd (0.f ,1.f ); 135 std::vector < float > x ((size_t )NT * H ); 136for (auto & v :x )v = nd (rng ); 137 std::vector < float > xr (x ); 138if (f .had_gu )for (int k = 0 ;k < NT ;++ k ) cpu::hadamard_rotate (xr .data ()+ k * H , (int )H ,f .had_block ,f .gu_signs ); 139 std::vector < float > ref ((size_t )NT * H ),got_c ((size_t )NT * H ),got_g ((size_t )NT * H ); 140for (int k = 0 ;k < NT ;++ k ) { 141 std::vector < float > h (FF ); 142for (int64_t r = 0 ;r < FF ;++ r ) { 143double g = 0 ,u = 0 ; 144for (int64_t i = 0 ;i < H ;++ i ) {g += (double )G [r * H + i ]* xr [k * H + i ];u += (double )U [r * H + i ]* xr [k * H + i ]; } 145h [r ]= (float ) ((g / (1.0 + std::exp (- g )))* u ); 146 } 147if (f .had_d ) cpu::hadamard_rotate (h .data (), (int )FF ,f .had_block ,f .d_signs ); 148for (int64_t r = 0 ;r < H ;++ r ) { 149double o = 0 ; 150for (int64_t i = 0 ;i < FF ;++ i )o += (double )D [r * FF + i ]* h [i ]; 151ref [k * H + r ]= (float )o ; 152 } 153 } 154// (b) the CPU engine path 155 { 156 std::vector < std::vector < uint8_t >> act (NT , std::vector < uint8_t > (cpu::kNativeActBytes )); 157 std::vector < std::vector < uint8_t >> hq (NT , std::vector < uint8_t > (cpu::kNativeHBytes )); 158 std::vector < std::vector < float >> ff (NT , std::vector < float > (FF )); 159const void * a [NT ]; 160float * ffp [NT ]; 161const void * hp [NT ]; 162float * op [NT ]; 163for (int k = 0 ;k < NT ;++ k ) { 164 cpu::native_quant_act (f ,x .data ()+ k * H ,act [k ].data ()); 165a [k ]= act [k ].data (); 166ffp [k ]= ff [k ].data (); 167 } 168 cpu::native_gu_rows (f ,blob .data (),a ,NT ,ffp ,0 , (int )FF ); 169for (int k = 0 ;k < NT ;++ k ) { 170 cpu::native_quant_h (f ,ff [k ].data (),hq [k ].data ()); 171hp [k ]= hq [k ].data (); 172op [k ]= got_c .data ()+ k * H ; 173 } 174 cpu::native_down_rows (f ,blob .data (),hp ,NT ,op ,0 , (int )H ); 175 } 176// (c) the GPU: one group holding the NT entries 177auto L = strata::kernels::native_expert_layout (f .gu_type ,f .d_type ,H ,FF ); 178if (f .had_d ) {L .had_block = f .had_block ;L .d_signs = strata::kernels::hadamard_device_signs (f .d_signs , (int )FF ); } 179uint8_t * dblob = dmalloc < uint8_t > (blob .size ()); 180float * dx = dmalloc < float > (x .size ()); 181uint8_t * dxq = dmalloc < uint8_t > ((size_t )NT * H /32 * 36 ); 182const size_t scr_bytes = strata::kernels::native_expert_scratch_bytes (NT ,FF ); 183uint8_t * dscr = dmalloc < uint8_t > (scr_bytes ); 184float * dout = dmalloc < float > ((size_t )NT * H ); 185unsigned long long * dptr = dmalloc < unsigned long long > (1 ); 186int32_t * dstart = dmalloc < int32_t > (2 ),* dn = dmalloc < int32_t > (1 ),* ddst = dmalloc < int32_t > (NT ),* dtok = dmalloc < int32_t > (NT ); 187to_dev (dblob ,blob .data (),blob .size ()); 188to_dev (dx ,x .data (),x .size ()* 4 ); 189const unsigned long long p = (unsigned long long )dblob ; 190const int32_t st2 [2 ]= {0 ,NT },one = 1 ,idx [NT ]= {0 ,1 ,2 }; 191to_dev (dptr ,& p ,8 ); 192to_dev (dstart ,st2 ,8 ); 193to_dev (dn ,& one ,4 ); 194to_dev (ddst ,idx ,NT * 4 ); 195to_dev (dtok ,idx ,NT * 4 ); 196if (f .had_gu ) { 197 strata::kernels::hadamard_rows (dx ,dx ,NT ,H ,f .had_block , strata::kernels::hadamard_device_signs (f .gu_signs , (int )H ),s ); 198 std::vector < float > gx (x .size ()); 199s -> wait (); 200from_dev (gx .data (),dx ,gx .size ()* 4 ); 201const bool same = std::memcmp (gx .data (),xr .data (),gx .size ()* 4 )== 0 ; 202 std::printf (" gpu hadamard_rows vs cpu hadamard_rotate: %s (rel %.2e)\n" ,same ?"bitwise equal" :"differ" ,rel (gx ,xr )); 203if (!(rel (gx ,xr )< 1e-6 ))++ failures ; 204 std::vector < uint16_t > xh (x .size ()),xh_out (x .size ()); 205for (size_t i = 0 ;i < x .size ();++ i )xh [i ]= ggml_fp32_to_fp16 (x [i ]); 206uint16_t * dxh = dmalloc < uint16_t > (xh .size ()); 207to_dev (dxh ,xh .data (),xh .size ()* 2 ); 208 strata::kernels::hadamard_rows_f16 (dxh ,NT ,H ,f .had_block , strata::kernels::hadamard_device_signs (f .gu_signs , (int )H ),s ); 209s -> wait (); 210from_dev (xh_out .data (),dxh ,xh .size ()* 2 ); 211 sycl::free (dxh ,* s ); 212 std::vector < float > xf (x .size ()); 213for (size_t i = 0 ;i < xf .size ();++ i )xf [i ]= ggml_fp16_to_fp32 (xh_out [i ]); 214 std::printf (" gpu hadamard_rows_f16 vs cpu float rotation: rel %.2e\n" ,rel (xf ,xr )); 215if (!(rel (xf ,xr )< 2e-3 ))++ failures ; 216 } 217 strata::kernels::quantize_q8_1_rows (dx ,NT ,H ,dxq ,s ); 218 strata::kernels::native_expert_grouped (L ,dptr ,dstart ,dn ,ddst ,dtok ,1 ,NT ,dxq ,dscr ,dout ,s ); 219s -> wait (); 220from_dev (got_g .data (),dout ,got_g .size ()* 4 ); 221 {// bit-for-bit replay of the lane kernels 222 std::vector < uint8_t > hxq ((size_t )NT * H /32 * 36 ); 223from_dev (hxq .data (),dxq ,hxq .size ()); 224const size_t fb = (size_t )NT * FF * 4 ,fa = (fb + 255 )& ~(size_t )255 ; 225 std::vector < float > gate ((size_t )NT * FF ),up ((size_t )NT * FF ),hh ((size_t )NT * FF ); 226from_dev (gate .data (), (const float * )dscr ,gate .size ()* 4 ); 227from_dev (up .data (), (const float * ) (dscr + fa ),up .size ()* 4 ); 228from_dev (hh .data (), (const float * ) (dscr + 2 * fa ),hh .size ()* 4 ); 229 std::vector < uint8_t > hhq ((size_t )NT * FF /32 * 36 ); 230from_dev (hhq .data (),dscr + 3 * fa ,hhq .size ()); 231size_t gu_diff = 0 ,dn_diff = 0 ; 232for (int k = 0 ;k < NT ;++ k ) { 233for (int64_t r = 0 ;r < FF ;++ r ) { 234const float eg = emu_dot (f .gu_type ,blob .data ()+ r * f .gu_row ,hxq .data ()+ (size_t )k * H /32 * 36 ,H ); 235const float eu = emu_dot (f .gu_type ,blob .data ()+ f .up_off + r * f .gu_row ,hxq .data ()+ (size_t )k * H /32 * 36 ,H ); 236if (std::memcmp (& eg ,& gate [k * FF + r ],4 )!= 0 && gu_diff < 4 ) 237 std::printf (" [debug] gate k %d r %lld: device %g host replay %g (d of block 0: %g)\n" ,k , (long long )r , 238gate [k * FF + r ],eg ,h2f ((uint16_t ) (blob [r * f .gu_row ] | (blob [r * f .gu_row + 1 ] <<8 )))); 239gu_diff += std::memcmp (& eg ,& gate [k * FF + r ],4 )!= 0 ; 240gu_diff += std::memcmp (& eu ,& up [k * FF + r ],4 )!= 0 ; 241 } 242for (int64_t r = 0 ;r < H ;++ r ) { 243const float ed = emu_dot (f .d_type ,blob .data ()+ f .down_off + r * f .d_row ,hhq .data ()+ (size_t )k * FF /32 * 36 ,FF ); 244dn_diff += std::memcmp (& ed ,& got_g [k * H + r ],4 )!= 0 ; 245 } 246 } 247 std::printf (" kernel replay on the host (int8 codebook, q8_1, lane sums): gate/up %zu of %lld, down %zu of %lld values differ in any bit\n" , 248gu_diff , (long long ) (2 * NT * FF ),dn_diff , (long long ) (NT * H )); 249if (gu_diff || dn_diff )++ failures ; 250 (void )hh ; 251 } 252 {// (d) the GPU dequantizers (prompt path) against the fp16-codebook floats 253float * dq = dmalloc < float > ((size_t )FF * H ); 254uint16_t * dh = dmalloc < uint16_t > ((size_t )FF * H ); 255 std::vector < float > got_dq ((size_t )FF * H ); 256 std::vector < uint16_t > got_h ((size_t )FF * H ); 257size_t differ = 0 ,h_differ = 0 ; 258const struct {const uint8_t * src ;int type ;const std::vector < float >* want ; }mats [3 ]= { 259 {dblob ,f .gu_type ,& G }, {dblob + f .up_off ,f .gu_type ,& U }, {dblob + f .down_off ,f .d_type ,& D }}; 260for (const auto & m :mats ) { 261 strata::kernels::iq_dequant_f32 (m .type ,m .src ,FF * H ,dq ,s ); 262 strata::kernels::iq_dequant_f16 (m .type ,m .src ,FF * H ,dh ,s ); 263s -> wait (); 264from_dev (got_dq .data (),dq ,got_dq .size ()* 4 ); 265from_dev (got_h .data (),dh ,got_h .size ()* 2 ); 266for (size_t i = 0 ;i < got_dq .size ();++ i ) { 267differ += std::memcmp (& got_dq [i ],& (* m .want )[i ],4 )!= 0 ; 268h_differ += got_h [i ]!= ggml_fp32_to_fp16 (got_dq [i ]); 269 } 270 } 271 sycl::free (dq ,* s ); sycl::free (dh ,* s ); 272 std::printf (" GPU dequant vs fp16-codebook floats: %zu of %lld values differ in any bit; fp16 of it: %zu differ\n" , 273differ , (long long ) (3 * FF * H ),h_differ ); 274if (differ || h_differ )++ failures ; 275 } 276 sycl::free (dblob ,* s ); sycl::free (dx ,* s ); sycl::free (dxq ,* s ); sycl::free (dscr ,* s ); sycl::free (dout ,* s ); 277 sycl::free (dptr ,* s ); sycl::free (dstart ,* s ); sycl::free (dn ,* s ); sycl::free (ddst ,* s ); sycl::free (dtok ,* s ); 278const double ec = rel (got_c ,ref ),eg = rel (got_g ,ref ),ecg = rel (got_c ,got_g ); 279const bool ok = ec < 3e-2 && eg < 3e-2 && std::isfinite (ec )&& std::isfinite (eg ); 280 std::printf ("%-9s types %d/%d blob %8zu cpu rel %.2e gpu rel %.2e cpu-gpu %.2e %s\n" ,label .c_str (),f .gu_type ,f .d_type , 281f .bytes ,ec ,eg ,ecg ,ok ?"ok" :"FAIL" ); 282return failures + (ok ?0 :1 ); 283} 284}// namespace 285 286// random trellis bytes (every bit pattern is a valid trellis), scales in a sane fp16 range 287std::vector < uint8_t > synthetic_blob (const cpu::NativeFmt & f ,int seed ) { 288 std::mt19937 rng (1234 + seed ); 289 std::vector < uint8_t > blob (f .bytes ); 290for (auto & b :blob )b = (uint8_t )rng (); 291auto fix = [& ](uint8_t * base ,int type ,size_t row_bytes ,int64_t rows ) { 292for (int64_t r = 0 ;r < rows ;++ r ) 293for (size_t b = 0 ;b < row_bytes /bsize (type );++ b ) { 294uint8_t * blk = base + r * row_bytes + b * bsize (type ); 295const float d = 0.002f + 0.03f * ((rng ()& 0xffff ) /65535.0f ); 296const uint16_t h = ggml_fp32_to_fp16 (d ); 297blk [0 ]= (uint8_t ) (h & 0xff ); 298blk [1 ]= (uint8_t ) (h >>8 ); 299 } 300 }; 301if (f .gu_type >=144 && f .d_type >=144 ) {// (the bench also times other formats on random bytes) 302fix (blob .data (),f .gu_type ,f .gu_row ,FF ); 303fix (blob .data ()+ f .up_off ,f .gu_type ,f .gu_row ,FF ); 304fix (blob .data ()+ f .down_off ,f .d_type ,f .d_row ,H ); 305 } 306return blob ; 307} 308int type_of (const std::string & s ) {return std::atoi (s .c_str ()); } 309 310// the grouped expert kernels on `groups` distinct experts held in VRAM (more bytes than the card's L2), `epg` entries each: 311// microseconds per call and per expert, and the weight bytes read per second 312int bench (int gu ,int dn ,int groups ,int epg ,int iters ,bool had ,int phase ) { 313 strata::kernels::native_expert_set_mode (7 ,phase );// 0: all, 1: gate/up + SwiGLU + quantize, 2: down only 314 cpu::NativeFmt f ; 315 std::string err ; 316if (!cpu::native_fmt (gu ,dn ,H ,FF ,f ,err )|| !strata::kernels::native_expert_supported (gu ,dn ,H ,FF )) { 317 std::printf ("%d/%d: not a pair the GPU expert kernels take\n" ,gu ,dn ); 318return 1 ; 319 } 320static std::vector < float > sx ((size_t )H ),sh ((size_t )FF ); 321 std::mt19937 srng (5 ); 322for (auto & v :sx )v = (srng ()& 1 ) ?1.f :-1.f ; 323for (auto & v :sh )v = (srng ()& 1 ) ?1.f :-1.f ; 324if (had ) {f .had_block = 128 ;f .had_gu = f .had_d = true;f .gu_signs = sx .data ();f .d_signs = sh .data (); } 325 sycl::queue * s = & dpct::get_in_order_queue (); 326const int ent = groups * epg ,ntok = 4 ; 327uint8_t * dblob = dmalloc < uint8_t > ((size_t )groups * f .bytes ); 328for (int g = 0 ;g < groups ;++ g ) { 329const std::vector < uint8_t > b = synthetic_blob (f ,g ); 330to_dev (dblob + (size_t )g * f .bytes ,b .data (),b .size ()); 331 } 332 std::vector < float > x ((size_t )ntok * H ); 333 std::mt19937 rng (7 ); 334 std::normal_distribution < float > nd (0.f ,1.f ); 335for (auto & v :x )v = nd (rng ); 336float * dx = dmalloc < float > (x .size ()); 337to_dev (dx ,x .data (),x .size ()* 4 ); 338uint8_t * dxq = dmalloc < uint8_t > ((size_t )ntok * H /32 * 36 ); 339uint8_t * dscr = dmalloc < uint8_t > (strata::kernels::native_expert_scratch_bytes (ent ,FF )); 340float * dout = dmalloc < float > ((size_t )ent * H ); 341 std::vector < unsigned long long > ptr (groups ); 342 std::vector < int32_t > st (groups + 1 ),dst (ent ),tok (ent ); 343for (int g = 0 ;g < groups ;++ g ) {ptr [g ]= (unsigned long long ) (dblob + (size_t )g * f .bytes );st [g ]= g * epg ; } 344st [groups ]= ent ; 345for (int e = 0 ;e < ent ;++ e ) {dst [e ]= e ;tok [e ]= e %ntok ; } 346unsigned long long * dptr = dmalloc < unsigned long long > (groups ); 347int32_t * dstart = dmalloc < int32_t > (groups + 1 ),* dn_ = dmalloc < int32_t > (1 ),* ddst = dmalloc < int32_t > (ent ),* dtok = dmalloc < int32_t > (ent ); 348to_dev (dptr ,ptr .data (),groups * 8 ); 349to_dev (dstart ,st .data (), (groups + 1 )* 4 ); 350to_dev (dn_ ,& groups ,4 ); 351to_dev (ddst ,dst .data (),ent * 4 ); 352to_dev (dtok ,tok .data (),ent * 4 ); 353auto L = strata::kernels::native_expert_layout (gu ,dn ,H ,FF ); 354if (had ) {L .had_block = f .had_block ;L .d_signs = strata::kernels::hadamard_device_signs (f .d_signs , (int )FF ); } 355 strata::kernels::quantize_q8_1_rows (dx ,ntok ,H ,dxq ,s ); 356for (int i = 0 ;i < 3 ;++ i ) strata::kernels::native_expert_grouped (L ,dptr ,dstart ,dn_ ,ddst ,dtok ,groups ,ent ,dxq ,dscr ,dout ,s ); 357s -> wait (); 358 std::vector < double > us ; 359for (int i = 0 ;i < iters ;++ i ) { 360const auto t0 = std::chrono::steady_clock::now (); 361 strata::kernels::native_expert_grouped (L ,dptr ,dstart ,dn_ ,ddst ,dtok ,groups ,ent ,dxq ,dscr ,dout ,s ); 362s -> wait (); 363us .push_back (std::chrono::duration < double , std::micro > (std::chrono::steady_clock::now ()- t0 ).count ()); 364 } 365 std::sort (us .begin (),us .end ()); 366const double med = us [us .size () /2 ]; 367 std::printf ("bench %d/%d%s: %d groups x %d entries, %zu B per expert: median %.0f us per call = %.1f us per expert, %.1f GB/s of weights (min %.0f us)\n" , 368gu ,dn ,had ?" hadamard" :"" ,groups ,epg ,f .bytes ,med ,med /groups , (double )groups * f .bytes / (med * 1e-6 ) /1e9 ,us .front ()); 369 sycl::free (dblob ,* s ); sycl::free (dx ,* s ); sycl::free (dxq ,* s ); sycl::free (dscr ,* s ); sycl::free (dout ,* s ); 370 sycl::free (dptr ,* s ); sycl::free (dstart ,* s ); sycl::free (dn_ ,* s ); sycl::free (ddst ,* s ); sycl::free (dtok ,* s ); 371return 0 ; 372} 373 374int main (int argc ,char ** argv ) { 375setvbuf (stdout ,nullptr ,_IONBF ,0 ); 376if (argc >=3 && std::string (argv [1 ])== "--bench" ) { 377const std::string arg = argv [2 ]; 378const size_t slash = arg .find ('/' ); 379const bool had = std::getenv ("TQ_BENCH_HADAMARD" )!= nullptr ; 380return bench (type_of (arg .substr (0 ,slash )),type_of (arg .substr (slash + 1 )),argc > 3 ? std::atoi (argv [3 ]) :64 , 381argc > 4 ? std::atoi (argv [4 ]) :1 ,argc > 5 ? std::atoi (argv [5 ]) :50 ,had ,argc > 6 ? std::atoi (argv [6 ]) :0 ); 382 } 383if (argc >=2 && std::string (argv [1 ])== "--synthetic" ) { 384int failures = 0 ,i = 2 ; 385const bool had = argc > 2 && std::string (argv [2 ])== "--hadamard" ; 386if (had )++ i ; 387static std::vector < float > sx ((size_t )H ),sh ((size_t )FF ); 388 std::mt19937 srng (5 ); 389for (auto & v :sx )v = (srng ()& 1 ) ?1.f :-1.f ; 390for (auto & v :sh )v = (srng ()& 1 ) ?1.f :-1.f ; 391for (;i < argc ;++ i ) { 392const std::string arg = argv [i ]; 393const size_t slash = arg .find ('/' ); 394const int gu = type_of (arg .substr (0 ,slash )),dn = type_of (arg .substr (slash + 1 )); 395 cpu::NativeFmt f ; 396 std::string err ; 397if (slash == std::string::npos || !cpu::native_fmt (gu ,dn ,H ,FF ,f ,err )|| 398 !strata::kernels::native_expert_supported (gu ,dn ,H ,FF )) { 399 std::printf ("%s: %s\n" ,arg .c_str (),err .empty () ?"not a pair the GPU expert kernels take" :err .c_str ()); 400++ failures ; 401continue ; 402 } 403if (had ) { 404f .had_block = 128 ; 405f .had_gu = f .had_d = true; 406f .gu_signs = sx .data (); 407f .d_signs = sh .data (); 408 } 409failures += check_layer (f ,synthetic_blob (f ,i ),i ,had ?"hadamard" :"synthetic" ); 410 } 411 std::printf ("tq_sycl_parity: %d failures\n" ,failures ); 412return failures ?1 :0 ; 413 } 414if (argc < 3 ) { std::fprintf (stderr ,"usage: tq_sycl_parity <pack-dir> <model.gguf> [layer ...]\n" );return 2 ; } 415 std::string err ; 416if (!cpu::expert_layout_load (argv [1 ],48 ,512 ,err )) { std::printf ("expert_layout_load: %s\n" ,err .c_str ());return 1 ; } 417const auto & lay = cpu::expert_layout (); 418const strata::GgufModel model (strata::gguf_split_paths (argv [2 ])); 419 std::vector < int > layers ; 420for (int i = 3 ;i < argc ;++ i )layers .push_back (std::atoi (argv [i ])); 421if (layers .empty ())layers = {0 ,1 ,2 ,3 ,20 ,47 }; 422int failures = 0 ; 423for (int l :layers ) { 424const strata::TensorInfo * t [3 ]= {}; 425const uint8_t * data [3 ]= {}; 426const char * roles [3 ]= {"gate" ,"up" ,"down" }; 427for (int r = 0 ;r < 3 ;++ r ) { 428size_t at = 0 ; 429t [r ]= model .find ("blk." + std::to_string (l )+ ".ffn_" + roles [r ]+ "_exps.weight" ,& at ); 430if (t [r ])data [r ]= model .shard (at ).tensor_data (* t [r ]); 431 } 432if (!t [0 ]|| !t [1 ]|| !t [2 ]) { std::printf ("layer %d: no expert tensors\n" ,l );++ failures ;continue ; } 433 cpu::NativeFmt f = lay .fmt [(size_t )l ]; 434for (int E : {7 ,100 }) { 435 std::vector < uint8_t > blob (f .bytes ); 436 std::memcpy (blob .data (),data [0 ]+ (size_t )E * f .up_off ,f .up_off ); 437 std::memcpy (blob .data ()+ f .up_off ,data [1 ]+ (size_t )E * f .up_off ,f .up_off ); 438 std::memcpy (blob .data ()+ f .down_off ,data [2 ]+ (size_t )E * (f .bytes - f .down_off ),f .bytes - f .down_off ); 439failures += check_layer (f ,blob ,l * 1000 + E ,"layer " + std::to_string (l )+ " e" + std::to_string (E )); 440 } 441 } 442 std::printf ("tq_sycl_parity: %d failures\n" ,failures ); 443return failures ?1 :0 ; 444}