char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
b615f5b4b
char/aliceai-t5-moe
1#pragma once 2 3#include "common.h" 4 5#define GGML_COMMON_DECL_METAL 6#define GGML_COMMON_IMPL_METAL 7#if defined(GGML_METAL_EMBED_LIBRARY ) 8__embed_ggml - common .h__ 9#else 10#include "ggml-common.h" 11#endif 12 13#define QK_NL 16 // shared by mul_mm and get_rows_q instantiations 14 15// NOTE: this is not dequantizing - we are simply fitting the template 16template < typename type4x4 > 17void dequantize_f32 (device const float4x4 * src ,short il ,thread type4x4 & reg ) { 18reg = (type4x4 )(* src ); 19} 20 21template < typename type4 > 22void dequantize_f32_t4 (device const float4 * src ,short il ,thread type4 & reg ) { 23reg = (type4 )(* src ); 24} 25 26template < typename type4x4 > 27void dequantize_f16 (device const half4x4 * src ,short il ,thread type4x4 & reg ) { 28reg = (type4x4 )(* src ); 29} 30 31template < typename type4 > 32void dequantize_f16_t4 (device const half4 * src ,short il ,thread type4 & reg ) { 33reg = (type4 )(* (src )); 34} 35 36#if defined(GGML_METAL_HAS_BF16 ) 37template < typename type4x4 > 38void dequantize_bf16 (device const bfloat4x4 * src ,short il ,thread type4x4 & reg ) { 39reg = (type4x4 )(* src ); 40} 41 42template < typename type4 > 43void dequantize_bf16_t4 (device const bfloat4 * src ,short il ,thread type4 & reg ) { 44reg = (type4 )(* (src )); 45} 46#endif 47 48template < typename type4x4 > 49void dequantize_q1_0 (device const block_q1_0 * xb ,short il ,thread type4x4 & reg ) { 50device const uint8_t * qs = xb -> qs ; 51const float d = xb -> d ; 52const float neg_d = - d ; 53 54const int byte_offset = il * 2 ; // il*16 bits = il*2 bytes 55const uint8_t b0 = qs [ byte_offset ]; 56const uint8_t b1 = qs [ byte_offset + 1 ]; 57 58float4x4 reg_f ; 59 60reg_f [ 0 ][ 0 ] = select ( neg_d , d , bool ( b0 & 0x01 )); 61reg_f [ 0 ][ 1 ] = select ( neg_d , d , bool ( b0 & 0x02 )); 62reg_f [ 0 ][ 2 ] = select ( neg_d , d , bool ( b0 & 0x04 )); 63reg_f [ 0 ][ 3 ] = select ( neg_d , d , bool ( b0 & 0x08 )); 64reg_f [ 1 ][ 0 ] = select ( neg_d , d , bool ( b0 & 0x10 )); 65reg_f [ 1 ][ 1 ] = select ( neg_d , d , bool ( b0 & 0x20 )); 66reg_f [ 1 ][ 2 ] = select ( neg_d , d , bool ( b0 & 0x40 )); 67reg_f [ 1 ][ 3 ] = select ( neg_d , d , bool ( b0 & 0x80 )); 68 69reg_f [ 2 ][ 0 ] = select ( neg_d , d , bool ( b1 & 0x01 )); 70reg_f [ 2 ][ 1 ] = select ( neg_d , d , bool ( b1 & 0x02 )); 71reg_f [ 2 ][ 2 ] = select ( neg_d , d , bool ( b1 & 0x04 )); 72reg_f [ 2 ][ 3 ] = select ( neg_d , d , bool ( b1 & 0x08 )); 73reg_f [ 3 ][ 0 ] = select ( neg_d , d , bool ( b1 & 0x10 )); 74reg_f [ 3 ][ 1 ] = select ( neg_d , d , bool ( b1 & 0x20 )); 75reg_f [ 3 ][ 2 ] = select ( neg_d , d , bool ( b1 & 0x40 )); 76reg_f [ 3 ][ 3 ] = select ( neg_d , d , bool ( b1 & 0x80 )); 77 78reg = ( type4x4 ) reg_f ; 79} 80 81template < typename type4 > 82void dequantize_q1_0_t4 ( device const block_q1_0 * xb , short il , thread type4 & reg ) { 83const float d = xb -> d ; 84const float neg_d = - d ; 85const int base = il * 4 ; 86const uint8_t byte = xb -> qs [ base / 8 ]; 87const int s = base % 8 ; 88 89float4 reg_f ; 90reg_f [ 0 ] = select ( neg_d , d , bool (( byte >> ( s )) & 1 )); 91reg_f [ 1 ] = select ( neg_d , d , bool (( byte >> ( s + 1 )) & 1 )); 92reg_f [ 2 ] = select ( neg_d , d , bool (( byte >> ( s + 2 )) & 1 )); 93reg_f [ 3 ] = select ( neg_d , d , bool (( byte >> ( s + 3 )) & 1 )); 94 95reg = ( type4 ) reg_f ; 96} 97 98template < typename type4x4 > 99void dequantize_q2_0 ( device const block_q2_0 * xb , short il , thread type4x4 & reg ) { 100device const uint8_t * qs = xb -> qs ; 101const float d = xb -> d ; 102 103const int byte_offset = il * 4 ; // il*16 elements = il*4 bytes (4 elements per byte) 104float4x4 reg_f ; 105 106for ( int i = 0 ; i < 4 ; i ++) { 107const uint8_t b = qs [ byte_offset + i ]; 108reg_f [ i ][ 0 ] = (( float )(( b >> 0 ) & 3 ) - 1.0f ) * d ; 109reg_f [ i ][ 1 ] = (( float )(( b >> 2 ) & 3 ) - 1.0f ) * d ; 110reg_f [ i ][ 2 ] = (( float )(( b >> 4 ) & 3 ) - 1.0f ) * d ; 111reg_f [ i ][ 3 ] = (( float )(( b >> 6 ) & 3 ) - 1.0f ) * d ; 112} 113 114reg = ( type4x4 ) reg_f ; 115} 116 117template < typename type4 > 118void dequantize_q2_0_t4 ( device const block_q2_0 * xb , short il , thread type4 & reg ) { 119const float d = xb -> d ; 120const uint8_t b = xb -> qs [ il ]; 121 122float4 reg_f ; 123reg_f [ 0 ] = (( float )(( b >> 0 ) & 3 ) - 1.0f ) * d ; 124reg_f [ 1 ] = (( float )(( b >> 2 ) & 3 ) - 1.0f ) * d ; 125reg_f [ 2 ] = (( float )(( b >> 4 ) & 3 ) - 1.0f ) * d ; 126reg_f [ 3 ] = (( float )(( b >> 6 ) & 3 ) - 1.0f ) * d ; 127 128reg = ( type4 ) reg_f ; 129} 130 131template < typename type4x4 > 132void dequantize_q4_0 ( device const block_q4_0 * xb , short il , thread type4x4 & reg ) { 133device const uint16_t * qs = (( device const uint16_t *) xb + 1 ); 134const float d1 = il ? ( xb -> d / 16. h ) : xb -> d ; 135const float d2 = d1 / 256.f ; 136const float md = -8. h * xb -> d ; 137const ushort mask0 = il ? 0x00F0 : 0x000F ; 138const ushort mask1 = mask0 << 8 ; 139 140float4x4 reg_f ; 141 142for ( int i = 0 ; i < 8 ; i ++) { 143reg_f [ i / 2 ][ 2 *( i % 2 ) + 0 ] = d1 * ( qs [ i ] & mask0 ) + md ; 144reg_f [ i / 2 ][ 2 *( i % 2 ) + 1 ] = d2 * ( qs [ i ] & mask1 ) + md ; 145} 146 147reg = ( type4x4 ) reg_f ; 148} 149 150template < typename type4 > 151void dequantize_q4_0_t4 ( device const block_q4_0 * xb , short il , thread type4 & reg ) { 152device const uint16_t * qs = (( device const uint16_t *) xb + 1 ); 153const float d1 = ( il / 4 ) ? ( xb -> d / 16. h ) : xb -> d ; 154const float d2 = d1 / 256.f ; 155const float md = -8. h * xb -> d ; 156const ushort mask0 = ( il / 4 ) ? 0x00F0 : 0x000F ; 157const ushort mask1 = mask0 << 8 ; 158 159for ( int i = 0 ; i < 2 ; i ++) { 160reg [ 2 * i + 0 ] = d1 * ( qs [ 2 *( il % 4 ) + i ] & mask0 ) + md ; 161reg [ 2 * i + 1 ] = d2 * ( qs [ 2 *( il % 4 ) + i ] & mask1 ) + md ; 162} 163} 164 165 166 167template < typename type4x4 > 168void dequantize_q4_1 ( device const block_q4_1 * xb , short il , thread type4x4 & reg ) { 169device const uint16_t * qs = (( device const uint16_t *) xb + 2 ); 170const float d1 = il ? ( xb -> d / 16. h ) : xb -> d ; 171const float d2 = d1 / 256.f ; 172const float m = xb -> m ; 173const ushort mask0 = il ? 0x00F0 : 0x000F ; 174const ushort mask1 = mask0 << 8 ; 175 176float4x4 reg_f ; 177 178for ( int i = 0 ; i < 8 ; i ++) { 179reg_f [ i / 2 ][ 2 *( i % 2 ) + 0 ] = (( qs [ i ] & mask0 ) * d1 ) + m ; 180reg_f [ i / 2 ][ 2 *( i % 2 ) + 1 ] = (( qs [ i ] & mask1 ) * d2 ) + m ; 181} 182 183reg = ( type4x4 ) reg_f ; 184} 185 186template < typename type4 > 187void dequantize_q4_1_t4 ( device const block_q4_1 * xb , short il , thread type4 & reg ) { 188device const uint16_t * qs = (( device const uint16_t *) xb + 2 ); 189const float d1 = ( il / 4 ) ? ( xb -> d / 16. h ) : xb -> d ; 190const float d2 = d1 / 256.f ; 191const float m = xb -> m ; 192const ushort mask0 = ( il / 4 ) ? 0x00F0 : 0x000F ; 193const ushort mask1 = mask0 << 8 ; 194 195for ( int i = 0 ; i < 2 ; i ++) { 196reg [ 2 * i + 0 ] = d1 * ( qs [ 2 *( il % 4 ) + i ] & mask0 ) + m ; 197reg [ 2 * i + 1 ] = d2 * ( qs [ 2 *( il % 4 ) + i ] & mask1 ) + m ; 198} 199} 200 201template < typename type4x4 > 202void dequantize_q5_0 ( device const block_q5_0 * xb , short il , thread type4x4 & reg ) { 203device const uint16_t * qs = (( device const uint16_t *) xb + 3 ); 204const float d = xb -> d ; 205const float md = -16. h * xb -> d ; 206const ushort mask = il ? 0x00F0 : 0x000F ; 207 208const uint32_t qh = *(( device const uint32_t *) xb -> qh ); 209 210const int x_mv = il ? 4 : 0 ; 211 212const int gh_mv = il ? 12 : 0 ; 213const int gh_bk = il ? 0 : 4 ; 214 215float4x4 reg_f ; 216 217for ( int i = 0 ; i < 8 ; i ++) { 218// extract the 5-th bits for x0 and x1 219const uint8_t xh_0 = (( qh >> ( gh_mv + 2 * i )) << gh_bk ) & 0x10 ; 220const uint8_t xh_1 = (( qh >> ( gh_mv + 2 * i + 1 )) << gh_bk ) & 0x10 ; 221 222// combine the 4-bits from qs with the 5th bit 223const int32_t x0 = (((( qs [ i ] ) & mask ) >> x_mv ) | xh_0 ); 224const int32_t x1 = (((( qs [ i ] >> 8 ) & mask ) >> x_mv ) | xh_1 ); 225 226reg_f [ i / 2 ][ 2 *( i % 2 ) + 0 ] = d * x0 + md ; 227reg_f [ i / 2 ][ 2 *( i % 2 ) + 1 ] = d * x1 + md ; 228} 229 230reg = ( type4x4 ) reg_f ; 231} 232 233template < typename type4 > 234void dequantize_q5_0_t4 ( device const block_q5_0 * xb , short il , thread type4 & reg ) { 235device const uint16_t * qs = (( device const uint16_t *) xb + 3 ); 236const float d = xb -> d ; 237const float md = -16. h * xb -> d ; 238const ushort mask = ( il / 4 ) ? 0x00F0 : 0x000F ; 239 240const uint32_t qh = *(( device const uint32_t *) xb -> qh ); 241 242const int x_mv = ( il / 4 ) ? 4 : 0 ; 243 244const int gh_mv = ( il / 4 ) ? 12 : 0 ; 245const int gh_bk = ( il / 4 ) ? 0 : 4 ; 246 247for ( int ii = 0 ; ii < 2 ; ii ++) { 248int i = 2 *( il % 4 ) + ii ; 249 250// extract the 5-th bits for x0 and x1 251const uint8_t xh_0 = (( qh >> ( gh_mv + 2 * i )) << gh_bk ) & 0x10 ; 252const uint8_t xh_1 = (( qh >> ( gh_mv + 2 * i + 1 )) << gh_bk ) & 0x10 ; 253 254// combine the 4-bits from qs with the 5th bit 255const int32_t x0 = (((( qs [ i ] ) & mask ) >> x_mv ) | xh_0 ); 256const int32_t x1 = (((( qs [ i ] >> 8 ) & mask ) >> x_mv ) | xh_1 ); 257 258reg [ 2 * ii + 0 ] = d * x0 + md ; 259reg [ 2 * ii + 1 ] = d * x1 + md ; 260} 261} 262 263template < typename type4x4 > 264void dequantize_q5_1 ( device const block_q5_1 * xb , short il , thread type4x4 & reg ) { 265device const uint16_t * qs = (( device const uint16_t *) xb + 4 ); 266const float d = xb -> d ; 267const float m = xb -> m ; 268const ushort mask = il ? 0x00F0 : 0x000F ; 269 270const uint32_t qh = *(( device const uint32_t *) xb -> qh ); 271 272const int x_mv = il ? 4 : 0 ; 273 274const int gh_mv = il ? 12 : 0 ; 275const int gh_bk = il ? 0 : 4 ; 276 277float4x4 reg_f ; 278 279for ( int i = 0 ; i < 8 ; i ++) { 280// extract the 5-th bits for x0 and x1 281const uint8_t xh_0 = (( qh >> ( gh_mv + 2 * i )) << gh_bk ) & 0x10 ; 282const uint8_t xh_1 = (( qh >> ( gh_mv + 2 * i + 1 )) << gh_bk ) & 0x10 ; 283 284// combine the 4-bits from qs with the 5th bit 285const int32_t x0 = (((( qs [ i ] ) & mask ) >> x_mv ) | xh_0 ); 286const int32_t x1 = (((( qs [ i ] >> 8 ) & mask ) >> x_mv ) | xh_1 ); 287 288reg_f [ i / 2 ][ 2 *( i % 2 ) + 0 ] = d * x0 + m ; 289reg_f [ i / 2 ][ 2 *( i % 2 ) + 1 ] = d * x1 + m ; 290} 291 292reg = ( type4x4 ) reg_f ; 293} 294 295template < typename type4 > 296void dequantize_q5_1_t4 ( device const block_q5_1 * xb , short il , thread type4 & reg ) { 297device const uint16_t * qs = (( device const uint16_t *) xb + 4 ); 298const float d = xb -> d ; 299const float m = xb -> m ; 300const ushort mask = ( il / 4 ) ? 0x00F0 : 0x000F ; 301 302const uint32_t qh = *(( device const uint32_t *) xb -> qh ); 303 304const int x_mv = ( il / 4 ) ? 4 : 0 ; 305 306const int gh_mv = ( il / 4 ) ? 12 : 0 ; 307const int gh_bk = ( il / 4 ) ? 0 : 4 ; 308 309for ( int ii = 0 ; ii < 2 ; ii ++) { 310int i = 2 *( il % 4 ) + ii ; 311 312// extract the 5-th bits for x0 and x1 313const uint8_t xh_0 = (( qh >> ( gh_mv + 2 * i )) << gh_bk ) & 0x10 ; 314const uint8_t xh_1 = (( qh >> ( gh_mv + 2 * i + 1 )) << gh_bk ) & 0x10 ; 315 316// combine the 4-bits from qs with the 5th bit 317const int32_t x0 = (((( qs [ i ] ) & mask ) >> x_mv ) | xh_0 ); 318const int32_t x1 = (((( qs [ i ] >> 8 ) & mask ) >> x_mv ) | xh_1 ); 319 320reg [ 2 * ii + 0 ] = d * x0 + m ; 321reg [ 2 * ii + 1 ] = d * x1 + m ; 322} 323} 324 325template < typename type4x4 > 326void dequantize_q8_0 ( device const block_q8_0 * xb , short il , thread type4x4 & reg ) { 327device const packed_char4 * qs = ( device const packed_char4 *) xb -> qs ; 328const float d = xb -> d ; 329 330float4x4 reg_f ; 331 332for ( int i = 0 ; i < 4 ; ++ i ) { 333reg_f [ i ] = float4 ( qs [ 4 * il + i ]) * d ; 334} 335 336reg = ( type4x4 ) reg_f ; 337} 338 339template < typename type4 > 340void dequantize_q8_0_t4 ( device const block_q8_0 * xb , short il , thread type4 & reg ) { 341device const packed_char4 * qs = ( device const packed_char4 *) xb -> qs ; 342const float d = xb -> d ; 343 344reg = ( type4 ) ( float4 ( qs [ il ]) * d ); 345} 346 347template < typename type4x4 > 348void dequantize_mxfp4 ( device const block_mxfp4 * xb , short il , thread type4x4 & reg ) { 349device const uint8_t * q2 = ( device const uint8_t *) xb -> qs ; 350 351const float d = e8m0_to_fp32 ( xb -> e ); 352const uint8_t shr = il >= 1 ? 4 : 0 ; 353 354for ( int i = 0 ; i < 4 ; ++ i ) { 355reg [ i ][ 0 ] = d * kvalues_mxfp4_f [( q2 [ 4 * i + 0 ] >> shr ) & 0x0F ]; 356reg [ i ][ 1 ] = d * kvalues_mxfp4_f [( q2 [ 4 * i + 1 ] >> shr ) & 0x0F ]; 357reg [ i ][ 2 ] = d * kvalues_mxfp4_f [( q2 [ 4 * i + 2 ] >> shr ) & 0x0F ]; 358reg [ i ][ 3 ] = d * kvalues_mxfp4_f [( q2 [ 4 * i + 3 ] >> shr ) & 0x0F ]; 359} 360} 361 362template < typename type4 > 363void dequantize_mxfp4_t4 ( device const block_mxfp4 * xb , short il , thread type4 & reg ) { 364device const uint8_t * q2 = ( device const uint8_t *) xb -> qs ; 365 366const float d = e8m0_to_fp32 ( xb -> e ); 367const short il4 = il % 4 ; 368 369const uint8_t shr = il >= 4 ? 4 : 0 ; 370 371reg [ 0 ] = d * kvalues_mxfp4_f [( q2 [ 4 * il4 + 0 ] >> shr ) & 0x0F ]; 372reg [ 1 ] = d * kvalues_mxfp4_f [( q2 [ 4 * il4 + 1 ] >> shr ) & 0x0F ]; 373reg [ 2 ] = d * kvalues_mxfp4_f [( q2 [ 4 * il4 + 2 ] >> shr ) & 0x0F ]; 374reg [ 3 ] = d * kvalues_mxfp4_f [( q2 [ 4 * il4 + 3 ] >> shr ) & 0x0F ]; 375} 376 377template < typename type4x4 > 378void dequantize_q2_K ( device const block_q2_K * xb , short il , thread type4x4 & reg ) { 379const float d = xb -> d ; 380const float min = xb -> dmin ; 381device const uint8_t * q = ( device const uint8_t *) xb -> qs ; 382float dl , ml ; 383uint8_t sc = xb -> scales [ il ]; 384 385q = q + 32 *( il / 8 ) + 16 *( il & 1 ); 386il = ( il / 2 )% 4 ; 387 388half coef = il > 1 ? ( il > 2 ? 1 / 64. h : 1 / 16. h ) : ( il > 0 ? 1 / 4. h : 1. h ); 389uchar mask = il > 1 ? ( il > 2 ? 192 : 48 ) : ( il > 0 ? 12 : 3 ); 390dl = d * ( sc & 0xF ) * coef , ml = min * ( sc >> 4 ); 391for ( int i = 0 ; i < 16 ; ++ i ) { 392reg [ i / 4 ][ i % 4 ] = dl * ( q [ i ] & mask ) - ml ; 393} 394} 395 396template < typename type4x4 > 397void dequantize_q3_K ( device const block_q3_K * xb , short il , thread type4x4 & reg ) { 398const half d_all = xb -> d ; 399device const uint8_t * q = ( device const uint8_t *) xb -> qs ; 400device const uint8_t * h = ( device const uint8_t *) xb -> hmask ; 401device const int8_t * scales = ( device const int8_t *) xb -> scales ; 402 403q = q + 32 * ( il / 8 ) + 16 * ( il & 1 ); 404h = h + 16 * ( il & 1 ); 405uint8_t m = 1 << ( il / 2 ); 406uint16_t kmask1 = ( il / 4 )> 1 ? (( il / 4 )> 2 ? 192 : 48 ) : \ 407(( il / 4 )> 0 ? 12 : 3 ); 408uint16_t kmask2 = il / 8 ? 0xF0 : 0x0F ; 409uint16_t scale_2 = scales [ il % 8 ], scale_1 = scales [ 8 + il % 4 ]; 410int16_t dl_int = ( il / 4 )& 1 ? ( scale_2 & kmask2 ) | (( scale_1 & kmask1 ) << 2 ) 411: ( scale_2 & kmask2 ) | (( scale_1 & kmask1 ) << 4 ); 412float dl = il < 8 ? d_all * ( dl_int - 32.f ) : d_all * ( dl_int / 16.f - 32.f ); 413const float ml = 4.f * dl ; 414 415il = ( il / 2 ) & 3 ; 416const half coef = il > 1 ? ( il > 2 ? 1 / 64. h : 1 / 16. h ) : ( il > 0 ? 1 / 4. h : 1. h ); 417const uint8_t mask = il > 1 ? ( il > 2 ? 192 : 48 ) : ( il > 0 ? 12 : 3 ); 418dl *= coef ; 419 420for ( int i = 0 ; i < 16 ; ++ i ) { 421reg [ i / 4 ][ i % 4 ] = dl * ( q [ i ] & mask ) - ( h [ i ] & m ? 0 : ml ); 422} 423} 424 425static inline uchar2 get_scale_min_k4_just2 ( int j , int k , device const uchar * q ) { 426return j < 4 ? uchar2 { uchar ( q [ j + 0 + k ] & 63 ), uchar ( q [ j + 4 + k ] & 63 )} 427: uchar2 { uchar (( q [ j + 4 + k ] & 0xF ) | (( q [ j - 4 + k ] & 0xc0 ) >> 2 )), uchar (( q [ j + 4 + k ] >> 4 ) | (( q [ j - 0 + k ] & 0xc0 ) >> 2 ))}; 428} 429 430template < typename type4x4 > 431void dequantize_q4_K ( device const block_q4_K * xb , short il , thread type4x4 & reg ) { 432device const uchar * q = xb -> qs ; 433 434short is = ( il / 4 ) * 2 ; 435q = q + ( il / 4 ) * 32 + 16 * ( il & 1 ); 436il = il & 3 ; 437const uchar2 sc = get_scale_min_k4_just2 ( is , il / 2 , xb -> scales ); 438const float d = il < 2 ? xb -> d : xb -> d / 16. h ; 439const float min = xb -> dmin ; 440const float dl = d * sc [ 0 ]; 441const float ml = min * sc [ 1 ]; 442 443const ushort mask = il < 2 ? 0x0F : 0xF0 ; 444for ( int i = 0 ; i < 16 ; ++ i ) { 445reg [ i / 4 ][ i % 4 ] = dl * ( q [ i ] & mask ) - ml ; 446} 447} 448 449template < typename type4x4 > 450void dequantize_q5_K ( device const block_q5_K * xb , short il , thread type4x4 & reg ) { 451device const uint8_t * q = xb -> qs ; 452device const uint8_t * qh = xb -> qh ; 453 454short is = ( il / 4 ) * 2 ; 455q = q + 32 * ( il / 4 ) + 16 * ( il & 1 ); 456qh = qh + 16 * ( il & 1 ); 457uint8_t ul = 1 << ( il / 2 ); 458il = il & 3 ; 459const uchar2 sc = get_scale_min_k4_just2 ( is , il / 2 , xb -> scales ); 460const float d = il < 2 ? xb -> d : xb -> d / 16.f ; 461const float min = xb -> dmin ; 462const float dl = d * sc [ 0 ]; 463const float ml = min * sc [ 1 ]; 464 465const ushort mask = il < 2 ? 0x0F : 0xF0 ; 466const float qh_val = il < 2 ? 16.f : 256.f ; 467for ( int i = 0 ; i < 16 ; ++ i ) { 468reg [ i / 4 ][ i % 4 ] = dl * (( q [ i ] & mask ) + ( qh [ i ] & ul ? qh_val : 0 )) - ml ; 469} 470} 471 472template < typename type4x4 > 473void dequantize_q6_K ( device const block_q6_K * xb , short il , thread type4x4 & reg ) { 474const half d_all = xb -> d ; 475device const uint16_t * ql = ( device const uint16_t *) xb -> ql ; 476device const uint16_t * qh = ( device const uint16_t *) xb -> qh ; 477device const int8_t * scales = ( device const int8_t *) xb -> scales ; 478 479ql = ql + 32 *( il / 8 ) + 16 *(( il / 2 )& 1 ) + 8 *( il & 1 ); 480qh = qh + 16 *( il / 8 ) + 8 *( il & 1 ); 481float sc = scales [( il % 2 ) + 2 * (( il / 2 ))]; 482il = ( il / 2 ) & 3 ; 483 484const uint32_t kmask1 = il > 1 ? ( il > 2 ? 0xC0C0C0C0 : 0x30303030 ) : ( il > 0 ? 0x0C0C0C0C : 0x03030303 ); 485const uint32_t kmask2 = il > 1 ? 0xF0F0F0F0 : 0x0F0F0F0F ; 486const float ml = d_all * sc * 32.f ; 487const float dl0 = d_all * sc ; 488const float dl1 = dl0 / 256.f ; 489const float dl2 = dl0 / ( 256.f * 256.f ); 490const float dl3 = dl0 / ( 256.f * 256.f * 256.f ); 491const uint8_t shr_h = il > 2 ? 2 : 0 ; 492const uint8_t shl_h = il > 1 ? 0 : ( il > 0 ? 2 : 4 ); 493const uint8_t shr_l = il > 1 ? 4 : 0 ; 494for ( int i = 0 ; i < 4 ; ++ i ) { 495const uint32_t low = ( ql [ 2 * i ] | ( uint32_t )( ql [ 2 * i + 1 ] << 16 )) & kmask2 ; 496const uint32_t high = ( qh [ 2 * i ] | ( uint32_t )( qh [ 2 * i + 1 ] << 16 )) & kmask1 ; 497const uint32_t q = (( high << shl_h ) >> shr_h ) | ( low >> shr_l ); 498reg [ i ][ 0 ] = dl0 * (( half )( q & 0xFF )) - ml ; 499reg [ i ][ 1 ] = dl1 * (( float )( q & 0xFF00 )) - ml ; 500reg [ i ][ 2 ] = dl2 * (( float )( q & 0xFF0000 )) - ml ; 501reg [ i ][ 3 ] = dl3 * (( float )( q & 0xFF000000 )) - ml ; 502} 503} 504 505template < typename type4x4 > 506void dequantize_iq2_xxs ( device const block_iq2_xxs * xb , short il , thread type4x4 & reg ) { 507// il is 0...15 for QK_K = 256 => index of block of 32 is il/2 508const float d = xb -> d ; 509const int ib32 = il / 2 ; 510il = il % 2 ; 511// il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16 512// each block of 32 needs 2 uint32_t's for the quants & scale, so 4 uint16_t's. 513device const uint16_t * q2 = xb -> qs + 4 * ib32 ; 514const uint32_t aux32_g = q2 [ 0 ] | ( q2 [ 1 ] << 16 ); 515const uint32_t aux32_s = q2 [ 2 ] | ( q2 [ 3 ] << 16 ); 516thread const uint8_t * aux8 = ( thread const uint8_t *)& aux32_g ; 517const float dl = d * ( 0.5f + ( aux32_s >> 28 )) * 0.25f ; 518constant uint8_t * grid = ( constant uint8_t *)( iq2xxs_grid + aux8 [ 2 * il + 0 ]); 519uint8_t signs = ksigns_iq2xs [( aux32_s >> 14 * il ) & 127 ]; 520for ( int i = 0 ; i < 8 ; ++ i ) { 521reg [ i / 4 ][ i % 4 ] = dl * grid [ i ] * ( signs & kmask_iq2xs [ i ] ? -1.f : 1.f ); 522} 523grid = ( constant uint8_t *)( iq2xxs_grid + aux8 [ 2 * il + 1 ]); 524signs = ksigns_iq2xs [( aux32_s >> ( 14 * il + 7 )) & 127 ]; 525for ( int i = 0 ; i < 8 ; ++ i ) { 526reg [ 2 + i / 4 ][ i % 4 ] = dl * grid [ i ] * ( signs & kmask_iq2xs [ i ] ? -1.f : 1.f ); 527} 528} 529 530template < typename type4x4 > 531void dequantize_iq2_xs ( device const block_iq2_xs * xb , short il , thread type4x4 & reg ) { 532// il is 0...15 for QK_K = 256 => index of block of 32 is il/2 533const float d = xb -> d ; 534const int ib32 = il / 2 ; 535il = il % 2 ; 536// il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16 537device const uint16_t * q2 = xb -> qs + 4 * ib32 ; 538const float dl = d * ( 0.5f + (( xb -> scales [ ib32 ] >> 4 * il ) & 0xf )) * 0.25f ; 539constant uint8_t * grid = ( constant uint8_t *)( iq2xs_grid + ( q2 [ 2 * il + 0 ] & 511 )); 540uint8_t signs = ksigns_iq2xs [ q2 [ 2 * il + 0 ] >> 9 ]; 541for ( int i = 0 ; i < 8 ; ++ i ) { 542reg [ i / 4 ][ i % 4 ] = dl * grid [ i ] * ( signs & kmask_iq2xs [ i ] ? -1.f : 1.f ); 543} 544grid = ( constant uint8_t *)( iq2xs_grid + ( q2 [ 2 * il + 1 ] & 511 )); 545signs = ksigns_iq2xs [ q2 [ 2 * il + 1 ] >> 9 ]; 546for ( int i = 0 ; i < 8 ; ++ i ) { 547reg [ 2 + i / 4 ][ i % 4 ] = dl * grid [ i ] * ( signs & kmask_iq2xs [ i ] ? -1.f : 1.f ); 548} 549} 550 551template < typename type4x4 > 552void dequantize_iq3_xxs ( device const block_iq3_xxs * xb , short il , thread type4x4 & reg ) { 553// il is 0...15 for QK_K = 256 => index of block of 32 is il/2 554const float d = xb -> d ; 555const int ib32 = il / 2 ; 556il = il % 2 ; 557// il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16 558device const uint8_t * q3 = xb -> qs + 8 * ib32 ; 559device const uint16_t * gas = ( device const uint16_t *)( xb -> qs + QK_K / 4 ) + 2 * ib32 ; 560const uint32_t aux32 = gas [ 0 ] | ( gas [ 1 ] << 16 ); 561const float dl = d * ( 0.5f + ( aux32 >> 28 )) * 0.5f ; 562constant uint8_t * grid1 = ( constant uint8_t *)( iq3xxs_grid + q3 [ 4 * il + 0 ]); 563constant uint8_t * grid2 = ( constant uint8_t *)( iq3xxs_grid + q3 [ 4 * il + 1 ]); 564uint8_t signs = ksigns_iq2xs [( aux32 >> 14 * il ) & 127 ]; 565for ( int i = 0 ; i < 4 ; ++ i ) { 566reg [ 0 ][ i ] = dl * grid1 [ i ] * ( signs & kmask_iq2xs [ i + 0 ] ? -1.f : 1.f ); 567reg [ 1 ][ i ] = dl * grid2 [ i ] * ( signs & kmask_iq2xs [ i + 4 ] ? -1.f : 1.f ); 568} 569grid1 = ( constant uint8_t *)( iq3xxs_grid + q3 [ 4 * il + 2 ]); 570grid2 = ( constant uint8_t *)( iq3xxs_grid + q3 [ 4 * il + 3 ]); 571signs = ksigns_iq2xs [( aux32 >> ( 14 * il + 7 )) & 127 ]; 572for ( int i = 0 ; i < 4 ; ++ i ) { 573reg [ 2 ][ i ] = dl * grid1 [ i ] * ( signs & kmask_iq2xs [ i + 0 ] ? -1.f : 1.f ); 574reg [ 3 ][ i ] = dl * grid2 [ i ] * ( signs & kmask_iq2xs [ i + 4 ] ? -1.f : 1.f ); 575} 576} 577 578template < typename type4x4 > 579void dequantize_iq3_s ( device const block_iq3_s * xb , short il , thread type4x4 & reg ) { 580// il is 0...15 for QK_K = 256 => index of block of 32 is il/2 581const float d = xb -> d ; 582const int ib32 = il / 2 ; 583il = il % 2 ; 584// il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16 585device const uint8_t * qs = xb -> qs + 8 * ib32 ; 586device const uint8_t * signs = xb -> signs + 4 * ib32 + 2 * il ; 587const uint8_t qh = xb -> qh [ ib32 ] >> 4 * il ; 588const float dl = d * ( 1 + 2 *(( xb -> scales [ ib32 / 2 ] >> 4 *( ib32 % 2 )) & 0xf )); 589constant uint8_t * grid1 = ( constant uint8_t *)( iq3s_grid + ( qs [ 4 * il + 0 ] | (( qh << 8 ) & 256 ))); 590constant uint8_t * grid2 = ( constant uint8_t *)( iq3s_grid + ( qs [ 4 * il + 1 ] | (( qh << 7 ) & 256 ))); 591for ( int i = 0 ; i < 4 ; ++ i ) { 592reg [ 0 ][ i ] = dl * grid1 [ i ] * select ( 1 , -1 , signs [ 0 ] & kmask_iq2xs [ i + 0 ]); 593reg [ 1 ][ i ] = dl * grid2 [ i ] * select ( 1 , -1 , signs [ 0 ] & kmask_iq2xs [ i + 4 ]); 594} 595grid1 = ( constant uint8_t *)( iq3s_grid + ( qs [ 4 * il + 2 ] | (( qh << 6 ) & 256 ))); 596grid2 = ( constant uint8_t *)( iq3s_grid + ( qs [ 4 * il + 3 ] | (( qh << 5 ) & 256 ))); 597for ( int i = 0 ; i < 4 ; ++ i ) { 598reg [ 2 ][ i ] = dl * grid1 [ i ] * select ( 1 , -1 , signs [ 1 ] & kmask_iq2xs [ i + 0 ]); 599reg [ 3 ][ i ] = dl * grid2 [ i ] * select ( 1 , -1 , signs [ 1 ] & kmask_iq2xs [ i + 4 ]); 600} 601} 602 603template < typename type4x4 > 604void dequantize_iq2_s ( device const block_iq2_s * xb , short il , thread type4x4 & reg ) { 605// il is 0...15 for QK_K = 256 => index of block of 32 is il/2 606const float d = xb -> d ; 607const int ib32 = il / 2 ; 608il = il % 2 ; 609// il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16 610device const uint8_t * qs = xb -> qs + 4 * ib32 + 2 * il ; 611device const uint8_t * signs = qs + QK_K / 8 ; 612const uint8_t qh = xb -> qh [ ib32 ] >> 4 * il ; 613const float dl = d * ( 0.5f + (( xb -> scales [ ib32 ] >> 4 * il ) & 0xf )) * 0.25f ; 614constant uint8_t * grid1 = ( constant uint8_t *)( iq2s_grid + ( qs [ 0 ] | (( qh << 8 ) & 0x300 ))); 615constant uint8_t * grid2 = ( constant uint8_t *)( iq2s_grid + ( qs [ 1 ] | (( qh << 6 ) & 0x300 ))); 616for ( int i = 0 ; i < 8 ; ++ i ) { 617reg [ i / 4 + 0 ][ i % 4 ] = dl * grid1 [ i ] * select ( 1 , -1 , signs [ 0 ] & kmask_iq2xs [ i ]); 618reg [ i / 4 + 2 ][ i % 4 ] = dl * grid2 [ i ] * select ( 1 , -1 , signs [ 1 ] & kmask_iq2xs [ i ]); 619} 620} 621 622template < typename type4x4 > 623void dequantize_iq1_s ( device const block_iq1_s * xb , short il , thread type4x4 & reg ) { 624// il is 0...15 for QK_K = 256 => index of block of 32 is il/2 625const int ib32 = il / 2 ; 626il = il % 2 ; 627const float d = xb -> d ; 628device const uint8_t * qs = xb -> qs + 4 * ib32 + 2 * il ; 629device const uint16_t * qh = xb -> qh ; 630const float dl = d * ( 2 *(( qh [ ib32 ] >> 12 ) & 7 ) + 1 ); 631const float ml = dl * ( qh [ ib32 ] & 0x8000 ? -1 - IQ1S_DELTA : -1 + IQ1S_DELTA ); 632const uint16_t h = qh [ ib32 ] >> 6 * il ; 633constant uint8_t * grid1 = ( constant uint8_t *)( iq1s_grid_gpu + ( qs [ 0 ] | (( h << 8 ) & 0x700 ))); 634constant uint8_t * grid2 = ( constant uint8_t *)( iq1s_grid_gpu + ( qs [ 1 ] | (( h << 5 ) & 0x700 ))); 635for ( int i = 0 ; i < 4 ; ++ i ) { 636reg [ 0 ][ i ] = dl * ( grid1 [ i ] & 0xf ) + ml ; 637reg [ 1 ][ i ] = dl * ( grid1 [ i ] >> 4 ) + ml ; 638reg [ 2 ][ i ] = dl * ( grid2 [ i ] & 0xf ) + ml ; 639reg [ 3 ][ i ] = dl * ( grid2 [ i ] >> 4 ) + ml ; 640} 641} 642 643template < typename type4x4 > 644void dequantize_iq1_m ( device const block_iq1_m * xb , short il , thread type4x4 & reg ) { 645// il is 0...15 for QK_K = 256 => index of block of 32 is il/2 646const int ib32 = il / 2 ; 647il = il % 2 ; 648device const uint16_t * sc = ( device const uint16_t *) xb -> scales ; 649 650iq1m_scale_t scale ; 651scale . u16 = ( sc [ 0 ] >> 12 ) | (( sc [ 1 ] >> 8 ) & 0x00f0 ) | (( sc [ 2 ] >> 4 ) & 0x0f00 ) | ( sc [ 3 ] & 0xf000 ); 652const float d = scale . f16 ; 653 654device const uint8_t * qs = xb -> qs + 4 * ib32 + 2 * il ; 655device const uint8_t * qh = xb -> qh + 2 * ib32 + il ; 656 657const float dl = d * ( 2 *(( sc [ ib32 / 2 ] >> ( 6 *( ib32 % 2 )+ 3 * il )) & 7 ) + 1 ); 658const float ml1 = dl * ( qh [ 0 ] & 0x08 ? -1 - IQ1M_DELTA : -1 + IQ1M_DELTA ); 659const float ml2 = dl * ( qh [ 0 ] & 0x80 ? -1 - IQ1M_DELTA : -1 + IQ1M_DELTA ); 660constant uint8_t * grid1 = ( constant uint8_t *)( iq1s_grid_gpu + ( qs [ 0 ] | (( qh [ 0 ] << 8 ) & 0x700 ))); 661constant uint8_t * grid2 = ( constant uint8_t *)( iq1s_grid_gpu + ( qs [ 1 ] | (( qh [ 0 ] << 4 ) & 0x700 ))); 662for ( int i = 0 ; i < 4 ; ++ i ) { 663reg [ 0 ][ i ] = dl * ( grid1 [ i ] & 0xf ) + ml1 ; 664reg [ 1 ][ i ] = dl * ( grid1 [ i ] >> 4 ) + ml1 ; 665reg [ 2 ][ i ] = dl * ( grid2 [ i ] & 0xf ) + ml2 ; 666reg [ 3 ][ i ] = dl * ( grid2 [ i ] >> 4 ) + ml2 ; 667} 668} 669 670template < typename type4x4 > 671void dequantize_iq4_nl ( device const block_iq4_nl * xb , short il , thread type4x4 & reg ) { 672device const uint16_t * q4 = ( device const uint16_t *) xb -> qs ; 673const float d = xb -> d ; 674uint32_t aux32 ; 675thread const uint8_t * q8 = ( thread const uint8_t *)& aux32 ; 676for ( int i = 0 ; i < 4 ; ++ i ) { 677aux32 = (( q4 [ 2 * i ] | ( q4 [ 2 * i + 1 ] << 16 )) >> 4 * il ) & 0x0f0f0f0f ; 678reg [ i ][ 0 ] = d * kvalues_iq4nl_f [ q8 [ 0 ]]; 679reg [ i ][ 1 ] = d * kvalues_iq4nl_f [ q8 [ 1 ]]; 680reg [ i ][ 2 ] = d * kvalues_iq4nl_f [ q8 [ 2 ]]; 681reg [ i ][ 3 ] = d * kvalues_iq4nl_f [ q8 [ 3 ]]; 682} 683} 684 685template < typename type4 > 686void dequantize_iq4_nl_t4 ( device const block_iq4_nl * xb , short il , thread type4 & reg ) { 687device const uint16_t * q4 = ( device const uint16_t *) xb -> qs ; 688const float d = xb -> d ; 689uint32_t aux32 ; 690thread const uint8_t * q8 = ( thread const uint8_t *)& aux32 ; 691aux32 = (( q4 [ 2 *( il % 4 )] | ( q4 [ 2 *( il % 4 )+ 1 ] << 16 )) >> 4 *( il / 4 )) & 0x0f0f0f0f ; 692reg [ 0 ] = d * kvalues_iq4nl_f [ q8 [ 0 ]]; 693reg [ 1 ] = d * kvalues_iq4nl_f [ q8 [ 1 ]]; 694reg [ 2 ] = d * kvalues_iq4nl_f [ q8 [ 2 ]]; 695reg [ 3 ] = d * kvalues_iq4nl_f [ q8 [ 3 ]]; 696} 697 698template < typename type4x4 > 699void dequantize_iq4_xs ( device const block_iq4_xs * xb , short il , thread type4x4 & reg ) { 700// il is 0...15 for QK_K = 256 => index of block of 32 is il/2 701const int ib32 = il / 2 ; 702il = il % 2 ; 703// il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16 704device const uint32_t * q4 = ( device const uint32_t *) xb -> qs + 4 * ib32 ; 705const int ls = (( xb -> scales_l [ ib32 / 2 ] >> 4 *( ib32 % 2 )) & 0xf ) | ((( xb -> scales_h >> 2 * ib32 ) & 3 ) << 4 ); 706const float d = ( float ) xb -> d * ( ls - 32 ); 707uint32_t aux32 ; 708thread const uint8_t * q8 = ( thread const uint8_t *)& aux32 ; 709for ( int i = 0 ; i < 4 ; ++ i ) { 710aux32 = ( q4 [ i ] >> 4 * il ) & 0x0f0f0f0f ; 711reg [ i ][ 0 ] = d * kvalues_iq4nl_f [ q8 [ 0 ]]; 712reg [ i ][ 1 ] = d * kvalues_iq4nl_f [ q8 [ 1 ]]; 713reg [ i ][ 2 ] = d * kvalues_iq4nl_f [ q8 [ 2 ]]; 714reg [ i ][ 3 ] = d * kvalues_iq4nl_f [ q8 [ 3 ]]; 715} 716} 717 718template < typename type4x4 > 719void dequantize_tq2_0 ( device const block_tq2_0 * xb , short il , thread type4x4 & reg ) { 720device const uint8_t * qs = xb -> qs ; 721const float d = xb -> d ; 722 723float4x4 reg_f ; 724 725// 2 bits per element, 4 elements per byte, 128 elements per 32-byte group 726const short base = il * 16 ; 727for ( int k = 0 ; k < 16 ; k ++) { 728const int i = base + k ; 729const int byte = (( i >> 7 ) & 1 ) * 32 + ( i & 31 ); 730const int l = ( i >> 5 ) & 3 ; 731reg_f [ k / 4 ][ k % 4 ] = d * ( float )((( qs [ byte ] >> ( 2 * l )) & 3 ) - 1 ); 732} 733 734reg = ( type4x4 ) reg_f ; 735}