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

722 lines · 32341 bytes Raw Blame History
1//
2// MIT license
3// Copyright (C) 2024 Intel Corporation
4// SPDX-License-Identifier: MIT
5//
6
7//
8// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
9// See https://llvm.org/LICENSE.txt for license information.
10// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
11//
12
13#include "ggml-impl.h"
14#include "common.hpp"
15#include "dequantize.hpp"
16#include "getrows.hpp"
17
18
19template<int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
20static void k_get_rows(
21 const void * src0, const int32_t * src1, dst_t * dst,
22 int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
23 /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
24 /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
25 /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
26 size_t s10, size_t s11, size_t s12,
27 const sycl::nd_item<3> &item_ct1/*, size_t s13*/) {
28
29 const int i00 = (item_ct1.get_group(2) * item_ct1.get_local_range(2) +
30 item_ct1.get_local_id(2)) *
31 2;
32 const int i10 = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
33 item_ct1.get_local_id(1);
34 const int i11 = (item_ct1.get_group(0) * item_ct1.get_local_range(0) +
35 item_ct1.get_local_id(0)) /
36 ne12;
37 const int i12 = (item_ct1.get_group(0) * item_ct1.get_local_range(0) +
38 item_ct1.get_local_id(0)) %
39 ne12;
40
41 if (i00 >= ne00) {
42 return;
43 }
44
45 const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
46
47 dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
48 const void * src0_row = (const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03;
49
50 const int ib = i00/qk; // block index
51 const int iqs = (i00%qk)/qr; // quant index
52 const int iybs = i00 - i00%qk; // dst block start index
53 const int y_offset = qr == 1 ? 1 : qk/2;
54
55 // dequantize
56 dfloat2 v;
57 dequantize_kernel(src0_row, ib, iqs, v);
58
59 dst_row[iybs + iqs + 0] = v.x();
60 dst_row[iybs + iqs + y_offset] = v.y();
61}
62
63template<int qk, int qr, dequantize_kernel_f32_t dequantize_kernel, typename dst_t>
64static void k_get_rows_f32(
65 const void * src0, const int32_t * src1, dst_t * dst,
66 int64_t ne00,
67 int64_t ne12,
68 size_t s1, size_t s2, size_t s3,
69 size_t nb01, size_t nb02, size_t nb03,
70 size_t s10, size_t s11, size_t s12,
71 const sycl::nd_item<3> &item_ct1) {
72
73 const int i00 = (item_ct1.get_group(2) * item_ct1.get_local_range(2) +
74 item_ct1.get_local_id(2)) *
75 2;
76 const int i10 = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
77 item_ct1.get_local_id(1);
78 const int i11 = (item_ct1.get_group(0) * item_ct1.get_local_range(0) +
79 item_ct1.get_local_id(0)) /
80 ne12;
81 const int i12 = (item_ct1.get_group(0) * item_ct1.get_local_range(0) +
82 item_ct1.get_local_id(0)) %
83 ne12;
84
85 if (i00 >= ne00) {
86 return;
87 }
88
89 const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
90
91 dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
92 const void * src0_row = (const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03;
93
94 const int ib = i00/qk;
95 const int iqs = (i00%qk)/qr;
96 const int iybs = i00 - i00%qk;
97 const int y_offset = qr == 1 ? 1 : qk/2;
98
99 float v0;
100 float v1;
101 dequantize_kernel(src0_row, ib, iqs, v0, v1);
102
103 dst_row[iybs + iqs + 0] = (dst_t) v0;
104 dst_row[iybs + iqs + y_offset] = (dst_t) v1;
105}
106
107template<typename src0_t, typename dst_t>
108static void k_get_rows_float(
109 const src0_t * src0, const int32_t * src1, dst_t * dst,
110 int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
111 /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
112 /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
113 /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
114 size_t s10, size_t s11, size_t s12,
115 const sycl::nd_item<3> &item_ct1/*, size_t s13*/) {
116
117 const int i00 = item_ct1.get_group(2) * item_ct1.get_local_range(2) +
118 item_ct1.get_local_id(2);
119 const int i10 = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
120 item_ct1.get_local_id(1);
121 const int i11 = (item_ct1.get_group(0) * item_ct1.get_local_range(0) +
122 item_ct1.get_local_id(0)) /
123 ne12;
124 const int i12 = (item_ct1.get_group(0) * item_ct1.get_local_range(0) +
125 item_ct1.get_local_id(0)) %
126 ne12;
127
128 if (i00 >= ne00) {
129 return;
130 }
131
132 const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
133
134 dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
135 const src0_t * src0_row = (const src0_t *)((const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03);
136
137 dst_row[i00] = src0_row[i00];
138}
139
140// src0 is in reorder layout: all qs of the tensor, then all d
141template<int qk, int qr, dequantize_kernel_t_reorder dequantize_kernel, typename dst_t>
142static void k_get_rows_reorder(
143 const void * src0_qs, const void * src0_d, const int32_t * src1, dst_t * dst,
144 int64_t ne00, int64_t ne12,
145 size_t s1, size_t s2, size_t s3,
146 size_t s10, size_t s11, size_t s12,
147 const sycl::nd_item<3> &item_ct1) {
148
149 const int i00 = (item_ct1.get_group(2) * item_ct1.get_local_range(2) +
150 item_ct1.get_local_id(2)) *
151 2;
152 const int i10 = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
153 item_ct1.get_local_id(1);
154 const int i11 = (item_ct1.get_group(0) * item_ct1.get_local_range(0) +
155 item_ct1.get_local_id(0)) /
156 ne12;
157 const int i12 = (item_ct1.get_group(0) * item_ct1.get_local_range(0) +
158 item_ct1.get_local_id(0)) %
159 ne12;
160
161 if (i00 >= ne00) {
162 return;
163 }
164
165 const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
166
167 dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
168
169 const int64_t ib = (int64_t) i01*(ne00/qk) + i00/qk; // block index in the whole tensor
170 const int iqs = (i00%qk)/qr;
171 const int iybs = i00 - i00%qk;
172 const int y_offset = qr == 1 ? 1 : qk/2;
173
174 dfloat2 v;
175 dequantize_kernel(src0_d, ib, src0_qs, ib*(qk/qr) + iqs, v);
176
177 dst_row[iybs + iqs + 0] = v.x();
178 dst_row[iybs + iqs + y_offset] = v.y();
179}
180
181template <int qk, int qr, dequantize_kernel_t dq>
182static void get_rows_sycl(ggml_backend_sycl_context & ctx, const ggml_tensor *src0, const ggml_tensor *src1,
183 ggml_tensor *dst, const void *src0_dd,
184 const int32_t *src1_dd, float *dst_dd,
185 queue_ptr stream) {
186
187 GGML_TENSOR_BINARY_OP_LOCALS
188
189 const sycl::range<3> block_dims(1, 1, SYCL_GET_ROWS_BLOCK_SIZE);
190 const int block_num_x = (ne00 + 2*SYCL_GET_ROWS_BLOCK_SIZE - 1) / (2*SYCL_GET_ROWS_BLOCK_SIZE);
191 const sycl::range<3> block_nums(ne11 * ne12, ne10, block_num_x);
192
193 // strides in elements
194 //const size_t s0 = nb0 / ggml_element_size(dst);
195 const size_t s1 = nb1 / ggml_element_size(dst);
196 const size_t s2 = nb2 / ggml_element_size(dst);
197 const size_t s3 = nb3 / ggml_element_size(dst);
198
199 const size_t s10 = nb10 / ggml_element_size(src1);
200 const size_t s11 = nb11 / ggml_element_size(src1);
201 const size_t s12 = nb12 / ggml_element_size(src1);
202 //const size_t s13 = nb13 / ggml_element_size(src1);
203
204 GGML_ASSERT(ne00 % 2 == 0);
205
206 stream->parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
207 [=](sycl::nd_item<3> item_ct1) {
208 k_get_rows<qk, qr, dq>(
209 src0_dd, src1_dd, dst_dd, ne00, ne12, s1, s2,
210 s3, nb01, nb02, nb03, s10, s11, s12, item_ct1);
211 });
212
213 GGML_UNUSED(dst);
214 GGML_UNUSED(ctx);
215}
216
217template <int qk, int qr, dequantize_kernel_t_reorder dq>
218static void get_rows_sycl_reorder(const ggml_tensor *src0, const ggml_tensor *src1,
219 ggml_tensor *dst, const void *src0_dd,
220 const int32_t *src1_dd, float *dst_dd,
221 queue_ptr stream) {
222
223 GGML_TENSOR_BINARY_OP_LOCALS
224
225 // reorder is done only on 2D weights
226 GGML_ASSERT(ne02 == 1 && ne03 == 1);
227 GGML_ASSERT(ne00 % 2 == 0);
228
229 const sycl::range<3> block_dims(1, 1, SYCL_GET_ROWS_BLOCK_SIZE);
230 const int block_num_x = (ne00 + 2*SYCL_GET_ROWS_BLOCK_SIZE - 1) / (2*SYCL_GET_ROWS_BLOCK_SIZE);
231 const sycl::range<3> block_nums(ne11 * ne12, ne10, block_num_x);
232
233 const size_t s1 = nb1 / ggml_element_size(dst);
234 const size_t s2 = nb2 / ggml_element_size(dst);
235 const size_t s3 = nb3 / ggml_element_size(dst);
236
237 const size_t s10 = nb10 / ggml_element_size(src1);
238 const size_t s11 = nb11 / ggml_element_size(src1);
239 const size_t s12 = nb12 / ggml_element_size(src1);
240
241 const void * src0_d = (const char *) src0_dd + ne00*ne01/qr;
242
243 stream->parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
244 [=](sycl::nd_item<3> item_ct1) {
245 k_get_rows_reorder<qk, qr, dq>(
246 src0_dd, src0_d, src1_dd, dst_dd, ne00, ne12, s1, s2,
247 s3, s10, s11, s12, item_ct1);
248 });
249
250 GGML_UNUSED(src0);
251}
252
253// K-quants in reorder layout: one work-group dequantizes one QK_K block of one row
254template <int dims, int nthreads, int nlocal, typename F>
255static void get_rows_sycl_reorder_k(const ggml_tensor *src0, const ggml_tensor *src1,
256 ggml_tensor *dst, const int32_t *src1_dd, float *dst_dd,
257 queue_ptr stream, F dequantize_block) {
258
259 GGML_TENSOR_BINARY_OP_LOCALS
260
261 // reorder is done only on 2D weights
262 GGML_ASSERT(ne02 == 1 && ne03 == 1);
263 GGML_ASSERT(ne00 % QK_K == 0);
264
265 const int64_t nblk = ne00 / QK_K;
266 const int64_t nb = nblk * ne01; // blocks in the whole tensor
267 const int64_t ngroups = ne10 * ne11 * ne12 * nblk;
268
269 const size_t s1 = nb1 / ggml_element_size(dst);
270 const size_t s2 = nb2 / ggml_element_size(dst);
271 const size_t s3 = nb3 / ggml_element_size(dst);
272
273 const size_t s10 = nb10 / ggml_element_size(src1);
274 const size_t s11 = nb11 / ggml_element_size(src1);
275 const size_t s12 = nb12 / ggml_element_size(src1);
276
277 stream->submit([&](sycl::handler & cgh) {
278 sycl::local_accessor<uint8_t, 1> scales_local(sycl::range<1>(nlocal), cgh);
279
280 auto kernel = [=](sycl::nd_item<dims> item_ct1) {
281 const int64_t g = item_ct1.get_group(dims - 1);
282 const int64_t b = g % nblk;
283 const int64_t r = g / nblk;
284 const int64_t i10 = r % ne10;
285 const int64_t i11 = (r / ne10) % ne11;
286 const int64_t i12 = r / (ne10 * ne11);
287
288 const int64_t i01 = src1_dd[i10*s10 + i11*s11 + i12*s12];
289
290 float * y = dst_dd + i10*s1 + i11*s2 + i12*s3 + b*QK_K;
291 dequantize_block(y, i01*nblk + b, get_pointer(scales_local), item_ct1, nb);
292 };
293
294 if constexpr (dims == 1) {
295 cgh.parallel_for(sycl::nd_range<1>(sycl::range<1>(ngroups * nthreads), sycl::range<1>(nthreads)), kernel);
296 } else {
297 cgh.parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, ngroups * nthreads), sycl::range<3>(1, 1, nthreads)), kernel);
298 }
299 });
300
301 GGML_UNUSED(src0);
302}
303
304// IQ3_S / IQ3_XXS in the persistent reorder layout: one work-group of 32 threads
305// dequantizes one QK_K block of one requested row, reusing the reorder-aware
306// block dequantizers that the dequantize-based mul_mat path already uses.
307template <typename F>
308static void get_rows_sycl_reorder_iq3(ggml_backend_sycl_context & ctx, const ggml_tensor * src0,
309 const ggml_tensor * src1, ggml_tensor * dst,
310 const int32_t * src1_dd, float * dst_dd,
311 F dequantize_block) {
312 GGML_TENSOR_BINARY_OP_LOCALS
313
314 // the persistent reorder is only installed for 2D weights
315 GGML_ASSERT(ne02 == 1 && ne03 == 1);
316 GGML_ASSERT(ne00 % QK_K == 0);
317
318 const int64_t nblk = ne00 / QK_K;
319 const int64_t nb = nblk * ne01; // blocks in the whole tensor
320 const int64_t ngroups = ne10 * ne11 * ne12 * nblk;
321
322 const size_t s1 = nb1 / ggml_element_size(dst);
323 const size_t s2 = nb2 / ggml_element_size(dst);
324 const size_t s3 = nb3 / ggml_element_size(dst);
325
326 const size_t s10 = nb10 / ggml_element_size(src1);
327 const size_t s11 = nb11 / ggml_element_size(src1);
328 const size_t s12 = nb12 / ggml_element_size(src1);
329
330 queue_ptr stream = ctx.stream();
331 stream->parallel_for(
332 sycl::nd_range<3>(sycl::range<3>(1, 1, ngroups * 32), sycl::range<3>(1, 1, 32)),
333 [=](sycl::nd_item<3> item_ct1) {
334 const int64_t g = item_ct1.get_group(2);
335 const int64_t b = g % nblk;
336 const int64_t r = g / nblk;
337 const int64_t i10 = r % ne10;
338 const int64_t i11 = (r / ne10) % ne11;
339 const int64_t i12 = r / (ne10 * ne11);
340
341 const int64_t i01 = src1_dd[i10*s10 + i11*s11 + i12*s12];
342
343 float * y = dst_dd + i10*s1 + i11*s2 + i12*s3 + b*QK_K;
344 dequantize_block(y, i01*nblk + b, item_ct1, nb);
345 });
346
347 GGML_UNUSED(dst);
348}
349
350template <int qk, int qr, dequantize_kernel_f32_t dq>
351static void get_rows_sycl_f32(ggml_backend_sycl_context & ctx, const ggml_tensor *src0, const ggml_tensor *src1,
352 ggml_tensor *dst, const void *src0_dd,
353 const int32_t *src1_dd, float *dst_dd,
354 queue_ptr stream) {
355
356 GGML_TENSOR_BINARY_OP_LOCALS
357
358 const sycl::range<3> block_dims(1, 1, SYCL_GET_ROWS_BLOCK_SIZE);
359 const int block_num_x = (ne00 + 2*SYCL_GET_ROWS_BLOCK_SIZE - 1) / (2*SYCL_GET_ROWS_BLOCK_SIZE);
360 const sycl::range<3> block_nums(ne11 * ne12, ne10, block_num_x);
361
362 const size_t s1 = nb1 / ggml_element_size(dst);
363 const size_t s2 = nb2 / ggml_element_size(dst);
364 const size_t s3 = nb3 / ggml_element_size(dst);
365
366 const size_t s10 = nb10 / ggml_element_size(src1);
367 const size_t s11 = nb11 / ggml_element_size(src1);
368 const size_t s12 = nb12 / ggml_element_size(src1);
369
370 GGML_ASSERT(ne00 % 2 == 0);
371
372 stream->parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
373 [=](sycl::nd_item<3> item_ct1) {
374 k_get_rows_f32<qk, qr, dq>(
375 src0_dd, src1_dd, dst_dd, ne00, ne12, s1, s2,
376 s3, nb01, nb02, nb03, s10, s11, s12, item_ct1);
377 });
378
379 GGML_UNUSED(dst);
380 GGML_UNUSED(ctx);
381}
382
383template <typename src0_t, typename dst_t>
384static void get_rows_sycl_float(ggml_backend_sycl_context & ctx, const ggml_tensor *src0,
385 const ggml_tensor *src1, ggml_tensor *dst,
386 const src0_t *src0_dd, const int32_t *src1_dd,
387 dst_t *dst_dd, queue_ptr stream) {
388
389 GGML_TENSOR_BINARY_OP_LOCALS
390
391 const sycl::range<3> block_dims(1, 1, SYCL_GET_ROWS_BLOCK_SIZE);
392 const int block_num_x = (ne00 + SYCL_GET_ROWS_BLOCK_SIZE - 1) / SYCL_GET_ROWS_BLOCK_SIZE;
393 const sycl::range<3> block_nums(ne11 * ne12, ne10, block_num_x);
394
395 // strides in elements
396 //const size_t s0 = nb0 / ggml_element_size(dst);
397 const size_t s1 = nb1 / ggml_element_size(dst);
398 const size_t s2 = nb2 / ggml_element_size(dst);
399 const size_t s3 = nb3 / ggml_element_size(dst);
400
401 const size_t s10 = nb10 / ggml_element_size(src1);
402 const size_t s11 = nb11 / ggml_element_size(src1);
403 const size_t s12 = nb12 / ggml_element_size(src1);
404 //const size_t s13 = nb13 / ggml_element_size(src1);
405
406 {
407 dpct::has_capability_or_fail(stream->get_device(),
408 {sycl::aspect::fp16});
409
410 stream->parallel_for(
411 sycl::nd_range<3>(block_nums * block_dims, block_dims),
412 [=](sycl::nd_item<3> item_ct1) {
413 k_get_rows_float(src0_dd, src1_dd, dst_dd, ne00, ne12, s1, s2,
414 s3, nb01, nb02, nb03, s10, s11, s12, item_ct1);
415 });
416 }
417
418 GGML_UNUSED(dst);
419 GGML_UNUSED(ctx);
420}
421
422template <typename src0_t>
423static void k_get_rows_back_float(const src0_t * src0, const int32_t * src1, float * dst,
424 const int64_t ncols, const int64_t nrows_grad_10, const int64_t nrows_grad_11, const int64_t nrows_dst,
425 const size_t s01, const size_t s02,
426 const size_t s10, const size_t s11,
427 const size_t s1,
428 const int64_t block_num_y,
429 const sycl::nd_item<3> & item_ct1) {
430 const int64_t col = item_ct1.get_group(2) * item_ct1.get_local_range(2) + item_ct1.get_local_id(2);
431 if (col >= ncols) {
432 return;
433 }
434
435 // block_num_y is clamped, so stride over destination rows like CUDA k_get_rows_back_float
436 for (int64_t dst_row = item_ct1.get_group(1); dst_row < nrows_dst; dst_row += block_num_y) {
437 float sum = 0.0f;
438
439 const int64_t nrows_grad_total = nrows_grad_10 * nrows_grad_11;
440 for (int64_t i = 0; i < nrows_grad_total; ++i) {
441 const int64_t i10 = i % nrows_grad_10;
442 const int64_t i11 = i / nrows_grad_10;
443 if (src1[i10*s10 + i11*s11] != dst_row) {
444 continue;
445 }
446 sum += (float) src0[col + i10*s01 + i11*s02];
447 }
448
449 dst[col + dst_row*s1] = sum;
450 }
451}
452
453template <typename src0_t>
454static void get_rows_back_sycl_float(ggml_backend_sycl_context & ctx, const ggml_tensor * src0,
455 const ggml_tensor * src1, ggml_tensor * dst,
456 const src0_t * src0_dd, const int32_t * src1_dd,
457 float * dst_dd, queue_ptr stream) {
458
459 GGML_TENSOR_BINARY_OP_LOCALS
460
461 GGML_ASSERT(ne02*ne03 == 1);
462 GGML_ASSERT(ne12*ne13 == 1);
463 GGML_ASSERT(ne2*ne3 == 1);
464 GGML_ASSERT(src0->nb[0] == ggml_type_size(src0->type));
465 GGML_ASSERT(src1->nb[0] == ggml_type_size(src1->type));
466 GGML_ASSERT(dst->nb[0] == ggml_type_size(dst->type));
467
468 const int64_t ncols = ne00;
469 const int64_t nrows_grad_10 = ne10;
470 const int64_t nrows_grad_11 = ne11;
471 const int64_t nrows_dst = ne1;
472
473 const size_t s01 = nb01 / sizeof(src0_t);
474 const size_t s02 = nb02 / sizeof(src0_t);
475
476 const size_t s10 = nb10 / sizeof(int32_t);
477 const size_t s11 = nb11 / sizeof(int32_t);
478
479 const size_t s1 = nb1 / sizeof(float);
480
481 const sycl::range<3> block_dims(1, 1, SYCL_GET_ROWS_BLOCK_SIZE);
482 const int64_t block_num_x = (ncols + SYCL_GET_ROWS_BLOCK_SIZE - 1) / SYCL_GET_ROWS_BLOCK_SIZE;
483 const int64_t block_num_y = std::min<int64_t>(nrows_dst, (int64_t) UINT16_MAX);
484 const sycl::range<3> block_nums(1, block_num_y, block_num_x);
485
486 stream->parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
487 [=](sycl::nd_item<3> item_ct1) {
488 k_get_rows_back_float(src0_dd, src1_dd, dst_dd,
489 ncols, nrows_grad_10, nrows_grad_11, nrows_dst,
490 s01, s02, s10, s11, s1,
491 block_num_y, item_ct1);
492 });
493
494 GGML_UNUSED(src0);
495 GGML_UNUSED(src1);
496 GGML_UNUSED(dst);
497 GGML_UNUSED(ctx);
498}
499
500void ggml_sycl_op_get_rows(ggml_backend_sycl_context & ctx, ggml_tensor * dst) {
501 GGML_ASSERT(dst->src[1]->type == GGML_TYPE_I32);
502 GGML_ASSERT(dst->type == GGML_TYPE_F32 || dst->type == GGML_TYPE_I32 );
503
504 GGML_ASSERT(dst->src[0]->nb[0] == ggml_type_size(dst->src[0]->type));
505 GGML_ASSERT(dst->src[1]->nb[0] == ggml_type_size(dst->src[1]->type));
506 GGML_ASSERT(dst->nb[0] == ggml_type_size(dst->type));
507
508 const int32_t * src1_i32 = (const int32_t *) dst->src[1]->data;
509 // a weight shared with MUL_MAT (e.g. tied embeddings) can be in the
510 // persistent reorder layout (incl. IQ3_S / IQ3_XXS since the SYCL IQ3
511 // reorder work)
512 const bool src0_reordered = dst->src[0]->extra &&
513 ((const ggml_tensor_extra_gpu *) dst->src[0]->extra)->optimized_feature.reorder;
514 /* TODO: Refactor and remove duplicates */
515 switch (dst->src[0]->type) {
516 case GGML_TYPE_F16:
517 get_rows_sycl_float(ctx, dst->src[0], dst->src[1], dst, (const sycl::half *)dst->src[0]->data,
518 src1_i32, (float *)dst->data, ctx.stream());
519 break;
520 case GGML_TYPE_BF16:
521 get_rows_sycl_float(ctx, dst->src[0], dst->src[1], dst, (const sycl::ext::oneapi::bfloat16 *)dst->src[0]->data,
522 src1_i32, (float *)dst->data, ctx.stream());
523 break;
524 case GGML_TYPE_F32:
525 get_rows_sycl_float(ctx, dst->src[0], dst->src[1], dst, (const float *)dst->src[0]->data,
526 src1_i32, (float *)dst->data, ctx.stream());
527 break;
528 case GGML_TYPE_I32:
529 get_rows_sycl_float(ctx, dst->src[0], dst->src[1], dst, (const int32_t *)dst->src[0]->data,
530 src1_i32, (int32_t *)dst->data, ctx.stream());
531 break;
532 case GGML_TYPE_Q1_0:
533 get_rows_sycl<QK1_0, 1, dequantize_q1_0>(ctx, dst->src[0], dst->src[1], dst, (const float *)dst->src[0]->data,
534 src1_i32, (float *)dst->data, ctx.stream());
535 break;
536 case GGML_TYPE_MXFP4:
537 get_rows_sycl<QK_MXFP4, 2, dequantize_mxfp4>(ctx, dst->src[0], dst->src[1], dst, (const float *)dst->src[0]->data,
538 src1_i32, (float *)dst->data, ctx.stream());
539 break;
540 case GGML_TYPE_NVFP4:
541 get_rows_sycl<QK_NVFP4, 1, dequantize_nvfp4>(ctx, dst->src[0], dst->src[1], dst, (const float *)dst->src[0]->data,
542 src1_i32, (float *)dst->data, ctx.stream());
543 break;
544 case GGML_TYPE_IQ2_XXS:
545 get_rows_sycl<QK_K, 1, dequantize_iq2_xxs>(ctx, dst->src[0], dst->src[1], dst, (const float *)dst->src[0]->data,
546 src1_i32, (float *)dst->data, ctx.stream());
547 break;
548 case GGML_TYPE_IQ2_XS:
549 get_rows_sycl<QK_K, 1, dequantize_iq2_xs>(ctx, dst->src[0], dst->src[1], dst, (const float *)dst->src[0]->data,
550 src1_i32, (float *)dst->data, ctx.stream());
551 break;
552 case GGML_TYPE_IQ2_S:
553 get_rows_sycl<QK_K, 1, dequantize_iq2_s>(ctx, dst->src[0], dst->src[1], dst, (const float *)dst->src[0]->data,
554 src1_i32, (float *)dst->data, ctx.stream());
555 break;
556 case GGML_TYPE_IQ3_XXS:
557 if (src0_reordered) {
558 const void * vx = dst->src[0]->data;
559 get_rows_sycl_reorder_iq3(ctx, dst->src[0], dst->src[1], dst, src1_i32, (float *) dst->data,
560 [=](float * y, int64_t iblk, const sycl::nd_item<3> & item_ct1, int64_t nb) {
561 dequantize_block_iq3_xxs_reorder(vx, y, iblk, item_ct1, nb,
562 iq3xxs_grid, ksigns_iq2xs, kmask_iq2xs);
563 });
564 break;
565 }
566 get_rows_sycl<QK_K, 1, dequantize_iq3_xxs>(ctx, dst->src[0], dst->src[1], dst, (const float *)dst->src[0]->data,
567 src1_i32, (float *)dst->data, ctx.stream());
568 break;
569 case GGML_TYPE_IQ1_S:
570 get_rows_sycl<QK_K, 1, dequantize_iq1_s>(ctx, dst->src[0], dst->src[1], dst, (const float *)dst->src[0]->data,
571 src1_i32, (float *)dst->data, ctx.stream());
572 break;
573 case GGML_TYPE_IQ1_M:
574 get_rows_sycl<QK_K, 1, dequantize_iq1_m>(ctx, dst->src[0], dst->src[1], dst, (const float *)dst->src[0]->data,
575 src1_i32, (float *)dst->data, ctx.stream());
576 break;
577 case GGML_TYPE_IQ3_S:
578 if (src0_reordered) {
579 const void * vx = dst->src[0]->data;
580 get_rows_sycl_reorder_iq3(ctx, dst->src[0], dst->src[1], dst, src1_i32, (float *) dst->data,
581 [=](float * y, int64_t iblk, const sycl::nd_item<3> & item_ct1, int64_t nb) {
582 dequantize_block_iq3_s_reorder(vx, y, iblk, item_ct1, nb,
583 kmask_iq2xs, iq3s_grid);
584 });
585 break;
586 }
587 get_rows_sycl<QK_K, 1, dequantize_iq3_s>(ctx, dst->src[0], dst->src[1], dst, (const float *)dst->src[0]->data,
588 src1_i32, (float *)dst->data, ctx.stream());
589 break;
590 case GGML_TYPE_IQ4_NL:
591 get_rows_sycl<QK4_NL, 1, dequantize_iq4_nl>(ctx, dst->src[0], dst->src[1], dst, (const float *)dst->src[0]->data,
592 src1_i32, (float *)dst->data, ctx.stream());
593 break;
594 case GGML_TYPE_IQ4_XS:
595 get_rows_sycl<QK_K, 1, dequantize_iq4_xs>(ctx, dst->src[0], dst->src[1], dst, (const float *)dst->src[0]->data,
596 src1_i32, (float *)dst->data, ctx.stream());
597 break;
598 case GGML_TYPE_Q2_K:
599 if (src0_reordered) {
600 const void * vx = dst->src[0]->data;
601 get_rows_sycl_reorder_k<3, 64, 1>(dst->src[0], dst->src[1], dst, src1_i32, (float *)dst->data, ctx.stream(),
602 [=](float * y, int64_t ib, uint8_t * scales_local, const sycl::nd_item<3> & item_ct1, int64_t nb) {
603 dequantize_block_q2_K_reorder(vx, y, ib, item_ct1, nb);
604 GGML_UNUSED(scales_local);
605 });
606 break;
607 }
608 get_rows_sycl_f32<QK_K, 1, dequantize_q2_K_f32>(ctx, dst->src[0], dst->src[1], dst, (const float *)dst->src[0]->data,
609 src1_i32, (float *)dst->data, ctx.stream());
610 break;
611 case GGML_TYPE_Q3_K:
612 if (src0_reordered) {
613 const void * vx = dst->src[0]->data;
614 get_rows_sycl_reorder_k<3, 64, 1>(dst->src[0], dst->src[1], dst, src1_i32, (float *)dst->data, ctx.stream(),
615 [=](float * y, int64_t ib, uint8_t * scales_local, const sycl::nd_item<3> & item_ct1, int64_t nb) {
616 dequantize_block_q3_K_reorder(vx, y, ib, item_ct1, nb);
617 GGML_UNUSED(scales_local);
618 });
619 break;
620 }
621 get_rows_sycl<QK_K, 1, dequantize_q3_K>(ctx, dst->src[0], dst->src[1], dst, (const float *)dst->src[0]->data,
622 src1_i32, (float *)dst->data, ctx.stream());
623 break;
624 case GGML_TYPE_Q4_0:
625 if (src0_reordered) {
626 get_rows_sycl_reorder<QK4_0, QR4_0, dequantize_q4_0_reorder>(dst->src[0], dst->src[1], dst, dst->src[0]->data,
627 src1_i32, (float *)dst->data, ctx.stream());
628 break;
629 }
630 get_rows_sycl<QK4_0, QR4_0, dequantize_q4_0>(ctx, dst->src[0], dst->src[1], dst, (const float *)dst->src[0]->data,
631 src1_i32, (float *)dst->data, ctx.stream());
632 break;
633 case GGML_TYPE_Q4_1:
634 get_rows_sycl<QK4_1, QR4_1, dequantize_q4_1>(ctx, dst->src[0], dst->src[1], dst, (const float *)dst->src[0]->data,
635 src1_i32, (float *)dst->data, ctx.stream());
636 break;
637 case GGML_TYPE_Q4_K:
638 if (src0_reordered) {
639 const void * vx = dst->src[0]->data;
640 get_rows_sycl_reorder_k<1, 32, K_SCALE_SIZE>(dst->src[0], dst->src[1], dst, src1_i32, (float *)dst->data, ctx.stream(),
641 [=](float * y, int64_t ib, uint8_t * scales_local, const sycl::nd_item<1> & item_ct1, int64_t nb) {
642 dequantize_block_q4_K_reorder(vx, y, ib, scales_local, item_ct1, nb);
643 });
644 break;
645 }
646 get_rows_sycl_f32<QK_K, 1, dequantize_q4_K_f32>(ctx, dst->src[0], dst->src[1], dst, (const float *)dst->src[0]->data,
647 src1_i32, (float *)dst->data, ctx.stream());
648 break;
649 case GGML_TYPE_Q5_0:
650 get_rows_sycl<QK5_0, QR5_0, dequantize_q5_0>(ctx, dst->src[0], dst->src[1], dst, (const float *)dst->src[0]->data,
651 src1_i32, (float *)dst->data, ctx.stream());
652 break;
653 case GGML_TYPE_Q5_1:
654 get_rows_sycl<QK5_1, QR5_1, dequantize_q5_1>(ctx, dst->src[0], dst->src[1], dst, (const float *)dst->src[0]->data,
655 src1_i32, (float *)dst->data, ctx.stream());
656 break;
657 case GGML_TYPE_Q5_K:
658 if (src0_reordered) {
659 const void * vx = dst->src[0]->data;
660 get_rows_sycl_reorder_k<3, 32, K_SCALE_SIZE>(dst->src[0], dst->src[1], dst, src1_i32, (float *)dst->data, ctx.stream(),
661 [=](float * y, int64_t ib, uint8_t * scales_local, const sycl::nd_item<3> & item_ct1, int64_t nb) {
662 dequantize_block_q5_K_reorder(vx, y, ib, scales_local, item_ct1, nb);
663 });
664 break;
665 }
666 get_rows_sycl_f32<QK_K, 1, dequantize_q5_K_f32>(ctx, dst->src[0], dst->src[1], dst, (const float *)dst->src[0]->data,
667 src1_i32, (float *)dst->data, ctx.stream());
668 break;
669 case GGML_TYPE_Q6_K:
670 if (src0_reordered) {
671 const void * vx = dst->src[0]->data;
672 get_rows_sycl_reorder_k<3, 64, 1>(dst->src[0], dst->src[1], dst, src1_i32, (float *)dst->data, ctx.stream(),
673 [=](float * y, int64_t ib, uint8_t * scales_local, const sycl::nd_item<3> & item_ct1, int64_t nb) {
674 dequantize_block_q6_K_reorder(vx, y, ib, item_ct1, nb);
675 GGML_UNUSED(scales_local);
676 });
677 break;
678 }
679 get_rows_sycl<QK_K, 1, dequantize_q6_K>(ctx, dst->src[0], dst->src[1], dst, (const float *)dst->src[0]->data,
680 src1_i32, (float *)dst->data, ctx.stream());
681 break;
682 case GGML_TYPE_Q8_0:
683 if (src0_reordered) {
684 get_rows_sycl_reorder<QK8_0, QR8_0, dequantize_q8_0_reorder>(dst->src[0], dst->src[1], dst, dst->src[0]->data,
685 src1_i32, (float *)dst->data, ctx.stream());
686 break;
687 }
688 get_rows_sycl<QK8_0, QR8_0, dequantize_q8_0>(ctx, dst->src[0], dst->src[1], dst, (const float *)dst->src[0]->data,
689 src1_i32, (float *)dst->data, ctx.stream());
690 break;
691 default:
692 // TODO: k-quants
693 GGML_LOG_ERROR("%s: unsupported type: %s\n", __func__, ggml_type_name(dst->src[0]->type));
694 GGML_ABORT("fatal error");
695 }
696}
697
698void ggml_sycl_op_get_rows_back(ggml_backend_sycl_context & ctx, ggml_tensor * dst) {
699 const ggml_tensor * src0 = dst->src[0];
700 const ggml_tensor * src1 = dst->src[1];
701
702 GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16);
703 GGML_ASSERT(src1->type == GGML_TYPE_I32);
704 GGML_ASSERT(dst->type == GGML_TYPE_F32);
705
706 GGML_ASSERT(ggml_is_contiguous(dst));
707
708 switch (src0->type) {
709 case GGML_TYPE_F16:
710 get_rows_back_sycl_float(ctx, src0, src1, dst, (const sycl::half *) src0->data,
711 (const int32_t *) src1->data, (float *) dst->data,
712 ctx.stream());
713 break;
714 case GGML_TYPE_F32:
715 get_rows_back_sycl_float(ctx, src0, src1, dst, (const float *) src0->data,
716 (const int32_t *) src1->data, (float *) dst->data,
717 ctx.stream());
718 break;
719 default:
720 GGML_ABORT("%s: unsupported src0 type: %s\n", __func__, ggml_type_name(src0->type));
721 }
722}