okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
9c5bbc36d
main
1// src/kernels/dequant_wide_parity.cpp - the wide dequant kernels (a thread per 8 values, dequant_bf16.cu) against the 2// per-group ones, bitwise, for Q4_K / Q5_K / Q6_K / IQ4_XS into FP16, BF16 and FP32: whole tensors, row slices and a 3// padded row stride, on blocks with sane fields and on raw random bytes (NaN and Inf scales included). 4// 5// build/dequant_wide_parity [--bench] 6// --bench adds each kernel's time per call on the prompt path's shapes (FP16 out). Exit 0: every case bitwise equal. 7#include "strata/kernels/dequant_bf16.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 <string> 17#include <vector> 18 19namespace K = strata::kernels; 20 21static void ck (cudaError_t e ,const char * what ) { 22if (e != cudaSuccess ) { std::fprintf (stderr ,"dequant_wide_parity: %s: %s\n" ,what ,cudaGetErrorString (e )); std::exit (2 ); } 23} 24static uint16_t f2h (float f ) {// FP32 -> FP16 bits, round to nearest even (normal range only: the scales below) 25uint32_t u ; 26 std::memcpy (& u ,& f ,4 ); 27const uint32_t sign = (u >>16 )& 0x8000u ; 28int e = (int ) ((u >>23 )& 0xff )- 127 + 15 ; 29uint32_t m = u & 0x7fffffu ; 30if (e <=0 )return (uint16_t )sign ; 31if (e >=31 )return (uint16_t ) (sign |0x7c00u ); 32uint32_t h = sign | ((uint32_t )e <<10 ) | (m >>13 ); 33if ((m & 0x1fffu )> 0x1000u || ((m & 0x1fffu )== 0x1000u && (h & 1u )))++ h ; 34return (uint16_t )h ; 35} 36struct Type {int id ;const char * name ;int block_bytes ; }; 37static const Type kTypes []= {{12 ,"Q4_K" ,144 }, {13 ,"Q5_K" ,176 }, {14 ,"Q6_K" ,210 }, {23 ,"IQ4_XS" ,136 }}; 38 39// `sane`: random codes and scales with a block scale in a weight's range; else raw random bytes 40static std::vector < uint8_t > make_blocks (const Type & t ,int64_t rows ,int64_t cols ,bool sane ,uint32_t seed ) { 41 std::mt19937 rng (seed ); 42const int64_t nb = rows * (cols /256 ); 43 std::vector < uint8_t > b ((size_t )nb * t .block_bytes ); 44for (size_t i = 0 ;i + 4 <=b .size ();i += 4 ) {const uint32_t r = rng (); std::memcpy (& b [i ],& r ,4 ); } 45for (size_t i = b .size ()& ~(size_t )3 ;i < b .size ();++ i )b [i ]= (uint8_t )rng (); 46if (sane ) { 47 std::uniform_real_distribution < float > ud (1e-4f ,3e-2f ); 48for (int64_t k = 0 ;k < nb ;++ k ) { 49uint8_t * p = b .data ()+ (size_t )k * t .block_bytes ; 50const uint16_t d = f2h ((rng ()& 1 ?1.0f :-1.0f )* ud (rng )),dm = f2h (- ud (rng )); 51if (t .id == 14 ) std::memcpy (p + 208 ,& d ,2 ); 52else if (t .id == 23 ) std::memcpy (p ,& d ,2 ); 53else { std::memcpy (p ,& d ,2 ); std::memcpy (p + 2 ,& dm ,2 ); } 54 } 55 } 56return b ; 57} 58 59int main (int argc ,char ** argv ) { 60setvbuf (stdout ,nullptr ,_IONBF ,0 ); 61const bool bench = argc > 1 && std::string (argv [1 ])== "--bench" ; 62cudaStream_t s ; 63ck (cudaStreamCreate (& s ),"stream" ); 64struct Shape {int64_t rows ,cols ; }; 65const Shape shapes []= {{10240 ,2560 }, {6144 ,2560 }, {2560 ,6144 }, {12288 ,2560 }, {512 ,2560 }, {2560 ,4096 }, {320 ,10240 }, {7 ,256 }, {33 ,768 }}; 66int cases = 0 ,failures = 0 ; 67for (const Type & t :kTypes ) { 68for (const Shape & sh :shapes ) { 69for (int sane = 1 ;sane >=0 ;-- sane ) { 70const int64_t rows = sh .rows ,cols = sh .cols ,n = rows * cols ; 71const std::vector < uint8_t > hb = make_blocks (t ,rows ,cols ,sane != 0 , (uint32_t ) (t .id * 1000003 + rows * 31 + cols + sane )); 72uint8_t * db = nullptr ; 73ck (cudaMalloc ((void ** )& db ,hb .size ()),"blocks" ); 74ck (cudaMemcpy (db ,hb .data (),hb .size (),cudaMemcpyHostToDevice ),"upload" ); 75const int64_t ldp = cols + 64 ;// the padded stride (dequant_f16_ld) 76const size_t cap = (size_t )rows * (size_t )ldp * 4 ; 77uint8_t * o0 = nullptr ,* o1 = nullptr ; 78ck (cudaMalloc ((void ** )& o0 ,cap ),"out0" ); 79ck (cudaMalloc ((void ** )& o1 ,cap ),"out1" ); 80 std::vector < uint8_t > h0 (cap ),h1 (cap ); 81// what: 0 FP16, 1 BF16, 2 FP32, 3 FP16 with the padded stride, 4 FP16 of a row slice 82for (int what = 0 ;what < 5 ;++ what ) { 83const int64_t r0 = what == 4 ?rows /3 :0 ,nr = what == 4 ?rows - rows /3 - rows /5 :rows ; 84if (nr <=0 )continue ; 85for (int wide = 0 ;wide < 2 ;++ wide ) { 86uint8_t * o = wide ?o1 :o0 ; 87ck (cudaMemsetAsync (o ,0xA5 ,cap ,s ),"memset" ); 88K ::dequant_set_wide (wide ); 89if (what == 0 || what == 4 )K ::dequant_f16 (t .id ,db ,r0 ,nr ,cols , (uint16_t * )o ,s ); 90else if (what == 1 )K ::dequant_bf16 (t .id ,db ,r0 ,nr ,cols , (uint16_t * )o ,s ); 91else if (what == 2 )K ::dequant_f32 (t .id ,db ,r0 ,nr ,cols , (float * )o ,s ); 92else if (!K ::dequant_f16_ld (t .id ,db ,r0 ,nr ,cols ,ldp , (uint16_t * )o ,s )) { std::printf ("FAIL %s: dequant_f16_ld refused\n" ,t .name );++ failures ; } 93ck (cudaMemcpyAsync (wide ?h1 .data () :h0 .data (),o ,cap ,cudaMemcpyDeviceToHost ,s ),"download" ); 94 } 95ck (cudaStreamSynchronize (s ),"sync" ); 96++ cases ; 97if (std::memcmp (h0 .data (),h1 .data (),cap )!= 0 ) { 98size_t nd = 0 ,first = 0 ; 99for (size_t i = 0 ;i < cap ;++ i )if (h0 [i ]!= h1 [i ]) {if (!nd )first = i ;++ nd ; } 100 std::printf ("FAIL %s %lldx%lld %s what %d: %zu bytes differ, first at %zu\n" ,t .name , (long long )rows , (long long )cols , 101sane ?"sane" :"raw" ,what ,nd ,first ); 102++ failures ; 103 } 104// the default-pattern bytes outside the written values must be untouched by both (the padded stride's gaps) 105if (what == 3 ) { 106size_t bad = 0 ; 107for (int64_t r = 0 ;r < nr ;++ r ) 108for (int64_t c = cols ;c < ldp ;++ c ) 109if (h1 [(size_t ) (r * ldp + c )* 2 ]!= 0xA5 || h1 [(size_t ) (r * ldp + c )* 2 + 1 ]!= 0xA5 )++ bad ; 110if (bad ) { std::printf ("FAIL %s %lldx%lld: the wide kernel wrote %zu values into the row padding\n" ,t .name , (long long )rows , (long long )cols ,bad );++ failures ; } 111 } 112 } 113if (bench && sane && rows >=512 ) { 114cudaEvent_t e0 ,e1 ; 115cudaEventCreate (& e0 ); 116cudaEventCreate (& e1 ); 117double ms [2 ]= {0 ,0 }; 118const int reps = 20 ; 119for (int wide = 0 ;wide < 2 ;++ wide ) { 120K ::dequant_set_wide (wide ); 121for (int i = 0 ;i < 3 ;++ i )K ::dequant_f16 (t .id ,db ,0 ,rows ,cols , (uint16_t * )o0 ,s ); 122cudaEventRecord (e0 ,s ); 123for (int i = 0 ;i < reps ;++ i )K ::dequant_f16 (t .id ,db ,0 ,rows ,cols , (uint16_t * )o0 ,s ); 124cudaEventRecord (e1 ,s ); 125ck (cudaEventSynchronize (e1 ),"bench sync" ); 126float tms = 0 ; 127cudaEventElapsedTime (& tms ,e0 ,e1 ); 128ms [wide ]= tms /reps ; 129 } 130const double bytes = (double )hb .size ()+ (double )n * 2 ; 131 std::printf ("BENCH %-6s %6lldx%-6lld per-group %8.3f ms (%5.1f GB/s) | wide %8.3f ms (%5.1f GB/s) | %.2fx\n" ,t .name , (long long )rows , 132 (long long )cols ,ms [0 ],bytes /ms [0 ] /1e6 ,ms [1 ],bytes /ms [1 ] /1e6 ,ms [0 ] /ms [1 ]); 133cudaEventDestroy (e0 ); 134cudaEventDestroy (e1 ); 135 } 136cudaFree (db ); 137cudaFree (o0 ); 138cudaFree (o1 ); 139 } 140 } 141 } 142K ::dequant_set_wide (-1 ); 143 std::printf ("dequant_wide_parity: %d cases, %s\n" ,cases ,failures ?"FAILED" :"OK (bitwise)" ); 144return failures ?1 :0 ; 145}