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-cuda / getrows.cu

488 lines · 23077 bytes Raw Blame History
1#include "getrows.cuh"
2#include "dequantize.cuh"
3#include "convert.cuh"
4
5template<int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
6static __global__ void k_get_rows(
7 const void * __restrict__ src0, const int32_t * __restrict__ src1, dst_t * __restrict__ dst,
8 const int64_t ne00, /*const int64_t ne01, const int64_t ne02, const int64_t ne03,*/
9 /*const int64_t ne10,*/ const int64_t ne11, const uint3 ne12_fdv, /*const int64_t ne13,*/
10 /*const size_t s0,*/ const size_t s1, const size_t s2, const size_t s3,
11 /*const size_t nb00,*/ const size_t nb01, const size_t nb02, const size_t nb03,
12 const size_t s10, const size_t s11, const size_t s12/*, const size_t s13*/) {
13
14 ggml_cuda_pdl_sync();
15 for (int64_t z = blockIdx.z; z < ne11*(int64_t)ne12_fdv.z; z += gridDim.z) {
16 for (int64_t i00 = 2*(blockIdx.y*blockDim.x + threadIdx.x); i00 < ne00; i00 += gridDim.y*blockDim.x) {
17 // The x and y dimensions of the grid are swapped because the maximum allowed grid size for x is higher.
18 const int i10 = blockIdx.x;
19 const uint2 dm = fast_div_modulo((uint32_t)z, ne12_fdv);
20 const int i11 = dm.x;
21 const int i12 = dm.y;
22
23 const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
24
25 dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
26 const void * src0_row = (const char *) src0 + i01*nb01 + i11*nb02 + i12*nb03;
27
28 const int ib = i00/qk; // block index
29 const int iqs = (i00%qk)/qr; // quant index
30 const int iybs = i00 - i00%qk; // dst block start index
31 const int y_offset = qr == 1 ? 1 : qk/2;
32
33 // dequantize
34 float2 v;
35 dequantize_kernel(src0_row, ib, iqs, v);
36
37 dst_row[iybs + iqs + 0] = ggml_cuda_cast<dst_t>(v.x);
38 dst_row[iybs + iqs + y_offset] = ggml_cuda_cast<dst_t>(v.y);
39 }
40 }
41}
42
43template<typename dst_t, dequantize_kq_t<dst_t> dequantize_kq>
44static __global__ void k_get_rows_kq(
45 const void * __restrict__ src0, const int32_t * __restrict__ src1, dst_t * __restrict__ dst,
46 const int64_t ne00, /*const int64_t ne01, const int64_t ne02, const int64_t ne03,*/
47 /*const int64_t ne10,*/ const int64_t ne11, const uint3 ne12_fdv, /*const int64_t ne13,*/
48 /*const size_t s0,*/ const size_t s1, const size_t s2, const size_t s3,
49 /*const size_t nb00,*/ const size_t nb01, const size_t nb02, const size_t nb03,
50 const size_t s10, const size_t s11, const size_t s12/*, const size_t s13*/) {
51
52 ggml_cuda_pdl_sync();
53 const int64_t nsb = ne00/QK_K; // super-blocks per row
54 for (int64_t z = blockIdx.z; z < ne11*(int64_t)ne12_fdv.z; z += gridDim.z) {
55 // The x and y dimensions of the grid are swapped because the maximum allowed grid size for x is higher.
56 const int i10 = blockIdx.x;
57 const uint2 dm = fast_div_modulo((uint32_t)z, ne12_fdv);
58 const int i11 = dm.x;
59 const int i12 = dm.y;
60
61 const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
62
63 dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
64 const void * src0_row = (const char *) src0 + i01*nb01 + i11*nb02 + i12*nb03;
65
66 for (int64_t ib = blockIdx.y; ib < nsb; ib += gridDim.y) {
67 dequantize_kq(src0_row, ib, dst_row + ib*QK_K, threadIdx.x);
68 }
69 }
70}
71
72template<typename src0_t, typename dst_t>
73static __global__ void k_get_rows_float(
74 const src0_t * src0_ptr, const int32_t * src1_ptr, dst_t * dst_ptr,
75 const int64_t ne00, /*const int64_t ne01, const int64_t ne02, const int64_t ne03,*/
76 /*const int64_t ne10,*/ const int64_t ne11, const uint3 ne12_fdv, /*const int64_t ne13,*/
77 /*const size_t s0,*/ const size_t s1, const size_t s2, const size_t s3,
78 /*const size_t nb00,*/ const size_t nb01, const size_t nb02, const size_t nb03,
79 const size_t s10, const size_t s11, const size_t s12/*, const size_t s13*/) {
80
81 ggml_cuda_pdl_lc();
82 const src0_t * GGML_CUDA_RESTRICT src0 = src0_ptr;
83 const int32_t * GGML_CUDA_RESTRICT src1 = src1_ptr;
84 dst_t * GGML_CUDA_RESTRICT dst = dst_ptr;
85 ggml_cuda_pdl_sync();
86 for (int64_t z = blockIdx.z; z < ne11*(int64_t)ne12_fdv.z; z += gridDim.z) {
87 // The x and y dimensions of the grid are swapped because the maximum allowed grid size for x is higher.
88 const int i10 = blockIdx.x;
89 const uint2 dm = fast_div_modulo((uint32_t)z, ne12_fdv);
90 const int i11 = dm.x;
91 const int i12 = dm.y;
92
93 const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
94
95 dst_t * GGML_CUDA_RESTRICT dst_row = dst + i10*s1 + i11*s2 + i12*s3;
96 const src0_t * GGML_CUDA_RESTRICT src0_row = (const src0_t *)((const char *) src0 + i01*nb01 + i11*nb02 + i12*nb03);
97
98 for (int64_t i00 = blockIdx.y*blockDim.x + threadIdx.x; i00 < ne00; i00 += gridDim.y*blockDim.x) {
99 dst_row[i00] = ggml_cuda_cast<dst_t>(src0_row[i00]);
100 }
101 }
102}
103
104template<typename dst_t>
105static __global__ void k_get_rows_float_vec(
106 const dst_t * src0_ptr, const int32_t * src1_ptr, dst_t * dst_ptr,
107 const int64_t ne00v,
108 const int64_t ne11, const uint3 ne12_fdv,
109 const size_t s1, const size_t s2, const size_t s3,
110 const size_t nb01, const size_t nb02, const size_t nb03,
111 const size_t s10, const size_t s11, const size_t s12) {
112
113 ggml_cuda_pdl_lc();
114 ggml_cuda_pdl_sync();
115 for (int64_t z = blockIdx.z; z < ne11*(int64_t)ne12_fdv.z; z += gridDim.z) {
116 const int i10 = blockIdx.x;
117 const uint2 dm = fast_div_modulo((uint32_t)z, ne12_fdv);
118 const int i11 = dm.x;
119 const int i12 = dm.y;
120
121 const int i01 = src1_ptr[i10*s10 + i11*s11 + i12*s12];
122
123 int4 * GGML_CUDA_RESTRICT dst_row = (int4 *) (dst_ptr + i10*s1 + i11*s2 + i12*s3);
124 const int4 * GGML_CUDA_RESTRICT src0_row = (const int4 *)((const char *) src0_ptr + i01*nb01 + i11*nb02 + i12*nb03);
125
126 for (int64_t i = blockIdx.y*blockDim.x + threadIdx.x; i < ne00v; i += gridDim.y*blockDim.x) {
127 dst_row[i] = src0_row[i];
128 }
129 }
130}
131
132template<typename grad_t, typename dst_t>
133static __global__ void k_get_rows_back_float(
134 const grad_t * __restrict__ grad, const int32_t * __restrict__ rows, dst_t * __restrict__ dst,
135 const int64_t ncols, const int64_t nrows_grad, const int64_t nrows_dst) {
136 const int col = blockIdx.x*blockDim.x + threadIdx.x;
137
138 if (col >= ncols) {
139 return;
140 }
141
142 ggml_cuda_pdl_sync();
143
144 // grid.y is clamped to the CUDA grid limit, so stride over the destination rows
145 for (int64_t dst_row = blockIdx.y; dst_row < nrows_dst; dst_row += gridDim.y) {
146 float sum = 0.0f;
147
148 for (int64_t i = 0; i < nrows_grad; ++i) {
149 if (rows[i] != dst_row) {
150 continue;
151 }
152 sum += grad[i*ncols + col];
153 }
154
155 dst[dst_row*ncols + col] = sum;
156 }
157}
158
159template<int qk, int qr, dequantize_kernel_t dq, typename dst_t>
160static void get_rows_cuda_q(
161 const void * src0_d, const int32_t * src1_d, dst_t * dst_d,
162 const int64_t ne00, const size_t nb01, const size_t nb02, const size_t nb03,
163 const int64_t ne10, const int64_t ne11, const int64_t ne12, const size_t nb10, const size_t nb11, const size_t nb12,
164 const size_t nb1, const size_t nb2, const size_t nb3,
165 cudaStream_t stream) {
166 const dim3 block_dims(CUDA_GET_ROWS_BLOCK_SIZE, 1, 1);
167 const int block_num_y = (ne00 + 2*CUDA_GET_ROWS_BLOCK_SIZE - 1) / (2*CUDA_GET_ROWS_BLOCK_SIZE);
168 const dim3 block_nums(ne10, MIN(block_num_y, UINT16_MAX), MIN(ne11*ne12, UINT16_MAX));
169
170 // strides in elements
171 // const size_t s0 = nb0 / sizeof(dst_t);
172 const size_t s1 = nb1 / sizeof(dst_t);
173 const size_t s2 = nb2 / sizeof(dst_t);
174 const size_t s3 = nb3 / sizeof(dst_t);
175
176 const size_t s10 = nb10 / sizeof(int32_t);
177 const size_t s11 = nb11 / sizeof(int32_t);
178 const size_t s12 = nb12 / sizeof(int32_t);
179 // const size_t s13 = nb13 / sizeof(int32_t);
180
181 GGML_ASSERT(ne00 % 2 == 0);
182
183 GGML_ASSERT(ne12 > 0);
184 GGML_ASSERT(ne11 <= std::numeric_limits<uint32_t>::max() / ne12);
185 const uint3 ne12_fdv = init_fastdiv_values(ne12);
186
187 k_get_rows<qk, qr, dq><<<block_nums, block_dims, 0, stream>>>(
188 src0_d, src1_d, dst_d,
189 ne00, /*ne01, ne02, ne03,*/
190 /*ne10,*/ ne11, ne12_fdv, /*ne13,*/
191 /* s0,*/ s1, s2, s3,
192 /* nb00,*/ nb01, nb02, nb03,
193 s10, s11, s12/*, s13*/);
194}
195
196template<int block_dim, typename dst_t, dequantize_kq_t<dst_t> dequantize_kq>
197static void get_rows_cuda_kq(
198 const void * src0_d, const int32_t * src1_d, dst_t * dst_d,
199 const int64_t ne00, const size_t nb01, const size_t nb02, const size_t nb03,
200 const int64_t ne10, const int64_t ne11, const int64_t ne12, const size_t nb10, const size_t nb11, const size_t nb12,
201 const size_t nb1, const size_t nb2, const size_t nb3,
202 cudaStream_t stream) {
203 GGML_ASSERT(ne00 % QK_K == 0);
204 const int64_t nsb = ne00/QK_K;
205
206 const dim3 block_dims(block_dim, 1, 1);
207 const dim3 block_nums(ne10, MIN(nsb, UINT16_MAX), MIN(ne11*ne12, UINT16_MAX));
208
209 // strides in elements
210 // const size_t s0 = nb0 / sizeof(dst_t);
211 const size_t s1 = nb1 / sizeof(dst_t);
212 const size_t s2 = nb2 / sizeof(dst_t);
213 const size_t s3 = nb3 / sizeof(dst_t);
214
215 const size_t s10 = nb10 / sizeof(int32_t);
216 const size_t s11 = nb11 / sizeof(int32_t);
217 const size_t s12 = nb12 / sizeof(int32_t);
218 // const size_t s13 = nb13 / sizeof(int32_t);
219
220 GGML_ASSERT(ne12 > 0);
221 GGML_ASSERT(ne11 <= std::numeric_limits<uint32_t>::max() / ne12);
222 const uint3 ne12_fdv = init_fastdiv_values(ne12);
223
224 k_get_rows_kq<dst_t, dequantize_kq><<<block_nums, block_dims, 0, stream>>>(
225 src0_d, src1_d, dst_d,
226 ne00, /*ne01, ne02, ne03,*/
227 /*ne10,*/ ne11, ne12_fdv, /*ne13,*/
228 /* s0,*/ s1, s2, s3,
229 /* nb00,*/ nb01, nb02, nb03,
230 s10, s11, s12/*, s13*/);
231}
232
233template<typename src0_t, typename dst_t>
234static void get_rows_cuda_float(
235 const src0_t * src0_d, const int32_t * src1_d, dst_t * dst_d,
236 const int64_t ne00, const size_t nb01, const size_t nb02, const size_t nb03,
237 const int64_t ne10, const int64_t ne11, const int64_t ne12, const size_t nb10, const size_t nb11, const size_t nb12,
238 const size_t nb1, const size_t nb2, const size_t nb3,
239 cudaStream_t stream) {
240 const dim3 block_dims(CUDA_GET_ROWS_BLOCK_SIZE, 1, 1);
241
242 // strides in elements
243 // const size_t s0 = nb0 / sizeof(dst_t);
244 const size_t s1 = nb1 / sizeof(dst_t);
245 const size_t s2 = nb2 / sizeof(dst_t);
246 const size_t s3 = nb3 / sizeof(dst_t);
247
248 const size_t s10 = nb10 / sizeof(int32_t);
249 const size_t s11 = nb11 / sizeof(int32_t);
250 const size_t s12 = nb12 / sizeof(int32_t);
251 // const size_t s13 = nb13 / sizeof(int32_t);
252
253 GGML_ASSERT(ne12 > 0);
254 GGML_ASSERT(ne11 <= std::numeric_limits<uint32_t>::max() / ne12);
255 const uint3 ne12_fdv = init_fastdiv_values(ne12);
256
257 if constexpr (std::is_same<src0_t, dst_t>::value) {
258 constexpr int VEC = 16 / sizeof(dst_t);
259 const int64_t ne00v = ne00 / VEC;
260 const int64_t vec_block_num_y = (ne00v + CUDA_GET_ROWS_BLOCK_SIZE - 1) / CUDA_GET_ROWS_BLOCK_SIZE;
261 const bool enough_blocks = vec_block_num_y * ne10 * ne11 * ne12 >= 128;
262 const bool can_vec = VEC > 1 && enough_blocks &&
263 (ne00 % VEC == 0) &&
264 (nb01 % 16 == 0) && (nb02 % 16 == 0) && (nb03 % 16 == 0) &&
265 (nb1 % 16 == 0) && (nb2 % 16 == 0) && (nb3 % 16 == 0) &&
266 (((uintptr_t) src0_d) % 16 == 0) && (((uintptr_t) dst_d) % 16 == 0);
267
268 if (can_vec) {
269 const int block_num_y = vec_block_num_y;
270 const dim3 block_nums(ne10, MIN(block_num_y, UINT16_MAX), MIN(ne11*ne12, UINT16_MAX));
271 const ggml_cuda_kernel_launch_params launch_params = ggml_cuda_kernel_launch_params{block_nums, block_dims, 0, stream};
272 ggml_cuda_kernel_launch(k_get_rows_float_vec<dst_t>, launch_params,
273 (const dst_t *) src0_d, src1_d, dst_d,
274 ne00v, ne11, ne12_fdv,
275 s1, s2, s3,
276 nb01, nb02, nb03,
277 s10, s11, s12);
278 return;
279 }
280 }
281
282 const int block_num_y = (ne00 + CUDA_GET_ROWS_BLOCK_SIZE - 1) / CUDA_GET_ROWS_BLOCK_SIZE;
283 const dim3 block_nums(ne10, MIN(block_num_y, UINT16_MAX), MIN(ne11*ne12, UINT16_MAX));
284
285 const ggml_cuda_kernel_launch_params launch_params = ggml_cuda_kernel_launch_params{block_nums, block_dims, 0, stream};
286 ggml_cuda_kernel_launch(k_get_rows_float<src0_t, dst_t>, launch_params,
287 src0_d, src1_d, dst_d,
288 ne00, /*ne01, ne02, ne03,*/
289 /*ne10,*/ ne11, ne12_fdv, /*ne13,*/
290 /* s0,*/ s1, s2, s3,
291 /* nb00,*/ nb01, nb02, nb03,
292 s10, s11, s12/*, s13*/);
293}
294
295template <typename dst_t>
296static void ggml_cuda_get_rows_switch_src0_type(
297 const void * src0_d, const ggml_type src0_type, const int32_t * src1_d, dst_t * dst_d,
298 const int64_t ne00, const size_t nb01, const size_t nb02, const size_t nb03,
299 const int64_t ne10, const int64_t ne11, const int64_t ne12, const size_t nb10, const size_t nb11, const size_t nb12,
300 const size_t nb1, const size_t nb2, const size_t nb3,
301 cudaStream_t stream) {
302 switch (src0_type) {
303 case GGML_TYPE_F16:
304 get_rows_cuda_float((const half *) src0_d, src1_d, dst_d,
305 ne00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb1, nb2, nb3, stream);
306 break;
307 case GGML_TYPE_F32:
308 get_rows_cuda_float((const float *) src0_d, src1_d, dst_d,
309 ne00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb1, nb2, nb3, stream);
310 break;
311 case GGML_TYPE_I32:
312 get_rows_cuda_float((const int32_t *) src0_d, src1_d, dst_d,
313 ne00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb1, nb2, nb3, stream);
314 break;
315 case GGML_TYPE_BF16:
316 get_rows_cuda_float((const nv_bfloat16 *) src0_d, src1_d, dst_d,
317 ne00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb1, nb2, nb3, stream);
318 break;
319 case GGML_TYPE_Q1_0:
320 get_rows_cuda_q<QK1_0, QR1_0, dequantize_q1_0>(src0_d, src1_d, dst_d,
321 ne00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb1, nb2, nb3, stream);
322 break;
323 case GGML_TYPE_Q2_0:
324 get_rows_cuda_q<QK2_0, QR2_0, dequantize_q2_0>(src0_d, src1_d, dst_d,
325 ne00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb1, nb2, nb3, stream);
326 break;
327 case GGML_TYPE_Q4_0:
328 get_rows_cuda_q<QK4_0, QR4_0, dequantize_q4_0>(src0_d, src1_d, dst_d,
329 ne00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb1, nb2, nb3, stream);
330 break;
331 case GGML_TYPE_Q4_1:
332 get_rows_cuda_q<QK4_1, QR4_1, dequantize_q4_1>(src0_d, src1_d, dst_d,
333 ne00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb1, nb2, nb3, stream);
334 break;
335 case GGML_TYPE_Q5_0:
336 get_rows_cuda_q<QK5_0, QR5_0, dequantize_q5_0>(src0_d, src1_d, dst_d,
337 ne00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb1, nb2, nb3, stream);
338 break;
339 case GGML_TYPE_Q5_1:
340 get_rows_cuda_q<QK5_1, QR5_1, dequantize_q5_1>(src0_d, src1_d, dst_d,
341 ne00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb1, nb2, nb3, stream);
342 break;
343 case GGML_TYPE_Q8_0:
344 get_rows_cuda_q<QK8_0, QR8_0, dequantize_q8_0>(src0_d, src1_d, dst_d,
345 ne00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb1, nb2, nb3, stream);
346 break;
347 case GGML_TYPE_Q2_K:
348 get_rows_cuda_kq<64, dst_t, dequantize_q2_K<dst_t>>(src0_d, src1_d, dst_d,
349 ne00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb1, nb2, nb3, stream);
350 break;
351 case GGML_TYPE_Q3_K:
352 get_rows_cuda_kq<64, dst_t, dequantize_q3_K<dst_t>>(src0_d, src1_d, dst_d,
353 ne00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb1, nb2, nb3, stream);
354 break;
355 case GGML_TYPE_Q4_K:
356 get_rows_cuda_kq<32, dst_t, dequantize_q4_K<dst_t>>(src0_d, src1_d, dst_d,
357 ne00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb1, nb2, nb3, stream);
358 break;
359 case GGML_TYPE_Q5_K:
360 get_rows_cuda_kq<64, dst_t, dequantize_q5_K<dst_t>>(src0_d, src1_d, dst_d,
361 ne00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb1, nb2, nb3, stream);
362 break;
363 case GGML_TYPE_Q6_K:
364 get_rows_cuda_kq<64, dst_t, dequantize_q6_K<dst_t>>(src0_d, src1_d, dst_d,
365 ne00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb1, nb2, nb3, stream);
366 break;
367 case GGML_TYPE_IQ2_XXS:
368 get_rows_cuda_kq<32, dst_t, dequantize_iq2_xxs<dst_t>>(src0_d, src1_d, dst_d,
369 ne00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb1, nb2, nb3, stream);
370 break;
371 case GGML_TYPE_IQ2_XS:
372 get_rows_cuda_kq<32, dst_t, dequantize_iq2_xs<dst_t>>(src0_d, src1_d, dst_d,
373 ne00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb1, nb2, nb3, stream);
374 break;
375 case GGML_TYPE_IQ2_S:
376 get_rows_cuda_kq<32, dst_t, dequantize_iq2_s<dst_t>>(src0_d, src1_d, dst_d,
377 ne00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb1, nb2, nb3, stream);
378 break;
379 case GGML_TYPE_IQ3_XXS:
380 get_rows_cuda_kq<32, dst_t, dequantize_iq3_xxs<dst_t>>(src0_d, src1_d, dst_d,
381 ne00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb1, nb2, nb3, stream);
382 break;
383 case GGML_TYPE_IQ3_S:
384 get_rows_cuda_kq<32, dst_t, dequantize_iq3_s<dst_t>>(src0_d, src1_d, dst_d,
385 ne00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb1, nb2, nb3, stream);
386 break;
387 case GGML_TYPE_IQ1_S:
388 get_rows_cuda_kq<32, dst_t, dequantize_iq1_s<dst_t>>(src0_d, src1_d, dst_d,
389 ne00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb1, nb2, nb3, stream);
390 break;
391 case GGML_TYPE_IQ1_M:
392 get_rows_cuda_kq<32, dst_t, dequantize_iq1_m<dst_t>>(src0_d, src1_d, dst_d,
393 ne00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb1, nb2, nb3, stream);
394 break;
395 case GGML_TYPE_IQ4_NL:
396 get_rows_cuda_kq<32, dst_t, dequantize_iq4_nl<dst_t>>(src0_d, src1_d, dst_d,
397 ne00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb1, nb2, nb3, stream);
398 break;
399 case GGML_TYPE_IQ4_XS:
400 get_rows_cuda_kq<32, dst_t, dequantize_iq4_xs<dst_t>>(src0_d, src1_d, dst_d,
401 ne00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb1, nb2, nb3, stream);
402 break;
403 case GGML_TYPE_MXFP4:
404 get_rows_cuda_kq<32, dst_t, dequantize_mxfp4<dst_t>>(src0_d, src1_d, dst_d,
405 ne00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb1, nb2, nb3, stream);
406 break;
407 default:
408 GGML_ABORT("%s: unsupported src0 type: %s\n", __func__, ggml_type_name(src0_type));
409 }
410}
411
412void get_rows_cuda(
413 const void * src0_d, ggml_type src0_type, const int32_t * src1_d, void * dst_d, ggml_type dst_type,
414 int64_t ne00, size_t nb01, size_t nb02, size_t nb03,
415 int64_t ne10, int64_t ne11, int64_t ne12, size_t nb10, size_t nb11, size_t nb12,
416 size_t nb1, size_t nb2, size_t nb3,
417 cudaStream_t stream) {
418 switch (dst_type) {
419 case GGML_TYPE_F32:
420 ggml_cuda_get_rows_switch_src0_type(src0_d, src0_type, src1_d, (float *) dst_d,
421 ne00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb1, nb2, nb3, stream);
422 break;
423 case GGML_TYPE_I32:
424 ggml_cuda_get_rows_switch_src0_type(src0_d, src0_type, src1_d, (int32_t *) dst_d,
425 ne00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb1, nb2, nb3, stream);
426 break;
427 case GGML_TYPE_F16:
428 ggml_cuda_get_rows_switch_src0_type(src0_d, src0_type, src1_d, (half *) dst_d,
429 ne00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb1, nb2, nb3, stream);
430 break;
431 case GGML_TYPE_BF16:
432 ggml_cuda_get_rows_switch_src0_type(src0_d, src0_type, src1_d, (nv_bfloat16 *) dst_d,
433 ne00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb1, nb2, nb3, stream);
434 break;
435 default:
436 GGML_ABORT("%s: unsupported dst type: %s\n", __func__, ggml_type_name(dst_type));
437 }
438}
439
440void ggml_cuda_op_get_rows(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
441 const ggml_tensor * src0 = dst->src[0];
442 const ggml_tensor * src1 = dst->src[1];
443
444 cudaStream_t stream = ctx.stream();
445
446 GGML_TENSOR_BINARY_OP_LOCALS
447
448 GGML_ASSERT(src1->type == GGML_TYPE_I32);
449 GGML_ASSERT(ne13 == 1);
450
451 GGML_ASSERT(src0->nb[0] == ggml_type_size(src0->type));
452 GGML_ASSERT(src1->nb[0] == ggml_type_size(src1->type));
453 GGML_ASSERT(dst->nb[0] == ggml_type_size(dst->type));
454
455 get_rows_cuda(src0->data, src0->type, (const int32_t *) src1->data, dst->data, dst->type,
456 ne00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb1, nb2, nb3, stream);
457}
458
459void ggml_cuda_op_get_rows_back(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
460 const ggml_tensor * src0 = dst->src[0]; // gradients of forward pass output
461 const ggml_tensor * src1 = dst->src[1]; // src1 in forward pass
462
463 GGML_TENSOR_BINARY_OP_LOCALS
464
465 const float * src0_d = (const float *) src0->data;
466 const int32_t * src1_d = (const int32_t *) src1->data;
467 float * dst_d = (float *) dst->data;
468
469 cudaStream_t stream = ctx.stream();
470
471 GGML_ASSERT(src0->type == GGML_TYPE_F32);
472 GGML_ASSERT(src1->type == GGML_TYPE_I32);
473 GGML_ASSERT(dst->type == GGML_TYPE_F32);
474
475 GGML_ASSERT(ggml_is_contiguous(src0));
476 GGML_ASSERT(ggml_is_contiguous(src1));
477 GGML_ASSERT(ggml_is_contiguous(dst));
478
479 GGML_ASSERT(ne02*ne03 == 1);
480 GGML_ASSERT(ne12*ne13 == 1);
481 GGML_ASSERT(ne2*ne3 == 1);
482
483 const dim3 block_dims(CUDA_GET_ROWS_BACK_BLOCK_SIZE, 1, 1);
484 const int block_num_x = (ne00 + CUDA_GET_ROWS_BACK_BLOCK_SIZE - 1) / CUDA_GET_ROWS_BACK_BLOCK_SIZE;
485 const dim3 block_nums(block_num_x, MIN(ne1, (int64_t)UINT16_MAX), 1);
486
487 k_get_rows_back_float<<<block_nums, block_dims, 0, stream>>>(src0_d, src1_d, dst_d, ne00, ne10, ne1);
488}