okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
86bed1c92
main
1// src/kernels/cuda/dequant_bf16.cu - see include/strata/kernels/dequant_bf16.hpp. 2// 3// Arithmetic transcribed from ggml/src/ggml-quants.c at the pinned llama.cpp (MIT License, Copyright (c) 2023-2026 4// The ggml authors): dequantize_row_q2_0/q4_0/q5_0/q8_0/q3_K/q4_K/q5_K/q6_K/iq4_nl/iq4_xs. 5#define DPCT_PROFILING_ENABLED 6#include <sycl/sycl.hpp> 7#include "strata/kprof.hpp" 8#include <dpct/dpct.hpp> 9#include "strata/sycl_queue.hpp" 10#include "strata/kernels/dequant_bf16.hpp" 11#include "strata/kernels/iq_kernels.hpp" 12 13#include <cstdio> 14#include <cstdlib> 15 16namespace strata::kernels { 17namespace { 18 19__dpct_inline__ float h2f (const uint8_t * p ) { 20return sycl::vec < sycl::half ,1 > (sycl::bit_cast < sycl::half ,unsigned short > ( 21 (uint16_t )(p [0 ] | (p [1 ] <<8 )))) 22 .convert < float , sycl::rounding_mode::automatic > ()[0 ]; 23} 24__dpct_inline__ uint16_t f2bf (float f ) { 25uint32_t u = sycl::bit_cast < unsigned int > (f ); 26// a NaN (a NaN scale in the block) stays a quiet NaN, as in ggml_compute_fp32_to_bf16 and `bf16_from_f32`: 27// the rounding add below would carry it into -0 or inf 28if ((u & 0x7fffffffu )> 0x7f800000u )return (uint16_t ) ((u >>16 ) |64u ); 29u += 0x7fffu + ((u >>16 )& 1u );// round to nearest even 30return (uint16_t ) (u >>16 ); 31} 32__dpct_inline__ void put (uint16_t * o ,int i ,float v ) {o [i ]= f2bf (v ); } 33struct H16 {uint16_t v ; }; 34__dpct_inline__ void put (H16 * o ,int i ,float v ) { 35o [i ].v = sycl::bit_cast < unsigned short , sycl::half > ( 36 sycl::vec < float ,1 > (v ) 37 .convert < sycl::half , sycl::rounding_mode::rte > ()[0 ]); 38} 39__dpct_inline__ void put (float * o ,int i ,float v ) {o [i ]= v ; } 40 41const int8_t kv_iq4nl [16 ]= {-127 ,-104 ,-83 ,-65 ,-49 ,-35 ,-22 ,-10 , 421 ,13 ,25 ,38 ,53 ,69 ,89 ,113 }; 43 44__dpct_inline__ void scale_min_k4 (int j ,const uint8_t * q ,int & d ,int & m ) { 45if (j < 4 ) {d = q [j ]& 63 ;m = q [j + 4 ]& 63 ; } 46else {d = (q [j + 4 ]& 0xF ) | ((q [j - 4 ] >>6 ) <<4 );m = (q [j + 4 ] >>4 ) | ((q [j ] >>6 ) <<4 ); } 47} 48 49// One 32-element group `g` (row-major over the whole slice); `out` points at that group's 32 outputs. 50template < int TYPE ,typename T > 51/* 52DPCT1110: The total declared local variable size in device function group32 53exceeds 128 bytes and may cause high register pressure. Consult with your 54hardware vendor to find the total register size available and adjust the code, 55or use smaller sub-group size to avoid high register pressure. 56*/ 57__dpct_inline__ void group32 (const uint8_t * row_blocks ,int gi_in_row ,T * out ) { 58if constexpr (TYPE == 42 ) {// Q2_0: 64 per block of 18 B 59const uint8_t * b = row_blocks + (size_t ) (gi_in_row /2 )* 18 ; 60const float d = h2f (b ); 61const int e0 = (gi_in_row %2 )* 32 ; 62#pragma unroll 63for (int j = 0 ;j < 32 ;++ j ) { 64const int e = e0 + j ; 65const int q = (b [2 + e /4 ] >> ((e %4 )* 2 ))& 3 ; 66put (out ,j , (float ) (q - 1 )* d ); 67 } 68 }else if constexpr (TYPE == 2 ) {// Q4_0 69const uint8_t * b = row_blocks + (size_t )gi_in_row * 18 ; 70const float d = h2f (b ); 71#pragma unroll 72for (int j = 0 ;j < 16 ;++ j ) { 73put (out ,j , (float ) ((b [2 + j ]& 0x0F )- 8 )* d ); 74put (out ,j + 16 , (float ) ((b [2 + j ] >>4 )- 8 )* d ); 75 } 76 }else if constexpr (TYPE == 6 ) {// Q5_0 77const uint8_t * b = row_blocks + (size_t )gi_in_row * 22 ; 78const float d = h2f (b ); 79const uint32_t qh = (uint32_t )b [2 ] | ((uint32_t )b [3 ] <<8 ) | ((uint32_t )b [4 ] <<16 ) | ((uint32_t )b [5 ] <<24 ); 80for (int j = 0 ;j < 16 ;++ j ) { 81const int xh0 = ((qh >>j ) <<4 )& 0x10 ; 82const int xh1 = (qh >> (j + 12 ))& 0x10 ; 83put (out ,j , (float ) (((b [6 + j ]& 0x0F ) |xh0 )- 16 )* d ); 84put (out ,j + 16 , (float ) (((b [6 + j ] >>4 ) |xh1 )- 16 )* d ); 85 } 86 }else if constexpr (TYPE == 7 ) {// Q5_1 87const uint8_t * b = row_blocks + (size_t )gi_in_row * 24 ; 88const float d = h2f (b ),m = h2f (b + 2 ); 89const uint32_t qh = (uint32_t )b [4 ] | ((uint32_t )b [5 ] <<8 ) | 90 ((uint32_t )b [6 ] <<16 ) | ((uint32_t )b [7 ] <<24 ); 91for (int j = 0 ;j < 16 ;++ j ) { 92const int xh0 = ((qh >>j )& 1 ) <<4 ; 93const int xh1 = ((qh >> (j + 16 ))& 1 ) <<4 ; 94put (out ,j , (float ) ((b [8 + j ]& 15 ) |xh0 )* d + m ); 95put (out ,j + 16 , (float ) ((b [8 + j ] >>4 ) |xh1 )* d + m ); 96 } 97 }else if constexpr (TYPE == 8 ) {// Q8_0 98const uint8_t * b = row_blocks + (size_t )gi_in_row * 34 ; 99const float d = h2f (b ); 100#pragma unroll 101for (int j = 0 ;j < 32 ;++ j )put (out ,j , (float )(int8_t )b [2 + j ]* d ); 102 }else if constexpr (TYPE == 20 ) {// IQ4_NL 103const uint8_t * b = row_blocks + (size_t )gi_in_row * 18 ; 104const float d = h2f (b ); 105#pragma unroll 106for (int j = 0 ;j < 16 ;++ j ) { 107put (out ,j ,d * (float )kv_iq4nl [b [2 + j ]& 0xf ]); 108put (out ,j + 16 ,d * (float )kv_iq4nl [b [2 + j ] >>4 ]); 109 } 110 }else if constexpr (TYPE == 11 ) {// Q3_K: hmask[32] qs[64] scales[12] d 111const uint8_t * b = row_blocks + (size_t ) (gi_in_row /8 )* 110 ; 112const int gi = gi_in_row %8 ,n = gi /4 ,jj = gi %4 ; 113const uint8_t * hm = b ; 114const uint8_t * q = b + 32 + n * 32 ; 115const uint8_t * sc = b + 96 ; 116const float d_all = h2f (b + 108 ); 117uint32_t aux [4 ]; 118memcpy (aux ,sc ,12 ); 119const uint32_t kmask1 = 0x03030303u ,kmask2 = 0x0f0f0f0fu ,tmp = aux [2 ]; 120aux [2 ]= ((aux [0 ] >>4 )& kmask2 ) | (((tmp >>4 )& kmask1 ) <<4 ); 121aux [3 ]= ((aux [1 ] >>4 )& kmask2 ) | (((tmp >>6 )& kmask1 ) <<4 ); 122aux [0 ]= (aux [0 ]& kmask2 ) | (((tmp >>0 )& kmask1 ) <<4 ); 123aux [1 ]= (aux [1 ]& kmask2 ) | (((tmp >>2 )& kmask1 ) <<4 ); 124const int8_t * scales = reinterpret_cast < const int8_t *> (aux ); 125const int shift = 2 * jj ; 126const uint8_t m = (uint8_t ) (1u << (n * 4 + jj )); 127for (int t = 0 ;t < 32 ;++ t ) { 128const int is = n * 8 + jj * 2 + (t >=16 ?1 :0 ); 129const float dl = d_all * (float ) (scales [is ]- 32 ); 130put (out ,t ,dl * (float ) ((int ) ((q [t ] >>shift )& 3 )- ((hm [t ]& m ) ?0 :4 ))); 131 } 132 }else if constexpr (TYPE == 12 ) {// Q4_K: d dmin scales[12] qs[128] 133const uint8_t * b = row_blocks + (size_t ) (gi_in_row /8 )* 144 ; 134const int gi = gi_in_row %8 ,j64 = gi /2 ,hi = gi %2 ; 135const float d = h2f (b ),dmin = h2f (b + 2 ); 136int sc ,m ; 137scale_min_k4 (gi ,b + 4 ,sc ,m ); 138const float d1 = d * (float )sc ,m1 = dmin * (float )m ; 139const uint8_t * q = b + 16 + 32 * j64 ; 140#pragma unroll 141for (int l = 0 ;l < 32 ;++ l ) 142put (out ,l ,d1 * (float )(hi ? (q [l ] >>4 ) : (q [l ]& 0xF ))- m1 ); 143 }else if constexpr (TYPE == 13 ) {// Q5_K: d dmin scales[12] qh[32] qs[128] 144const uint8_t * b = row_blocks + (size_t ) (gi_in_row /8 )* 176 ; 145const int gi = gi_in_row %8 ,j64 = gi /2 ,hi = gi %2 ; 146const float d = h2f (b ),dmin = h2f (b + 2 ); 147int sc ,m ; 148scale_min_k4 (gi ,b + 4 ,sc ,m ); 149const float d1 = d * (float )sc ,m1 = dmin * (float )m ; 150const uint8_t * qh = b + 16 ; 151const uint8_t * ql = b + 48 + 32 * j64 ; 152const uint8_t u = (uint8_t ) (1u << (2 * j64 + hi )); 153for (int l = 0 ;l < 32 ;++ l ) { 154const int nib = hi ? (ql [l ] >>4 ) : (ql [l ]& 0xF ); 155put (out ,l ,d1 * (float ) (nib + ((qh [l ]& u ) ?16 :0 ))- m1 ); 156 } 157 }else if constexpr (TYPE == 14 ) {// Q6_K: ql[128] qh[64] scales[16] d 158const uint8_t * b = row_blocks + (size_t ) (gi_in_row /8 )* 210 ; 159const int gi = gi_in_row %8 ,n = gi /4 ,qu = gi %4 ; 160const uint8_t * ql = b + 64 * n ; 161const uint8_t * qh = b + 128 + 32 * n ; 162const int8_t * sc = reinterpret_cast < const int8_t *> (b + 192 )+ 8 * n ; 163const float d = h2f (b + 208 ); 164for (int l = 0 ;l < 32 ;++ l ) { 165const int is = l /16 ; 166int q ; 167if (qu == 0 )q = (ql [l ]& 0xF ) | (((qh [l ] >>0 )& 3 ) <<4 ); 168else if (qu == 1 )q = (ql [l + 32 ]& 0xF ) | (((qh [l ] >>2 )& 3 ) <<4 ); 169else if (qu == 2 )q = (ql [l ] >>4 ) | (((qh [l ] >>4 )& 3 ) <<4 ); 170else q = (ql [l + 32 ] >>4 ) | (((qh [l ] >>6 )& 3 ) <<4 ); 171put (out ,l ,d * (float )sc [is + 2 * qu ]* (float ) (q - 32 )); 172 } 173 }else if constexpr (TYPE == 23 ) {// IQ4_XS: d scales_h scales_l[4] qs[128] 174const uint8_t * b = row_blocks + (size_t ) (gi_in_row /8 )* 136 ; 175const int ib = gi_in_row %8 ; 176const float d = h2f (b ); 177const uint16_t scales_h = (uint16_t ) (b [2 ] | (b [3 ] <<8 )); 178const int ls = ((b [4 + ib /2 ] >> (4 * (ib %2 )))& 0xf ) | (((scales_h >> (2 * ib ))& 3 ) <<4 ); 179const float dl = d * (float ) (ls - 32 ); 180const uint8_t * qs = b + 8 + 16 * ib ; 181#pragma unroll 182for (int j = 0 ;j < 16 ;++ j ) { 183put (out ,j ,dl * (float )kv_iq4nl [qs [j ]& 0xf ]); 184put (out ,j + 16 ,dl * (float )kv_iq4nl [qs [j ] >>4 ]); 185 } 186 } 187} 188 189template < int TYPE ,typename T > 190__dpct_inline__ void dequant_kernel (const uint8_t * __restrict__blocks , 191int64_t row_bytes ,int64_t row0 , 192int64_t rows ,int64_t groups_per_row , 193int64_t ld ,T * __restrict__out ) { 194auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item < 3 > (); 195const int64_t g = 196 (int64_t )item_ct1 .get_group (2 )* item_ct1 .get_local_range (2 )+ 197item_ct1 .get_local_id (2 ); 198if (g >=rows * groups_per_row )return ; 199const int64_t r = g /groups_per_row ,gi = g %groups_per_row ; 200group32 < TYPE > (blocks + (row0 + r )* row_bytes , (int )gi ,out + r * ld + gi * 32 ); 201} 202 203bool geometry (int type ,int & block_elems ,int & block_bytes ) { 204switch (type ) { 205case 2 :block_elems = 32 ;block_bytes = 18 ;return true; 206case 6 :block_elems = 32 ;block_bytes = 22 ;return true; 207case 7 :block_elems = 32 ;block_bytes = 24 ;return true; 208case 8 :block_elems = 32 ;block_bytes = 34 ;return true; 209case 20 :block_elems = 32 ;block_bytes = 18 ;return true; 210case 11 :block_elems = 256 ;block_bytes = 110 ;return true; 211case 12 :block_elems = 256 ;block_bytes = 144 ;return true; 212case 13 :block_elems = 256 ;block_bytes = 176 ;return true; 213case 14 :block_elems = 256 ;block_bytes = 210 ;return true; 214case 23 :block_elems = 256 ;block_bytes = 136 ;return true; 215case 42 :block_elems = 64 ;block_bytes = 18 ;return true; 216default :return false; 217 } 218} 219 220template < typename T > 221void launch (int type ,const void * blocks ,int64_t row0 ,int64_t rows ,int64_t cols ,T * out ,void * stream , 222int64_t ld = 0 ) { 223int be = 0 ,bb = 0 ; 224if (!geometry (type ,be ,bb )|| cols %be != 0 || rows <=0 ) { 225 std::fprintf (stderr ,"dequant: unsupported type %d or shape %lld x %lld\n" ,type , (long long )rows , 226 (long long )cols ); 227 std::exit (1 ); 228 } 229const int64_t row_bytes = cols /be * bb ,gpr = cols /32 ,total = rows * gpr ; 230if (ld <=0 )ld = cols ; 231const unsigned grid = (unsigned ) ((total + 255 ) /256 ); 232const uint8_t * p = (const uint8_t * )blocks ; 233 dpct::queue_ptr st = strata::q_of (stream ); 234#define STRATA_DQ (TY ) \ 235 { \ 236 auto exp_props = sycl::ext::oneapi::experimental::properties{ \ 237 sycl::ext::oneapi::experimental::use_root_sync}; \ 238 \ 239 strata::kprof::kp(st, __FILE__, __func__, __LINE__)->submit([&](sycl::handler &cgh) { \ 240 auto p_ct0 = p; \ 241 auto row_bytes_ct1 = row_bytes; \ 242 auto row0_ct2 = row0; \ 243 auto rows_ct3 = rows; \ 244 auto gpr_ct4 = gpr; \ 245 auto ld_ct5 = ld; \ 246 auto out_ct6 = out; \ 247 \ 248 cgh.parallel_for<dpct_kernel_name<class dequant_kernel_ec9311, \ 249 dpct_kernel_scalar<TY>, T>>( \ 250 sycl::nd_range<3>(sycl::range(1, 1, grid) * \ 251 sycl::range(1, 1, 256), \ 252 sycl::range(1, 1, 256)), \ 253 exp_props, [=](sycl::nd_item<3> item_ct1) { \ 254 dequant_kernel<TY, T>(p_ct0, row_bytes_ct1, row0_ct2, \ 255 rows_ct3, gpr_ct4, ld_ct5, out_ct6); \ 256 }); \ 257 }); \ 258 } break 259switch (type ) { 260case 2 :STRATA_DQ (2 ); 261case 6 :STRATA_DQ (6 ); 262case 7 :STRATA_DQ (7 ); 263case 8 :STRATA_DQ (8 ); 264case 11 :STRATA_DQ (11 ); 265case 12 :STRATA_DQ (12 ); 266case 13 :STRATA_DQ (13 ); 267case 14 :STRATA_DQ (14 ); 268case 20 :STRATA_DQ (20 ); 269case 23 :STRATA_DQ (23 ); 270case 42 :STRATA_DQ (42 ); 271 } 272#undef STRATA_DQ 273/* 274DPCT1010: SYCL uses exceptions to report errors and does not use the 275error codes. The cudaGetLastError function call was replaced with 0. You 276need to rewrite this code. 277*/ 278const dpct::err0 e = 0 ; 279/* 280DPCT1009: SYCL reports errors using exceptions and does not use error 281codes. Please replace the "get_error_string_dummy(...)" with a real 282error-handling function. 283*/ 284} 285 286}// namespace 287 288bool dequant_bf16_supported (int ggml_type )noexcept { 289int a ,b ; 290return geometry (ggml_type ,a ,b ); 291} 292 293namespace { 294// plan v0.3 P6: the i-quant formats (llama.cpp's dequantizers, iq_kernels.cu) 295bool iq_only (int t ) {return t == 16 || t == 17 || t == 18 || t == 21 || t == 22 || t == 29 ; } 296}// namespace 297 298void dequant_bf16 (int ggml_type ,const void * blocks ,int64_t row0 ,int64_t rows ,int64_t cols ,uint16_t * out , 299void * stream ) { 300launch < uint16_t > (ggml_type ,blocks ,row0 ,rows ,cols ,out ,stream ); 301} 302 303void dequant_f16 (int ggml_type ,const void * blocks ,int64_t row0 ,int64_t rows ,int64_t cols ,uint16_t * out , 304void * stream ) { 305if (iq_only (ggml_type )) { 306iq_dequant_f16 (ggml_type , (const uint8_t * )blocks + (size_t )row0 * iq_row_bytes (ggml_type ,cols ),rows * cols , 307out ,stream ); 308return ; 309 } 310launch < H16 > (ggml_type ,blocks ,row0 ,rows ,cols ,reinterpret_cast < H16 *> (out ),stream ); 311} 312 313bool dequant_f16_ld (int ggml_type ,const void * blocks ,int64_t row0 ,int64_t rows ,int64_t cols ,int64_t ld , 314uint16_t * out ,void * stream ) { 315int be = 0 ,bb = 0 ; 316if (iq_only (ggml_type )|| !geometry (ggml_type ,be ,bb )|| cols %be != 0 || rows <=0 || ld < cols || ld %8 != 0 ) 317return false; 318launch < H16 > (ggml_type ,blocks ,row0 ,rows ,cols ,reinterpret_cast < H16 *> (out ),stream ,ld ); 319return true; 320} 321 322void dequant_f32 (int ggml_type ,const void * blocks ,int64_t row0 ,int64_t rows ,int64_t cols ,float * out ,void * stream ) { 323if (iq_only (ggml_type )) { 324iq_dequant_f32 (ggml_type , (const uint8_t * )blocks + (size_t )row0 * iq_row_bytes (ggml_type ,cols ),rows * cols , 325out ,stream ); 326return ; 327 } 328launch < float > (ggml_type ,blocks ,row0 ,rows ,cols ,out ,stream ); 329} 330 331}// namespace strata::kernels