char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

Sigbjørn Skjæretsycl : add Q2_K reordered MMVQ and ESIMD kernels (again) (#27490)867229003

char/aliceai-t5-moe
58.6 KiB1681 linesraw
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,
21                                            const 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,
29                                            const int iqs, dfloat2 &v) {
30    const block_q2_0 * x = (const block_q2_0 *) vx;
31
32    const dfloat d = x[ib].d;
33
34    const int byte_idx = iqs / 4;
35    const int shift = (iqs % 4) * 2;
36    const uint8_t vui = x[ib].qs[byte_idx];
37
38    v.x() = (vui >> shift) & 3;
39    v.y() = (vui >> (shift + 2)) & 3;
40
41#ifdef GGML_SYCL_F16
42    v.s0() = ((dfloat)v.s0() - 1.0f) * d;
43    v.s1() = ((dfloat)v.s1() - 1.0f) * d;
44#else
45    v.x() = ((dfloat)v.x() - 1.0f) * d;
46    v.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,
51                                            const int iqs, dfloat2 &v) {
52    const block_q4_0 * x = (const block_q4_0 *) vx;
53
54    const dfloat d = x[ib].d;
55
56    const int vui = x[ib].qs[iqs];
57
58    v.x() = vui & 0xF;
59    v.y() = vui >> 4;
60
61#ifdef GGML_SYCL_F16
62    // v = v - {8.0f, 8.0f};
63    // v = v * {d, d};
64    v.s0() = (v.s0() - 8.0f) * d;
65    v.s1() = (v.s1() - 8.0f) * d;
66
67#else
68    v.x() = (v.x() - 8.0f) * d;
69    v.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,
74                                            const int iqs, dfloat2 &v) {
75    // const block_q4_0 * x = (const block_q4_0 *) vx;
76
77    const dfloat d = (const dfloat)*((const sycl::half*)d_ptr+ib);
78
79    const int vui = *((const uint8_t *)qs+iqs);
80
81    v.x() = vui & 0xF;
82    v.y() = vui >> 4;
83
84#ifdef GGML_SYCL_F16
85    // v = v - {8.0f, 8.0f};
86    // v = v * {d, d};
87    v.s0() = (v.s0() - 8.0f) * d;
88    v.s1() = (v.s1() - 8.0f) * d;
89
90#else
91    v.x() = (v.x() - 8.0f) * d;
92    v.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,
97                                            const int iqs, dfloat2 &v) {
98    // Q1_0 reorder layout: scale values followed by quantized bits
99    const dfloat d = (const dfloat)*((const sycl::half*)d_ptr+ib);
100
101    const int bit_index_0 = iqs + 0;
102    const int bit_index_1 = iqs + 1;
103
104    const int bit_0 = (*((const uint8_t *)qs + bit_index_0 / 8) >> (bit_index_0 % 8)) & 1;
105    const int bit_1 = (*((const uint8_t *)qs + bit_index_1 / 8) >> (bit_index_1 % 8)) & 1;
106
107    v.x() = (2 * bit_0 - 1) * d;
108    v.y() = (2 * bit_1 - 1) * d;
109}
110
111static __dpct_inline__ void dequantize_q1_0(const void *vx, const int64_t ib,
112                                            const int iqs, dfloat2 &v) {
113    const block_q1_0 * x = (const block_q1_0 *) vx;
114    const dfloat d = x[ib].d;
115
116    const int bit_index_0 = iqs + 0;
117    const int bit_index_1 = iqs + 1;
118
119    const int bit_0 = (x[ib].qs[bit_index_0 / 8] >> (bit_index_0 % 8)) & 1;
120    const int bit_1 = (x[ib].qs[bit_index_1 / 8] >> (bit_index_1 % 8)) & 1;
121
122    v.x() = (2 * bit_0 - 1) * d;
123    v.y() = (2 * bit_1 - 1) * d;
124}
125
126static __dpct_inline__ void dequantize_q4_1(const void *vx, const int64_t ib,
127                                            const int iqs, dfloat2 &v) {
128    const block_q4_1 * x = (const block_q4_1 *) vx;
129
130    const dfloat d = x[ib].dm[0];
131    const dfloat m = x[ib].dm[1];
132
133    const int vui = x[ib].qs[iqs];
134
135    v.x() = vui & 0xF;
136    v.y() = vui >> 4;
137
138#ifdef GGML_SYCL_F16
139    // v = v * {d, d};
140    // v = v + {m, m};
141    v.s0() = sycl::fma(v.s0(), d, m);
142    v.s1() = sycl::fma(v.s1(), d, m);
143
144#else
145    v.x() = sycl::fma(v.x(), d, m);
146    v.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,
151                                            const int iqs, dfloat2 &v) {
152#if QK_K == 256
153    const block_q4_K * x = (const block_q4_K *) vx;
154    const sycl::half2 dm = x[ib].dm;
155    const float dall = dm[0];
156    const float dmin = dm[1];
157
158    auto dequantize_one = [&](const int idx) -> dfloat {
159        const int il = idx / 64;
160        const int in = idx % 64;
161        const int is = 2 * il + (in >= 32 ? 1 : 0);
162        const int off = in & 31;
163        const int qsi = 32 * il + off;
164
165        uint8_t sc;
166        uint8_t m;
167        get_scale_min_k4(is, x[ib].scales, sc, m);
168
169        const uint8_t q = x[ib].qs[qsi];
170        const uint8_t qv = (in >= 32) ? (q >> 4) : (q & 0xF);
171        return sycl::fma((dfloat) qv, (dfloat) (dall * sc), (dfloat) (-dmin * m));
172    };
173
174    v.x() = dequantize_one(iqs + 0);
175    v.y() = dequantize_one(iqs + 1);
176#else
177    GGML_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,
182                                                const int iqs, float &v0, float &v1) {
183#if QK_K == 256
184    const block_q4_K * x = (const block_q4_K *) vx;
185    const sycl::half2 dm = x[ib].dm;
186    const float dall = dm[0];
187    const float dmin = dm[1];
188
189    auto dequantize_one = [&](const int idx) -> float {
190        const int il = idx / 64;
191        const int in = idx % 64;
192        const int is = 2 * il + (in >= 32 ? 1 : 0);
193        const int qsi = 32 * il + (in & 31);
194
195        uint8_t sc;
196        uint8_t m;
197        get_scale_min_k4(is, x[ib].scales, sc, m);
198
199        const float d = dall * sc;
200        const float mn = dmin * m;
201        const uint8_t q = x[ib].qs[qsi];
202        const uint8_t qv = (in >= 32) ? (q >> 4) : (q & 0xF);
203
204        return d * qv - mn;
205    };
206
207    v0 = dequantize_one(iqs + 0);
208    v1 = dequantize_one(iqs + 1);
209#else
210    GGML_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,
215                                            const int iqs, dfloat2 &v) {
216#if QK_K == 256
217    const block_q2_K * x = (const block_q2_K *) vx;
218    const float dall = x[ib].dm[0];
219    const float dmin = x[ib].dm[1];
220
221    auto dequantize_one = [&](const int idx) -> dfloat {
222        const int n = idx / 128;
223        const int r = idx % 128;
224        const int g = r / 32;
225        const int l = r % 32;
226        const int is = 8 * n + l / 16;
227
228        const uint8_t q = x[ib].qs[32 * n + l];
229        const uint8_t sc = x[ib].scales[is + 2 * g];
230        const float d = dall * (sc & 0xF);
231        const float m = dmin * (sc >> 4);
232
233        return (dfloat) d * (dfloat) ((q >> (2 * g)) & 3) - (dfloat) m;
234    };
235
236    v.x() = dequantize_one(iqs + 0);
237    v.y() = dequantize_one(iqs + 1);
238#else
239    GGML_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,
244                                                const int iqs, float &v0, float &v1) {
245#if QK_K == 256
246    const block_q2_K * x = (const block_q2_K *) vx;
247    const float dall = x[ib].dm[0];
248    const float dmin = x[ib].dm[1];
249
250    auto dequantize_one = [&](const int idx) -> float {
251        const int n = idx / 128;
252        const int r = idx % 128;
253        const int g = r / 32;
254        const int l = r % 32;
255        const int is = 8 * n + l / 16;
256
257        const uint8_t q = x[ib].qs[32 * n + l];
258        const uint8_t sc = x[ib].scales[is + 2 * g];
259        const float d = dall * (sc & 0xF);
260        const float m = dmin * (sc >> 4);
261
262        return d * ((q >> (2 * g)) & 3) - m;
263    };
264
265    v0 = dequantize_one(iqs + 0);
266    v1 = dequantize_one(iqs + 1);
267#else
268    GGML_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,
273                                            const int iqs, dfloat2 &v) {
274#if QK_K == 256
275    const block_q3_K * x = (const block_q3_K *) vx;
276    const float d_all = x[ib].d;
277
278    auto dequantize_one = [&](const int idx) -> dfloat {
279        const int n = idx / 128;
280        const int r = idx % 128;
281        const int j = r / 32;
282        const int l = r % 32;
283
284        const int is0 = l / 16;
285        const int is = 8 * n + 2 * j + is0;
286        const int shift = 2 * j;
287        const uint8_t m = 1 << (4 * n + j);
288
289        const int8_t us = is <  4 ? (x[ib].scales[is - 0] & 0xF) | (((x[ib].scales[is + 8] >> 0) & 3) << 4) :
290                         is <  8 ? (x[ib].scales[is - 0] & 0xF) | (((x[ib].scales[is + 4] >> 2) & 3) << 4) :
291                         is < 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
294        const float dl = d_all * (us - 32);
295        const uint8_t q = x[ib].qs[32 * n + l];
296        const uint8_t h = x[ib].hmask[l];
297        const int8_t qv = ((q >> shift) & 3) - ((h & m) ? 0 : 4);
298
299        return (dfloat) (dl * qv);
300    };
301
302    v.x() = dequantize_one(iqs + 0);
303    v.y() = dequantize_one(iqs + 1);
304#else
305    GGML_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,
310                                            const int iqs, dfloat2 &v) {
311#if QK_K == 256
312    const block_q5_K * x = (const block_q5_K *) vx;
313    const float dall = x[ib].dm[0];
314    const float dmin = x[ib].dm[1];
315
316    auto dequantize_one = [&](const int idx) -> dfloat {
317        const int il = idx / 64;
318        const int in = idx % 64;
319        const int is = 2 * il + (in >= 32 ? 1 : 0);
320        const int ir = (in & 31) / 2;
321        const int iq = in & 1;
322
323        const uint8_t q = x[ib].qs[32 * il + 2 * ir + iq];
324        const uint8_t h = x[ib].qh[2 * ir + iq];
325        const uint8_t qv = (in >= 32) ? (q >> 4) : (q & 0xF);
326
327        uint8_t sc;
328        uint8_t m;
329        get_scale_min_k4(is, x[ib].scales, sc, m);
330
331        const float d = dall * sc;
332        const float mn = dmin * m;
333        const uint8_t hm = 1 << (2 * il + (in >= 32 ? 1 : 0));
334
335        return sycl::fma((dfloat) (qv + ((h & hm) ? 16 : 0)), (dfloat) d, (dfloat) (-mn));
336    };
337
338    v.x() = dequantize_one(iqs + 0);
339    v.y() = dequantize_one(iqs + 1);
340#else
341    GGML_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,
346                                                const int iqs, float &v0, float &v1) {
347#if QK_K == 256
348    const block_q5_K * x = (const block_q5_K *) vx;
349    const float dall = x[ib].dm[0];
350    const float dmin = x[ib].dm[1];
351
352    auto dequantize_one = [&](const int idx) -> float {
353        const int il = idx / 64;
354        const int in = idx % 64;
355        const int is = 2 * il + (in >= 32 ? 1 : 0);
356        const int ir = (in & 31) / 2;
357        const int iq = in & 1;
358
359        const uint8_t q = x[ib].qs[32 * il + 2 * ir + iq];
360        const uint8_t h = x[ib].qh[2 * ir + iq];
361        const uint8_t qv = (in >= 32) ? (q >> 4) : (q & 0xF);
362
363        uint8_t sc;
364        uint8_t m;
365        get_scale_min_k4(is, x[ib].scales, sc, m);
366
367        const float d = dall * sc;
368        const float mn = dmin * m;
369        const uint8_t hm = 1 << (2 * il + (in >= 32 ? 1 : 0));
370
371        return (qv + ((h & hm) ? 16 : 0)) * d - mn;
372    };
373
374    v0 = dequantize_one(iqs + 0);
375    v1 = dequantize_one(iqs + 1);
376#else
377    GGML_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,
382                                            const int iqs, dfloat2 &v) {
383#if QK_K == 256
384    const block_q6_K * x = (const block_q6_K *) vx;
385    const float d = x[ib].d;
386
387    auto dequantize_one = [&](const int idx) -> dfloat {
388        const int ip = idx / 128;
389        const int in = idx % 128;
390        const int il = in & 31;
391        const int ig = in / 32;
392        const int is = 8 * ip + il / 16;
393
394        const uint8_t ql0 = x[ib].ql[64 * ip + il];
395        const uint8_t ql1 = x[ib].ql[64 * ip + il + 32];
396        const uint8_t qh = x[ib].qh[32 * ip + il];
397        const int8_t * sc = x[ib].scales + is;
398
399        uint8_t qv;
400        int8_t scale;
401        if (ig == 0) {
402            qv = (ql0 & 0xF) | (((qh >> 0) & 3) << 4);
403            scale = sc[0];
404        } else if (ig == 1) {
405            qv = (ql1 & 0xF) | (((qh >> 2) & 3) << 4);
406            scale = sc[2];
407        } else if (ig == 2) {
408            qv = (ql0 >> 4) | (((qh >> 4) & 3) << 4);
409            scale = sc[4];
410        } else {
411            qv = (ql1 >> 4) | (((qh >> 6) & 3) << 4);
412            scale = sc[6];
413        }
414
415        return (dfloat) (d * scale * ((int8_t) qv - 32));
416    };
417
418    v.x() = dequantize_one(iqs + 0);
419    v.y() = dequantize_one(iqs + 1);
420#else
421    GGML_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,
426                                             const int iqs, dfloat2 &v) {
427    const block_mxfp4 * x = (const block_mxfp4 *) vx;
428    const float d = ggml_sycl_e8m0_to_fp32(x[ib].e);
429    const uint8_t q = x[ib].qs[iqs];
430
431    v.x() = d * kvalues_mxfp4[q & 0xF] * 0.5f;
432    v.y() = d * kvalues_mxfp4[q >> 4] * 0.5f;
433}
434
435static __dpct_inline__ void dequantize_nvfp4(const void *vx, const int64_t ib,
436                                             const int iqs, dfloat2 &v) {
437    const block_nvfp4 & xb = ((const block_nvfp4 *) vx)[ib];
438
439    auto dequantize_one = [&](const int idx) -> dfloat {
440        const int sub = idx / QK_NVFP4_SUB;
441        const int j = idx % QK_NVFP4_SUB;
442        const int jh = j % (QK_NVFP4_SUB / 2);
443
444        const float d = ggml_sycl_ue4m3_to_fp32(xb.d[sub]);
445        const uint8_t q = xb.qs[sub * (QK_NVFP4_SUB / 2) + jh];
446        const uint8_t qv = (j < (QK_NVFP4_SUB / 2)) ? (q & 0x0F) : (q >> 4);
447
448        return d * kvalues_mxfp4[qv];
449    };
450
451    v.x() = dequantize_one(iqs + 0);
452    v.y() = dequantize_one(iqs + 1);
453}
454
455static __dpct_inline__ void dequantize_iq2_xxs(const void *vx, const int64_t ib,
456                                               const int iqs, dfloat2 &v) {
457#if QK_K == 256
458    const block_iq2_xxs * x = (const block_iq2_xxs *) vx;
459
460    auto dequantize_one = [&](const int idx) -> dfloat {
461        const int ib8 = idx / 32;
462        const int r = idx % 32;
463        const int il = r / 8;
464        const int j = r % 8;
465
466        const uint16_t * q2 = x[ib].qs + 4 * ib8;
467        const uint8_t * aux8 = (const uint8_t *) q2;
468        const uint8_t * grid = (const uint8_t *) (iq2xxs_grid + aux8[il]);
469        const uint32_t aux32 = q2[2] | (q2[3] << 16);
470        const float d = (float) x[ib].d * (0.5f + (aux32 >> 28)) * 0.25f;
471        const uint8_t signs = ksigns_iq2xs[(aux32 >> (7 * il)) & 127];
472
473        return d * grid[j] * ((signs & kmask_iq2xs[j]) ? -1.f : 1.f);
474    };
475
476    v.x() = dequantize_one(iqs + 0);
477    v.y() = dequantize_one(iqs + 1);
478#else
479    GGML_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,
484                                              const int iqs, dfloat2 &v) {
485#if QK_K == 256
486    const block_iq2_xs * x = (const block_iq2_xs *) vx;
487
488    auto dequantize_one = [&](const int idx) -> dfloat {
489        const int ib8 = idx / 32;
490        const int r = idx % 32;
491        const int il = r / 8;
492        const int j = r % 8;
493
494        const uint16_t * q2 = x[ib].qs + 4 * ib8;
495        const uint8_t * grid = (const uint8_t *) (iq2xs_grid + (q2[il] & 511));
496        const float d = (float) x[ib].d * (0.5f + ((x[ib].scales[ib8] >> (4 * (il / 2))) & 0xf)) * 0.25f;
497        const uint8_t signs = ksigns_iq2xs[q2[il] >> 9];
498
499        return d * grid[j] * ((signs & kmask_iq2xs[j]) ? -1.f : 1.f);
500    };
501
502    v.x() = dequantize_one(iqs + 0);
503    v.y() = dequantize_one(iqs + 1);
504#else
505    GGML_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,
510                                             const int iqs, dfloat2 &v) {
511#if QK_K == 256
512    const block_iq2_s * x = (const block_iq2_s *) vx;
513
514    auto dequantize_one = [&](const int idx) -> dfloat {
515        const int ib8 = idx / 32;
516        const int r = idx % 32;
517        const int il = r / 8;
518        const int j = r % 8;
519
520        const uint16_t grid_id = x[ib].qs[4 * ib8 + il] | ((x[ib].qh[ib8] << (8 - 2 * il)) & 0x300);
521        const uint8_t * grid = (const uint8_t *) (iq2s_grid + grid_id);
522        const float d = (float) x[ib].d * (0.5f + ((x[ib].scales[ib8] >> (4 * (il / 2))) & 0xf)) * 0.25f;
523        const uint8_t signs = x[ib].qs[QK_K / 8 + 4 * ib8 + il];
524
525        return d * grid[j] * ((signs & kmask_iq2xs[j]) ? -1.f : 1.f);
526    };
527
528    v.x() = dequantize_one(iqs + 0);
529    v.y() = dequantize_one(iqs + 1);
530#else
531    GGML_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,
536                                               const int iqs, dfloat2 &v) {
537#if QK_K == 256
538    const block_iq3_xxs * x = (const block_iq3_xxs *) vx;
539
540    auto dequantize_one = [&](const int idx) -> dfloat {
541        const int ib8 = idx / 32;
542        const int r = idx % 32;
543        const int il = r / 8;
544        const int j = r % 8;
545
546        const uint8_t * q3 = x[ib].qs + 8 * ib8;
547        const uint16_t * gas = (const uint16_t *) (x[ib].qs + QK_K / 4) + 2 * ib8;
548        const uint8_t * grid1 = (const uint8_t *) (iq3xxs_grid + q3[2 * il + 0]);
549        const uint8_t * grid2 = (const uint8_t *) (iq3xxs_grid + q3[2 * il + 1]);
550        const uint32_t aux32 = gas[0] | (gas[1] << 16);
551        const float d = (float) x[ib].d * (0.5f + (aux32 >> 28)) * 0.5f;
552        const uint8_t signs = ksigns_iq2xs[(aux32 >> (7 * il)) & 127];
553
554        if (j < 4) {
555            return d * grid1[j] * ((signs & kmask_iq2xs[j + 0]) ? -1.f : 1.f);
556        }
557        return d * grid2[j - 4] * ((signs & kmask_iq2xs[j + 0]) ? -1.f : 1.f);
558    };
559
560    v.x() = dequantize_one(iqs + 0);
561    v.y() = dequantize_one(iqs + 1);
562#else
563    GGML_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,
568                                             const int iqs, dfloat2 &v) {
569#if QK_K == 256
570    const block_iq3_s * x = (const block_iq3_s *) vx;
571
572    auto dequantize_one = [&](const int idx) -> dfloat {
573        const int ib8 = idx / 32;
574        const int r = idx % 32;
575        const int il = r / 8;
576        const int j = r % 8;
577
578        const uint8_t * qs = x[ib].qs + 8 * ib8;
579        const uint16_t grid1_id = qs[2 * il + 0] | ((x[ib].qh[ib8] << (8 - 2 * il)) & 256);
580        const uint16_t grid2_id = qs[2 * il + 1] | ((x[ib].qh[ib8] << (7 - 2 * il)) & 256);
581        const uint8_t * grid1 = (const uint8_t *) (iq3s_grid + grid1_id);
582        const uint8_t * grid2 = (const uint8_t *) (iq3s_grid + grid2_id);
583        const float d = (float) x[ib].d * (1 + 2 * ((x[ib].scales[ib8 / 2] >> (4 * (ib8 % 2))) & 0xf));
584        const uint8_t signs = x[ib].signs[4 * ib8 + il];
585
586        if (j < 4) {
587            return d * grid1[j] * ((signs & kmask_iq2xs[j + 0]) ? -1.f : 1.f);
588        }
589        return d * grid2[j - 4] * ((signs & kmask_iq2xs[j + 0]) ? -1.f : 1.f);
590    };
591
592    v.x() = dequantize_one(iqs + 0);
593    v.y() = dequantize_one(iqs + 1);
594#else
595    GGML_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,
600                                             const int iqs, dfloat2 &v) {
601#if QK_K == 256
602    const block_iq1_s * x = (const block_iq1_s *) vx;
603
604    auto dequantize_one = [&](const int idx) -> dfloat {
605        const int ib8 = idx / 32;
606        const int r = idx % 32;
607        const int il = r / 8;
608        const int j = r % 8;
609
610        const float delta = (x[ib].qh[ib8] & 0x8000) ? (-1.f - IQ1S_DELTA) : (-1.f + IQ1S_DELTA);
611        const float d = (float) x[ib].d * (2 * ((x[ib].qh[ib8] >> 12) & 7) + 1);
612        const uint16_t grid_id = x[ib].qs[4 * ib8 + il] | (((x[ib].qh[ib8] >> (3 * il)) & 7) << 8);
613        const uint32_t g = iq1s_grid_gpu[grid_id];
614        const int8_t qv = (j < 4) ? ((g >> (8 * j)) & 0x0F) : ((g >> (8 * (j - 4) + 4)) & 0x0F);
615
616        return d * (qv + delta);
617    };
618
619    v.x() = dequantize_one(iqs + 0);
620    v.y() = dequantize_one(iqs + 1);
621#else
622    GGML_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,
627                                             const int iqs, dfloat2 &v) {
628#if QK_K == 256
629    const block_iq1_m * x = (const block_iq1_m *) vx;
630
631    auto dequantize_one = [&](const int idx) -> dfloat {
632        const int ib8 = idx / 32;
633        const int r = idx % 32;
634        const int il = r / 8;
635        const int j = r % 8;
636
637        const uint16_t * sc = (const uint16_t *) x[ib].scales;
638        iq1m_scale_t scale;
639        scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
640
641        const int ib16 = 2 * ib8 + il / 2;
642        const float d = (float) scale.f16 * (2 * ((sc[ib16 / 4] >> (3 * (ib16 % 4))) & 0x7) + 1);
643
644        const uint8_t qh = x[ib].qh[2 * ib8 + il / 2];
645        const float delta = (qh & (0x08 << (4 * (il % 2)))) ? (-1.f - IQ1M_DELTA) : (-1.f + IQ1M_DELTA);
646
647        const uint16_t grid_id = x[ib].qs[4 * ib8 + il] | (((qh >> (4 * (il % 2))) & 7) << 8);
648        const uint32_t g = iq1s_grid_gpu[grid_id];
649        const int8_t qv = (j < 4) ? ((g >> (8 * j)) & 0x0F) : ((g >> (8 * (j - 4) + 4)) & 0x0F);
650
651        return d * (qv + delta);
652    };
653
654    v.x() = dequantize_one(iqs + 0);
655    v.y() = dequantize_one(iqs + 1);
656#else
657    GGML_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,
662                                              const int iqs, dfloat2 &v) {
663    const block_iq4_nl * x = (const block_iq4_nl *) vx;
664    const float d = (float) x[ib].d;
665
666    auto dequantize_one = [&](const int idx) -> dfloat {
667        if (idx < 16) {
668            return d * kvalues_iq4nl[x[ib].qs[idx] & 0xF];
669        }
670        return d * kvalues_iq4nl[x[ib].qs[idx - 16] >> 4];
671    };
672
673    v.x() = dequantize_one(iqs + 0);
674    v.y() = dequantize_one(iqs + 1);
675}
676
677static __dpct_inline__ void dequantize_iq4_xs(const void *vx, const int64_t ib,
678                                              const int iqs, dfloat2 &v) {
679#if QK_K == 256
680    const block_iq4_xs * x = (const block_iq4_xs *) vx;
681
682    auto dequantize_one = [&](const int idx) -> dfloat {
683        const int ib8 = idx / 32;
684        const int r = idx % 32;
685        const int byte_idx = (r < 16) ? r : (r - 16);
686        const uint8_t q = x[ib].qs[16 * ib8 + byte_idx];
687        const uint8_t qv = (r < 16) ? (q & 0x0F) : (q >> 4);
688
689        const 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);
691        return d * kvalues_iq4nl[qv];
692    };
693
694    v.x() = dequantize_one(iqs + 0);
695    v.y() = dequantize_one(iqs + 1);
696#else
697    GGML_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,
702                                            const int iqs, dfloat2 &v) {
703    const block_q5_0 * x = (const block_q5_0 *) vx;
704
705    const dfloat d = x[ib].d;
706
707    uint32_t qh;
708    memcpy(&qh, x[ib].qh, sizeof(qh));
709
710    const int xh_0 = ((qh >> (iqs +  0)) << 4) & 0x10;
711    const int xh_1 = ((qh >> (iqs + 12))     ) & 0x10;
712
713    v.x() = ((x[ib].qs[iqs] & 0xf) | xh_0);
714    v.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};
719    v.s0() = (v.s0() - 16.0f) * d;
720    v.s1() = (v.s1() - 16.0f) * d;
721
722#else
723    v.x() = (v.x() - 16.0f) * d;
724    v.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,
729                                            const int iqs, dfloat2 &v) {
730    const block_q5_1 * x = (const block_q5_1 *) vx;
731
732    const dfloat d = x[ib].dm[0];
733    const dfloat m = x[ib].dm[1];
734
735    uint32_t qh;
736    memcpy(&qh, x[ib].qh, sizeof(qh));
737
738    const int xh_0 = ((qh >> (iqs +  0)) << 4) & 0x10;
739    const int xh_1 = ((qh >> (iqs + 12))     ) & 0x10;
740
741    v.x() = ((x[ib].qs[iqs] & 0xf) | xh_0);
742    v.y() = ((x[ib].qs[iqs] >> 4) | xh_1);
743
744#ifdef GGML_SYCL_F16
745    // v = v * {d, d};
746    // v = v + {m, m};
747    v.s0() = sycl::fma(v.s0(), d, m);
748    v.s1() = sycl::fma(v.s1(), d, m);
749#else
750    v.x() = sycl::fma(v.x(), d, m);
751    v.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,
756                                            const int iqs, dfloat2 &v) {
757    const dfloat d = (const dfloat)*((const sycl::half*)d_ptr + ib);
758
759    v.x() = ((const int8_t *)qs)[iqs + 0];
760    v.y() = ((const int8_t *)qs)[iqs + 1];
761
762#ifdef GGML_SYCL_F16
763    v.s0() *= d;
764    v.s1() *= d;
765#else
766    v.x() *= d;
767    v.y() *= d;
768#endif // GGML_SYCL_F16
769}
770
771static __dpct_inline__ void dequantize_q8_0(const void *vx, const int64_t ib,
772                                            const int iqs, dfloat2 &v) {
773    const block_q8_0 * x = (const block_q8_0 *) vx;
774
775    const dfloat d = x[ib].d;
776
777    v.x() = x[ib].qs[iqs + 0];
778    v.y() = x[ib].qs[iqs + 1];
779
780#ifdef GGML_SYCL_F16
781    // v = v * {d, d};
782    v.s0() *= d;
783    v.s1() *= d;
784#else
785    v.x() *= d;
786    v.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,
792                                  const sycl::nd_item<3> &item_ct1) {
793
794    const int64_t i = item_ct1.get_group(2);
795
796    // assume 32 threads
797    const int64_t tid = item_ct1.get_local_id(2);
798    const int64_t il  = tid/8;
799    const int64_t ir  = tid%8;
800    const int64_t ib = 8*i + ir;
801    if (ib >= nb32) {
802        return;
803    }
804
805    dst_t * y = yy + 256*i + 32*ir + 4*il;
806
807    const block_q4_0 * x = (const block_q4_0 *)vx + ib;
808    const float d = sycl::vec<sycl::half, 1>(x->d)
809                        .convert<float, sycl::rounding_mode::automatic>()[0];
810    const float dm = -8*d;
811
812    const uint8_t * q = x->qs + 4*il;
813
814    for (int l = 0; l < 4; ++l) {
815        y[l+ 0] = d * (q[l] & 0xF) + dm;
816        y[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,
822                                  const sycl::nd_item<3> &item_ct1) {
823
824    const int64_t i = item_ct1.get_group(2);
825    auto k=nb32;
826    // assume 32 threads
827    const int64_t tid = item_ct1.get_local_id(2);
828    const int lane_ib = i * WARP_SIZE + tid;
829
830    if (lane_ib >= k / QK4_0) {
831        return;
832    }
833
834    dst_t * y_ptr = yy + lane_ib * QK4_0;
835
836    auto qs = (const uint8_t*)vx + lane_ib * QK4_0 / 2;
837    auto s_ptr = (const sycl::half*)((const uint8_t*)vx + k / 2) + lane_ib;
838
839    const float d = float(*s_ptr);
840
841#pragma unroll
842    for (int l = 0; l < QK4_0 / 2; ++l) {
843        int vq = qs[l];
844        y_ptr[l + 0] = d * ((vq & 0xF) - 8);
845        y_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,
854                                  const sycl::nd_item<3> &item_ct1) {
855
856    const int64_t i = item_ct1.get_group(2);
857    const int64_t tid = item_ct1.get_local_id(2);
858    const int lane_ib = i * WARP_SIZE + tid;
859
860    if (lane_ib >= k / QK8_0) {
861        return;
862    }
863
864    dst_t * y_ptr = yy + lane_ib * QK8_0;
865
866    auto qs = (const int8_t*)vx + lane_ib * QK8_0;
867    auto s_ptr = (const sycl::half*)((const uint8_t*)vx + k) + lane_ib;
868
869    const float d = float(*s_ptr);
870
871#pragma unroll
872    for (int l = 0; l < QK8_0; ++l) {
873        y_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,
880                                  const sycl::nd_item<3> &item_ct1) {
881
882    const int64_t i = item_ct1.get_group(2);
883
884    // assume 32 threads
885    const int64_t tid = item_ct1.get_local_id(2);
886    const int64_t il  = tid/8;
887    const int64_t ir  = tid%8;
888    const int64_t ib = 8*i + ir;
889    if (ib >= nb32) {
890        return;
891    }
892
893    dst_t * y = yy + 256*i + 32*ir + 4*il;
894
895    const block_q4_1 * x = (const block_q4_1 *)vx + ib;
896    const sycl::float2 d =
897        x->dm.convert<float, sycl::rounding_mode::automatic>();
898
899    const uint8_t * q = x->qs + 4*il;
900
901    for (int l = 0; l < 4; ++l) {
902        y[l + 0] = d.x() * (q[l] & 0xF) + d.y();
903        y[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,
912                                  const sycl::nd_item<3> &item_ct1) {
913
914    const int64_t i = item_ct1.get_group(2);
915    const block_q2_K * x = (const block_q2_K *) vx;
916
917    const int64_t tid = item_ct1.get_local_id(2);
918#if QK_K == 256
919    const int64_t n   = tid/32;
920    const int64_t l   = tid - 32*n;
921    const int64_t is  = 8*n + l/16;
922
923    const uint8_t q = x[i].qs[32*n + l];
924    dst_t * y = yy + i*QK_K + 128*n;
925
926    float dall = x[i].dm[0];
927    float dmin = x[i].dm[1];
928    y[l+ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
929    y[l+32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 2) & 3) - dmin * (x[i].scales[is+2] >> 4);
930    y[l+64] = dall * (x[i].scales[is+4] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+4] >> 4);
931    y[l+96] = dall * (x[i].scales[is+6] & 0xF) * ((q >> 6) & 3) - dmin * (x[i].scales[is+6] >> 4);
932#else
933    const int64_t is = tid/16;  // 0 or 1
934    const int64_t il = tid%16;  // 0...15
935    const uint8_t q = x[i].qs[il] >> (2*is);
936    dst_t * y = yy + i*QK_K + 16*is + il;
937
938    float dall = x[i].dm[0];
939    float dmin = x[i].dm[1];
940    y[ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
941    y[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,
948                                          const sycl::nd_item<3> & item_ct1, int64_t n_blocks) {
949#if QK_K == 256
950    const int64_t i = item_ct1.get_group(2);
951    if (i >= n_blocks) {
952        return;
953    }
954
955    const uint8_t * base          = static_cast<const uint8_t *>(vx);
956    const size_t    qs_offset     = i * (QK_K / 4);
957    const size_t    scales_offset = n_blocks * (QK_K / 4) + i * (QK_K / 16);
958    const size_t    dm_offset     = n_blocks * (QK_K / 4) + n_blocks * (QK_K / 16) + i * sizeof(ggml_half2);
959
960    const uint8_t *     qs     = base + qs_offset;
961    const uint8_t *     scales = base + scales_offset;
962    const ggml_half2 * dm     = reinterpret_cast<const ggml_half2 *>(base + dm_offset);
963
964    const int64_t tid = item_ct1.get_local_id(2);
965    const int64_t n    = tid / 32;
966    const int64_t l    = tid - 32 * n;
967    const int64_t is   = 8 * n + l / 16;
968
969    const uint8_t q = qs[32 * n + l];
970    dst_t * y = yy + i * QK_K + 128 * n;
971
972    const float dall = (*dm)[0];
973    const float dmin = (*dm)[1];
974    y[l+ 0] = dall * (scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (scales[is+0] >> 4);
975    y[l+32] = dall * (scales[is+2] & 0xF) * ((q >> 2) & 3) - dmin * (scales[is+2] >> 4);
976    y[l+64] = dall * (scales[is+4] & 0xF) * ((q >> 4) & 3) - dmin * (scales[is+4] >> 4);
977    y[l+96] = dall * (scales[is+6] & 0xF) * ((q >> 6) & 3) - dmin * (scales[is+6] >> 4);
978#else
979    GGML_UNUSED(vx);
980    GGML_UNUSED(yy);
981    GGML_UNUSED(item_ct1);
982    GGML_UNUSED(n_blocks);
983    GGML_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,
989                                  const sycl::nd_item<3> &item_ct1) {
990
991    const int64_t i = item_ct1.get_group(2);
992    const block_q3_K * x = (const block_q3_K *) vx;
993
994#if QK_K == 256
995    const int64_t r = item_ct1.get_local_id(2) / 4;
996    const int64_t tid = r/2;
997    const int64_t is0 = r%2;
998    const int64_t l0 = 16 * is0 + 4 * (item_ct1.get_local_id(2) % 4);
999    const int64_t n = tid / 4;
1000    const int64_t j = tid - 4*n;
1001
1002    uint8_t m = 1 << (4*n + j);
1003    int64_t is = 8*n + 2*j + is0;
1004    int shift = 2*j;
1005
1006    int8_t us = is <  4 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+8] >> 0) & 3) << 4) :
1007                is <  8 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+4] >> 2) & 3) << 4) :
1008                is < 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);
1010    float d_all = x[i].d;
1011    float dl = d_all * (us - 32);
1012
1013    dst_t * y = yy + i*QK_K + 128*n + 32*j;
1014    const uint8_t * q = x[i].qs + 32*n;
1015    const uint8_t * hm = x[i].hmask;
1016
1017    for (int l = l0; l < l0+4; ++l) y[l] = dl * ((int8_t)((q[l] >> shift) & 3) - ((hm[l] & m) ? 0 : 4));
1018#else
1019    const int64_t tid = item_ct1.get_local_id(2);
1020    const int64_t is  = tid/16;  // 0 or 1
1021    const int64_t il  = tid%16;  // 0...15
1022    const int64_t im  = il/8;    // 0...1
1023    const int64_t in  = il%8;    // 0...7
1024
1025    dst_t * y = yy + i*QK_K + 16*is + il;
1026
1027    const uint8_t q = x[i].qs[il] >> (2*is);
1028    const uint8_t h = x[i].hmask[in] >> (2*is + im);
1029    const float   d = (float)x[i].d;
1030
1031    if (is == 0) {
1032        y[ 0] = d * ((x[i].scales[0] & 0xF) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
1033        y[32] = d * ((x[i].scales[1] & 0xF) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
1034    } else {
1035        y[ 0] = d * ((x[i].scales[0] >>  4) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
1036        y[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,
1044                                          const sycl::nd_item<3> & item_ct1, int64_t n_blocks) {
1045#if QK_K == 256
1046    const int64_t i = item_ct1.get_group(2);
1047    if (i >= n_blocks) {
1048        return;
1049    }
1050
1051    const uint8_t * base          = static_cast<const uint8_t *>(vx);
1052    const size_t    qs_offset     = i * (QK_K / 4);
1053    const size_t    hmask_offset  = n_blocks * (QK_K / 4) + i * (QK_K / 8);
1054    const size_t    scales_offset = n_blocks * (QK_K / 4) + n_blocks * (QK_K / 8) + i * 12;
1055    const size_t    d_offset      = n_blocks * (QK_K / 4) + n_blocks * (QK_K / 8) + n_blocks * 12 +
1056                                 i * sizeof(ggml_half);
1057
1058    const uint8_t * qs     = base + qs_offset;
1059    const uint8_t * hmask  = base + hmask_offset;
1060    const uint8_t * scales = base + scales_offset;
1061    const float     d_all  = static_cast<float>(*reinterpret_cast<const ggml_half *>(base + d_offset));
1062
1063    const int64_t r    = item_ct1.get_local_id(2) / 4;
1064    const int64_t tid  = r / 2;
1065    const int64_t is0  = r % 2;
1066    const int64_t l0   = 16 * is0 + 4 * (item_ct1.get_local_id(2) % 4);
1067    const int64_t n    = tid / 4;
1068    const int64_t j    = tid - 4 * n;
1069    const int64_t is   = 8 * n + 2 * j + is0;
1070    const int     shift = 2 * j;
1071    uint8_t       m    = 1 << (4 * n + j);
1072
1073    uint8_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
1081    const float dl = d_all * (us - 32);
1082
1083    dst_t * y = yy + i * QK_K + 128 * n + 32 * j;
1084    const uint8_t * q  = qs + 32 * n;
1085    const uint8_t * hm = hmask;
1086
1087    for (int l = l0; l < l0 + 4; ++l) {
1088        y[l] = dl * ((int8_t) ((q[l] >> shift) & 3) - ((hm[l] & m) ? 0 : 4));
1089    }
1090#else
1091    GGML_UNUSED(vx);
1092    GGML_UNUSED(yy);
1093    GGML_UNUSED(item_ct1);
1094    GGML_UNUSED(n_blocks);
1095    GGML_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) {
1101    if (j < 4) {
1102        d = q[j] & 63;
1103        m = q[j + 4] & 63;
1104    } else {
1105        d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
1106        m = (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,
1113                                   const float dmin, uint8_t * __restrict__ scales_local, int il, int ir) {
1114    const int is = 2 * il;
1115    constexpr int n  = 4;
1116
1117    uint8_t sc, m;
1118    get_scale_min_k4(is + 0, scales_local, sc, m);
1119    const float d1 = dall * sc;
1120    const float m1 = dmin * m;
1121
1122    get_scale_min_k4(is + 1, scales_local, sc, m);
1123    const float d2 = dall * sc;
1124    const 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);
1127    for (int l = 0; l < n; ++l) {
1128        y[l + 0]  = d1 * (q_vec[l] & 0xF) - m1;
1129        y[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,
1135                                  uint8_t* scales_local, const sycl::nd_item<3> &item_ct1) {
1136    const block_q4_K * x = (const block_q4_K *) vx;
1137
1138    const int64_t i = item_ct1.get_group(2);
1139
1140#if QK_K == 256
1141    const int64_t tid = item_ct1.get_local_id(2);
1142    const int64_t il  = tid / 8;
1143    const int64_t ir  = tid % 8;
1144
1145    dst_t * y = yy + i * QK_K + 64 * il + 4 * ir;
1146
1147    const sycl::half2 dm = x[i].dm;
1148    const float dall = dm[0];
1149    const float dmin = dm[1];
1150
1151    if (tid < 12) {
1152        scales_local[tid] = x[i].scales[tid];
1153    }
1154
1155    item_ct1.barrier(sycl::access::fence_space::local_space);
1156    dequantize_q4_K_common(y, x[i].qs, dall, dmin, scales_local, il, ir);
1157#else
1158    const int64_t tid = item_ct1.get_local_id(2);
1159    const uint8_t * q = x[i].qs;
1160    dst_t * y = yy + i*QK_K;
1161    const float d = (float)x[i].dm[0];
1162    const float m = (float)x[i].dm[1];
1163    y[tid+ 0] = d * (x[i].scales[0] & 0xF) * (q[tid] & 0xF) - m * (x[i].scales[0] >> 4);
1164    y[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,
1170                                          const sycl::nd_item<1> & item_ct1, int64_t nb) {
1171    const int64_t i   = item_ct1.get_group(0);     // block index
1172    const int64_t tid = item_ct1.get_local_id(0);  // thread index within block
1173    const int64_t il  = tid / 8;
1174    const int64_t ir  = tid % 8;
1175
1176    dst_t * y = yy + i * QK_K + 64 * il + 4 * ir;
1177
1178    const uint8_t * base          = static_cast<const uint8_t *>(vx);
1179    const size_t    qs_offset     = i * (QK_K / 2);
1180    const size_t    scales_offset = nb * (QK_K / 2) + i * K_SCALE_SIZE;
1181    const size_t    dm_offset     = nb * (QK_K / 2) + nb * K_SCALE_SIZE + i * sizeof(ggml_half2);
1182
1183    const uint8_t *    qs_ptr     = base + qs_offset;
1184    const uint8_t *    scales_ptr = base + scales_offset;
1185    ggml_half2         dm_values  = *reinterpret_cast<const ggml_half2 *>(base + dm_offset);
1186
1187    const float dall = dm_values.x();
1188    const float dmin = dm_values.y();
1189
1190    if (tid < 12) {
1191        scales_local[tid] = scales_ptr[tid];
1192    }
1193
1194    item_ct1.barrier(sycl::access::fence_space::local_space);
1195    dequantize_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,
1200                                  const sycl::nd_item<3> &item_ct1) {
1201    const block_q5_K * x = (const block_q5_K *) vx;
1202
1203    const 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
1207    const int64_t tid = item_ct1.get_local_id(2);
1208    const int64_t il  = tid/16;   // il is in 0...3
1209    const int64_t ir  = tid%16;   // ir is in 0...15
1210    const int64_t is  = 2*il;     // is is in 0...6
1211
1212    dst_t * y = yy + i*QK_K + 64*il + 2*ir;
1213
1214    const float dall = x[i].dm[0];
1215    const float dmin = x[i].dm[1];
1216
1217    const uint8_t * ql = x[i].qs + 32*il + 2*ir;
1218    const uint8_t * qh = x[i].qh + 2*ir;
1219
1220    uint8_t sc, m;
1221    get_scale_min_k4(is + 0, x[i].scales, sc, m);
1222    const float d1 = dall * sc; const float m1 = dmin * m;
1223    get_scale_min_k4(is + 1, x[i].scales, sc, m);
1224    const float d2 = dall * sc; const float m2 = dmin * m;
1225
1226    uint8_t   hm  = 1 << (2*il);
1227    y[ 0] = d1 * ((ql[ 0] & 0xF) + (qh[ 0] & hm ? 16 : 0)) - m1;
1228    y[ 1] = d1 * ((ql[ 1] & 0xF) + (qh[ 1] & hm ? 16 : 0)) - m1;
1229    hm <<= 1;
1230    y[32] = d2 * ((ql[ 0] >>  4) + (qh[ 0] & hm ? 16 : 0)) - m2;
1231    y[33] = d2 * ((ql[ 1] >>  4) + (qh[ 1] & hm ? 16 : 0)) - m2;
1232#else
1233    const int64_t tid = item_ct1.get_local_id(2);
1234    const uint8_t q = x[i].qs[tid];
1235    const int64_t im = tid/8;  // 0...3
1236    const int64_t in = tid%8;  // 0...7
1237    const int64_t is = tid/16; // 0 or 1
1238    const uint8_t h = x[i].qh[in] >> im;
1239    const float d = x[i].d;
1240    dst_t * y = yy + i*QK_K + tid;
1241    y[ 0] = d * x[i].scales[is+0] * ((q & 0xF) - ((h >> 0) & 1 ? 0 : 16));
1242    y[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,
1248                                          uint8_t * scales_local, const sycl::nd_item<3> & item_ct1, int64_t n_blocks) {
1249    const int64_t ib = item_ct1.get_group(2);
1250
1251#if QK_K == 256
1252    // assume 64 threads
1253    const int64_t tid = item_ct1.get_local_id(2);
1254    const int64_t il  = tid / 16;   // 0...3
1255    const int64_t ir  = tid % 16;   // 0...15
1256    const int64_t is  = 2 * il;
1257
1258    dst_t * y = yy + ib * QK_K + 64 * il + 2 * ir;
1259
1260    const 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)]
1263    const size_t qs_offset     = ib * (QK_K / 2);
1264    const size_t qh_offset     = n_blocks * (QK_K / 2) + ib * (QK_K / 8);
1265    const size_t scales_offset = n_blocks * (QK_K / 2) + n_blocks * (QK_K / 8) + ib * K_SCALE_SIZE;
1266    const size_t dm_offset     = n_blocks * (QK_K / 2) + n_blocks * (QK_K / 8) + n_blocks * K_SCALE_SIZE + ib * sizeof(ggml_half2);
1267
1268    const uint8_t *  qs_ptr     = base + qs_offset;
1269    const uint8_t *  qh_ptr     = base + qh_offset;
1270    const uint8_t *  scales_ptr = base + scales_offset;
1271    const ggml_half2 dm_values  = *reinterpret_cast<const ggml_half2 *>(base + dm_offset);
1272
1273    const float dall = dm_values.x();
1274    const float dmin = dm_values.y();
1275
1276    const uint8_t * ql = qs_ptr + 32 * il + 2 * ir;
1277    const uint8_t * qh = qh_ptr + 2 * ir;
1278
1279    if (tid < K_SCALE_SIZE) {
1280        scales_local[tid] = scales_ptr[tid];
1281    }
1282
1283    item_ct1.barrier(sycl::access::fence_space::local_space);
1284
1285    uint8_t sc, m;
1286    get_scale_min_k4(is + 0, scales_local, sc, m);
1287    const float d1 = dall * sc; const float m1 = dmin * m;
1288    get_scale_min_k4(is + 1, scales_local, sc, m);
1289    const float d2 = dall * sc; const float m2 = dmin * m;
1290
1291    uint8_t hm  = 1 << (2 * il);
1292    y[ 0] = d1 * ((ql[ 0] & 0xF) + (qh[ 0] & hm ? 16 : 0)) - m1;
1293    y[ 1] = d1 * ((ql[ 1] & 0xF) + (qh[ 1] & hm ? 16 : 0)) - m1;
1294    hm <<= 1;
1295    y[32] = d2 * ((ql[ 0] >>  4) + (qh[ 0] & hm ? 16 : 0)) - m2;
1296    y[33] = d2 * ((ql[ 1] >>  4) + (qh[ 1] & hm ? 16 : 0)) - m2;
1297#else
1298    GGML_UNUSED(ib); GGML_UNUSED(tid); GGML_UNUSED(yy); GGML_UNUSED(scales_local); GGML_UNUSED(n_blocks);
1299    GGML_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,
1305                                  const sycl::nd_item<3> &item_ct1) {
1306    const block_q6_K * x = (const block_q6_K *) vx;
1307
1308    const 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
1312    const int64_t tid = item_ct1.get_local_id(2);
1313    const int64_t ip  = tid/32;   // ip is 0 or 1
1314    const int64_t il  = tid - 32*ip; // 0...32
1315    const int64_t is  = 8*ip + il/16;
1316
1317    dst_t * y = yy + i*QK_K + 128*ip + il;
1318
1319    const float d = x[i].d;
1320
1321    const uint8_t * ql = x[i].ql + 64*ip + il;
1322    const uint8_t   qh = x[i].qh[32*ip + il];
1323    const int8_t  * sc = x[i].scales + is;
1324
1325    y[ 0] = d * sc[0] * ((int8_t)((ql[ 0] & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
1326    y[32] = d * sc[2] * ((int8_t)((ql[32] & 0xF) | (((qh >> 2) & 3) << 4)) - 32);
1327    y[64] = d * sc[4] * ((int8_t)((ql[ 0]  >> 4) | (((qh >> 4) & 3) << 4)) - 32);
1328    y[96] = d * sc[6] * ((int8_t)((ql[32]  >> 4) | (((qh >> 6) & 3) << 4)) - 32);
1329#else
1330
1331    // assume 32 threads
1332    const int64_t tid = item_ct1.get_local_id(2);
1333    const int64_t ip  = tid/16;         // 0 or 1
1334    const int64_t il  = tid - 16*ip;    // 0...15
1335
1336    dst_t * y = yy + i*QK_K + 16*ip + il;
1337
1338    const float d = x[i].d;
1339
1340    const uint8_t   ql = x[i].ql[16*ip + il];
1341    const uint8_t   qh = x[i].qh[il] >> (2*ip);
1342    const int8_t  * sc = x[i].scales;
1343
1344    y[ 0] = d * sc[ip+0] * ((int8_t)((ql & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
1345    y[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,
1351                                          const sycl::nd_item<3> & item_ct1, int64_t n_blocks) {
1352    const int64_t ib = item_ct1.get_group(2);
1353
1354    const int64_t tid = item_ct1.get_local_id(2);
1355    const int64_t ip  = tid / 32;       // ip is 0 or 1
1356    const int64_t il  = tid - 32 * ip;  // 0...32
1357    const int64_t is  = 8 * ip + il / 16;
1358
1359    const uint8_t *   base_ptr           = static_cast<const uint8_t *>(vx);
1360    const auto        ql_offset          = ib * (QK_K / 2);
1361    const auto        qh_offset          = (QK_K / 2) * n_blocks + (QK_K / 4) * ib;
1362    const auto        base_scales_offset = (QK_K / 2) * n_blocks + (QK_K / 4) * n_blocks + (QK_K / 16) * ib;
1363    const auto        base_d_offset      = ((QK_K / 2) + (QK_K / 4) + (QK_K / 16)) * n_blocks;
1364    const uint8_t *   ql_ptr             = base_ptr + ql_offset;
1365    const uint8_t *   qh_ptr             = base_ptr + qh_offset;
1366    const uint8_t *   scales_ptr         = base_ptr + base_scales_offset;
1367    const ggml_half * d                  = (const ggml_half *) (base_ptr + base_d_offset) + ib;
1368
1369    dst_t * y = yy + ib * QK_K + 128 * ip + il;
1370
1371    const uint8_t * ql = ql_ptr + 64 * ip + il;
1372    const uint8_t   qh = *(qh_ptr + 32 * ip + il);
1373    const int8_t *  sc = reinterpret_cast<const int8_t *>(scales_ptr + is);
1374
1375    y[0]  = *d * sc[0] * ((int8_t) ((ql[0] & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
1376    y[32] = *d * sc[2] * ((int8_t) ((ql[32] & 0xF) | (((qh >> 2) & 3) << 4)) - 32);
1377    y[64] = *d * sc[4] * ((int8_t) ((ql[0] >> 4) | (((qh >> 4) & 3) << 4)) - 32);
1378    y[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,
1383                                     const sycl::nd_item<3> &item_ct1,
1384                                     const uint64_t *iq2xxs_grid_ptr,
1385                                     const uint8_t *ksigns_iq2xs_ptr,
1386                                     const uint8_t *kmask_iq2xs_ptr) {
1387
1388    const int64_t i = item_ct1.get_group(2);
1389    const block_iq2_xxs * x = (const block_iq2_xxs  *) vx;
1390
1391    const int64_t tid = item_ct1.get_local_id(2);
1392#if QK_K == 256
1393    const int64_t il = tid/8; // 0...3
1394    const int64_t ib = tid%8; // 0...7
1395    dst_t * y = yy + i*QK_K + 32*ib + 8*il;
1396    const uint16_t * q2 = x[i].qs + 4*ib;
1397    const uint8_t  * aux8 = (const uint8_t *)q2;
1398    const uint8_t  * grid = (const uint8_t *)(iq2xxs_grid_ptr + aux8[il]);
1399    const uint32_t aux32 = q2[2] | (q2[3] << 16);
1400    const float d = (float)x[i].d * (0.5f + (aux32 >> 28)) * 0.25f;
1401    const uint8_t signs = ksigns_iq2xs_ptr[(aux32 >> 7*il) & 127];
1402    for (int j = 0; j < 8; ++j) y[j] = d * grid[j] * (signs & kmask_iq2xs_ptr[j] ? -1.f : 1.f);
1403#else
1404    assert(false);
1405#endif
1406
1407}
1408
1409template<typename dst_t>
1410static void dequantize_block_iq2_xs(const void * __restrict__ vx, dst_t * __restrict__ yy,
1411                                    const sycl::nd_item<3> &item_ct1,
1412                                    const uint64_t *iq2xs_grid,
1413                                    const uint8_t *ksigns_iq2xs,
1414                                    const uint8_t *kmask_iq2xs) {
1415
1416    const int64_t i = item_ct1.get_group(2);
1417    const block_iq2_xs * x = (const block_iq2_xs *) vx;
1418
1419    const int64_t tid = item_ct1.get_local_id(2);
1420#if QK_K == 256
1421    const int64_t il = tid/8; // 0...3
1422    const int64_t ib = tid%8; // 0...7
1423    dst_t * y = yy + i*QK_K + 32*ib + 8*il;
1424    const uint16_t * q2 = x[i].qs + 4*ib;
1425    const uint8_t  * grid = (const uint8_t *)(iq2xs_grid + (q2[il] & 511));
1426    const float d = (float)x[i].d * (0.5f + ((x[i].scales[ib] >> 4*(il/2)) & 0xf)) * 0.25f;
1427    const uint8_t signs = ksigns_iq2xs[q2[il] >> 9];
1428    for (int j = 0; j < 8; ++j) y[j] = d * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
1429#else
1430    assert(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,
1438                       const sycl::nd_item<3> &item_ct1) {
1439
1440    const int64_t i = item_ct1.get_group(2);
1441    const block_iq2_s * x = (const block_iq2_s *) vx;
1442
1443    const int64_t tid = item_ct1.get_local_id(2);
1444#if QK_K == 256
1445    const int64_t il = tid/8; // 0...3
1446    const int64_t ib = tid%8; // 0...7
1447    dst_t * y = yy + i*QK_K + 32*ib + 8*il;
1448    const uint8_t * grid = (const uint8_t *)(iq2s_grid + (x[i].qs[4*ib+il] | ((x[i].qh[ib] << (8-2*il)) & 0x300)));
1449    const float d = (float)x[i].d * (0.5f + ((x[i].scales[ib] >> 4*(il/2)) & 0xf)) * 0.25f;
1450    const uint8_t signs = x[i].qs[QK_K/8+4*ib+il];
1451#pragma unroll
1452    for (int j = 0; j < 8; ++j)
1453        y[j] = d * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
1454#else
1455    assert(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,
1463                                     const sycl::nd_item<3> &item_ct1,
1464                                     const uint32_t *iq3xxs_grid,
1465                                     const uint8_t *ksigns_iq2xs,
1466                                     const uint8_t *kmask_iq2xs) {
1467
1468    const int64_t i = item_ct1.get_group(2);
1469    const block_iq3_xxs * x = (const block_iq3_xxs  *) vx;
1470
1471    const int64_t tid = item_ct1.get_local_id(2);
1472#if QK_K == 256
1473    const int64_t il = tid/8; // 0...3
1474    const int64_t ib = tid%8; // 0...7
1475    dst_t * y = yy + i*QK_K + 32*ib + 8*il;
1476    const uint8_t  * q3 = x[i].qs + 8*ib;
1477    const uint16_t * gas = (const uint16_t *)(x[i].qs + QK_K/4) + 2*ib;
1478    const uint8_t  * grid1 = (const uint8_t *)(iq3xxs_grid + q3[2*il+0]);
1479    const uint8_t  * grid2 = (const uint8_t *)(iq3xxs_grid + q3[2*il+1]);
1480    const uint32_t aux32 = gas[0] | (gas[1] << 16);
1481    const float d = (float)x[i].d * (0.5f + (aux32 >> 28)) * 0.5f;
1482    const uint8_t signs = ksigns_iq2xs[(aux32 >> 7*il) & 127];
1483    for (int j = 0; j < 4; ++j) {
1484        y[j+0] = d * grid1[j] * (signs & kmask_iq2xs[j+0] ? -1.f : 1.f);
1485        y[j+4] = d * grid2[j] * (signs & kmask_iq2xs[j+4] ? -1.f : 1.f);
1486    }
1487#else
1488    assert(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,
1496                       const sycl::nd_item<3> &item_ct1,
1497                       const uint8_t *kmask_iq2xs, const uint32_t *iq3s_grid) {
1498
1499    const int64_t i = item_ct1.get_group(2);
1500    const block_iq3_s * x = (const block_iq3_s *) vx;
1501
1502    const int64_t tid = item_ct1.get_local_id(2);
1503#if QK_K == 256
1504    const int64_t il = tid/8; // 0...3
1505    const int64_t ib = tid%8; // 0...7
1506    dst_t * y = yy + i*QK_K + 32*ib + 8*il;
1507    const uint8_t * qs = x[i].qs + 8*ib;
1508    const uint8_t * grid1 = (const uint8_t *)(iq3s_grid + (qs[2*il+0] | ((x[i].qh[ib] << (8-2*il)) & 256)));
1509    const uint8_t * grid2 = (const uint8_t *)(iq3s_grid + (qs[2*il+1] | ((x[i].qh[ib] << (7-2*il)) & 256)));
1510    const float d = (float)x[i].d * (1 + 2*((x[i].scales[ib/2] >> 4*(ib%2)) & 0xf));
1511    const uint8_t signs = x[i].signs[4*ib + il];
1512#pragma unroll
1513    for (int j = 0; j < 4; ++j) {
1514        y[j+0] = d * grid1[j] * (signs & kmask_iq2xs[j+0] ? -1.f : 1.f);
1515        y[j+4] = d * grid2[j] * (signs & kmask_iq2xs[j+4] ? -1.f : 1.f);
1516    }
1517#else
1518    assert(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,
1526                       const sycl::nd_item<3> &item_ct1,
1527                       const uint32_t *iq1s_grid_gpu) {
1528
1529    const int64_t i = item_ct1.get_group(2);
1530    const block_iq1_s * x = (const block_iq1_s  *) vx;
1531
1532    const int64_t tid = item_ct1.get_local_id(2);
1533#if QK_K == 256
1534    const int64_t il = tid/8; // 0...3
1535    const int64_t ib = tid%8; // 0...7
1536    dst_t * y = yy + i*QK_K + 32*ib + 8*il;
1537    const float delta = x[i].qh[ib] & 0x8000 ? -1 - IQ1S_DELTA : -1 + IQ1S_DELTA;
1538    const float d = (float)x[i].d * (2*((x[i].qh[ib] >> 12) & 7) + 1);
1539    uint32_t grid32[2]; const int8_t * q = (const int8_t *)grid32;
1540    grid32[0] = iq1s_grid_gpu[x[i].qs[4*ib+il] | (((x[i].qh[ib] >> 3*il) & 7) << 8)];
1541    grid32[1] = (grid32[0] >> 4) & 0x0f0f0f0f;
1542    grid32[0] &= 0x0f0f0f0f;
1543#pragma unroll
1544    for (int j = 0; j < 8; ++j) {
1545        y[j] = d * (q[j] + delta);
1546    }
1547#else
1548    assert(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,
1556                       const sycl::nd_item<3> &item_ct1,
1557                       const uint32_t *iq1s_grid_gpu) {
1558
1559    const int64_t i = item_ct1.get_group(2);
1560    const block_iq1_m * x = (const block_iq1_m  *) vx;
1561
1562    const int64_t tid = item_ct1.get_local_id(2);
1563#if QK_K == 256
1564    const int64_t il = tid/8; // 0...3
1565    const int64_t ib = tid%8; // 0...7
1566    dst_t * y = yy + i*QK_K + 32*ib + 8*il;
1567    const uint16_t * sc = (const uint16_t *)x[i].scales;
1568    iq1m_scale_t scale;
1569    scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
1570    const int ib16 = 2*ib + il/2; // sc[ib16/4] >> 3*(ib16%4) -> sc[ib/2] >> 3*((2*ib+il/2)%4);
1571    const float d = (float)scale.f16 * (2*((sc[ib16/4] >> 3*(ib16%4)) & 0x7) + 1);
1572    const float delta = x[i].qh[2*ib+il/2] & (0x08 << 4*(il%2)) ? -1 - IQ1M_DELTA : -1 + IQ1M_DELTA;
1573    uint32_t grid32[2]; const int8_t * q = (const int8_t *)grid32;
1574    grid32[0] = iq1s_grid_gpu[x[i].qs[4*ib+il] | (((x[i].qh[2*ib+il/2] >> 4*(il%2)) & 7) << 8)];
1575    grid32[1] = (grid32[0] >> 4) & 0x0f0f0f0f;
1576    grid32[0] &= 0x0f0f0f0f;
1577#pragma unroll
1578    for (int j = 0; j < 8; ++j) {
1579        y[j] = d * (q[j] + delta);
1580    }
1581#else
1582    assert(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,
1590                        const sycl::nd_item<3> &item_ct1) {
1591
1592    const int64_t i = item_ct1.get_group(2);
1593    const block_iq4_nl * x = (const block_iq4_nl *) vx + i*(QK_K/QK4_NL);
1594
1595    const int64_t tid = item_ct1.get_local_id(2);
1596    const int64_t il = tid/8; // 0...3
1597    const int64_t ib = tid%8; // 0...7
1598    dst_t * y = yy + i*QK_K + 32*ib + 4*il;
1599    const uint8_t  * q4 = x[ib].qs + 4*il;
1600    const float d = (float)x[ib].d;
1601#pragma unroll
1602    for (int j = 0; j < 4; ++j) {
1603        y[j+ 0] = d * kvalues_iq4nl[q4[j] & 0xf];
1604        y[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,
1613                        const sycl::nd_item<3> &item_ct1) {
1614    const int64_t i = item_ct1.get_group(2);
1615    const block_iq4_xs * x = (const block_iq4_xs *)vx;
1616
1617    const int64_t tid = item_ct1.get_local_id(2);
1618    const int64_t il = tid/8; // 0...3
1619    const int64_t ib = tid%8; // 0...7
1620    dst_t * y = yy + i*QK_K + 32*ib + 4*il;
1621    const uint8_t  * q4 = x[i].qs + 16*ib + 4*il;
1622    const 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
1624    for (int j = 0; j < 4; ++j) {
1625        y[j+ 0] = d * kvalues_iq4nl[q4[j] & 0xf];
1626        y[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,
1632                                   const sycl::nd_item<3> &item_ct1) {
1633    // auto                item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item<3>();
1634    const int64_t       i        = item_ct1.get_group(2);
1635    const block_mxfp4 * x = (const block_mxfp4 *) vx + i*(QK_K/QK_MXFP4);
1636
1637    const int64_t    tid = item_ct1.get_local_id(2);
1638    const int64_t il = tid/8; // 0...3
1639    const int64_t ib = tid%8; // 0...7
1640    dst_t * y = yy + i*QK_K + 32*ib + 4*il;
1641    const uint8_t  * q4 = x[ib].qs + 4*il;
1642    const float d = ggml_sycl_e8m0_to_fp32(x[ib].e);
1643    for (int j = 0; j < 4; ++j) {
1644        y[j+ 0] = d * kvalues_mxfp4[q4[j] & 0xf]*0.5f;
1645        y[j+16] = d * kvalues_mxfp4[q4[j] >>  4]*0.5f;
1646    }
1647}
1648
1649
1650template <typename dst_t>
1651static void dequantize_block_nvfp4(
1652        const void * __restrict__ vx,
1653        dst_t * __restrict__ yy,
1654        const int64_t ne) {
1655    auto          item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item<3>();
1656    const int64_t i        = item_ct1.get_group(2);
1657    const int     tid      = item_ct1.get_local_id(2);
1658
1659    const int64_t base = i * QK_NVFP4;
1660    if (base >= ne) {
1661        return;
1662    }
1663
1664    const block_nvfp4 * x = (const block_nvfp4 *) vx;
1665    const block_nvfp4 & xb = x[i];
1666
1667    const int sub = tid / (QK_NVFP4_SUB / 2);
1668    const int j = tid % (QK_NVFP4_SUB / 2);
1669
1670    const float d = ggml_sycl_ue4m3_to_fp32(xb.d[sub]);
1671    const uint8_t q = xb.qs[sub * (QK_NVFP4_SUB / 2) + j];
1672
1673    const int64_t y0 = base + sub * QK_NVFP4_SUB + j;
1674    const int64_t y1 = y0 + QK_NVFP4_SUB / 2;
1675
1676    yy[y0] = ggml_sycl_cast<dst_t>(d * kvalues_mxfp4[q & 0x0F]);
1677    yy[y1] = ggml_sycl_cast<dst_t>(d * kvalues_mxfp4[q >> 4]);
1678}
1679
1680
1681#endif // GGML_SYCL_DEQUANTIZE_HPP