char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
91f6a6cf3
char/aliceai-t5-moe
1 2// Each format defines a scalar dequantFunc<T> plus a V=4 dequantFunc<T>_v 3// passed as the optional vector decoder to coopMatLoadTensorNV via 4// GL_NV_cooperative_matrix_decode_vector. When the driver doesn't support 5// the extension, ggml-vulkan.cpp strips it from the compiled SPIR-V. 6#ifdef GL_NV_cooperative_matrix_decode_vector 7#extension GL_NV_cooperative_matrix_decode_vector : enable 8#endif 9 10#include "types.glsl" 11 12layout (buffer_reference ,std430 ,buffer_reference_align = 16 )buffer decodeBufF32 { 13vec4 block ; 14}; 15 16float16_t dequantFuncF32 (const in decodeBufF32 bl ,const in uint blockCoords [2 ],const in uint coordInBlock [2 ]) 17{ 18const vec4 v = bl .block ; 19const uint idx = coordInBlock [1 ]; 20const f16vec4 vf16 = f16vec4 (v ); 21return vf16 [idx ]; 22} 23 24layout (buffer_reference ,std430 ,buffer_reference_align = 2 )buffer decodeBufQ1_0 { 25block_q1_0 block ; 26}; 27 28float16_t dequantFuncQ1_0 (const in decodeBufQ1_0 bl ,const in uint blockCoords [2 ],const in uint coordInBlock [2 ]) 29{ 30const float16_t d = bl .block .d ; 31const uint idx = coordInBlock [1 ]; 32const uint bit = (uint (bl .block .qs [(idx & 0x78 ) >>3 ]) >> (idx & 0x7 ))& 1u ; 33return bit != 0u ?d :- d ; 34} 35 36f16vec4 dequantFuncQ1_0_v (const in decodeBufQ1_0 bl ,const in uint blockCoords [2 ],const in uint coordInBlock [2 ]) 37{ 38const float16_t d = bl .block .d ; 39const float16_t md = - d ; 40const uint idx = coordInBlock [1 ]; 41const uint qs_nib = uint (bl .block .qs [idx >>3 ]) >> (idx & 0x4u ); 42return f16vec4 ( 43 (qs_nib & 1u )!= 0u ?d :md , 44 (qs_nib & 2u )!= 0u ?d :md , 45 (qs_nib & 4u )!= 0u ?d :md , 46 (qs_nib & 8u )!= 0u ?d :md ); 47} 48 49layout (buffer_reference ,std430 ,buffer_reference_align = 2 )buffer decodeBufQ2_0 { 50block_q2_0 block ; 51}; 52 53float16_t dequantFuncQ2_0 (const in decodeBufQ2_0 bl ,const in uint blockCoords [2 ],const in uint coordInBlock [2 ]) 54{ 55const float16_t d = bl .block .d ; 56const uint idx = coordInBlock [1 ]; 57const uint bits = uint (bl .block .qs [idx >>2 ]) >> (2u * (idx & 3u )); 58return (float16_t (bits & 3u )- float16_t (1.0 ))* d ; 59} 60 61f16vec4 dequantFuncQ2_0_v (const in decodeBufQ2_0 bl ,const in uint blockCoords [2 ],const in uint coordInBlock [2 ]) 62{ 63const float16_t d = bl .block .d ; 64const uint idx = coordInBlock [1 ]; 65const uint bits = uint (bl .block .qs [idx >>2 ]); 66return f16vec4 ((vec4 (bits & 3u , (bits >>2u )& 3u , (bits >>4u )& 3u ,bits >>6u )- 1.0f )* float (d )); 67} 68 69layout (buffer_reference ,std430 ,buffer_reference_align = 2 )buffer decodeBufQ4_0 { 70block_q4_0_packed16 block ; 71}; 72 73float16_t dequantFuncQ4_0 (const in decodeBufQ4_0 bl ,const in uint blockCoords [2 ],const in uint coordInBlock [2 ]) 74{ 75const float16_t d = bl .block .d ; 76const uint idx = coordInBlock [1 ]; 77const uint shift = (idx & 0x10 ) >>2 ; 78uint32_t qs = uint32_t (bl .block .qs [(idx & 0xE ) >>1 ]); 79qs >>=shift ; 80qs &=0x0F0F ; 81qs = unpack8 (qs )[idx & 1 ]; 82float16_t ret = (float16_t (qs )- float16_t (8 ))* d ; 83return ret ; 84} 85 86f16vec4 dequantFuncQ4_0_v (const in decodeBufQ4_0 bl ,const in uint blockCoords [2 ],const in uint coordInBlock [2 ]) 87{ 88const float16_t d = bl .block .d ; 89const uint idx = coordInBlock [1 ]; 90const uint shift = (idx & 0x10 ) >>2 ;// 0 or 4 91const uint qs_i = (idx & 0xE ) >>1 ;// even, in {0,2,4,6} 92const uint qsw = uint32_t (bl .block .qs [qs_i ]) 93 | (uint32_t (bl .block .qs [qs_i + 1u ]) <<16 ); 94// shift in {0,4}: per-byte mask 0x0F isolates the wanted nibble in each byte. 95const uint q4 = (qsw >>shift )& 0x0F0F0F0Fu ; 96const u8vec4 q = unpack8 (q4 ); 97return f16vec4 ((vec4 (q )- vec4 (8.0 ))* vec4 (float (d ))); 98} 99 100layout (buffer_reference ,std430 ,buffer_reference_align = 4 )buffer decodeBufQ4_1 { 101block_q4_1 block ; 102}; 103 104layout (buffer_reference ,std430 ,buffer_reference_align = 4 )buffer decodeBufQ4_1_packed32 { 105block_q4_1_packed32 block ; 106}; 107 108float16_t dequantFuncQ4_1 (const in decodeBufQ4_1 bl ,const in uint blockCoords [2 ],const in uint coordInBlock [2 ]) 109{ 110const float16_t d = bl .block .d ; 111const float16_t m = bl .block .m ; 112const uint idx = coordInBlock [1 ]; 113const uint iqs = idx & 0xF ; 114const uint shift = (idx & 0x10 ) >>2 ; 115uint32_t qs = bl .block .qs [iqs ]; 116qs >>=shift ; 117qs &=0xF ; 118float16_t ret = float16_t (qs )* d + m ; 119return ret ; 120} 121 122f16vec4 dequantFuncQ4_1_v (const in decodeBufQ4_1 bl ,const in uint blockCoords [2 ],const in uint coordInBlock [2 ]) 123{ 124decodeBufQ4_1_packed32 bl32 = decodeBufQ4_1_packed32 (bl ); 125const float16_t d = bl .block .d ; 126const float16_t m = bl .block .m ; 127const uint idx = coordInBlock [1 ]; 128const uint shift = (idx & 0x10 ) >>2 ;// 0 or 4 129const uint qs_w = (idx & 0xC ) >>2 ;// iqs / 4 in [0,4) 130const uint qsw = uint32_t (bl32 .block .qs [qs_w ]); 131const u8vec4 q = unpack8 ((qsw >>shift )& 0x0F0F0F0Fu ); 132return f16vec4 (vec4 (q )* vec4 (float (d ))+ vec4 (float (m ))); 133} 134 135layout (buffer_reference ,std430 ,buffer_reference_align = 2 )buffer decodeBufQ5_0 { 136block_q5_0 block ; 137}; 138 139layout (buffer_reference ,std430 ,buffer_reference_align = 2 )buffer decodeBufQ5_0_packed16 { 140block_q5_0_packed16 block ; 141}; 142 143float16_t dequantFuncQ5_0 (const in decodeBufQ5_0 bl ,const in uint blockCoords [2 ],const in uint coordInBlock [2 ]) 144{ 145const float16_t d = bl .block .d ; 146const uint idx = coordInBlock [1 ]; 147const uint iqs = idx & 0xF ; 148 149const uint uint_qh = uint (bl .block .qh [1 ]) <<16 |bl .block .qh [0 ]; 150const uint qh = ((uint_qh >>idx ) <<4 )& 0x10 ; 151 152const uint shift = (idx & 0x10 ) >>2 ; 153uint32_t qs = bl .block .qs [iqs ]; 154qs >>=shift ; 155qs &=0xF ; 156 157float16_t ret = (float16_t (qs |qh )- float16_t (16 ))* d ; 158return ret ; 159} 160 161f16vec4 dequantFuncQ5_0_v (const in decodeBufQ5_0 bl ,const in uint blockCoords [2 ],const in uint coordInBlock [2 ]) 162{ 163decodeBufQ5_0_packed16 bl16 = decodeBufQ5_0_packed16 (bl ); 164const float16_t d = bl .block .d ; 165const uint idx = coordInBlock [1 ]; 166const uint shift = (idx & 0x10 ) >>2 ;// 0 or 4 167const uint qs_i = (idx & 0xC ) >>1 ;// packed16 word index, in {0,2,4,6} 168const uint qsw = uint32_t (bl16 .block .qs [qs_i ]) 169 | (uint32_t (bl16 .block .qs [qs_i + 1u ]) <<16 ); 170const u8vec4 ql = unpack8 ((qsw >>shift )& 0x0F0F0F0Fu ); 171 172const uint uint_qh = uint (bl16 .block .qh [1 ]) <<16 |uint (bl16 .block .qh [0 ]); 173const uint qh_pack = uint_qh >>idx ;// bits 0..3 = element idx..idx+3 high bits 174const uvec4 qh_high = (uvec4 (qh_pack ,qh_pack >>1u ,qh_pack >>2u ,qh_pack >>3u )& uvec4 (0x01u )) <<4u ; 175 176return f16vec4 ((vec4 (ql )+ vec4 (qh_high )- vec4 (16.0 ))* vec4 (float (d ))); 177} 178 179layout (buffer_reference ,std430 ,buffer_reference_align = 8 )buffer decodeBufQ5_1 { 180block_q5_1 block ; 181}; 182 183layout (buffer_reference ,std430 ,buffer_reference_align = 8 )buffer decodeBufQ5_1_packed32 { 184block_q5_1_packed32 block ; 185}; 186 187float16_t dequantFuncQ5_1 (const in decodeBufQ5_1 bl ,const in uint blockCoords [2 ],const in uint coordInBlock [2 ]) 188{ 189const float16_t d = bl .block .d ; 190const float16_t m = bl .block .m ; 191const uint idx = coordInBlock [1 ]; 192const uint iqs = idx & 0xF ; 193 194const uint uint_qh = bl .block .qh ; 195const uint qh = ((uint_qh >>idx ) <<4 )& 0x10 ; 196 197const uint shift = (idx & 0x10 ) >>2 ; 198uint32_t qs = bl .block .qs [iqs ]; 199qs >>=shift ; 200qs &=0xF ; 201 202float16_t ret = float16_t (qs |qh )* d + m ; 203return ret ; 204} 205 206f16vec4 dequantFuncQ5_1_v (const in decodeBufQ5_1 bl ,const in uint blockCoords [2 ],const in uint coordInBlock [2 ]) 207{ 208decodeBufQ5_1_packed32 bl32 = decodeBufQ5_1_packed32 (bl ); 209const float16_t d = bl .block .d ; 210const float16_t m = bl .block .m ; 211const uint idx = coordInBlock [1 ]; 212const uint shift = (idx & 0x10 ) >>2 ;// 0 or 4 213const uint qs_w = (idx & 0xC ) >>2 ;// iqs / 4 in [0,4) 214const uint qsw = uint32_t (bl32 .block .qs [qs_w ]); 215const u8vec4 ql = unpack8 ((qsw >>shift )& 0x0F0F0F0Fu ); 216 217const uint qh_pack = bl .block .qh >>idx ;// bits 0..3 = element idx..idx+3 high bits 218const uvec4 qh_high = (uvec4 (qh_pack ,qh_pack >>1u ,qh_pack >>2u ,qh_pack >>3u )& uvec4 (0x01u )) <<4u ; 219 220return f16vec4 ((vec4 (ql )+ vec4 (qh_high ))* vec4 (float (d ))+ vec4 (float (m ))); 221} 222 223layout (buffer_reference ,std430 ,buffer_reference_align = 2 )buffer decodeBufQ8_0 { 224block_q8_0_packed16 block ; 225}; 226 227float16_t dequantFuncQ8_0 (const in decodeBufQ8_0 bl ,const in uint blockCoords [2 ],const in uint coordInBlock [2 ]) 228{ 229const float16_t d = bl .block .d ; 230const uint idx = coordInBlock [1 ]; 231const uint iqs = idx ; 232 233// Load 16b and select the byte for this element 234int32_t qs = unpack8 (bl .block .qs [(iqs & 0x1E ) >>1 ])[iqs & 1 ]; 235float16_t ret = float16_t (qs )* d ; 236return ret ; 237} 238 239f16vec4 dequantFuncQ8_0_v (const in decodeBufQ8_0 bl ,const in uint blockCoords [2 ],const in uint coordInBlock [2 ]) 240{ 241const float16_t d = bl .block .d ; 242const uint idx = coordInBlock [1 ]; 243const uint base = idx >>1u ; 244const uint w = uint (uint16_t (bl .block .qs [base ])) 245 | (uint (uint16_t (bl .block .qs [base + 1u ])) <<16u ); 246const i8vec4 qi = unpack8 (int32_t (w )); 247return f16vec4 (vec4 (qi )* vec4 (float (d ))); 248} 249 250layout (buffer_reference ,std430 ,buffer_reference_align = 2 )buffer decodeBufTQ1_0 { 251block_tq1_0 block ; 252}; 253 254float16_t dequantFuncTQ1_0 (const in decodeBufTQ1_0 bl ,const in uint blockCoords [2 ],const in uint coordInBlock [2 ]) 255{ 256const uint e = coordInBlock [1 ]; 257const uint bidx = tq1_0_byte_of (e ); 258const uint qbyte = uint (bidx < 48u ?bl .block .qs [bidx ] :bl .block .qh [bidx - 48u ]); 259const uint xi = tq1_0_trit (qbyte ,tq1_0_digit_of (e )); 260return bl .block .d * (float16_t (int (xi ))- float16_t (1.0 )); 261} 262 263f16vec4 dequantFuncTQ1_0_v (const in decodeBufTQ1_0 bl ,const in uint blockCoords [2 ],const in uint coordInBlock [2 ]) 264{ 265const uint e = coordInBlock [1 ]; 266f16vec4 v ; 267 [[unroll ]]for (uint k = 0u ;k < 4u ;++ k ) { 268const uint ee = e + k ; 269const uint bidx = tq1_0_byte_of (ee ); 270const uint qbyte = uint (bidx < 48u ?bl .block .qs [bidx ] :bl .block .qh [bidx - 48u ]); 271const uint xi = tq1_0_trit (qbyte ,tq1_0_digit_of (ee )); 272v [k ]= bl .block .d * (float16_t (int (xi ))- float16_t (1.0 )); 273 } 274return v ; 275} 276 277layout (buffer_reference ,std430 ,buffer_reference_align = 2 )buffer decodeBufTQ2_0 { 278block_tq2_0 block ; 279}; 280 281layout (buffer_reference ,std430 ,buffer_reference_align = 2 )buffer decodeBufTQ2_0_packed16 { 282block_tq2_0_packed16 block ; 283}; 284 285float16_t dequantFuncTQ2_0 (const in decodeBufTQ2_0 bl ,const in uint blockCoords [2 ],const in uint coordInBlock [2 ]) 286{ 287decodeBufTQ2_0_packed16 bl16 = decodeBufTQ2_0_packed16 (bl ); 288const uint idx = coordInBlock [1 ]; 289 290const uint qsshift = (idx & 0x60 ) >>4 ;// 0,2,4,6 291 292uint qs = uint32_t (bl16 .block .qs [((idx & 0x80 ) >>3 )+ ((idx & 0x1E ) >>1 )]); 293qs = (qs >>qsshift )& 0x0303 ; 294qs = unpack8 (qs )[idx & 1 ]; 295 296return bl .block .d * (float16_t (int (qs ))- float16_t (1.0 )); 297} 298 299f16vec4 dequantFuncTQ2_0_v (const in decodeBufTQ2_0 bl ,const in uint blockCoords [2 ],const in uint coordInBlock [2 ]) 300{ 301const uint idx = coordInBlock [1 ]; 302 303const uint qsshift = (idx & 0x60 ) >>4 ;// 0,2,4,6 304const uint qsi = ((idx & 0x80 ) >>2 )+ (idx & 0x1C );// byte index of 4-aligned group 305 306const uint qsw = (uint (bl .block .qs [qsi ])) 307 | (uint (bl .block .qs [qsi + 1 ]) <<8 ) 308 | (uint (bl .block .qs [qsi + 2 ]) <<16 ) 309 | (uint (bl .block .qs [qsi + 3 ]) <<24 ); 310const u8vec4 q = unpack8 ((qsw >>qsshift )& 0x03030303 ); 311 312return bl .block .d * (f16vec4 (q )- f16vec4 (1.0 )); 313} 314 315layout (buffer_reference ,std430 ,buffer_reference_align = 4 )buffer decodeBufQ2_K { 316block_q2_K block ; 317}; 318 319layout (buffer_reference ,std430 ,buffer_reference_align = 16 )buffer decodeBufQ2_K_packed16 { 320block_q2_K_packed16 block ; 321}; 322 323layout (buffer_reference ,std430 ,buffer_reference_align = 4 )buffer decodeBufQ2_K_packed32 { 324block_q2_K_packed32 block ; 325}; 326 327float16_t dequantFuncQ2_K (const in decodeBufQ2_K bl ,const in uint blockCoords [2 ],const in uint coordInBlock [2 ]) 328{ 329decodeBufQ2_K_packed16 bl16 = decodeBufQ2_K_packed16 (bl ); 330const f16vec2 dm = bl .block .dm ; 331const uint idx = coordInBlock [1 ]; 332 333const uint scalesi = (idx & 0xF0 ) >>4 ;// 0..15 334const uint qsshift = (idx & 0x60 ) >>4 ;// 0,2,4,6 335 336uint qs = uint32_t (bl16 .block .qs [((idx & 0x80 ) >>3 )+ ((idx & 0x1E ) >>1 )]); 337qs = (qs >>qsshift )& 0x0303 ; 338qs = unpack8 (qs )[idx & 1 ]; 339 340const uint scales = bl .block .scales [scalesi ]; 341float16_t ret = dm .x * float16_t (scales & 0xF )* float16_t (qs )- dm .y * float16_t (scales >>4 ); 342return ret ; 343} 344 345f16vec4 dequantFuncQ2_K_v (const in decodeBufQ2_K bl ,const in uint blockCoords [2 ],const in uint coordInBlock [2 ]) 346{ 347decodeBufQ2_K_packed32 bl32 = decodeBufQ2_K_packed32 (bl ); 348const f16vec2 dm = bl .block .dm ; 349const uint idx = coordInBlock [1 ]; 350 351const uint scalesi = idx >>4 ;// 0..15 352const uint qsshift = (idx & 0x60 ) >>4 ;// 0,2,4,6 353 354// qs_i (packed16) = ((idx & 0x80) >> 3) + ((idx & 0x1E) >> 1) is even for idx % 4 == 0, 355// so qs_w (packed32) = qs_i / 2 = ((idx & 0x80) >> 4) + ((idx & 0x1Cu) >> 2). 356const uint qs_w = ((idx & 0x80 ) >>4 )+ ((idx & 0x1Cu ) >>2 ); 357const uint qsw = uint32_t (bl32 .block .qs [qs_w ]); 358const uint qs4 = (qsw >>qsshift )& 0x03030303u ; 359const u8vec4 qi = unpack8 (qs4 ); 360 361const uint scales = bl .block .scales [scalesi ]; 362const float16_t d_sub = dm .x * float16_t (scales & 0xF ); 363const float16_t m_sub = dm .y * float16_t (scales >>4 ); 364return f16vec4 (vec4 (qi )* vec4 (float (d_sub ))- vec4 (float (m_sub ))); 365} 366 367layout (buffer_reference ,std430 ,buffer_reference_align = 2 )buffer decodeBufQ3_K { 368block_q3_K block ; 369}; 370 371layout (buffer_reference ,std430 ,buffer_reference_align = 2 )buffer decodeBufQ3_K_packed16 { 372block_q3_K_packed16 block ; 373}; 374 375float16_t dequantFuncQ3_K (const in decodeBufQ3_K bl ,const in uint blockCoords [2 ],const in uint coordInBlock [2 ]) 376{ 377const uint idx = coordInBlock [1 ]; 378const uint iqs = idx ; 379 380const uint n = iqs /128 ;// 0,1 381const uint qsi = n * 32 + (iqs %32 );// 0..63 382const uint hmi = (iqs %32 );// 0..31 383const uint j = (iqs %128 ) /8 ;// 0..15 384const uint is = iqs /16 ;// 0..15 385const uint halfsplit = ((iqs %128 ) /32 );// 0,1,2,3 386const uint qsshift = halfsplit * 2 ;// 0,2,4,6 387const uint m = 1 << (4 * n + halfsplit );// 1,2,4,8,16,32,64,128 388 389uint32_t scaleidx0 = (is < 8 ) ?is : (is - 8 ); 390uint32_t scaleidx0shift = (is < 8 ) ?0 :4 ; 391uint32_t scaleidx1 = is + 8 - (is /4 )* 4 ; 392uint32_t scaleidx1shift = (is /4 )* 2 ; 393 394const int8_t us = int8_t (((bl .block .scales [scaleidx0 ] >>scaleidx0shift )& 0xF ) | (((bl .block .scales [scaleidx1 ] >>scaleidx1shift )& 3 ) <<4 )); 395 396const float16_t dl = bl .block .d * float16_t (us - 32 ); 397 398float16_t ret = dl * float16_t (int8_t ((bl .block .qs [qsi ] >>qsshift )& 3 )- (((bl .block .hmask [hmi ]& m )!= 0 ) ?0 :4 )); 399 400return ret ; 401} 402 403f16vec4 dequantFuncQ3_K_v (const in decodeBufQ3_K bl ,const in uint blockCoords [2 ],const in uint coordInBlock [2 ]) 404{ 405decodeBufQ3_K_packed16 bl16 = decodeBufQ3_K_packed16 (bl ); 406const uint idx = coordInBlock [1 ]; 407 408const uint n = idx >>7 ;// 0,1 409const uint is = idx >>4 ;// 0..15 410const uint halfsplit = (idx & 0x60 ) >>5 ;// 0,1,2,3 411const uint qsshift = halfsplit <<1 ;// 0,2,4,6 412const uint hbit = (n <<2 )+ halfsplit ;// 0..7 (bit position in hmask byte) 413 414uint32_t scaleidx0 = (is < 8 ) ?is : (is - 8 ); 415uint32_t scaleidx0shift = (is < 8 ) ?0u :4u ; 416uint32_t scaleidx1 = is + 8 - (is /4 )* 4 ; 417uint32_t scaleidx1shift = (is /4 )* 2 ; 418 419const int8_t us = int8_t ( 420 ((bl .block .scales [scaleidx0 ] >>scaleidx0shift )& 0xF ) | 421 (((bl .block .scales [scaleidx1 ] >>scaleidx1shift )& 3 ) <<4 )); 422const float16_t dl = bl .block .d * float16_t (int (us )- 32 ); 423 424// For idx % 4 == 0: (idx & 0x1F) == (idx & 0x1C) is a multiple of 4. 425const uint qsi = (n <<5 )+ (idx & 0x1Cu ); 426const uint hmi = (idx & 0x1Cu ); 427 428// Two adjacent uint16 packed16 reads, combined into a uint32 in registers. 429// After this: byte j of qsw / hmw holds the data for element idx+j. 430const uint qsw = uint32_t (bl16 .block .qs [qsi >>1 ]) 431 | (uint32_t (bl16 .block .qs [(qsi >>1 )+ 1u ]) <<16 ); 432const uint hmw = uint32_t (bl16 .block .hmask [hmi >>1 ]) 433 | (uint32_t (bl16 .block .hmask [(hmi >>1 )+ 1u ]) <<16 ); 434 435// qsshift in {0,2,4,6} and hbit in {0..7}: per-byte masks isolate the wanted bits 436// with no inter-byte leakage. 437const uint ql4 = (qsw >>qsshift )& 0x03030303u ; 438const uint qh4 = (hmw >>hbit )& 0x01010101u ; 439 440const ivec4 q = ivec4 (unpack8 (ql4 | (qh4 <<2 )))- ivec4 (4 ); 441return f16vec4 (vec4 (q )* vec4 (float (dl ))); 442} 443 444layout (buffer_reference ,std430 ,buffer_reference_align = 16 )buffer decodeBufQ4_K { 445block_q4_K block ; 446}; 447 448layout (buffer_reference ,std430 ,buffer_reference_align = 16 )buffer decodeBufQ4_K_packed16 { 449block_q4_K_packed16 block ; 450}; 451 452layout (buffer_reference ,std430 ,buffer_reference_align = 16 )buffer decodeBufQ4_K_packed32 { 453block_q4_K_packed32 block ; 454}; 455 456layout (buffer_reference ,std430 ,buffer_reference_align = 16 )buffer decodeBufQ4_K_packed128 { 457block_q4_K_packed128 block ; 458}; 459 460#if defined(IS_MUL_MM2 ) 461 462// For Q4_K and Q5_K in the mat-mul shader, we decode a tile's worth of scales 463// into shared memory and then process the whole tile using those scales. 464// There is a fetch function that loads into private variables and then a store 465// function that stores into shared memory. 466// Q4_K and Q5_K have the same encoding of scales, so everything is shared except 467// the part that fetches from the structure (which has a different block layout). 468#if defined(DATA_A_Q4_K )|| defined(DATA_A_Q5_K ) 469const uint shAscales_stride = (BM + 2 ); 470// 1 scale per 32 elements -> 8 scales per block, per row 471shared vec2 shAscales [8 * shAscales_stride ]; 472uvec4 row_v ; 473#endif 474 475#if defined(DATA_A_Q4_K ) 476layout (binding = 0 )readonly buffer A_Q4_K_128 {block_q4_K_packed128 data_a_q4_k_packed128 [];}; 477 478void fetch_scalesQ4_K (uint ir_BM ,uint pos_a ,uint stride_a ,uint block_k ,uint tid ,bool in_bounds ) 479{ 480uint tids_per_row = BLOCK_SIZE /BM ; 481uint is_per_tid = 8 /tids_per_row ; 482uint is_start = is_per_tid * (tid %tids_per_row ); 483uint tid_row = tid /tids_per_row ; 484 485uint row = ir_BM + tid_row ; 486uint block_index = pos_a + row * stride_a + (block_k /QUANT_K ); 487if (in_bounds || row < p .M ) { 488row_v = data_a_q4_k_packed128 [block_index ].q4k [0 ]; 489 } 490} 491#endif 492#if defined(DATA_A_Q5_K ) 493layout (binding = 0 )readonly buffer A_Q5_K_128 {block_q5_K_packed128 data_a_q5_k_packed128 [];}; 494 495void fetch_scalesQ5_K (uint ir_BM ,uint pos_a ,uint stride_a ,uint block_k ,uint tid ,bool in_bounds ) 496{ 497uint tids_per_row = BLOCK_SIZE /BM ; 498uint is_per_tid = 8 /tids_per_row ; 499uint is_start = is_per_tid * (tid %tids_per_row ); 500uint tid_row = tid /tids_per_row ; 501 502uint row = ir_BM + tid_row ; 503uint block_index = pos_a + row * stride_a + (block_k /QUANT_K ); 504if (in_bounds || row < p .M ) { 505row_v = data_a_q5_k_packed128 [block_index ].q5k [0 ]; 506 } 507} 508#endif 509 510#if defined(DATA_A_Q4_K )|| defined(DATA_A_Q5_K ) 511void store_scalesQ4_K (uint tid ) 512{ 513barrier (); 514 515uint tids_per_row = BLOCK_SIZE /BM ; 516uint is_per_tid = 8 /tids_per_row ; 517uint is_start = is_per_tid * (tid %tids_per_row ); 518uint tid_row = tid /tids_per_row ; 519 520 [[unroll ]]for (uint idx = 0 ;idx < is_per_tid ;++ idx ) { 521uint is = idx + is_start ; 522uvec4 v = row_v ; 523const vec2 loadd = vec2 (unpackFloat2x16 (v .x )); 524 525uint32_t sc ; 526uint32_t mbyte ; 527 528uint32_t scale0 = v .y ; 529uint32_t scale4 = v .z ; 530uint32_t scale8 = v .w ; 531 532uint32_t sc_lo = scale0 ; 533uint32_t mb_lo = scale4 ; 534uint32_t sc_hi = (scale8 & 0x0F0F0F0F ) | ((scale0 & 0xC0C0C0C0 ) >>2 ); 535uint32_t mb_hi = ((scale8 & 0xF0F0F0F0 ) >>4 ) | ((scale4 & 0xC0C0C0C0 ) >>2 ); 536 537sc = is < 4 ?sc_lo :sc_hi ; 538mbyte = is < 4 ?mb_lo :mb_hi ; 539sc = sc >> (8 * (is & 3 )); 540mbyte = mbyte >> (8 * (is & 3 )); 541sc &=0x3F ; 542mbyte &=0x3F ; 543 544const float d = loadd .x * float (sc ); 545const float m = loadd .y * float (mbyte ); 546shAscales [is * shAscales_stride + tid_row ]= vec2 (d ,m ); 547 } 548 549barrier (); 550} 551#endif 552 553#endif 554 555float16_t dequantFuncQ4_K (const in decodeBufQ4_K bl ,const in uint blockCoords [2 ],const in uint coordInBlock [2 ]) 556{ 557decodeBufQ4_K_packed16 bl16 = decodeBufQ4_K_packed16 (bl ); 558decodeBufQ4_K_packed128 bl128 = decodeBufQ4_K_packed128 (bl ); 559const uint idx = coordInBlock [1 ]; 560 561const uint b = (idx & 0x20 ) >>5 ;// 0,1 562const uint is = (idx & 0xE0 ) >>5 ;// 0..7 563 564#if defined(IS_MUL_MM2 )&& defined(DATA_A_Q4_K ) 565vec2 v = shAscales [is * shAscales_stride + (blockCoords [0 ] %BM )]; 566float d = v .x ; 567float m = v .y ; 568#else 569uvec4 v = bl128 .block .q4k [0 ]; 570const vec2 loadd = vec2 (unpackFloat2x16 (v .x )); 571 572uint32_t sc ; 573uint32_t mbyte ; 574 575uint32_t scale0 = v .y ; 576uint32_t scale4 = v .z ; 577uint32_t scale8 = v .w ; 578 579uint32_t sc_lo = scale0 ; 580uint32_t mb_lo = scale4 ; 581uint32_t sc_hi = (scale8 & 0x0F0F0F0F ) | ((scale0 & 0xC0C0C0C0 ) >>2 ); 582uint32_t mb_hi = ((scale8 & 0xF0F0F0F0 ) >>4 ) | ((scale4 & 0xC0C0C0C0 ) >>2 ); 583 584sc = is < 4 ?sc_lo :sc_hi ; 585mbyte = is < 4 ?mb_lo :mb_hi ; 586sc = sc >> (8 * (is & 3 )); 587mbyte = mbyte >> (8 * (is & 3 )); 588sc &=0x3F ; 589mbyte &=0x3F ; 590 591const float d = loadd .x * float (sc ); 592const float m = loadd .y * float (mbyte ); 593#endif 594 595uint qs = uint32_t (bl16 .block .qs [((idx & 0xC0 ) >>2 )+ ((idx & 0x1E ) >>1 )]); 596qs = (qs >> (b * 4 + 8 * (idx & 1 )))& 0xF ; 597 598float ret = d * float (qs )- m ; 599 600return float16_t (ret ); 601} 602 603f16vec4 dequantFuncQ4_K_v (const in decodeBufQ4_K bl ,const in uint blockCoords [2 ],const in uint coordInBlock [2 ]) 604{ 605decodeBufQ4_K_packed32 bl32 = decodeBufQ4_K_packed32 (bl ); 606decodeBufQ4_K_packed128 bl128 = decodeBufQ4_K_packed128 (bl ); 607const uint idx = coordInBlock [1 ]; 608 609const uint is = idx >>5 ;// 0..7 610 611#if defined(IS_MUL_MM2 )&& defined(DATA_A_Q4_K ) 612vec2 v = shAscales [is * shAscales_stride + (blockCoords [0 ] %BM )]; 613float d = v .x ; 614float m = v .y ; 615#else 616uvec4 v = bl128 .block .q4k [0 ]; 617const vec2 loadd = vec2 (unpackFloat2x16 (v .x )); 618 619uint32_t sc ; 620uint32_t mbyte ; 621 622uint32_t scale0 = v .y ; 623uint32_t scale4 = v .z ; 624uint32_t scale8 = v .w ; 625 626uint32_t sc_lo = scale0 ; 627uint32_t mb_lo = scale4 ; 628uint32_t sc_hi = (scale8 & 0x0F0F0F0F ) | ((scale0 & 0xC0C0C0C0 ) >>2 ); 629uint32_t mb_hi = ((scale8 & 0xF0F0F0F0 ) >>4 ) | ((scale4 & 0xC0C0C0C0 ) >>2 ); 630 631sc = is < 4 ?sc_lo :sc_hi ; 632mbyte = is < 4 ?mb_lo :mb_hi ; 633sc = sc >> (8 * (is & 3 )); 634mbyte = mbyte >> (8 * (is & 3 )); 635sc &=0x3F ; 636mbyte &=0x3F ; 637 638const float d = loadd .x * float (sc ); 639const float m = loadd .y * float (mbyte ); 640#endif 641 642// idx in [0,256); vector decode uses idx a multiple of 4. packed32 word index: 643// (qs_i >> 1) == (idx >> 6) * 8 + ((idx & 0x1E) >> 2). sh is 0 or 4 only, so a 644// single (w >> sh) & 0x0F0F0F0F isolates all four nibbles without inter-byte leakage. 645const uint sh = (idx & 0x20u ) >>3u ; 646const uint w = uint32_t (bl32 .block .qs [(idx >>6 )* 8u + ((idx & 0x1Eu ) >>2 )]); 647const u8vec4 q = unpack8 ((w >>sh )& 0x0F0F0F0Fu ); 648 649return f16vec4 (vec4 (d )* vec4 (q )- vec4 (m )); 650} 651 652layout (buffer_reference ,std430 ,buffer_reference_align = 16 )buffer decodeBufQ5_K { 653block_q5_K block ; 654}; 655 656layout (buffer_reference ,std430 ,buffer_reference_align = 16 )buffer decodeBufQ5_K_packed16 { 657block_q5_K_packed16 block ; 658}; 659 660layout (buffer_reference ,std430 ,buffer_reference_align = 16 )buffer decodeBufQ5_K_packed128 { 661block_q5_K_packed128 block ; 662}; 663 664layout (buffer_reference ,std430 ,buffer_reference_align = 16 )buffer decodeBufQ5_K_packed32 { 665block_q5_K_packed32 block ; 666}; 667 668float16_t dequantFuncQ5_K (const in decodeBufQ5_K bl ,const in uint blockCoords [2 ],const in uint coordInBlock [2 ]) 669{ 670decodeBufQ5_K_packed16 bl16 = decodeBufQ5_K_packed16 (bl ); 671decodeBufQ5_K_packed128 bl128 = decodeBufQ5_K_packed128 (bl ); 672const uint idx = coordInBlock [1 ]; 673 674const uint b = (idx & 0x20 ) >>5 ;// 0,1 675const uint is = (idx & 0xE0 ) >>5 ;// 0..7 676 677#if defined(IS_MUL_MM2 )&& defined(DATA_A_Q5_K ) 678vec2 v = shAscales [is * shAscales_stride + (blockCoords [0 ] %BM )]; 679float d = v .x ; 680float m = v .y ; 681#else 682uvec4 v = bl128 .block .q5k [0 ]; 683 684const f16vec2 loadd = unpackFloat2x16 (v .x ); 685 686uint32_t sc ; 687uint32_t mbyte ; 688 689uint32_t scale0 = v .y ; 690uint32_t scale4 = v .z ; 691uint32_t scale8 = v .w ; 692 693uint32_t sc_lo = scale0 ; 694uint32_t mb_lo = scale4 ; 695uint32_t sc_hi = (scale8 & 0x0F0F0F0F ) | ((scale0 & 0xC0C0C0C0 ) >>2 ); 696uint32_t mb_hi = ((scale8 & 0xF0F0F0F0 ) >>4 ) | ((scale4 & 0xC0C0C0C0 ) >>2 ); 697 698sc = is < 4 ?sc_lo :sc_hi ; 699mbyte = is < 4 ?mb_lo :mb_hi ; 700sc = sc >> (8 * (is & 3 )); 701mbyte = mbyte >> (8 * (is & 3 )); 702sc &=0x3F ; 703mbyte &=0x3F ; 704 705const float16_t d = loadd .x * float16_t (sc ); 706const float16_t m = loadd .y * float16_t (mbyte ); 707#endif 708 709uint qh = uint32_t (bl16 .block .qh [(idx & 0x1E ) >>1 ]); 710qh = ((qh >>is )& 0x101 ) <<4 ; 711 712uint qs = uint32_t (bl16 .block .qs [((idx & 0xC0 ) >>2 )+ ((idx & 0x1E ) >>1 )]); 713qs = (qs >> (b * 4 ))& 0x0F0F ; 714qs = unpack8 (qs |qh )[idx & 1 ]; 715 716float ret = d * float (qs )- m ; 717 718return float16_t (ret ); 719} 720 721f16vec4 dequantFuncQ5_K_v (const in decodeBufQ5_K bl ,const in uint blockCoords [2 ],const in uint coordInBlock [2 ]) 722{ 723decodeBufQ5_K_packed32 bl32 = decodeBufQ5_K_packed32 (bl ); 724decodeBufQ5_K_packed128 bl128 = decodeBufQ5_K_packed128 (bl ); 725const uint idx = coordInBlock [1 ]; 726const uint is = idx >>5 ; 727 728#if defined(IS_MUL_MM2 )&& defined(DATA_A_Q5_K ) 729vec2 v = shAscales [is * shAscales_stride + (blockCoords [0 ] %BM )]; 730float d = v .x ; 731float m = v .y ; 732#else 733uvec4 v = bl128 .block .q5k [0 ]; 734 735const f16vec2 loadd = unpackFloat2x16 (v .x ); 736 737uint32_t sc ; 738uint32_t mbyte ; 739 740uint32_t scale0 = v .y ; 741uint32_t scale4 = v .z ; 742uint32_t scale8 = v .w ; 743 744uint32_t sc_lo = scale0 ; 745uint32_t mb_lo = scale4 ; 746uint32_t sc_hi = (scale8 & 0x0F0F0F0F ) | ((scale0 & 0xC0C0C0C0 ) >>2 ); 747uint32_t mb_hi = ((scale8 & 0xF0F0F0F0 ) >>4 ) | ((scale4 & 0xC0C0C0C0 ) >>2 ); 748 749sc = is < 4 ?sc_lo :sc_hi ; 750mbyte = is < 4 ?mb_lo :mb_hi ; 751sc = sc >> (8 * (is & 3 )); 752mbyte = mbyte >> (8 * (is & 3 )); 753sc &=0x3F ; 754mbyte &=0x3F ; 755 756const float16_t d = loadd .x * float16_t (sc ); 757const float16_t m = loadd .y * float16_t (mbyte ); 758#endif 759 760// sh is 0 or 4; mask 0x0F0F0F0F covers the four nibbles regardless (no inter-byte leakage). 761const uint sh = (idx & 0x20u ) >>3u ; 762const uint qs_w = (idx >>6 )* 8u + ((idx & 0x1Eu ) >>2 ); 763const uint qh_w = (idx & 0x1Eu ) >>2 ; 764 765const uint ql4 = (uint32_t (bl32 .block .qs [qs_w ]) >>sh )& 0x0F0F0F0Fu ; 766// qh stores bit `is` per element across 4 consecutive bytes; one shift+mask handles all 4. 767const uint qh4 = ((uint32_t (bl32 .block .qh [qh_w ]) >>is )& 0x01010101u ) <<4u ; 768 769const u8vec4 qi = unpack8 (ql4 |qh4 ); 770return f16vec4 (vec4 (qi )* vec4 (d )- vec4 (m )); 771} 772 773layout (buffer_reference ,std430 ,buffer_reference_align = 2 )buffer decodeBufQ6_K { 774block_q6_K block ; 775}; 776 777layout (buffer_reference ,std430 ,buffer_reference_align = 16 )buffer decodeBufQ6_K_packed16 { 778block_q6_K_packed16 block ; 779}; 780 781float16_t dequantFuncQ6_K (const in decodeBufQ6_K bl ,const in uint blockCoords [2 ],const in uint coordInBlock [2 ]) 782{ 783decodeBufQ6_K_packed16 bl16 = decodeBufQ6_K_packed16 (bl ); 784const uint idx = coordInBlock [1 ]; 785 786const uint b = (idx & 0x40 ) >>6 ;// 0,1 787const uint qhshift = (idx & 0x60 ) >>4 ;// 0,2,4,6 788const uint is = (idx & 0xF0 ) >>4 ;// 0..15 789 790const float16_t dscale = bl .block .d * float16_t (bl .block .scales [is ]); 791 792uint ql = uint32_t (bl16 .block .ql [((idx & 0x80 ) >>2 )+ ((idx & 0x3E ) >>1 )]); 793ql = (ql >> (b * 4 ))& 0x0F0F ; 794 795uint qh = uint32_t (bl16 .block .qh [((idx & 0x80 ) >>3 )+ ((idx & 0x1E ) >>1 )]); 796qh = ((qh >>qhshift )& 0x0303 ) <<4 ; 797 798int q = unpack8 (ql |qh )[idx & 1 ]; 799 800float16_t ret = dscale * float16_t (q - 32 ); 801 802return ret ; 803} 804 805f16vec4 dequantFuncQ6_K_v (const in decodeBufQ6_K bl ,const in uint blockCoords [2 ],const in uint coordInBlock [2 ]) 806{ 807decodeBufQ6_K_packed16 bl16 = decodeBufQ6_K_packed16 (bl ); 808const uint idx = coordInBlock [1 ]; 809 810const uint b = (idx & 0x40 ) >>6 ; 811const uint qhshift = (idx & 0x60 ) >>4 ;// 0,2,4,6 812const uint is = idx >>4 ; 813const uint sh = b * 4 ;// 0 or 4 814 815const float16_t dscale = bl .block .d * float16_t (bl .block .scales [is ]); 816 817const uint ql_i = ((idx & 0x80 ) >>2 )+ ((idx & 0x3E ) >>1 ); 818const uint qh_i = ((idx & 0x80 ) >>3 )+ ((idx & 0x1E ) >>1 ); 819 820// Two adjacent uint16 packed16 reads, combined into a uint32 in registers. 821// After this: byte j of qlw / qhw holds the data for element idx+j. 822const uint qlw = uint32_t (bl16 .block .ql [ql_i ]) | (uint32_t (bl16 .block .ql [ql_i + 1 ]) <<16 ); 823const uint qhw = uint32_t (bl16 .block .qh [qh_i ]) | (uint32_t (bl16 .block .qh [qh_i + 1 ]) <<16 ); 824 825// sh in {0,4} and qhshift in {0,2,4,6}: per-byte masks 0x0F / 0x03 keep only the 826// wanted bits with no inter-byte leakage; place qh's 2 bits at nibble high position. 827const uint ql4 = (qlw >>sh )& 0x0F0F0F0Fu ; 828const uint qh4 = ((qhw >>qhshift )& 0x03030303u ) <<4u ; 829 830const ivec4 qi = ivec4 (unpack8 (ql4 |qh4 )); 831return f16vec4 ((vec4 (qi )- vec4 (32.0f ))* vec4 (float (dscale ))); 832} 833 834#if defined(DATA_A_IQ1_S ) 835layout (buffer_reference ,std430 ,buffer_reference_align = 2 )buffer decodeBufIQ1_S { 836block_iq1_s block ; 837}; 838 839float16_t dequantFuncIQ1_S (const in decodeBufIQ1_S bl ,const in uint blockCoords [2 ],const in uint coordInBlock [2 ]) 840{ 841const float16_t d = bl .block .d ; 842const uint idx = coordInBlock [1 ]; 843 844const uint ib32 = (idx & 0xE0 ) >>5 ; 845const uint ib8 = (idx & 0xF8 ) >>3 ; 846 847const uint qh = bl .block .qh [ib32 ]; 848const uint qs = bl .block .qs [ib8 ]; 849const float dl = d * float (2 * bitfieldExtract (qh ,12 ,3 )+ 1 ); 850const float delta = ((qh & 0x8000 )!= 0 ) ?- IQ1S_DELTA :IQ1S_DELTA ; 851const uint grid = iq1s_grid [qs | (bitfieldExtract (qh ,3 * int (ib8 & 3 ),3 ) <<8 )]; 852 853float16_t ret = float16_t (dl )* (float16_t (bitfieldExtract (int (grid ),2 * int (idx %8 ),2 ))+ float16_t (delta )); 854return ret ; 855} 856 857f16vec4 dequantFuncIQ1_S_v (const in decodeBufIQ1_S bl ,const in uint blockCoords [2 ],const in uint coordInBlock [2 ]) 858{ 859const float16_t d = bl .block .d ; 860const uint idx = coordInBlock [1 ]; 861 862const uint ib32 = idx >>5 ; 863const uint ib8 = idx >>3 ; 864const int i8b = int (idx & 4 );// 0 or 4 865 866const uint qh = bl .block .qh [ib32 ]; 867const uint qs = bl .block .qs [ib8 ]; 868const float dl = float (d )* float (2 * bitfieldExtract (qh ,12 ,3 )+ 1 ); 869const float delta = ((qh & 0x8000u )!= 0u ) ?- IQ1S_DELTA :IQ1S_DELTA ; 870const uint grid = iq1s_grid [qs | (bitfieldExtract (qh ,3 * int (ib8 & 3 ),3 ) <<8 )]; 871 872const ivec4 q = ivec4 ( 873bitfieldExtract (int (grid ),2 * (i8b + 0 ),2 ), 874bitfieldExtract (int (grid ),2 * (i8b + 1 ),2 ), 875bitfieldExtract (int (grid ),2 * (i8b + 2 ),2 ), 876bitfieldExtract (int (grid ),2 * (i8b + 3 ),2 )); 877return f16vec4 ((vec4 (q )+ vec4 (delta ))* dl ); 878} 879#endif 880 881#if defined(DATA_A_IQ1_M ) 882layout (buffer_reference ,std430 ,buffer_reference_align = 2 )buffer decodeBufIQ1_M { 883block_iq1_m block ; 884}; 885 886layout (buffer_reference ,std430 ,buffer_reference_align = 8 )buffer decodeBufIQ1_M_packed64 { 887block_iq1_m_packed64 block ; 888}; 889 890float16_t dequantFuncIQ1_M (const in decodeBufIQ1_M bl ,const in uint blockCoords [2 ],const in uint coordInBlock [2 ]) 891{ 892decodeBufIQ1_M_packed64 bl64 = decodeBufIQ1_M_packed64 (bl ); 893const uint idx = coordInBlock [1 ]; 894 895uvec2 scales = unpack32 (bl64 .block .scales ); 896const float16_t d = uint16BitsToHalf (uint16_t (((scales .x & 0xF000 ) >>12 ) | ((scales .x & 0xF0000000 ) >>24 ) | ((scales .y & 0xF000 ) >>4 ) | ((scales .y & 0xF0000000 ) >>16 ))); 897 898const uint ib8 = (idx & 0xF8 ) >>3 ; 899const uint ib16 = (idx & 0xF0 ) >>4 ; 900const int i8 = int (idx %8 ); 901const uint sc = bl .block .scales [ib8 /8 ]; 902const uint qs = bl .block .qs [ib8 ]; 903const uint qh = bl .block .qh [ib16 ] >> (4 * (ib8 & 1 )); 904const float dl = 2 * bitfieldExtract (sc ,3 * int (ib16 & 3 ),3 )+ 1 ; 905const float delta = ((qh & 8 )!= 0 ) ?- IQ1S_DELTA :IQ1S_DELTA ; 906const uint grid = iq1s_grid [qs | ((qh & 7 ) <<8 )]; 907 908float16_t ret = d * float16_t (dl )* (float16_t (bitfieldExtract (int (grid ),2 * i8 ,2 ))+ float16_t (delta )); 909return ret ; 910} 911 912f16vec4 dequantFuncIQ1_M_v (const in decodeBufIQ1_M bl ,const in uint blockCoords [2 ],const in uint coordInBlock [2 ]) 913{ 914decodeBufIQ1_M_packed64 bl64 = decodeBufIQ1_M_packed64 (bl ); 915const uint idx = coordInBlock [1 ]; 916 917uvec2 scales = unpack32 (bl64 .block .scales ); 918const float16_t d = uint16BitsToHalf (uint16_t (((scales .x & 0xF000 ) >>12 ) | ((scales .x & 0xF0000000 ) >>24 ) | ((scales .y & 0xF000 ) >>4 ) | ((scales .y & 0xF0000000 ) >>16 ))); 919 920const uint ib8 = idx >>3 ; 921const uint ib16 = idx >>4 ; 922const int i8b = int (idx & 4 );// 0 or 4 -- i8 base for the V=4 group 923 924const uint sc = bl .block .scales [ib8 /8 ]; 925const uint qs = bl .block .qs [ib8 ]; 926const uint qh = bl .block .qh [ib16 ] >> (4 * (ib8 & 1 )); 927const float dl = 2.0 * float (bitfieldExtract (sc ,3 * int (ib16 & 3 ),3 ))+ 1.0 ; 928const float delta = ((qh & 8u )!= 0u ) ?- IQ1S_DELTA :IQ1S_DELTA ; 929const uint grid = iq1s_grid [qs | ((qh & 7u ) <<8 )]; 930 931const ivec4 q = ivec4 ( 932bitfieldExtract (int (grid ),2 * (i8b + 0 ),2 ), 933bitfieldExtract (int (grid ),2 * (i8b + 1 ),2 ), 934bitfieldExtract (int (grid ),2 * (i8b + 2 ),2 ), 935bitfieldExtract (int (grid ),2 * (i8b + 3 ),2 )); 936return f16vec4 ((vec4 (q )+ vec4 (delta ))* (float (d )* dl )); 937} 938#endif 939 940#if defined(DATA_A_IQ2_XXS ) 941layout (buffer_reference ,std430 ,buffer_reference_align = 2 )buffer decodeBufIQ2_XXS { 942block_iq2_xxs block ; 943}; 944 945layout (buffer_reference ,std430 ,buffer_reference_align = 2 )buffer decodeBufIQ2_XXS_packed16 { 946block_iq2_xxs_packed16 block ; 947}; 948 949float16_t dequantFuncIQ2_XXS (const in decodeBufIQ2_XXS bl ,const in uint blockCoords [2 ],const in uint coordInBlock [2 ]) 950{ 951decodeBufIQ2_XXS_packed16 bl16 = decodeBufIQ2_XXS_packed16 (bl ); 952const float16_t d = bl .block .d ; 953const uint idx = coordInBlock [1 ]; 954 955const uint ib32 = (idx & 0xE0 ) >>5 ;// 0..7 956const uint ib8 = (idx & 0x18 ) >>3 ;// 0..3 957const uint iqs = 8 * ib32 + ib8 ; 958 959const uint qs = bl .block .qs [iqs ]; 960const uint signscale = pack32 (u16vec2 (bl16 .block .qs [4 * ib32 + 2 ],bl16 .block .qs [4 * ib32 + 3 ])); 961 962const float dscale = float (bl .block .d )* 0.25 * (0.5 + float (signscale >>28 )); 963uint sign = bitfieldExtract (signscale ,7 * int (ib8 ),7 ); 964sign |=bitCount (sign ) <<7 ; 965 966uint g2 = iq2xxs_grid [qs ][(idx & 4 ) >>2 ]; 967g2 >>= (idx & 2 )* 8 ; 968const vec2 g = vec2 (unpack8 (g2 )); 969 970vec2 ret = dscale * g * ((sign & (1 << (idx & 7 )))!= 0 ?-1.0 hf :1.0 hf ); 971return float16_t (ret [idx & 1 ]); 972} 973 974f16vec4 dequantFuncIQ2_XXS_v (const in decodeBufIQ2_XXS bl ,const in uint blockCoords [2 ],const in uint coordInBlock [2 ]) 975{ 976decodeBufIQ2_XXS_packed16 bl16 = decodeBufIQ2_XXS_packed16 (bl ); 977const uint idx = coordInBlock [1 ]; 978 979const uint ib32 = idx >>5 ; 980const uint ib8 = (idx & 0x18 ) >>3 ; 981const uint iqs = 8 * ib32 + ib8 ; 982 983const uint qs = bl .block .qs [iqs ]; 984const uint signscale = pack32 (u16vec2 (bl16 .block .qs [4 * ib32 + 2 ],bl16 .block .qs [4 * ib32 + 3 ])); 985const float dscale = float (bl .block .d )* 0.25 * (0.5 + float (signscale >>28 )); 986 987uint sign = bitfieldExtract (signscale ,7 * int (ib8 ),7 ); 988sign |=bitCount (sign ) <<7 ; 989const uint sb = sign >> (idx & 7u ); 990 991const uint g2 = iq2xxs_grid [qs ][(idx & 4 ) >>2 ]; 992const u8vec4 g = unpack8 (g2 ); 993 994return f16vec4 ( 995dscale * float (g .x )* ((sb & 1u )!= 0u ?-1.0 :1.0 ), 996dscale * float (g .y )* ((sb & 2u )!= 0u ?-1.0 :1.0 ), 997dscale * float (g .z )* ((sb & 4u )!= 0u ?-1.0 :1.0 ), 998dscale * float (g .w )* ((sb & 8u )!= 0u ?-1.0 :1.0 )); 999} 1000#endif 1001 1002#if defined(DATA_A_IQ2_XS ) 1003layout (buffer_reference ,std430 ,buffer_reference_align = 2 )buffer decodeBufIQ2_XS { 1004block_iq2_xs block ; 1005}; 1006 1007float16_t dequantFuncIQ2_XS (const in decodeBufIQ2_XS bl ,const in uint blockCoords [2 ],const in uint coordInBlock [2 ]) 1008{ 1009const float16_t d = bl .block .d ; 1010const uint idx = coordInBlock [1 ]; 1011 1012const uint is = (idx & 0xE0 ) >>5 ;// 0..8 1013const uint sshift = (idx & 0x10 ) >>2 ;// 0,4 1014const uint iqs = (idx & 0xF8 ) >>3 ;// 0..63 1015 1016const uint16_t qs = bl .block .qs [iqs ]; 1017const float dscale = float (bl .block .d )* 0.25 * (0.5 + float ((bl .block .scales [is ] >>sshift )& 0xF )); 1018 1019uint sign = uint (qs >>9 ); 1020sign |=bitCount (sign ) <<7 ; 1021uint g2 = iq2xs_grid [qs & 0x1FF ][(idx & 4 ) >>2 ]; 1022g2 >>= (idx & 2 )* 8 ; 1023const vec2 g = vec2 (unpack8 (g2 )); 1024 1025vec2 ret = dscale * g * ((sign & (1 << (idx & 7 )))!= 0 ?-1.0 hf :1.0 hf ); 1026return float16_t (ret [idx & 1 ]); 1027} 1028 1029f16vec4 dequantFuncIQ2_XS_v (const in decodeBufIQ2_XS bl ,const in uint blockCoords [2 ],const in uint coordInBlock [2 ]) 1030{ 1031const uint idx = coordInBlock [1 ]; 1032 1033const uint is = idx >>5 ; 1034const uint sshift = (idx & 0x10 ) >>2 ; 1035const uint iqs = idx >>3 ; 1036 1037const uint16_t qs = bl .block .qs [iqs ]; 1038const float dscale = float (bl .block .d )* 0.25 * (0.5 + float ((bl .block .scales [is ] >>sshift )& 0xF )); 1039 1040uint sign = uint (qs >>9 ); 1041sign |=bitCount (sign ) <<7 ; 1042const uint sb = sign >> (idx & 7u ); 1043 1044const uint g2 = iq2xs_grid [qs & 0x1FF ][(idx & 4 ) >>2 ]; 1045const u8vec4 g = unpack8 (g2 ); 1046 1047return f16vec4 ( 1048dscale * float (g .x )* ((sb & 1u )!= 0u ?-1.0 :1.0 ), 1049dscale * float (g .y )* ((sb & 2u )!= 0u ?-1.0 :1.0 ), 1050dscale * float (g .z )* ((sb & 4u )!= 0u ?-1.0 :1.0 ), 1051dscale * float (g .w )* ((sb & 8u )!= 0u ?-1.0 :1.0 )); 1052} 1053#endif 1054 1055#if defined(DATA_A_IQ2_S ) 1056layout (buffer_reference ,std430 ,buffer_reference_align = 2 )buffer decodeBufIQ2_S { 1057block_iq2_s block ; 1058}; 1059 1060float16_t dequantFuncIQ2_S (const in decodeBufIQ2_S bl ,const in uint blockCoords [2 ],const in uint coordInBlock [2 ]) 1061{ 1062uint idx = coordInBlock [1 ]; 1063 1064const uint ib32 = (idx & 0xE0 ) >>5 ;// 0..7 1065const uint ib8 = (idx & 0xF8 ) >>3 ;// 0..31 1066const uint qhshift = 2 * (ib8 %4 ); 1067 1068const uint scale = (bl .block .scales [ib32 ] >> ((idx & 0x10 ) >>2 ))& 0xf ; 1069const uint qs = bl .block .qs [ib8 ]; 1070const uint qh = bl .block .qh [ib32 ]; 1071const uint sign = bl .block .qs [QUANT_K_IQ2_S /8 + ib8 ] >> (idx & 0x6 ); 1072 1073const float d = float (bl .block .d ); 1074const float db = d * 0.25 * (0.5 + scale ); 1075const ivec2 sign01 = 1 - (2 & ivec2 (sign <<1 ,sign )); 1076uint g2 = iq2s_grid [qs | ((qh << (8 - qhshift ))& 0x300 )][(idx & 4 ) >>2 ]; 1077g2 >>= (idx & 2 )* 8 ; 1078const vec2 v = db * vec2 (sign01 )* vec2 (unpack8 (g2 )); 1079return float16_t (v [idx & 1 ]); 1080} 1081 1082f16vec4 dequantFuncIQ2_S_v (const in decodeBufIQ2_S bl ,const in uint blockCoords [2 ],const in uint coordInBlock [2 ]) 1083{ 1084const uint idx = coordInBlock [1 ]; 1085 1086const uint ib32 = idx >>5 ; 1087const uint ib8 = idx >>3 ; 1088const uint qhshift = 2 * (ib8 %4 ); 1089 1090const uint scale = (bl .block .scales [ib32 ] >> ((idx & 0x10 ) >>2 ))& 0xf ; 1091const uint qs = bl .block .qs [ib8 ]; 1092const uint qh = bl .block .qh [ib32 ]; 1093const uint sb = uint (bl .block .qs [QUANT_K_IQ2_S /8 + ib8 ]) >> (idx & 0x6u ); 1094 1095const float d = float (bl .block .d ); 1096const float db = d * 0.25 * (0.5 + scale ); 1097 1098const uint g2 = iq2s_grid [qs | ((qh << (8 - qhshift ))& 0x300 )][(idx & 4 ) >>2 ]; 1099const u8vec4 g = unpack8 (g2 ); 1100 1101return f16vec4 ( 1102db * float (g .x )* ((sb & 1u )!= 0u ?-1.0 :1.0 ), 1103db * float (g .y )* ((sb & 2u )!= 0u ?-1.0 :1.0 ), 1104db * float (g .z )* ((sb & 4u )!= 0u ?-1.0 :1.0 ), 1105db * float (g .w )* ((sb & 8u )!= 0u ?-1.0 :1.0 )); 1106} 1107#endif 1108 1109#if defined(DATA_A_IQ3_XXS ) 1110layout (buffer_reference ,std430 ,buffer_reference_align = 2 )buffer decodeBufIQ3_XXS { 1111block_iq3_xxs block ; 1112}; 1113 1114layout (buffer_reference ,std430 ,buffer_reference_align = 2 )buffer decodeBufIQ3_XXS_packed16 { 1115block_iq3_xxs_packed16 block ; 1116}; 1117 1118float16_t dequantFuncIQ3_XXS (const in decodeBufIQ3_XXS bl ,const in uint blockCoords [2 ],const in uint coordInBlock [2 ]) 1119{ 1120decodeBufIQ3_XXS_packed16 bl16 = decodeBufIQ3_XXS_packed16 (bl ); 1121uint idx = coordInBlock [1 ]; 1122 1123const uint iqs = (idx & 0xFC ) >>2 ;// 0..63 1124const uint is = QUANT_K_IQ3_XXS /4 + ((idx & 0xE0 ) >>3 );// 8 values 1125 1126const float d = float (bl .block .d ); 1127const uint qs = bl .block .qs [iqs ]; 1128const uint signs = pack32 (u16vec2 ( 1129bl16 .block .qs [is /2 + 0 ], 1130bl16 .block .qs [is /2 + 1 ] 1131 )); 1132const float db = d * 0.5 * (0.5 + (signs >>28 )); 1133const uint32_t sign7 = bitfieldExtract (signs ,7 * (int (iqs /2 ) %4 ),7 ); 1134const uint sign = (sign7 | (bitCount (sign7 ) <<7 )) >> (idx & 0x6 ); 1135const ivec2 sign01 = ivec2 (1 - (2 & ivec2 (sign <<1 ,sign ))); 1136const uint grid = iq3xxs_grid [qs ] >> (16 * ((idx & 2 ) >>1 )); 1137const vec2 v = db * vec2 (sign01 )* vec2 (unpack8 (grid ).xy ); 1138return float16_t (v [idx & 1 ]); 1139} 1140 1141f16vec4 dequantFuncIQ3_XXS_v (const in decodeBufIQ3_XXS bl ,const in uint blockCoords [2 ],const in uint coordInBlock [2 ]) 1142{ 1143decodeBufIQ3_XXS_packed16 bl16 = decodeBufIQ3_XXS_packed16 (bl ); 1144const uint idx = coordInBlock [1 ]; 1145 1146const uint iqs = idx >>2 ; 1147const uint is = QUANT_K_IQ3_XXS /4 + ((idx & 0xE0 ) >>3 ); 1148 1149const float d = float (bl .block .d ); 1150const uint qs = bl .block .qs [iqs ]; 1151const uint signs = pack32 (u16vec2 (bl16 .block .qs [is /2 + 0 ],bl16 .block .qs [is /2 + 1 ])); 1152const float db = d * 0.5 * (0.5 + (signs >>28 )); 1153 1154const uint sign7 = bitfieldExtract (signs ,7 * (int (iqs /2 ) %4 ),7 ); 1155const uint sb = (sign7 | (bitCount (sign7 ) <<7 )) >> (idx & 0x6u ); 1156 1157const uint grid = iq3xxs_grid [qs ]; 1158const u8vec4 g = unpack8 (grid ); 1159 1160return f16vec4 ( 1161db * float (g .x )* ((sb & 1u )!= 0u ?-1.0 :1.0 ), 1162db * float (g .y )* ((sb & 2u )!= 0u ?-1.0 :1.0 ), 1163db * float (g .z )* ((sb & 4u )!= 0u ?-1.0 :1.0 ), 1164db * float (g .w )* ((sb & 8u )!= 0u ?-1.0 :1.0 )); 1165} 1166#endif 1167 1168#if defined(DATA_A_IQ3_S ) 1169layout (buffer_reference ,std430 ,buffer_reference_align = 2 )buffer decodeBufIQ3_S { 1170block_iq3_s block ; 1171}; 1172 1173float16_t dequantFuncIQ3_S (const in decodeBufIQ3_S bl ,const in uint blockCoords [2 ],const in uint coordInBlock [2 ]) 1174{ 1175uint idx = coordInBlock [1 ]; 1176 1177const uint iqs = (idx & 0xFC ) >>2 ;// 0..63 1178const uint iqh = (idx & 0xE0 ) >>5 ; 1179 1180const float d = float (bl .block .d ); 1181const uint qs = bl .block .qs [iqs ]; 1182const uint qh = bl .block .qh [iqh ]; 1183const int8_t sign = int8_t (bl .block .signs [iqs /2 ] >> (idx & 0x6 )); 1184const uint scale = bl .block .scales [iqs /16 ]; 1185const ivec2 sign01 = ivec2 (1 - (2 & ivec2 (sign <<1 ,sign ))); 1186const float db = d * (1 + 2 * ((scale >> (4 * (iqh & 1 )))& 0xf )); 1187const uint32_t grid = iq3s_grid [qs | ((qh << (8 - (iqs %8 )))& 256 )] >> ((idx & 2 ) <<3 ); 1188const vec2 v = db * vec2 (sign01 )* vec2 (unpack8 (grid ).xy ); 1189 1190return float16_t (v [idx & 1 ]); 1191} 1192 1193f16vec4 dequantFuncIQ3_S_v (const in decodeBufIQ3_S bl ,const in uint blockCoords [2 ],const in uint coordInBlock [2 ]) 1194{ 1195const uint idx = coordInBlock [1 ]; 1196 1197const uint iqs = idx >>2 ; 1198const uint iqh = idx >>5 ; 1199 1200const float d = float (bl .block .d ); 1201const uint qs = bl .block .qs [iqs ]; 1202const uint qh = bl .block .qh [iqh ]; 1203const uint sb = uint (bl .block .signs [iqs /2 ]) >> (idx & 0x6u ); 1204const uint scale = bl .block .scales [iqs /16 ]; 1205const float db = d * (1 + 2 * ((scale >> (4 * (iqh & 1 )))& 0xf )); 1206 1207const uint grid = iq3s_grid [qs | ((qh << (8 - (iqs %8 )))& 256 )]; 1208const u8vec4 g = unpack8 (grid ); 1209 1210return f16vec4 ( 1211db * float (g .x )* ((sb & 1u )!= 0u ?-1.0 :1.0 ), 1212db * float (g .y )* ((sb & 2u )!= 0u ?-1.0 :1.0 ), 1213db * float (g .z )* ((sb & 4u )!= 0u ?-1.0 :1.0 ), 1214db * float (g .w )* ((sb & 8u )!= 0u ?-1.0 :1.0 )); 1215} 1216#endif 1217 1218#if defined(DATA_A_IQ4_XS ) 1219layout (buffer_reference ,std430 ,buffer_reference_align = 2 )buffer decodeBufIQ4_XS { 1220block_iq4_xs block ; 1221}; 1222 1223layout (buffer_reference ,std430 ,buffer_reference_align = 4 )buffer decodeBufIQ4_XS_packed32 { 1224block_iq4_xs_packed32 block ; 1225}; 1226 1227float16_t dequantFuncIQ4_XS (const in decodeBufIQ4_XS bl ,const in uint blockCoords [2 ],const in uint coordInBlock [2 ]) 1228{ 1229const float16_t d = bl .block .d ; 1230const uint idx = coordInBlock [1 ]; 1231 1232const uint ib32 = (idx & 0xE0 ) >>5 ;// 0..7 1233 1234const uint sl = (bl .block .scales_l [ib32 /2 ] >> (4 * (ib32 & 1 )))& 0xF ; 1235const uint sh = ((bl .block .scales_h ) >> (2 * ib32 ))& 3 ; 1236const uint qshift = (idx & 16 ) >>2 ; 1237const uint q = (bl .block .qs [16 * ib32 + (idx %16 )] >>qshift )& 0xF ; 1238 1239float16_t ret = d * float16_t (int (sl | (sh <<4 ))- 32 )* float16_t (kvalues_iq4nl [q ]); 1240return ret ; 1241} 1242 1243f16vec4 dequantFuncIQ4_XS_v (const in decodeBufIQ4_XS bl ,const in uint blockCoords [2 ],const in uint coordInBlock [2 ]) 1244{ 1245decodeBufIQ4_XS_packed32 bl32 = decodeBufIQ4_XS_packed32 (bl ); 1246const float16_t d = bl .block .d ; 1247const uint idx = coordInBlock [1 ]; 1248 1249const uint ib32 = idx >>5 ;// 0..7 1250const uint sl = (bl32 .block .scales_l >> (4 * ib32 ))& 0xF ; 1251const uint sh = (uint (bl32 .block .scales_h ) >> (2 * ib32 ))& 0x3 ; 1252const uint qshift = (idx & 0x10 ) >>2 ;// {0, 4} 1253const uint qs_w = 4 * ib32 + ((idx & 0xC ) >>2 );// iqs / 4, in [0,32) 1254 1255const float16_t dl = d * float16_t (int (sl | (sh <<4 ))- 32 ); 1256 1257const uint qsw = bl32 .block .qs [qs_w ]; 1258const u8vec4 qv = unpack8 ((qsw >>qshift )& 0x0F0F0F0Fu ); 1259const vec4 ret = vec4 ( 1260float (kvalues_iq4nl [qv .x ]), 1261float (kvalues_iq4nl [qv .y ]), 1262float (kvalues_iq4nl [qv .z ]), 1263float (kvalues_iq4nl [qv .w ]))* float (dl ); 1264return f16vec4 (ret ); 1265} 1266#endif 1267 1268#if defined(DATA_A_IQ4_NL ) 1269layout (buffer_reference ,std430 ,buffer_reference_align = 2 )buffer decodeBufIQ4_NL { 1270block_iq4_nl block ; 1271}; 1272 1273layout (buffer_reference ,std430 ,buffer_reference_align = 2 )buffer decodeBufIQ4_NL_packed16 { 1274block_iq4_nl_packed16 block ; 1275}; 1276 1277float16_t dequantFuncIQ4_NL (const in decodeBufIQ4_NL bl ,const in uint blockCoords [2 ],const in uint coordInBlock [2 ]) 1278{ 1279const float16_t d = bl .block .d ; 1280const uint idx = coordInBlock [1 ]; 1281const uint iqs = idx & 0xF ; 1282const uint shift = (idx & 0x10 ) >>2 ; 1283uint32_t qs = bl .block .qs [iqs ]; 1284qs >>=shift ; 1285qs &=0xF ; 1286float16_t ret = float16_t (kvalues_iq4nl [qs ])* d ; 1287return ret ; 1288} 1289 1290f16vec4 dequantFuncIQ4_NL_v (const in decodeBufIQ4_NL bl ,const in uint blockCoords [2 ],const in uint coordInBlock [2 ]) 1291{ 1292decodeBufIQ4_NL_packed16 bl16 = decodeBufIQ4_NL_packed16 (bl ); 1293const float16_t d = bl .block .d ; 1294const uint idx = coordInBlock [1 ]; 1295const uint shift = (idx & 0x10 ) >>2 ;// 0 or 4 1296const uint qs_i = (idx & 0xC ) >>1 ;// packed16 word index, in {0,2,4,6} 1297const uint qsw = uint32_t (bl16 .block .qs [qs_i ]) 1298 | (uint32_t (bl16 .block .qs [qs_i + 1u ]) <<16 ); 1299// shift in {0,4}: per-byte mask 0x0F isolates the wanted nibble in each byte. 1300const u8vec4 q = unpack8 ((qsw >>shift )& 0x0F0F0F0Fu ); 1301return f16vec4 ( 1302float (d )* float (kvalues_iq4nl [q .x ]), 1303float (d )* float (kvalues_iq4nl [q .y ]), 1304float (d )* float (kvalues_iq4nl [q .z ]), 1305float (d )* float (kvalues_iq4nl [q .w ])); 1306} 1307#endif 1308 1309#if defined(DATA_A_MXFP4 ) 1310layout (buffer_reference ,std430 ,buffer_reference_align = 2 )buffer decodeBufMXFP4 { 1311block_mxfp4 block ; 1312}; 1313 1314float16_t dequantFuncMXFP4 (const in decodeBufMXFP4 bl ,const in uint blockCoords [2 ],const in uint coordInBlock [2 ]) 1315{ 1316const float d = e8m0_to_fp32 (bl .block .e ); 1317const uint idx = coordInBlock [1 ]; 1318const uint iqs = idx & 0xF ; 1319const uint shift = (idx & 0x10 ) >>2 ; 1320#ifdef USE_OCP_FP4 1321return float16_t (bitcastExtractfe2m1EXT (bl .block .qs [iqs ],shift ))* float16_t (d ); 1322#else 1323uint32_t qs = bl .block .qs [iqs ]; 1324qs >>=shift ; 1325qs &=0xF ; 1326float16_t ret = float16_t (kvalues_mxfp4 [qs ]* d * 0.5 ); 1327return ret ; 1328#endif 1329} 1330 1331f16vec4 dequantFuncMXFP4_v (const in decodeBufMXFP4 bl ,const in uint blockCoords [2 ],const in uint coordInBlock [2 ]) 1332{ 1333const float d = e8m0_to_fp32 (bl .block .e ); 1334const uint idx = coordInBlock [1 ]; 1335const uint iqs = idx & 0xF ; 1336const uint shift = (idx & 0x10 ) >>2 ; 1337#ifdef USE_OCP_FP4 1338const fe2m1vec4 qv = bitcastExtractfe2m1EXT ( 1339u8vec4 ( 1340bl .block .qs [iqs ], 1341bl .block .qs [iqs + 1u ], 1342bl .block .qs [iqs + 2u ], 1343bl .block .qs [iqs + 3u ]), 1344shift ); 1345return f16vec4 (qv )* float16_t (d ); 1346#else 1347uvec4 qv = uvec4 ( 1348uint (bl .block .qs [iqs ]), 1349uint (bl .block .qs [iqs + 1u ]), 1350uint (bl .block .qs [iqs + 2u ]), 1351uint (bl .block .qs [iqs + 3u ])); 1352qv = (qv >>shift )& 0xFu ; 1353const vec4 ret = vec4 ( 1354float (kvalues_mxfp4 [qv .x ]), 1355float (kvalues_mxfp4 [qv .y ]), 1356float (kvalues_mxfp4 [qv .z ]), 1357float (kvalues_mxfp4 [qv .w ]))* d * 0.5f ; 1358return f16vec4 (ret ); 1359#endif 1360} 1361#endif 1362 1363#if defined(DATA_A_NVFP4 ) 1364layout (buffer_reference ,std430 ,buffer_reference_align = 4 )buffer decodeBufNVFP4 { 1365block_nvfp4 block ; 1366}; 1367 1368layout (buffer_reference ,std430 ,buffer_reference_align = 4 )buffer decodeBufNVFP4_packed32 { 1369block_nvfp4_packed32 block ; 1370}; 1371 1372float16_t dequantFuncNVFP4 (const in decodeBufNVFP4 bl ,const in uint blockCoords [2 ],const in uint coordInBlock [2 ]) 1373{ 1374const uint idx = coordInBlock [1 ]; 1375const uint sub = (idx & 0x30 ) >>4 ; 1376const uint iqs = ((idx & 0x30 ) >>1 )+ (idx & 0x7 ); 1377const uint shift = (idx & 0x8 ) >>1 ; 1378#ifdef USE_OCP_FP4 1379const float16_t d = float16_t (ue4m3_from_bits (bl .block .d [sub ])); 1380return float16_t (bitcastExtractfe2m1EXT (bl .block .qs [iqs ],shift ))* d ; 1381#else 1382const float d = ue4m3_to_fp32 (bl .block .d [sub ]); 1383uint qs = uint (bl .block .qs [iqs ]); 1384qs = (qs >>shift )& 0xF ; 1385return float16_t (kvalues_mxfp4 [qs ]* d * 0.5 ); 1386#endif 1387} 1388 1389f16vec4 dequantFuncNVFP4_v (const in decodeBufNVFP4 bl ,const in uint blockCoords [2 ],const in uint coordInBlock [2 ]) 1390{ 1391decodeBufNVFP4_packed32 bl32 = decodeBufNVFP4_packed32 (bl ); 1392const uint idx = coordInBlock [1 ]; 1393const uint sub = idx >>4 ; 1394const uint qs_w = ((idx & 0x30 ) >>3 )+ ((idx & 0x4u ) >>2 );// iqs / 4, in [0,8) 1395const uint shift = (idx & 0x8 ) >>1 ; 1396 1397const uint qsw = uint32_t (bl32 .block .qs [qs_w ]); 1398#ifdef USE_OCP_FP4 1399const float16_t d = float16_t (ue4m3_from_bits (bl .block .d [sub ])); 1400const fe2m1vec4 qv = bitcastExtractfe2m1EXT (unpack8 (qsw ),shift ); 1401return f16vec4 (qv )* d ; 1402#else 1403const float d = ue4m3_to_fp32 (bl .block .d [sub ]); 1404const u8vec4 qv = unpack8 ((qsw >>shift )& 0x0F0F0F0Fu ); 1405const vec4 ret = vec4 ( 1406float (kvalues_mxfp4 [qv .x ]), 1407float (kvalues_mxfp4 [qv .y ]), 1408float (kvalues_mxfp4 [qv .z ]), 1409float (kvalues_mxfp4 [qv .w ]))* d * 0.5f ; 1410return f16vec4 (ret ); 1411#endif 1412} 1413#endif 1414 1415#if defined(DATA_A_Q1_0 ) 1416#define dequantFuncA dequantFuncQ1_0 1417#define dequantFuncA_v dequantFuncQ1_0_v 1418#elif defined(DATA_A_Q2_0 ) 1419#define dequantFuncA dequantFuncQ2_0 1420#define dequantFuncA_v dequantFuncQ2_0_v 1421#elif defined(DATA_A_Q4_0 ) 1422#define dequantFuncA dequantFuncQ4_0 1423#define dequantFuncA_v dequantFuncQ4_0_v 1424#elif defined(DATA_A_Q4_1 ) 1425#define dequantFuncA dequantFuncQ4_1 1426#define dequantFuncA_v dequantFuncQ4_1_v 1427#elif defined(DATA_A_Q5_0 ) 1428#define dequantFuncA dequantFuncQ5_0 1429#define dequantFuncA_v dequantFuncQ5_0_v 1430#elif defined(DATA_A_Q5_1 ) 1431#define dequantFuncA dequantFuncQ5_1 1432#define dequantFuncA_v dequantFuncQ5_1_v 1433#elif defined(DATA_A_Q8_0 ) 1434#define dequantFuncA dequantFuncQ8_0 1435#define dequantFuncA_v dequantFuncQ8_0_v 1436#elif defined(DATA_A_TQ1_0 ) 1437#define dequantFuncA dequantFuncTQ1_0 1438#elif defined(DATA_A_TQ2_0 ) 1439#define dequantFuncA dequantFuncTQ2_0 1440#define dequantFuncA_v dequantFuncTQ2_0_v 1441#elif defined(DATA_A_Q2_K ) 1442#define dequantFuncA dequantFuncQ2_K 1443#define dequantFuncA_v dequantFuncQ2_K_v 1444#elif defined(DATA_A_Q3_K ) 1445#define dequantFuncA dequantFuncQ3_K 1446#define dequantFuncA_v dequantFuncQ3_K_v 1447#elif defined(DATA_A_Q4_K ) 1448#define dequantFuncA dequantFuncQ4_K 1449#define dequantFuncA_v dequantFuncQ4_K_v 1450#define fetch_scales fetch_scalesQ4_K 1451#define store_scales store_scalesQ4_K 1452#elif defined(DATA_A_Q5_K ) 1453#define dequantFuncA dequantFuncQ5_K 1454#define dequantFuncA_v dequantFuncQ5_K_v 1455#define fetch_scales fetch_scalesQ5_K 1456#define store_scales store_scalesQ4_K 1457#elif defined(DATA_A_Q6_K ) 1458#define dequantFuncA dequantFuncQ6_K 1459#define dequantFuncA_v dequantFuncQ6_K_v 1460#elif defined(DATA_A_IQ1_S ) 1461#define dequantFuncA dequantFuncIQ1_S 1462#define dequantFuncA_v dequantFuncIQ1_S_v 1463#elif defined(DATA_A_IQ1_M ) 1464#define dequantFuncA dequantFuncIQ1_M 1465#define dequantFuncA_v dequantFuncIQ1_M_v 1466#elif defined(DATA_A_IQ2_XXS ) 1467#define dequantFuncA dequantFuncIQ2_XXS 1468#define dequantFuncA_v dequantFuncIQ2_XXS_v 1469#elif defined(DATA_A_IQ2_XS ) 1470#define dequantFuncA dequantFuncIQ2_XS 1471#define dequantFuncA_v dequantFuncIQ2_XS_v 1472#elif defined(DATA_A_IQ2_S ) 1473#define dequantFuncA dequantFuncIQ2_S 1474#define dequantFuncA_v dequantFuncIQ2_S_v 1475#elif defined(DATA_A_IQ3_XXS ) 1476#define dequantFuncA dequantFuncIQ3_XXS 1477#define dequantFuncA_v dequantFuncIQ3_XXS_v 1478#elif defined(DATA_A_IQ3_S ) 1479#define dequantFuncA dequantFuncIQ3_S 1480#define dequantFuncA_v dequantFuncIQ3_S_v 1481#elif defined(DATA_A_IQ4_XS ) 1482#define dequantFuncA dequantFuncIQ4_XS 1483#define dequantFuncA_v dequantFuncIQ4_XS_v 1484#elif defined(DATA_A_IQ4_NL ) 1485#define dequantFuncA dequantFuncIQ4_NL 1486#define dequantFuncA_v dequantFuncIQ4_NL_v 1487#elif defined(DATA_A_MXFP4 ) 1488#define dequantFuncA dequantFuncMXFP4 1489#define dequantFuncA_v dequantFuncMXFP4_v 1490#elif defined(DATA_A_NVFP4 ) 1491#define dequantFuncA dequantFuncNVFP4 1492#define dequantFuncA_v dequantFuncNVFP4_v 1493#elif defined(DATA_A_F32 ) 1494#define dequantFuncA dequantFuncF32 1495#endif