char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
867229003
char/aliceai-t5-moe
1// 2// MIT license 3// Copyright (C) 2024 Intel Corporation 4// SPDX-License-Identifier: MIT 5// 6 7// 8// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 9// See https://llvm.org/LICENSE.txt for license information. 10// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 11// 12 13#ifndef GGML_SYCL_DEQUANTIZE_HPP 14#define GGML_SYCL_DEQUANTIZE_HPP 15 16#include "common.hpp" 17#include "convert.hpp" 18 19typedef void (* dequantize_kernel_t )(const void * vx ,const int64_t ib ,const int iqs ,dfloat2 & v ); 20typedef void (* dequantize_kernel_t_reorder )(const void * d ,const int64_t ib ,const void * qs , 21const int iqs ,dfloat2 & v ); 22typedef void (* dequantize_kernel_f32_t )(const void * vx ,const int64_t ib ,const int iqs ,float & v0 ,float & v1 ); 23 24#if QK_K == 256 25static inline void get_scale_min_k4 (int j ,const uint8_t * q ,uint8_t & d ,uint8_t & m ); 26#endif 27 28static __dpct_inline__ void dequantize_q2_0 (const void * vx ,const int64_t ib , 29const int iqs ,dfloat2 & v ) { 30const block_q2_0 * x = (const block_q2_0 * )vx ; 31 32const dfloat d = x [ib ].d ; 33 34const int byte_idx = iqs /4 ; 35const int shift = (iqs %4 )* 2 ; 36const uint8_t vui = x [ib ].qs [byte_idx ]; 37 38v .x ()= (vui >>shift )& 3 ; 39v .y ()= (vui >> (shift + 2 ))& 3 ; 40 41#ifdef GGML_SYCL_F16 42v .s0 ()= ((dfloat )v .s0 ()- 1.0f )* d ; 43v .s1 ()= ((dfloat )v .s1 ()- 1.0f )* d ; 44#else 45v .x ()= ((dfloat )v .x ()- 1.0f )* d ; 46v .y ()= ((dfloat )v .y ()- 1.0f )* d ; 47#endif // GGML_SYCL_F16 48} 49 50static __dpct_inline__ void dequantize_q4_0 (const void * vx ,const int64_t ib , 51const int iqs ,dfloat2 & v ) { 52const block_q4_0 * x = (const block_q4_0 * )vx ; 53 54const dfloat d = x [ib ].d ; 55 56const int vui = x [ib ].qs [iqs ]; 57 58v .x ()= vui & 0xF ; 59v .y ()= vui >>4 ; 60 61#ifdef GGML_SYCL_F16 62// v = v - {8.0f, 8.0f}; 63// v = v * {d, d}; 64v .s0 ()= (v .s0 ()- 8.0f )* d ; 65v .s1 ()= (v .s1 ()- 8.0f )* d ; 66 67#else 68v .x ()= (v .x ()- 8.0f )* d ; 69v .y ()= (v .y ()- 8.0f )* d ; 70#endif // GGML_SYCL_F16 71} 72 73static __dpct_inline__ void dequantize_q4_0_reorder (const void * d_ptr ,const int64_t ib ,const void * qs , 74const int iqs ,dfloat2 & v ) { 75// const block_q4_0 * x = (const block_q4_0 *) vx; 76 77const dfloat d = (const dfloat )* ((const sycl::half * )d_ptr + ib ); 78 79const int vui = * ((const uint8_t * )qs + iqs ); 80 81v .x ()= vui & 0xF ; 82v .y ()= vui >>4 ; 83 84#ifdef GGML_SYCL_F16 85// v = v - {8.0f, 8.0f}; 86// v = v * {d, d}; 87v .s0 ()= (v .s0 ()- 8.0f )* d ; 88v .s1 ()= (v .s1 ()- 8.0f )* d ; 89 90#else 91v .x ()= (v .x ()- 8.0f )* d ; 92v .y ()= (v .y ()- 8.0f )* d ; 93#endif // GGML_SYCL_F16 94} 95 96static __dpct_inline__ void dequantize_q1_0_reorder (const void * d_ptr ,const int64_t ib ,const void * qs , 97const int iqs ,dfloat2 & v ) { 98// Q1_0 reorder layout: scale values followed by quantized bits 99const dfloat d = (const dfloat )* ((const sycl::half * )d_ptr + ib ); 100 101const int bit_index_0 = iqs + 0 ; 102const int bit_index_1 = iqs + 1 ; 103 104const int bit_0 = (* ((const uint8_t * )qs + bit_index_0 /8 ) >> (bit_index_0 %8 ))& 1 ; 105const int bit_1 = (* ((const uint8_t * )qs + bit_index_1 /8 ) >> (bit_index_1 %8 ))& 1 ; 106 107v .x ()= (2 * bit_0 - 1 )* d ; 108v .y ()= (2 * bit_1 - 1 )* d ; 109} 110 111static __dpct_inline__ void dequantize_q1_0 (const void * vx ,const int64_t ib , 112const int iqs ,dfloat2 & v ) { 113const block_q1_0 * x = (const block_q1_0 * )vx ; 114const dfloat d = x [ib ].d ; 115 116const int bit_index_0 = iqs + 0 ; 117const int bit_index_1 = iqs + 1 ; 118 119const int bit_0 = (x [ib ].qs [bit_index_0 /8 ] >> (bit_index_0 %8 ))& 1 ; 120const int bit_1 = (x [ib ].qs [bit_index_1 /8 ] >> (bit_index_1 %8 ))& 1 ; 121 122v .x ()= (2 * bit_0 - 1 )* d ; 123v .y ()= (2 * bit_1 - 1 )* d ; 124} 125 126static __dpct_inline__ void dequantize_q4_1 (const void * vx ,const int64_t ib , 127const int iqs ,dfloat2 & v ) { 128const block_q4_1 * x = (const block_q4_1 * )vx ; 129 130const dfloat d = x [ib ].dm [0 ]; 131const dfloat m = x [ib ].dm [1 ]; 132 133const int vui = x [ib ].qs [iqs ]; 134 135v .x ()= vui & 0xF ; 136v .y ()= vui >>4 ; 137 138#ifdef GGML_SYCL_F16 139// v = v * {d, d}; 140// v = v + {m, m}; 141v .s0 ()= sycl::fma (v .s0 (),d ,m ); 142v .s1 ()= sycl::fma (v .s1 (),d ,m ); 143 144#else 145v .x ()= sycl::fma (v .x (),d ,m ); 146v .y ()= sycl::fma (v .y (),d ,m ); 147#endif // GGML_SYCL_F16 148} 149 150static __dpct_inline__ void dequantize_q4_K (const void * vx ,const int64_t ib , 151const int iqs ,dfloat2 & v ) { 152#if QK_K == 256 153const block_q4_K * x = (const block_q4_K * )vx ; 154const sycl::half2 dm = x [ib ].dm ; 155const float dall = dm [0 ]; 156const float dmin = dm [1 ]; 157 158auto dequantize_one = [& ](const int idx )-> dfloat { 159const int il = idx /64 ; 160const int in = idx %64 ; 161const int is = 2 * il + (in >=32 ?1 :0 ); 162const int off = in & 31 ; 163const int qsi = 32 * il + off ; 164 165uint8_t sc ; 166uint8_t m ; 167get_scale_min_k4 (is ,x [ib ].scales ,sc ,m ); 168 169const uint8_t q = x [ib ].qs [qsi ]; 170const uint8_t qv = (in >=32 ) ? (q >>4 ) : (q & 0xF ); 171return sycl::fma ((dfloat )qv , (dfloat ) (dall * sc ), (dfloat ) (- dmin * m )); 172 }; 173 174v .x ()= dequantize_one (iqs + 0 ); 175v .y ()= dequantize_one (iqs + 1 ); 176#else 177GGML_ABORT ("Q4_K dequantize not supported for QK_K != 256" ); 178#endif 179} 180 181static __dpct_inline__ void dequantize_q4_K_f32 (const void * vx ,const int64_t ib , 182const int iqs ,float & v0 ,float & v1 ) { 183#if QK_K == 256 184const block_q4_K * x = (const block_q4_K * )vx ; 185const sycl::half2 dm = x [ib ].dm ; 186const float dall = dm [0 ]; 187const float dmin = dm [1 ]; 188 189auto dequantize_one = [& ](const int idx )-> float { 190const int il = idx /64 ; 191const int in = idx %64 ; 192const int is = 2 * il + (in >=32 ?1 :0 ); 193const int qsi = 32 * il + (in & 31 ); 194 195uint8_t sc ; 196uint8_t m ; 197get_scale_min_k4 (is ,x [ib ].scales ,sc ,m ); 198 199const float d = dall * sc ; 200const float mn = dmin * m ; 201const uint8_t q = x [ib ].qs [qsi ]; 202const uint8_t qv = (in >=32 ) ? (q >>4 ) : (q & 0xF ); 203 204return d * qv - mn ; 205 }; 206 207v0 = dequantize_one (iqs + 0 ); 208v1 = dequantize_one (iqs + 1 ); 209#else 210GGML_ABORT ("Q4_K dequantize not supported for QK_K != 256" ); 211#endif 212} 213 214static __dpct_inline__ void dequantize_q2_K (const void * vx ,const int64_t ib , 215const int iqs ,dfloat2 & v ) { 216#if QK_K == 256 217const block_q2_K * x = (const block_q2_K * )vx ; 218const float dall = x [ib ].dm [0 ]; 219const float dmin = x [ib ].dm [1 ]; 220 221auto dequantize_one = [& ](const int idx )-> dfloat { 222const int n = idx /128 ; 223const int r = idx %128 ; 224const int g = r /32 ; 225const int l = r %32 ; 226const int is = 8 * n + l /16 ; 227 228const uint8_t q = x [ib ].qs [32 * n + l ]; 229const uint8_t sc = x [ib ].scales [is + 2 * g ]; 230const float d = dall * (sc & 0xF ); 231const float m = dmin * (sc >>4 ); 232 233return (dfloat )d * (dfloat ) ((q >> (2 * g ))& 3 )- (dfloat )m ; 234 }; 235 236v .x ()= dequantize_one (iqs + 0 ); 237v .y ()= dequantize_one (iqs + 1 ); 238#else 239GGML_ABORT ("Q2_K dequantize not supported for QK_K != 256" ); 240#endif 241} 242 243static __dpct_inline__ void dequantize_q2_K_f32 (const void * vx ,const int64_t ib , 244const int iqs ,float & v0 ,float & v1 ) { 245#if QK_K == 256 246const block_q2_K * x = (const block_q2_K * )vx ; 247const float dall = x [ib ].dm [0 ]; 248const float dmin = x [ib ].dm [1 ]; 249 250auto dequantize_one = [& ](const int idx )-> float { 251const int n = idx /128 ; 252const int r = idx %128 ; 253const int g = r /32 ; 254const int l = r %32 ; 255const int is = 8 * n + l /16 ; 256 257const uint8_t q = x [ib ].qs [32 * n + l ]; 258const uint8_t sc = x [ib ].scales [is + 2 * g ]; 259const float d = dall * (sc & 0xF ); 260const float m = dmin * (sc >>4 ); 261 262return d * ((q >> (2 * g ))& 3 )- m ; 263 }; 264 265v0 = dequantize_one (iqs + 0 ); 266v1 = dequantize_one (iqs + 1 ); 267#else 268GGML_ABORT ("Q2_K dequantize not supported for QK_K != 256" ); 269#endif 270} 271 272static __dpct_inline__ void dequantize_q3_K (const void * vx ,const int64_t ib , 273const int iqs ,dfloat2 & v ) { 274#if QK_K == 256 275const block_q3_K * x = (const block_q3_K * )vx ; 276const float d_all = x [ib ].d ; 277 278auto dequantize_one = [& ](const int idx )-> dfloat { 279const int n = idx /128 ; 280const int r = idx %128 ; 281const int j = r /32 ; 282const int l = r %32 ; 283 284const int is0 = l /16 ; 285const int is = 8 * n + 2 * j + is0 ; 286const int shift = 2 * j ; 287const uint8_t m = 1 << (4 * n + j ); 288 289const int8_t us = is < 4 ? (x [ib ].scales [is - 0 ]& 0xF ) | (((x [ib ].scales [is + 8 ] >>0 )& 3 ) <<4 ) : 290is < 8 ? (x [ib ].scales [is - 0 ]& 0xF ) | (((x [ib ].scales [is + 4 ] >>2 )& 3 ) <<4 ) : 291is < 12 ? (x [ib ].scales [is - 8 ] >>4 ) | (((x [ib ].scales [is + 0 ] >>4 )& 3 ) <<4 ) : 292 (x [ib ].scales [is - 8 ] >>4 ) | (((x [ib ].scales [is - 4 ] >>6 )& 3 ) <<4 ); 293 294const float dl = d_all * (us - 32 ); 295const uint8_t q = x [ib ].qs [32 * n + l ]; 296const uint8_t h = x [ib ].hmask [l ]; 297const int8_t qv = ((q >>shift )& 3 )- ((h & m ) ?0 :4 ); 298 299return (dfloat ) (dl * qv ); 300 }; 301 302v .x ()= dequantize_one (iqs + 0 ); 303v .y ()= dequantize_one (iqs + 1 ); 304#else 305GGML_ABORT ("Q3_K dequantize not supported for QK_K != 256" ); 306#endif 307} 308 309static __dpct_inline__ void dequantize_q5_K (const void * vx ,const int64_t ib , 310const int iqs ,dfloat2 & v ) { 311#if QK_K == 256 312const block_q5_K * x = (const block_q5_K * )vx ; 313const float dall = x [ib ].dm [0 ]; 314const float dmin = x [ib ].dm [1 ]; 315 316auto dequantize_one = [& ](const int idx )-> dfloat { 317const int il = idx /64 ; 318const int in = idx %64 ; 319const int is = 2 * il + (in >=32 ?1 :0 ); 320const int ir = (in & 31 ) /2 ; 321const int iq = in & 1 ; 322 323const uint8_t q = x [ib ].qs [32 * il + 2 * ir + iq ]; 324const uint8_t h = x [ib ].qh [2 * ir + iq ]; 325const uint8_t qv = (in >=32 ) ? (q >>4 ) : (q & 0xF ); 326 327uint8_t sc ; 328uint8_t m ; 329get_scale_min_k4 (is ,x [ib ].scales ,sc ,m ); 330 331const float d = dall * sc ; 332const float mn = dmin * m ; 333const uint8_t hm = 1 << (2 * il + (in >=32 ?1 :0 )); 334 335return sycl::fma ((dfloat ) (qv + ((h & hm ) ?16 :0 )), (dfloat )d , (dfloat ) (- mn )); 336 }; 337 338v .x ()= dequantize_one (iqs + 0 ); 339v .y ()= dequantize_one (iqs + 1 ); 340#else 341GGML_ABORT ("Q5_K dequantize not supported for QK_K != 256" ); 342#endif 343} 344 345static __dpct_inline__ void dequantize_q5_K_f32 (const void * vx ,const int64_t ib , 346const int iqs ,float & v0 ,float & v1 ) { 347#if QK_K == 256 348const block_q5_K * x = (const block_q5_K * )vx ; 349const float dall = x [ib ].dm [0 ]; 350const float dmin = x [ib ].dm [1 ]; 351 352auto dequantize_one = [& ](const int idx )-> float { 353const int il = idx /64 ; 354const int in = idx %64 ; 355const int is = 2 * il + (in >=32 ?1 :0 ); 356const int ir = (in & 31 ) /2 ; 357const int iq = in & 1 ; 358 359const uint8_t q = x [ib ].qs [32 * il + 2 * ir + iq ]; 360const uint8_t h = x [ib ].qh [2 * ir + iq ]; 361const uint8_t qv = (in >=32 ) ? (q >>4 ) : (q & 0xF ); 362 363uint8_t sc ; 364uint8_t m ; 365get_scale_min_k4 (is ,x [ib ].scales ,sc ,m ); 366 367const float d = dall * sc ; 368const float mn = dmin * m ; 369const uint8_t hm = 1 << (2 * il + (in >=32 ?1 :0 )); 370 371return (qv + ((h & hm ) ?16 :0 ))* d - mn ; 372 }; 373 374v0 = dequantize_one (iqs + 0 ); 375v1 = dequantize_one (iqs + 1 ); 376#else 377GGML_ABORT ("Q5_K dequantize not supported for QK_K != 256" ); 378#endif 379} 380 381static __dpct_inline__ void dequantize_q6_K (const void * vx ,const int64_t ib , 382const int iqs ,dfloat2 & v ) { 383#if QK_K == 256 384const block_q6_K * x = (const block_q6_K * )vx ; 385const float d = x [ib ].d ; 386 387auto dequantize_one = [& ](const int idx )-> dfloat { 388const int ip = idx /128 ; 389const int in = idx %128 ; 390const int il = in & 31 ; 391const int ig = in /32 ; 392const int is = 8 * ip + il /16 ; 393 394const uint8_t ql0 = x [ib ].ql [64 * ip + il ]; 395const uint8_t ql1 = x [ib ].ql [64 * ip + il + 32 ]; 396const uint8_t qh = x [ib ].qh [32 * ip + il ]; 397const int8_t * sc = x [ib ].scales + is ; 398 399uint8_t qv ; 400int8_t scale ; 401if (ig == 0 ) { 402qv = (ql0 & 0xF ) | (((qh >>0 )& 3 ) <<4 ); 403scale = sc [0 ]; 404 }else if (ig == 1 ) { 405qv = (ql1 & 0xF ) | (((qh >>2 )& 3 ) <<4 ); 406scale = sc [2 ]; 407 }else if (ig == 2 ) { 408qv = (ql0 >>4 ) | (((qh >>4 )& 3 ) <<4 ); 409scale = sc [4 ]; 410 }else { 411qv = (ql1 >>4 ) | (((qh >>6 )& 3 ) <<4 ); 412scale = sc [6 ]; 413 } 414 415return (dfloat ) (d * scale * ((int8_t )qv - 32 )); 416 }; 417 418v .x ()= dequantize_one (iqs + 0 ); 419v .y ()= dequantize_one (iqs + 1 ); 420#else 421GGML_ABORT ("Q6_K dequantize not supported for QK_K != 256" ); 422#endif 423} 424 425static __dpct_inline__ void dequantize_mxfp4 (const void * vx ,const int64_t ib , 426const int iqs ,dfloat2 & v ) { 427const block_mxfp4 * x = (const block_mxfp4 * )vx ; 428const float d = ggml_sycl_e8m0_to_fp32 (x [ib ].e ); 429const uint8_t q = x [ib ].qs [iqs ]; 430 431v .x ()= d * kvalues_mxfp4 [q & 0xF ]* 0.5f ; 432v .y ()= d * kvalues_mxfp4 [q >>4 ]* 0.5f ; 433} 434 435static __dpct_inline__ void dequantize_nvfp4 (const void * vx ,const int64_t ib , 436const int iqs ,dfloat2 & v ) { 437const block_nvfp4 & xb = ((const block_nvfp4 * )vx )[ib ]; 438 439auto dequantize_one = [& ](const int idx )-> dfloat { 440const int sub = idx /QK_NVFP4_SUB ; 441const int j = idx %QK_NVFP4_SUB ; 442const int jh = j % (QK_NVFP4_SUB /2 ); 443 444const float d = ggml_sycl_ue4m3_to_fp32 (xb .d [sub ]); 445const uint8_t q = xb .qs [sub * (QK_NVFP4_SUB /2 )+ jh ]; 446const uint8_t qv = (j < (QK_NVFP4_SUB /2 )) ? (q & 0x0F ) : (q >>4 ); 447 448return d * kvalues_mxfp4 [qv ]; 449 }; 450 451v .x ()= dequantize_one (iqs + 0 ); 452v .y ()= dequantize_one (iqs + 1 ); 453} 454 455static __dpct_inline__ void dequantize_iq2_xxs (const void * vx ,const int64_t ib , 456const int iqs ,dfloat2 & v ) { 457#if QK_K == 256 458const block_iq2_xxs * x = (const block_iq2_xxs * )vx ; 459 460auto dequantize_one = [& ](const int idx )-> dfloat { 461const int ib8 = idx /32 ; 462const int r = idx %32 ; 463const int il = r /8 ; 464const int j = r %8 ; 465 466const uint16_t * q2 = x [ib ].qs + 4 * ib8 ; 467const uint8_t * aux8 = (const uint8_t * )q2 ; 468const uint8_t * grid = (const uint8_t * ) (iq2xxs_grid + aux8 [il ]); 469const uint32_t aux32 = q2 [2 ] | (q2 [3 ] <<16 ); 470const float d = (float )x [ib ].d * (0.5f + (aux32 >>28 ))* 0.25f ; 471const uint8_t signs = ksigns_iq2xs [(aux32 >> (7 * il ))& 127 ]; 472 473return d * grid [j ]* ((signs & kmask_iq2xs [j ]) ?-1.f :1.f ); 474 }; 475 476v .x ()= dequantize_one (iqs + 0 ); 477v .y ()= dequantize_one (iqs + 1 ); 478#else 479GGML_ABORT ("IQ2_XXS dequantize not supported for QK_K != 256" ); 480#endif 481} 482 483static __dpct_inline__ void dequantize_iq2_xs (const void * vx ,const int64_t ib , 484const int iqs ,dfloat2 & v ) { 485#if QK_K == 256 486const block_iq2_xs * x = (const block_iq2_xs * )vx ; 487 488auto dequantize_one = [& ](const int idx )-> dfloat { 489const int ib8 = idx /32 ; 490const int r = idx %32 ; 491const int il = r /8 ; 492const int j = r %8 ; 493 494const uint16_t * q2 = x [ib ].qs + 4 * ib8 ; 495const uint8_t * grid = (const uint8_t * ) (iq2xs_grid + (q2 [il ]& 511 )); 496const float d = (float )x [ib ].d * (0.5f + ((x [ib ].scales [ib8 ] >> (4 * (il /2 )))& 0xf ))* 0.25f ; 497const uint8_t signs = ksigns_iq2xs [q2 [il ] >>9 ]; 498 499return d * grid [j ]* ((signs & kmask_iq2xs [j ]) ?-1.f :1.f ); 500 }; 501 502v .x ()= dequantize_one (iqs + 0 ); 503v .y ()= dequantize_one (iqs + 1 ); 504#else 505GGML_ABORT ("IQ2_XS dequantize not supported for QK_K != 256" ); 506#endif 507} 508 509static __dpct_inline__ void dequantize_iq2_s (const void * vx ,const int64_t ib , 510const int iqs ,dfloat2 & v ) { 511#if QK_K == 256 512const block_iq2_s * x = (const block_iq2_s * )vx ; 513 514auto dequantize_one = [& ](const int idx )-> dfloat { 515const int ib8 = idx /32 ; 516const int r = idx %32 ; 517const int il = r /8 ; 518const int j = r %8 ; 519 520const uint16_t grid_id = x [ib ].qs [4 * ib8 + il ] | ((x [ib ].qh [ib8 ] << (8 - 2 * il ))& 0x300 ); 521const uint8_t * grid = (const uint8_t * ) (iq2s_grid + grid_id ); 522const float d = (float )x [ib ].d * (0.5f + ((x [ib ].scales [ib8 ] >> (4 * (il /2 )))& 0xf ))* 0.25f ; 523const uint8_t signs = x [ib ].qs [QK_K /8 + 4 * ib8 + il ]; 524 525return d * grid [j ]* ((signs & kmask_iq2xs [j ]) ?-1.f :1.f ); 526 }; 527 528v .x ()= dequantize_one (iqs + 0 ); 529v .y ()= dequantize_one (iqs + 1 ); 530#else 531GGML_ABORT ("IQ2_S dequantize not supported for QK_K != 256" ); 532#endif 533} 534 535static __dpct_inline__ void dequantize_iq3_xxs (const void * vx ,const int64_t ib , 536const int iqs ,dfloat2 & v ) { 537#if QK_K == 256 538const block_iq3_xxs * x = (const block_iq3_xxs * )vx ; 539 540auto dequantize_one = [& ](const int idx )-> dfloat { 541const int ib8 = idx /32 ; 542const int r = idx %32 ; 543const int il = r /8 ; 544const int j = r %8 ; 545 546const uint8_t * q3 = x [ib ].qs + 8 * ib8 ; 547const uint16_t * gas = (const uint16_t * ) (x [ib ].qs + QK_K /4 )+ 2 * ib8 ; 548const uint8_t * grid1 = (const uint8_t * ) (iq3xxs_grid + q3 [2 * il + 0 ]); 549const uint8_t * grid2 = (const uint8_t * ) (iq3xxs_grid + q3 [2 * il + 1 ]); 550const uint32_t aux32 = gas [0 ] | (gas [1 ] <<16 ); 551const float d = (float )x [ib ].d * (0.5f + (aux32 >>28 ))* 0.5f ; 552const uint8_t signs = ksigns_iq2xs [(aux32 >> (7 * il ))& 127 ]; 553 554if (j < 4 ) { 555return d * grid1 [j ]* ((signs & kmask_iq2xs [j + 0 ]) ?-1.f :1.f ); 556 } 557return d * grid2 [j - 4 ]* ((signs & kmask_iq2xs [j + 0 ]) ?-1.f :1.f ); 558 }; 559 560v .x ()= dequantize_one (iqs + 0 ); 561v .y ()= dequantize_one (iqs + 1 ); 562#else 563GGML_ABORT ("IQ3_XXS dequantize not supported for QK_K != 256" ); 564#endif 565} 566 567static __dpct_inline__ void dequantize_iq3_s (const void * vx ,const int64_t ib , 568const int iqs ,dfloat2 & v ) { 569#if QK_K == 256 570const block_iq3_s * x = (const block_iq3_s * )vx ; 571 572auto dequantize_one = [& ](const int idx )-> dfloat { 573const int ib8 = idx /32 ; 574const int r = idx %32 ; 575const int il = r /8 ; 576const int j = r %8 ; 577 578const uint8_t * qs = x [ib ].qs + 8 * ib8 ; 579const uint16_t grid1_id = qs [2 * il + 0 ] | ((x [ib ].qh [ib8 ] << (8 - 2 * il ))& 256 ); 580const uint16_t grid2_id = qs [2 * il + 1 ] | ((x [ib ].qh [ib8 ] << (7 - 2 * il ))& 256 ); 581const uint8_t * grid1 = (const uint8_t * ) (iq3s_grid + grid1_id ); 582const uint8_t * grid2 = (const uint8_t * ) (iq3s_grid + grid2_id ); 583const float d = (float )x [ib ].d * (1 + 2 * ((x [ib ].scales [ib8 /2 ] >> (4 * (ib8 %2 )))& 0xf )); 584const uint8_t signs = x [ib ].signs [4 * ib8 + il ]; 585 586if (j < 4 ) { 587return d * grid1 [j ]* ((signs & kmask_iq2xs [j + 0 ]) ?-1.f :1.f ); 588 } 589return d * grid2 [j - 4 ]* ((signs & kmask_iq2xs [j + 0 ]) ?-1.f :1.f ); 590 }; 591 592v .x ()= dequantize_one (iqs + 0 ); 593v .y ()= dequantize_one (iqs + 1 ); 594#else 595GGML_ABORT ("IQ3_S dequantize not supported for QK_K != 256" ); 596#endif 597} 598 599static __dpct_inline__ void dequantize_iq1_s (const void * vx ,const int64_t ib , 600const int iqs ,dfloat2 & v ) { 601#if QK_K == 256 602const block_iq1_s * x = (const block_iq1_s * )vx ; 603 604auto dequantize_one = [& ](const int idx )-> dfloat { 605const int ib8 = idx /32 ; 606const int r = idx %32 ; 607const int il = r /8 ; 608const int j = r %8 ; 609 610const float delta = (x [ib ].qh [ib8 ]& 0x8000 ) ? (-1.f - IQ1S_DELTA ) : (-1.f + IQ1S_DELTA ); 611const float d = (float )x [ib ].d * (2 * ((x [ib ].qh [ib8 ] >>12 )& 7 )+ 1 ); 612const uint16_t grid_id = x [ib ].qs [4 * ib8 + il ] | (((x [ib ].qh [ib8 ] >> (3 * il ))& 7 ) <<8 ); 613const uint32_t g = iq1s_grid_gpu [grid_id ]; 614const int8_t qv = (j < 4 ) ? ((g >> (8 * j ))& 0x0F ) : ((g >> (8 * (j - 4 )+ 4 ))& 0x0F ); 615 616return d * (qv + delta ); 617 }; 618 619v .x ()= dequantize_one (iqs + 0 ); 620v .y ()= dequantize_one (iqs + 1 ); 621#else 622GGML_ABORT ("IQ1_S dequantize not supported for QK_K != 256" ); 623#endif 624} 625 626static __dpct_inline__ void dequantize_iq1_m (const void * vx ,const int64_t ib , 627const int iqs ,dfloat2 & v ) { 628#if QK_K == 256 629const block_iq1_m * x = (const block_iq1_m * )vx ; 630 631auto dequantize_one = [& ](const int idx )-> dfloat { 632const int ib8 = idx /32 ; 633const int r = idx %32 ; 634const int il = r /8 ; 635const int j = r %8 ; 636 637const uint16_t * sc = (const uint16_t * )x [ib ].scales ; 638iq1m_scale_t scale ; 639scale .u16 = (sc [0 ] >>12 ) | ((sc [1 ] >>8 )& 0x00f0 ) | ((sc [2 ] >>4 )& 0x0f00 ) | (sc [3 ]& 0xf000 ); 640 641const int ib16 = 2 * ib8 + il /2 ; 642const float d = (float )scale .f16 * (2 * ((sc [ib16 /4 ] >> (3 * (ib16 %4 )))& 0x7 )+ 1 ); 643 644const uint8_t qh = x [ib ].qh [2 * ib8 + il /2 ]; 645const float delta = (qh & (0x08 << (4 * (il %2 )))) ? (-1.f - IQ1M_DELTA ) : (-1.f + IQ1M_DELTA ); 646 647const uint16_t grid_id = x [ib ].qs [4 * ib8 + il ] | (((qh >> (4 * (il %2 )))& 7 ) <<8 ); 648const uint32_t g = iq1s_grid_gpu [grid_id ]; 649const int8_t qv = (j < 4 ) ? ((g >> (8 * j ))& 0x0F ) : ((g >> (8 * (j - 4 )+ 4 ))& 0x0F ); 650 651return d * (qv + delta ); 652 }; 653 654v .x ()= dequantize_one (iqs + 0 ); 655v .y ()= dequantize_one (iqs + 1 ); 656#else 657GGML_ABORT ("IQ1_M dequantize not supported for QK_K != 256" ); 658#endif 659} 660 661static __dpct_inline__ void dequantize_iq4_nl (const void * vx ,const int64_t ib , 662const int iqs ,dfloat2 & v ) { 663const block_iq4_nl * x = (const block_iq4_nl * )vx ; 664const float d = (float )x [ib ].d ; 665 666auto dequantize_one = [& ](const int idx )-> dfloat { 667if (idx < 16 ) { 668return d * kvalues_iq4nl [x [ib ].qs [idx ]& 0xF ]; 669 } 670return d * kvalues_iq4nl [x [ib ].qs [idx - 16 ] >>4 ]; 671 }; 672 673v .x ()= dequantize_one (iqs + 0 ); 674v .y ()= dequantize_one (iqs + 1 ); 675} 676 677static __dpct_inline__ void dequantize_iq4_xs (const void * vx ,const int64_t ib , 678const int iqs ,dfloat2 & v ) { 679#if QK_K == 256 680const block_iq4_xs * x = (const block_iq4_xs * )vx ; 681 682auto dequantize_one = [& ](const int idx )-> dfloat { 683const int ib8 = idx /32 ; 684const int r = idx %32 ; 685const int byte_idx = (r < 16 ) ?r : (r - 16 ); 686const uint8_t q = x [ib ].qs [16 * ib8 + byte_idx ]; 687const uint8_t qv = (r < 16 ) ? (q & 0x0F ) : (q >>4 ); 688 689const float d = (float )x [ib ].d * ((((x [ib ].scales_l [ib8 /2 ] >> (4 * (ib8 %2 )))& 0xf ) | 690 (((x [ib ].scales_h >> (2 * ib8 ))& 3 ) <<4 ))- 32 ); 691return d * kvalues_iq4nl [qv ]; 692 }; 693 694v .x ()= dequantize_one (iqs + 0 ); 695v .y ()= dequantize_one (iqs + 1 ); 696#else 697GGML_ABORT ("IQ4_XS dequantize not supported for QK_K != 256" ); 698#endif 699} 700 701static __dpct_inline__ void dequantize_q5_0 (const void * vx ,const int64_t ib , 702const int iqs ,dfloat2 & v ) { 703const block_q5_0 * x = (const block_q5_0 * )vx ; 704 705const dfloat d = x [ib ].d ; 706 707uint32_t qh ; 708memcpy (& qh ,x [ib ].qh ,sizeof (qh )); 709 710const int xh_0 = ((qh >> (iqs + 0 )) <<4 )& 0x10 ; 711const int xh_1 = ((qh >> (iqs + 12 )) )& 0x10 ; 712 713v .x ()= ((x [ib ].qs [iqs ]& 0xf ) |xh_0 ); 714v .y ()= ((x [ib ].qs [iqs ] >>4 ) |xh_1 ); 715 716#ifdef GGML_SYCL_F16 717// v = v - {16.0f, 16.0f}; 718// v = v * {d, d}; 719v .s0 ()= (v .s0 ()- 16.0f )* d ; 720v .s1 ()= (v .s1 ()- 16.0f )* d ; 721 722#else 723v .x ()= (v .x ()- 16.0f )* d ; 724v .y ()= (v .y ()- 16.0f )* d ; 725#endif // GGML_SYCL_F16 726} 727 728static __dpct_inline__ void dequantize_q5_1 (const void * vx ,const int64_t ib , 729const int iqs ,dfloat2 & v ) { 730const block_q5_1 * x = (const block_q5_1 * )vx ; 731 732const dfloat d = x [ib ].dm [0 ]; 733const dfloat m = x [ib ].dm [1 ]; 734 735uint32_t qh ; 736memcpy (& qh ,x [ib ].qh ,sizeof (qh )); 737 738const int xh_0 = ((qh >> (iqs + 0 )) <<4 )& 0x10 ; 739const int xh_1 = ((qh >> (iqs + 12 )) )& 0x10 ; 740 741v .x ()= ((x [ib ].qs [iqs ]& 0xf ) |xh_0 ); 742v .y ()= ((x [ib ].qs [iqs ] >>4 ) |xh_1 ); 743 744#ifdef GGML_SYCL_F16 745// v = v * {d, d}; 746// v = v + {m, m}; 747v .s0 ()= sycl::fma (v .s0 (),d ,m ); 748v .s1 ()= sycl::fma (v .s1 (),d ,m ); 749#else 750v .x ()= sycl::fma (v .x (),d ,m ); 751v .y ()= sycl::fma (v .y (),d ,m ); 752#endif // GGML_SYCL_F16 753} 754 755static __dpct_inline__ void dequantize_q8_0_reorder (const void * d_ptr ,const int64_t ib ,const void * qs , 756const int iqs ,dfloat2 & v ) { 757const dfloat d = (const dfloat )* ((const sycl::half * )d_ptr + ib ); 758 759v .x ()= ((const int8_t * )qs )[iqs + 0 ]; 760v .y ()= ((const int8_t * )qs )[iqs + 1 ]; 761 762#ifdef GGML_SYCL_F16 763v .s0 () *=d ; 764v .s1 () *=d ; 765#else 766v .x () *=d ; 767v .y () *=d ; 768#endif // GGML_SYCL_F16 769} 770 771static __dpct_inline__ void dequantize_q8_0 (const void * vx ,const int64_t ib , 772const int iqs ,dfloat2 & v ) { 773const block_q8_0 * x = (const block_q8_0 * )vx ; 774 775const dfloat d = x [ib ].d ; 776 777v .x ()= x [ib ].qs [iqs + 0 ]; 778v .y ()= x [ib ].qs [iqs + 1 ]; 779 780#ifdef GGML_SYCL_F16 781// v = v * {d, d}; 782v .s0 () *=d ; 783v .s1 () *=d ; 784#else 785v .x () *=d ; 786v .y () *=d ; 787#endif // GGML_SYCL_F16 788} 789 790template < typename dst_t > 791static void dequantize_block_q4_0 (const void * __restrict__vx ,dst_t * __restrict__yy ,int64_t nb32 , 792const sycl::nd_item < 3 > & item_ct1 ) { 793 794const int64_t i = item_ct1 .get_group (2 ); 795 796// assume 32 threads 797const int64_t tid = item_ct1 .get_local_id (2 ); 798const int64_t il = tid /8 ; 799const int64_t ir = tid %8 ; 800const int64_t ib = 8 * i + ir ; 801if (ib >=nb32 ) { 802return ; 803 } 804 805dst_t * y = yy + 256 * i + 32 * ir + 4 * il ; 806 807const block_q4_0 * x = (const block_q4_0 * )vx + ib ; 808const float d = sycl::vec < sycl::half ,1 > (x -> d ) 809 .convert < float , sycl::rounding_mode::automatic > ()[0 ]; 810const float dm = -8 * d ; 811 812const uint8_t * q = x -> qs + 4 * il ; 813 814for (int l = 0 ;l < 4 ;++ l ) { 815y [l + 0 ]= d * (q [l ]& 0xF )+ dm ; 816y [l + 16 ]= d * (q [l ] >>4 )+ dm ; 817 } 818} 819 820template < typename dst_t > 821static void dequantize_block_q4_0_reorder (const void * __restrict__vx ,dst_t * __restrict__yy ,int64_t nb32 , 822const sycl::nd_item < 3 > & item_ct1 ) { 823 824const int64_t i = item_ct1 .get_group (2 ); 825auto k = nb32 ; 826// assume 32 threads 827const int64_t tid = item_ct1 .get_local_id (2 ); 828const int lane_ib = i * WARP_SIZE + tid ; 829 830if (lane_ib >=k /QK4_0 ) { 831return ; 832 } 833 834dst_t * y_ptr = yy + lane_ib * QK4_0 ; 835 836auto qs = (const uint8_t * )vx + lane_ib * QK4_0 /2 ; 837auto s_ptr = (const sycl::half * )((const uint8_t * )vx + k /2 )+ lane_ib ; 838 839const float d = float (* s_ptr ); 840 841#pragma unroll 842for (int l = 0 ;l < QK4_0 /2 ;++ l ) { 843int vq = qs [l ]; 844y_ptr [l + 0 ]= d * ((vq & 0xF )- 8 ); 845y_ptr [l + 16 ]= d * ((vq >>4 )- 8 ); 846 } 847 848} 849 850// Dequantize Q8_0 from reorder layout: [all qs (k bytes)][all d values] 851// Each thread handles one block of QK8_0 elements. 852template < typename dst_t > 853static void dequantize_block_q8_0_reorder (const void * __restrict__vx ,dst_t * __restrict__yy ,int64_t k , 854const sycl::nd_item < 3 > & item_ct1 ) { 855 856const int64_t i = item_ct1 .get_group (2 ); 857const int64_t tid = item_ct1 .get_local_id (2 ); 858const int lane_ib = i * WARP_SIZE + tid ; 859 860if (lane_ib >=k /QK8_0 ) { 861return ; 862 } 863 864dst_t * y_ptr = yy + lane_ib * QK8_0 ; 865 866auto qs = (const int8_t * )vx + lane_ib * QK8_0 ; 867auto s_ptr = (const sycl::half * )((const uint8_t * )vx + k )+ lane_ib ; 868 869const float d = float (* s_ptr ); 870 871#pragma unroll 872for (int l = 0 ;l < QK8_0 ;++ l ) { 873y_ptr [l ]= d * qs [l ]; 874 } 875 876} 877 878template < typename dst_t > 879static void dequantize_block_q4_1 (const void * __restrict__vx ,dst_t * __restrict__yy ,int64_t nb32 , 880const sycl::nd_item < 3 > & item_ct1 ) { 881 882const int64_t i = item_ct1 .get_group (2 ); 883 884// assume 32 threads 885const int64_t tid = item_ct1 .get_local_id (2 ); 886const int64_t il = tid /8 ; 887const int64_t ir = tid %8 ; 888const int64_t ib = 8 * i + ir ; 889if (ib >=nb32 ) { 890return ; 891 } 892 893dst_t * y = yy + 256 * i + 32 * ir + 4 * il ; 894 895const block_q4_1 * x = (const block_q4_1 * )vx + ib ; 896const sycl::float2 d = 897x -> dm .convert < float , sycl::rounding_mode::automatic > (); 898 899const uint8_t * q = x -> qs + 4 * il ; 900 901for (int l = 0 ;l < 4 ;++ l ) { 902y [l + 0 ]= d .x ()* (q [l ]& 0xF )+ d .y (); 903y [l + 16 ]= d .x ()* (q [l ] >>4 )+ d .y (); 904 } 905} 906 907 908//================================== k-quants 909 910template < typename dst_t > 911static void dequantize_block_q2_K (const void * __restrict__vx ,dst_t * __restrict__yy , 912const sycl::nd_item < 3 > & item_ct1 ) { 913 914const int64_t i = item_ct1 .get_group (2 ); 915const block_q2_K * x = (const block_q2_K * )vx ; 916 917const int64_t tid = item_ct1 .get_local_id (2 ); 918#if QK_K == 256 919const int64_t n = tid /32 ; 920const int64_t l = tid - 32 * n ; 921const int64_t is = 8 * n + l /16 ; 922 923const uint8_t q = x [i ].qs [32 * n + l ]; 924dst_t * y = yy + i * QK_K + 128 * n ; 925 926float dall = x [i ].dm [0 ]; 927float dmin = x [i ].dm [1 ]; 928y [l + 0 ]= dall * (x [i ].scales [is + 0 ]& 0xF )* ((q >>0 )& 3 )- dmin * (x [i ].scales [is + 0 ] >>4 ); 929y [l + 32 ]= dall * (x [i ].scales [is + 2 ]& 0xF )* ((q >>2 )& 3 )- dmin * (x [i ].scales [is + 2 ] >>4 ); 930y [l + 64 ]= dall * (x [i ].scales [is + 4 ]& 0xF )* ((q >>4 )& 3 )- dmin * (x [i ].scales [is + 4 ] >>4 ); 931y [l + 96 ]= dall * (x [i ].scales [is + 6 ]& 0xF )* ((q >>6 )& 3 )- dmin * (x [i ].scales [is + 6 ] >>4 ); 932#else 933const int64_t is = tid /16 ;// 0 or 1 934const int64_t il = tid %16 ;// 0...15 935const uint8_t q = x [i ].qs [il ] >> (2 * is ); 936dst_t * y = yy + i * QK_K + 16 * is + il ; 937 938float dall = x [i ].dm [0 ]; 939float dmin = x [i ].dm [1 ]; 940y [0 ]= dall * (x [i ].scales [is + 0 ]& 0xF )* ((q >>0 )& 3 )- dmin * (x [i ].scales [is + 0 ] >>4 ); 941y [32 ]= dall * (x [i ].scales [is + 2 ]& 0xF )* ((q >>4 )& 3 )- dmin * (x [i ].scales [is + 2 ] >>4 ); 942#endif 943 944} 945 946template < typename dst_t > 947static void dequantize_block_q2_K_reorder (const void * __restrict__vx ,dst_t * __restrict__yy , 948const sycl::nd_item < 3 > & item_ct1 ,int64_t n_blocks ) { 949#if QK_K == 256 950const int64_t i = item_ct1 .get_group (2 ); 951if (i >=n_blocks ) { 952return ; 953 } 954 955const uint8_t * base = static_cast < const uint8_t *> (vx ); 956const size_t qs_offset = i * (QK_K /4 ); 957const size_t scales_offset = n_blocks * (QK_K /4 )+ i * (QK_K /16 ); 958const size_t dm_offset = n_blocks * (QK_K /4 )+ n_blocks * (QK_K /16 )+ i * sizeof (ggml_half2 ); 959 960const uint8_t * qs = base + qs_offset ; 961const uint8_t * scales = base + scales_offset ; 962const ggml_half2 * dm = reinterpret_cast < const ggml_half2 *> (base + dm_offset ); 963 964const int64_t tid = item_ct1 .get_local_id (2 ); 965const int64_t n = tid /32 ; 966const int64_t l = tid - 32 * n ; 967const int64_t is = 8 * n + l /16 ; 968 969const uint8_t q = qs [32 * n + l ]; 970dst_t * y = yy + i * QK_K + 128 * n ; 971 972const float dall = (* dm )[0 ]; 973const float dmin = (* dm )[1 ]; 974y [l + 0 ]= dall * (scales [is + 0 ]& 0xF )* ((q >>0 )& 3 )- dmin * (scales [is + 0 ] >>4 ); 975y [l + 32 ]= dall * (scales [is + 2 ]& 0xF )* ((q >>2 )& 3 )- dmin * (scales [is + 2 ] >>4 ); 976y [l + 64 ]= dall * (scales [is + 4 ]& 0xF )* ((q >>4 )& 3 )- dmin * (scales [is + 4 ] >>4 ); 977y [l + 96 ]= dall * (scales [is + 6 ]& 0xF )* ((q >>6 )& 3 )- dmin * (scales [is + 6 ] >>4 ); 978#else 979GGML_UNUSED (vx ); 980GGML_UNUSED (yy ); 981GGML_UNUSED (item_ct1 ); 982GGML_UNUSED (n_blocks ); 983GGML_ABORT ("Q2_K reorder dequantize not supported for QK_K != 256" ); 984#endif 985} 986 987template < typename dst_t > 988static void dequantize_block_q3_K (const void * __restrict__vx ,dst_t * __restrict__yy , 989const sycl::nd_item < 3 > & item_ct1 ) { 990 991const int64_t i = item_ct1 .get_group (2 ); 992const block_q3_K * x = (const block_q3_K * )vx ; 993 994#if QK_K == 256 995const int64_t r = item_ct1 .get_local_id (2 ) /4 ; 996const int64_t tid = r /2 ; 997const int64_t is0 = r %2 ; 998const int64_t l0 = 16 * is0 + 4 * (item_ct1 .get_local_id (2 ) %4 ); 999const int64_t n = tid /4 ; 1000const int64_t j = tid - 4 * n ; 1001 1002uint8_t m = 1 << (4 * n + j ); 1003int64_t is = 8 * n + 2 * j + is0 ; 1004int shift = 2 * j ; 1005 1006int8_t us = is < 4 ? (x [i ].scales [is - 0 ]& 0xF ) | (((x [i ].scales [is + 8 ] >>0 )& 3 ) <<4 ) : 1007is < 8 ? (x [i ].scales [is - 0 ]& 0xF ) | (((x [i ].scales [is + 4 ] >>2 )& 3 ) <<4 ) : 1008is < 12 ? (x [i ].scales [is - 8 ] >>4 ) | (((x [i ].scales [is + 0 ] >>4 )& 3 ) <<4 ) : 1009 (x [i ].scales [is - 8 ] >>4 ) | (((x [i ].scales [is - 4 ] >>6 )& 3 ) <<4 ); 1010float d_all = x [i ].d ; 1011float dl = d_all * (us - 32 ); 1012 1013dst_t * y = yy + i * QK_K + 128 * n + 32 * j ; 1014const uint8_t * q = x [i ].qs + 32 * n ; 1015const uint8_t * hm = x [i ].hmask ; 1016 1017for (int l = l0 ;l < l0 + 4 ;++ l )y [l ]= dl * ((int8_t )((q [l ] >>shift )& 3 )- ((hm [l ]& m ) ?0 :4 )); 1018#else 1019const int64_t tid = item_ct1 .get_local_id (2 ); 1020const int64_t is = tid /16 ;// 0 or 1 1021const int64_t il = tid %16 ;// 0...15 1022const int64_t im = il /8 ;// 0...1 1023const int64_t in = il %8 ;// 0...7 1024 1025dst_t * y = yy + i * QK_K + 16 * is + il ; 1026 1027const uint8_t q = x [i ].qs [il ] >> (2 * is ); 1028const uint8_t h = x [i ].hmask [in ] >> (2 * is + im ); 1029const float d = (float )x [i ].d ; 1030 1031if (is == 0 ) { 1032y [0 ]= d * ((x [i ].scales [0 ]& 0xF )- 8 )* ((int8_t )((q >>0 )& 3 )- ((h >>0 )& 1 ?0 :4 )); 1033y [32 ]= d * ((x [i ].scales [1 ]& 0xF )- 8 )* ((int8_t )((q >>4 )& 3 )- ((h >>4 )& 1 ?0 :4 )); 1034 }else { 1035y [0 ]= d * ((x [i ].scales [0 ] >>4 )- 8 )* ((int8_t )((q >>0 )& 3 )- ((h >>0 )& 1 ?0 :4 )); 1036y [32 ]= d * ((x [i ].scales [1 ] >>4 )- 8 )* ((int8_t )((q >>4 )& 3 )- ((h >>4 )& 1 ?0 :4 )); 1037 } 1038#endif 1039 1040} 1041 1042template < typename dst_t > 1043static void dequantize_block_q3_K_reorder (const void * __restrict__vx ,dst_t * __restrict__yy , 1044const sycl::nd_item < 3 > & item_ct1 ,int64_t n_blocks ) { 1045#if QK_K == 256 1046const int64_t i = item_ct1 .get_group (2 ); 1047if (i >=n_blocks ) { 1048return ; 1049 } 1050 1051const uint8_t * base = static_cast < const uint8_t *> (vx ); 1052const size_t qs_offset = i * (QK_K /4 ); 1053const size_t hmask_offset = n_blocks * (QK_K /4 )+ i * (QK_K /8 ); 1054const size_t scales_offset = n_blocks * (QK_K /4 )+ n_blocks * (QK_K /8 )+ i * 12 ; 1055const size_t d_offset = n_blocks * (QK_K /4 )+ n_blocks * (QK_K /8 )+ n_blocks * 12 + 1056i * sizeof (ggml_half ); 1057 1058const uint8_t * qs = base + qs_offset ; 1059const uint8_t * hmask = base + hmask_offset ; 1060const uint8_t * scales = base + scales_offset ; 1061const float d_all = static_cast < float > (* reinterpret_cast < const ggml_half *> (base + d_offset )); 1062 1063const int64_t r = item_ct1 .get_local_id (2 ) /4 ; 1064const int64_t tid = r /2 ; 1065const int64_t is0 = r %2 ; 1066const int64_t l0 = 16 * is0 + 4 * (item_ct1 .get_local_id (2 ) %4 ); 1067const int64_t n = tid /4 ; 1068const int64_t j = tid - 4 * n ; 1069const int64_t is = 8 * n + 2 * j + is0 ; 1070const int shift = 2 * j ; 1071uint8_t m = 1 << (4 * n + j ); 1072 1073uint8_t us = is < 4 1074 ? (scales [is - 0 ]& 0xF ) | (((scales [is + 8 ] >>0 )& 3 ) <<4 ) 1075 :is < 8 1076 ? (scales [is - 0 ]& 0xF ) | (((scales [is + 4 ] >>2 )& 3 ) <<4 ) 1077 :is < 12 1078 ? (scales [is - 8 ] >>4 ) | (((scales [is + 0 ] >>4 )& 3 ) <<4 ) 1079 : (scales [is - 8 ] >>4 ) | (((scales [is - 4 ] >>6 )& 3 ) <<4 ); 1080 1081const float dl = d_all * (us - 32 ); 1082 1083dst_t * y = yy + i * QK_K + 128 * n + 32 * j ; 1084const uint8_t * q = qs + 32 * n ; 1085const uint8_t * hm = hmask ; 1086 1087for (int l = l0 ;l < l0 + 4 ;++ l ) { 1088y [l ]= dl * ((int8_t ) ((q [l ] >>shift )& 3 )- ((hm [l ]& m ) ?0 :4 )); 1089 } 1090#else 1091GGML_UNUSED (vx ); 1092GGML_UNUSED (yy ); 1093GGML_UNUSED (item_ct1 ); 1094GGML_UNUSED (n_blocks ); 1095GGML_ABORT ("Q3_K reorder dequantize not supported for QK_K != 256" ); 1096#endif 1097} 1098 1099#if QK_K == 256 1100static inline void get_scale_min_k4 (int j ,const uint8_t * q ,uint8_t & d ,uint8_t & m ) { 1101if (j < 4 ) { 1102d = q [j ]& 63 ; 1103m = q [j + 4 ]& 63 ; 1104 }else { 1105d = (q [j + 4 ]& 0xF ) | ((q [j - 4 ] >>6 ) <<4 ); 1106m = (q [j + 4 ] >>4 ) | ((q [j - 0 ] >>6 ) <<4 ); 1107 } 1108} 1109#endif 1110 1111template < typename dst_t > 1112inline void dequantize_q4_K_common (dst_t * __restrict__y ,const uint8_t * __restrict__qs_ptr ,const float dall , 1113const float dmin ,uint8_t * __restrict__scales_local ,int il ,int ir ) { 1114const int is = 2 * il ; 1115constexpr int n = 4 ; 1116 1117uint8_t sc ,m ; 1118get_scale_min_k4 (is + 0 ,scales_local ,sc ,m ); 1119const float d1 = dall * sc ; 1120const float m1 = dmin * m ; 1121 1122get_scale_min_k4 (is + 1 ,scales_local ,sc ,m ); 1123const float d2 = dall * sc ; 1124const float m2 = dmin * m ; 1125 1126 sycl::vec < uint8_t ,n > q_vec = vec_aligned_load < uint8_t ,n > (qs_ptr + 32 * il + n * ir ); 1127for (int l = 0 ;l < n ;++ l ) { 1128y [l + 0 ]= d1 * (q_vec [l ]& 0xF )- m1 ; 1129y [l + 32 ]= d2 * (q_vec [l ] >>4 )- m2 ; 1130 } 1131} 1132 1133template < typename dst_t > 1134static void dequantize_block_q4_K (const void * __restrict__vx ,dst_t * __restrict__yy , 1135uint8_t * scales_local ,const sycl::nd_item < 3 > & item_ct1 ) { 1136const block_q4_K * x = (const block_q4_K * )vx ; 1137 1138const int64_t i = item_ct1 .get_group (2 ); 1139 1140#if QK_K == 256 1141const int64_t tid = item_ct1 .get_local_id (2 ); 1142const int64_t il = tid /8 ; 1143const int64_t ir = tid %8 ; 1144 1145dst_t * y = yy + i * QK_K + 64 * il + 4 * ir ; 1146 1147const sycl::half2 dm = x [i ].dm ; 1148const float dall = dm [0 ]; 1149const float dmin = dm [1 ]; 1150 1151if (tid < 12 ) { 1152scales_local [tid ]= x [i ].scales [tid ]; 1153 } 1154 1155item_ct1 .barrier (sycl::access::fence_space::local_space ); 1156dequantize_q4_K_common (y ,x [i ].qs ,dall ,dmin ,scales_local ,il ,ir ); 1157#else 1158const int64_t tid = item_ct1 .get_local_id (2 ); 1159const uint8_t * q = x [i ].qs ; 1160dst_t * y = yy + i * QK_K ; 1161const float d = (float )x [i ].dm [0 ]; 1162const float m = (float )x [i ].dm [1 ]; 1163y [tid + 0 ]= d * (x [i ].scales [0 ]& 0xF )* (q [tid ]& 0xF )- m * (x [i ].scales [0 ] >>4 ); 1164y [tid + 32 ]= d * (x [i ].scales [1 ]& 0xF )* (q [tid ] >>4 )- m * (x [i ].scales [1 ] >>4 ); 1165#endif 1166} 1167 1168template < typename dst_t > 1169static void dequantize_block_q4_K_reorder (const void * __restrict__vx ,dst_t * __restrict__yy ,uint8_t * scales_local , 1170const sycl::nd_item < 1 > & item_ct1 ,int64_t nb ) { 1171const int64_t i = item_ct1 .get_group (0 );// block index 1172const int64_t tid = item_ct1 .get_local_id (0 );// thread index within block 1173const int64_t il = tid /8 ; 1174const int64_t ir = tid %8 ; 1175 1176dst_t * y = yy + i * QK_K + 64 * il + 4 * ir ; 1177 1178const uint8_t * base = static_cast < const uint8_t *> (vx ); 1179const size_t qs_offset = i * (QK_K /2 ); 1180const size_t scales_offset = nb * (QK_K /2 )+ i * K_SCALE_SIZE ; 1181const size_t dm_offset = nb * (QK_K /2 )+ nb * K_SCALE_SIZE + i * sizeof (ggml_half2 ); 1182 1183const uint8_t * qs_ptr = base + qs_offset ; 1184const uint8_t * scales_ptr = base + scales_offset ; 1185ggml_half2 dm_values = * reinterpret_cast < const ggml_half2 *> (base + dm_offset ); 1186 1187const float dall = dm_values .x (); 1188const float dmin = dm_values .y (); 1189 1190if (tid < 12 ) { 1191scales_local [tid ]= scales_ptr [tid ]; 1192 } 1193 1194item_ct1 .barrier (sycl::access::fence_space::local_space ); 1195dequantize_q4_K_common (y ,qs_ptr ,dall ,dmin ,scales_local ,il ,ir ); 1196} 1197 1198template < typename dst_t > 1199static void dequantize_block_q5_K (const void * __restrict__vx ,dst_t * __restrict__yy , 1200const sycl::nd_item < 3 > & item_ct1 ) { 1201const block_q5_K * x = (const block_q5_K * )vx ; 1202 1203const int64_t i = item_ct1 .get_group (2 ); 1204 1205#if QK_K == 256 1206// assume 64 threads - this is very slightly better than the one below 1207const int64_t tid = item_ct1 .get_local_id (2 ); 1208const int64_t il = tid /16 ;// il is in 0...3 1209const int64_t ir = tid %16 ;// ir is in 0...15 1210const int64_t is = 2 * il ;// is is in 0...6 1211 1212dst_t * y = yy + i * QK_K + 64 * il + 2 * ir ; 1213 1214const float dall = x [i ].dm [0 ]; 1215const float dmin = x [i ].dm [1 ]; 1216 1217const uint8_t * ql = x [i ].qs + 32 * il + 2 * ir ; 1218const uint8_t * qh = x [i ].qh + 2 * ir ; 1219 1220uint8_t sc ,m ; 1221get_scale_min_k4 (is + 0 ,x [i ].scales ,sc ,m ); 1222const float d1 = dall * sc ;const float m1 = dmin * m ; 1223get_scale_min_k4 (is + 1 ,x [i ].scales ,sc ,m ); 1224const float d2 = dall * sc ;const float m2 = dmin * m ; 1225 1226uint8_t hm = 1 << (2 * il ); 1227y [0 ]= d1 * ((ql [0 ]& 0xF )+ (qh [0 ]& hm ?16 :0 ))- m1 ; 1228y [1 ]= d1 * ((ql [1 ]& 0xF )+ (qh [1 ]& hm ?16 :0 ))- m1 ; 1229hm <<=1 ; 1230y [32 ]= d2 * ((ql [0 ] >>4 )+ (qh [0 ]& hm ?16 :0 ))- m2 ; 1231y [33 ]= d2 * ((ql [1 ] >>4 )+ (qh [1 ]& hm ?16 :0 ))- m2 ; 1232#else 1233const int64_t tid = item_ct1 .get_local_id (2 ); 1234const uint8_t q = x [i ].qs [tid ]; 1235const int64_t im = tid /8 ;// 0...3 1236const int64_t in = tid %8 ;// 0...7 1237const int64_t is = tid /16 ;// 0 or 1 1238const uint8_t h = x [i ].qh [in ] >>im ; 1239const float d = x [i ].d ; 1240dst_t * y = yy + i * QK_K + tid ; 1241y [0 ]= d * x [i ].scales [is + 0 ]* ((q & 0xF )- ((h >>0 )& 1 ?0 :16 )); 1242y [32 ]= d * x [i ].scales [is + 2 ]* ((q >>4 )- ((h >>4 )& 1 ?0 :16 )); 1243#endif 1244} 1245 1246template < typename dst_t > 1247static void dequantize_block_q5_K_reorder (const void * __restrict__vx ,dst_t * __restrict__yy , 1248uint8_t * scales_local ,const sycl::nd_item < 3 > & item_ct1 ,int64_t n_blocks ) { 1249const int64_t ib = item_ct1 .get_group (2 ); 1250 1251#if QK_K == 256 1252// assume 64 threads 1253const int64_t tid = item_ct1 .get_local_id (2 ); 1254const int64_t il = tid /16 ;// 0...3 1255const int64_t ir = tid %16 ;// 0...15 1256const int64_t is = 2 * il ; 1257 1258dst_t * y = yy + ib * QK_K + 64 * il + 2 * ir ; 1259 1260const uint8_t * base = static_cast < const uint8_t *> (vx ); 1261 1262// Reordered layout: [qs (QK_K/2 per block)] [qh (QK_K/8 per block)] [scales (K_SCALE_SIZE per block)] [dm (half2 per block)] 1263const size_t qs_offset = ib * (QK_K /2 ); 1264const size_t qh_offset = n_blocks * (QK_K /2 )+ ib * (QK_K /8 ); 1265const size_t scales_offset = n_blocks * (QK_K /2 )+ n_blocks * (QK_K /8 )+ ib * K_SCALE_SIZE ; 1266const size_t dm_offset = n_blocks * (QK_K /2 )+ n_blocks * (QK_K /8 )+ n_blocks * K_SCALE_SIZE + ib * sizeof (ggml_half2 ); 1267 1268const uint8_t * qs_ptr = base + qs_offset ; 1269const uint8_t * qh_ptr = base + qh_offset ; 1270const uint8_t * scales_ptr = base + scales_offset ; 1271const ggml_half2 dm_values = * reinterpret_cast < const ggml_half2 *> (base + dm_offset ); 1272 1273const float dall = dm_values .x (); 1274const float dmin = dm_values .y (); 1275 1276const uint8_t * ql = qs_ptr + 32 * il + 2 * ir ; 1277const uint8_t * qh = qh_ptr + 2 * ir ; 1278 1279if (tid < K_SCALE_SIZE ) { 1280scales_local [tid ]= scales_ptr [tid ]; 1281 } 1282 1283item_ct1 .barrier (sycl::access::fence_space::local_space ); 1284 1285uint8_t sc ,m ; 1286get_scale_min_k4 (is + 0 ,scales_local ,sc ,m ); 1287const float d1 = dall * sc ;const float m1 = dmin * m ; 1288get_scale_min_k4 (is + 1 ,scales_local ,sc ,m ); 1289const float d2 = dall * sc ;const float m2 = dmin * m ; 1290 1291uint8_t hm = 1 << (2 * il ); 1292y [0 ]= d1 * ((ql [0 ]& 0xF )+ (qh [0 ]& hm ?16 :0 ))- m1 ; 1293y [1 ]= d1 * ((ql [1 ]& 0xF )+ (qh [1 ]& hm ?16 :0 ))- m1 ; 1294hm <<=1 ; 1295y [32 ]= d2 * ((ql [0 ] >>4 )+ (qh [0 ]& hm ?16 :0 ))- m2 ; 1296y [33 ]= d2 * ((ql [1 ] >>4 )+ (qh [1 ]& hm ?16 :0 ))- m2 ; 1297#else 1298GGML_UNUSED (ib );GGML_UNUSED (tid );GGML_UNUSED (yy );GGML_UNUSED (scales_local );GGML_UNUSED (n_blocks ); 1299GGML_ABORT ("Q5_K reorder dequantize not supported for QK_K != 256" ); 1300#endif 1301} 1302 1303template < typename dst_t > 1304static void dequantize_block_q6_K (const void * __restrict__vx ,dst_t * __restrict__yy , 1305const sycl::nd_item < 3 > & item_ct1 ) { 1306const block_q6_K * x = (const block_q6_K * )vx ; 1307 1308const int64_t i = item_ct1 .get_group (2 ); 1309#if QK_K == 256 1310 1311// assume 64 threads - this is very slightly better than the one below 1312const int64_t tid = item_ct1 .get_local_id (2 ); 1313const int64_t ip = tid /32 ;// ip is 0 or 1 1314const int64_t il = tid - 32 * ip ;// 0...32 1315const int64_t is = 8 * ip + il /16 ; 1316 1317dst_t * y = yy + i * QK_K + 128 * ip + il ; 1318 1319const float d = x [i ].d ; 1320 1321const uint8_t * ql = x [i ].ql + 64 * ip + il ; 1322const uint8_t qh = x [i ].qh [32 * ip + il ]; 1323const int8_t * sc = x [i ].scales + is ; 1324 1325y [0 ]= d * sc [0 ]* ((int8_t )((ql [0 ]& 0xF ) | (((qh >>0 )& 3 ) <<4 ))- 32 ); 1326y [32 ]= d * sc [2 ]* ((int8_t )((ql [32 ]& 0xF ) | (((qh >>2 )& 3 ) <<4 ))- 32 ); 1327y [64 ]= d * sc [4 ]* ((int8_t )((ql [0 ] >>4 ) | (((qh >>4 )& 3 ) <<4 ))- 32 ); 1328y [96 ]= d * sc [6 ]* ((int8_t )((ql [32 ] >>4 ) | (((qh >>6 )& 3 ) <<4 ))- 32 ); 1329#else 1330 1331// assume 32 threads 1332const int64_t tid = item_ct1 .get_local_id (2 ); 1333const int64_t ip = tid /16 ;// 0 or 1 1334const int64_t il = tid - 16 * ip ;// 0...15 1335 1336dst_t * y = yy + i * QK_K + 16 * ip + il ; 1337 1338const float d = x [i ].d ; 1339 1340const uint8_t ql = x [i ].ql [16 * ip + il ]; 1341const uint8_t qh = x [i ].qh [il ] >> (2 * ip ); 1342const int8_t * sc = x [i ].scales ; 1343 1344y [0 ]= d * sc [ip + 0 ]* ((int8_t )((ql & 0xF ) | (((qh >>0 )& 3 ) <<4 ))- 32 ); 1345y [32 ]= d * sc [ip + 2 ]* ((int8_t )((ql >>4 ) | (((qh >>4 )& 3 ) <<4 ))- 32 ); 1346#endif 1347} 1348 1349template < typename dst_t > 1350static void dequantize_block_q6_K_reorder (const void * __restrict__vx ,dst_t * __restrict__yy , 1351const sycl::nd_item < 3 > & item_ct1 ,int64_t n_blocks ) { 1352const int64_t ib = item_ct1 .get_group (2 ); 1353 1354const int64_t tid = item_ct1 .get_local_id (2 ); 1355const int64_t ip = tid /32 ;// ip is 0 or 1 1356const int64_t il = tid - 32 * ip ;// 0...32 1357const int64_t is = 8 * ip + il /16 ; 1358 1359const uint8_t * base_ptr = static_cast < const uint8_t *> (vx ); 1360const auto ql_offset = ib * (QK_K /2 ); 1361const auto qh_offset = (QK_K /2 )* n_blocks + (QK_K /4 )* ib ; 1362const auto base_scales_offset = (QK_K /2 )* n_blocks + (QK_K /4 )* n_blocks + (QK_K /16 )* ib ; 1363const auto base_d_offset = ((QK_K /2 )+ (QK_K /4 )+ (QK_K /16 ))* n_blocks ; 1364const uint8_t * ql_ptr = base_ptr + ql_offset ; 1365const uint8_t * qh_ptr = base_ptr + qh_offset ; 1366const uint8_t * scales_ptr = base_ptr + base_scales_offset ; 1367const ggml_half * d = (const ggml_half * ) (base_ptr + base_d_offset )+ ib ; 1368 1369dst_t * y = yy + ib * QK_K + 128 * ip + il ; 1370 1371const uint8_t * ql = ql_ptr + 64 * ip + il ; 1372const uint8_t qh = * (qh_ptr + 32 * ip + il ); 1373const int8_t * sc = reinterpret_cast < const int8_t *> (scales_ptr + is ); 1374 1375y [0 ]= * d * sc [0 ]* ((int8_t ) ((ql [0 ]& 0xF ) | (((qh >>0 )& 3 ) <<4 ))- 32 ); 1376y [32 ]= * d * sc [2 ]* ((int8_t ) ((ql [32 ]& 0xF ) | (((qh >>2 )& 3 ) <<4 ))- 32 ); 1377y [64 ]= * d * sc [4 ]* ((int8_t ) ((ql [0 ] >>4 ) | (((qh >>4 )& 3 ) <<4 ))- 32 ); 1378y [96 ]= * d * sc [6 ]* ((int8_t ) ((ql [32 ] >>4 ) | (((qh >>6 )& 3 ) <<4 ))- 32 ); 1379} 1380 1381template < typename dst_t > 1382static void dequantize_block_iq2_xxs (const void * __restrict__vx ,dst_t * __restrict__yy , 1383const sycl::nd_item < 3 > & item_ct1 , 1384const uint64_t * iq2xxs_grid_ptr , 1385const uint8_t * ksigns_iq2xs_ptr , 1386const uint8_t * kmask_iq2xs_ptr ) { 1387 1388const int64_t i = item_ct1 .get_group (2 ); 1389const block_iq2_xxs * x = (const block_iq2_xxs * )vx ; 1390 1391const int64_t tid = item_ct1 .get_local_id (2 ); 1392#if QK_K == 256 1393const int64_t il = tid /8 ;// 0...3 1394const int64_t ib = tid %8 ;// 0...7 1395dst_t * y = yy + i * QK_K + 32 * ib + 8 * il ; 1396const uint16_t * q2 = x [i ].qs + 4 * ib ; 1397const uint8_t * aux8 = (const uint8_t * )q2 ; 1398const uint8_t * grid = (const uint8_t * )(iq2xxs_grid_ptr + aux8 [il ]); 1399const uint32_t aux32 = q2 [2 ] | (q2 [3 ] <<16 ); 1400const float d = (float )x [i ].d * (0.5f + (aux32 >>28 ))* 0.25f ; 1401const uint8_t signs = ksigns_iq2xs_ptr [(aux32 >>7 * il )& 127 ]; 1402for (int j = 0 ;j < 8 ;++ j )y [j ]= d * grid [j ]* (signs & kmask_iq2xs_ptr [j ] ?-1.f :1.f ); 1403#else 1404assert (false); 1405#endif 1406 1407} 1408 1409template < typename dst_t > 1410static void dequantize_block_iq2_xs (const void * __restrict__vx ,dst_t * __restrict__yy , 1411const sycl::nd_item < 3 > & item_ct1 , 1412const uint64_t * iq2xs_grid , 1413const uint8_t * ksigns_iq2xs , 1414const uint8_t * kmask_iq2xs ) { 1415 1416const int64_t i = item_ct1 .get_group (2 ); 1417const block_iq2_xs * x = (const block_iq2_xs * )vx ; 1418 1419const int64_t tid = item_ct1 .get_local_id (2 ); 1420#if QK_K == 256 1421const int64_t il = tid /8 ;// 0...3 1422const int64_t ib = tid %8 ;// 0...7 1423dst_t * y = yy + i * QK_K + 32 * ib + 8 * il ; 1424const uint16_t * q2 = x [i ].qs + 4 * ib ; 1425const uint8_t * grid = (const uint8_t * )(iq2xs_grid + (q2 [il ]& 511 )); 1426const float d = (float )x [i ].d * (0.5f + ((x [i ].scales [ib ] >>4 * (il /2 ))& 0xf ))* 0.25f ; 1427const uint8_t signs = ksigns_iq2xs [q2 [il ] >>9 ]; 1428for (int j = 0 ;j < 8 ;++ j )y [j ]= d * grid [j ]* (signs & kmask_iq2xs [j ] ?-1.f :1.f ); 1429#else 1430assert (false); 1431#endif 1432 1433} 1434 1435template < typename dst_t > 1436__dpct_inline__ static void 1437dequantize_block_iq2_s (const void * __restrict__vx ,dst_t * __restrict__yy , 1438const sycl::nd_item < 3 > & item_ct1 ) { 1439 1440const int64_t i = item_ct1 .get_group (2 ); 1441const block_iq2_s * x = (const block_iq2_s * )vx ; 1442 1443const int64_t tid = item_ct1 .get_local_id (2 ); 1444#if QK_K == 256 1445const int64_t il = tid /8 ;// 0...3 1446const int64_t ib = tid %8 ;// 0...7 1447dst_t * y = yy + i * QK_K + 32 * ib + 8 * il ; 1448const uint8_t * grid = (const uint8_t * )(iq2s_grid + (x [i ].qs [4 * ib + il ] | ((x [i ].qh [ib ] << (8 - 2 * il ))& 0x300 ))); 1449const float d = (float )x [i ].d * (0.5f + ((x [i ].scales [ib ] >>4 * (il /2 ))& 0xf ))* 0.25f ; 1450const uint8_t signs = x [i ].qs [QK_K /8 + 4 * ib + il ]; 1451#pragma unroll 1452for (int j = 0 ;j < 8 ;++ j ) 1453y [j ]= d * grid [j ]* (signs & kmask_iq2xs [j ] ?-1.f :1.f ); 1454#else 1455assert (false); 1456 1457#endif 1458 1459} 1460 1461template < typename dst_t > 1462static void dequantize_block_iq3_xxs (const void * __restrict__vx ,dst_t * __restrict__yy , 1463const sycl::nd_item < 3 > & item_ct1 , 1464const uint32_t * iq3xxs_grid , 1465const uint8_t * ksigns_iq2xs , 1466const uint8_t * kmask_iq2xs ) { 1467 1468const int64_t i = item_ct1 .get_group (2 ); 1469const block_iq3_xxs * x = (const block_iq3_xxs * )vx ; 1470 1471const int64_t tid = item_ct1 .get_local_id (2 ); 1472#if QK_K == 256 1473const int64_t il = tid /8 ;// 0...3 1474const int64_t ib = tid %8 ;// 0...7 1475dst_t * y = yy + i * QK_K + 32 * ib + 8 * il ; 1476const uint8_t * q3 = x [i ].qs + 8 * ib ; 1477const uint16_t * gas = (const uint16_t * )(x [i ].qs + QK_K /4 )+ 2 * ib ; 1478const uint8_t * grid1 = (const uint8_t * )(iq3xxs_grid + q3 [2 * il + 0 ]); 1479const uint8_t * grid2 = (const uint8_t * )(iq3xxs_grid + q3 [2 * il + 1 ]); 1480const uint32_t aux32 = gas [0 ] | (gas [1 ] <<16 ); 1481const float d = (float )x [i ].d * (0.5f + (aux32 >>28 ))* 0.5f ; 1482const uint8_t signs = ksigns_iq2xs [(aux32 >>7 * il )& 127 ]; 1483for (int j = 0 ;j < 4 ;++ j ) { 1484y [j + 0 ]= d * grid1 [j ]* (signs & kmask_iq2xs [j + 0 ] ?-1.f :1.f ); 1485y [j + 4 ]= d * grid2 [j ]* (signs & kmask_iq2xs [j + 4 ] ?-1.f :1.f ); 1486 } 1487#else 1488assert (false); 1489#endif 1490 1491} 1492 1493template < typename dst_t > 1494__dpct_inline__ static void 1495dequantize_block_iq3_s (const void * __restrict__vx ,dst_t * __restrict__yy , 1496const sycl::nd_item < 3 > & item_ct1 , 1497const uint8_t * kmask_iq2xs ,const uint32_t * iq3s_grid ) { 1498 1499const int64_t i = item_ct1 .get_group (2 ); 1500const block_iq3_s * x = (const block_iq3_s * )vx ; 1501 1502const int64_t tid = item_ct1 .get_local_id (2 ); 1503#if QK_K == 256 1504const int64_t il = tid /8 ;// 0...3 1505const int64_t ib = tid %8 ;// 0...7 1506dst_t * y = yy + i * QK_K + 32 * ib + 8 * il ; 1507const uint8_t * qs = x [i ].qs + 8 * ib ; 1508const uint8_t * grid1 = (const uint8_t * )(iq3s_grid + (qs [2 * il + 0 ] | ((x [i ].qh [ib ] << (8 - 2 * il ))& 256 ))); 1509const uint8_t * grid2 = (const uint8_t * )(iq3s_grid + (qs [2 * il + 1 ] | ((x [i ].qh [ib ] << (7 - 2 * il ))& 256 ))); 1510const float d = (float )x [i ].d * (1 + 2 * ((x [i ].scales [ib /2 ] >>4 * (ib %2 ))& 0xf )); 1511const uint8_t signs = x [i ].signs [4 * ib + il ]; 1512#pragma unroll 1513for (int j = 0 ;j < 4 ;++ j ) { 1514y [j + 0 ]= d * grid1 [j ]* (signs & kmask_iq2xs [j + 0 ] ?-1.f :1.f ); 1515y [j + 4 ]= d * grid2 [j ]* (signs & kmask_iq2xs [j + 4 ] ?-1.f :1.f ); 1516 } 1517#else 1518assert (false); 1519#endif 1520 1521} 1522 1523template < typename dst_t > 1524__dpct_inline__ static void 1525dequantize_block_iq1_s (const void * __restrict__vx ,dst_t * __restrict__yy , 1526const sycl::nd_item < 3 > & item_ct1 , 1527const uint32_t * iq1s_grid_gpu ) { 1528 1529const int64_t i = item_ct1 .get_group (2 ); 1530const block_iq1_s * x = (const block_iq1_s * )vx ; 1531 1532const int64_t tid = item_ct1 .get_local_id (2 ); 1533#if QK_K == 256 1534const int64_t il = tid /8 ;// 0...3 1535const int64_t ib = tid %8 ;// 0...7 1536dst_t * y = yy + i * QK_K + 32 * ib + 8 * il ; 1537const float delta = x [i ].qh [ib ]& 0x8000 ?-1 - IQ1S_DELTA :-1 + IQ1S_DELTA ; 1538const float d = (float )x [i ].d * (2 * ((x [i ].qh [ib ] >>12 )& 7 )+ 1 ); 1539uint32_t grid32 [2 ];const int8_t * q = (const int8_t * )grid32 ; 1540grid32 [0 ]= iq1s_grid_gpu [x [i ].qs [4 * ib + il ] | (((x [i ].qh [ib ] >>3 * il )& 7 ) <<8 )]; 1541grid32 [1 ]= (grid32 [0 ] >>4 )& 0x0f0f0f0f ; 1542grid32 [0 ] &=0x0f0f0f0f ; 1543#pragma unroll 1544for (int j = 0 ;j < 8 ;++ j ) { 1545y [j ]= d * (q [j ]+ delta ); 1546 } 1547#else 1548assert (false); 1549#endif 1550 1551} 1552 1553template < typename dst_t > 1554__dpct_inline__ static void 1555dequantize_block_iq1_m (const void * __restrict__vx ,dst_t * __restrict__yy , 1556const sycl::nd_item < 3 > & item_ct1 , 1557const uint32_t * iq1s_grid_gpu ) { 1558 1559const int64_t i = item_ct1 .get_group (2 ); 1560const block_iq1_m * x = (const block_iq1_m * )vx ; 1561 1562const int64_t tid = item_ct1 .get_local_id (2 ); 1563#if QK_K == 256 1564const int64_t il = tid /8 ;// 0...3 1565const int64_t ib = tid %8 ;// 0...7 1566dst_t * y = yy + i * QK_K + 32 * ib + 8 * il ; 1567const uint16_t * sc = (const uint16_t * )x [i ].scales ; 1568iq1m_scale_t scale ; 1569scale .u16 = (sc [0 ] >>12 ) | ((sc [1 ] >>8 )& 0x00f0 ) | ((sc [2 ] >>4 )& 0x0f00 ) | (sc [3 ]& 0xf000 ); 1570const int ib16 = 2 * ib + il /2 ;// sc[ib16/4] >> 3*(ib16%4) -> sc[ib/2] >> 3*((2*ib+il/2)%4); 1571const float d = (float )scale .f16 * (2 * ((sc [ib16 /4 ] >>3 * (ib16 %4 ))& 0x7 )+ 1 ); 1572const float delta = x [i ].qh [2 * ib + il /2 ]& (0x08 <<4 * (il %2 )) ?-1 - IQ1M_DELTA :-1 + IQ1M_DELTA ; 1573uint32_t grid32 [2 ];const int8_t * q = (const int8_t * )grid32 ; 1574grid32 [0 ]= iq1s_grid_gpu [x [i ].qs [4 * ib + il ] | (((x [i ].qh [2 * ib + il /2 ] >>4 * (il %2 ))& 7 ) <<8 )]; 1575grid32 [1 ]= (grid32 [0 ] >>4 )& 0x0f0f0f0f ; 1576grid32 [0 ] &=0x0f0f0f0f ; 1577#pragma unroll 1578for (int j = 0 ;j < 8 ;++ j ) { 1579y [j ]= d * (q [j ]+ delta ); 1580 } 1581#else 1582assert (false); 1583#endif 1584 1585} 1586 1587template < typename dst_t > 1588__dpct_inline__ static void 1589dequantize_block_iq4_nl (const void * __restrict__vx ,dst_t * __restrict__yy , 1590const sycl::nd_item < 3 > & item_ct1 ) { 1591 1592const int64_t i = item_ct1 .get_group (2 ); 1593const block_iq4_nl * x = (const block_iq4_nl * )vx + i * (QK_K /QK4_NL ); 1594 1595const int64_t tid = item_ct1 .get_local_id (2 ); 1596const int64_t il = tid /8 ;// 0...3 1597const int64_t ib = tid %8 ;// 0...7 1598dst_t * y = yy + i * QK_K + 32 * ib + 4 * il ; 1599const uint8_t * q4 = x [ib ].qs + 4 * il ; 1600const float d = (float )x [ib ].d ; 1601#pragma unroll 1602for (int j = 0 ;j < 4 ;++ j ) { 1603y [j + 0 ]= d * kvalues_iq4nl [q4 [j ]& 0xf ]; 1604y [j + 16 ]= d * kvalues_iq4nl [q4 [j ] >>4 ]; 1605 } 1606 1607} 1608 1609 1610template < typename dst_t > 1611__dpct_inline__ static void 1612dequantize_block_iq4_xs (const void * __restrict__vx ,dst_t * __restrict__yy , 1613const sycl::nd_item < 3 > & item_ct1 ) { 1614const int64_t i = item_ct1 .get_group (2 ); 1615const block_iq4_xs * x = (const block_iq4_xs * )vx ; 1616 1617const int64_t tid = item_ct1 .get_local_id (2 ); 1618const int64_t il = tid /8 ;// 0...3 1619const int64_t ib = tid %8 ;// 0...7 1620dst_t * y = yy + i * QK_K + 32 * ib + 4 * il ; 1621const uint8_t * q4 = x [i ].qs + 16 * ib + 4 * il ; 1622const float d = (float )x [i ].d * ((((x [i ].scales_l [ib /2 ] >>4 * (ib %2 ))& 0xf ) | (((x [i ].scales_h >>2 * ib )& 3 ) <<4 ))- 32 ); 1623#pragma unroll 1624for (int j = 0 ;j < 4 ;++ j ) { 1625y [j + 0 ]= d * kvalues_iq4nl [q4 [j ]& 0xf ]; 1626y [j + 16 ]= d * kvalues_iq4nl [q4 [j ] >>4 ]; 1627 } 1628} 1629 1630template < typename dst_t > 1631static void dequantize_block_mxfp4 (const void * __restrict__vx ,dst_t * __restrict__yy , 1632const sycl::nd_item < 3 > & item_ct1 ) { 1633// auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item<3>(); 1634const int64_t i = item_ct1 .get_group (2 ); 1635const block_mxfp4 * x = (const block_mxfp4 * )vx + i * (QK_K /QK_MXFP4 ); 1636 1637const int64_t tid = item_ct1 .get_local_id (2 ); 1638const int64_t il = tid /8 ;// 0...3 1639const int64_t ib = tid %8 ;// 0...7 1640dst_t * y = yy + i * QK_K + 32 * ib + 4 * il ; 1641const uint8_t * q4 = x [ib ].qs + 4 * il ; 1642const float d = ggml_sycl_e8m0_to_fp32 (x [ib ].e ); 1643for (int j = 0 ;j < 4 ;++ j ) { 1644y [j + 0 ]= d * kvalues_mxfp4 [q4 [j ]& 0xf ]* 0.5f ; 1645y [j + 16 ]= d * kvalues_mxfp4 [q4 [j ] >>4 ]* 0.5f ; 1646 } 1647} 1648 1649 1650template < typename dst_t > 1651static void dequantize_block_nvfp4 ( 1652const void * __restrict__vx , 1653dst_t * __restrict__yy , 1654const int64_t ne ) { 1655auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item < 3 > (); 1656const int64_t i = item_ct1 .get_group (2 ); 1657const int tid = item_ct1 .get_local_id (2 ); 1658 1659const int64_t base = i * QK_NVFP4 ; 1660if (base >=ne ) { 1661return ; 1662 } 1663 1664const block_nvfp4 * x = (const block_nvfp4 * )vx ; 1665const block_nvfp4 & xb = x [i ]; 1666 1667const int sub = tid / (QK_NVFP4_SUB /2 ); 1668const int j = tid % (QK_NVFP4_SUB /2 ); 1669 1670const float d = ggml_sycl_ue4m3_to_fp32 (xb .d [sub ]); 1671const uint8_t q = xb .qs [sub * (QK_NVFP4_SUB /2 )+ j ]; 1672 1673const int64_t y0 = base + sub * QK_NVFP4_SUB + j ; 1674const int64_t y1 = y0 + QK_NVFP4_SUB /2 ; 1675 1676yy [y0 ]= ggml_sycl_cast < dst_t > (d * kvalues_mxfp4 [q & 0x0F ]); 1677yy [y1 ]= ggml_sycl_cast < dst_t > (d * kvalues_mxfp4 [q >>4 ]); 1678} 1679 1680 1681#endif // GGML_SYCL_DEQUANTIZE_HPP