interplane.Get Omega
Read in your language

ggml

[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)

Brought by drakestapleton

branch master Branches Tags History

ggml / src / ggml-sycl / dmmv.cpp

2113 lines · 88975 bytes Raw Blame History
1#include "convert.hpp"
2#include "dmmv.hpp"
3#include "dequantize.hpp"
4#include "presets.hpp"
5
6#if defined(__INTEL_LLVM_COMPILER)
7 #if __has_include(<sycl/ext/oneapi/bfloat16.hpp>)
8 #include <sycl/ext/oneapi/bfloat16.hpp>
9 #define GGML_SYCL_DMMV_HAS_BF16
10 #endif
11 #include <sycl/ext/intel/esimd.hpp>
12 #include "esimd.hpp"
13#endif
14
15static void convert_f16(const void * vx, const int64_t ib, const int iqs, dfloat2 & v){
16 const sycl::half *x = (const sycl::half *)vx;
17
18 // automatic half -> float type cast if dfloat == float
19 v.x() = x[ib + iqs + 0];
20 v.y() = x[ib + iqs + 1];
21}
22
23#ifdef GGML_SYCL_DMMV_HAS_BF16
24static void convert_bf16(const void * vx, const int64_t ib, const int iqs, dfloat2 & v){
25 const sycl::ext::oneapi::bfloat16 *x = (const sycl::ext::oneapi::bfloat16 *)vx;
26
27 // automatic bfloat16 -> float type cast if dfloat == float
28 v.x() = x[ib + iqs + 0];
29 v.y() = x[ib + iqs + 1];
30}
31#endif
32
33static void convert_f32(const void * vx, const int64_t ib, const int iqs, dfloat2 & v){
34 const float * x = (const float *) vx;
35
36 // automatic half -> float type cast if dfloat == float
37 v.x() = x[ib + iqs + 0];
38 v.y() = x[ib + iqs + 1];
39}
40
41template <int qk, int qr, dequantize_kernel_t dequantize_kernel>
42static void dequantize_mul_mat_vec(const void * __restrict__ vx, const dfloat * __restrict__ y, float * __restrict__ dst, const int ncols, const int nrows,
43 const sycl::nd_item<3> &item_ct1) {
44 // qk = quantized weights per x block
45 // qr = number of quantized weights per data value in x block
46 const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
47 item_ct1.get_local_id(1);
48
49 if (row >= nrows) {
50 return;
51 }
52
53 const int tid = item_ct1.get_local_id(2);
54
55 const int iter_stride = 2*GGML_SYCL_DMMV_X;
56 const int vals_per_iter = iter_stride / WARP_SIZE; // num quantized vals per thread and i iter
57 const int y_offset = qr == 1 ? 1 : qk/2;
58
59// partial sum for each thread
60#ifdef GGML_SYCL_F16
61 sycl::half2 tmp = {0.0f, 0.0f}; // two sums for f16 to take advantage of half2 intrinsics
62#else
63 float tmp = 0.0f;
64#endif // GGML_SYCL_F16
65
66 for (int i = 0; i < ncols; i += iter_stride) {
67 const int col = i + vals_per_iter*tid;
68 if (col >= ncols) {
69 continue;
70 }
71 const int ib = (row*ncols + col)/qk; // x block index
72 const int iqs = (col%qk)/qr; // x quant index
73 const int iybs = col - col%qk; // y block start index
74
75// processing >2 values per i iter is faster for fast GPUs
76#pragma unroll
77 for (int j = 0; j < vals_per_iter; j += 2) {
78 // process 2 vals per j iter
79
80 // dequantize
81 // for qr = 2 the iqs needs to increase by 1 per j iter because 2 weights per data val
82 dfloat2 v;
83 dequantize_kernel(vx, ib, iqs + j/qr, v);
84
85 // matrix multiplication
86 // for qr = 2 the y index needs to increase by 1 per j iter because of y_offset = qk/2
87#ifdef GGML_SYCL_F16
88 dfloat2 t1{y[iybs + iqs + j / qr + 0],
89 y[iybs + iqs + j / qr + y_offset]};
90
91 tmp += v * t1;
92#else
93 tmp += v.x() * y[iybs + iqs + j / qr + 0];
94 tmp += v.y() * y[iybs + iqs + j / qr + y_offset];
95#endif // GGML_SYCL_F16
96 }
97 }
98
99 // sum up partial sums and write back result
100 const int mask_start = ncols > GGML_SYCL_DMMV_X ? WARP_SIZE >> 1 : WARP_SIZE >> 2;
101 for (int mask = mask_start; mask > 0; mask >>= 1) {
102 tmp +=
103 dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
104 }
105
106 if (tid == 0) {
107#ifdef GGML_SYCL_F16
108 dst[row] = tmp.x() + tmp.y();
109#else
110 dst[row] = tmp;
111#endif // GGML_SYCL_F16
112 }
113}
114
115template <int qk, int qr, int qbytes, dequantize_kernel_t_reorder dequantize_kernel_reorder>
116static void dequantize_mul_mat_vec_reorder(const void * __restrict__ vx, const dfloat * __restrict__ y, float * __restrict__ dst, const int ncols, const int nrows,
117 const sycl::nd_item<3> &item_ct1) {
118 // qk = quantized weights per x block
119 // qr = number of quantized weights per data value in x block
120 // qbytes = bytes of the quants region per block (not qk/qr for bit-packed types)
121 const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
122 item_ct1.get_local_id(1);
123
124 if (row >= nrows) {
125 return;
126 }
127
128 const int tid = item_ct1.get_local_id(2);
129
130
131 const int ncols_left = ncols % (qk*WARP_SIZE);
132 const int ncols_align = ncols - ncols_left;
133 const int iter_stride = 8*2*GGML_SYCL_DMMV_X;
134 const int vals_per_iter = iter_stride / WARP_SIZE; // num quantized vals per thread and i iter //64/16=4, 512/16/2= 16
135 const int y_offset = qr == 1 ? 1 : qk/2;
136
137// partial sum for each thread
138#ifdef GGML_SYCL_F16
139 sycl::half2 tmp = {0.0f, 0.0f}; // two sums for f16 to take advantage of half2 intrinsics
140#else
141 float tmp = 0.0f;
142#endif // GGML_SYCL_F16
143 const char *d_ptr = (const char*)vx + (size_t) ncols * nrows / qk * qbytes;
144 int i=0;
145 for (i = 0; i < ncols_align; i += iter_stride) {
146 const int col = i + vals_per_iter*tid;
147 const int ib = (row*ncols + col)/qk; // x block index
148 const int iqs = (col%qk)/qr; // x quant index
149 const int iybs = col - col%qk; // y block start index
150
151// processing >2 values per i iter is faster for fast GPUs
152#pragma unroll
153 for (int j = 0; j < vals_per_iter; j += 2) {
154 // process 2 vals per j iter
155
156 // dequantize
157 // for qr = 2 the iqs needs to increase by 1 per j iter because 2 weights per data val
158 dfloat2 v;
159 dequantize_kernel_reorder((const void *)d_ptr, ib, (const void *)vx, ib * (qk/qr) +iqs+j/qr, v);
160
161 // matrix multiplication
162 // for qr = 2 the y index needs to increase by 1 per j iter because of y_offset = qk/2
163#ifdef GGML_SYCL_F16
164 dfloat2 t1{y[iybs + iqs + j / qr + 0],
165 y[iybs + iqs + j / qr + y_offset]};
166
167 tmp += v * t1;
168#else
169 tmp += v.x() * y[iybs + iqs + j / qr + 0];
170 tmp += v.y() * y[iybs + iqs + j / qr + y_offset];
171#endif // GGML_SYCL_F16
172 }
173 }
174
175 for (; i < ncols; i += iter_stride) {
176 if (tid>=ncols_left/qk) continue;
177 const int col = i + vals_per_iter*tid;
178 const int ib = (row*ncols + col)/qk; // x block index
179 const int iqs = (col%qk)/qr; // x quant index
180 const int iybs = col - col%qk; // y block start index
181
182// processing >2 values per i iter is faster for fast GPUs
183#pragma unroll
184 for (int j = 0; j < vals_per_iter; j += 2) {
185 // process 2 vals per j iter
186
187 // dequantize
188 // for qr = 2 the iqs needs to increase by 1 per j iter because 2 weights per data val
189 dfloat2 v;
190 dequantize_kernel_reorder((const void *)d_ptr, ib, (const void *)vx, ib * (qk/qr) +iqs+j/qr, v);
191
192 // matrix multiplication
193 // for qr = 2 the y index needs to increase by 1 per j iter because of y_offset = qk/2
194#ifdef GGML_SYCL_F16
195 dfloat2 t1{y[iybs + iqs + j / qr + 0],
196 y[iybs + iqs + j / qr + y_offset]};
197
198 tmp += v * t1;
199#else
200 tmp += v.x() * y[iybs + iqs + j / qr + 0];
201 tmp += v.y() * y[iybs + iqs + j / qr + y_offset];
202#endif // GGML_SYCL_F16
203 }
204 }
205
206 // sum up partial sums and write back result
207 const int mask_start = ncols > GGML_SYCL_DMMV_X ? WARP_SIZE >> 1 : WARP_SIZE >> 2;
208 for (int mask = mask_start; mask > 0; mask >>= 1) {
209 tmp +=
210 dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
211 }
212
213 if (tid == 0) {
214#ifdef GGML_SYCL_F16
215 dst[row] = tmp.x() + tmp.y();
216#else
217 dst[row] = tmp;
218#endif // GGML_SYCL_F16
219 }
220}
221
222static void convert_mul_mat_vec_f16_sycl(const void *vx, const dfloat *y,
223 float *dst, const int ncols,
224 const int nrows,
225 dpct::queue_ptr stream) {
226 GGML_ASSERT(ncols % GGML_SYCL_DMMV_X == 0);
227 const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
228 const sycl::range<3> block_nums(1, 1, block_num_y);
229 const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
230 {
231 dpct::has_capability_or_fail(stream->get_device(),
232 {sycl::aspect::fp16});
233
234 stream->parallel_for(
235 sycl::nd_range<3>(block_nums * block_dims, block_dims),
236 [=](sycl::nd_item<3> item_ct1) [[sycl::reqd_sub_group_size(WARP_SIZE)]] {
237 dequantize_mul_mat_vec<1, 1, convert_f16>(vx, y, dst, ncols,
238 nrows, item_ct1);
239 });
240 }
241}
242
243#ifdef GGML_SYCL_DMMV_HAS_BF16
244static void convert_mul_mat_vec_bf16_sycl(const void *vx, const dfloat *y,
245 float *dst, const int ncols,
246 const int nrows,
247 dpct::queue_ptr stream) {
248 // The qk=1 kernel iterates with stride 2*GGML_SYCL_DMMV_X, so ncols must be a
249 // multiple of that — not just GGML_SYCL_DMMV_X — to avoid out-of-bounds reads.
250 GGML_ASSERT(ncols % (2*GGML_SYCL_DMMV_X) == 0);
251 const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
252 const sycl::range<3> block_nums(1, 1, block_num_y);
253 const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
254 {
255 stream->parallel_for(
256 sycl::nd_range<3>(block_nums * block_dims, block_dims),
257 [=](sycl::nd_item<3> item_ct1) [[sycl::reqd_sub_group_size(WARP_SIZE)]] {
258 dequantize_mul_mat_vec<1, 1, convert_bf16>(vx, y, dst, ncols,
259 nrows, item_ct1);
260 });
261 }
262}
263#endif
264
265static void dequantize_mul_mat_vec_q2_k(const void *__restrict__ vx,
266 const float *__restrict__ yy,
267 float *__restrict__ dst,
268 const int ncols, int nrows,
269 const sycl::nd_item<3> &item_ct1) {
270
271 static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
272
273 const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
274 item_ct1.get_local_id(1);
275 if (row >= nrows) return;
276
277 const int num_blocks_per_row = ncols / QK_K;
278 const int ib0 = row*num_blocks_per_row;
279
280 const block_q2_K * x = (const block_q2_K *)vx + ib0;
281
282 float tmp = 0; // partial sum for thread in warp
283
284#if QK_K == 256
285 const int tid =
286 item_ct1.get_local_id(2) / K_QUANTS_PER_ITERATION; // 0...7 or 0...15
287 const int ix =
288 item_ct1.get_local_id(2) % K_QUANTS_PER_ITERATION; // 0 or 0,1
289
290 const int step = 16/K_QUANTS_PER_ITERATION;
291
292 const int in = tid % step; // 0...15 or 0...7
293
294 const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15 or 0...14 in steps of 2
295
296 uint32_t aux[4];
297 const uint8_t * d = (const uint8_t *)aux;
298 const uint8_t * m = (const uint8_t *)(aux + 2);
299
300 for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
301
302 const float dall = x[i].dm[0];
303 const float dmin = x[i].dm[1];
304
305 for (int im = 0; im < 2; ++im) {
306 const int q_offset = 32*im + l0;
307 const int s_offset = 8*im;
308 const int y_offset = 128*im + l0;
309
310 const float * y = yy + i * QK_K + y_offset;
311 const uint8_t * q = x[i].qs + q_offset;
312
313 const uint32_t * a = (const uint32_t *)(x[i].scales + s_offset);
314 aux[0] = a[0] & 0x0f0f0f0f;
315 aux[1] = a[1] & 0x0f0f0f0f;
316 aux[2] = (a[0] >> 4) & 0x0f0f0f0f;
317 aux[3] = (a[1] >> 4) & 0x0f0f0f0f;
318
319 float sum1 = 0, sum2 = 0;
320 for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
321 sum1 += y[l+ 0] * d[0] * ((q[l+ 0] >> 0) & 3)
322 + y[l+32] * d[2] * ((q[l+ 0] >> 2) & 3)
323 + y[l+64] * d[4] * ((q[l+ 0] >> 4) & 3)
324 + y[l+96] * d[6] * ((q[l+ 0] >> 6) & 3)
325 + y[l+16] * d[1] * ((q[l+16] >> 0) & 3)
326 + y[l+48] * d[3] * ((q[l+16] >> 2) & 3)
327 + y[l+80] * d[5] * ((q[l+16] >> 4) & 3)
328 +y[l+112] * d[7] * ((q[l+16] >> 6) & 3);
329 sum2 += y[l+ 0] * m[0] + y[l+32] * m[2] + y[l+64] * m[4] + y[ l+96] * m[6]
330 + y[l+16] * m[1] + y[l+48] * m[3] + y[l+80] * m[5] + y[l+112] * m[7];
331
332 }
333 tmp += dall * sum1 - dmin * sum2;
334 }
335
336 }
337#else
338 const int tid = item_ct1.get_local_id(2) /
339 (2 * K_QUANTS_PER_ITERATION); // 0...15 or 0...7
340 const int ix = item_ct1.get_local_id(2) %
341 (2 * K_QUANTS_PER_ITERATION); // 0....1 or 0...3
342 const int offset = tid * K_QUANTS_PER_ITERATION;
343
344 uint32_t uaux[2];
345 const uint8_t * d = (const uint8_t *)uaux;
346
347
348 for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
349
350 const float * y = yy + i * QK_K + offset;
351 const uint8_t * q = x[i].qs + offset;
352 const uint32_t * s = (const uint32_t *)x[i].scales;
353
354 uaux[0] = s[0] & 0x0f0f0f0f;
355 uaux[1] = (s[0] >> 4) & 0x0f0f0f0f;
356
357 const sycl::float2 dall =
358 x[i].dm.convert<float, sycl::rounding_mode::automatic>();
359
360 float sum1 = 0, sum2 = 0;
361 for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
362 const uint8_t ql = q[l];
363 sum1 += y[l+ 0] * d[0] * ((ql >> 0) & 3)
364 + y[l+16] * d[1] * ((ql >> 2) & 3)
365 + y[l+32] * d[2] * ((ql >> 4) & 3)
366 + y[l+48] * d[3] * ((ql >> 6) & 3);
367 sum2 += y[l+0] * d[4] + y[l+16] * d[5] + y[l+32] * d[6] + y[l+48] * d[7];
368 }
369 tmp += dall.x() * sum1 - dall.y() * sum2;
370 }
371
372#endif
373
374 // sum up partial sums and write back result
375#pragma unroll
376 for (int mask = WARP_SIZE / 2; mask > 0; mask >>= 1) {
377 tmp +=
378 dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
379 }
380
381 if (item_ct1.get_local_id(2) == 0) {
382 dst[row] = tmp;
383 }
384}
385
386static void dequantize_mul_mat_vec_q2_k_reorder(const void *__restrict__ vx,
387 const float *__restrict__ yy,
388 float *__restrict__ dst,
389 const int ncols, int nrows,
390 const sycl::nd_item<3> &item_ct1) {
391
392 static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
393
394 const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
395 item_ct1.get_local_id(1);
396 if (row >= nrows) return;
397
398 const int num_blocks_per_row = ncols / QK_K;
399 const int ib0 = row*num_blocks_per_row;
400
401 // SOA base pointers for the reordered layout:
402 // [qs: nb * (QK_K/4)] [scales: nb * (QK_K/16)] [dm: nb * sizeof(half2)]
403 const int nb = nrows * num_blocks_per_row;
404 const uint8_t * qs_base = (const uint8_t *)vx;
405 const uint8_t * scales_base = qs_base + (size_t)nb * (QK_K / 4);
406 const sycl::half2 * dm_base = (const sycl::half2 *)(scales_base + (size_t)nb * (QK_K / 16));
407
408 float tmp = 0; // partial sum for thread in warp
409
410#if QK_K == 256
411 const int tid =
412 item_ct1.get_local_id(2) / K_QUANTS_PER_ITERATION; // 0...7 or 0...15
413 const int ix =
414 item_ct1.get_local_id(2) % K_QUANTS_PER_ITERATION; // 0 or 0,1
415
416 const int step = 16/K_QUANTS_PER_ITERATION;
417
418 const int in = tid % step; // 0...15 or 0...7
419
420 const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15 or 0...14 in steps of 2
421
422 uint32_t aux[4];
423 const uint8_t * d = (const uint8_t *)aux;
424 const uint8_t * m = (const uint8_t *)(aux + 2);
425
426 for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
427 const int bi = ib0 + i;
428
429 const sycl::half2 dm_val = dm_base[bi];
430 const float dall = dm_val[0];
431 const float dmin = dm_val[1];
432
433 for (int im = 0; im < 2; ++im) {
434 const int q_offset = 32*im + l0;
435 const int s_offset = 8*im;
436 const int y_offset = 128*im + l0;
437
438 const float * y = yy + i * QK_K + y_offset;
439 const uint8_t * q = qs_base + bi * (QK_K / 4) + q_offset;
440
441 const uint32_t * a = (const uint32_t *)(scales_base + bi * (QK_K / 16) + s_offset);
442 aux[0] = a[0] & 0x0f0f0f0f;
443 aux[1] = a[1] & 0x0f0f0f0f;
444 aux[2] = (a[0] >> 4) & 0x0f0f0f0f;
445 aux[3] = (a[1] >> 4) & 0x0f0f0f0f;
446
447 float sum1 = 0, sum2 = 0;
448 for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
449 sum1 += y[l+ 0] * d[0] * ((q[l+ 0] >> 0) & 3)
450 + y[l+32] * d[2] * ((q[l+ 0] >> 2) & 3)
451 + y[l+64] * d[4] * ((q[l+ 0] >> 4) & 3)
452 + y[l+96] * d[6] * ((q[l+ 0] >> 6) & 3)
453 + y[l+16] * d[1] * ((q[l+16] >> 0) & 3)
454 + y[l+48] * d[3] * ((q[l+16] >> 2) & 3)
455 + y[l+80] * d[5] * ((q[l+16] >> 4) & 3)
456 +y[l+112] * d[7] * ((q[l+16] >> 6) & 3);
457 sum2 += y[l+ 0] * m[0] + y[l+32] * m[2] + y[l+64] * m[4] + y[ l+96] * m[6]
458 + y[l+16] * m[1] + y[l+48] * m[3] + y[l+80] * m[5] + y[l+112] * m[7];
459
460 }
461 tmp += dall * sum1 - dmin * sum2;
462 }
463 }
464#else
465 GGML_UNUSED(vx);
466 GGML_UNUSED(yy);
467 GGML_UNUSED(ncols);
468 GGML_UNUSED(item_ct1);
469 GGML_ABORT("Q2_K reorder DMMV not supported for QK_K != 256");
470#endif
471
472 // sum up partial sums and write back result
473#pragma unroll
474 for (int mask = WARP_SIZE / 2; mask > 0; mask >>= 1) {
475 tmp +=
476 dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
477 }
478
479 if (item_ct1.get_local_id(2) == 0) {
480 dst[row] = tmp;
481 }
482}
483
484static void dequantize_mul_mat_vec_q3_k(const void *__restrict__ vx,
485 const float *__restrict__ yy,
486 float *__restrict__ dst,
487 const int ncols, int nrows,
488 const sycl::nd_item<3> &item_ct1) {
489
490 const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
491 item_ct1.get_local_id(1);
492 if (row >= nrows) return;
493
494 const int num_blocks_per_row = ncols / QK_K;
495 const int ib0 = row*num_blocks_per_row;
496
497 const block_q3_K * x = (const block_q3_K *)vx + ib0;
498
499 float tmp = 0; // partial sum for thread in warp
500
501#if QK_K == 256
502
503 const uint16_t kmask1 = 0x0303;
504 const uint16_t kmask2 = 0x0f0f;
505
506 const int tid =
507 item_ct1.get_local_id(2) / K_QUANTS_PER_ITERATION; // 0...7 or 0...15
508 const int ix =
509 item_ct1.get_local_id(2) % K_QUANTS_PER_ITERATION; // 0 or 0,1
510
511 const int n = K_QUANTS_PER_ITERATION; // iterations in the inner loop
512 const int step = 16/K_QUANTS_PER_ITERATION;
513 const int in = tid % step; // 0...15 or 0...7
514
515 const int l0 = n*in; // 0...15 or 0...14 in steps of 2
516
517 uint16_t utmp[4];
518 const int8_t * s = (const int8_t *)utmp;
519
520 for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
521
522 const uint8_t * h = x[i].hmask + l0;
523 const float d = x[i].d;
524
525 for (int im = 0; im < 2; ++im) {
526 const int q_offset = 32*im + l0;
527 const int y_offset = 128*im + l0;
528 const uint16_t s_shift = 4*im;
529 const uint8_t m = 1 << (4*im);
530
531 const float * y = yy + i * QK_K + y_offset;
532 const uint8_t * q = x[i].qs + q_offset;
533
534 const uint16_t * a = (const uint16_t *)x[i].scales;
535 utmp[0] = ((a[0] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 0)) & kmask1) << 4);
536 utmp[1] = ((a[1] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 0)) & kmask1) << 4);
537 utmp[2] = ((a[2] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 2)) & kmask1) << 4);
538 utmp[3] = ((a[3] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 2)) & kmask1) << 4);
539
540 float sum = 0;
541 for (int l = 0; l < n; ++l) {
542 sum += y[l+ 0] * (s[0] - 32) * (((q[l] >> 0) & 3) - (h[l] & (m << 0) ? 0 : 4))
543 + y[l+32] * (s[2] - 32) * (((q[l] >> 2) & 3) - (h[l] & (m << 1) ? 0 : 4))
544 + y[l+64] * (s[4] - 32) * (((q[l] >> 4) & 3) - (h[l] & (m << 2) ? 0 : 4))
545 + y[l+96] * (s[6] - 32) * (((q[l] >> 6) & 3) - (h[l] & (m << 3) ? 0 : 4));
546 sum += y[l+16] * (s[1] - 32) * (((q[l+16] >> 0) & 3) - (h[l+16] & (m << 0) ? 0 : 4))
547 + y[l+48] * (s[3] - 32) * (((q[l+16] >> 2) & 3) - (h[l+16] & (m << 1) ? 0 : 4))
548 + y[l+80] * (s[5] - 32) * (((q[l+16] >> 4) & 3) - (h[l+16] & (m << 2) ? 0 : 4))
549 + y[l+112] * (s[7] - 32) * (((q[l+16] >> 6) & 3) - (h[l+16] & (m << 3) ? 0 : 4));
550 }
551 tmp += d * sum;
552 }
553
554 }
555#else
556
557 const int tid = item_ct1.get_local_id(2)/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
558 const int ix = item_ct1.get_local_id(2)%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
559 const int offset = tid * K_QUANTS_PER_ITERATION; // 0...15 or 0...14
560 const int in = offset/8; // 0 or 1
561 const int im = offset%8; // 0...7
562
563 for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
564
565 const float * y = yy + i * QK_K + offset;
566 const uint8_t * q = x[i].qs + offset;
567 const uint8_t * s = x[i].scales;
568
569 const float dall = (float)x[i].d;
570
571 float sum = 0;
572 for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
573 const uint8_t hl = x[i].hmask[im+l] >> in;
574 const uint8_t ql = q[l];
575 sum += y[l+ 0] * dall * ((s[0] & 0xF) - 8) * ((int8_t)((ql >> 0) & 3) - ((hl >> 0) & 1 ? 0 : 4))
576 + y[l+16] * dall * ((s[0] >> 4) - 8) * ((int8_t)((ql >> 2) & 3) - ((hl >> 2) & 1 ? 0 : 4))
577 + y[l+32] * dall * ((s[1] & 0xF) - 8) * ((int8_t)((ql >> 4) & 3) - ((hl >> 4) & 1 ? 0 : 4))
578 + y[l+48] * dall * ((s[1] >> 4) - 8) * ((int8_t)((ql >> 6) & 3) - ((hl >> 6) & 1 ? 0 : 4));
579 }
580 tmp += sum;
581 }
582#endif
583
584 // sum up partial sums and write back result
585#pragma unroll
586 for (int mask = WARP_SIZE / 2; mask > 0; mask >>= 1) {
587 tmp +=
588 dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
589 }
590
591 if (item_ct1.get_local_id(2) == 0) {
592 dst[row] = tmp;
593 }
594}
595
596static void dequantize_mul_mat_vec_q3_k_reorder(const void *__restrict__ vx,
597 const float *__restrict__ yy,
598 float *__restrict__ dst,
599 const int ncols, int nrows,
600 const sycl::nd_item<3> &item_ct1) {
601
602 const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
603 item_ct1.get_local_id(1);
604 if (row >= nrows) return;
605
606 const int num_blocks_per_row = ncols / QK_K;
607 const int ib0 = row*num_blocks_per_row;
608
609 // SOA base pointers for the reordered layout:
610 // [qs: nb * (QK_K/4)] [hmask: nb * (QK_K/8)] [scales: nb * 12] [d: nb * sizeof(half)]
611 const int nb = nrows * num_blocks_per_row;
612 const uint8_t * qs_base = (const uint8_t *)vx;
613 const uint8_t * hmask_base = qs_base + (size_t)nb * (QK_K / 4);
614 const uint8_t * scales_base = hmask_base + (size_t)nb * (QK_K / 8);
615 const sycl::half * d_base = (const sycl::half *)(scales_base + (size_t)nb * 12);
616
617 float tmp = 0; // partial sum for thread in warp
618
619#if QK_K == 256
620
621 const uint16_t kmask1 = 0x0303;
622 const uint16_t kmask2 = 0x0f0f;
623
624 const int tid =
625 item_ct1.get_local_id(2) / K_QUANTS_PER_ITERATION; // 0...7 or 0...15
626 const int ix =
627 item_ct1.get_local_id(2) % K_QUANTS_PER_ITERATION; // 0 or 0,1
628
629 const int n = K_QUANTS_PER_ITERATION; // iterations in the inner loop
630 const int step = 16/K_QUANTS_PER_ITERATION;
631 const int in = tid % step; // 0...15 or 0...7
632
633 const int l0 = n*in; // 0...15 or 0...14 in steps of 2
634
635 uint16_t utmp[4];
636 const int8_t * s = (const int8_t *)utmp;
637
638 for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
639 const int bi = ib0 + i;
640
641 const uint8_t * h = hmask_base + bi * (QK_K / 8) + l0;
642
643 const float d = d_base[bi];
644
645 for (int im = 0; im < 2; ++im) {
646 const int q_offset = 32*im + l0;
647 const int y_offset = 128*im + l0;
648 const uint16_t s_shift = 4*im;
649 const uint8_t m = 1 << (4*im);
650
651 const float * y = yy + i * QK_K + y_offset;
652 const uint8_t * q = qs_base + bi * (QK_K / 4) + q_offset;
653
654 const uint16_t * a = (const uint16_t *)(scales_base + bi * 12);
655 utmp[0] = ((a[0] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 0)) & kmask1) << 4);
656 utmp[1] = ((a[1] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 0)) & kmask1) << 4);
657 utmp[2] = ((a[2] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 2)) & kmask1) << 4);
658 utmp[3] = ((a[3] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 2)) & kmask1) << 4);
659
660 float sum = 0;
661 for (int l = 0; l < n; ++l) {
662 sum += y[l+ 0] * (s[0] - 32) * (((q[l] >> 0) & 3) - (h[l] & (m << 0) ? 0 : 4))
663 + y[l+32] * (s[2] - 32) * (((q[l] >> 2) & 3) - (h[l] & (m << 1) ? 0 : 4))
664 + y[l+64] * (s[4] - 32) * (((q[l] >> 4) & 3) - (h[l] & (m << 2) ? 0 : 4))
665 + y[l+96] * (s[6] - 32) * (((q[l] >> 6) & 3) - (h[l] & (m << 3) ? 0 : 4));
666 sum += y[l+16] * (s[1] - 32) * (((q[l+16] >> 0) & 3) - (h[l+16] & (m << 0) ? 0 : 4))
667 + y[l+48] * (s[3] - 32) * (((q[l+16] >> 2) & 3) - (h[l+16] & (m << 1) ? 0 : 4))
668 + y[l+80] * (s[5] - 32) * (((q[l+16] >> 4) & 3) - (h[l+16] & (m << 2) ? 0 : 4))
669 + y[l+112] * (s[7] - 32) * (((q[l+16] >> 6) & 3) - (h[l+16] & (m << 3) ? 0 : 4));
670 }
671 tmp += d * sum;
672 }
673 }
674#else
675 GGML_UNUSED(vx);
676 GGML_UNUSED(yy);
677 GGML_UNUSED(ncols);
678 GGML_UNUSED(item_ct1);
679 GGML_ABORT("Q3_K reorder DMMV not supported for QK_K != 256");
680#endif
681
682 // sum up partial sums and write back result
683#pragma unroll
684 for (int mask = WARP_SIZE / 2; mask > 0; mask >>= 1) {
685 tmp +=
686 dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
687 }
688
689 if (item_ct1.get_local_id(2) == 0) {
690 dst[row] = tmp;
691 }
692}
693
694static void dequantize_mul_mat_vec_q4_k(const void *__restrict__ vx,
695 const float *__restrict__ yy,
696 float *__restrict__ dst,
697 const int ncols, int nrows,
698 const sycl::nd_item<3> &item_ct1) {
699
700 const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
701 item_ct1.get_local_id(1);
702 if (row >= nrows) return;
703 const int num_blocks_per_row = ncols / QK_K;
704 const int ib0 = row*num_blocks_per_row;
705
706 const block_q4_K * x = (const block_q4_K *)vx + ib0;
707
708#if QK_K == 256
709 const uint16_t kmask1 = 0x3f3f;
710 const uint16_t kmask2 = 0x0f0f;
711 const uint16_t kmask3 = 0xc0c0;
712
713 const int tid =
714 item_ct1.get_local_id(2) / K_QUANTS_PER_ITERATION; // 0...7 or 0...15
715 const int ix =
716 item_ct1.get_local_id(2) % K_QUANTS_PER_ITERATION; // 0 or 0,1
717
718 const int step = 8/K_QUANTS_PER_ITERATION; // 8 or 4
719
720 const int il_base = tid/step; // 0 or 1 (was 0...3)
721 const int ir = tid - step*il_base; // 0...7 or 0...3
722 const int n = 2 * K_QUANTS_PER_ITERATION; // 2 or 4
723
724 const int in = il_base%2;
725
726 const int l0 = n*(2*ir + in);
727
728 uint16_t aux[4];
729 const uint8_t * sc = (const uint8_t *)aux;
730
731#if K_QUANTS_PER_ITERATION == 2
732 uint32_t q32[4];
733 const uint8_t * q4 = (const uint8_t *)q32;
734#else
735 uint16_t q16[4];
736 const uint8_t * q4 = (const uint8_t *)q16;
737#endif
738
739 float tmp = 0; // partial sum for thread in warp
740
741 for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
742
743 const float dall = x[i].dm[0];
744 const float dmin = x[i].dm[1];
745
746 for (int im = 0; im < 2; ++im) {
747 const int q_offset = 32*im + l0;
748 const int y_offset = 64*im + l0;
749
750 const float * y1 = yy + i*QK_K + y_offset;
751 const float * y2 = y1 + 128;
752
753 const uint16_t * a = (const uint16_t *)x[i].scales;
754 aux[0] = a[im+0] & kmask1;
755 aux[1] = a[im+2] & kmask1;
756 aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
757 aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
758
759#if K_QUANTS_PER_ITERATION == 2
760 const uint32_t * q1 = (const uint32_t *)(x[i].qs + q_offset);
761 const uint32_t * q2 = q1 + 16;
762
763 q32[0] = q1[0] & 0x0f0f0f0f;
764 q32[1] = q1[0] & 0xf0f0f0f0;
765 q32[2] = q2[0] & 0x0f0f0f0f;
766 q32[3] = q2[0] & 0xf0f0f0f0;
767
768 sycl::float4 s = {0.f, 0.f, 0.f, 0.f};
769 float smin = 0;
770 for (int l = 0; l < 4; ++l) {
771 s.x() += y1[l] * q4[l + 0]; s.y() += y1[l + 32] * q4[l + 4];
772 s.z() += y2[l] * q4[l + 8]; s.w() += y2[l + 32] * q4[l + 12];
773 smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
774 }
775 tmp += dall * (s.x() * sc[0] + s.y() * sc[1] * 1.f / 16.f +
776 s.z() * sc[4] + s.w() * sc[5] * 1.f / 16.f) -
777 dmin * smin;
778#else
779 const uint16_t * q1 = (const uint16_t *)(x[i].qs + q_offset);
780 const uint16_t * q2 = q1 + 32;
781
782 q16[0] = q1[0] & 0x0f0f;
783 q16[1] = q1[0] & 0xf0f0;
784 q16[2] = q2[0] & 0x0f0f;
785 q16[3] = q2[0] & 0xf0f0;
786
787 sycl::float4 s = {0.f, 0.f, 0.f, 0.f};
788 float smin = 0;
789 for (int l = 0; l < 2; ++l) {
790 s.x() += y1[l] * q4[l+0]; s.y() += y1[l+32] * q4[l+2];
791 s.z() += y2[l] * q4[l+4]; s.w() += y2[l+32] * q4[l+6];
792 smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
793 }
794 tmp += dall * (s.x() * sc[0] + s.y() * sc[1] * 1.f/16.f + s.z() * sc[4] + s.w() * sc[5] * 1.f/16.f) - dmin * smin;
795#endif
796 }
797
798 }
799#else
800 const int tid = item_ct1.get_local_id(2)/(2*K_QUANTS_PER_ITERATION); // 0...15
801 const int ix = item_ct1.get_local_id(2)%(2*K_QUANTS_PER_ITERATION);
802
803 const int step = tid * K_QUANTS_PER_ITERATION;
804
805 uint16_t aux16[2];
806 const uint8_t * s = (const uint8_t *)aux16;
807
808 float tmp = 0;
809
810 for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
811 const uint8_t * q = x[i].qs + step;
812 const float * y = yy + i*QK_K + step;
813 const uint16_t * a = (const uint16_t *)x[i].scales;
814 aux16[0] = a[0] & 0x0f0f;
815 aux16[1] = (a[0] >> 4) & 0x0f0f;
816 const float d = (float)x[i].dm[0];
817 const float m = (float)x[i].dm[1];
818 float sum = 0.f;
819 for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
820 sum += y[j+ 0] * (d * s[0] * (q[j+ 0] & 0xF) - m * s[2])
821 + y[j+16] * (d * s[0] * (q[j+16] & 0xF) - m * s[2])
822 + y[j+32] * (d * s[1] * (q[j+ 0] >> 4) - m * s[3])
823 + y[j+48] * (d * s[1] * (q[j+16] >> 4) - m * s[3]);
824 }
825 tmp += sum;
826 }
827
828#endif
829
830 // sum up partial sums and write back result
831#pragma unroll
832 for (int mask = WARP_SIZE / 2; mask > 0; mask >>= 1) {
833 tmp +=
834 dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
835 }
836
837 if (tid == 0) {
838 dst[row] = tmp;
839 }
840}
841
842static void dequantize_mul_mat_vec_q4_k_reorder(const void *__restrict__ vx,
843 const float *__restrict__ yy,
844 float *__restrict__ dst,
845 const int ncols, int nrows,
846 const sycl::nd_item<3> &item_ct1) {
847
848 const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
849 item_ct1.get_local_id(1);
850 if (row >= nrows) return;
851 const int num_blocks_per_row = ncols / QK_K;
852 const int ib0 = row*num_blocks_per_row;
853
854 // SOA base pointers for the reordered layout:
855 // [qs: nb * QK_K/2] [scales: nb * K_SCALE_SIZE] [dm: nb * sizeof(half2)]
856 const int nb = nrows * num_blocks_per_row;
857 const uint8_t * qs_base = (const uint8_t *)vx;
858 const uint8_t * scales_base = qs_base + (size_t)nb * (QK_K / 2);
859 const sycl::half2 * dm_base = (const sycl::half2 *)(scales_base + (size_t)nb * K_SCALE_SIZE);
860
861#if QK_K == 256
862 const uint16_t kmask1 = 0x3f3f;
863 const uint16_t kmask2 = 0x0f0f;
864 const uint16_t kmask3 = 0xc0c0;
865
866 const int tid =
867 item_ct1.get_local_id(2) / K_QUANTS_PER_ITERATION; // 0...7 or 0...15
868 const int ix =
869 item_ct1.get_local_id(2) % K_QUANTS_PER_ITERATION; // 0 or 0,1
870
871 const int step = 8/K_QUANTS_PER_ITERATION; // 8 or 4
872
873 const int il_base = tid/step; // 0 or 1 (was 0...3)
874 const int ir = tid - step*il_base; // 0...7 or 0...3
875 const int n = 2 * K_QUANTS_PER_ITERATION; // 2 or 4
876
877 const int in = il_base%2;
878
879 const int l0 = n*(2*ir + in);
880
881 uint16_t aux[4];
882 const uint8_t * sc = (const uint8_t *)aux;
883
884#if K_QUANTS_PER_ITERATION == 2
885 uint32_t q32[4];
886 const uint8_t * q4 = (const uint8_t *)q32;
887#else
888 uint16_t q16[4];
889 const uint8_t * q4 = (const uint8_t *)q16;
890#endif
891
892 float tmp = 0; // partial sum for thread in warp
893
894 for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
895 const int bi = ib0 + i;
896
897 const sycl::half2 dm_val = dm_base[bi];
898 const float dall = dm_val[0];
899 const float dmin = dm_val[1];
900
901 for (int im = 0; im < 2; ++im) {
902 const int q_offset = 32*im + l0;
903 const int y_offset = 64*im + l0;
904
905 const float * y1 = yy + i*QK_K + y_offset;
906 const float * y2 = y1 + 128;
907
908 const uint16_t * a = (const uint16_t *)(scales_base + bi * K_SCALE_SIZE);
909 aux[0] = a[im+0] & kmask1;
910 aux[1] = a[im+2] & kmask1;
911 aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
912 aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
913
914#if K_QUANTS_PER_ITERATION == 2
915 const uint32_t * q1 = (const uint32_t *)(qs_base + bi * (QK_K / 2) + q_offset);
916 const uint32_t * q2 = q1 + 16;
917
918 q32[0] = q1[0] & 0x0f0f0f0f;
919 q32[1] = q1[0] & 0xf0f0f0f0;
920 q32[2] = q2[0] & 0x0f0f0f0f;
921 q32[3] = q2[0] & 0xf0f0f0f0;
922
923 sycl::float4 s = {0.f, 0.f, 0.f, 0.f};
924 float smin = 0;
925 for (int l = 0; l < 4; ++l) {
926 s.x() += y1[l] * q4[l + 0]; s.y() += y1[l + 32] * q4[l + 4];
927 s.z() += y2[l] * q4[l + 8]; s.w() += y2[l + 32] * q4[l + 12];
928 smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
929 }
930 tmp += dall * (s.x() * sc[0] + s.y() * sc[1] * 1.f / 16.f +
931 s.z() * sc[4] + s.w() * sc[5] * 1.f / 16.f) -
932 dmin * smin;
933#else
934 const uint16_t * q1 = (const uint16_t *)(qs_base + bi * (QK_K / 2) + q_offset);
935 const uint16_t * q2 = q1 + 32;
936
937 q16[0] = q1[0] & 0x0f0f;
938 q16[1] = q1[0] & 0xf0f0;
939 q16[2] = q2[0] & 0x0f0f;
940 q16[3] = q2[0] & 0xf0f0;
941
942 sycl::float4 s = {0.f, 0.f, 0.f, 0.f};
943 float smin = 0;
944 for (int l = 0; l < 2; ++l) {
945 s.x() += y1[l] * q4[l+0]; s.y() += y1[l+32] * q4[l+2];
946 s.z() += y2[l] * q4[l+4]; s.w() += y2[l+32] * q4[l+6];
947 smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
948 }
949 tmp += dall * (s.x() * sc[0] + s.y() * sc[1] * 1.f/16.f + s.z() * sc[4] + s.w() * sc[5] * 1.f/16.f) - dmin * smin;
950#endif
951 }
952
953 }
954#else
955 const int tid = item_ct1.get_local_id(2)/(2*K_QUANTS_PER_ITERATION); // 0...15
956 const int ix = item_ct1.get_local_id(2)%(2*K_QUANTS_PER_ITERATION);
957
958 const int step = tid * K_QUANTS_PER_ITERATION;
959
960 uint16_t aux16[2];
961 const uint8_t * s = (const uint8_t *)aux16;
962
963 float tmp = 0;
964
965 for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
966 const int bi = ib0 + i;
967
968 const uint8_t * q = qs_base + bi * (QK_K / 2) + step;
969 const float * y = yy + i*QK_K + step;
970 const uint16_t * a = (const uint16_t *)(scales_base + bi * K_SCALE_SIZE);
971 aux16[0] = a[0] & 0x0f0f;
972 aux16[1] = (a[0] >> 4) & 0x0f0f;
973 const sycl::half2 dm_val = dm_base[bi];
974 const float d = (float)dm_val[0];
975 const float m = (float)dm_val[1];
976 float sum = 0.f;
977 for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
978 sum += y[j+ 0] * (d * s[0] * (q[j+ 0] & 0xF) - m * s[2])
979 + y[j+16] * (d * s[0] * (q[j+16] & 0xF) - m * s[2])
980 + y[j+32] * (d * s[1] * (q[j+ 0] >> 4) - m * s[3])
981 + y[j+48] * (d * s[1] * (q[j+16] >> 4) - m * s[3]);
982 }
983 tmp += sum;
984 }
985
986#endif
987
988 // sum up partial sums and write back result
989#pragma unroll
990 for (int mask = WARP_SIZE / 2; mask > 0; mask >>= 1) {
991 tmp +=
992 dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
993 }
994
995 if (tid == 0) {
996 dst[row] = tmp;
997 }
998}
999
1000static void dequantize_mul_mat_vec_q5_k(const void *__restrict__ vx,
1001 const float *__restrict__ yy,
1002 float *__restrict__ dst,
1003 const int ncols, int nrows,
1004 const sycl::nd_item<3> &item_ct1) {
1005
1006 const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
1007 item_ct1.get_local_id(1);
1008 if (row >= nrows) return;
1009 const int num_blocks_per_row = ncols / QK_K;
1010 const int ib0 = row*num_blocks_per_row;
1011
1012 const block_q5_K * x = (const block_q5_K *)vx + ib0;
1013
1014 float tmp = 0; // partial sum for thread in warp
1015
1016#if QK_K == 256
1017 const uint16_t kmask1 = 0x3f3f;
1018 const uint16_t kmask2 = 0x0f0f;
1019 const uint16_t kmask3 = 0xc0c0;
1020
1021 const int tid = item_ct1.get_local_id(2) / 2; // 0...7
1022 const int ix = item_ct1.get_local_id(2) % 2;
1023
1024 const int il_base = tid/4; // 0 or 1 (was 0...3)
1025 const int ir = tid - 4*il_base;// 0...3
1026 const int n = 2;
1027
1028 const int in = il_base%2;
1029
1030 const int l0 = n*(2*ir + in);
1031
1032 uint16_t aux[4];
1033 const uint8_t * sc = (const uint8_t *)aux;
1034
1035 uint16_t q16[8];
1036 const uint8_t * q4 = (const uint8_t *)q16;
1037
1038 for (int i = ix; i < num_blocks_per_row; i += 2) {
1039
1040 const uint8_t * qh = x[i].qh + l0;
1041 const float dall = x[i].dm[0];
1042 const float dmin = x[i].dm[1];
1043
1044 for (int im = 0; im < 2; ++im) {
1045 const int q_offset = 32*im + l0;
1046 const int y_offset = 64*im + l0;
1047
1048 const uint8_t hm1 = 1 << (2*im);
1049 const uint8_t hm2 = hm1 << 4;
1050
1051 const uint8_t * ql1 = x[i].qs + q_offset;
1052 const float * y1 = yy + i*QK_K + y_offset;
1053 const float * y2 = y1 + 128;
1054
1055 const uint16_t * a = (const uint16_t *)x[i].scales;
1056 aux[0] = a[im+0] & kmask1;
1057 aux[1] = a[im+2] & kmask1;
1058 aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
1059 aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
1060
1061 sycl::float4 sum = {0.f, 0.f, 0.f, 0.f};
1062 float smin = 0;
1063 const uint16_t * q1 = (const uint16_t *)ql1;
1064 const uint16_t * q2 = q1 + 32;
1065 q16[0] = q1[0] & 0x0f0f;
1066 q16[1] = q1[8] & 0x0f0f;
1067 q16[2] = (q1[0] >> 4) & 0x0f0f;
1068 q16[3] = (q1[8] >> 4) & 0x0f0f;
1069 q16[4] = q2[0] & 0x0f0f;
1070 q16[5] = q2[8] & 0x0f0f;
1071 q16[6] = (q2[0] >> 4) & 0x0f0f;
1072 q16[7] = (q2[8] >> 4) & 0x0f0f;
1073 for (int l = 0; l < n; ++l) {
1074 sum.x() +=
1075 y1[l + 0] * (q4[l + 0] + (qh[l + 0] & (hm1 << 0) ? 16 : 0)) +
1076 y1[l + 16] * (q4[l + 2] + (qh[l + 16] & (hm1 << 0) ? 16 : 0));
1077 sum.y() +=
1078 y1[l + 32] * (q4[l + 4] + (qh[l + 0] & (hm1 << 1) ? 16 : 0)) +
1079 y1[l + 48] * (q4[l + 6] + (qh[l + 16] & (hm1 << 1) ? 16 : 0));
1080 sum.z() +=
1081 y2[l + 0] * (q4[l + 8] + (qh[l + 0] & (hm2 << 0) ? 16 : 0)) +
1082 y2[l + 16] * (q4[l + 10] + (qh[l + 16] & (hm2 << 0) ? 16 : 0));
1083 sum.w() +=
1084 y2[l + 32] * (q4[l + 12] + (qh[l + 0] & (hm2 << 1) ? 16 : 0)) +
1085 y2[l + 48] * (q4[l + 14] + (qh[l + 16] & (hm2 << 1) ? 16 : 0));
1086 smin += (y1[l] + y1[l+16]) * sc[2] + (y1[l+32] + y1[l+48]) * sc[3]
1087 + (y2[l] + y2[l+16]) * sc[6] + (y2[l+32] + y2[l+48]) * sc[7];
1088 }
1089 tmp += dall * (sum.x() * sc[0] + sum.y() * sc[1] + sum.z() * sc[4] +
1090 sum.w() * sc[5]) -
1091 dmin * smin;
1092 }
1093 }
1094
1095#else
1096 const int tid = item_ct1.get_local_id(2)/(2*K_QUANTS_PER_ITERATION); // 0...15
1097 const int ix = item_ct1.get_local_id(2)%(2*K_QUANTS_PER_ITERATION);
1098 const int step = tid * K_QUANTS_PER_ITERATION;
1099 const int im = step/8;
1100 const int in = step%8;
1101
1102 for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
1103 const uint8_t * q = x[i].qs + step;
1104 const int8_t * s = x[i].scales;
1105 const float * y = yy + i*QK_K + step;
1106 const float d = x[i].d;
1107 float sum = 0.f;
1108 for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
1109 const uint8_t h = x[i].qh[in+j] >> im;
1110 sum += y[j+ 0] * d * s[0] * ((q[j+ 0] & 0xF) - ((h >> 0) & 1 ? 0 : 16))
1111 + y[j+16] * d * s[1] * ((q[j+16] & 0xF) - ((h >> 2) & 1 ? 0 : 16))
1112 + y[j+32] * d * s[2] * ((q[j+ 0] >> 4) - ((h >> 4) & 1 ? 0 : 16))
1113 + y[j+48] * d * s[3] * ((q[j+16] >> 4) - ((h >> 6) & 1 ? 0 : 16));
1114 }
1115 tmp += sum;
1116 }
1117#endif
1118
1119 // sum up partial sums and write back result
1120#pragma unroll
1121 for (int mask = WARP_SIZE / 2; mask > 0; mask >>= 1) {
1122 tmp +=
1123 dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
1124 }
1125
1126 if (item_ct1.get_local_id(2) == 0) {
1127 dst[row] = tmp;
1128 }
1129}
1130
1131static void dequantize_mul_mat_vec_q5_k_reorder(const void *__restrict__ vx,
1132 const float *__restrict__ yy,
1133 float *__restrict__ dst,
1134 const int ncols, int nrows,
1135 const sycl::nd_item<3> &item_ct1) {
1136
1137 const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
1138 item_ct1.get_local_id(1);
1139 if (row >= nrows) return;
1140 const int num_blocks_per_row = ncols / QK_K;
1141 const int ib0 = row*num_blocks_per_row;
1142
1143 // SOA base pointers for the reordered layout:
1144 // [qs: nb * QK_K/2] [qh: nb * QK_K/8] [scales: nb * K_SCALE_SIZE] [dm: nb * sizeof(half2)]
1145 const int nb = nrows * num_blocks_per_row;
1146 const uint8_t * qs_base = (const uint8_t *)vx;
1147 const uint8_t * qh_base = qs_base + (size_t)nb * (QK_K / 2);
1148 const uint8_t * scales_base = qh_base + (size_t)nb * (QK_K / 8);
1149 const sycl::half2 * dm_base = (const sycl::half2 *)(scales_base + (size_t)nb * K_SCALE_SIZE);
1150
1151 float tmp = 0; // partial sum for thread in warp
1152
1153#if QK_K == 256
1154 const uint16_t kmask1 = 0x3f3f;
1155 const uint16_t kmask2 = 0x0f0f;
1156 const uint16_t kmask3 = 0xc0c0;
1157
1158 const int tid = item_ct1.get_local_id(2) / 2; // 0...15
1159 const int ix = item_ct1.get_local_id(2) % 2;
1160
1161 const int il_base = tid/4; // 0...3
1162 const int ir = tid - 4*il_base;// 0...3
1163 const int n = 2;
1164
1165 const int in = il_base%2;
1166
1167 const int l0 = n*(2*ir + in);
1168
1169 uint16_t aux[4];
1170 const uint8_t * sc = (const uint8_t *)aux;
1171
1172 uint16_t q16[8];
1173 const uint8_t * q4 = (const uint8_t *)q16;
1174
1175 for (int i = ix; i < num_blocks_per_row; i += 2) {
1176 const int bi = ib0 + i;
1177
1178 const uint8_t * qh = qh_base + bi * (QK_K / 8) + l0;
1179 const sycl::half2 dm_val = dm_base[bi];
1180 const float dall = dm_val[0];
1181 const float dmin = dm_val[1];
1182
1183 for (int im = 0; im < 2; ++im) {
1184 const int q_offset = 32*im + l0;
1185 const int y_offset = 64*im + l0;
1186
1187 const uint8_t hm1 = 1 << (2*im);
1188 const uint8_t hm2 = hm1 << 4;
1189
1190 const uint8_t * ql1 = qs_base + bi * (QK_K / 2) + q_offset;
1191 const float * y1 = yy + i*QK_K + y_offset;
1192 const float * y2 = y1 + 128;
1193
1194 const uint16_t * a = (const uint16_t *)(scales_base + bi * K_SCALE_SIZE);
1195 aux[0] = a[im+0] & kmask1;
1196 aux[1] = a[im+2] & kmask1;
1197 aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
1198 aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
1199
1200 sycl::float4 sum = {0.f, 0.f, 0.f, 0.f};
1201 float smin = 0;
1202 const uint16_t * q1 = (const uint16_t *)ql1;
1203 const uint16_t * q2 = q1 + 32;
1204 q16[0] = q1[0] & 0x0f0f;
1205 q16[1] = q1[8] & 0x0f0f;
1206 q16[2] = (q1[0] >> 4) & 0x0f0f;
1207 q16[3] = (q1[8] >> 4) & 0x0f0f;
1208 q16[4] = q2[0] & 0x0f0f;
1209 q16[5] = q2[8] & 0x0f0f;
1210 q16[6] = (q2[0] >> 4) & 0x0f0f;
1211 q16[7] = (q2[8] >> 4) & 0x0f0f;
1212 for (int l = 0; l < n; ++l) {
1213 sum.x() +=
1214 y1[l + 0] * (q4[l + 0] + (qh[l + 0] & (hm1 << 0) ? 16 : 0)) +
1215 y1[l + 16] * (q4[l + 2] + (qh[l + 16] & (hm1 << 0) ? 16 : 0));
1216 sum.y() +=
1217 y1[l + 32] * (q4[l + 4] + (qh[l + 0] & (hm1 << 1) ? 16 : 0)) +
1218 y1[l + 48] * (q4[l + 6] + (qh[l + 16] & (hm1 << 1) ? 16 : 0));
1219 sum.z() +=
1220 y2[l + 0] * (q4[l + 8] + (qh[l + 0] & (hm2 << 0) ? 16 : 0)) +
1221 y2[l + 16] * (q4[l + 10] + (qh[l + 16] & (hm2 << 0) ? 16 : 0));
1222 sum.w() +=
1223 y2[l + 32] * (q4[l + 12] + (qh[l + 0] & (hm2 << 1) ? 16 : 0)) +
1224 y2[l + 48] * (q4[l + 14] + (qh[l + 16] & (hm2 << 1) ? 16 : 0));
1225 smin += (y1[l] + y1[l+16]) * sc[2] + (y1[l+32] + y1[l+48]) * sc[3]
1226 + (y2[l] + y2[l+16]) * sc[6] + (y2[l+32] + y2[l+48]) * sc[7];
1227 }
1228 tmp += dall * (sum.x() * sc[0] + sum.y() * sc[1] + sum.z() * sc[4] +
1229 sum.w() * sc[5]) -
1230 dmin * smin;
1231 }
1232 }
1233#else
1234 // The reordered Q5_K layout is only produced for QK_K == 256.
1235#endif
1236
1237 // sum up partial sums and write back result
1238#pragma unroll
1239 for (int mask = WARP_SIZE / 2; mask > 0; mask >>= 1) {
1240 tmp +=
1241 dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
1242 }
1243
1244 if (item_ct1.get_local_id(2) == 0) {
1245 dst[row] = tmp;
1246 }
1247}
1248
1249static void dequantize_mul_mat_vec_q6_k(const void * __restrict__ vx, const float * __restrict__ yy, float * __restrict__ dst, const int ncols, int nrows,
1250 const sycl::nd_item<3> &item_ct1) {
1251
1252 static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
1253
1254 const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
1255 item_ct1.get_local_id(1);
1256 if (row >= nrows) return;
1257
1258 const int num_blocks_per_row = ncols / QK_K;
1259 const int ib0 = row*num_blocks_per_row;
1260
1261 const block_q6_K * x = (const block_q6_K *)vx + ib0;
1262
1263#if QK_K == 256
1264
1265 const int tid =
1266 item_ct1.get_local_id(2) / K_QUANTS_PER_ITERATION; // 0...7 or 0...15
1267 const int ix =
1268 item_ct1.get_local_id(2) % K_QUANTS_PER_ITERATION; // 0 or 0, 1
1269
1270 const int step = 16/K_QUANTS_PER_ITERATION; // 16 or 8
1271
1272 const int in = tid % step; // 0...15 or 0...7
1273
1274#if K_QUANTS_PER_ITERATION == 1
1275 const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15
1276 const int is = 0;
1277#else
1278 const int l0 = 4 * in; // 0, 4, 8, ..., 28
1279 const int is = in / 4;
1280#endif
1281
1282 float tmp = 0; // partial sum for thread in warp
1283
1284 for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
1285
1286 const float d = x[i].d;
1287
1288 for (int im = 0; im < 2; ++im) {
1289 const int ql_offset = 64*im + l0;
1290 const int qh_offset = 32*im + l0;
1291 const int s_offset = 8*im + is;
1292 const int y_offset = 128*im + l0;
1293
1294 const float * y = yy + i * QK_K + y_offset;
1295 const uint8_t * ql = x[i].ql + ql_offset;
1296 const uint8_t * qh = x[i].qh + qh_offset;
1297 const int8_t * s = x[i].scales + s_offset;
1298
1299#if K_QUANTS_PER_ITERATION == 1
1300 float sum = y[ 0] * s[0] * d * ((int8_t)((ql[ 0] & 0xF) | ((qh[ 0] & 0x03) << 4)) - 32)
1301 + y[16] * s[1] * d * ((int8_t)((ql[16] & 0xF) | ((qh[16] & 0x03) << 4)) - 32)
1302 + y[32] * s[2] * d * ((int8_t)((ql[32] & 0xF) | ((qh[ 0] & 0x0c) << 2)) - 32)
1303 + y[48] * s[3] * d * ((int8_t)((ql[48] & 0xF) | ((qh[16] & 0x0c) << 2)) - 32)
1304 + y[64] * s[4] * d * ((int8_t)((ql[ 0] >> 4) | ((qh[ 0] & 0x30) >> 0)) - 32)
1305 + y[80] * s[5] * d * ((int8_t)((ql[16] >> 4) | ((qh[16] & 0x30) >> 0)) - 32)
1306 + y[96] * s[6] * d * ((int8_t)((ql[32] >> 4) | ((qh[ 0] & 0xc0) >> 2)) - 32)
1307 +y[112] * s[7] * d * ((int8_t)((ql[48] >> 4) | ((qh[16] & 0xc0) >> 2)) - 32);
1308 tmp += sum;
1309#else
1310 float sum = 0;
1311 for (int l = 0; l < 4; ++l) {
1312 sum += y[l+ 0] * s[0] * d * ((int8_t)((ql[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32)
1313 + y[l+32] * s[2] * d * ((int8_t)((ql[l+32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32)
1314 + y[l+64] * s[4] * d * ((int8_t)((ql[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32)
1315 + y[l+96] * s[6] * d * ((int8_t)((ql[l+32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32);
1316 }
1317 tmp += sum;
1318#endif
1319 }
1320
1321 }
1322
1323#else
1324
1325 const int tid = item_ct1.get_local_id(2)/(2*K_QUANTS_PER_ITERATION); // 0...7
1326 const int ix = item_ct1.get_local_id(2)%(2*K_QUANTS_PER_ITERATION); // 0...3
1327
1328 const int step = tid * K_QUANTS_PER_ITERATION;
1329
1330 float tmp = 0; // partial sum for thread in warp
1331
1332 for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
1333
1334 const float * y = yy + i * QK_K + step;
1335 const uint8_t * ql = x[i].ql + step;
1336 const uint8_t * qh = x[i].qh + step;
1337 const int8_t * s = x[i].scales;
1338
1339 const float d = x[i+0].d;
1340
1341 float sum = 0;
1342 for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
1343 sum += y[j+ 0] * s[0] * d * ((int8_t)((ql[j+ 0] & 0xF) | ((qh[j] & 0x03) << 4)) - 32)
1344 + y[j+16] * s[1] * d * ((int8_t)((ql[j+16] & 0xF) | ((qh[j] & 0x0c) << 2)) - 32)
1345 + y[j+32] * s[2] * d * ((int8_t)((ql[j+ 0] >> 4) | ((qh[j] & 0x30) >> 0)) - 32)
1346 + y[j+48] * s[3] * d * ((int8_t)((ql[j+16] >> 4) | ((qh[j] & 0xc0) >> 2)) - 32);
1347 }
1348 tmp += sum;
1349
1350 }
1351
1352#endif
1353
1354 // sum up partial sums and write back result
1355#pragma unroll
1356 for (int mask = WARP_SIZE / 2; mask > 0; mask >>= 1) {
1357 tmp +=
1358 dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
1359 }
1360
1361 if (tid == 0) {
1362 dst[row] = tmp;
1363 }
1364}
1365
1366static void dequantize_mul_mat_vec_q6_k_reorder(const void * __restrict__ vx, const float * __restrict__ yy, float * __restrict__ dst, const int ncols, int nrows,
1367 const sycl::nd_item<3> &item_ct1) {
1368
1369 static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
1370
1371 const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
1372 item_ct1.get_local_id(1);
1373 if (row >= nrows) return;
1374
1375 const int num_blocks_per_row = ncols / QK_K;
1376 const int ib0 = row*num_blocks_per_row;
1377
1378 // SOA base pointers for the reordered layout:
1379 // [ql: nb * QK_K/2] [qh: nb * QK_K/4] [scales: nb * QK_K/16] [d: nb * sizeof(half)]
1380 const int nb = nrows * num_blocks_per_row;
1381 const uint8_t * ql_base = (const uint8_t *)vx;
1382 const uint8_t * qh_base = ql_base + (size_t)nb * (QK_K / 2);
1383 const int8_t * scales_base = (const int8_t *)(qh_base + (size_t)nb * (QK_K / 4));
1384 const sycl::half * d_base = (const sycl::half *)((const uint8_t *)scales_base + (size_t)nb * (QK_K / 16));
1385
1386#if QK_K == 256
1387
1388 const int tid =
1389 item_ct1.get_local_id(2) / K_QUANTS_PER_ITERATION; // 0...7 or 0...15
1390 const int ix =
1391 item_ct1.get_local_id(2) % K_QUANTS_PER_ITERATION; // 0 or 0, 1
1392
1393 const int step = 16/K_QUANTS_PER_ITERATION; // 16 or 8
1394
1395 const int in = tid % step; // 0...15 or 0...7
1396
1397#if K_QUANTS_PER_ITERATION == 1
1398 const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15
1399 const int is = 0;
1400#else
1401 const int l0 = 4 * in; // 0, 4, 8, ..., 28
1402 const int is = in / 4;
1403#endif
1404
1405 float tmp = 0; // partial sum for thread in warp
1406
1407 for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
1408 const int bi = ib0 + i;
1409
1410 const float d = d_base[bi];
1411
1412 for (int im = 0; im < 2; ++im) {
1413 const int ql_offset = 64*im + l0;
1414 const int qh_offset = 32*im + l0;
1415 const int s_offset = 8*im + is;
1416 const int y_offset = 128*im + l0;
1417
1418 const float * y = yy + i * QK_K + y_offset;
1419 const uint8_t * ql = ql_base + bi * (QK_K / 2) + ql_offset;
1420 const uint8_t * qh = qh_base + bi * (QK_K / 4) + qh_offset;
1421 const int8_t * s = scales_base + bi * (QK_K / 16) + s_offset;
1422
1423#if K_QUANTS_PER_ITERATION == 1
1424 float sum = y[ 0] * s[0] * d * ((int8_t)((ql[ 0] & 0xF) | ((qh[ 0] & 0x03) << 4)) - 32)
1425 + y[16] * s[1] * d * ((int8_t)((ql[16] & 0xF) | ((qh[16] & 0x03) << 4)) - 32)
1426 + y[32] * s[2] * d * ((int8_t)((ql[32] & 0xF) | ((qh[ 0] & 0x0c) << 2)) - 32)
1427 + y[48] * s[3] * d * ((int8_t)((ql[48] & 0xF) | ((qh[16] & 0x0c) << 2)) - 32)
1428 + y[64] * s[4] * d * ((int8_t)((ql[ 0] >> 4) | ((qh[ 0] & 0x30) >> 0)) - 32)
1429 + y[80] * s[5] * d * ((int8_t)((ql[16] >> 4) | ((qh[16] & 0x30) >> 0)) - 32)
1430 + y[96] * s[6] * d * ((int8_t)((ql[32] >> 4) | ((qh[ 0] & 0xc0) >> 2)) - 32)
1431 +y[112] * s[7] * d * ((int8_t)((ql[48] >> 4) | ((qh[16] & 0xc0) >> 2)) - 32);
1432 tmp += sum;
1433#else
1434 float sum = 0;
1435 for (int l = 0; l < 4; ++l) {
1436 sum += y[l+ 0] * s[0] * d * ((int8_t)((ql[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32)
1437 + y[l+32] * s[2] * d * ((int8_t)((ql[l+32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32)
1438 + y[l+64] * s[4] * d * ((int8_t)((ql[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32)
1439 + y[l+96] * s[6] * d * ((int8_t)((ql[l+32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32);
1440 }
1441 tmp += sum;
1442#endif
1443 }
1444
1445 }
1446
1447#else
1448
1449 const int tid = item_ct1.get_local_id(2)/(2*K_QUANTS_PER_ITERATION); // 0...7
1450 const int ix = item_ct1.get_local_id(2)%(2*K_QUANTS_PER_ITERATION); // 0...3
1451
1452 const int step = tid * K_QUANTS_PER_ITERATION;
1453
1454 float tmp = 0; // partial sum for thread in warp
1455
1456 for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
1457 const int bi = ib0 + i;
1458
1459 const float * y = yy + i * QK_K + step;
1460 const uint8_t * ql = ql_base + bi * (QK_K / 2) + step;
1461 const uint8_t * qh = qh_base + bi * (QK_K / 4) + step;
1462 const int8_t * s = scales_base + bi * (QK_K / 16);
1463
1464 const float d = d_base[bi];
1465
1466 float sum = 0;
1467 for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
1468 sum += y[j+ 0] * s[0] * d * ((int8_t)((ql[j+ 0] & 0xF) | ((qh[j] & 0x03) << 4)) - 32)
1469 + y[j+16] * s[1] * d * ((int8_t)((ql[j+16] & 0xF) | ((qh[j] & 0x0c) << 2)) - 32)
1470 + y[j+32] * s[2] * d * ((int8_t)((ql[j+ 0] >> 4) | ((qh[j] & 0x30) >> 0)) - 32)
1471 + y[j+48] * s[3] * d * ((int8_t)((ql[j+16] >> 4) | ((qh[j] & 0xc0) >> 2)) - 32);
1472 }
1473 tmp += sum;
1474
1475 }
1476
1477#endif
1478
1479 // sum up partial sums and write back result
1480#pragma unroll
1481 for (int mask = WARP_SIZE / 2; mask > 0; mask >>= 1) {
1482 tmp +=
1483 dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
1484 }
1485
1486 if (tid == 0) {
1487 dst[row] = tmp;
1488 }
1489}
1490
1491template <int qk, int qr, dequantize_kernel_t dequantize_kernel>
1492static void launch_dequantize_mul_mat_vec(const void *vx, const dfloat *y, float *dst,
1493 const int ncols, const int nrows,
1494 dpct::queue_ptr stream) {
1495 GGML_ASSERT(ncols % GGML_SYCL_DMMV_X == 0);
1496 const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
1497 // the number of rows may exceed maximum grid size in the y or z dimensions, use the x dimension instead
1498 const sycl::range<3> block_nums(1, 1, block_num_y);
1499 const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
1500 {
1501 dpct::has_capability_or_fail(stream->get_device(),
1502 {sycl::aspect::fp16});
1503
1504 stream->parallel_for(
1505 sycl::nd_range<3>(block_nums * block_dims, block_dims),
1506 [=](sycl::nd_item<3> item_ct1) [[sycl::reqd_sub_group_size(WARP_SIZE)]] {
1507 dequantize_mul_mat_vec<qk, qr, dequantize_kernel>(
1508 vx, y, dst, ncols, nrows, item_ct1);
1509 });
1510 }
1511}
1512
1513template <int qk, int qr, int qbytes, dequantize_kernel_t_reorder dequantize_kernel>
1514static void launch_dequantize_mul_mat_vec_reorder(const void *vx, const dfloat *y, float *dst,
1515 const int ncols, const int nrows,
1516 dpct::queue_ptr stream) {
1517 GGML_ASSERT(ncols % GGML_SYCL_DMMV_X == 0);
1518 const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
1519 // the number of rows may exceed maximum grid size in the y or z dimensions, use the x dimension instead
1520 const sycl::range<3> block_nums(1, 1, block_num_y);
1521 const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
1522 {
1523 dpct::has_capability_or_fail(stream->get_device(),
1524 {sycl::aspect::fp16});
1525
1526 stream->parallel_for(
1527 sycl::nd_range<3>(block_nums * block_dims, block_dims),
1528 [=](sycl::nd_item<3> item_ct1) [[sycl::reqd_sub_group_size(WARP_SIZE)]] {
1529 dequantize_mul_mat_vec_reorder<qk, qr, qbytes, dequantize_kernel>(
1530 vx, y, dst, ncols, nrows, item_ct1);
1531 });
1532 }
1533}
1534
1535template <int ny, auto kernel>
1536static void launch_dequantize_mul_mat_vec_k(const void *vx, const float *y, float *dst,
1537 const int ncols, const int nrows,
1538 dpct::queue_ptr stream) {
1539 GGML_ASSERT(ncols % QK_K == 0);
1540 const int block_num_y = (nrows + ny - 1) / ny;
1541 const sycl::range<3> block_nums(1, 1, block_num_y);
1542 const sycl::range<3> block_dims(1, ny, WARP_SIZE);
1543 stream->parallel_for(
1544 sycl::nd_range<3>(block_nums * block_dims, block_dims),
1545 [=](sycl::nd_item<3> item_ct1) [[sycl::reqd_sub_group_size(WARP_SIZE)]] {
1546 kernel(vx, y, dst, ncols, nrows, item_ct1);
1547 });
1548}
1549
1550#ifdef GGML_SYCL_DMMV_HAS_ESIMD
1551using ggml_sycl_esimd::GGML_SYCL_DMMV_ESIMD_WG_SIZE;
1552
1553// policies for dequantize_mul_mat_vec_reorder_esimd: row mapping and what to do with the two sums
1554// plain mat-vec: a work-group owns two consecutive rows of one matrix
1555struct esimd_dmmv_row_pair_policy {
1556 const void * vx;
1557 float * dst;
1558
1559 ESIMD_INLINE const void * w_a() const { return vx; }
1560 ESIMD_INLINE const void * w_b() const { return vx; }
1561
1562 ESIMD_INLINE int row_a(int g) const { return g * 2; }
1563 ESIMD_INLINE int row_b(int g) const { return g * 2 + 1; }
1564 ESIMD_INLINE bool has_b(int g, int nrows) const { return row_b(g) < nrows; }
1565
1566 ESIMD_INLINE void epilogue(int g, int nrows, float sum_a, float sum_b) const {
1567 dst[row_a(g)] = sum_a;
1568 if (has_b(g, nrows)) {
1569 dst[row_b(g)] = sum_b;
1570 }
1571 }
1572};
1573
1574// fused gate+up+GLU: a work-group owns one row of both matrices; a is the gate, b the up
1575struct esimd_dmmv_glu_policy {
1576 const void * vgate;
1577 const void * vup;
1578 float * dst;
1579 ggml_glu_op glu_op;
1580
1581 ESIMD_INLINE const void * w_a() const { return vgate; }
1582 ESIMD_INLINE const void * w_b() const { return vup; }
1583
1584 ESIMD_INLINE int row_a(int g) const { return g; }
1585 ESIMD_INLINE int row_b(int g) const { return g; }
1586 ESIMD_INLINE bool has_b(int, int) const { return true; } // up always contributes
1587
1588 ESIMD_INLINE void epilogue(int g, int, float gate, float up) const {
1589 using namespace sycl::ext::intel::esimd;
1590
1591 // SwiGLU: silu(gate)*up = gate*sigmoid(gate)*up. GEGLU's tanh form reduces
1592 // to the same shape via tanh(y) = 2*sigmoid(2y) - 1, so only the sigmoid
1593 // argument differs. sigmoid via exp2 (width-1 simd is a degenerate case for
1594 // the EM pipe, so broadcast to 16 lanes); lane 0 holds the result.
1595 const float GELU_COEF_A = 0.044715f;
1596 const float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
1597 const float NEG_LOG2E = -1.44269504088896341f;
1598
1599 const float arg = (glu_op == GGML_GLU_OP_GEGLU) ?
1600 2.0f * SQRT_2_OVER_PI * gate * (1.0f + GELU_COEF_A * gate * gate) :
1601 gate;
1602
1603 simd<float, 16> t = arg * NEG_LOG2E;
1604 simd<float, 16> e = exp2(t);
1605 dst[g] = gate / (1.0f + e[0]) * up;
1606 }
1607};
1608
1609// generic reordered dequantize-matvec: each work-group owns a pair of rows chosen by the policy
1610// and updates one 32-wide accumulator per row
1611template <ggml_type T, typename Policy>
1612ESIMD_INLINE void dequantize_mul_mat_vec_reorder_esimd(
1613 const Policy & policy, const float * y,
1614 const int ncols, const int nrows,
1615 sycl::local_accessor<float, 1> lmem,
1616 const sycl::nd_item<1> & it) {
1617 using namespace sycl::ext::intel::esimd;
1618 using traits = ggml_sycl_esimd::esimd_reorder_q_traits<T>;
1619
1620 const int num_blocks_per_row = ncols / QK_K;
1621 const size_t nb = (size_t) nrows * num_blocks_per_row;
1622 const auto pa = traits::make_ptrs(policy.w_a(), nb);
1623 const auto pb = traits::make_ptrs(policy.w_b(), nb);
1624
1625 const int tid = it.get_local_id(0);
1626 const int g = it.get_group(0);
1627 const int row_a = policy.row_a(g);
1628 const int row_b = policy.row_b(g);
1629 const bool has_b = policy.has_b(g, nrows);
1630
1631 // one 32-wide accumulator per row (small footprint, no spill)
1632 simd<float, 32> acc_a = 0.0f;
1633 simd<float, 32> acc_b = 0.0f;
1634
1635 for (int ib = tid; ib < num_blocks_per_row; ib += GGML_SYCL_DMMV_ESIMD_WG_SIZE) {
1636 simd<float, 256> y_vec = block_load<float, 256>(y + (size_t) ib * QK_K);
1637
1638 const size_t bi_a = (size_t) row_a * num_blocks_per_row + ib;
1639 const size_t bi_b = (size_t) row_b * num_blocks_per_row + ib;
1640
1641 traits::mac_pair(pa, bi_a, pb, bi_b, has_b, y_vec, acc_a, acc_b);
1642 }
1643
1644 lmem[tid * 2 + 0] = reduce<float>(acc_a, std::plus<>{});
1645 lmem[tid * 2 + 1] = reduce<float>(acc_b, std::plus<>{});
1646 it.barrier(sycl::access::fence_space::local_space);
1647
1648 if (tid == 0) {
1649 float sum_a = 0.0f;
1650 float sum_b = 0.0f;
1651 for (int p = 0; p < GGML_SYCL_DMMV_ESIMD_WG_SIZE; ++p) {
1652 sum_a += lmem[p * 2 + 0];
1653 sum_b += lmem[p * 2 + 1];
1654 }
1655
1656 policy.epilogue(g, nrows, sum_a, sum_b);
1657 }
1658}
1659
1660static void dequantize_mul_mat_vec_q2_K_sycl_reorder_esimd(const void *vx, const float *y,
1661 float *dst, const int ncols,
1662 const int nrows,
1663 dpct::queue_ptr stream) {
1664 GGML_ASSERT(ncols % QK_K == 0);
1665 const int workgroups = (nrows + 1) / 2;
1666 stream->submit([&](sycl::handler &h) {
1667 sycl::local_accessor<float, 1> lmem(sycl::range<1>(GGML_SYCL_DMMV_ESIMD_WG_SIZE * 2), h);
1668 h.parallel_for(
1669 sycl::nd_range<1>(sycl::range<1>((size_t)workgroups * GGML_SYCL_DMMV_ESIMD_WG_SIZE), sycl::range<1>(GGML_SYCL_DMMV_ESIMD_WG_SIZE)),
1670 [=](sycl::nd_item<1> it) [[intel::sycl_explicit_simd]] {
1671 dequantize_mul_mat_vec_reorder_esimd<GGML_TYPE_Q2_K>(
1672 esimd_dmmv_row_pair_policy{ vx, dst }, y, ncols, nrows, lmem, it);
1673 });
1674 });
1675}
1676
1677static void dequantize_mul_mat_vec_q3_K_sycl_reorder_esimd(const void *vx, const float *y,
1678 float *dst, const int ncols,
1679 const int nrows,
1680 dpct::queue_ptr stream) {
1681 GGML_ASSERT(ncols % QK_K == 0);
1682 const int workgroups = (nrows + 1) / 2;
1683 stream->submit([&](sycl::handler &h) {
1684 sycl::local_accessor<float, 1> lmem(sycl::range<1>(GGML_SYCL_DMMV_ESIMD_WG_SIZE * 2), h);
1685 h.parallel_for(
1686 sycl::nd_range<1>(sycl::range<1>((size_t)workgroups * GGML_SYCL_DMMV_ESIMD_WG_SIZE), sycl::range<1>(GGML_SYCL_DMMV_ESIMD_WG_SIZE)),
1687 [=](sycl::nd_item<1> it) [[intel::sycl_explicit_simd]] {
1688 dequantize_mul_mat_vec_reorder_esimd<GGML_TYPE_Q3_K>(
1689 esimd_dmmv_row_pair_policy{ vx, dst }, y, ncols, nrows, lmem, it);
1690 });
1691 });
1692}
1693
1694static void dequantize_mul_mat_vec_q4_K_sycl_reorder_esimd(const void *vx, const float *y,
1695 float *dst, const int ncols,
1696 const int nrows,
1697 dpct::queue_ptr stream) {
1698 GGML_ASSERT(ncols % QK_K == 0);
1699 const int workgroups = (nrows + 1) / 2;
1700 stream->submit([&](sycl::handler &h) {
1701 sycl::local_accessor<float, 1> lmem(sycl::range<1>(GGML_SYCL_DMMV_ESIMD_WG_SIZE * 2), h);
1702 h.parallel_for(
1703 sycl::nd_range<1>(sycl::range<1>((size_t)workgroups * GGML_SYCL_DMMV_ESIMD_WG_SIZE), sycl::range<1>(GGML_SYCL_DMMV_ESIMD_WG_SIZE)),
1704 [=](sycl::nd_item<1> it) [[intel::sycl_explicit_simd]] {
1705 dequantize_mul_mat_vec_reorder_esimd<GGML_TYPE_Q4_K>(
1706 esimd_dmmv_row_pair_policy{ vx, dst }, y, ncols, nrows, lmem, it);
1707 });
1708 });
1709}
1710
1711static void dequantize_mul_mat_vec_q5_K_sycl_reorder_esimd(const void *vx, const float *y,
1712 float *dst, const int ncols,
1713 const int nrows,
1714 dpct::queue_ptr stream) {
1715 GGML_ASSERT(ncols % QK_K == 0);
1716 const int workgroups = (nrows + 1) / 2;
1717 stream->submit([&](sycl::handler &h) {
1718 sycl::local_accessor<float, 1> lmem(sycl::range<1>(GGML_SYCL_DMMV_ESIMD_WG_SIZE * 2), h);
1719 h.parallel_for(
1720 sycl::nd_range<1>(sycl::range<1>((size_t)workgroups * GGML_SYCL_DMMV_ESIMD_WG_SIZE), sycl::range<1>(GGML_SYCL_DMMV_ESIMD_WG_SIZE)),
1721 [=](sycl::nd_item<1> it) [[intel::sycl_explicit_simd]] {
1722 dequantize_mul_mat_vec_reorder_esimd<GGML_TYPE_Q5_K>(
1723 esimd_dmmv_row_pair_policy{ vx, dst }, y, ncols, nrows, lmem, it);
1724 });
1725 });
1726}
1727
1728static void dequantize_mul_mat_vec_q6_K_sycl_reorder_esimd(const void *vx, const float *y,
1729 float *dst, const int ncols,
1730 const int nrows,
1731 dpct::queue_ptr stream) {
1732 GGML_ASSERT(ncols % QK_K == 0);
1733 const int workgroups = (nrows + 1) / 2;
1734 stream->submit([&](sycl::handler &h) {
1735 sycl::local_accessor<float, 1> lmem(sycl::range<1>(GGML_SYCL_DMMV_ESIMD_WG_SIZE * 2), h);
1736 h.parallel_for(
1737 sycl::nd_range<1>(sycl::range<1>((size_t)workgroups * GGML_SYCL_DMMV_ESIMD_WG_SIZE), sycl::range<1>(GGML_SYCL_DMMV_ESIMD_WG_SIZE)),
1738 [=](sycl::nd_item<1> it) [[intel::sycl_explicit_simd]] {
1739 dequantize_mul_mat_vec_reorder_esimd<GGML_TYPE_Q6_K>(
1740 esimd_dmmv_row_pair_policy{ vx, dst }, y, ncols, nrows, lmem, it);
1741 });
1742 });
1743}
1744
1745// Q8_0 SOA reorder layout: [qs: nb*QK8_0] [d: nb*sizeof(half)].
1746// Process eight blocks per stripe and process remaining blocks one at a time.
1747template <int NBLK>
1748ESIMD_INLINE void q8_0_mac_stripe(
1749 const int8_t * qs_a, const int8_t * qs_b,
1750 const sycl::half * d_a, const sycl::half * d_b, bool has_b,
1751 sycl::ext::intel::esimd::simd<float, 32 * NBLK> & y_vec,
1752 sycl::ext::intel::esimd::simd<float, 32> & acc_a,
1753 sycl::ext::intel::esimd::simd<float, 32> & acc_b) {
1754 using namespace sycl::ext::intel::esimd;
1755
1756 simd<int8_t, 32 * NBLK> qa = block_load<int8_t, 32 * NBLK>(qs_a);
1757 simd<int8_t, 32 * NBLK> qb = 0;
1758 // Scale rows can be only 2-byte aligned when nblk_row is odd.
1759 simd<sycl::half, NBLK> da = block_load<sycl::half, NBLK>(d_a, element_aligned_tag{});
1760 simd<sycl::half, NBLK> db = 0;
1761 if (has_b) {
1762 qb = block_load<int8_t, 32 * NBLK>(qs_b);
1763 db = block_load<sycl::half, NBLK>(d_b, element_aligned_tag{});
1764 }
1765
1766 simd<float, NBLK> da_f = convert<float>(da);
1767 simd<float, NBLK> db_f = convert<float>(db);
1768
1769#pragma unroll
1770 for (int s = 0; s < NBLK; ++s) {
1771 simd<float, 32> y_s = y_vec.template select<32, 1>(s * 32);
1772 simd<int8_t, 32> qa_s = qa.template select<32, 1>(s * 32);
1773 simd<int8_t, 32> qb_s = qb.template select<32, 1>(s * 32);
1774 const float sa = da_f[s];
1775 const float sb = db_f[s];
1776 acc_a += y_s * (convert<float>(qa_s) * sa);
1777 acc_b += y_s * (convert<float>(qb_s) * sb);
1778 }
1779}
1780
1781template <int WG, typename Policy>
1782ESIMD_INLINE void dequantize_mul_mat_vec_q8_0_reorder_esimd(
1783 const Policy & policy, const float * y,
1784 const int ncols, const int nrows,
1785 sycl::local_accessor<float, 1> lmem,
1786 const sycl::nd_item<1> & it) {
1787 using namespace sycl::ext::intel::esimd;
1788
1789 constexpr int STRIPE = 8;
1790
1791 const int nblk_row = ncols / QK8_0;
1792 const size_t nb = (size_t) nrows * nblk_row;
1793 const int8_t * qs_a = (const int8_t *) policy.w_a();
1794 const int8_t * qs_b = (const int8_t *) policy.w_b();
1795 const sycl::half * d_a = (const sycl::half *) (qs_a + nb * QK8_0);
1796 const sycl::half * d_b = (const sycl::half *) (qs_b + nb * QK8_0);
1797
1798 const int tid = it.get_local_id(0);
1799 const int g = it.get_group(0);
1800 const bool has_b = policy.has_b(g, nrows);
1801
1802 const size_t base_a = (size_t) policy.row_a(g) * nblk_row;
1803 const size_t base_b = (size_t) policy.row_b(g) * nblk_row;
1804
1805 simd<float, 32> acc_a = 0.0f;
1806 simd<float, 32> acc_b = 0.0f;
1807
1808 // Each thread processes one contiguous stripe.
1809 int ib = 0;
1810 for (; ib + WG * STRIPE <= nblk_row; ib += WG * STRIPE) {
1811 const int b = ib + tid * STRIPE;
1812 simd<float, 256> y_vec = block_load<float, 256>(y + (size_t) b * QK8_0);
1813 q8_0_mac_stripe<STRIPE>(qs_a + (base_a + b) * QK8_0, qs_b + (base_b + b) * QK8_0,
1814 d_a + base_a + b, d_b + base_b + b, has_b, y_vec, acc_a, acc_b);
1815 }
1816
1817 // Distribute remaining blocks across the work-group.
1818 for (int b = ib + tid; b < nblk_row; b += WG) {
1819 simd<float, 32> y_vec = block_load<float, 32>(y + (size_t) b * QK8_0);
1820 q8_0_mac_stripe<1>(qs_a + (base_a + b) * QK8_0, qs_b + (base_b + b) * QK8_0,
1821 d_a + base_a + b, d_b + base_b + b, has_b, y_vec, acc_a, acc_b);
1822 }
1823
1824 lmem[tid * 2 + 0] = reduce<float>(acc_a, std::plus<>{});
1825 lmem[tid * 2 + 1] = reduce<float>(acc_b, std::plus<>{});
1826 it.barrier(sycl::access::fence_space::local_space);
1827
1828 if (tid == 0) {
1829 float sum_a = 0.0f;
1830 float sum_b = 0.0f;
1831 for (int p = 0; p < WG; ++p) {
1832 sum_a += lmem[p * 2 + 0];
1833 sum_b += lmem[p * 2 + 1];
1834 }
1835
1836 policy.epilogue(g, nrows, sum_a, sum_b);
1837 }
1838}
1839
1840template <int WG, typename Policy>
1841static void q8_0_esimd_launch(const Policy & policy, const float * y, const int ncols, const int nrows,
1842 const int workgroups, dpct::queue_ptr stream) {
1843 stream->submit([&](sycl::handler & h) {
1844 sycl::local_accessor<float, 1> lmem(sycl::range<1>(WG * 2), h);
1845 h.parallel_for(
1846 sycl::nd_range<1>(sycl::range<1>((size_t) workgroups * WG), sycl::range<1>(WG)),
1847 [=](sycl::nd_item<1> it) [[intel::sycl_explicit_simd]] {
1848 dequantize_mul_mat_vec_q8_0_reorder_esimd<WG>(policy, y, ncols, nrows, lmem, it);
1849 });
1850 });
1851}
1852
1853// Scale the work-group with the number of blocks per row.
1854template <typename Policy>
1855static void q8_0_esimd_launch_scaled(const Policy & policy, const float * y, const int ncols, const int nrows,
1856 const int workgroups, dpct::queue_ptr stream) {
1857 GGML_ASSERT(ncols % QK8_0 == 0);
1858
1859 const int nblk_row = ncols / QK8_0;
1860 if (nblk_row >= 64) {
1861 q8_0_esimd_launch<8>(policy, y, ncols, nrows, workgroups, stream);
1862 } else if (nblk_row >= 32) {
1863 q8_0_esimd_launch<4>(policy, y, ncols, nrows, workgroups, stream);
1864 } else if (nblk_row >= 16) {
1865 q8_0_esimd_launch<2>(policy, y, ncols, nrows, workgroups, stream);
1866 } else {
1867 q8_0_esimd_launch<1>(policy, y, ncols, nrows, workgroups, stream);
1868 }
1869}
1870
1871static void dequantize_mul_mat_vec_q8_0_sycl_reorder_esimd(const void *vx, const float *y,
1872 float *dst, const int ncols,
1873 const int nrows,
1874 dpct::queue_ptr stream) {
1875 q8_0_esimd_launch_scaled(esimd_dmmv_row_pair_policy{ vx, dst }, y, ncols, nrows, (nrows + 1) / 2, stream);
1876}
1877
1878// ESIMD fused gate+up+GLU on reordered K-quant SOA weights: one output row (gate
1879// and up) per work-group, two independent 32-wide accumulators per super-block.
1880// Reuses the DMMV kernels' dequant+MAC primitive (esimd_reorder_q_traits<T>::mac_pair).
1881template <ggml_type T>
1882static void dequantize_mul_mat_vec_glu_reorder_esimd(const void * vx, const void * vgate,
1883 const float * y, float * dst,
1884 const int ncols, const int nrows,
1885 const ggml_glu_op glu_op,
1886 dpct::queue_ptr stream) {
1887 GGML_ASSERT(ncols % QK_K == 0);
1888 const int workgroups = nrows; // one output row (gate+up) per work-group
1889 stream->submit([&](sycl::handler & h) {
1890 sycl::local_accessor<float, 1> lmem(sycl::range<1>(GGML_SYCL_DMMV_ESIMD_WG_SIZE * 2), h);
1891 h.parallel_for(
1892 sycl::nd_range<1>(sycl::range<1>((size_t) workgroups * GGML_SYCL_DMMV_ESIMD_WG_SIZE), sycl::range<1>(GGML_SYCL_DMMV_ESIMD_WG_SIZE)),
1893 [=](sycl::nd_item<1> it) [[intel::sycl_explicit_simd]] {
1894 dequantize_mul_mat_vec_reorder_esimd<T>(
1895 esimd_dmmv_glu_policy{ vgate, vx, dst, glu_op }, y, ncols, nrows, lmem, it);
1896 });
1897 });
1898}
1899
1900bool ggml_sycl_dequantize_mul_mat_vec_glu_reorder_esimd(enum ggml_type src0_type, enum ggml_glu_op glu_op,
1901 const void * vx, const void * vgate, const float * y,
1902 float * dst, int ncols, int nrows, dpct::queue_ptr stream) {
1903 switch (src0_type) {
1904 case GGML_TYPE_Q2_K:
1905 dequantize_mul_mat_vec_glu_reorder_esimd<GGML_TYPE_Q2_K>(vx, vgate, y, dst, ncols, nrows, glu_op, stream);
1906 return true;
1907 case GGML_TYPE_Q3_K:
1908 dequantize_mul_mat_vec_glu_reorder_esimd<GGML_TYPE_Q3_K>(vx, vgate, y, dst, ncols, nrows, glu_op, stream);
1909 return true;
1910 case GGML_TYPE_Q4_K:
1911 dequantize_mul_mat_vec_glu_reorder_esimd<GGML_TYPE_Q4_K>(vx, vgate, y, dst, ncols, nrows, glu_op, stream);
1912 return true;
1913 case GGML_TYPE_Q5_K:
1914 dequantize_mul_mat_vec_glu_reorder_esimd<GGML_TYPE_Q5_K>(vx, vgate, y, dst, ncols, nrows, glu_op, stream);
1915 return true;
1916 case GGML_TYPE_Q6_K:
1917 dequantize_mul_mat_vec_glu_reorder_esimd<GGML_TYPE_Q6_K>(vx, vgate, y, dst, ncols, nrows, glu_op, stream);
1918 return true;
1919 case GGML_TYPE_Q8_0:
1920 q8_0_esimd_launch_scaled(esimd_dmmv_glu_policy{ vgate, vx, dst, glu_op }, y, ncols, nrows, nrows, stream);
1921 return true;
1922 default:
1923 return false;
1924 }
1925}
1926
1927#endif // GGML_SYCL_DMMV_HAS_ESIMD
1928
1929void ggml_sycl_op_dequantize_mul_mat_vec(
1930 ggml_backend_sycl_context & ctx,
1931 const ggml_tensor *src0, const ggml_tensor *src1, ggml_tensor *dst,
1932 const char *src0_dd_i, const float *src1_ddf_i, const char *src1_ddq_i,
1933 float *dst_dd_i, const int64_t row_low, const int64_t row_high,
1934 const int64_t src1_ncols, const int64_t src1_padded_row_size,
1935 const dpct::queue_ptr &stream) {
1936
1937 const int64_t ne00 = src0->ne[0];
1938 const int64_t row_diff = row_high - row_low;
1939 GGML_ASSERT(src1->type == GGML_TYPE_F32);
1940 // on some GPUs it is faster to convert src1 to half and to use half precision intrinsics
1941#ifdef GGML_SYCL_F16
1942 ggml_sycl_pool_alloc<sycl::half> src1_dfloat_a(ctx.pool());
1943 sycl::half *src1_dfloat = nullptr; // dfloat == half
1944
1945#ifdef GGML_SYCL_DMMV_HAS_ESIMD
1946 // The ESIMD Q8_0 kernel reads F32 activations.
1947 const bool q8_0_esimd = src0->type == GGML_TYPE_Q8_0 && g_ggml_sycl_enable_esimd &&
1948 ((ggml_tensor_extra_gpu *) dst->src[0]->extra) &&
1949 ((ggml_tensor_extra_gpu *) dst->src[0]->extra)->optimized_feature.reorder;
1950#else
1951 const bool q8_0_esimd = false;
1952#endif
1953
1954 bool src1_convert_f16 =
1955 src0->type == GGML_TYPE_Q1_0 ||
1956 src0->type == GGML_TYPE_Q4_0 || src0->type == GGML_TYPE_Q4_1 ||
1957 src0->type == GGML_TYPE_Q5_0 || src0->type == GGML_TYPE_Q5_1 ||
1958 (src0->type == GGML_TYPE_Q8_0 && !q8_0_esimd) || src0->type == GGML_TYPE_F16 ||
1959 src0->type == GGML_TYPE_BF16;
1960
1961 if (src1_convert_f16) {
1962 scope_op_debug_print scope_dbg_print(__func__, "/to_fp16_sycl", dst, /*num_src=*/2,
1963 " : converting src1 to fp16");
1964 src1_dfloat = src1_dfloat_a.alloc(ne00);
1965 const to_fp16_sycl_t to_fp16_sycl = ggml_get_to_fp16_sycl(src1->type, dst);
1966 GGML_ASSERT(to_fp16_sycl != nullptr);
1967 to_fp16_sycl(src1_ddf_i, src1_dfloat, ne00, stream);
1968 }
1969#else
1970 const dfloat * src1_dfloat = (const dfloat *) src1_ddf_i; // dfloat == float, no conversion
1971#endif // GGML_SYCL_F16
1972
1973 switch (src0->type) {
1974 case GGML_TYPE_Q1_0:
1975 if ((ggml_tensor_extra_gpu*)dst->src[0]->extra &&
1976 ((ggml_tensor_extra_gpu*)dst->src[0]->extra)->optimized_feature.reorder) {
1977 launch_dequantize_mul_mat_vec_reorder<QK1_0, QR1_0, QK1_0/8, dequantize_q1_0_reorder>(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
1978 } else {
1979 launch_dequantize_mul_mat_vec<QK1_0, QR1_0, dequantize_q1_0>(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
1980 }
1981 break;
1982 case GGML_TYPE_Q4_0:
1983 if ((ggml_tensor_extra_gpu*)dst->src[0]->extra &&
1984 ((ggml_tensor_extra_gpu*)dst->src[0]->extra)->optimized_feature.reorder) {
1985 launch_dequantize_mul_mat_vec_reorder<QK4_0, QR4_0, QK4_0/2, dequantize_q4_0_reorder>(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
1986 } else {
1987 launch_dequantize_mul_mat_vec<QK4_0, QR4_0, dequantize_q4_0>(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
1988 }
1989 break;
1990 case GGML_TYPE_Q4_1:
1991 launch_dequantize_mul_mat_vec<QK4_1, QR4_1, dequantize_q4_1>(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
1992 break;
1993 case GGML_TYPE_Q5_0:
1994 launch_dequantize_mul_mat_vec<QK5_0, QR5_0, dequantize_q5_0>(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
1995 break;
1996 case GGML_TYPE_Q5_1:
1997 launch_dequantize_mul_mat_vec<QK5_1, QR5_1, dequantize_q5_1>(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
1998 break;
1999 case GGML_TYPE_Q8_0:
2000 if ((ggml_tensor_extra_gpu *) dst->src[0]->extra &&
2001 ((ggml_tensor_extra_gpu *) dst->src[0]->extra)->optimized_feature.reorder) {
2002#ifdef GGML_SYCL_DMMV_HAS_ESIMD
2003 if (g_ggml_sycl_enable_esimd) {
2004 dequantize_mul_mat_vec_q8_0_sycl_reorder_esimd(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
2005 } else
2006#endif
2007 {
2008 launch_dequantize_mul_mat_vec_reorder<QK8_0, QR8_0, QK8_0, dequantize_q8_0_reorder>(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
2009 }
2010 } else {
2011 launch_dequantize_mul_mat_vec<QK8_0, QR8_0, dequantize_q8_0>(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
2012 }
2013 break;
2014 case GGML_TYPE_Q2_K:
2015 if ((ggml_tensor_extra_gpu *) dst->src[0]->extra &&
2016 ((ggml_tensor_extra_gpu *) dst->src[0]->extra)->optimized_feature.reorder) {
2017#ifdef GGML_SYCL_DMMV_HAS_ESIMD
2018 if (g_ggml_sycl_enable_esimd) {
2019 dequantize_mul_mat_vec_q2_K_sycl_reorder_esimd(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
2020 }
2021 else
2022#endif
2023 {
2024 launch_dequantize_mul_mat_vec_k<2/K_QUANTS_PER_ITERATION, dequantize_mul_mat_vec_q2_k_reorder>(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
2025 }
2026 } else {
2027 launch_dequantize_mul_mat_vec_k<2, dequantize_mul_mat_vec_q2_k>(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
2028 }
2029 break;
2030 case GGML_TYPE_Q3_K:
2031 if ((ggml_tensor_extra_gpu *) dst->src[0]->extra &&
2032 ((ggml_tensor_extra_gpu *) dst->src[0]->extra)->optimized_feature.reorder) {
2033#ifdef GGML_SYCL_DMMV_HAS_ESIMD
2034 if (g_ggml_sycl_enable_esimd) {
2035 dequantize_mul_mat_vec_q3_K_sycl_reorder_esimd(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
2036 }
2037 else
2038#endif
2039 {
2040 launch_dequantize_mul_mat_vec_k<2/K_QUANTS_PER_ITERATION, dequantize_mul_mat_vec_q3_k_reorder>(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
2041 }
2042 } else {
2043 launch_dequantize_mul_mat_vec_k<2/K_QUANTS_PER_ITERATION, dequantize_mul_mat_vec_q3_k>(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
2044 }
2045 break;
2046 case GGML_TYPE_Q4_K:
2047 if ((ggml_tensor_extra_gpu *) dst->src[0]->extra &&
2048 ((ggml_tensor_extra_gpu *) dst->src[0]->extra)->optimized_feature.reorder) {
2049#ifdef GGML_SYCL_DMMV_HAS_ESIMD
2050 if (g_ggml_sycl_enable_esimd) {
2051 dequantize_mul_mat_vec_q4_K_sycl_reorder_esimd(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
2052 }
2053 else
2054#endif
2055 {
2056 launch_dequantize_mul_mat_vec_k<2/K_QUANTS_PER_ITERATION, dequantize_mul_mat_vec_q4_k_reorder>(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
2057 }
2058 } else {
2059 launch_dequantize_mul_mat_vec_k<2/K_QUANTS_PER_ITERATION, dequantize_mul_mat_vec_q4_k>(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
2060 }
2061 break;
2062 case GGML_TYPE_Q5_K:
2063 if ((ggml_tensor_extra_gpu *) dst->src[0]->extra &&
2064 ((ggml_tensor_extra_gpu *) dst->src[0]->extra)->optimized_feature.reorder) {
2065#ifdef GGML_SYCL_DMMV_HAS_ESIMD
2066 if (g_ggml_sycl_enable_esimd) {
2067 dequantize_mul_mat_vec_q5_K_sycl_reorder_esimd(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
2068 }
2069 else
2070#endif
2071 {
2072 launch_dequantize_mul_mat_vec_k<2/K_QUANTS_PER_ITERATION, dequantize_mul_mat_vec_q5_k_reorder>(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
2073 }
2074 } else {
2075 launch_dequantize_mul_mat_vec_k<2/K_QUANTS_PER_ITERATION, dequantize_mul_mat_vec_q5_k>(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
2076 }
2077 break;
2078 case GGML_TYPE_Q6_K:
2079 if ((ggml_tensor_extra_gpu *) dst->src[0]->extra &&
2080 ((ggml_tensor_extra_gpu *) dst->src[0]->extra)->optimized_feature.reorder) {
2081#ifdef GGML_SYCL_DMMV_HAS_ESIMD
2082 if (g_ggml_sycl_enable_esimd) {
2083 dequantize_mul_mat_vec_q6_K_sycl_reorder_esimd(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
2084 }
2085 else
2086#endif
2087 {
2088 launch_dequantize_mul_mat_vec_k<2/K_QUANTS_PER_ITERATION, dequantize_mul_mat_vec_q6_k_reorder>(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
2089 }
2090 } else {
2091 launch_dequantize_mul_mat_vec_k<2/K_QUANTS_PER_ITERATION, dequantize_mul_mat_vec_q6_k>(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
2092 }
2093 break;
2094 case GGML_TYPE_F16:
2095 convert_mul_mat_vec_f16_sycl(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
2096 break;
2097#ifdef GGML_SYCL_DMMV_HAS_BF16
2098 case GGML_TYPE_BF16:
2099 convert_mul_mat_vec_bf16_sycl(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
2100 break;
2101#endif
2102 default:
2103 printf("ggml_sycl_op_dequantize_mul_mat_vec unsupported GGML_TYPE %d\n", src0->type);
2104 GGML_ABORT("fatal error");
2105 }
2106
2107 GGML_UNUSED(src1);
2108 GGML_UNUSED(dst);
2109 GGML_UNUSED(src1_ddq_i);
2110 GGML_UNUSED(src1_ncols);
2111 GGML_UNUSED(src1_padded_row_size);
2112 GGML_UNUSED(ctx);
2113}