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 / mmvq.cpp

3255 lines · 168241 bytes Raw Blame History
1#include "mmvq.hpp"
2
3#include "ggml.h"
4#include "common.hpp"
5#include "element_wise.hpp"
6#include "quants.hpp"
7#include "vecdotq.hpp"
8
9#ifdef GGML_SYCL_MMVQ_HAS_XMX
10# include "esimd.hpp"
11#endif // GGML_SYCL_MMVQ_HAS_XMX
12
13// vec_dot_q_sycl_t adapters for the IQ vec_dots that take their codebook tables as extra
14// arguments: bind the constant tables here (as vec_dot_iq2_s_q8_1 / vec_dot_iq1_m_q8_1 already do
15// internally) so they can be used as template arguments of mul_mat_vec_q_moe.
16static __dpct_inline__ float vec_dot_iq2_xxs_q8_1_moe(const void * __restrict__ vbq,
17 const block_q8_1 * __restrict__ bq8_1,
18 const int & iqs) {
19 return vec_dot_iq2_xxs_q8_1(vbq, bq8_1, iqs, iq2xxs_grid, ksigns_iq2xs, kmask_iq2xs);
20}
21
22static __dpct_inline__ float vec_dot_iq2_xs_q8_1_moe(const void * __restrict__ vbq,
23 const block_q8_1 * __restrict__ bq8_1,
24 const int & iqs) {
25 return vec_dot_iq2_xs_q8_1(vbq, bq8_1, iqs, iq2xs_grid);
26}
27
28static __dpct_inline__ float vec_dot_iq3_xxs_q8_1_moe(const void * __restrict__ vbq,
29 const block_q8_1 * __restrict__ bq8_1,
30 const int & iqs) {
31 return vec_dot_iq3_xxs_q8_1(vbq, bq8_1, iqs, iq3xxs_grid);
32}
33
34static __dpct_inline__ float vec_dot_iq3_s_q8_1_moe(const void * __restrict__ vbq,
35 const block_q8_1 * __restrict__ bq8_1,
36 const int & iqs) {
37 return vec_dot_iq3_s_q8_1(vbq, bq8_1, iqs, iq3s_grid);
38}
39
40static __dpct_inline__ float vec_dot_iq1_s_q8_1_moe(const void * __restrict__ vbq,
41 const block_q8_1 * __restrict__ bq8_1,
42 const int & iqs) {
43 return vec_dot_iq1_s_q8_1(vbq, bq8_1, iqs, iq1s_grid_gpu);
44}
45
46// Minimum weight-row count at which the Q4_K multi-column MMVQ kernel handles two output rows per
47// subgroup (rows_per_sg == 2) instead of one, when ncols_dst == 2.
48//
49// Pairing rows lets a subgroup load each activation block once and apply it to two rows, at the cost
50// of halving the number of subgroups in the launch. With only two destination columns there is too
51// little work per row to hide that loss of parallelism, so pairing only pays off once there are
52// enough rows to keep the device occupied. This is a measured performance crossover, not a
53// correctness or hardware limit - both variants compute the same result for any nrows.
54//
55// Derived on Intel Arc Pro B70 with `test-backend-ops perf -o MUL_MAT` (Q4_K, ncols_dst == 2),
56// sweeping nrows over 5120..6912 at ncols 17408 and 19968: one row per subgroup was up to 9% faster
57// below the crossover, two rows per subgroup 8-15% faster above it, and the crossover fell inside
58// (6144, 6272] for both ncols with no measurable ncols dependence. A later 32-row granularity sweep
59// narrowed it to (6144, 6176], so 6272 is a conservative gate rather than the exact crossover.
60// ncols_dst >= 3 amortizes the activation loads over more columns and is faster with two rows at
61// every row count, so it does not consult this threshold.
62static constexpr int Q4_K_MMVQ_ROW_PAIR_MIN_NROWS = 6272;
63
64template <typename reorder_vec_dot_q_sycl>
65static void mul_mat_vec_q_reorder(const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
66 const int ncols, const int nrows, const sycl::nd_item<3> & nd_item) {
67 using block_type = ggml_sycl_reordered::block_q_t<reorder_vec_dot_q_sycl::gtype>;
68 using block_traits = typename block_type::traits;
69
70 const auto sg = nd_item.get_sub_group();
71 const int sg_range = sg.get_group_linear_range();
72 const int workgroup_id = nd_item.get_group_linear_id();
73 const int sg_id = sg.get_group_linear_id();
74 const int row = workgroup_id * sg_range + sg_id;
75
76 if (row >= nrows) {
77 return;
78 }
79
80 const int blocks_per_row = ncols / block_traits::qk;
81 constexpr int blocks_per_subgroup = ceil_div(block_traits::vdr_mmvq * WARP_SIZE, block_traits::qi);
82 constexpr int block_elements_per_subgroup = block_traits::qi / block_traits::vdr_mmvq;
83 const int nblocks = nrows * (ncols / block_traits::qk);
84
85 static_assert(blocks_per_subgroup > 0);
86 static_assert(block_elements_per_subgroup > 0);
87
88 float partial_sum = 0.0f;
89 for (int i = sg.get_local_linear_id() / block_elements_per_subgroup; i < blocks_per_row; i += blocks_per_subgroup) {
90 const int ibx = row * blocks_per_row + i; // x block index
91
92 const auto bx_offset = block_type::get_block_offset(ibx, nblocks);
93 const auto d_offset = block_type::get_d_offset(nrows, ncols, ibx);
94 // Y block index that aligns with ibx
95 const int iby = i * block_type::block_to_q8_1_ratio();
96 const int8_t* q8_1_quant_ptr = (const int8_t*)vy + iby * QK8_1;
97 const sycl::half2* q8_1_ds_ptr = (const sycl::half2*)((const char*)vy + ncols + iby * sizeof(sycl::half2));
98
99#pragma unroll
100 for (int elem = 0; elem < block_elements_per_subgroup; elem += WARP_SIZE) {
101 // x block quant index when casting the quants to int
102 const int iqs = elem + block_traits::vdr_mmvq * (sg.get_local_linear_id() % block_elements_per_subgroup);
103
104 partial_sum += reorder_vec_dot_q_sycl()(vx, bx_offset, d_offset, q8_1_quant_ptr, q8_1_ds_ptr, iqs);
105 }
106 }
107
108 auto sum = sycl::reduce_over_group(nd_item.get_sub_group(), partial_sum, std::plus<>());
109
110 if (sg.leader()) {
111 dst[row] = sum;
112 }
113}
114
115// With has_fusion, `vgate` is a second weight matrix sharing vx's shape, stride and reorder
116// layout: one pass computes both row dot products and the epilogue writes glu(gate, up).
117template <typename reorder_vec_dot_q_sycl, int ncols_dst, bool has_fusion = false, int rows_per_sg = 1,
118 bool shared_weights = reorder_vec_dot_shared_weights<reorder_vec_dot_q_sycl::gtype>::value>
119static void mul_mat_vec_q_reorder_ncols(const void * __restrict__ vx, const void * __restrict__ vgate,
120 const void * __restrict__ vy, float * __restrict__ dst, const int ncols,
121 const int nrows, const int stride_col_y_bytes, const int stride_col_dst,
122 const ggml_glu_op glu_op, const sycl::nd_item<3> & nd_item) {
123 using block_type = ggml_sycl_reordered::block_q_t<reorder_vec_dot_q_sycl::gtype>;
124 using block_traits = typename block_type::traits;
125
126 const auto sg = nd_item.get_sub_group();
127 const int sg_range = sg.get_group_linear_range();
128 const int workgroup_id = nd_item.get_group_linear_id();
129 const int sg_id = sg.get_group_linear_id();
130 const int row0 = (workgroup_id * sg_range + sg_id) * rows_per_sg;
131
132 // row is sub-group uniform, so this retires whole sub-groups and the collectives below
133 // stay convergent
134 if (row0 >= nrows) {
135 return;
136 }
137
138 static_assert(rows_per_sg == 1 ||
139 reorder_vec_dot_shared_activations<reorder_vec_dot_q_sycl::gtype>::value);
140
141 const int blocks_per_row = ncols / block_traits::qk;
142 constexpr int blocks_per_subgroup = ceil_div(block_traits::vdr_mmvq * WARP_SIZE, block_traits::qi);
143 constexpr int block_elements_per_subgroup = block_traits::qi / block_traits::vdr_mmvq;
144 const int nblocks = nrows * (ncols / block_traits::qk);
145
146 static_assert(blocks_per_subgroup > 0);
147 static_assert(block_elements_per_subgroup > 0);
148
149 float partial_sum[ncols_dst][rows_per_sg] = {};
150 // sized 1 rather than 0 when unused: zero-length arrays are not standard C++, and the
151 // array is dead and eliminated in that case
152 [[maybe_unused]] float partial_gate[has_fusion ? ncols_dst : 1][has_fusion ? rows_per_sg : 1] = {};
153 for (int i = sg.get_local_linear_id() / block_elements_per_subgroup; i < blocks_per_row; i += blocks_per_subgroup) {
154 const int iby = i * block_type::block_to_q8_1_ratio();
155
156#pragma unroll
157 for (int elem = 0; elem < block_elements_per_subgroup; elem += WARP_SIZE) {
158 const int iqs = elem + block_traits::vdr_mmvq * (sg.get_local_linear_id() % block_elements_per_subgroup);
159
160 if constexpr (rows_per_sg > 1) {
161 typename reorder_vec_dot_q_sycl::weights wx[rows_per_sg];
162 [[maybe_unused]] typename reorder_vec_dot_q_sycl::weights wg[rows_per_sg];
163#pragma unroll
164 for (int r = 0; r < rows_per_sg; ++r) {
165 const int row = sycl::min(row0 + r, nrows - 1);
166 const int ibx = row * blocks_per_row + i;
167 const auto bx_offset = block_type::get_block_offset(ibx, nblocks);
168 const auto d_offset = block_type::get_d_offset(nrows, ncols, ibx);
169 wx[r] = reorder_vec_dot_q_sycl::load(vx, bx_offset, d_offset, iqs);
170 if constexpr (has_fusion) {
171 wg[r] = reorder_vec_dot_q_sycl::load(vgate, bx_offset, d_offset, iqs);
172 }
173 }
174#pragma unroll
175 for (int j = 0; j < ncols_dst; ++j) {
176 const char * vy_j = (const char *) vy + j * stride_col_y_bytes;
177 const int8_t * q8_1_quant_ptr = (const int8_t *) vy_j + iby * QK8_1;
178 const sycl::half2 * q8_1_ds_ptr =
179 (const sycl::half2 *) (vy_j + ncols + iby * sizeof(sycl::half2));
180 const auto a = reorder_vec_dot_q_sycl::load_activations(q8_1_quant_ptr, q8_1_ds_ptr, iqs);
181#pragma unroll
182 for (int r = 0; r < rows_per_sg; ++r) {
183 partial_sum[j][r] += reorder_vec_dot_q_sycl::apply(wx[r], a);
184 if constexpr (has_fusion) {
185 partial_gate[j][r] += reorder_vec_dot_q_sycl::apply(wg[r], a);
186 }
187 }
188 }
189 } else if constexpr (shared_weights) {
190 const int ibx = row0 * blocks_per_row + i;
191 const auto bx_offset = block_type::get_block_offset(ibx, nblocks);
192 const auto d_offset = block_type::get_d_offset(nrows, ncols, ibx);
193 const auto wx = reorder_vec_dot_q_sycl::load(vx, bx_offset, d_offset, iqs);
194 if constexpr (has_fusion) {
195 const auto wg = reorder_vec_dot_q_sycl::load(vgate, bx_offset, d_offset, iqs);
196
197#pragma unroll
198 for (int j = 0; j < ncols_dst; ++j) {
199 const char * vy_j = (const char *) vy + j * stride_col_y_bytes;
200 const int8_t * q8_1_quant_ptr = (const int8_t *) vy_j + iby * QK8_1;
201 const sycl::half2 * q8_1_ds_ptr =
202 (const sycl::half2 *) (vy_j + ncols + iby * sizeof(sycl::half2));
203
204 // up and gate share the activation, so load it once and apply it twice
205 const auto a = reorder_vec_dot_q_sycl::load_activations(q8_1_quant_ptr, q8_1_ds_ptr, iqs);
206
207 partial_sum[j][0] += reorder_vec_dot_q_sycl::apply(wx, a);
208 partial_gate[j][0] += reorder_vec_dot_q_sycl::apply(wg, a);
209 }
210 } else {
211#pragma unroll
212 for (int j = 0; j < ncols_dst; ++j) {
213 const char * vy_j = (const char *) vy + j * stride_col_y_bytes;
214 const int8_t * q8_1_quant_ptr = (const int8_t *) vy_j + iby * QK8_1;
215 const sycl::half2 * q8_1_ds_ptr =
216 (const sycl::half2 *) (vy_j + ncols + iby * sizeof(sycl::half2));
217
218 partial_sum[j][0] += reorder_vec_dot_q_sycl::dot(wx, q8_1_quant_ptr, q8_1_ds_ptr, iqs);
219 }
220 }
221 } else {
222 const int ibx = row0 * blocks_per_row + i;
223 const auto bx_offset = block_type::get_block_offset(ibx, nblocks);
224 const auto d_offset = block_type::get_d_offset(nrows, ncols, ibx);
225#pragma unroll
226 for (int j = 0; j < ncols_dst; ++j) {
227 const char * vy_j = (const char *) vy + j * stride_col_y_bytes;
228 const int8_t * q8_1_quant_ptr = (const int8_t *) vy_j + iby * QK8_1;
229 const sycl::half2 * q8_1_ds_ptr =
230 (const sycl::half2 *) (vy_j + ncols + iby * sizeof(sycl::half2));
231
232 partial_sum[j][0] +=
233 reorder_vec_dot_q_sycl()(vx, bx_offset, d_offset, q8_1_quant_ptr, q8_1_ds_ptr, iqs);
234
235 if constexpr (has_fusion) {
236 partial_gate[j][0] +=
237 reorder_vec_dot_q_sycl()(vgate, bx_offset, d_offset, q8_1_quant_ptr, q8_1_ds_ptr, iqs);
238 }
239 }
240 }
241 }
242 }
243
244#pragma unroll
245 for (int j = 0; j < ncols_dst; ++j) {
246#pragma unroll
247 for (int r = 0; r < rows_per_sg; ++r) {
248 float sum = sycl::reduce_over_group(nd_item.get_sub_group(), partial_sum[j][r], std::plus<>());
249
250 if constexpr (has_fusion) {
251 const float gate = sycl::reduce_over_group(nd_item.get_sub_group(), partial_gate[j][r], std::plus<>());
252
253 // uniform across the launch; the launcher only instantiates SWIGLU and GEGLU
254 sum *= glu_op == GGML_GLU_OP_SWIGLU ? op_silu(gate) : op_gelu(gate);
255 }
256
257 if (sg.leader() && row0 + r < nrows) {
258 dst[j * stride_col_dst + row0 + r] = sum;
259 }
260 }
261 }
262}
263
264template <int qk, int qi, typename block_q_t, int vdr, vec_dot_q_sycl_t vec_dot_q_sycl>
265static void mul_mat_vec_q(const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
266 const int ncols, const int nrows, const sycl::nd_item<3> & item_ct1) {
267 const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) + item_ct1.get_local_id(1);
268
269 if (row >= nrows) {
270 return;
271 }
272
273 const int blocks_per_row = ncols / qk;
274 constexpr int blocks_per_warp = (vdr * WARP_SIZE + qi - 1) / qi; // Ensuring blocks_per_warp > 0
275
276 assert(blocks_per_warp > 0);
277
278 // partial sum for each thread
279 float tmp = 0.0f;
280
281 const block_q_t * x = (const block_q_t *) vx;
282 const block_q8_1 * y = (const block_q8_1 *) vy;
283
284 for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row; i += blocks_per_warp) {
285 const int ibx = row * blocks_per_row + i; // x block index
286
287 const int iby = i * (qk / QK8_1); // y block index that aligns with ibx
288
289 for (size_t elem = 0; elem < qi / vdr; elem += WARP_SIZE) {
290 const int iqs = elem + vdr * (item_ct1.get_local_id(2) %
291 (qi / vdr)); // x block quant index when casting the quants to int
292
293 tmp += vec_dot_q_sycl(&x[ibx], &y[iby], iqs);
294 }
295 }
296
297 // sum up partial sums and write back result
298#pragma unroll
299 for (int mask = WARP_SIZE / 2; mask > 0; mask >>= 1) {
300 tmp += dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
301 }
302
303 if (item_ct1.get_local_id(2) == 0) {
304 dst[row] = tmp;
305 }
306}
307
308template <int qk, int qi, typename block_q_t, int vdr,
309 vec_dot_q_sycl_t vec_dot_q_sycl, int ncols_dst>
310static void mul_mat_vec_q_ncols(
311 const void * __restrict__ vx,
312 const void * __restrict__ vy,
313 float * __restrict__ dst,
314 const int ncols,
315 const int nrows,
316 const int stride_col_y,
317 const int stride_col_dst,
318 const sycl::nd_item<3> & item_ct1) {
319
320 const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1)
321 + item_ct1.get_local_id(1);
322
323 if (row >= nrows) {
324 return;
325 }
326
327 const int blocks_per_row = ncols / qk;
328 constexpr int blocks_per_warp = (vdr * WARP_SIZE + qi - 1) / qi;
329
330 // partial sums: one per output column
331 float tmp[ncols_dst] = {0.0f};
332
333 const block_q_t * x = (const block_q_t *) vx;
334 const block_q8_1 * y = (const block_q8_1 *) vy;
335
336 for (int i = item_ct1.get_local_id(2) / (qi / vdr);
337 i < blocks_per_row;
338 i += blocks_per_warp) {
339
340 const int ibx = row * blocks_per_row + i;
341 const int iby = i * (qk / QK8_1);
342
343 // read weight block once, dot against all columns
344 for (size_t elem = 0; elem < qi / vdr; elem += WARP_SIZE) {
345 const int iqs = elem + vdr * (item_ct1.get_local_id(2) % (qi / vdr));
346
347#pragma unroll
348 for (int j = 0; j < ncols_dst; ++j) {
349 tmp[j] += vec_dot_q_sycl(&x[ibx], &y[j * stride_col_y + iby], iqs);
350 }
351 }
352 }
353
354 // reduce within subgroup
355#pragma unroll
356 for (int j = 0; j < ncols_dst; ++j) {
357#pragma unroll
358 for (int mask = WARP_SIZE / 2; mask > 0; mask >>= 1) {
359 tmp[j] += dpct::permute_sub_group_by_xor(
360 item_ct1.get_sub_group(), tmp[j], mask);
361 }
362 }
363
364 if (item_ct1.get_local_id(2) == 0) {
365#pragma unroll
366 for (int j = 0; j < ncols_dst; ++j) {
367 dst[j * stride_col_dst + row] = tmp[j];
368 }
369 }
370}
371
372template <int qk, int qi, typename block_q_t, int vdr, vec_dot_q_sycl_t vec_dot_q_sycl, int assert_qk>
373static void launch_mul_mat_vec_q(const void * vx, const void * vy, float * dst,
374 const int ncols, const int nrows, const int ncols_dst,
375 const int stride_col_y, const int stride_col_dst,
376 dpct::queue_ptr stream) {
377 GGML_ASSERT(ncols % assert_qk == 0);
378 const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
379 const sycl::range<3> block_nums(1, 1, block_num_y);
380 const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
381
382 const auto launch = [&](auto ncols_tag) {
383 constexpr int N = decltype(ncols_tag)::value;
384 stream->submit([&](sycl::handler & cgh) {
385 cgh.parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
386 [=](sycl::nd_item<3> item_ct1) [[sycl::reqd_sub_group_size(WARP_SIZE)]] {
387 if constexpr (N == 1) {
388 mul_mat_vec_q<qk, qi, block_q_t, vdr, vec_dot_q_sycl>(
389 vx, vy, dst, ncols, nrows, item_ct1);
390 } else {
391 mul_mat_vec_q_ncols<qk, qi, block_q_t, vdr, vec_dot_q_sycl, N>(
392 vx, vy, dst, ncols, nrows, stride_col_y, stride_col_dst, item_ct1);
393 }
394 });
395 });
396 };
397
398 switch (ncols_dst) {
399 case 1: launch(std::integral_constant<int, 1>{}); break;
400 case 2: launch(std::integral_constant<int, 2>{}); break;
401 case 3: launch(std::integral_constant<int, 3>{}); break;
402 case 4: launch(std::integral_constant<int, 4>{}); break;
403 case 5: launch(std::integral_constant<int, 5>{}); break;
404 case 6: launch(std::integral_constant<int, 6>{}); break;
405 case 7: launch(std::integral_constant<int, 7>{}); break;
406 case 8: launch(std::integral_constant<int, 8>{}); break;
407 default: GGML_ABORT("unsupported ncols_dst=%d", ncols_dst);
408 }
409}
410
411template <int qk, int qi, typename block_q_t, int vdr>
412static void mul_mat_vec_q_iq2_xxs_q8_1(const void *__restrict__ vx,
413 const void *__restrict__ vy,
414 float *__restrict__ dst, const int ncols,
415 const int nrows,
416 const sycl::nd_item<3> &item_ct1) {
417 const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
418 item_ct1.get_local_id(1);
419
420 if (row >= nrows) {
421 return;
422 }
423
424 const int blocks_per_row = ncols / qk;
425 const int blocks_per_warp = vdr * WARP_SIZE / qi;
426 assert(blocks_per_warp>0);
427
428// partial sum for each thread
429 float tmp = 0.0f;
430
431 const block_q_t * x = (const block_q_t *) vx;
432 const block_q8_1 * y = (const block_q8_1 *) vy;
433
434 for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row;
435 i += blocks_per_warp) {
436 const int ibx = row*blocks_per_row + i; // x block index
437
438 const int iby = i * (qk/QK8_1); // y block index that aligns with ibx
439
440 const int iqs =
441 vdr *
442 (item_ct1.get_local_id(2) %
443 (qi / vdr)); // x block quant index when casting the quants to int
444
445 tmp += vec_dot_iq2_xxs_q8_1(&x[ibx], &y[iby], iqs, iq2xxs_grid, ksigns_iq2xs, kmask_iq2xs);
446 }
447
448 // sum up partial sums and write back result
449#pragma unroll
450 for (int mask = WARP_SIZE / 2; mask > 0; mask >>= 1) {
451 tmp +=
452 dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
453 }
454
455 if (item_ct1.get_local_id(2) == 0) {
456 dst[row] = tmp;
457 }
458}
459
460template <int qk, int qi, typename block_q_t, int vdr>
461static void mul_mat_vec_q_iq2_xs_q8_1(const void *__restrict__ vx,
462 const void *__restrict__ vy,
463 float *__restrict__ dst, const int ncols,
464 const int nrows,
465 const sycl::nd_item<3> &item_ct1) {
466 const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
467 item_ct1.get_local_id(1);
468
469 if (row >= nrows) {
470 return;
471 }
472
473 const int blocks_per_row = ncols / qk;
474 const int blocks_per_warp = vdr * WARP_SIZE / qi;
475 assert(blocks_per_warp>0);
476// partial sum for each thread
477 float tmp = 0.0f;
478
479 const block_q_t * x = (const block_q_t *) vx;
480 const block_q8_1 * y = (const block_q8_1 *) vy;
481
482 for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row;
483 i += blocks_per_warp) {
484 const int ibx = row*blocks_per_row + i; // x block index
485
486 const int iby = i * (qk/QK8_1); // y block index that aligns with ibx
487
488 const int iqs =
489 vdr *
490 (item_ct1.get_local_id(2) %
491 (qi / vdr)); // x block quant index when casting the quants to int
492
493 tmp += vec_dot_iq2_xs_q8_1(&x[ibx], &y[iby], iqs, iq2xs_grid);
494 }
495
496 // sum up partial sums and write back result
497#pragma unroll
498 for (int mask = WARP_SIZE / 2; mask > 0; mask >>= 1) {
499 tmp +=
500 dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
501 }
502
503 if (item_ct1.get_local_id(2) == 0) {
504 dst[row] = tmp;
505 }
506}
507
508template <int qk, int qi, typename block_q_t, int vdr>
509static void mul_mat_vec_q_iq2_s_q8_1(const void *__restrict__ vx,
510 const void *__restrict__ vy,
511 float *__restrict__ dst, const int ncols,
512 const int nrows,
513 const sycl::nd_item<3> &item_ct1) {
514 const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
515 item_ct1.get_local_id(1);
516
517 if (row >= nrows) {
518 return;
519 }
520
521 const int blocks_per_row = ncols / qk;
522 const int blocks_per_warp = vdr * WARP_SIZE / qi;
523 assert(blocks_per_warp>0);
524// partial sum for each thread
525 float tmp = 0.0f;
526
527 const block_q_t * x = (const block_q_t *) vx;
528 const block_q8_1 * y = (const block_q8_1 *) vy;
529
530 for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row;
531 i += blocks_per_warp) {
532 const int ibx = row*blocks_per_row + i; // x block index
533
534 const int iby = i * (qk/QK8_1); // y block index that aligns with ibx
535
536 const int iqs =
537 vdr *
538 (item_ct1.get_local_id(2) %
539 (qi / vdr)); // x block quant index when casting the quants to int
540
541 tmp += vec_dot_iq2_s_q8_1(&x[ibx], &y[iby], iqs);
542 }
543
544 // sum up partial sums and write back result
545#pragma unroll
546 for (int mask = WARP_SIZE / 2; mask > 0; mask >>= 1) {
547 tmp +=
548 dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
549 }
550
551 if (item_ct1.get_local_id(2) == 0) {
552 dst[row] = tmp;
553 }
554}
555
556template <int qk, int qi, typename block_q_t, int vdr>
557static void mul_mat_vec_q_iq3_xxs_q8_1(const void *__restrict__ vx,
558 const void *__restrict__ vy,
559 float *__restrict__ dst, const int ncols,
560 const int nrows,
561 const sycl::nd_item<3> &item_ct1) {
562 const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
563 item_ct1.get_local_id(1);
564
565 if (row >= nrows) {
566 return;
567 }
568
569 const int blocks_per_row = ncols / qk;
570 const int blocks_per_warp = vdr * WARP_SIZE / qi;
571 assert(blocks_per_warp>0);
572// partial sum for each thread
573 float tmp = 0.0f;
574
575 const block_q_t * x = (const block_q_t *) vx;
576 const block_q8_1 * y = (const block_q8_1 *) vy;
577
578 for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row;
579 i += blocks_per_warp) {
580 const int ibx = row*blocks_per_row + i; // x block index
581
582 const int iby = i * (qk/QK8_1); // y block index that aligns with ibx
583
584 const int iqs =
585 vdr *
586 (item_ct1.get_local_id(2) %
587 (qi / vdr)); // x block quant index when casting the quants to int
588
589 tmp += vec_dot_iq3_xxs_q8_1(&x[ibx], &y[iby], iqs, iq3xxs_grid);
590 }
591
592 // sum up partial sums and write back result
593#pragma unroll
594 for (int mask = WARP_SIZE / 2; mask > 0; mask >>= 1) {
595 tmp +=
596 dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
597 }
598
599 if (item_ct1.get_local_id(2) == 0) {
600 dst[row] = tmp;
601 }
602}
603
604static __dpct_inline__ float vec_dot_iq3_s_q8_1_adapter(
605 const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
606 return vec_dot_iq3_s_q8_1(vbq, bq8_1, iqs, iq3s_grid);
607}
608
609template <int qk, int qi, typename block_q_t, int vdr>
610static void mul_mat_vec_q_iq3_s_q8_1(const void *__restrict__ vx,
611 const void *__restrict__ vy,
612 float *__restrict__ dst, const int ncols,
613 const int nrows,
614 const sycl::nd_item<3> &item_ct1) {
615 const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
616 item_ct1.get_local_id(1);
617
618 if (row >= nrows) {
619 return;
620 }
621
622 const int blocks_per_row = ncols / qk;
623 const int blocks_per_warp = vdr * WARP_SIZE / qi;
624 assert(blocks_per_warp>0);
625// partial sum for each thread
626 float tmp = 0.0f;
627
628 const block_q_t * x = (const block_q_t *) vx;
629 const block_q8_1 * y = (const block_q8_1 *) vy;
630
631 for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row;
632 i += blocks_per_warp) {
633 const int ibx = row*blocks_per_row + i; // x block index
634
635 const int iby = i * (qk/QK8_1); // y block index that aligns with ibx
636
637 const int iqs =
638 vdr *
639 (item_ct1.get_local_id(2) %
640 (qi / vdr)); // x block quant index when casting the quants to int
641
642 tmp += vec_dot_iq3_s_q8_1(&x[ibx], &y[iby], iqs, iq3s_grid);
643 }
644
645 // sum up partial sums and write back result
646#pragma unroll
647 for (int mask = WARP_SIZE / 2; mask > 0; mask >>= 1) {
648 tmp +=
649 dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
650 }
651
652 if (item_ct1.get_local_id(2) == 0) {
653 dst[row] = tmp;
654 }
655}
656
657template <int qk, int qi, typename block_q_t, int vdr>
658static void mul_mat_vec_q_iq1_s_q8_1(const void *__restrict__ vx,
659 const void *__restrict__ vy,
660 float *__restrict__ dst, const int ncols,
661 const int nrows,
662 const sycl::nd_item<3> &item_ct1) {
663 const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
664 item_ct1.get_local_id(1);
665
666 if (row >= nrows) {
667 return;
668 }
669
670 const int blocks_per_row = ncols / qk;
671 const int blocks_per_warp = vdr * WARP_SIZE / qi;
672 assert(blocks_per_warp>0);
673// partial sum for each thread
674 float tmp = 0.0f;
675
676 const block_q_t * x = (const block_q_t *) vx;
677 const block_q8_1 * y = (const block_q8_1 *) vy;
678
679 for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row;
680 i += blocks_per_warp) {
681 const int ibx = row*blocks_per_row + i; // x block index
682
683 const int iby = i * (qk/QK8_1); // y block index that aligns with ibx
684
685 const int iqs =
686 vdr *
687 (item_ct1.get_local_id(2) %
688 (qi / vdr)); // x block quant index when casting the quants to int
689
690 tmp += vec_dot_iq1_s_q8_1(&x[ibx], &y[iby], iqs, iq1s_grid_gpu);
691 }
692
693 // sum up partial sums and write back result
694#pragma unroll
695 for (int mask = WARP_SIZE / 2; mask > 0; mask >>= 1) {
696 tmp +=
697 dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
698 }
699
700 if (item_ct1.get_local_id(2) == 0) {
701 dst[row] = tmp;
702 }
703}
704
705template <int qk, int qi, typename block_q_t, int vdr>
706static void mul_mat_vec_q_iq1_m_q8_1(const void *__restrict__ vx,
707 const void *__restrict__ vy,
708 float *__restrict__ dst, const int ncols,
709 const int nrows,
710 const sycl::nd_item<3> &item_ct1) {
711 const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
712 item_ct1.get_local_id(1);
713
714 if (row >= nrows) {
715 return;
716 }
717
718 const int blocks_per_row = ncols / qk;
719 const int blocks_per_warp = vdr * WARP_SIZE / qi;
720 assert(blocks_per_warp>0);
721// partial sum for each thread
722 float tmp = 0.0f;
723
724 const block_q_t * x = (const block_q_t *) vx;
725 const block_q8_1 * y = (const block_q8_1 *) vy;
726
727 for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row;
728 i += blocks_per_warp) {
729 const int ibx = row*blocks_per_row + i; // x block index
730
731 const int iby = i * (qk/QK8_1); // y block index that aligns with ibx
732
733 const int iqs =
734 vdr *
735 (item_ct1.get_local_id(2) %
736 (qi / vdr)); // x block quant index when casting the quants to int
737
738 tmp += vec_dot_iq1_m_q8_1(&x[ibx], &y[iby], iqs);
739 }
740
741 // sum up partial sums and write back result
742#pragma unroll
743 for (int mask = WARP_SIZE / 2; mask > 0; mask >>= 1) {
744 tmp +=
745 dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
746 }
747
748 if (item_ct1.get_local_id(2) == 0) {
749 dst[row] = tmp;
750 }
751}
752
753template <int qk, int qi, typename block_q_t, int vdr>
754static void mul_mat_vec_q_iq4_nl_q8_1(const void *__restrict__ vx,
755 const void *__restrict__ vy,
756 float *__restrict__ dst, const int ncols,
757 const int nrows,
758 const sycl::nd_item<3> &item_ct1) {
759 const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
760 item_ct1.get_local_id(1);
761
762 if (row >= nrows) {
763 return;
764 }
765
766 const int blocks_per_row = ncols / qk;
767 const int blocks_per_warp = vdr * WARP_SIZE / qi;
768 assert(blocks_per_warp>0);
769// partial sum for each thread
770 float tmp = 0.0f;
771
772 const block_q_t * x = (const block_q_t *) vx;
773 const block_q8_1 * y = (const block_q8_1 *) vy;
774
775 for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row;
776 i += blocks_per_warp) {
777 const int ibx = row*blocks_per_row + i; // x block index
778
779 const int iby = i * (qk/QK8_1); // y block index that aligns with ibx
780
781 const int iqs =
782 vdr *
783 (item_ct1.get_local_id(2) %
784 (qi / vdr)); // x block quant index when casting the quants to int
785
786 tmp += vec_dot_iq4_nl_q8_1(&x[ibx], &y[iby], iqs);
787 }
788
789 // sum up partial sums and write back result
790#pragma unroll
791 for (int mask = WARP_SIZE / 2; mask > 0; mask >>= 1) {
792 tmp +=
793 dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
794 }
795
796 if (item_ct1.get_local_id(2) == 0) {
797 dst[row] = tmp;
798 }
799}
800
801
802template <int qk, int qi, typename block_q_t, int vdr>
803static void mul_mat_vec_q_iq4_xs_q8_1(const void *__restrict__ vx,
804 const void *__restrict__ vy,
805 float *__restrict__ dst, const int ncols,
806 const int nrows,
807 const sycl::nd_item<3> &item_ct1) {
808 const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
809 item_ct1.get_local_id(1);
810
811 if (row >= nrows) {
812 return;
813 }
814
815 const int blocks_per_row = ncols / qk;
816 const int blocks_per_warp = vdr * WARP_SIZE / qi;
817 assert(blocks_per_warp>0);
818// partial sum for each thread
819 float tmp = 0.0f;
820
821 const block_q_t * x = (const block_q_t *) vx;
822 const block_q8_1 * y = (const block_q8_1 *) vy;
823
824 for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row;
825 i += blocks_per_warp) {
826 const int ibx = row*blocks_per_row + i; // x block index
827
828 const int iby = i * (qk/QK8_1); // y block index that aligns with ibx
829
830 const int iqs =
831 vdr *
832 (item_ct1.get_local_id(2) %
833 (qi / vdr)); // x block quant index when casting the quants to int
834
835 tmp += vec_dot_iq4_xs_q8_1(&x[ibx], &y[iby], iqs);
836 }
837
838 // sum up partial sums and write back result
839#pragma unroll
840 for (int mask = WARP_SIZE / 2; mask > 0; mask >>= 1) {
841 tmp +=
842 dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
843 }
844
845 if (item_ct1.get_local_id(2) == 0) {
846 dst[row] = tmp;
847 }
848}
849
850static void reorder_mul_mat_vec_q4_0_q8_1_sycl(const void * vx, const void * vy, float * dst, const int ncols,
851 const int nrows, dpct::queue_ptr stream) {
852 GGML_ASSERT(ncols % QK4_0 == 0);
853 // Round up to a whole number of subgroup-sized workgroups; out-of-range rows are skipped inside the kernel.
854 constexpr size_t num_subgroups = WARP_SIZE;
855 const int block_num_y = ceil_div(nrows, GGML_SYCL_MMV_Y * (int) num_subgroups);
856 const sycl::range<3> block_nums(1, 1, block_num_y);
857 const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, num_subgroups * WARP_SIZE);
858
859 stream->submit([&](sycl::handler & cgh) {
860 cgh.parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
861 [=](sycl::nd_item<3> nd_item) [[sycl::reqd_sub_group_size(WARP_SIZE)]] {
862 mul_mat_vec_q_reorder<reorder_vec_dot_q_sycl<GGML_TYPE_Q4_0>>(vx, vy, dst, ncols, nrows,
863 nd_item);
864 });
865 });
866}
867
868template <int ncols_dst>
869static void reorder_mul_mat_vec_q4_0_q8_1_sycl_ncols(
870 const void * vx, const void * vy, float * dst,
871 const int ncols, const int nrows,
872 const int stride_col_y_bytes, const int stride_col_dst,
873 dpct::queue_ptr stream) {
874 GGML_ASSERT(ncols % QK4_0 == 0);
875 constexpr size_t num_subgroups = WARP_SIZE;
876 const int block_num_y = ceil_div(nrows, GGML_SYCL_MMV_Y * (int) num_subgroups);
877 const sycl::range<3> block_nums(1, 1, block_num_y);
878 const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, num_subgroups * WARP_SIZE);
879
880 stream->submit([&](sycl::handler & cgh) {
881 cgh.parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
882 [=](sycl::nd_item<3> nd_item) [[sycl::reqd_sub_group_size(WARP_SIZE)]] {
883 mul_mat_vec_q_reorder_ncols<reorder_vec_dot_q_sycl<GGML_TYPE_Q4_0>, ncols_dst>(
884 vx, /*vgate=*/ nullptr, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst,
885 /*glu_op=*/ GGML_GLU_OP_SWIGLU, nd_item);
886 });
887 });
888}
889
890static void reorder_mul_mat_vec_q4_0_q8_1_sycl_switch_ncols(
891 const void * vx, const void * vy, float * dst,
892 const int ncols, const int nrows, const int ncols_dst,
893 const int stride_col_y_bytes, const int stride_col_dst,
894 dpct::queue_ptr stream) {
895 switch (ncols_dst) {
896 case 1: reorder_mul_mat_vec_q4_0_q8_1_sycl(vx, vy, dst, ncols, nrows, stream); break;
897 case 2: reorder_mul_mat_vec_q4_0_q8_1_sycl_ncols<2>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
898 case 3: reorder_mul_mat_vec_q4_0_q8_1_sycl_ncols<3>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
899 case 4: reorder_mul_mat_vec_q4_0_q8_1_sycl_ncols<4>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
900 case 5: reorder_mul_mat_vec_q4_0_q8_1_sycl_ncols<5>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
901 case 6: reorder_mul_mat_vec_q4_0_q8_1_sycl_ncols<6>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
902 case 7: reorder_mul_mat_vec_q4_0_q8_1_sycl_ncols<7>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
903 case 8: reorder_mul_mat_vec_q4_0_q8_1_sycl_ncols<8>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
904 default: GGML_ABORT("unsupported ncols_dst=%d for Q4_0 reorder multi-col MMVQ", ncols_dst);
905 }
906}
907
908static void mul_mat_vec_q4_0_q8_1_sycl(const void * vx, const void * vy, float * dst, const int ncols, const int nrows,
909 dpct::queue_ptr stream) {
910 launch_mul_mat_vec_q<QK4_0, QI4_0, block_q4_0, VDR_Q4_0_Q8_1_MMVQ, vec_dot_q4_0_q8_1, QK4_0>(
911 vx, vy, dst, ncols, nrows, 1, 0, 0, stream);
912}
913
914
915static void mul_mat_vec_q4_0_q8_1_sycl_switch_ncols(
916 const void * vx, const void * vy, float * dst,
917 const int ncols, const int nrows, const int ncols_dst,
918 const int stride_col_y, const int stride_col_dst,
919 dpct::queue_ptr stream) {
920 if (ncols_dst < 1 || ncols_dst > 8) {
921 GGML_ABORT("unsupported ncols_dst=%d for Q4_0 multi-col MMVQ", ncols_dst);
922 }
923 launch_mul_mat_vec_q<QK4_0, QI4_0, block_q4_0, VDR_Q4_0_Q8_1_MMVQ, vec_dot_q4_0_q8_1, QK4_0>(
924 vx, vy, dst, ncols, nrows, ncols_dst, stride_col_y, stride_col_dst, stream);
925}
926
927
928static void mul_mat_vec_q4_1_q8_1_sycl(const void *vx, const void *vy,
929 float *dst, const int ncols,
930 const int nrows,
931 dpct::queue_ptr stream) {
932 launch_mul_mat_vec_q<QK4_0, QI4_1, block_q4_1,
933 VDR_Q4_1_Q8_1_MMVQ, vec_dot_q4_1_q8_1, QK4_1>(
934 vx, vy, dst, ncols, nrows, 1, 0, 0, stream);
935}
936
937
938static void mul_mat_vec_q4_1_q8_1_sycl_switch_ncols(
939 const void * vx, const void * vy, float * dst,
940 const int ncols, const int nrows, const int ncols_dst,
941 const int stride_col_y, const int stride_col_dst,
942 dpct::queue_ptr stream) {
943 if (ncols_dst < 1 || ncols_dst > 8) {
944 GGML_ABORT("unsupported ncols_dst=%d for Q4_1 multi-col MMVQ", ncols_dst);
945 }
946 launch_mul_mat_vec_q<QK4_0, QI4_1, block_q4_1,
947 VDR_Q4_1_Q8_1_MMVQ, vec_dot_q4_1_q8_1, QK4_1>(
948 vx, vy, dst, ncols, nrows, ncols_dst, stride_col_y, stride_col_dst, stream);
949}
950
951
952static void mul_mat_vec_mxfp4_q8_1_sycl(const void * vx, const void * vy, float * dst, const int ncols, const int nrows,
953 dpct::queue_ptr stream) {
954 launch_mul_mat_vec_q<QK_MXFP4, QI_MXFP4, block_mxfp4, VDR_MXFP4_Q8_1_MMVQ, vec_dot_mxfp4_q8_1, QK_MXFP4>(
955 vx, vy, dst, ncols, nrows, 1, 0, 0, stream);
956}
957
958
959static void mul_mat_vec_mxfp4_q8_1_sycl_switch_ncols(
960 const void * vx, const void * vy, float * dst,
961 const int ncols, const int nrows, const int ncols_dst,
962 const int stride_col_y, const int stride_col_dst,
963 dpct::queue_ptr stream) {
964 if (ncols_dst < 1 || ncols_dst > 8) {
965 GGML_ABORT("unsupported ncols_dst=%d for MXFP4 multi-col MMVQ", ncols_dst);
966 }
967 launch_mul_mat_vec_q<QK_MXFP4, QI_MXFP4, block_mxfp4, VDR_MXFP4_Q8_1_MMVQ, vec_dot_mxfp4_q8_1, QK_MXFP4>(
968 vx, vy, dst, ncols, nrows, ncols_dst, stride_col_y, stride_col_dst, stream);
969}
970
971
972static void mul_mat_vec_nvfp4_q8_1_sycl(const void * vx, const void * vy, float * dst, const int ncols, const int nrows,
973 dpct::queue_ptr stream) {
974 launch_mul_mat_vec_q<QK_NVFP4, QI_NVFP4, block_nvfp4, VDR_NVFP4_Q8_1_MMVQ, vec_dot_nvfp4_q8_1, QK_NVFP4>(
975 vx, vy, dst, ncols, nrows, 1, 0, 0, stream);
976}
977
978
979static void mul_mat_vec_nvfp4_q8_1_sycl_switch_ncols(
980 const void * vx, const void * vy, float * dst,
981 const int ncols, const int nrows, const int ncols_dst,
982 const int stride_col_y, const int stride_col_dst,
983 dpct::queue_ptr stream) {
984 if (ncols_dst < 1 || ncols_dst > 8) {
985 GGML_ABORT("unsupported ncols_dst=%d for NVFP4 multi-col MMVQ", ncols_dst);
986 }
987 launch_mul_mat_vec_q<QK_NVFP4, QI_NVFP4, block_nvfp4, VDR_NVFP4_Q8_1_MMVQ, vec_dot_nvfp4_q8_1, QK_NVFP4>(
988 vx, vy, dst, ncols, nrows, ncols_dst, stride_col_y, stride_col_dst, stream);
989}
990
991
992static void mul_mat_vec_q5_0_q8_1_sycl(const void *vx, const void *vy,
993 float *dst, const int ncols,
994 const int nrows,
995 dpct::queue_ptr stream) {
996 launch_mul_mat_vec_q<QK5_0, QI5_0, block_q5_0,
997 VDR_Q5_0_Q8_1_MMVQ, vec_dot_q5_0_q8_1, QK5_0>(
998 vx, vy, dst, ncols, nrows, 1, 0, 0, stream);
999}
1000
1001
1002static void mul_mat_vec_q5_0_q8_1_sycl_switch_ncols(
1003 const void * vx, const void * vy, float * dst,
1004 const int ncols, const int nrows, const int ncols_dst,
1005 const int stride_col_y, const int stride_col_dst,
1006 dpct::queue_ptr stream) {
1007 if (ncols_dst < 1 || ncols_dst > 8) {
1008 GGML_ABORT("unsupported ncols_dst=%d for Q5_0 multi-col MMVQ", ncols_dst);
1009 }
1010 launch_mul_mat_vec_q<QK5_0, QI5_0, block_q5_0,
1011 VDR_Q5_0_Q8_1_MMVQ, vec_dot_q5_0_q8_1, QK5_0>(
1012 vx, vy, dst, ncols, nrows, ncols_dst, stride_col_y, stride_col_dst, stream);
1013}
1014
1015
1016static void mul_mat_vec_q5_1_q8_1_sycl(const void *vx, const void *vy,
1017 float *dst, const int ncols,
1018 const int nrows,
1019 dpct::queue_ptr stream) {
1020 launch_mul_mat_vec_q<QK5_1, QI5_1, block_q5_1,
1021 VDR_Q5_1_Q8_1_MMVQ, vec_dot_q5_1_q8_1, QK5_1>(
1022 vx, vy, dst, ncols, nrows, 1, 0, 0, stream);
1023}
1024
1025
1026static void mul_mat_vec_q5_1_q8_1_sycl_switch_ncols(
1027 const void * vx, const void * vy, float * dst,
1028 const int ncols, const int nrows, const int ncols_dst,
1029 const int stride_col_y, const int stride_col_dst,
1030 dpct::queue_ptr stream) {
1031 if (ncols_dst < 1 || ncols_dst > 8) {
1032 GGML_ABORT("unsupported ncols_dst=%d for Q5_1 multi-col MMVQ", ncols_dst);
1033 }
1034 launch_mul_mat_vec_q<QK5_1, QI5_1, block_q5_1,
1035 VDR_Q5_1_Q8_1_MMVQ, vec_dot_q5_1_q8_1, QK5_1>(
1036 vx, vy, dst, ncols, nrows, ncols_dst, stride_col_y, stride_col_dst, stream);
1037}
1038
1039
1040static void reorder_mul_mat_vec_q8_0_q8_1_sycl(const void * vx, const void * vy, float * dst, const int ncols,
1041 const int nrows, dpct::queue_ptr stream) {
1042 GGML_ASSERT(ncols % QK8_0 == 0);
1043 // Round up to a whole number of subgroup-sized workgroups; out-of-range rows are skipped inside the kernel.
1044 constexpr size_t num_subgroups = WARP_SIZE;
1045 const int block_num_y = ceil_div(nrows, GGML_SYCL_MMV_Y * (int) num_subgroups);
1046 const sycl::range<3> block_nums(1, 1, block_num_y);
1047 const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, num_subgroups * WARP_SIZE);
1048
1049 if (g_ggml_sycl_mmvq_wide) {
1050 stream->submit([&](sycl::handler & cgh) {
1051 cgh.parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
1052 [=](sycl::nd_item<3> nd_item) [[sycl::reqd_sub_group_size(WARP_SIZE)]] {
1053 mul_mat_vec_q_reorder<reorder_vec_dot_q8_0_wide>(vx, vy, dst, ncols, nrows, nd_item);
1054 });
1055 });
1056 return;
1057 }
1058
1059 stream->submit([&](sycl::handler & cgh) {
1060 cgh.parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
1061 [=](sycl::nd_item<3> nd_item) [[sycl::reqd_sub_group_size(WARP_SIZE)]] {
1062 mul_mat_vec_q_reorder<reorder_vec_dot_q_sycl<GGML_TYPE_Q8_0>>(vx, vy, dst, ncols, nrows,
1063 nd_item);
1064 });
1065 });
1066}
1067
1068template <int ncols_dst>
1069static void reorder_mul_mat_vec_q8_0_q8_1_sycl_ncols(
1070 const void * vx, const void * vy, float * dst,
1071 const int ncols, const int nrows,
1072 const int stride_col_y_bytes, const int stride_col_dst,
1073 dpct::queue_ptr stream) {
1074 GGML_ASSERT(ncols % QK8_0 == 0);
1075 constexpr size_t num_subgroups = WARP_SIZE;
1076 const int block_num_y = ceil_div(nrows, GGML_SYCL_MMV_Y * (int) num_subgroups);
1077 const sycl::range<3> block_nums(1, 1, block_num_y);
1078 const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, num_subgroups * WARP_SIZE);
1079
1080 stream->submit([&](sycl::handler & cgh) {
1081 cgh.parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
1082 [=](sycl::nd_item<3> nd_item) [[sycl::reqd_sub_group_size(WARP_SIZE)]] {
1083 mul_mat_vec_q_reorder_ncols<reorder_vec_dot_q_sycl<GGML_TYPE_Q8_0>, ncols_dst>(
1084 vx, /*vgate=*/ nullptr, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst,
1085 /*glu_op=*/ GGML_GLU_OP_SWIGLU, nd_item);
1086 });
1087 });
1088}
1089
1090static void reorder_mul_mat_vec_q8_0_q8_1_sycl_switch_ncols(
1091 const void * vx, const void * vy, float * dst,
1092 const int ncols, const int nrows, const int ncols_dst,
1093 const int stride_col_y_bytes, const int stride_col_dst,
1094 dpct::queue_ptr stream) {
1095 switch (ncols_dst) {
1096 case 1: reorder_mul_mat_vec_q8_0_q8_1_sycl(vx, vy, dst, ncols, nrows, stream); break;
1097 case 2: reorder_mul_mat_vec_q8_0_q8_1_sycl_ncols<2>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
1098 case 3: reorder_mul_mat_vec_q8_0_q8_1_sycl_ncols<3>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
1099 case 4: reorder_mul_mat_vec_q8_0_q8_1_sycl_ncols<4>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
1100 case 5: reorder_mul_mat_vec_q8_0_q8_1_sycl_ncols<5>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
1101 case 6: reorder_mul_mat_vec_q8_0_q8_1_sycl_ncols<6>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
1102 case 7: reorder_mul_mat_vec_q8_0_q8_1_sycl_ncols<7>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
1103 case 8: reorder_mul_mat_vec_q8_0_q8_1_sycl_ncols<8>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
1104 default: GGML_ABORT("unsupported ncols_dst=%d for Q8_0 reorder multi-col MMVQ", ncols_dst);
1105 }
1106}
1107
1108// MXFP4 reorder GEMV. Only MoE expert slices are reordered (opt_for_reorder_id); these dense entry
1109// points serve per-expert ggml_sycl_mul_mat calls from multi-token MUL_MAT_ID after that reorder.
1110static void reorder_mul_mat_vec_mxfp4_q8_1_sycl(const void * vx, const void * vy, float * dst, const int ncols,
1111 const int nrows, dpct::queue_ptr stream) {
1112 GGML_ASSERT(ncols % QK_MXFP4 == 0);
1113 constexpr size_t num_subgroups = WARP_SIZE;
1114 const int block_num_y = ceil_div(nrows, GGML_SYCL_MMV_Y * (int) num_subgroups);
1115 const sycl::range<3> block_nums(1, 1, block_num_y);
1116 const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, num_subgroups * WARP_SIZE);
1117
1118 stream->submit([&](sycl::handler & cgh) {
1119 cgh.parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
1120 [=](sycl::nd_item<3> nd_item) [[sycl::reqd_sub_group_size(WARP_SIZE)]] {
1121 mul_mat_vec_q_reorder<reorder_vec_dot_q_sycl<GGML_TYPE_MXFP4>>(vx, vy, dst, ncols, nrows,
1122 nd_item);
1123 });
1124 });
1125}
1126
1127template <int ncols_dst>
1128static void reorder_mul_mat_vec_mxfp4_q8_1_sycl_ncols(
1129 const void * vx, const void * vy, float * dst,
1130 const int ncols, const int nrows,
1131 const int stride_col_y_bytes, const int stride_col_dst,
1132 dpct::queue_ptr stream) {
1133 GGML_ASSERT(ncols % QK_MXFP4 == 0);
1134 constexpr size_t num_subgroups = WARP_SIZE;
1135 const int block_num_y = ceil_div(nrows, GGML_SYCL_MMV_Y * (int) num_subgroups);
1136 const sycl::range<3> block_nums(1, 1, block_num_y);
1137 const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, num_subgroups * WARP_SIZE);
1138
1139 stream->submit([&](sycl::handler & cgh) {
1140 cgh.parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
1141 [=](sycl::nd_item<3> nd_item) [[sycl::reqd_sub_group_size(WARP_SIZE)]] {
1142 mul_mat_vec_q_reorder_ncols<reorder_vec_dot_q_sycl<GGML_TYPE_MXFP4>, ncols_dst>(
1143 vx, /*vgate=*/ nullptr, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst,
1144 /*glu_op=*/ GGML_GLU_OP_SWIGLU, nd_item);
1145 });
1146 });
1147}
1148
1149static void reorder_mul_mat_vec_mxfp4_q8_1_sycl_switch_ncols(
1150 const void * vx, const void * vy, float * dst,
1151 const int ncols, const int nrows, const int ncols_dst,
1152 const int stride_col_y_bytes, const int stride_col_dst,
1153 dpct::queue_ptr stream) {
1154 switch (ncols_dst) {
1155 case 1: reorder_mul_mat_vec_mxfp4_q8_1_sycl(vx, vy, dst, ncols, nrows, stream); break;
1156 case 2: reorder_mul_mat_vec_mxfp4_q8_1_sycl_ncols<2>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
1157 case 3: reorder_mul_mat_vec_mxfp4_q8_1_sycl_ncols<3>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
1158 case 4: reorder_mul_mat_vec_mxfp4_q8_1_sycl_ncols<4>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
1159 case 5: reorder_mul_mat_vec_mxfp4_q8_1_sycl_ncols<5>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
1160 case 6: reorder_mul_mat_vec_mxfp4_q8_1_sycl_ncols<6>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
1161 case 7: reorder_mul_mat_vec_mxfp4_q8_1_sycl_ncols<7>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
1162 case 8: reorder_mul_mat_vec_mxfp4_q8_1_sycl_ncols<8>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
1163 default: GGML_ABORT("unsupported ncols_dst=%d for MXFP4 reorder multi-col MMVQ", ncols_dst);
1164 }
1165}
1166
1167static void mul_mat_vec_q8_0_q8_1_sycl(const void *vx, const void *vy,
1168 float *dst, const int ncols,
1169 const int nrows,
1170 dpct::queue_ptr stream) {
1171 launch_mul_mat_vec_q<QK8_0, QI8_0, block_q8_0,
1172 VDR_Q8_0_Q8_1_MMVQ, vec_dot_q8_0_q8_1, QK8_0>(
1173 vx, vy, dst, ncols, nrows, 1, 0, 0, stream);
1174}
1175
1176
1177static void mul_mat_vec_q8_0_q8_1_sycl_switch_ncols(
1178 const void * vx, const void * vy, float * dst,
1179 const int ncols, const int nrows, const int ncols_dst,
1180 const int stride_col_y, const int stride_col_dst,
1181 dpct::queue_ptr stream) {
1182 if (ncols_dst < 1 || ncols_dst > 8) {
1183 GGML_ABORT("unsupported ncols_dst=%d for Q8_0 multi-col MMVQ", ncols_dst);
1184 }
1185 launch_mul_mat_vec_q<QK8_0, QI8_0, block_q8_0,
1186 VDR_Q8_0_Q8_1_MMVQ, vec_dot_q8_0_q8_1, QK8_0>(
1187 vx, vy, dst, ncols, nrows, ncols_dst, stride_col_y, stride_col_dst, stream);
1188}
1189
1190
1191static void mul_mat_vec_q1_0_q8_1_sycl(const void * vx, const void * vy,
1192 float * dst, const int ncols,
1193 const int nrows,
1194 dpct::queue_ptr stream) {
1195 launch_mul_mat_vec_q<QK1_0, QI1_0, block_q1_0,
1196 VDR_Q1_0_Q8_1_MMVQ, vec_dot_q1_0_q8_1, QK1_0>(
1197 vx, vy, dst, ncols, nrows, 1, 0, 0, stream);
1198}
1199
1200
1201static void mul_mat_vec_q1_0_q8_1_sycl_switch_ncols(
1202 const void * vx, const void * vy, float * dst,
1203 const int ncols, const int nrows, const int ncols_dst,
1204 const int stride_col_y, const int stride_col_dst,
1205 dpct::queue_ptr stream) {
1206 if (ncols_dst < 1 || ncols_dst > 8) {
1207 GGML_ABORT("unsupported ncols_dst=%d for Q1_0 multi-col MMVQ", ncols_dst);
1208 }
1209 launch_mul_mat_vec_q<QK1_0, QI1_0, block_q1_0,
1210 VDR_Q1_0_Q8_1_MMVQ, vec_dot_q1_0_q8_1, QK1_0>(
1211 vx, vy, dst, ncols, nrows, ncols_dst, stride_col_y, stride_col_dst, stream);
1212}
1213
1214
1215static void mul_mat_vec_q2_0_q8_1_sycl(const void * vx, const void * vy,
1216 float * dst, const int ncols,
1217 const int nrows,
1218 dpct::queue_ptr stream) {
1219 launch_mul_mat_vec_q<QK2_0, QI2_0, block_q2_0,
1220 VDR_Q2_0_Q8_1_MMVQ, vec_dot_q2_0_q8_1, QK2_0>(
1221 vx, vy, dst, ncols, nrows, 1, 0, 0, stream);
1222}
1223
1224
1225static void mul_mat_vec_q2_0_q8_1_sycl_switch_ncols(
1226 const void * vx, const void * vy, float * dst,
1227 const int ncols, const int nrows, const int ncols_dst,
1228 const int stride_col_y, const int stride_col_dst,
1229 dpct::queue_ptr stream) {
1230 if (ncols_dst < 1 || ncols_dst > 8) {
1231 GGML_ABORT("unsupported ncols_dst=%d for Q2_0 multi-col MMVQ", ncols_dst);
1232 }
1233 launch_mul_mat_vec_q<QK2_0, QI2_0, block_q2_0,
1234 VDR_Q2_0_Q8_1_MMVQ, vec_dot_q2_0_q8_1, QK2_0>(
1235 vx, vy, dst, ncols, nrows, ncols_dst, stride_col_y, stride_col_dst, stream);
1236}
1237
1238
1239static void mul_mat_vec_q2_K_q8_1_sycl(const void *vx, const void *vy,
1240 float *dst, const int ncols,
1241 const int nrows,
1242 dpct::queue_ptr stream) {
1243 launch_mul_mat_vec_q<QK_K, QI2_K, block_q2_K,
1244 VDR_Q2_K_Q8_1_MMVQ, vec_dot_q2_K_q8_1, QK_K>(
1245 vx, vy, dst, ncols, nrows, 1, 0, 0, stream);
1246}
1247
1248
1249static void mul_mat_vec_q2_K_q8_1_sycl_switch_ncols(
1250 const void * vx, const void * vy, float * dst,
1251 const int ncols, const int nrows, const int ncols_dst,
1252 const int stride_col_y, const int stride_col_dst,
1253 dpct::queue_ptr stream) {
1254 if (ncols_dst < 1 || ncols_dst > 8) {
1255 GGML_ABORT("unsupported ncols_dst=%d for Q2_K multi-col MMVQ", ncols_dst);
1256 }
1257 launch_mul_mat_vec_q<QK_K, QI2_K, block_q2_K,
1258 VDR_Q2_K_Q8_1_MMVQ, vec_dot_q2_K_q8_1, QK_K>(
1259 vx, vy, dst, ncols, nrows, ncols_dst, stride_col_y, stride_col_dst, stream);
1260}
1261
1262
1263static void reorder_mul_mat_vec_q2_k_q8_1_sycl(const void * vx, const void * vy, float * dst, const int ncols,
1264 const int nrows, dpct::queue_ptr stream) {
1265 GGML_ASSERT(ncols % QK_K == 0);
1266
1267 // Round up to a whole number of subgroup-sized workgroups; out-of-range rows are skipped inside the kernel.
1268 constexpr size_t num_subgroups = WARP_SIZE;
1269 const int block_num_y = ceil_div(nrows, GGML_SYCL_MMV_Y * (int) num_subgroups);
1270 const sycl::range<3> block_nums(1, 1, block_num_y);
1271 const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, num_subgroups * WARP_SIZE);
1272
1273 stream->submit([&](sycl::handler & cgh) {
1274 cgh.parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
1275 [=](sycl::nd_item<3> nd_item) [[sycl::reqd_sub_group_size(WARP_SIZE)]] {
1276 mul_mat_vec_q_reorder<reorder_vec_dot_q_sycl<GGML_TYPE_Q2_K>>(vx, vy, dst, ncols, nrows,
1277 nd_item);
1278 });
1279 });
1280}
1281
1282template <int ncols_dst>
1283static void reorder_mul_mat_vec_q2_k_q8_1_sycl_ncols(
1284 const void * vx, const void * vy, float * dst,
1285 const int ncols, const int nrows,
1286 const int stride_col_y_bytes, const int stride_col_dst,
1287 dpct::queue_ptr stream) {
1288 GGML_ASSERT(ncols % QK_K == 0);
1289 constexpr size_t num_subgroups = WARP_SIZE;
1290 const int block_num_y = ceil_div(nrows, GGML_SYCL_MMV_Y * (int) num_subgroups);
1291 const sycl::range<3> block_nums(1, 1, block_num_y);
1292 const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, num_subgroups * WARP_SIZE);
1293
1294 stream->submit([&](sycl::handler & cgh) {
1295 cgh.parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
1296 [=](sycl::nd_item<3> nd_item) [[sycl::reqd_sub_group_size(WARP_SIZE)]] {
1297 mul_mat_vec_q_reorder_ncols<reorder_vec_dot_q_sycl<GGML_TYPE_Q2_K>, ncols_dst>(
1298 vx, /*vgate=*/ nullptr, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst,
1299 /*glu_op=*/ GGML_GLU_OP_SWIGLU, nd_item);
1300 });
1301 });
1302}
1303
1304static void reorder_mul_mat_vec_q2_k_q8_1_sycl_switch_ncols(
1305 const void * vx, const void * vy, float * dst,
1306 const int ncols, const int nrows, const int ncols_dst,
1307 const int stride_col_y_bytes, const int stride_col_dst,
1308 dpct::queue_ptr stream) {
1309 switch (ncols_dst) {
1310 case 1: reorder_mul_mat_vec_q2_k_q8_1_sycl(vx, vy, dst, ncols, nrows, stream); break;
1311 case 2: reorder_mul_mat_vec_q2_k_q8_1_sycl_ncols<2>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
1312 case 3: reorder_mul_mat_vec_q2_k_q8_1_sycl_ncols<3>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
1313 case 4: reorder_mul_mat_vec_q2_k_q8_1_sycl_ncols<4>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
1314 case 5: reorder_mul_mat_vec_q2_k_q8_1_sycl_ncols<5>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
1315 case 6: reorder_mul_mat_vec_q2_k_q8_1_sycl_ncols<6>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
1316 case 7: reorder_mul_mat_vec_q2_k_q8_1_sycl_ncols<7>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
1317 case 8: reorder_mul_mat_vec_q2_k_q8_1_sycl_ncols<8>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
1318 default: GGML_ABORT("unsupported ncols_dst=%d for Q2_K reorder multi-col MMVQ", ncols_dst);
1319 }
1320}
1321
1322static void mul_mat_vec_q3_K_q8_1_sycl(const void *vx, const void *vy,
1323 float *dst, const int ncols,
1324 const int nrows,
1325 dpct::queue_ptr stream) {
1326 launch_mul_mat_vec_q<QK_K, QI3_K, block_q3_K,
1327 VDR_Q3_K_Q8_1_MMVQ, vec_dot_q3_K_q8_1, QK_K>(
1328 vx, vy, dst, ncols, nrows, 1, 0, 0, stream);
1329}
1330
1331
1332static void reorder_mul_mat_vec_q3_k_q8_1_sycl(const void * vx, const void * vy, float * dst, const int ncols,
1333 const int nrows, dpct::queue_ptr stream) {
1334 GGML_ASSERT(ncols % QK_K == 0);
1335
1336 // Round up to a whole number of subgroup-sized workgroups; out-of-range rows are skipped inside the kernel.
1337 constexpr size_t num_subgroups = WARP_SIZE;
1338 const int block_num_y = ceil_div(nrows, GGML_SYCL_MMV_Y * (int) num_subgroups);
1339 const sycl::range<3> block_nums(1, 1, block_num_y);
1340 const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, num_subgroups * WARP_SIZE);
1341
1342 stream->submit([&](sycl::handler & cgh) {
1343 cgh.parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
1344 [=](sycl::nd_item<3> nd_item) [[sycl::reqd_sub_group_size(WARP_SIZE)]] {
1345 mul_mat_vec_q_reorder<reorder_vec_dot_q_sycl<GGML_TYPE_Q3_K>>(vx, vy, dst, ncols, nrows,
1346 nd_item);
1347 });
1348 });
1349}
1350
1351template <int ncols_dst>
1352static void reorder_mul_mat_vec_q3_k_q8_1_sycl_ncols(
1353 const void * vx, const void * vy, float * dst,
1354 const int ncols, const int nrows,
1355 const int stride_col_y_bytes, const int stride_col_dst,
1356 dpct::queue_ptr stream) {
1357 GGML_ASSERT(ncols % QK_K == 0);
1358 constexpr size_t num_subgroups = WARP_SIZE;
1359 const int block_num_y = ceil_div(nrows, GGML_SYCL_MMV_Y * (int) num_subgroups);
1360 const sycl::range<3> block_nums(1, 1, block_num_y);
1361 const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, num_subgroups * WARP_SIZE);
1362
1363 stream->submit([&](sycl::handler & cgh) {
1364 cgh.parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
1365 [=](sycl::nd_item<3> nd_item) [[sycl::reqd_sub_group_size(WARP_SIZE)]] {
1366 mul_mat_vec_q_reorder_ncols<reorder_vec_dot_q_sycl<GGML_TYPE_Q3_K>, ncols_dst>(
1367 vx, /*vgate=*/ nullptr, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst,
1368 /*glu_op=*/ GGML_GLU_OP_SWIGLU, nd_item);
1369 });
1370 });
1371}
1372
1373static void reorder_mul_mat_vec_q3_k_q8_1_sycl_switch_ncols(
1374 const void * vx, const void * vy, float * dst,
1375 const int ncols, const int nrows, const int ncols_dst,
1376 const int stride_col_y_bytes, const int stride_col_dst,
1377 dpct::queue_ptr stream) {
1378 switch (ncols_dst) {
1379 case 1: reorder_mul_mat_vec_q3_k_q8_1_sycl(vx, vy, dst, ncols, nrows, stream); break;
1380 case 2: reorder_mul_mat_vec_q3_k_q8_1_sycl_ncols<2>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
1381 case 3: reorder_mul_mat_vec_q3_k_q8_1_sycl_ncols<3>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
1382 case 4: reorder_mul_mat_vec_q3_k_q8_1_sycl_ncols<4>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
1383 case 5: reorder_mul_mat_vec_q3_k_q8_1_sycl_ncols<5>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
1384 case 6: reorder_mul_mat_vec_q3_k_q8_1_sycl_ncols<6>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
1385 case 7: reorder_mul_mat_vec_q3_k_q8_1_sycl_ncols<7>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
1386 case 8: reorder_mul_mat_vec_q3_k_q8_1_sycl_ncols<8>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
1387 default: GGML_ABORT("unsupported ncols_dst=%d for Q3_K reorder multi-col MMVQ", ncols_dst);
1388 }
1389}
1390
1391static void mul_mat_vec_q3_K_q8_1_sycl_switch_ncols(
1392 const void * vx, const void * vy, float * dst,
1393 const int ncols, const int nrows, const int ncols_dst,
1394 const int stride_col_y, const int stride_col_dst,
1395 dpct::queue_ptr stream) {
1396 if (ncols_dst < 1 || ncols_dst > 8) {
1397 GGML_ABORT("unsupported ncols_dst=%d for Q3_K multi-col MMVQ", ncols_dst);
1398 }
1399 launch_mul_mat_vec_q<QK_K, QI3_K, block_q3_K,
1400 VDR_Q3_K_Q8_1_MMVQ, vec_dot_q3_K_q8_1, QK_K>(
1401 vx, vy, dst, ncols, nrows, ncols_dst, stride_col_y, stride_col_dst, stream);
1402}
1403
1404
1405
1406static void mul_mat_vec_q4_K_q8_1_sycl(const void *vx, const void *vy,
1407 float *dst, const int ncols,
1408 const int nrows,
1409 dpct::queue_ptr stream) {
1410 launch_mul_mat_vec_q<QK_K, QI4_K, block_q4_K,
1411 VDR_Q4_K_Q8_1_MMVQ, vec_dot_q4_K_q8_1, QK_K>(
1412 vx, vy, dst, ncols, nrows, 1, 0, 0, stream);
1413}
1414
1415
1416static void mul_mat_vec_q4_K_q8_1_sycl_switch_ncols(
1417 const void * vx, const void * vy, float * dst,
1418 const int ncols, const int nrows,
1419 const int ncols_dst,
1420 const int stride_col_y, const int stride_col_dst,
1421 dpct::queue_ptr stream) {
1422 if (ncols_dst < 1 || ncols_dst > 8) {
1423 GGML_ABORT("unsupported ncols_dst=%d for Q4_K multi-col MMVQ", ncols_dst);
1424 }
1425 launch_mul_mat_vec_q<QK_K, QI4_K, block_q4_K,
1426 VDR_Q4_K_Q8_1_MMVQ, vec_dot_q4_K_q8_1, QK_K>(
1427 vx, vy, dst, ncols, nrows, ncols_dst, stride_col_y, stride_col_dst, stream);
1428}
1429
1430
1431static void reorder_mul_mat_vec_q4_k_q8_1_sycl(const void * vx, const void * vy, float * dst, const int ncols,
1432 const int nrows, dpct::queue_ptr stream) {
1433 GGML_ASSERT(ncols % QK_K == 0);
1434
1435 // Round up to a whole number of subgroup-sized workgroups; out-of-range rows are skipped inside the kernel.
1436 constexpr size_t num_subgroups = WARP_SIZE;
1437 const int block_num_y = ceil_div(nrows, GGML_SYCL_MMV_Y * (int) num_subgroups);
1438 const sycl::range<3> block_nums(1, 1, block_num_y);
1439 const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, num_subgroups * WARP_SIZE);
1440
1441 stream->submit([&](sycl::handler & cgh) {
1442 cgh.parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
1443 [=](sycl::nd_item<3> nd_item) [[sycl::reqd_sub_group_size(WARP_SIZE)]] {
1444 mul_mat_vec_q_reorder<reorder_vec_dot_q_sycl<GGML_TYPE_Q4_K>>(vx, vy, dst, ncols,
1445 nrows, nd_item);
1446 });
1447 });
1448}
1449
1450template <int ncols_dst, int rows_per_sg>
1451static void reorder_mul_mat_vec_q4_k_q8_1_sycl_ncols_impl(
1452 const void * vx, const void * vy, float * dst,
1453 const int ncols, const int nrows,
1454 const int stride_col_y_bytes, const int stride_col_dst,
1455 dpct::queue_ptr stream) {
1456 GGML_ASSERT(ncols % QK_K == 0);
1457
1458 constexpr size_t num_subgroups = WARP_SIZE;
1459 const int block_num_y = ceil_div(nrows, GGML_SYCL_MMV_Y * (int) num_subgroups * rows_per_sg);
1460 const sycl::range<3> block_nums(1, 1, block_num_y);
1461 const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, num_subgroups * WARP_SIZE);
1462
1463 stream->submit([&](sycl::handler & cgh) {
1464 cgh.parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
1465 [=](sycl::nd_item<3> nd_item) [[sycl::reqd_sub_group_size(WARP_SIZE)]] {
1466 mul_mat_vec_q_reorder_ncols<reorder_vec_dot_q_sycl<GGML_TYPE_Q4_K>, ncols_dst,
1467 /*has_fusion=*/ false, rows_per_sg>(
1468 vx, /*vgate=*/ nullptr, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst,
1469 /*glu_op=*/ GGML_GLU_OP_SWIGLU, nd_item);
1470 });
1471 });
1472}
1473
1474template <int ncols_dst>
1475static void reorder_mul_mat_vec_q4_k_q8_1_sycl_ncols(
1476 const void * vx, const void * vy, float * dst,
1477 const int ncols, const int nrows,
1478 const int stride_col_y_bytes, const int stride_col_dst,
1479 dpct::queue_ptr stream) {
1480 constexpr int rows_per_sg = ncols_dst >= 3 && ncols_dst <= 4 ? 2 : 1;
1481 reorder_mul_mat_vec_q4_k_q8_1_sycl_ncols_impl<ncols_dst, rows_per_sg>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream);
1482}
1483
1484static void reorder_mul_mat_vec_q4_k_q8_1_sycl_switch_ncols(
1485 const void * vx, const void * vy, float * dst,
1486 const int ncols, const int nrows, const int ncols_dst,
1487 const int stride_col_y_bytes, const int stride_col_dst,
1488 dpct::queue_ptr stream) {
1489 switch (ncols_dst) {
1490 case 1: reorder_mul_mat_vec_q4_k_q8_1_sycl(vx, vy, dst, ncols, nrows, stream); break;
1491 case 2:
1492 if (nrows >= Q4_K_MMVQ_ROW_PAIR_MIN_NROWS) {
1493 reorder_mul_mat_vec_q4_k_q8_1_sycl_ncols_impl<2, 2>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream);
1494 } else {
1495 reorder_mul_mat_vec_q4_k_q8_1_sycl_ncols_impl<2, 1>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream);
1496 }
1497 break;
1498 case 3: reorder_mul_mat_vec_q4_k_q8_1_sycl_ncols<3>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
1499 case 4: reorder_mul_mat_vec_q4_k_q8_1_sycl_ncols<4>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
1500 case 5: reorder_mul_mat_vec_q4_k_q8_1_sycl_ncols<5>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
1501 case 6: reorder_mul_mat_vec_q4_k_q8_1_sycl_ncols<6>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
1502 case 7: reorder_mul_mat_vec_q4_k_q8_1_sycl_ncols<7>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
1503 case 8: reorder_mul_mat_vec_q4_k_q8_1_sycl_ncols<8>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
1504 default: GGML_ABORT("unsupported ncols_dst=%d for Q4_K reorder multi-col MMVQ", ncols_dst);
1505 }
1506}
1507
1508static void mul_mat_vec_q5_K_q8_1_sycl(const void *vx, const void *vy,
1509 float *dst, const int ncols,
1510 const int nrows,
1511 dpct::queue_ptr stream) {
1512 launch_mul_mat_vec_q<QK_K, QI5_K, block_q5_K,
1513 VDR_Q5_K_Q8_1_MMVQ, vec_dot_q5_K_q8_1, QK_K>(
1514 vx, vy, dst, ncols, nrows, 1, 0, 0, stream);
1515}
1516
1517
1518static void mul_mat_vec_q5_K_q8_1_sycl_switch_ncols(
1519 const void * vx, const void * vy, float * dst,
1520 const int ncols, const int nrows,
1521 const int ncols_dst,
1522 const int stride_col_y, const int stride_col_dst,
1523 dpct::queue_ptr stream) {
1524 if (ncols_dst < 1 || ncols_dst > 8) {
1525 GGML_ABORT("unsupported ncols_dst=%d for Q5_K multi-col MMVQ", ncols_dst);
1526 }
1527 launch_mul_mat_vec_q<QK_K, QI5_K, block_q5_K,
1528 VDR_Q5_K_Q8_1_MMVQ, vec_dot_q5_K_q8_1, QK_K>(
1529 vx, vy, dst, ncols, nrows, ncols_dst, stride_col_y, stride_col_dst, stream);
1530}
1531
1532
1533static void reorder_mul_mat_vec_q5_k_q8_1_sycl(const void * vx, const void * vy, float * dst, const int ncols,
1534 const int nrows, dpct::queue_ptr stream) {
1535 GGML_ASSERT(ncols % QK_K == 0);
1536
1537 constexpr size_t num_subgroups = WARP_SIZE;
1538 const int block_num_y = ceil_div(nrows, GGML_SYCL_MMV_Y * (int) num_subgroups);
1539 const sycl::range<3> block_nums(1, 1, block_num_y);
1540 const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, num_subgroups * WARP_SIZE);
1541
1542 stream->submit([&](sycl::handler & cgh) {
1543 cgh.parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
1544 [=](sycl::nd_item<3> nd_item) [[sycl::reqd_sub_group_size(WARP_SIZE)]] {
1545 mul_mat_vec_q_reorder<reorder_vec_dot_q_sycl<GGML_TYPE_Q5_K>>(vx, vy, dst, ncols,
1546 nrows, nd_item);
1547 });
1548 });
1549}
1550
1551template <int ncols_dst, int rows_per_sg, bool shared_weights>
1552static void reorder_mul_mat_vec_q5_k_q8_1_sycl_ncols_impl(
1553 const void * vx, const void * vy, float * dst,
1554 const int ncols, const int nrows,
1555 const int stride_col_y_bytes, const int stride_col_dst,
1556 dpct::queue_ptr stream) {
1557 GGML_ASSERT(ncols % QK_K == 0);
1558
1559 constexpr size_t num_subgroups = WARP_SIZE;
1560 const int block_num_y = ceil_div(nrows, GGML_SYCL_MMV_Y * (int) num_subgroups * rows_per_sg);
1561 const sycl::range<3> block_nums(1, 1, block_num_y);
1562 const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, num_subgroups * WARP_SIZE);
1563
1564 stream->submit([&](sycl::handler & cgh) {
1565 cgh.parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
1566 [=](sycl::nd_item<3> nd_item) [[sycl::reqd_sub_group_size(WARP_SIZE)]] {
1567 mul_mat_vec_q_reorder_ncols<reorder_vec_dot_q_sycl<GGML_TYPE_Q5_K>, ncols_dst,
1568 /*has_fusion=*/ false, rows_per_sg, shared_weights>(
1569 vx, /*vgate=*/ nullptr, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst,
1570 /*glu_op=*/ GGML_GLU_OP_SWIGLU, nd_item);
1571 });
1572 });
1573}
1574
1575template <int ncols_dst>
1576static void reorder_mul_mat_vec_q5_k_q8_1_sycl_ncols(
1577 const void * vx, const void * vy, float * dst,
1578 const int ncols, const int nrows,
1579 const int stride_col_y_bytes, const int stride_col_dst,
1580 dpct::queue_ptr stream) {
1581 if (ggml_sycl_q5_k_mmvq_reuse(ggml_sycl_get_device())) {
1582 constexpr int rows_per_sg = ncols_dst >= 3 ? 2 : 1;
1583 reorder_mul_mat_vec_q5_k_q8_1_sycl_ncols_impl<ncols_dst, rows_per_sg, true>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream);
1584 } else {
1585 reorder_mul_mat_vec_q5_k_q8_1_sycl_ncols_impl<ncols_dst, 1, false>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream);
1586 }
1587}
1588
1589static void reorder_mul_mat_vec_q5_k_q8_1_sycl_switch_ncols(
1590 const void * vx, const void * vy, float * dst,
1591 const int ncols, const int nrows, const int ncols_dst,
1592 const int stride_col_y_bytes, const int stride_col_dst,
1593 dpct::queue_ptr stream) {
1594 switch (ncols_dst) {
1595 case 1: reorder_mul_mat_vec_q5_k_q8_1_sycl(vx, vy, dst, ncols, nrows, stream); break;
1596 case 2: reorder_mul_mat_vec_q5_k_q8_1_sycl_ncols<2>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
1597 case 3: reorder_mul_mat_vec_q5_k_q8_1_sycl_ncols<3>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
1598 case 4: reorder_mul_mat_vec_q5_k_q8_1_sycl_ncols<4>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
1599 case 5: reorder_mul_mat_vec_q5_k_q8_1_sycl_ncols<5>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
1600 case 6: reorder_mul_mat_vec_q5_k_q8_1_sycl_ncols<6>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
1601 case 7: reorder_mul_mat_vec_q5_k_q8_1_sycl_ncols<7>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
1602 case 8: reorder_mul_mat_vec_q5_k_q8_1_sycl_ncols<8>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
1603 default: GGML_ABORT("unsupported ncols_dst=%d for Q5_K reorder multi-col MMVQ", ncols_dst);
1604 }
1605}
1606
1607static void reorder_mul_mat_vec_q6_k_q8_1_sycl(const void * vx, const void * vy, float * dst, const int ncols,
1608 const int nrows, dpct::queue_ptr stream) {
1609 GGML_ASSERT(ncols % QK_K == 0);
1610 // Round up to a whole number of subgroup-sized workgroups; out-of-range rows are skipped inside the kernel.
1611 constexpr size_t num_subgroups = WARP_SIZE;
1612 const int block_num_y = ceil_div(nrows, GGML_SYCL_MMV_Y * (int) num_subgroups);
1613 const sycl::range<3> block_nums(1, 1, block_num_y);
1614 const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, num_subgroups * WARP_SIZE);
1615
1616
1617 stream->submit([&](sycl::handler & cgh) {
1618 cgh.parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
1619 [=](sycl::nd_item<3> nd_item) [[sycl::reqd_sub_group_size(WARP_SIZE)]] {
1620 mul_mat_vec_q_reorder<reorder_vec_dot_q_sycl<GGML_TYPE_Q6_K>>(vx, vy, dst, ncols, nrows,
1621 nd_item);
1622 });
1623 });
1624}
1625
1626template <int ncols_dst>
1627static void reorder_mul_mat_vec_q6_k_q8_1_sycl_ncols(
1628 const void * vx, const void * vy, float * dst,
1629 const int ncols, const int nrows,
1630 const int stride_col_y_bytes, const int stride_col_dst,
1631 dpct::queue_ptr stream) {
1632 GGML_ASSERT(ncols % QK_K == 0);
1633 constexpr size_t num_subgroups = WARP_SIZE;
1634 const int block_num_y = ceil_div(nrows, GGML_SYCL_MMV_Y * (int) num_subgroups);
1635 const sycl::range<3> block_nums(1, 1, block_num_y);
1636 const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, num_subgroups * WARP_SIZE);
1637
1638 stream->submit([&](sycl::handler & cgh) {
1639 cgh.parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
1640 [=](sycl::nd_item<3> nd_item) [[sycl::reqd_sub_group_size(WARP_SIZE)]] {
1641 mul_mat_vec_q_reorder_ncols<reorder_vec_dot_q_sycl<GGML_TYPE_Q6_K>, ncols_dst>(
1642 vx, /*vgate=*/ nullptr, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst,
1643 /*glu_op=*/ GGML_GLU_OP_SWIGLU, nd_item);
1644 });
1645 });
1646}
1647
1648static void reorder_mul_mat_vec_q6_k_q8_1_sycl_switch_ncols(
1649 const void * vx, const void * vy, float * dst,
1650 const int ncols, const int nrows, const int ncols_dst,
1651 const int stride_col_y_bytes, const int stride_col_dst,
1652 dpct::queue_ptr stream) {
1653 switch (ncols_dst) {
1654 case 1: reorder_mul_mat_vec_q6_k_q8_1_sycl(vx, vy, dst, ncols, nrows, stream); break;
1655 case 2: reorder_mul_mat_vec_q6_k_q8_1_sycl_ncols<2>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
1656 case 3: reorder_mul_mat_vec_q6_k_q8_1_sycl_ncols<3>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
1657 case 4: reorder_mul_mat_vec_q6_k_q8_1_sycl_ncols<4>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
1658 case 5: reorder_mul_mat_vec_q6_k_q8_1_sycl_ncols<5>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
1659 case 6: reorder_mul_mat_vec_q6_k_q8_1_sycl_ncols<6>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
1660 case 7: reorder_mul_mat_vec_q6_k_q8_1_sycl_ncols<7>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
1661 case 8: reorder_mul_mat_vec_q6_k_q8_1_sycl_ncols<8>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
1662 default: GGML_ABORT("unsupported ncols_dst=%d for Q6_K reorder multi-col MMVQ", ncols_dst);
1663 }
1664}
1665
1666static void mul_mat_vec_q6_K_q8_1_sycl(const void *vx, const void *vy,
1667 float *dst, const int ncols,
1668 const int nrows,
1669 dpct::queue_ptr stream) {
1670 launch_mul_mat_vec_q<QK_K, QI6_K, block_q6_K,
1671 VDR_Q6_K_Q8_1_MMVQ, vec_dot_q6_K_q8_1, QK_K>(
1672 vx, vy, dst, ncols, nrows, 1, 0, 0, stream);
1673}
1674
1675
1676static void mul_mat_vec_q6_K_q8_1_sycl_switch_ncols(
1677 const void * vx, const void * vy, float * dst,
1678 const int ncols, const int nrows,
1679 const int ncols_dst,
1680 const int stride_col_y, const int stride_col_dst,
1681 dpct::queue_ptr stream) {
1682 if (ncols_dst < 1 || ncols_dst > 8) {
1683 GGML_ABORT("unsupported ncols_dst=%d for Q6_K multi-col MMVQ", ncols_dst);
1684 }
1685 launch_mul_mat_vec_q<QK_K, QI6_K, block_q6_K,
1686 VDR_Q6_K_Q8_1_MMVQ, vec_dot_q6_K_q8_1, QK_K>(
1687 vx, vy, dst, ncols, nrows, ncols_dst, stride_col_y, stride_col_dst, stream);
1688}
1689
1690
1691
1692static void mul_mat_vec_iq2_xxs_q8_1_sycl(const void *vx, const void *vy,
1693 float *dst, const int ncols,
1694 const int nrows,
1695 dpct::queue_ptr stream) {
1696 GGML_ASSERT(ncols % QK_K == 0);
1697 const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
1698 const sycl::range<3> block_nums(1, 1, block_num_y);
1699 const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
1700 {
1701 stream->submit([&](sycl::handler &cgh) {
1702 cgh.parallel_for(
1703 sycl::nd_range<3>(block_nums * block_dims, block_dims),
1704 [=](sycl::nd_item<3> item_ct1)
1705 [[sycl::reqd_sub_group_size(WARP_SIZE)]] {
1706 mul_mat_vec_q_iq2_xxs_q8_1<QK_K, QI2_XXS/2, block_iq2_xxs, 1>(
1707 vx, vy, dst, ncols, nrows, item_ct1);
1708 });
1709 });
1710 }
1711}
1712
1713static void mul_mat_vec_iq2_xs_q8_1_sycl(const void *vx, const void *vy,
1714 float *dst, const int ncols,
1715 const int nrows,
1716 dpct::queue_ptr stream) {
1717 GGML_ASSERT(ncols % QK_K == 0);
1718 const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
1719 const sycl::range<3> block_nums(1, 1, block_num_y);
1720 const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
1721 {
1722 stream->submit([&](sycl::handler & cgh) {
1723 cgh.parallel_for(
1724 sycl::nd_range<3>(block_nums * block_dims, block_dims),
1725 [=](sycl::nd_item<3> item_ct1)
1726 [[sycl::reqd_sub_group_size(WARP_SIZE)]] {
1727 mul_mat_vec_q_iq2_xs_q8_1<QK_K, QI2_XS/2, block_iq2_xs, 1>(
1728 vx, vy, dst, ncols, nrows, item_ct1);
1729 });
1730 });
1731 }
1732}
1733
1734static void mul_mat_vec_iq2_s_q8_1_sycl(const void *vx, const void *vy,
1735 float *dst, const int ncols,
1736 const int nrows,
1737 dpct::queue_ptr stream) {
1738 GGML_ASSERT(ncols % QK_K == 0);
1739 const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
1740 const sycl::range<3> block_nums(1, 1, block_num_y);
1741 const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
1742 {
1743
1744 stream->submit([&](sycl::handler &cgh) {
1745 cgh.parallel_for(
1746 sycl::nd_range<3>(block_nums * block_dims, block_dims),
1747 [=](sycl::nd_item<3> item_ct1)
1748 [[sycl::reqd_sub_group_size(WARP_SIZE)]] {
1749 mul_mat_vec_q_iq2_s_q8_1<QK_K, QI2_S/2, block_iq2_s, 1>(
1750 vx, vy, dst, ncols, nrows, item_ct1);
1751 });
1752 });
1753 }
1754}
1755
1756static void mul_mat_vec_iq3_xxs_q8_1_sycl(const void *vx, const void *vy,
1757 float *dst, const int ncols,
1758 const int nrows,
1759 dpct::queue_ptr stream) {
1760 GGML_ASSERT(ncols % QK_K == 0);
1761 const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
1762 const sycl::range<3> block_nums(1, 1, block_num_y);
1763 const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
1764 {
1765
1766 stream->submit([&](sycl::handler &cgh) {
1767 cgh.parallel_for(
1768 sycl::nd_range<3>(block_nums * block_dims, block_dims),
1769 [=](sycl::nd_item<3> item_ct1)
1770 [[sycl::reqd_sub_group_size(WARP_SIZE)]] {
1771 mul_mat_vec_q_iq3_xxs_q8_1<QK_K, QI3_XXS/2, block_iq3_xxs, 1>(
1772 vx, vy, dst, ncols, nrows, item_ct1);
1773 });
1774 });
1775 }
1776}
1777
1778template <ggml_type type>
1779static void reorder_mul_mat_vec_iq3_q8_1_sycl(const void * vx, const void * vy, float * dst, const int ncols,
1780 const int nrows, dpct::queue_ptr stream) {
1781 static_assert(type == GGML_TYPE_IQ3_XXS || type == GGML_TYPE_IQ3_S);
1782 GGML_ASSERT(ncols % QK_K == 0);
1783 constexpr size_t num_subgroups = WARP_SIZE;
1784 const int block_num_y = ceil_div(nrows, GGML_SYCL_MMV_Y * (int) num_subgroups);
1785 const sycl::range<3> block_nums(1, 1, block_num_y);
1786 const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, num_subgroups * WARP_SIZE);
1787
1788 stream->submit([&](sycl::handler & cgh) {
1789 cgh.parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
1790 [=](sycl::nd_item<3> nd_item) [[sycl::reqd_sub_group_size(WARP_SIZE)]] {
1791 mul_mat_vec_q_reorder<reorder_vec_dot_q_sycl<type>>(
1792 vx, vy, dst, ncols, nrows, nd_item);
1793 });
1794 });
1795}
1796
1797template <ggml_type type, int ncols_dst>
1798static void reorder_mul_mat_vec_iq3_q8_1_sycl_ncols(
1799 const void * vx, const void * vy, float * dst,
1800 const int ncols, const int nrows,
1801 const int stride_col_y_bytes, const int stride_col_dst,
1802 dpct::queue_ptr stream) {
1803 static_assert(type == GGML_TYPE_IQ3_XXS || type == GGML_TYPE_IQ3_S);
1804 GGML_ASSERT(ncols % QK_K == 0);
1805 constexpr size_t num_subgroups = WARP_SIZE;
1806 const int block_num_y = ceil_div(nrows, GGML_SYCL_MMV_Y * (int) num_subgroups);
1807 const sycl::range<3> block_nums(1, 1, block_num_y);
1808 const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, num_subgroups * WARP_SIZE);
1809
1810 stream->submit([&](sycl::handler & cgh) {
1811 cgh.parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
1812 [=](sycl::nd_item<3> nd_item) [[sycl::reqd_sub_group_size(WARP_SIZE)]] {
1813 mul_mat_vec_q_reorder_ncols<reorder_vec_dot_q_sycl<type>, ncols_dst>(
1814 vx, /*vgate=*/ nullptr, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst,
1815 /*glu_op=*/ GGML_GLU_OP_SWIGLU, nd_item);
1816 });
1817 });
1818}
1819
1820template <ggml_type type>
1821static void reorder_mul_mat_vec_iq3_q8_1_sycl_switch_ncols(
1822 const void * vx, const void * vy, float * dst,
1823 const int ncols, const int nrows, const int ncols_dst,
1824 const int stride_col_y_bytes, const int stride_col_dst,
1825 dpct::queue_ptr stream) {
1826 switch (ncols_dst) {
1827 case 1: reorder_mul_mat_vec_iq3_q8_1_sycl<type>(vx, vy, dst, ncols, nrows, stream); break;
1828 case 2: reorder_mul_mat_vec_iq3_q8_1_sycl_ncols<type, 2>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
1829 case 3: reorder_mul_mat_vec_iq3_q8_1_sycl_ncols<type, 3>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
1830 case 4: reorder_mul_mat_vec_iq3_q8_1_sycl_ncols<type, 4>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
1831 case 5: reorder_mul_mat_vec_iq3_q8_1_sycl_ncols<type, 5>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
1832 case 6: reorder_mul_mat_vec_iq3_q8_1_sycl_ncols<type, 6>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
1833 case 7: reorder_mul_mat_vec_iq3_q8_1_sycl_ncols<type, 7>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
1834 case 8: reorder_mul_mat_vec_iq3_q8_1_sycl_ncols<type, 8>(vx, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, stream); break;
1835 default: GGML_ABORT("unsupported ncols_dst=%d for reordered IQ3 multi-col MMVQ", ncols_dst);
1836 }
1837}
1838
1839static void mul_mat_vec_iq3_s_q8_1_sycl(const void *vx, const void *vy,
1840 float *dst, const int ncols,
1841 const int nrows,
1842 dpct::queue_ptr stream) {
1843 GGML_ASSERT(ncols % QK_K == 0);
1844 const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
1845 const sycl::range<3> block_nums(1, 1, block_num_y);
1846 const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
1847 {
1848
1849 stream->submit([&](sycl::handler &cgh) {
1850 cgh.parallel_for(
1851 sycl::nd_range<3>(block_nums * block_dims, block_dims),
1852 [=](sycl::nd_item<3> item_ct1)
1853 [[sycl::reqd_sub_group_size(WARP_SIZE)]] {
1854 mul_mat_vec_q_iq3_s_q8_1<QK_K, QI3_S/2, block_iq3_s, 1>(
1855 vx, vy, dst, ncols, nrows, item_ct1);
1856 });
1857 });
1858 }
1859}
1860
1861template <int ncols_dst>
1862static void mul_mat_vec_iq3_s_q8_1_sycl_ncols(const void * vx,
1863 const void * vy,
1864 float * dst,
1865 const int ncols,
1866 const int nrows,
1867 const int stride_col_y,
1868 const int stride_col_dst,
1869 dpct::queue_ptr stream) {
1870 GGML_ASSERT(ncols % QK_K == 0);
1871 const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
1872 const sycl::range<3> block_nums(1, 1, block_num_y);
1873 const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
1874 stream->submit([&](sycl::handler & cgh) {
1875 cgh.parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
1876 [=](sycl::nd_item<3> item_ct1) [[sycl::reqd_sub_group_size(WARP_SIZE)]] {
1877 mul_mat_vec_q_ncols<QK_K, QI3_S / 2, block_iq3_s, 1, vec_dot_iq3_s_q8_1_moe, ncols_dst>(
1878 vx, vy, dst, ncols, nrows, stride_col_y, stride_col_dst, item_ct1);
1879 });
1880 });
1881}
1882
1883static void mul_mat_vec_iq3_s_q8_1_sycl_switch_ncols(const void * vx,
1884 const void * vy,
1885 float * dst,
1886 const int ncols,
1887 const int nrows,
1888 const int ncols_dst,
1889 const int stride_col_y,
1890 const int stride_col_dst,
1891 dpct::queue_ptr stream) {
1892 switch (ncols_dst) {
1893 case 1:
1894 mul_mat_vec_iq3_s_q8_1_sycl(vx, vy, dst, ncols, nrows, stream);
1895 break;
1896 case 2:
1897 mul_mat_vec_iq3_s_q8_1_sycl_ncols<2>(vx, vy, dst, ncols, nrows, stride_col_y, stride_col_dst, stream);
1898 break;
1899 case 3:
1900 mul_mat_vec_iq3_s_q8_1_sycl_ncols<3>(vx, vy, dst, ncols, nrows, stride_col_y, stride_col_dst, stream);
1901 break;
1902 case 4:
1903 mul_mat_vec_iq3_s_q8_1_sycl_ncols<4>(vx, vy, dst, ncols, nrows, stride_col_y, stride_col_dst, stream);
1904 break;
1905 case 5:
1906 mul_mat_vec_iq3_s_q8_1_sycl_ncols<5>(vx, vy, dst, ncols, nrows, stride_col_y, stride_col_dst, stream);
1907 break;
1908 case 6:
1909 mul_mat_vec_iq3_s_q8_1_sycl_ncols<6>(vx, vy, dst, ncols, nrows, stride_col_y, stride_col_dst, stream);
1910 break;
1911 case 7:
1912 mul_mat_vec_iq3_s_q8_1_sycl_ncols<7>(vx, vy, dst, ncols, nrows, stride_col_y, stride_col_dst, stream);
1913 break;
1914 case 8:
1915 mul_mat_vec_iq3_s_q8_1_sycl_ncols<8>(vx, vy, dst, ncols, nrows, stride_col_y, stride_col_dst, stream);
1916 break;
1917 default:
1918 GGML_ABORT("unsupported ncols_dst=%d for IQ3_S multi-col MMVQ", ncols_dst);
1919 }
1920}
1921
1922static void mul_mat_vec_iq1_s_q8_1_sycl(const void *vx, const void *vy,
1923 float *dst, const int ncols,
1924 const int nrows,
1925 dpct::queue_ptr stream) {
1926 GGML_ASSERT(ncols % QK_K == 0);
1927 const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
1928 const sycl::range<3> block_nums(1, 1, block_num_y);
1929 const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
1930 {
1931
1932 stream->submit([&](sycl::handler &cgh) {
1933 cgh.parallel_for(
1934 sycl::nd_range<3>(block_nums * block_dims, block_dims),
1935 [=](sycl::nd_item<3> item_ct1)
1936 [[sycl::reqd_sub_group_size(WARP_SIZE)]] {
1937 mul_mat_vec_q_iq1_s_q8_1<QK_K, QI1_S, block_iq1_s, 1>(
1938 vx, vy, dst, ncols, nrows, item_ct1);
1939 });
1940 });
1941 }
1942}
1943
1944static void mul_mat_vec_iq1_m_q8_1_sycl(const void *vx, const void *vy,
1945 float *dst, const int ncols,
1946 const int nrows,
1947 dpct::queue_ptr stream) {
1948 GGML_ASSERT(ncols % QK_K == 0);
1949 const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
1950 const sycl::range<3> block_nums(1, 1, block_num_y);
1951 const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
1952 {
1953 stream->submit([&](sycl::handler &cgh) {
1954 cgh.parallel_for(
1955 sycl::nd_range<3>(block_nums * block_dims, block_dims),
1956 [=](sycl::nd_item<3> item_ct1)
1957 [[sycl::reqd_sub_group_size(WARP_SIZE)]] {
1958 mul_mat_vec_q_iq1_m_q8_1<QK_K, QI1_S, block_iq1_m, 1>(
1959 vx, vy, dst, ncols, nrows, item_ct1);
1960 });
1961 });
1962 }
1963}
1964
1965static void mul_mat_vec_iq4_nl_q8_1_sycl(const void *vx, const void *vy,
1966 float *dst, const int ncols,
1967 const int nrows,
1968 dpct::queue_ptr stream) {
1969 GGML_ASSERT(ncols % QK4_NL == 0);
1970 const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
1971 const sycl::range<3> block_nums(1, 1, block_num_y);
1972 const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
1973 {
1974
1975 stream->submit([&](sycl::handler &cgh) {
1976 cgh.parallel_for(
1977 sycl::nd_range<3>(block_nums * block_dims, block_dims),
1978 [=](sycl::nd_item<3> item_ct1)
1979 [[sycl::reqd_sub_group_size(WARP_SIZE)]] {
1980 mul_mat_vec_q_iq4_nl_q8_1<QK4_NL, QI4_NL, block_iq4_nl, 2>(
1981 vx, vy, dst, ncols, nrows, item_ct1);
1982 });
1983 });
1984 }
1985}
1986
1987static void mul_mat_vec_iq4_xs_q8_1_sycl(const void *vx, const void *vy,
1988 float *dst, const int ncols,
1989 const int nrows,
1990 dpct::queue_ptr stream) {
1991 GGML_ASSERT(ncols % QK_K == 0);
1992 const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
1993 const sycl::range<3> block_nums(1, 1, block_num_y);
1994 const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
1995 {
1996
1997 stream->submit([&](sycl::handler &cgh) {
1998 cgh.parallel_for(
1999 sycl::nd_range<3>(block_nums * block_dims, block_dims),
2000 [=](sycl::nd_item<3> item_ct1)
2001 [[sycl::reqd_sub_group_size(WARP_SIZE)]] {
2002 mul_mat_vec_q_iq4_xs_q8_1<QK_K, QI4_XS/4, block_iq4_xs, 1>(
2003 vx, vy, dst, ncols, nrows, item_ct1);
2004 });
2005 });
2006 }
2007}
2008
2009template <int ncols_dst>
2010static void mul_mat_vec_iq4_xs_q8_1_sycl_ncols(
2011 const void * vx, const void * vy, float * dst,
2012 const int ncols, const int nrows,
2013 const int stride_col_y, const int stride_col_dst,
2014 dpct::queue_ptr stream) {
2015 GGML_ASSERT(ncols % QK_K == 0);
2016 const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
2017 const sycl::range<3> block_nums(1, 1, block_num_y);
2018 const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
2019
2020 stream->submit([&](sycl::handler & cgh) {
2021 cgh.parallel_for(
2022 sycl::nd_range<3>(block_nums * block_dims, block_dims),
2023 [=](sycl::nd_item<3> item_ct1)
2024 [[sycl::reqd_sub_group_size(WARP_SIZE)]] {
2025 mul_mat_vec_q_ncols<QK_K, QI4_XS/4, block_iq4_xs,
2026 1,
2027 vec_dot_iq4_xs_q8_1,
2028 ncols_dst>(
2029 vx, vy, dst, ncols, nrows,
2030 stride_col_y, stride_col_dst, item_ct1);
2031 });
2032 });
2033}
2034
2035static void mul_mat_vec_iq4_xs_q8_1_sycl_switch_ncols(
2036 const void * vx, const void * vy, float * dst,
2037 const int ncols, const int nrows,
2038 const int ncols_dst,
2039 const int stride_col_y, const int stride_col_dst,
2040 dpct::queue_ptr stream) {
2041 switch (ncols_dst) {
2042 case 1: mul_mat_vec_iq4_xs_q8_1_sycl(vx, vy, dst, ncols, nrows, stream); break;
2043 case 2: mul_mat_vec_iq4_xs_q8_1_sycl_ncols<2>(vx, vy, dst, ncols, nrows, stride_col_y, stride_col_dst, stream); break;
2044 case 3: mul_mat_vec_iq4_xs_q8_1_sycl_ncols<3>(vx, vy, dst, ncols, nrows, stride_col_y, stride_col_dst, stream); break;
2045 case 4: mul_mat_vec_iq4_xs_q8_1_sycl_ncols<4>(vx, vy, dst, ncols, nrows, stride_col_y, stride_col_dst, stream); break;
2046 case 5: mul_mat_vec_iq4_xs_q8_1_sycl_ncols<5>(vx, vy, dst, ncols, nrows, stride_col_y, stride_col_dst, stream); break;
2047 case 6: mul_mat_vec_iq4_xs_q8_1_sycl_ncols<6>(vx, vy, dst, ncols, nrows, stride_col_y, stride_col_dst, stream); break;
2048 case 7: mul_mat_vec_iq4_xs_q8_1_sycl_ncols<7>(vx, vy, dst, ncols, nrows, stride_col_y, stride_col_dst, stream); break;
2049 case 8: mul_mat_vec_iq4_xs_q8_1_sycl_ncols<8>(vx, vy, dst, ncols, nrows, stride_col_y, stride_col_dst, stream); break;
2050 default: GGML_ABORT("unsupported ncols_dst=%d for IQ4_XS multi-col MMVQ", ncols_dst);
2051 }
2052}
2053
2054#ifdef GGML_SYCL_MMVQ_HAS_XMX
2055// vgate is the gate weight of the fused gate and up kernel, nullptr for a plain mat-vec
2056template <template <bool> class Traits>
2057static void ggml_sycl_mul_mat_vec_q_xmx_run(bool short_rows,
2058 const void * vx,
2059 const void * vgate,
2060 const char * vy,
2061 float * dst,
2062 int ne00,
2063 int64_t nrows,
2064 int64_t ncols_dst,
2065 int stride_y_bytes,
2066 int ldd,
2067 int glu_op,
2068 dpct::queue_ptr stream) {
2069 namespace esimd = ggml_sycl_esimd;
2070 if (vgate) {
2071 if (short_rows) {
2072 esimd::xmx_mul_mat_ncols<Traits<true>, true>(vx, vgate, vy, dst, ne00, nrows, ncols_dst, stride_y_bytes,
2073 ldd, glu_op, stream);
2074 } else {
2075 esimd::xmx_mul_mat_ncols<Traits<false>, true>(vx, vgate, vy, dst, ne00, nrows, ncols_dst, stride_y_bytes,
2076 ldd, glu_op, stream);
2077 }
2078 } else if (short_rows) {
2079 esimd::xmx_mul_mat_ncols<Traits<true>>(vx, nullptr, vy, dst, ne00, nrows, ncols_dst, stride_y_bytes, ldd, 0,
2080 stream);
2081 } else {
2082 esimd::xmx_mul_mat_ncols<Traits<false>>(vx, nullptr, vy, dst, ne00, nrows, ncols_dst, stride_y_bytes, ldd, 0,
2083 stream);
2084 }
2085}
2086
2087static bool ggml_sycl_mul_mat_vec_q_xmx_type(ggml_type type,
2088 bool short_rows,
2089 const void * vx,
2090 const void * vgate,
2091 const char * vy,
2092 float * dst,
2093 int ne00,
2094 int64_t nrows,
2095 int64_t ncols_dst,
2096 int stride_y_bytes,
2097 int ldd,
2098 int glu_op,
2099 dpct::queue_ptr stream) {
2100# define XMX_RUN(TRAITS) \
2101 ggml_sycl_mul_mat_vec_q_xmx_run<ggml_sycl_esimd::TRAITS>(short_rows, vx, vgate, vy, dst, ne00, nrows, \
2102 ncols_dst, stride_y_bytes, ldd, glu_op, stream); \
2103 return true
2104 switch (type) {
2105 case GGML_TYPE_Q2_K:
2106 XMX_RUN(xmx_traits_q2_k);
2107 case GGML_TYPE_Q3_K:
2108 XMX_RUN(xmx_traits_q3_k);
2109 case GGML_TYPE_Q4_K:
2110 XMX_RUN(xmx_traits_q4_k);
2111 case GGML_TYPE_Q5_K:
2112 XMX_RUN(xmx_traits_q5_k);
2113 case GGML_TYPE_Q6_K:
2114 XMX_RUN(xmx_traits_q6_k);
2115 case GGML_TYPE_Q8_0:
2116 XMX_RUN(xmx_traits_q8_0);
2117 default:
2118 return false;
2119 }
2120# undef XMX_RUN
2121}
2122
2123// multi-column XMX path for reordered weights, returns false when it does not apply
2124static bool ggml_sycl_mul_mat_vec_q_xmx(int device,
2125 const ggml_tensor * src0,
2126 const ggml_tensor * dst,
2127 const char * src0_dd_i,
2128 const char * src1_ddq_i,
2129 float * dst_dd_i,
2130 int64_t row_diff,
2131 int64_t src1_ncols,
2132 int64_t src1_padded_col_size,
2133 dpct::queue_ptr stream) {
2134 const auto * extra = static_cast<const ggml_tensor_extra_gpu *>(src0->extra);
2135 if (!ggml_sycl_xmx_enabled(device) || !ggml_sycl_xmx_supports_type(src0->type) || !extra ||
2136 !extra->optimized_feature.reorder) {
2137 return false;
2138 }
2139 if (src1_ncols < ggml_sycl_xmx_min_cols(src0->type) || src1_ncols > g_ggml_sycl_xmx_max_cols) {
2140 return false;
2141 }
2142
2143 const int ne00 = src0->ne[0];
2144 const int ldd = dst->ne[0];
2145 if (!ggml_sycl_esimd::xmx_supported(src0_dd_i, ne00)) {
2146 return false;
2147 }
2148
2149 // rows under 64 bytes use the variant without 2D block loads, it is not built for the widest tile
2150 const bool short_rows = ne00 < 4 * QK_K;
2151 if (short_rows && src1_ncols > 8) {
2152 return false;
2153 }
2154
2155 const int stride_y_bytes = src1_padded_col_size * sizeof(block_q8_1) / QK8_1;
2156 // the 2D block loads of the activations need a 64 byte aligned base and a pitch of 16 bytes
2157 if ((uintptr_t) src1_ddq_i % 64 != 0 || stride_y_bytes % 16 != 0) {
2158 return false;
2159 }
2160 return ggml_sycl_mul_mat_vec_q_xmx_type(src0->type, short_rows, src0_dd_i, nullptr, src1_ddq_i, dst_dd_i, ne00,
2161 row_diff, src1_ncols, stride_y_bytes, ldd, 0, stream);
2162}
2163#endif // GGML_SYCL_MMVQ_HAS_XMX
2164
2165bool ggml_sycl_mul_mat_vec_q_glu_xmx(int device,
2166 enum ggml_type src0_type,
2167 enum ggml_glu_op glu_op,
2168 const void * vx,
2169 const void * vgate,
2170 const void * vy,
2171 float * dst,
2172 int ncols,
2173 int nrows,
2174 int ncols_dst,
2175 int stride_col_y_bytes,
2176 int stride_col_dst,
2177 dpct::queue_ptr stream) {
2178#ifdef GGML_SYCL_MMVQ_HAS_XMX
2179 if (!ggml_sycl_xmx_enabled(device) || !ggml_sycl_xmx_supports_type(src0_type)) {
2180 return false;
2181 }
2182 if (glu_op != GGML_GLU_OP_SWIGLU && glu_op != GGML_GLU_OP_GEGLU) {
2183 return false;
2184 }
2185 if (ncols_dst < ggml_sycl_xmx_min_cols(src0_type) || ncols_dst > g_ggml_sycl_xmx_glu_max_cols) {
2186 return false;
2187 }
2188 if (!ggml_sycl_esimd::xmx_supported(vx, ncols) || !ggml_sycl_esimd::xmx_supported(vgate, ncols) ||
2189 (uintptr_t) vy % 64 != 0 || stride_col_y_bytes % 16 != 0) {
2190 return false;
2191 }
2192 // rows under 64 bytes use the variant without 2D block loads, it is not built for the wide tiles
2193 const bool short_rows = ncols < 4 * QK_K;
2194 if (short_rows && ncols_dst > 8) {
2195 return false;
2196 }
2197 return ggml_sycl_mul_mat_vec_q_xmx_type(src0_type, short_rows, vx, vgate, (const char *) vy, dst, ncols, nrows,
2198 ncols_dst, stride_col_y_bytes, stride_col_dst, (int) glu_op, stream);
2199#else
2200 GGML_UNUSED_VARS(device, src0_type, glu_op, vx, vgate, vy, dst, ncols, nrows, ncols_dst, stride_col_y_bytes,
2201 stride_col_dst, stream);
2202 return false;
2203#endif // GGML_SYCL_MMVQ_HAS_XMX
2204}
2205
2206void ggml_sycl_op_mul_mat_vec_q(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1,
2207 ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
2208 const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low,
2209 const int64_t row_high, const int64_t src1_ncols, const int64_t src1_padded_col_size,
2210 const dpct::queue_ptr & stream) {
2211 const int64_t ne10 = src1->ne[0];
2212 GGML_ASSERT(ne10 % QK8_1 == 0);
2213
2214 const int64_t ne00 = src0->ne[0];
2215 const int64_t row_diff = row_high - row_low;
2216
2217#ifdef GGML_SYCL_MMVQ_HAS_XMX
2218 if (ggml_sycl_mul_mat_vec_q_xmx(ctx.device, src0, dst, src0_dd_i, src1_ddq_i, dst_dd_i, row_diff, src1_ncols,
2219 src1_padded_col_size, stream)) {
2220 return;
2221 }
2222#endif // GGML_SYCL_MMVQ_HAS_XMX
2223
2224 int id;
2225 SYCL_CHECK(CHECK_TRY_ERROR(id = get_current_device_id()));
2226 const size_t q8_1_ts = sizeof(block_q8_1);
2227 const size_t q8_1_bs = QK8_1;
2228 // the main device has a larger memory buffer to hold the results from all GPUs
2229 // nrows_dst == nrows of the matrix that the kernel writes into
2230
2231 for (int i = 0; i < src1_ncols; i++) {
2232 const size_t src1_ddq_i_offset = i * src1_padded_col_size * q8_1_ts / q8_1_bs;
2233 const char * src1_ddq_i_bs = src1_ddq_i + src1_ddq_i_offset;
2234 float * dst_dd_i_bs = dst_dd_i + i * dst->ne[0];
2235 switch (src0->type) {
2236 case GGML_TYPE_Q4_0:
2237 if ((ggml_tensor_extra_gpu *) dst->src[0]->extra &&
2238 ((ggml_tensor_extra_gpu *) dst->src[0]->extra)->optimized_feature.reorder) {
2239 if (i == 0 && src1_ncols > 1 && src1_ncols <= 8) {
2240 const int stride_col_y_bytes = src1_padded_col_size * q8_1_ts / q8_1_bs;
2241 const int stride_col_dst = dst->ne[0];
2242 GGML_SYCL_DEBUG("Calling reorder_mul_mat_vec_q4_0_q8_1_sycl_switch_ncols ncols=%d\n", (int)src1_ncols);
2243 reorder_mul_mat_vec_q4_0_q8_1_sycl_switch_ncols(
2244 src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff,
2245 src1_ncols, stride_col_y_bytes, stride_col_dst, stream);
2246 return;
2247 } else {
2248 GGML_SYCL_DEBUG("Calling reorder_mul_mat_vec_q4_0_q8_1_sycl\n");
2249 reorder_mul_mat_vec_q4_0_q8_1_sycl(src0_dd_i, src1_ddq_i_bs, dst_dd_i_bs, ne00, row_diff, stream);
2250 }
2251 } else if (i == 0 && src1_ncols > 1 && src1_ncols <= 8) {
2252 const int stride_col_y = src1_padded_col_size / QK8_1;
2253 const int stride_col_dst = dst->ne[0];
2254 GGML_SYCL_DEBUG("Calling mul_mat_vec_q4_0_q8_1_sycl_switch_ncols ncols=%d\n", (int)src1_ncols);
2255 mul_mat_vec_q4_0_q8_1_sycl_switch_ncols(
2256 src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff,
2257 src1_ncols, stride_col_y, stride_col_dst, stream);
2258 return;
2259 } else if (i == 0 || src1_ncols == 1) {
2260 GGML_SYCL_DEBUG("Calling mul_mat_vec_q4_0_q8_1_sycl\n");
2261 mul_mat_vec_q4_0_q8_1_sycl(src0_dd_i, src1_ddq_i_bs, dst_dd_i_bs, ne00, row_diff, stream);
2262 }
2263 break;
2264 case GGML_TYPE_Q4_1:
2265 if (i == 0 && src1_ncols > 1 && src1_ncols <= 8) {
2266 const int stride_col_y = src1_padded_col_size / QK8_1;
2267 const int stride_col_dst = dst->ne[0];
2268 GGML_SYCL_DEBUG("Calling mul_mat_vec_q4_1_q8_1_sycl_switch_ncols ncols=%d\n", (int)src1_ncols);
2269 mul_mat_vec_q4_1_q8_1_sycl_switch_ncols(
2270 src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff,
2271 src1_ncols, stride_col_y, stride_col_dst, stream);
2272 return;
2273 } else if (i == 0 || src1_ncols == 1) {
2274 mul_mat_vec_q4_1_q8_1_sycl(src0_dd_i, src1_ddq_i_bs, dst_dd_i_bs, ne00, row_diff, stream);
2275 }
2276 break;
2277 case GGML_TYPE_Q5_0:
2278 if (i == 0 && src1_ncols > 1 && src1_ncols <= 8) {
2279 const int stride_col_y = src1_padded_col_size / QK8_1;
2280 const int stride_col_dst = dst->ne[0];
2281 GGML_SYCL_DEBUG("Calling mul_mat_vec_q5_0_q8_1_sycl_switch_ncols ncols=%d\n", (int)src1_ncols);
2282 mul_mat_vec_q5_0_q8_1_sycl_switch_ncols(
2283 src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff,
2284 src1_ncols, stride_col_y, stride_col_dst, stream);
2285 return;
2286 } else if (i == 0 || src1_ncols == 1) {
2287 mul_mat_vec_q5_0_q8_1_sycl(src0_dd_i, src1_ddq_i_bs, dst_dd_i_bs, ne00, row_diff, stream);
2288 }
2289 break;
2290 case GGML_TYPE_Q5_1:
2291 if (i == 0 && src1_ncols > 1 && src1_ncols <= 8) {
2292 const int stride_col_y = src1_padded_col_size / QK8_1;
2293 const int stride_col_dst = dst->ne[0];
2294 GGML_SYCL_DEBUG("Calling mul_mat_vec_q5_1_q8_1_sycl_switch_ncols ncols=%d\n", (int)src1_ncols);
2295 mul_mat_vec_q5_1_q8_1_sycl_switch_ncols(
2296 src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff,
2297 src1_ncols, stride_col_y, stride_col_dst, stream);
2298 return;
2299 } else if (i == 0 || src1_ncols == 1) {
2300 mul_mat_vec_q5_1_q8_1_sycl(src0_dd_i, src1_ddq_i_bs, dst_dd_i_bs, ne00, row_diff, stream);
2301 }
2302 break;
2303 case GGML_TYPE_Q8_0:
2304 if ((ggml_tensor_extra_gpu *) dst->src[0]->extra &&
2305 ((ggml_tensor_extra_gpu *) dst->src[0]->extra)->optimized_feature.reorder) {
2306 if (i == 0 && src1_ncols > 1 && src1_ncols <= 8) {
2307 const int stride_col_y_bytes = src1_padded_col_size * q8_1_ts / q8_1_bs;
2308 const int stride_col_dst = dst->ne[0];
2309 GGML_SYCL_DEBUG("Calling reorder_mul_mat_vec_q8_0_q8_1_sycl_switch_ncols ncols=%d\n", (int)src1_ncols);
2310 reorder_mul_mat_vec_q8_0_q8_1_sycl_switch_ncols(
2311 src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff,
2312 src1_ncols, stride_col_y_bytes, stride_col_dst, stream);
2313 return;
2314 } else {
2315 GGML_SYCL_DEBUG("Calling reorder_mul_mat_vec_q8_0_q8_1_sycl\n");
2316 reorder_mul_mat_vec_q8_0_q8_1_sycl(src0_dd_i, src1_ddq_i_bs, dst_dd_i_bs, ne00, row_diff, stream);
2317 }
2318 } else if (i == 0 && src1_ncols > 1 && src1_ncols <= 8) {
2319 const int stride_col_y = src1_padded_col_size / QK8_1;
2320 const int stride_col_dst = dst->ne[0];
2321 GGML_SYCL_DEBUG("Calling mul_mat_vec_q8_0_q8_1_sycl_switch_ncols ncols=%d\n", (int)src1_ncols);
2322 mul_mat_vec_q8_0_q8_1_sycl_switch_ncols(
2323 src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff,
2324 src1_ncols, stride_col_y, stride_col_dst, stream);
2325 return;
2326 } else if (i == 0 || src1_ncols == 1) {
2327 GGML_SYCL_DEBUG("Calling mul_mat_vec_q8_0_q8_1_sycl\n");
2328 mul_mat_vec_q8_0_q8_1_sycl(src0_dd_i, src1_ddq_i_bs, dst_dd_i_bs, ne00, row_diff, stream);
2329 }
2330 break;
2331 case GGML_TYPE_Q1_0:
2332 if (i == 0 && src1_ncols > 1 && src1_ncols <= 8) {
2333 const int stride_col_y = src1_padded_col_size / QK8_1;
2334 const int stride_col_dst = dst->ne[0];
2335 GGML_SYCL_DEBUG("Calling mul_mat_vec_q1_0_q8_1_sycl_switch_ncols ncols=%d\n", (int)src1_ncols);
2336 mul_mat_vec_q1_0_q8_1_sycl_switch_ncols(
2337 src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff,
2338 src1_ncols, stride_col_y, stride_col_dst, stream);
2339 return;
2340 } else if (i == 0 || src1_ncols == 1) {
2341 GGML_SYCL_DEBUG("Calling mul_mat_vec_q1_0_q8_1_sycl\n");
2342 mul_mat_vec_q1_0_q8_1_sycl(src0_dd_i, src1_ddq_i_bs, dst_dd_i_bs, ne00, row_diff, stream);
2343 }
2344 break;
2345 case GGML_TYPE_Q2_0:
2346 if (i == 0 && src1_ncols > 1 && src1_ncols <= 8) {
2347 const int stride_col_y = src1_padded_col_size / QK8_1;
2348 const int stride_col_dst = dst->ne[0];
2349 GGML_SYCL_DEBUG("Calling mul_mat_vec_q2_0_q8_1_sycl_switch_ncols ncols=%d\n", (int)src1_ncols);
2350 mul_mat_vec_q2_0_q8_1_sycl_switch_ncols(
2351 src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff,
2352 src1_ncols, stride_col_y, stride_col_dst, stream);
2353 return;
2354 } else if (i == 0 || src1_ncols == 1) {
2355 GGML_SYCL_DEBUG("Calling mul_mat_vec_q2_0_q8_1_sycl\n");
2356 mul_mat_vec_q2_0_q8_1_sycl(src0_dd_i, src1_ddq_i_bs, dst_dd_i_bs, ne00, row_diff, stream);
2357 }
2358 break;
2359 case GGML_TYPE_Q2_K:
2360 if ((ggml_tensor_extra_gpu *) dst->src[0]->extra &&
2361 ((ggml_tensor_extra_gpu *) dst->src[0]->extra)->optimized_feature.reorder) {
2362 if (i == 0 && src1_ncols > 1 && src1_ncols <= 8) {
2363 const int stride_col_y_bytes = src1_padded_col_size * q8_1_ts / q8_1_bs;
2364 const int stride_col_dst = dst->ne[0];
2365 GGML_SYCL_DEBUG("Calling reorder_mul_mat_vec_q2_k_q8_1_sycl_switch_ncols ncols=%d\n", (int)src1_ncols);
2366 reorder_mul_mat_vec_q2_k_q8_1_sycl_switch_ncols(
2367 src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff,
2368 src1_ncols, stride_col_y_bytes, stride_col_dst, stream);
2369 return;
2370 } else {
2371 GGML_SYCL_DEBUG("Calling reorder_mul_mat_vec_q2_k_q8_1_sycl\n");
2372 reorder_mul_mat_vec_q2_k_q8_1_sycl(src0_dd_i, src1_ddq_i_bs, dst_dd_i_bs, ne00, row_diff, stream);
2373 }
2374 } else if (i == 0 && src1_ncols > 1 && src1_ncols <= 8) {
2375 const int stride_col_y = src1_padded_col_size / QK8_1;
2376 const int stride_col_dst = dst->ne[0];
2377 GGML_SYCL_DEBUG("Calling mul_mat_vec_q2_K_q8_1_sycl_switch_ncols ncols=%d\n", (int)src1_ncols);
2378 mul_mat_vec_q2_K_q8_1_sycl_switch_ncols(
2379 src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff,
2380 src1_ncols, stride_col_y, stride_col_dst, stream);
2381 return;
2382 } else if (i == 0 || src1_ncols == 1) {
2383 GGML_SYCL_DEBUG("Calling mul_mat_vec_q2_K_q8_1_sycl\n");
2384 mul_mat_vec_q2_K_q8_1_sycl(src0_dd_i, src1_ddq_i_bs, dst_dd_i_bs, ne00, row_diff, stream);
2385 }
2386 break;
2387 case GGML_TYPE_Q3_K:
2388 if ((ggml_tensor_extra_gpu *) dst->src[0]->extra &&
2389 ((ggml_tensor_extra_gpu *) dst->src[0]->extra)->optimized_feature.reorder) {
2390 if (i == 0 && src1_ncols > 1 && src1_ncols <= 8) {
2391 const int stride_col_y_bytes = src1_padded_col_size * q8_1_ts / q8_1_bs;
2392 const int stride_col_dst = dst->ne[0];
2393 GGML_SYCL_DEBUG("Calling reorder_mul_mat_vec_q3_k_q8_1_sycl_switch_ncols ncols=%d\n", (int)src1_ncols);
2394 reorder_mul_mat_vec_q3_k_q8_1_sycl_switch_ncols(
2395 src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff,
2396 src1_ncols, stride_col_y_bytes, stride_col_dst, stream);
2397 return;
2398 } else {
2399 GGML_SYCL_DEBUG("Calling reorder_mul_mat_vec_q3_k_q8_1_sycl\n");
2400 reorder_mul_mat_vec_q3_k_q8_1_sycl(src0_dd_i, src1_ddq_i_bs, dst_dd_i_bs, ne00, row_diff, stream);
2401 }
2402 } else if (i == 0 && src1_ncols > 1 && src1_ncols <= 8) {
2403 const int stride_col_y = src1_padded_col_size / QK8_1;
2404 const int stride_col_dst = dst->ne[0];
2405 GGML_SYCL_DEBUG("Calling mul_mat_vec_q3_K_q8_1_sycl_switch_ncols ncols=%d\n", (int)src1_ncols);
2406 mul_mat_vec_q3_K_q8_1_sycl_switch_ncols(
2407 src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff,
2408 src1_ncols, stride_col_y, stride_col_dst, stream);
2409 return;
2410 } else if (i == 0 || src1_ncols == 1) {
2411 GGML_SYCL_DEBUG("Calling mul_mat_vec_q3_K_q8_1_sycl\n");
2412 mul_mat_vec_q3_K_q8_1_sycl(src0_dd_i, src1_ddq_i_bs, dst_dd_i_bs, ne00, row_diff, stream);
2413 }
2414 break;
2415 case GGML_TYPE_Q4_K:
2416 if ((ggml_tensor_extra_gpu *) dst->src[0]->extra &&
2417 ((ggml_tensor_extra_gpu *) dst->src[0]->extra)->optimized_feature.reorder) {
2418 if (i == 0 && src1_ncols > 1 && src1_ncols <= 8) {
2419 const int stride_col_y_bytes = src1_padded_col_size * q8_1_ts / q8_1_bs;
2420 const int stride_col_dst = dst->ne[0];
2421 GGML_SYCL_DEBUG("Calling reorder_mul_mat_vec_q4_k_q8_1_sycl_switch_ncols ncols=%d\n", (int)src1_ncols);
2422 reorder_mul_mat_vec_q4_k_q8_1_sycl_switch_ncols(
2423 src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff,
2424 src1_ncols, stride_col_y_bytes, stride_col_dst, stream);
2425 return;
2426 } else {
2427 GGML_SYCL_DEBUG("Calling reorder_mul_mat_vec_q4_k_q8_1_sycl\n");
2428 reorder_mul_mat_vec_q4_k_q8_1_sycl(src0_dd_i, src1_ddq_i_bs, dst_dd_i_bs, ne00, row_diff, stream);
2429 }
2430 } else if (i == 0 && src1_ncols > 1 && src1_ncols <= 8) {
2431 const int stride_col_y = src1_padded_col_size / QK8_1;
2432 const int stride_col_dst = dst->ne[0];
2433 GGML_SYCL_DEBUG("Calling mul_mat_vec_q4_K_q8_1_sycl_switch_ncols ncols=%d\n", (int)src1_ncols);
2434 mul_mat_vec_q4_K_q8_1_sycl_switch_ncols(
2435 src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff,
2436 src1_ncols, stride_col_y, stride_col_dst, stream);
2437 return;
2438 } else if (i == 0 || src1_ncols == 1) {
2439 GGML_SYCL_DEBUG("Calling mul_mat_vec_q4_K_q8_1_sycl\n");
2440 mul_mat_vec_q4_K_q8_1_sycl(src0_dd_i, src1_ddq_i_bs, dst_dd_i_bs, ne00, row_diff, stream);
2441 }
2442 break;
2443 case GGML_TYPE_Q5_K:
2444 if ((ggml_tensor_extra_gpu *) dst->src[0]->extra &&
2445 ((ggml_tensor_extra_gpu *) dst->src[0]->extra)->optimized_feature.reorder) {
2446 if (i == 0 && src1_ncols > 1 && src1_ncols <= 8) {
2447 const int stride_col_y_bytes = src1_padded_col_size * q8_1_ts / q8_1_bs;
2448 const int stride_col_dst = dst->ne[0];
2449 GGML_SYCL_DEBUG("Calling reorder_mul_mat_vec_q5_k_q8_1_sycl_switch_ncols ncols=%d\n", (int)src1_ncols);
2450 reorder_mul_mat_vec_q5_k_q8_1_sycl_switch_ncols(
2451 src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff,
2452 src1_ncols, stride_col_y_bytes, stride_col_dst, stream);
2453 return;
2454 } else {
2455 GGML_SYCL_DEBUG("Calling reorder_mul_mat_vec_q5_k_q8_1_sycl\n");
2456 reorder_mul_mat_vec_q5_k_q8_1_sycl(src0_dd_i, src1_ddq_i_bs, dst_dd_i_bs, ne00, row_diff, stream);
2457 }
2458 } else if (i == 0 && src1_ncols > 1 && src1_ncols <= 8) {
2459 const int stride_col_y = src1_padded_col_size / QK8_1;
2460 const int stride_col_dst = dst->ne[0];
2461 GGML_SYCL_DEBUG("Calling mul_mat_vec_q5_K_q8_1_sycl_switch_ncols ncols=%d\n", (int)src1_ncols);
2462 mul_mat_vec_q5_K_q8_1_sycl_switch_ncols(
2463 src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff,
2464 src1_ncols, stride_col_y, stride_col_dst, stream);
2465 return;
2466 } else if (i == 0 || src1_ncols == 1) {
2467 GGML_SYCL_DEBUG("Calling mul_mat_vec_q5_K_q8_1_sycl\n");
2468 mul_mat_vec_q5_K_q8_1_sycl(src0_dd_i, src1_ddq_i_bs, dst_dd_i_bs, ne00, row_diff, stream);
2469 }
2470 break;
2471 case GGML_TYPE_Q6_K:
2472 if ((ggml_tensor_extra_gpu *) dst->src[0]->extra &&
2473 ((ggml_tensor_extra_gpu *) dst->src[0]->extra)->optimized_feature.reorder) {
2474 if (i == 0 && src1_ncols > 1 && src1_ncols <= 8) {
2475 const int stride_col_y_bytes = src1_padded_col_size * q8_1_ts / q8_1_bs;
2476 const int stride_col_dst = dst->ne[0];
2477 GGML_SYCL_DEBUG("Calling reorder_mul_mat_vec_q6_k_q8_1_sycl_switch_ncols ncols=%d\n", (int)src1_ncols);
2478 reorder_mul_mat_vec_q6_k_q8_1_sycl_switch_ncols(
2479 src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff,
2480 src1_ncols, stride_col_y_bytes, stride_col_dst, stream);
2481 return;
2482 } else {
2483 GGML_SYCL_DEBUG("Calling reorder_mul_mat_vec_q6_k_q8_1_sycl\n");
2484 reorder_mul_mat_vec_q6_k_q8_1_sycl(src0_dd_i, src1_ddq_i_bs, dst_dd_i_bs, ne00, row_diff, stream);
2485 }
2486 } else if (i == 0 && src1_ncols > 1 && src1_ncols <= 8) {
2487 const int stride_col_y = src1_padded_col_size / QK8_1;
2488 const int stride_col_dst = dst->ne[0];
2489 GGML_SYCL_DEBUG("Calling mul_mat_vec_q6_K_q8_1_sycl_switch_ncols ncols=%d\n", (int)src1_ncols);
2490 mul_mat_vec_q6_K_q8_1_sycl_switch_ncols(
2491 src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff,
2492 src1_ncols, stride_col_y, stride_col_dst, stream);
2493 return;
2494 } else if (i == 0 || src1_ncols == 1) {
2495 GGML_SYCL_DEBUG("Calling mul_mat_vec_q6_k_q8_1_sycl\n");
2496 mul_mat_vec_q6_K_q8_1_sycl(src0_dd_i, src1_ddq_i_bs, dst_dd_i_bs, ne00, row_diff, stream);
2497 }
2498 break;
2499 case GGML_TYPE_IQ1_S:
2500 mul_mat_vec_iq1_s_q8_1_sycl(src0_dd_i, src1_ddq_i_bs, dst_dd_i_bs, ne00, row_diff, stream);
2501 break;
2502 case GGML_TYPE_IQ1_M:
2503 mul_mat_vec_iq1_m_q8_1_sycl(src0_dd_i, src1_ddq_i_bs, dst_dd_i_bs, ne00, row_diff, stream);
2504 break;
2505 case GGML_TYPE_IQ2_XXS:
2506 mul_mat_vec_iq2_xxs_q8_1_sycl(src0_dd_i, src1_ddq_i_bs, dst_dd_i_bs, ne00, row_diff, stream);
2507 break;
2508 case GGML_TYPE_IQ2_XS:
2509 mul_mat_vec_iq2_xs_q8_1_sycl(src0_dd_i, src1_ddq_i_bs, dst_dd_i_bs, ne00, row_diff, stream);
2510 break;
2511 case GGML_TYPE_IQ2_S:
2512 mul_mat_vec_iq2_s_q8_1_sycl(src0_dd_i, src1_ddq_i_bs, dst_dd_i_bs, ne00, row_diff, stream);
2513 break;
2514 case GGML_TYPE_IQ3_XXS:
2515 if ((ggml_tensor_extra_gpu *) dst->src[0]->extra &&
2516 ((ggml_tensor_extra_gpu *) dst->src[0]->extra)->optimized_feature.reorder) {
2517 if (i == 0 && src1_ncols > 1 && src1_ncols <= 8) {
2518 const int stride_col_y_bytes = src1_padded_col_size * q8_1_ts / q8_1_bs;
2519 const int stride_col_dst = dst->ne[0];
2520 GGML_SYCL_DEBUG("Calling reordered IQ3_XXS multi-col MMVQ ncols=%d\n", (int) src1_ncols);
2521 reorder_mul_mat_vec_iq3_q8_1_sycl_switch_ncols<GGML_TYPE_IQ3_XXS>(
2522 src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff,
2523 src1_ncols, stride_col_y_bytes, stride_col_dst, stream);
2524 return;
2525 } else {
2526 GGML_SYCL_DEBUG("Calling reordered IQ3_XXS MMVQ\n");
2527 reorder_mul_mat_vec_iq3_q8_1_sycl<GGML_TYPE_IQ3_XXS>(
2528 src0_dd_i, src1_ddq_i_bs, dst_dd_i_bs, ne00, row_diff, stream);
2529 }
2530 } else {
2531 mul_mat_vec_iq3_xxs_q8_1_sycl(
2532 src0_dd_i, src1_ddq_i_bs, dst_dd_i_bs, ne00, row_diff, stream);
2533 }
2534 break;
2535 case GGML_TYPE_IQ3_S:
2536 if ((ggml_tensor_extra_gpu *) dst->src[0]->extra &&
2537 ((ggml_tensor_extra_gpu *) dst->src[0]->extra)->optimized_feature.reorder) {
2538 if (i == 0 && src1_ncols > 1 && src1_ncols <= 8) {
2539 const int stride_col_y_bytes = src1_padded_col_size * q8_1_ts / q8_1_bs;
2540 const int stride_col_dst = dst->ne[0];
2541 GGML_SYCL_DEBUG("Calling reordered IQ3_S multi-col MMVQ ncols=%d\n", (int) src1_ncols);
2542 reorder_mul_mat_vec_iq3_q8_1_sycl_switch_ncols<GGML_TYPE_IQ3_S>(
2543 src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff,
2544 src1_ncols, stride_col_y_bytes, stride_col_dst, stream);
2545 return;
2546 } else {
2547 GGML_SYCL_DEBUG("Calling reordered IQ3_S MMVQ\n");
2548 reorder_mul_mat_vec_iq3_q8_1_sycl<GGML_TYPE_IQ3_S>(
2549 src0_dd_i, src1_ddq_i_bs, dst_dd_i_bs, ne00, row_diff, stream);
2550 }
2551 } else if (i == 0 && src1_ncols > 1 && src1_ncols <= 8) {
2552 const int stride_col_y = src1_padded_col_size / QK8_1;
2553 const int stride_col_dst = dst->ne[0];
2554 GGML_SYCL_DEBUG("Calling mul_mat_vec_iq3_s_q8_1_sycl_switch_ncols ncols=%d\n", (int) src1_ncols);
2555 mul_mat_vec_iq3_s_q8_1_sycl_switch_ncols(
2556 src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff,
2557 src1_ncols, stride_col_y, stride_col_dst, stream);
2558 return;
2559 } else if (i == 0 || src1_ncols == 1) {
2560 mul_mat_vec_iq3_s_q8_1_sycl(src0_dd_i, src1_ddq_i_bs, dst_dd_i_bs, ne00, row_diff, stream);
2561 }
2562 break;
2563 case GGML_TYPE_IQ4_NL:
2564 mul_mat_vec_iq4_nl_q8_1_sycl(src0_dd_i, src1_ddq_i_bs, dst_dd_i_bs, ne00, row_diff, stream);
2565 break;
2566 case GGML_TYPE_IQ4_XS:
2567 if (i == 0 && src1_ncols > 1 && src1_ncols <= 8) {
2568 const int stride_col_y = src1_padded_col_size / QK8_1;
2569 const int stride_col_dst = dst->ne[0];
2570 GGML_SYCL_DEBUG("Calling mul_mat_vec_iq4_xs_q8_1_sycl_switch_ncols ncols=%d\n", (int)src1_ncols);
2571 mul_mat_vec_iq4_xs_q8_1_sycl_switch_ncols(
2572 src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff,
2573 src1_ncols, stride_col_y, stride_col_dst, stream);
2574 return;
2575 } else if (i == 0 || src1_ncols == 1) {
2576 mul_mat_vec_iq4_xs_q8_1_sycl(src0_dd_i, src1_ddq_i_bs, dst_dd_i_bs, ne00, row_diff, stream);
2577 }
2578 break;
2579 case GGML_TYPE_MXFP4:
2580 if ((ggml_tensor_extra_gpu *) dst->src[0]->extra &&
2581 ((ggml_tensor_extra_gpu *) dst->src[0]->extra)->optimized_feature.reorder) {
2582 if (i == 0 && src1_ncols > 1 && src1_ncols <= 8) {
2583 const int stride_col_y_bytes = src1_padded_col_size * q8_1_ts / q8_1_bs;
2584 const int stride_col_dst = dst->ne[0];
2585 GGML_SYCL_DEBUG("Calling reorder_mul_mat_vec_mxfp4_q8_1_sycl_switch_ncols ncols=%d\n", (int)src1_ncols);
2586 reorder_mul_mat_vec_mxfp4_q8_1_sycl_switch_ncols(
2587 src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff,
2588 src1_ncols, stride_col_y_bytes, stride_col_dst, stream);
2589 return;
2590 } else {
2591 GGML_SYCL_DEBUG("Calling reorder_mul_mat_vec_mxfp4_q8_1_sycl\n");
2592 reorder_mul_mat_vec_mxfp4_q8_1_sycl(src0_dd_i, src1_ddq_i_bs, dst_dd_i_bs, ne00, row_diff, stream);
2593 }
2594 } else if (i == 0 && src1_ncols > 1 && src1_ncols <= 8) {
2595 const int stride_col_y = src1_padded_col_size / QK8_1;
2596 const int stride_col_dst = dst->ne[0];
2597 GGML_SYCL_DEBUG("Calling mul_mat_vec_mxfp4_q8_1_sycl_switch_ncols ncols=%d\n", (int)src1_ncols);
2598 mul_mat_vec_mxfp4_q8_1_sycl_switch_ncols(
2599 src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff,
2600 src1_ncols, stride_col_y, stride_col_dst, stream);
2601 return;
2602 } else if (i == 0 || src1_ncols == 1) {
2603 mul_mat_vec_mxfp4_q8_1_sycl(src0_dd_i, src1_ddq_i_bs, dst_dd_i_bs, ne00, row_diff, stream);
2604 }
2605 break;
2606 case GGML_TYPE_NVFP4:
2607 if (i == 0 && src1_ncols > 1 && src1_ncols <= 8) {
2608 const int stride_col_y = src1_padded_col_size / QK8_1;
2609 const int stride_col_dst = dst->ne[0];
2610 GGML_SYCL_DEBUG("Calling mul_mat_vec_nvfp4_q8_1_sycl_switch_ncols ncols=%d\n", (int)src1_ncols);
2611 mul_mat_vec_nvfp4_q8_1_sycl_switch_ncols(
2612 src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff,
2613 src1_ncols, stride_col_y, stride_col_dst, stream);
2614 return;
2615 } else if (i == 0 || src1_ncols == 1) {
2616 mul_mat_vec_nvfp4_q8_1_sycl(src0_dd_i, src1_ddq_i_bs, dst_dd_i_bs, ne00, row_diff, stream);
2617 }
2618 break;
2619 default:
2620 GGML_ABORT("fatal error: unsupport src0 data type %s\n", ggml_type_name(src0->type));
2621 }
2622 }
2623 GGML_UNUSED(src1);
2624 GGML_UNUSED(dst);
2625 GGML_UNUSED(src1_ddf_i);
2626 GGML_UNUSED(ctx);
2627}
2628
2629// src1_row_stride: 0 for shared src1 (gate/up proj), else per-expert stride (down proj).
2630template <int qk, int qi, typename block_q_t, int vdr, vec_dot_q_sycl_t vec_dot_q_sycl>
2631static void mul_mat_vec_q_moe(
2632 const void * __restrict__ vx_base, const void * __restrict__ vy_base,
2633 float * __restrict__ dst_base, const int32_t * __restrict__ ids_dev,
2634 const int ncols, const int nrows,
2635 const size_t expert_weight_stride, const size_t dst_row_stride,
2636 const size_t src1_row_stride,
2637 const sycl::nd_item<3> & item_ct1) {
2638
2639 const int expert_idx = item_ct1.get_group(1);
2640 const int i02 = ids_dev[expert_idx];
2641
2642 const char * vy = (const char *) vy_base + (size_t) expert_idx * src1_row_stride;
2643 float * dst = (float *) ((char *) dst_base + (size_t) expert_idx * dst_row_stride);
2644
2645 const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) + item_ct1.get_local_id(1);
2646
2647 if (row >= nrows) {
2648 return;
2649 }
2650
2651 if (i02 == -1) { // skipped slot
2652 if (item_ct1.get_local_id(2) == 0) {
2653 dst[row] = 0.0f;
2654 }
2655 return;
2656 }
2657
2658 const char * vx = (const char *) vx_base + (size_t) i02 * expert_weight_stride;
2659
2660 const int blocks_per_row = ncols / qk;
2661 constexpr int blocks_per_warp = (vdr * WARP_SIZE + qi - 1) / qi;
2662
2663 float tmp = 0.0f;
2664
2665 const block_q_t * x = (const block_q_t *) vx;
2666 const block_q8_1 * y = (const block_q8_1 *) vy;
2667
2668 for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row; i += blocks_per_warp) {
2669 const int ibx = row * blocks_per_row + i;
2670 const int iby = i * (qk / QK8_1);
2671
2672 for (size_t elem = 0; elem < qi / vdr; elem += WARP_SIZE) {
2673 const int iqs = elem + vdr * (item_ct1.get_local_id(2) % (qi / vdr));
2674 tmp += vec_dot_q_sycl(&x[ibx], &y[iby], iqs);
2675 }
2676 }
2677
2678#pragma unroll
2679 for (int mask = WARP_SIZE / 2; mask > 0; mask >>= 1) {
2680 tmp += dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
2681 }
2682
2683 if (item_ct1.get_local_id(2) == 0) {
2684 dst[row] = tmp;
2685 }
2686}
2687
2688template <int qk, int qi, typename block_q_t, int vdr, vec_dot_q_sycl_t vec_dot_q_sycl>
2689static void launch_mul_mat_vec_q_moe(
2690 const void * vx_base, const void * vy, const int32_t * ids_dev,
2691 float * dst_base, const int ncols, const int nrows, const int n_experts_used,
2692 const size_t expert_weight_stride, const size_t dst_row_stride,
2693 const size_t src1_row_stride,
2694 dpct::queue_ptr stream) {
2695 const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
2696 const sycl::range<3> block_nums(1, (unsigned) n_experts_used, (unsigned) block_num_y);
2697 const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
2698 stream->submit([&](sycl::handler & cgh) {
2699 cgh.parallel_for(
2700 sycl::nd_range<3>(block_nums * block_dims, block_dims),
2701 [=](sycl::nd_item<3> item) [[sycl::reqd_sub_group_size(WARP_SIZE)]] {
2702 mul_mat_vec_q_moe<qk, qi, block_q_t, vdr, vec_dot_q_sycl>(
2703 vx_base, vy, dst_base, ids_dev, ncols, nrows,
2704 expert_weight_stride, dst_row_stride, src1_row_stride, item);
2705 });
2706 });
2707}
2708
2709bool ggml_sycl_mul_mat_vec_q_id(
2710 enum ggml_type src0_type,
2711 const void * vx_base,
2712 const void * vy,
2713 const int32_t * ids_dev,
2714 float * dst_base,
2715 int ncols,
2716 int nrows,
2717 int n_experts_used,
2718 size_t expert_weight_stride,
2719 size_t dst_row_stride,
2720 size_t src1_row_stride,
2721 dpct::queue_ptr stream) {
2722 // if adding a new type, ensure it is also added to ggml_sycl_supports_mmvq_id
2723 switch (src0_type) {
2724 case GGML_TYPE_Q4_0:
2725 launch_mul_mat_vec_q_moe<QK4_0, QI4_0, block_q4_0, VDR_Q4_0_Q8_1_MMVQ, vec_dot_q4_0_q8_1>(
2726 vx_base, vy, ids_dev, dst_base, ncols, nrows, n_experts_used,
2727 expert_weight_stride, dst_row_stride, src1_row_stride, stream);
2728 return true;
2729 case GGML_TYPE_Q4_1:
2730 launch_mul_mat_vec_q_moe<QK4_1, QI4_1, block_q4_1, VDR_Q4_1_Q8_1_MMVQ, vec_dot_q4_1_q8_1>(
2731 vx_base, vy, ids_dev, dst_base, ncols, nrows, n_experts_used,
2732 expert_weight_stride, dst_row_stride, src1_row_stride, stream);
2733 return true;
2734 case GGML_TYPE_Q5_0:
2735 launch_mul_mat_vec_q_moe<QK5_0, QI5_0, block_q5_0, VDR_Q5_0_Q8_1_MMVQ, vec_dot_q5_0_q8_1>(
2736 vx_base, vy, ids_dev, dst_base, ncols, nrows, n_experts_used,
2737 expert_weight_stride, dst_row_stride, src1_row_stride, stream);
2738 return true;
2739 case GGML_TYPE_Q5_1:
2740 launch_mul_mat_vec_q_moe<QK5_1, QI5_1, block_q5_1, VDR_Q5_1_Q8_1_MMVQ, vec_dot_q5_1_q8_1>(
2741 vx_base, vy, ids_dev, dst_base, ncols, nrows, n_experts_used,
2742 expert_weight_stride, dst_row_stride, src1_row_stride, stream);
2743 return true;
2744 case GGML_TYPE_Q8_0:
2745 launch_mul_mat_vec_q_moe<QK8_0, QI8_0, block_q8_0, VDR_Q8_0_Q8_1_MMVQ, vec_dot_q8_0_q8_1>(
2746 vx_base, vy, ids_dev, dst_base, ncols, nrows, n_experts_used,
2747 expert_weight_stride, dst_row_stride, src1_row_stride, stream);
2748 return true;
2749 case GGML_TYPE_Q2_0:
2750 launch_mul_mat_vec_q_moe<QK2_0, QI2_0, block_q2_0, VDR_Q2_0_Q8_1_MMVQ, vec_dot_q2_0_q8_1>(
2751 vx_base, vy, ids_dev, dst_base, ncols, nrows, n_experts_used,
2752 expert_weight_stride, dst_row_stride, src1_row_stride, stream);
2753 return true;
2754 case GGML_TYPE_Q2_K:
2755 launch_mul_mat_vec_q_moe<QK_K, QI2_K, block_q2_K, VDR_Q2_K_Q8_1_MMVQ, vec_dot_q2_K_q8_1>(
2756 vx_base, vy, ids_dev, dst_base, ncols, nrows, n_experts_used,
2757 expert_weight_stride, dst_row_stride, src1_row_stride, stream);
2758 return true;
2759 case GGML_TYPE_Q3_K:
2760 launch_mul_mat_vec_q_moe<QK_K, QI3_K, block_q3_K, VDR_Q3_K_Q8_1_MMVQ, vec_dot_q3_K_q8_1>(
2761 vx_base, vy, ids_dev, dst_base, ncols, nrows, n_experts_used,
2762 expert_weight_stride, dst_row_stride, src1_row_stride, stream);
2763 return true;
2764 case GGML_TYPE_Q4_K:
2765 launch_mul_mat_vec_q_moe<QK_K, QI4_K, block_q4_K, VDR_Q4_K_Q8_1_MMVQ, vec_dot_q4_K_q8_1>(
2766 vx_base, vy, ids_dev, dst_base, ncols, nrows, n_experts_used,
2767 expert_weight_stride, dst_row_stride, src1_row_stride, stream);
2768 return true;
2769 case GGML_TYPE_Q5_K:
2770 launch_mul_mat_vec_q_moe<QK_K, QI5_K, block_q5_K, VDR_Q5_K_Q8_1_MMVQ, vec_dot_q5_K_q8_1>(
2771 vx_base, vy, ids_dev, dst_base, ncols, nrows, n_experts_used,
2772 expert_weight_stride, dst_row_stride, src1_row_stride, stream);
2773 return true;
2774 case GGML_TYPE_Q6_K:
2775 launch_mul_mat_vec_q_moe<QK_K, QI6_K, block_q6_K, VDR_Q6_K_Q8_1_MMVQ, vec_dot_q6_K_q8_1>(
2776 vx_base, vy, ids_dev, dst_base, ncols, nrows, n_experts_used,
2777 expert_weight_stride, dst_row_stride, src1_row_stride, stream);
2778 return true;
2779 case GGML_TYPE_MXFP4:
2780 launch_mul_mat_vec_q_moe<QK_MXFP4, QI_MXFP4, block_mxfp4, VDR_MXFP4_Q8_1_MMVQ, vec_dot_mxfp4_q8_1>(
2781 vx_base, vy, ids_dev, dst_base, ncols, nrows, n_experts_used,
2782 expert_weight_stride, dst_row_stride, src1_row_stride, stream);
2783 return true;
2784 case GGML_TYPE_NVFP4:
2785 launch_mul_mat_vec_q_moe<QK_NVFP4, QI_NVFP4, block_nvfp4, VDR_NVFP4_Q8_1_MMVQ, vec_dot_nvfp4_q8_1>(
2786 vx_base, vy, ids_dev, dst_base, ncols, nrows, n_experts_used,
2787 expert_weight_stride, dst_row_stride, src1_row_stride, stream);
2788 return true;
2789 case GGML_TYPE_IQ2_XXS:
2790 launch_mul_mat_vec_q_moe<QK_K, QI2_XXS/2, block_iq2_xxs, VDR_IQ2_XXS_Q8_1_MMVQ, vec_dot_iq2_xxs_q8_1_moe>(
2791 vx_base, vy, ids_dev, dst_base, ncols, nrows, n_experts_used,
2792 expert_weight_stride, dst_row_stride, src1_row_stride, stream);
2793 return true;
2794 case GGML_TYPE_IQ2_XS:
2795 launch_mul_mat_vec_q_moe<QK_K, QI2_XS/2, block_iq2_xs, VDR_IQ2_XS_Q8_1_MMVQ, vec_dot_iq2_xs_q8_1_moe>(
2796 vx_base, vy, ids_dev, dst_base, ncols, nrows, n_experts_used,
2797 expert_weight_stride, dst_row_stride, src1_row_stride, stream);
2798 return true;
2799 case GGML_TYPE_IQ2_S:
2800 launch_mul_mat_vec_q_moe<QK_K, QI2_S/2, block_iq2_s, VDR_IQ2_S_Q8_1_MMVQ, vec_dot_iq2_s_q8_1>(
2801 vx_base, vy, ids_dev, dst_base, ncols, nrows, n_experts_used,
2802 expert_weight_stride, dst_row_stride, src1_row_stride, stream);
2803 return true;
2804 case GGML_TYPE_IQ3_XXS:
2805 launch_mul_mat_vec_q_moe<QK_K, QI3_XXS/2, block_iq3_xxs, VDR_IQ3_XXS_Q8_1_MMVQ, vec_dot_iq3_xxs_q8_1_moe>(
2806 vx_base, vy, ids_dev, dst_base, ncols, nrows, n_experts_used,
2807 expert_weight_stride, dst_row_stride, src1_row_stride, stream);
2808 return true;
2809 case GGML_TYPE_IQ3_S:
2810 launch_mul_mat_vec_q_moe<QK_K, QI3_S/2, block_iq3_s, VDR_IQ3_S_Q8_1_MMVQ, vec_dot_iq3_s_q8_1_adapter>(
2811 vx_base, vy, ids_dev, dst_base, ncols, nrows, n_experts_used,
2812 expert_weight_stride, dst_row_stride, src1_row_stride, stream);
2813 return true;
2814 case GGML_TYPE_IQ1_S:
2815 launch_mul_mat_vec_q_moe<QK_K, QI1_S, block_iq1_s, VDR_IQ1_S_Q8_1_MMVQ, vec_dot_iq1_s_q8_1_moe>(
2816 vx_base, vy, ids_dev, dst_base, ncols, nrows, n_experts_used,
2817 expert_weight_stride, dst_row_stride, src1_row_stride, stream);
2818 return true;
2819 case GGML_TYPE_IQ1_M:
2820 launch_mul_mat_vec_q_moe<QK_K, QI1_S, block_iq1_m, VDR_IQ1_M_Q8_1_MMVQ, vec_dot_iq1_m_q8_1>(
2821 vx_base, vy, ids_dev, dst_base, ncols, nrows, n_experts_used,
2822 expert_weight_stride, dst_row_stride, src1_row_stride, stream);
2823 return true;
2824 case GGML_TYPE_IQ4_NL:
2825 launch_mul_mat_vec_q_moe<QK4_NL, QI4_NL, block_iq4_nl, VDR_IQ4_NL_Q8_1_MMVQ, vec_dot_iq4_nl_q8_1>(
2826 vx_base, vy, ids_dev, dst_base, ncols, nrows, n_experts_used,
2827 expert_weight_stride, dst_row_stride, src1_row_stride, stream);
2828 return true;
2829 case GGML_TYPE_IQ4_XS:
2830 launch_mul_mat_vec_q_moe<QK_K, QI4_XS/4, block_iq4_xs, VDR_IQ4_XS_Q8_1_MMVQ, vec_dot_iq4_xs_q8_1>(
2831 vx_base, vy, ids_dev, dst_base, ncols, nrows, n_experts_used,
2832 expert_weight_stride, dst_row_stride, src1_row_stride, stream);
2833 return true;
2834 default:
2835 return false;
2836 }
2837}
2838
2839// Reorder (SoA) MoE expert GEMV: MoE expert/row/lane indexing (from mul_mat_vec_q_moe) with the
2840// dense-reorder per-block reads (from mul_mat_vec_q_reorder). Each expert slice in vx_base is a
2841// self-contained SoA, so nblocks = nrows*(ncols/qk) per expert and the constant expert stride holds.
2842template <typename reorder_vec_dot_q_sycl>
2843static void mul_mat_vec_q_moe_reorder(
2844 const void * __restrict__ vx_base, const void * __restrict__ vy_base,
2845 float * __restrict__ dst_base, const int32_t * __restrict__ ids_dev,
2846 const int ncols, const int nrows,
2847 const size_t expert_weight_stride, const size_t dst_row_stride,
2848 const size_t src1_row_stride,
2849 const sycl::nd_item<3> & item_ct1) {
2850 using block_type = ggml_sycl_reordered::block_q_t<reorder_vec_dot_q_sycl::gtype>;
2851 using block_traits = typename block_type::traits;
2852
2853 const int expert_idx = item_ct1.get_group(1);
2854 const int i02 = ids_dev[expert_idx];
2855
2856 const char * vy = (const char *) vy_base + (size_t) expert_idx * src1_row_stride;
2857 float * dst = (float *) ((char *) dst_base + (size_t) expert_idx * dst_row_stride);
2858
2859 const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) + item_ct1.get_local_id(1);
2860 if (row >= nrows) {
2861 return;
2862 }
2863
2864 const auto sg = item_ct1.get_sub_group();
2865
2866 if (i02 == -1) { // skipped slot
2867 if (sg.leader()) {
2868 dst[row] = 0.0f;
2869 }
2870 return;
2871 }
2872
2873 const char * vx = (const char *) vx_base + (size_t) i02 * expert_weight_stride;
2874
2875 constexpr int vdr_mmvq = reorder_vec_dot_q_sycl::gtype == GGML_TYPE_Q8_0 ?
2876 VDR_Q8_0_Q8_1_MMVQ : block_traits::vdr_mmvq;
2877 const int blocks_per_row = ncols / block_traits::qk;
2878 constexpr int blocks_per_subgroup = ceil_div(vdr_mmvq * WARP_SIZE, block_traits::qi);
2879 constexpr int block_elements_per_subgroup = block_traits::qi / vdr_mmvq;
2880 const int nblocks = nrows * (ncols / block_traits::qk);
2881
2882 static_assert(blocks_per_subgroup > 0);
2883 static_assert(block_elements_per_subgroup > 0);
2884
2885 float partial_sum = 0.0f;
2886 for (int i = sg.get_local_linear_id() / block_elements_per_subgroup; i < blocks_per_row; i += blocks_per_subgroup) {
2887 const int ibx = row * blocks_per_row + i;
2888
2889 const auto bx_offset = block_type::get_block_offset(ibx, nblocks);
2890 const auto d_offset = block_type::get_d_offset(nrows, ncols, ibx);
2891
2892 const int iby = i * block_type::block_to_q8_1_ratio();
2893 const int8_t * q8_1_quant_ptr = (const int8_t *) vy + iby * QK8_1;
2894 const sycl::half2 * q8_1_ds_ptr = (const sycl::half2 *) ((const char *) vy + ncols + iby * sizeof(sycl::half2));
2895
2896#pragma unroll
2897 for (int elem = 0; elem < block_elements_per_subgroup; elem += WARP_SIZE) {
2898 const int iqs = elem + vdr_mmvq * (sg.get_local_linear_id() % block_elements_per_subgroup);
2899 if constexpr (reorder_vec_dot_q_sycl::gtype == GGML_TYPE_Q8_0) {
2900 partial_sum += reorder_vec_dot_q_sycl()(vx, bx_offset, d_offset, q8_1_quant_ptr, q8_1_ds_ptr, iqs, vdr_mmvq);
2901 } else {
2902 partial_sum += reorder_vec_dot_q_sycl()(vx, bx_offset, d_offset, q8_1_quant_ptr, q8_1_ds_ptr, iqs);
2903 }
2904 }
2905 }
2906
2907 float sum = partial_sum;
2908 if constexpr (reorder_vec_dot_q_sycl::gtype == GGML_TYPE_Q8_0) {
2909 // Keep the accumulation order of the non-reordered MoE kernel.
2910 for (int mask = WARP_SIZE / 2; mask > 0; mask >>= 1) {
2911 sum += dpct::permute_sub_group_by_xor(sg, sum, mask);
2912 }
2913 } else {
2914 sum = sycl::reduce_over_group(sg, partial_sum, std::plus<>());
2915 }
2916 if (sg.leader()) {
2917 dst[row] = sum;
2918 }
2919}
2920
2921template <typename reorder_vec_dot_q_sycl>
2922static void launch_mul_mat_vec_q_moe_reorder(
2923 const void * vx_base, const void * vy, const int32_t * ids_dev,
2924 float * dst_base, const int ncols, const int nrows, const int n_experts_used,
2925 const size_t expert_weight_stride, const size_t dst_row_stride,
2926 const size_t src1_row_stride,
2927 dpct::queue_ptr stream) {
2928 const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
2929 const sycl::range<3> block_nums(1, (unsigned) n_experts_used, (unsigned) block_num_y);
2930 const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
2931 stream->submit([&](sycl::handler & cgh) {
2932 cgh.parallel_for(
2933 sycl::nd_range<3>(block_nums * block_dims, block_dims),
2934 [=](sycl::nd_item<3> item) [[sycl::reqd_sub_group_size(WARP_SIZE)]] {
2935 mul_mat_vec_q_moe_reorder<reorder_vec_dot_q_sycl>(
2936 vx_base, vy, dst_base, ids_dev, ncols, nrows,
2937 expert_weight_stride, dst_row_stride, src1_row_stride, item);
2938 });
2939 });
2940}
2941
2942bool ggml_sycl_mul_mat_vec_q_id_reorder(
2943 enum ggml_type src0_type,
2944 const void * vx_base,
2945 const void * vy,
2946 const int32_t * ids_dev,
2947 float * dst_base,
2948 int ncols,
2949 int nrows,
2950 int n_experts_used,
2951 size_t expert_weight_stride,
2952 size_t dst_row_stride,
2953 size_t src1_row_stride,
2954 dpct::queue_ptr stream) {
2955 switch (src0_type) {
2956 case GGML_TYPE_Q8_0:
2957 launch_mul_mat_vec_q_moe_reorder<reorder_vec_dot_q_sycl<GGML_TYPE_Q8_0>>(
2958 vx_base, vy, ids_dev, dst_base, ncols, nrows, n_experts_used,
2959 expert_weight_stride, dst_row_stride, src1_row_stride, stream);
2960 return true;
2961 case GGML_TYPE_Q4_K:
2962 launch_mul_mat_vec_q_moe_reorder<reorder_vec_dot_q_sycl<GGML_TYPE_Q4_K>>(
2963 vx_base, vy, ids_dev, dst_base, ncols, nrows, n_experts_used,
2964 expert_weight_stride, dst_row_stride, src1_row_stride, stream);
2965 return true;
2966 case GGML_TYPE_Q5_K:
2967 launch_mul_mat_vec_q_moe_reorder<reorder_vec_dot_q_sycl<GGML_TYPE_Q5_K>>(
2968 vx_base, vy, ids_dev, dst_base, ncols, nrows, n_experts_used,
2969 expert_weight_stride, dst_row_stride, src1_row_stride, stream);
2970 return true;
2971 case GGML_TYPE_Q6_K:
2972 launch_mul_mat_vec_q_moe_reorder<reorder_vec_dot_q_sycl<GGML_TYPE_Q6_K>>(
2973 vx_base, vy, ids_dev, dst_base, ncols, nrows, n_experts_used,
2974 expert_weight_stride, dst_row_stride, src1_row_stride, stream);
2975 return true;
2976 case GGML_TYPE_MXFP4:
2977 launch_mul_mat_vec_q_moe_reorder<reorder_vec_dot_q_sycl<GGML_TYPE_MXFP4>>(
2978 vx_base, vy, ids_dev, dst_base, ncols, nrows, n_experts_used,
2979 expert_weight_stride, dst_row_stride, src1_row_stride, stream);
2980 return true;
2981 default:
2982 return false;
2983 }
2984}
2985
2986template <typename reorder_vec_dot_q_sycl, int ncols_dst, int rows_per_sg>
2987static void launch_mul_mat_vec_q_reorder_glu_impl(const void * vx, const void * vgate, const void * vy, float * dst,
2988 const int ncols, const int nrows, const int stride_col_y_bytes,
2989 const int stride_col_dst, const ggml_glu_op glu_op,
2990 dpct::queue_ptr stream) {
2991 GGML_ASSERT(ncols % QK_K == 0);
2992
2993 constexpr size_t num_subgroups = WARP_SIZE;
2994
2995 const int block_num_y = ceil_div(nrows, GGML_SYCL_MMV_Y * (int) num_subgroups * rows_per_sg);
2996 const sycl::range<3> block_nums(1, 1, block_num_y);
2997 const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, num_subgroups * WARP_SIZE);
2998
2999 stream->submit([&](sycl::handler & cgh) {
3000 cgh.parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
3001 [=](sycl::nd_item<3> nd_item) [[sycl::reqd_sub_group_size(WARP_SIZE)]] {
3002 mul_mat_vec_q_reorder_ncols<reorder_vec_dot_q_sycl, ncols_dst, /*has_fusion=*/ true,
3003 rows_per_sg>(
3004 vx, vgate, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, glu_op,
3005 nd_item);
3006 });
3007 });
3008}
3009
3010template <typename reorder_vec_dot_q_sycl, int ncols_dst>
3011static void launch_mul_mat_vec_q_reorder_glu(const void * vx, const void * vgate, const void * vy, float * dst,
3012 const int ncols, const int nrows, const int stride_col_y_bytes,
3013 const int stride_col_dst, const ggml_glu_op glu_op,
3014 dpct::queue_ptr stream) {
3015 // q4_K pairs rows for 3..4 columns, q5_K for 3..5
3016 constexpr int row_pair_max = reorder_vec_dot_q_sycl::gtype == GGML_TYPE_Q5_K ? 5 : 4;
3017 constexpr int rows_per_sg =
3018 reorder_vec_dot_shared_activations<reorder_vec_dot_q_sycl::gtype>::value && ncols_dst >= 3 &&
3019 ncols_dst <= row_pair_max
3020 ? 2
3021 : 1;
3022 launch_mul_mat_vec_q_reorder_glu_impl<reorder_vec_dot_q_sycl, ncols_dst, rows_per_sg>(vx, vgate, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, glu_op, stream);
3023}
3024
3025// ---------------------------------------------------------------------------
3026// Fused dense-FFN GEMV + GLU over the standard (non-reorder) weight layout.
3027//
3028// Unlike the reorder variant below, the two weights may carry different block
3029// types (e.g. an unsloth UD mix with an iq4_xs gate and a q5_K up), as long as
3030// both quantize in QK_K-sized super-blocks so that one q8_1 activation
3031// quantization serves both dots. Per-operand accumulation order matches
3032// mul_mat_vec_q exactly, so results are bit-identical to running the three
3033// nodes separately.
3034// ---------------------------------------------------------------------------
3035template <int qi_g, typename block_g_t, int vdr_g, vec_dot_q_sycl_t vec_dot_g,
3036 int qi_u, typename block_u_t, int vdr_u, vec_dot_q_sycl_t vec_dot_u, int ncols_dst>
3037static void mul_mat_vec_q_glu(const void * __restrict__ vxg, const void * __restrict__ vxu,
3038 const void * __restrict__ vy, float * __restrict__ dst, const int ncols,
3039 const int nrows, const int stride_col_y, const int stride_col_dst,
3040 const ggml_glu_op glu_op, const sycl::nd_item<3> & item_ct1) {
3041 static_assert(QK_K % QK8_1 == 0);
3042 const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) + item_ct1.get_local_id(1);
3043 if (row >= nrows) {
3044 return;
3045 }
3046 const int blocks_per_row = ncols / QK_K;
3047 constexpr int blocks_per_warp_g = (vdr_g * WARP_SIZE + qi_g - 1) / qi_g;
3048 constexpr int blocks_per_warp_u = (vdr_u * WARP_SIZE + qi_u - 1) / qi_u;
3049 // one partial sum per output column, per operand
3050 float tmpg[ncols_dst] = {0.0f};
3051 float tmpu[ncols_dst] = {0.0f};
3052 const block_g_t * xg = (const block_g_t *) vxg;
3053 const block_u_t * xu = (const block_u_t *) vxu;
3054 const block_q8_1 * y = (const block_q8_1 *) vy;
3055 for (int i = item_ct1.get_local_id(2) / (qi_g / vdr_g); i < blocks_per_row; i += blocks_per_warp_g) {
3056 const int ibx = row * blocks_per_row + i;
3057 const int iby = i * (QK_K / QK8_1);
3058 for (size_t elem = 0; elem < qi_g / vdr_g; elem += WARP_SIZE) {
3059 const int iqs = elem + vdr_g * (item_ct1.get_local_id(2) % (qi_g / vdr_g));
3060#pragma unroll
3061 for (int j = 0; j < ncols_dst; ++j) {
3062 tmpg[j] += vec_dot_g(&xg[ibx], &y[j * stride_col_y + iby], iqs);
3063 }
3064 }
3065 }
3066 for (int i = item_ct1.get_local_id(2) / (qi_u / vdr_u); i < blocks_per_row; i += blocks_per_warp_u) {
3067 const int ibx = row * blocks_per_row + i;
3068 const int iby = i * (QK_K / QK8_1);
3069 for (size_t elem = 0; elem < qi_u / vdr_u; elem += WARP_SIZE) {
3070 const int iqs = elem + vdr_u * (item_ct1.get_local_id(2) % (qi_u / vdr_u));
3071#pragma unroll
3072 for (int j = 0; j < ncols_dst; ++j) {
3073 tmpu[j] += vec_dot_u(&xu[ibx], &y[j * stride_col_y + iby], iqs);
3074 }
3075 }
3076 }
3077 // sum up partial sums and write back the activated product
3078#pragma unroll
3079 for (int j = 0; j < ncols_dst; ++j) {
3080#pragma unroll
3081 for (int mask = WARP_SIZE / 2; mask > 0; mask >>= 1) {
3082 tmpg[j] += dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmpg[j], mask);
3083 tmpu[j] += dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmpu[j], mask);
3084 }
3085 }
3086 if (item_ct1.get_local_id(2) == 0) {
3087#pragma unroll
3088 for (int j = 0; j < ncols_dst; ++j) {
3089 // uniform across the launch; the dispatcher only accepts SWIGLU and GEGLU
3090 const float gate = glu_op == GGML_GLU_OP_SWIGLU ? op_silu(tmpg[j]) : op_gelu(tmpg[j]);
3091 dst[j * stride_col_dst + row] = gate * tmpu[j];
3092 }
3093 }
3094}
3095
3096template <int qi_g, typename block_g_t, int vdr_g, vec_dot_q_sycl_t vec_dot_g,
3097 int qi_u, typename block_u_t, int vdr_u, vec_dot_q_sycl_t vec_dot_u, int ncols_dst>
3098static void launch_mul_mat_vec_q_glu(const void * vxg, const void * vxu, const void * vy, float * dst,
3099 const int ncols, const int nrows, const int stride_col_y,
3100 const int stride_col_dst, const ggml_glu_op glu_op,
3101 dpct::queue_ptr stream) {
3102 GGML_ASSERT(ncols % QK_K == 0);
3103 const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
3104 const sycl::range<3> block_nums(1, 1, block_num_y);
3105 const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
3106 stream->submit([&](sycl::handler & cgh) {
3107 cgh.parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
3108 [=](sycl::nd_item<3> nd_item) [[sycl::reqd_sub_group_size(WARP_SIZE)]] {
3109 mul_mat_vec_q_glu<qi_g, block_g_t, vdr_g, vec_dot_g,
3110 qi_u, block_u_t, vdr_u, vec_dot_u, ncols_dst>(
3111 vxg, vxu, vy, dst, ncols, nrows, stride_col_y, stride_col_dst,
3112 glu_op, nd_item);
3113 });
3114 });
3115}
3116
3117// Dispatch the plain-layout fused GLU GEMV over the activation batch: ncols_dst
3118// selects the kernel's per-column template parameter. Returns false when the
3119// batch exceeds the instantiated range; the caller falls back to unfused nodes.
3120template <int qi_g, typename block_g_t, int vdr_g, vec_dot_q_sycl_t vec_dot_g,
3121 int qi_u, typename block_u_t, int vdr_u, vec_dot_q_sycl_t vec_dot_u>
3122static bool dispatch_mul_mat_vec_q_glu_plain(const void * vgate, const void * vup, const void * vy,
3123 float * dst, const int ncols, const int nrows,
3124 const int stride_col_y, const int stride_col_dst,
3125 const ggml_glu_op glu_op, dpct::queue_ptr stream,
3126 const int ncols_dst) {
3127 switch (ncols_dst) {
3128 case 1:
3129 launch_mul_mat_vec_q_glu<qi_g, block_g_t, vdr_g, vec_dot_g,
3130 qi_u, block_u_t, vdr_u, vec_dot_u, 1>(
3131 vgate, vup, vy, dst, ncols, nrows, stride_col_y, stride_col_dst, glu_op, stream);
3132 return true;
3133 case 2:
3134 launch_mul_mat_vec_q_glu<qi_g, block_g_t, vdr_g, vec_dot_g,
3135 qi_u, block_u_t, vdr_u, vec_dot_u, 2>(
3136 vgate, vup, vy, dst, ncols, nrows, stride_col_y, stride_col_dst, glu_op, stream);
3137 return true;
3138 case 3:
3139 launch_mul_mat_vec_q_glu<qi_g, block_g_t, vdr_g, vec_dot_g,
3140 qi_u, block_u_t, vdr_u, vec_dot_u, 3>(
3141 vgate, vup, vy, dst, ncols, nrows, stride_col_y, stride_col_dst, glu_op, stream);
3142 return true;
3143 case 4:
3144 launch_mul_mat_vec_q_glu<qi_g, block_g_t, vdr_g, vec_dot_g,
3145 qi_u, block_u_t, vdr_u, vec_dot_u, 4>(
3146 vgate, vup, vy, dst, ncols, nrows, stride_col_y, stride_col_dst, glu_op, stream);
3147 return true;
3148 case 5:
3149 launch_mul_mat_vec_q_glu<qi_g, block_g_t, vdr_g, vec_dot_g,
3150 qi_u, block_u_t, vdr_u, vec_dot_u, 5>(
3151 vgate, vup, vy, dst, ncols, nrows, stride_col_y, stride_col_dst, glu_op, stream);
3152 return true;
3153 case 6:
3154 launch_mul_mat_vec_q_glu<qi_g, block_g_t, vdr_g, vec_dot_g,
3155 qi_u, block_u_t, vdr_u, vec_dot_u, 6>(
3156 vgate, vup, vy, dst, ncols, nrows, stride_col_y, stride_col_dst, glu_op, stream);
3157 return true;
3158 case 7:
3159 launch_mul_mat_vec_q_glu<qi_g, block_g_t, vdr_g, vec_dot_g,
3160 qi_u, block_u_t, vdr_u, vec_dot_u, 7>(
3161 vgate, vup, vy, dst, ncols, nrows, stride_col_y, stride_col_dst, glu_op, stream);
3162 return true;
3163 case 8:
3164 launch_mul_mat_vec_q_glu<qi_g, block_g_t, vdr_g, vec_dot_g,
3165 qi_u, block_u_t, vdr_u, vec_dot_u, 8>(
3166 vgate, vup, vy, dst, ncols, nrows, stride_col_y, stride_col_dst, glu_op, stream);
3167 return true;
3168 default:
3169 return false;
3170 }
3171}
3172
3173// Fused dense-FFN GEMV + GLU for weight pairs the reorder kernel does not cover.
3174// vgate/vup must be in the standard block layout; vy must be quantized with plain
3175// quantize_q8_1 (padded rows). stride_col_y is in block_q8_1 units.
3176// Returns false if the type pair or batch is unhandled; caller should fall back.
3177bool ggml_sycl_mul_mat_vec_q_glu_plain(enum ggml_type gate_type, enum ggml_type up_type,
3178 enum ggml_glu_op glu_op, const void * vgate, const void * vup,
3179 const void * vy, float * dst, int ncols, int nrows, int ncols_dst,
3180 int stride_col_y, int stride_col_dst, dpct::queue_ptr stream) {
3181 if (glu_op != GGML_GLU_OP_SWIGLU && glu_op != GGML_GLU_OP_GEGLU) {
3182 return false;
3183 }
3184 if (ncols % QK_K != 0) {
3185 return false;
3186 }
3187 if (gate_type == GGML_TYPE_Q5_K && up_type == GGML_TYPE_Q5_K) {
3188 return dispatch_mul_mat_vec_q_glu_plain<QI5_K, block_q5_K, VDR_Q5_K_Q8_1_MMVQ, vec_dot_q5_K_q8_1,
3189 QI5_K, block_q5_K, VDR_Q5_K_Q8_1_MMVQ, vec_dot_q5_K_q8_1>(
3190 vgate, vup, vy, dst, ncols, nrows, stride_col_y, stride_col_dst, glu_op, stream, ncols_dst);
3191 }
3192 if (gate_type == GGML_TYPE_IQ4_XS && up_type == GGML_TYPE_IQ4_XS) {
3193 return dispatch_mul_mat_vec_q_glu_plain<QI4_XS / 4, block_iq4_xs, VDR_IQ4_XS_Q8_1_MMVQ, vec_dot_iq4_xs_q8_1,
3194 QI4_XS / 4, block_iq4_xs, VDR_IQ4_XS_Q8_1_MMVQ, vec_dot_iq4_xs_q8_1>(
3195 vgate, vup, vy, dst, ncols, nrows, stride_col_y, stride_col_dst, glu_op, stream, ncols_dst);
3196 }
3197 if (gate_type == GGML_TYPE_IQ4_XS && up_type == GGML_TYPE_Q5_K) {
3198 return dispatch_mul_mat_vec_q_glu_plain<QI4_XS / 4, block_iq4_xs, VDR_IQ4_XS_Q8_1_MMVQ, vec_dot_iq4_xs_q8_1,
3199 QI5_K, block_q5_K, VDR_Q5_K_Q8_1_MMVQ, vec_dot_q5_K_q8_1>(
3200 vgate, vup, vy, dst, ncols, nrows, stride_col_y, stride_col_dst, glu_op, stream, ncols_dst);
3201 }
3202 if (gate_type == GGML_TYPE_Q5_K && up_type == GGML_TYPE_IQ4_XS) {
3203 return dispatch_mul_mat_vec_q_glu_plain<QI5_K, block_q5_K, VDR_Q5_K_Q8_1_MMVQ, vec_dot_q5_K_q8_1,
3204 QI4_XS / 4, block_iq4_xs, VDR_IQ4_XS_Q8_1_MMVQ, vec_dot_iq4_xs_q8_1>(
3205 vgate, vup, vy, dst, ncols, nrows, stride_col_y, stride_col_dst, glu_op, stream, ncols_dst);
3206 }
3207 return false;
3208}
3209
3210template <ggml_type type, int... Ns>
3211static bool mul_mat_vec_q_glu_reorder_ncols(enum ggml_glu_op glu_op, const void * vx, const void * vgate,
3212 const void * vy, float * dst, int ncols, int nrows, int ncols_dst,
3213 int stride_col_y_bytes, int stride_col_dst, dpct::queue_ptr stream) {
3214 using vec_dot = reorder_vec_dot_q_sycl<type>;
3215
3216 auto launch = [&](auto I) -> bool {
3217 constexpr int n = decltype(I)::value;
3218 if (ncols_dst != n) {
3219 return false;
3220 }
3221 if constexpr (type == GGML_TYPE_Q4_K && n == 2) {
3222 if (nrows >= Q4_K_MMVQ_ROW_PAIR_MIN_NROWS) {
3223 launch_mul_mat_vec_q_reorder_glu_impl<vec_dot, 2, 2>(vx, vgate, vy, dst, ncols, nrows, stride_col_y_bytes, stride_col_dst, glu_op, stream);
3224 return true;
3225 }
3226 }
3227 launch_mul_mat_vec_q_reorder_glu<vec_dot, n>(vx, vgate, vy, dst, ncols, nrows, stride_col_y_bytes,
3228 stride_col_dst, glu_op, stream);
3229 return true;
3230 };
3231
3232 // unary fold over launch
3233 return (launch(std::integral_constant<int, Ns>{}) || ...);
3234}
3235
3236bool ggml_sycl_mul_mat_vec_q_glu_reorder(enum ggml_type src0_type, enum ggml_glu_op glu_op, const void * vx,
3237 const void * vgate, const void * vy, float * dst, int ncols, int nrows,
3238 int ncols_dst, int stride_col_y_bytes, int stride_col_dst,
3239 dpct::queue_ptr stream) {
3240 if (glu_op != GGML_GLU_OP_SWIGLU && glu_op != GGML_GLU_OP_GEGLU) {
3241 return false;
3242 }
3243
3244 switch (src0_type) {
3245 case GGML_TYPE_Q4_K:
3246 return mul_mat_vec_q_glu_reorder_ncols<GGML_TYPE_Q4_K, 1, 2, 3, 4, 5, 6, 7, 8>(
3247 glu_op, vx, vgate, vy, dst, ncols, nrows, ncols_dst, stride_col_y_bytes, stride_col_dst, stream);
3248 case GGML_TYPE_Q5_K:
3249 // fusion declines q5_K past 5 columns
3250 return mul_mat_vec_q_glu_reorder_ncols<GGML_TYPE_Q5_K, 1, 2, 3, 4, 5>(
3251 glu_op, vx, vgate, vy, dst, ncols, nrows, ncols_dst, stride_col_y_bytes, stride_col_dst, stream);
3252 default:
3253 return false;
3254 }
3255}