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 / mmvf.cu

888 lines · 41951 bytes Raw Blame History
1#include "ggml.h"
2#include "common.cuh"
3#include "unary.cuh"
4#include "mmvf.cuh"
5#include "mmf.cuh"
6#include "convert.cuh"
7
8template <typename T, typename type_acc, int ncols_dst, int block_size, bool has_fusion = false, bool is_multi_token_id = false>
9static __global__ void mul_mat_vec_f(
10 const T * x_ptr, const float * y_ptr, const int32_t * ids_ptr, const ggml_cuda_mm_fusion_args_device fusion, float * dst_ptr,
11 const int ncols2, const uint3 nchannels_y, const int stride_row, const int stride_col_y2, const int stride_col_dst,
12 const uint3 channel_ratio, const int stride_channel_x, const int stride_channel_y, const int stride_channel_dst,
13 const uint3 sample_ratio, const int stride_sample_x, const int stride_sample_y, const int stride_sample_dst,
14 const int ids_stride) {
15 const T * GGML_CUDA_RESTRICT x = x_ptr;
16 const float * GGML_CUDA_RESTRICT y = y_ptr;
17 const int32_t * GGML_CUDA_RESTRICT ids = ids_ptr;
18 float * GGML_CUDA_RESTRICT dst = dst_ptr;
19 const int row = blockIdx.x;
20 // for MUL_MAT_ID - blockIdx.y = n_expert_used, blockIdx.z = ncols_dst (tokens)
21 const int channel_dst = blockIdx.y;
22 const int tid = threadIdx.x;
23
24 int token_idx;
25 int channel_x;
26 int channel_y;
27 int sample_dst;
28
29 ggml_cuda_pdl_sync();
30 if constexpr (is_multi_token_id) {
31 // Multi-token MUL_MAT_ID path, adding these in the normal path causes a perf regression for n_tokens=1 case
32 token_idx = blockIdx.z;
33 channel_x = ids[channel_dst + token_idx * ids_stride];
34 channel_y = fastmodulo(channel_dst, nchannels_y);
35 sample_dst = 0;
36 } else {
37 token_idx = ids ? blockIdx.z : 0;
38 channel_x = ids ? ids[blockIdx.y + token_idx * ids_stride] : fastdiv((uint32_t) channel_dst, channel_ratio);
39 channel_y = ids ? fastmodulo(blockIdx.y, nchannels_y) : channel_dst;
40 sample_dst = ids ? 0 : blockIdx.z;
41 }
42
43 const int sample_x = fastdiv((uint32_t) sample_dst, sample_ratio);
44 const int sample_y = sample_dst;
45
46 constexpr int warp_size = ggml_cuda_get_physical_warp_size();
47
48 x += int64_t(sample_x) *stride_sample_x + channel_x *stride_channel_x + row*stride_row;
49 y += int64_t(sample_y) *stride_sample_y + channel_y *stride_channel_y;
50 dst += int64_t(sample_dst)*stride_sample_dst + channel_dst*stride_channel_dst;
51 if constexpr (is_multi_token_id) {
52 y += token_idx*stride_col_y2*2;
53 dst += token_idx*stride_col_dst;
54 }
55
56 bool use_gate = false;
57 bool use_bias = false;
58 bool use_gate_bias = false;
59 ggml_glu_op glu_op = ggml_glu_op::GGML_GLU_OP_SWIGLU;
60 float glu_limit = 0.0f;
61 const T * gate_x = nullptr;
62 const float * x_bias = nullptr;
63 const float * gate_bias = nullptr;
64
65 if constexpr (has_fusion) {
66 use_gate = fusion.gate != nullptr;
67 use_bias = fusion.x_bias != nullptr;
68 use_gate_bias = fusion.gate_bias != nullptr;
69 glu_op = fusion.glu_op;
70 glu_limit = fusion.glu_limit;
71
72 if (use_gate) {
73 gate_x = static_cast<const T *>(fusion.gate);
74 }
75 if (use_bias) {
76 x_bias = static_cast<const float *>(fusion.x_bias);
77 }
78 if (use_gate_bias) {
79 gate_bias = static_cast<const float *>(fusion.gate_bias);
80 use_gate_bias = use_gate;
81 } else {
82 use_gate_bias = false;
83 }
84 }
85
86 if (use_gate) {
87 gate_x += int64_t(sample_x) *stride_sample_x + channel_x *stride_channel_x + row*stride_row;
88 }
89
90 if constexpr (has_fusion) {
91 const int channel_bias = ids ? channel_x : channel_dst;
92 if (use_bias) {
93 x_bias += int64_t(sample_dst)*stride_sample_dst + channel_bias*stride_channel_dst;
94 }
95 if (use_gate_bias) {
96 gate_bias += int64_t(sample_dst)*stride_sample_dst + channel_bias*stride_channel_dst;
97 }
98 }
99
100 const float2 * y2 = (const float2 *) y;
101
102 extern __shared__ char data_mmv[];
103 float * buf_iw = (float *) data_mmv;
104 [[maybe_unused]] float * buf_iw_gate = nullptr;
105 if constexpr (has_fusion) {
106 buf_iw_gate = (float *) (data_mmv + warp_size*sizeof(float));
107 }
108
109 if (block_size > warp_size) {
110 if (tid < warp_size) {
111 buf_iw[tid] = 0.0f;
112 if constexpr (has_fusion) {
113 if (use_gate) {
114 buf_iw_gate[tid] = 0.0f;
115 }
116 }
117 }
118 __syncthreads();
119 }
120
121 float sumf[ncols_dst] = {0.0f};
122 float sumf_gate[ncols_dst];
123 if constexpr (has_fusion) {
124#pragma unroll
125 for (int j = 0; j < ncols_dst; ++j) {
126 sumf_gate[j] = 0.0f;
127 }
128 }
129
130 if constexpr (std::is_same_v<T, float>) {
131 const float2 * x2 = (const float2 *) x;
132 [[maybe_unused]] const float2 * gate_x2 = nullptr;
133 if constexpr (has_fusion) {
134 if (use_gate) {
135 gate_x2 = (const float2 *) gate_x;
136 }
137 }
138
139 for (int col2 = tid; col2 < ncols2; col2 += block_size) {
140 const float2 tmpx = x2[col2];
141 float2 tmpx_gate = make_float2(0.0f, 0.0f);
142 if constexpr (has_fusion) {
143 if (use_gate) {
144 tmpx_gate = gate_x2[col2];
145 }
146 }
147
148#pragma unroll
149 for (int j = 0; j < ncols_dst; ++j) {
150 const float2 tmpy = y2[j*stride_col_y2 + col2];
151 ggml_cuda_mad(sumf[j], tmpx.x, tmpy.x);
152 ggml_cuda_mad(sumf[j], tmpx.y, tmpy.y);
153
154 if constexpr (has_fusion) {
155 if (use_gate) {
156 ggml_cuda_mad(sumf_gate[j], tmpx_gate.x, tmpy.x);
157 ggml_cuda_mad(sumf_gate[j], tmpx_gate.y, tmpy.y);
158 }
159 }
160 }
161 }
162 } else if constexpr (std::is_same_v<T, half>) {
163 const half2 * x2 = (const half2 *) x;
164 [[maybe_unused]] const half2 * gate_x2 = nullptr;
165 if constexpr (has_fusion) {
166 if (use_gate) {
167 gate_x2 = (const half2 *) gate_x;
168 }
169 }
170
171 if (std::is_same_v<type_acc, float>) {
172 for (int col2 = tid; col2 < ncols2; col2 += block_size) {
173 const float2 tmpx = __half22float2(x2[col2]);
174 float2 tmpx_gate = make_float2(0.0f, 0.0f);
175 if constexpr (has_fusion) {
176 if (use_gate) {
177 tmpx_gate = __half22float2(gate_x2[col2]);
178 }
179 }
180#pragma unroll
181 for (int j = 0; j < ncols_dst; ++j) {
182 const float2 tmpy = y2[j*stride_col_y2 + col2];
183 ggml_cuda_mad(sumf[j], tmpx.x, tmpy.x);
184 ggml_cuda_mad(sumf[j], tmpx.y, tmpy.y);
185
186 if constexpr (has_fusion) {
187 if (use_gate) {
188 ggml_cuda_mad(sumf_gate[j], tmpx_gate.x, tmpy.x);
189 ggml_cuda_mad(sumf_gate[j], tmpx_gate.y, tmpy.y);
190 }
191 }
192 }
193 }
194 } else {
195#ifdef FP16_AVAILABLE
196 half2 sumh2[ncols_dst] = {{0.0f, 0.0f}};
197 half2 sumh2_gate[ncols_dst] = {{0.0f, 0.0f}};
198
199 for (int col2 = tid; col2 < ncols2; col2 += block_size) {
200 const half2 tmpx = x2[col2];
201 half2 tmpx_gate = make_half2(0.0f, 0.0f);
202 if constexpr (has_fusion) {
203 if (use_gate) {
204 tmpx_gate = gate_x2[col2];
205 }
206 }
207#pragma unroll
208 for (int j = 0; j < ncols_dst; ++j) {
209 const float2 tmpy = y2[j*stride_col_y2 + col2];
210 sumh2[j] += tmpx * make_half2(tmpy.x, tmpy.y);
211
212 if constexpr (has_fusion) {
213 if (use_gate) {
214 sumh2_gate[j] += tmpx_gate * make_half2(tmpy.x, tmpy.y);
215 }
216 }
217 }
218 }
219
220#pragma unroll
221 for (int j = 0; j < ncols_dst; ++j) {
222 sumf[j] = __low2float(sumh2[j]) + __high2float(sumh2[j]);
223 }
224
225 if constexpr (has_fusion) {
226 if (use_gate) {
227#pragma unroll
228 for (int j = 0; j < ncols_dst; ++j) {
229 sumf_gate[j] = __low2float(sumh2_gate[j]) + __high2float(sumh2_gate[j]);
230 }
231 }
232 }
233#else
234 NO_DEVICE_CODE;
235#endif // FP16_AVAILABLE
236 }
237 } else if constexpr (std::is_same_v<T, nv_bfloat16>) {
238//TODO: add support for ggml_cuda_mad for hip_bfloat162
239#if defined(GGML_USE_HIP)
240 const int * x2 = (const int *) x;
241 const int * gate_x2 = nullptr;
242 if constexpr (has_fusion) {
243 if (use_gate) {
244 gate_x2 = (const int *) gate_x;
245 }
246 }
247 for (int col2 = tid; col2 < ncols2; col2 += block_size) {
248 const int tmpx = x2[col2];
249 int tmpx_gate = 0;
250 if constexpr (has_fusion) {
251 if (use_gate) {
252 tmpx_gate = gate_x2[col2];
253 }
254 }
255#pragma unroll
256 for (int j = 0; j < ncols_dst; ++j) {
257 const float2 tmpy = y2[j*stride_col_y2 + col2];
258 const float tmpx0 = ggml_cuda_cast<float>(reinterpret_cast<const nv_bfloat16 *>(&tmpx)[0]);
259 const float tmpx1 = ggml_cuda_cast<float>(reinterpret_cast<const nv_bfloat16 *>(&tmpx)[1]);
260 ggml_cuda_mad(sumf[j], tmpx0, tmpy.x);
261 ggml_cuda_mad(sumf[j], tmpx1, tmpy.y);
262
263 if constexpr (has_fusion) {
264 if (use_gate) {
265 const float tmpx0_gate = ggml_cuda_cast<float>(reinterpret_cast<const nv_bfloat16 *>(&tmpx_gate)[0]);
266 const float tmpx1_gate = ggml_cuda_cast<float>(reinterpret_cast<const nv_bfloat16 *>(&tmpx_gate)[1]);
267 ggml_cuda_mad(sumf_gate[j], tmpx0_gate, tmpy.x);
268 ggml_cuda_mad(sumf_gate[j], tmpx1_gate, tmpy.y);
269 }
270 }
271 }
272 }
273#else
274 const nv_bfloat162 * x2 = (const nv_bfloat162 *) x;
275 [[maybe_unused]] const nv_bfloat162 * gate_x2 = nullptr;
276 if constexpr (has_fusion) {
277 if (use_gate) {
278 gate_x2 = (const nv_bfloat162 *) gate_x;
279 }
280 }
281 for (int col2 = tid; col2 < ncols2; col2 += block_size) {
282 const nv_bfloat162 tmpx = x2[col2];
283 [[maybe_unused]] nv_bfloat162 tmpx_gate;
284 if constexpr (has_fusion) {
285 if (use_gate) {
286 tmpx_gate = gate_x2[col2];
287 }
288 }
289#pragma unroll
290 for (int j = 0; j < ncols_dst; ++j) {
291 const float2 tmpy = y2[j*stride_col_y2 + col2];
292 ggml_cuda_mad(sumf[j], tmpx.x, tmpy.x);
293 ggml_cuda_mad(sumf[j], tmpx.y, tmpy.y);
294
295 if constexpr (has_fusion) {
296 if (use_gate) {
297 ggml_cuda_mad(sumf_gate[j], tmpx_gate.x, tmpy.x);
298 ggml_cuda_mad(sumf_gate[j], tmpx_gate.y, tmpy.y);
299 }
300 }
301 }
302 }
303#endif
304 } else {
305 static_assert(std::is_same_v<T, void>, "unsupported type");
306 }
307
308 ggml_cuda_pdl_lc();
309#pragma unroll
310 for (int j = 0; j < ncols_dst; ++j) {
311 sumf[j] = warp_reduce_sum<warp_size>(sumf[j]);
312
313 if constexpr (has_fusion) {
314 if (use_gate) {
315 sumf_gate[j] = warp_reduce_sum<warp_size>(sumf_gate[j]);
316 }
317 }
318
319 if (block_size > warp_size) {
320 buf_iw[tid/warp_size] = sumf[j];
321 if constexpr (has_fusion) {
322 if (use_gate) {
323 buf_iw_gate[tid/warp_size] = sumf_gate[j];
324 }
325 }
326 __syncthreads();
327 if (tid < warp_size) {
328 sumf[j] = buf_iw[tid];
329 sumf[j] = warp_reduce_sum<warp_size>(sumf[j]);
330 if constexpr (has_fusion) {
331 if (use_gate) {
332 sumf_gate[j] = buf_iw_gate[tid];
333 sumf_gate[j] = warp_reduce_sum<warp_size>(sumf_gate[j]);
334 }
335 }
336 }
337
338 if (j < ncols_dst) {
339 __syncthreads();
340 }
341 }
342 }
343
344 if (tid >= ncols_dst) {
345 return;
346 }
347
348 float value = sumf[tid];
349
350 if constexpr (has_fusion) {
351 if (use_bias) {
352 value += x_bias[tid*stride_col_dst + row];
353 }
354
355 if (use_gate) {
356 float gate_value = sumf_gate[tid];
357 if (use_gate_bias) {
358 gate_value += gate_bias[tid*stride_col_dst + row];
359 }
360 switch (glu_op) {
361 case GGML_GLU_OP_SWIGLU:
362 value *= ggml_cuda_op_silu_single(gate_value);
363 break;
364 case GGML_GLU_OP_GEGLU:
365 value *= ggml_cuda_op_gelu_single(gate_value);
366 break;
367 case GGML_GLU_OP_SWIGLU_OAI: {
368 value = ggml_cuda_op_swiglu_oai_single(gate_value, value);
369 break;
370 }
371 case GGML_GLU_OP_SWIGLU_CLAMP:
372 value = ggml_cuda_op_swiglu_clamp_single(gate_value, value, glu_limit);
373 break;
374 default:
375 break;
376 }
377 }
378 }
379
380 dst[tid*stride_col_dst + row] = value;
381
382 if constexpr (!has_fusion) {
383 GGML_UNUSED_VARS(use_gate, use_bias, use_gate_bias, glu_op, glu_limit, gate_x, x_bias, gate_bias, sumf_gate);
384 }
385}
386
387template<typename T, typename type_acc, int ncols_dst, int block_size, bool is_multi_token_id = false>
388static void mul_mat_vec_f_switch_fusion(
389 const T * x, const float * y, const int32_t * ids, const ggml_cuda_mm_fusion_args_device fusion, float * dst,
390 const int64_t ncols, const uint3 nchannels_y,
391 const int64_t stride_row, const int64_t stride_col_y, const int64_t stride_col_dst,
392 const uint3 channel_ratio, const int stride_channel_x, const int stride_channel_y, const int stride_channel_dst,
393 const uint3 sample_ratio, const int stride_sample_x, const int stride_sample_y, const int stride_sample_dst,
394 const dim3 & block_dims, const dim3 & block_nums, const int nbytes_shared, const int ids_stride, const cudaStream_t stream) {
395
396 const ggml_cuda_kernel_launch_params launch_params = {block_nums, block_dims, nbytes_shared, stream};
397
398 const bool has_fusion = fusion.gate != nullptr || fusion.x_bias != nullptr || fusion.gate_bias != nullptr;
399 if constexpr (ncols_dst == 1) {
400 if (has_fusion) {
401 ggml_cuda_kernel_launch(mul_mat_vec_f<T, type_acc, ncols_dst, block_size, true, is_multi_token_id>, launch_params,
402 x, y, ids, fusion, dst, ncols, nchannels_y, stride_row, stride_col_y, stride_col_dst,
403 channel_ratio, stride_channel_x, stride_channel_y, stride_channel_dst,
404 sample_ratio, stride_sample_x, stride_sample_y, stride_sample_dst, ids_stride);
405 return;
406 }
407 }
408
409 GGML_ASSERT(!has_fusion && "fusion only supported for ncols_dst=1");
410
411 ggml_cuda_kernel_launch(mul_mat_vec_f<T, type_acc, ncols_dst, block_size, false, is_multi_token_id>, launch_params,
412 x, y, ids, fusion, dst, ncols, nchannels_y, stride_row, stride_col_y, stride_col_dst,
413 channel_ratio, stride_channel_x, stride_channel_y, stride_channel_dst,
414 sample_ratio, stride_sample_x, stride_sample_y, stride_sample_dst, ids_stride);
415
416}
417
418template <typename T, typename type_acc, int ncols_dst, bool is_multi_token_id = false>
419void launch_mul_mat_vec_f_cuda(
420 const T * x, const float * y, const int32_t * ids, const ggml_cuda_mm_fusion_args_device fusion, float * dst,
421 const int64_t ncols, const int64_t nrows,
422 const int64_t stride_row, const int64_t stride_col_y, const int64_t stride_col_dst,
423 const int64_t nchannels_x, const int64_t nchannels_y, const int64_t nchannels_dst,
424 const int64_t stride_channel_x, const int64_t stride_channel_y, const int64_t stride_channel_dst, const int64_t nsamples_x,
425 const int64_t nsamples_dst, const int64_t stride_sample_x, const int64_t stride_sample_y, const int64_t stride_sample_dst,
426 const int64_t nsamples_or_ntokens, const int64_t ids_stride, cudaStream_t stream) {
427 GGML_ASSERT(ncols % 2 == 0);
428 GGML_ASSERT(stride_row % 2 == 0);
429 GGML_ASSERT(stride_col_y % 2 == 0);
430 GGML_ASSERT(ids || nchannels_dst % nchannels_x == 0);
431 GGML_ASSERT( nsamples_dst % nsamples_x == 0);
432 const uint3 nchannels_y_fd = ids ? init_fastdiv_values(nchannels_y) : make_uint3(0, 0, 0);
433 const uint3 channel_ratio_fd = ids ? make_uint3(0, 0, 0) : init_fastdiv_values(nchannels_dst / nchannels_x);
434 const uint3 sample_ratio_fd = init_fastdiv_values(nsamples_dst / nsamples_x);
435
436 const int device = ggml_cuda_get_device();
437 const int warp_size = ggml_cuda_info().devices[device].warp_size;
438
439 int64_t block_size_best = warp_size;
440 int64_t niter_best = (ncols + 2*warp_size - 1) / (2*warp_size);
441 int64_t max_block_size = 256;
442 if(ggml_cuda_info().devices[device].cc > GGML_CUDA_CC_OFFSET_AMD && ggml_cuda_info().devices[device].cc < GGML_CUDA_CC_RDNA1) {
443 max_block_size = 128;
444 }
445 for (int64_t block_size = 2*warp_size; block_size <= max_block_size; block_size += warp_size) {
446 const int64_t niter = (ncols + 2*block_size - 1) / (2*block_size);
447 if (niter < niter_best) {
448 niter_best = niter;
449 block_size_best = block_size;
450 }
451 }
452
453 const bool has_fusion = fusion.gate != nullptr || fusion.x_bias != nullptr || fusion.gate_bias != nullptr;
454
455 const int nbytes_shared = warp_size*sizeof(float) + (has_fusion ? warp_size*sizeof(float) : 0);
456 const dim3 block_nums(nrows, nchannels_dst, nsamples_or_ntokens);
457 const dim3 block_dims(block_size_best, 1, 1);
458 switch (block_size_best) {
459 case 32: {
460 mul_mat_vec_f_switch_fusion<T, type_acc, ncols_dst, 32, is_multi_token_id>
461 (x, y, ids, fusion, dst, ncols/2, nchannels_y_fd, stride_row, stride_col_y/2, stride_col_dst,
462 channel_ratio_fd, stride_channel_x, stride_channel_y, stride_channel_dst,
463 sample_ratio_fd, stride_sample_x, stride_sample_y, stride_sample_dst, block_dims, block_nums, nbytes_shared, ids_stride, stream);
464 } break;
465 case 64: {
466 mul_mat_vec_f_switch_fusion<T, type_acc, ncols_dst, 64, is_multi_token_id>
467 (x, y, ids, fusion, dst, ncols/2, nchannels_y_fd, stride_row, stride_col_y/2, stride_col_dst,
468 channel_ratio_fd, stride_channel_x, stride_channel_y, stride_channel_dst,
469 sample_ratio_fd, stride_sample_x, stride_sample_y, stride_sample_dst, block_dims, block_nums, nbytes_shared, ids_stride, stream);
470 } break;
471 case 96: {
472 mul_mat_vec_f_switch_fusion<T, type_acc, ncols_dst, 96, is_multi_token_id>
473 (x, y, ids, fusion, dst, ncols/2, nchannels_y_fd, stride_row, stride_col_y/2, stride_col_dst,
474 channel_ratio_fd, stride_channel_x, stride_channel_y, stride_channel_dst,
475 sample_ratio_fd, stride_sample_x, stride_sample_y, stride_sample_dst, block_dims, block_nums, nbytes_shared, ids_stride, stream);
476 } break;
477 case 128: {
478 mul_mat_vec_f_switch_fusion<T, type_acc, ncols_dst, 128, is_multi_token_id>
479 (x, y, ids, fusion, dst, ncols/2, nchannels_y_fd, stride_row, stride_col_y/2, stride_col_dst,
480 channel_ratio_fd, stride_channel_x, stride_channel_y, stride_channel_dst,
481 sample_ratio_fd, stride_sample_x, stride_sample_y, stride_sample_dst, block_dims, block_nums, nbytes_shared, ids_stride, stream);
482 } break;
483 case 160: {
484 mul_mat_vec_f_switch_fusion<T, type_acc, ncols_dst, 160, is_multi_token_id>
485 (x, y, ids, fusion, dst, ncols/2, nchannels_y_fd, stride_row, stride_col_y/2, stride_col_dst,
486 channel_ratio_fd, stride_channel_x, stride_channel_y, stride_channel_dst,
487 sample_ratio_fd, stride_sample_x, stride_sample_y, stride_sample_dst, block_dims, block_nums, nbytes_shared, ids_stride, stream);
488 } break;
489 case 192: {
490 mul_mat_vec_f_switch_fusion<T, type_acc, ncols_dst, 192, is_multi_token_id>
491 (x, y, ids, fusion, dst, ncols/2, nchannels_y_fd, stride_row, stride_col_y/2, stride_col_dst,
492 channel_ratio_fd, stride_channel_x, stride_channel_y, stride_channel_dst,
493 sample_ratio_fd, stride_sample_x, stride_sample_y, stride_sample_dst, block_dims, block_nums, nbytes_shared, ids_stride, stream);
494 } break;
495 case 224: {
496 mul_mat_vec_f_switch_fusion<T, type_acc, ncols_dst, 224, is_multi_token_id>
497 (x, y, ids, fusion, dst, ncols/2, nchannels_y_fd, stride_row, stride_col_y/2, stride_col_dst,
498 channel_ratio_fd, stride_channel_x, stride_channel_y, stride_channel_dst,
499 sample_ratio_fd, stride_sample_x, stride_sample_y, stride_sample_dst, block_dims, block_nums, nbytes_shared, ids_stride, stream);
500 } break;
501 case 256: {
502 mul_mat_vec_f_switch_fusion<T, type_acc, ncols_dst, 256, is_multi_token_id>
503 (x, y, ids, fusion, dst, ncols/2, nchannels_y_fd, stride_row, stride_col_y/2, stride_col_dst,
504 channel_ratio_fd, stride_channel_x, stride_channel_y, stride_channel_dst,
505 sample_ratio_fd, stride_sample_x, stride_sample_y, stride_sample_dst, block_dims, block_nums, nbytes_shared, ids_stride, stream);
506 } break;
507 default:
508 GGML_ABORT("fatal error");
509 }
510}
511
512template <typename T, typename type_acc>
513static void mul_mat_vec_f_cuda_switch_ncols_dst(
514 const T * x, const float * y, const int32_t * ids, const ggml_cuda_mm_fusion_args_device fusion, float * dst,
515 const int64_t ncols, const int64_t nrows, const int64_t ncols_dst,
516 const int64_t stride_row, const int64_t stride_col_y, const int64_t stride_col_dst,
517 const int64_t nchannels_x, const int64_t nchannels_y, const int64_t nchannels_dst,
518 const int64_t stride_channel_x, const int64_t stride_channel_y, const int64_t stride_channel_dst, const int64_t nsamples_x,
519 const int64_t nsamples_dst, const int64_t stride_sample_x, const int64_t stride_sample_y, const int64_t stride_sample_dst,
520 const int64_t ids_stride, cudaStream_t stream) {
521
522 const bool has_ids = ids != nullptr;
523
524 if (has_ids && ncols_dst > 1) {
525 // Multi-token MUL_MAT_ID path only - single-token goes through regular path below
526 constexpr int c_ncols_dst = 1;
527 launch_mul_mat_vec_f_cuda<T, type_acc, c_ncols_dst, true>
528 (x, y, ids, fusion, dst, ncols, nrows, stride_row, stride_col_y, stride_col_dst,
529 nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y,
530 stride_channel_dst, nsamples_x, nsamples_dst, stride_sample_x, stride_sample_y, stride_sample_dst,
531 ncols_dst, ids_stride, stream);
532 return;
533 }
534
535 if (has_ids) {
536 // Single-token MUL_MAT_ID path
537 constexpr int c_ncols_dst = 1;
538 launch_mul_mat_vec_f_cuda<T, type_acc, c_ncols_dst>
539 (x, y, ids, fusion, dst, ncols, nrows, stride_row, stride_col_y, stride_col_dst,
540 nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y,
541 stride_channel_dst, nsamples_x, nsamples_dst, stride_sample_x, stride_sample_y, stride_sample_dst,
542 ncols_dst, ids_stride, stream);
543 return;
544 }
545
546 switch (ncols_dst) {
547 case 1:
548 launch_mul_mat_vec_f_cuda<T, type_acc, 1>
549 (x, y, ids, fusion, dst, ncols, nrows, stride_row, stride_col_y, stride_col_dst,
550 nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y,
551 stride_channel_dst, nsamples_x, nsamples_dst, stride_sample_x, stride_sample_y, stride_sample_dst,
552 nsamples_dst, ids_stride, stream);
553 break;
554 case 2:
555 launch_mul_mat_vec_f_cuda<T, type_acc, 2>
556 (x, y, ids, fusion, dst, ncols, nrows, stride_row, stride_col_y, stride_col_dst,
557 nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y,
558 stride_channel_dst, nsamples_x, nsamples_dst, stride_sample_x, stride_sample_y, stride_sample_dst,
559 nsamples_dst, ids_stride, stream);
560 break;
561 case 3:
562 launch_mul_mat_vec_f_cuda<T, type_acc, 3>
563 (x, y, ids, fusion, dst, ncols, nrows, stride_row, stride_col_y, stride_col_dst,
564 nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y,
565 stride_channel_dst, nsamples_x, nsamples_dst, stride_sample_x, stride_sample_y, stride_sample_dst,
566 nsamples_dst, ids_stride, stream);
567 break;
568 case 4:
569 launch_mul_mat_vec_f_cuda<T, type_acc, 4>
570 (x, y, ids, fusion, dst, ncols, nrows, stride_row, stride_col_y, stride_col_dst,
571 nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y,
572 stride_channel_dst, nsamples_x, nsamples_dst, stride_sample_x, stride_sample_y, stride_sample_dst,
573 nsamples_dst, ids_stride, stream);
574 break;
575 case 5:
576 launch_mul_mat_vec_f_cuda<T, type_acc, 5>
577 (x, y, ids, fusion, dst, ncols, nrows, stride_row, stride_col_y, stride_col_dst,
578 nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y,
579 stride_channel_dst, nsamples_x, nsamples_dst, stride_sample_x, stride_sample_y, stride_sample_dst,
580 nsamples_dst, ids_stride, stream);
581 break;
582 case 6:
583 launch_mul_mat_vec_f_cuda<T, type_acc, 6>
584 (x, y, ids, fusion, dst, ncols, nrows, stride_row, stride_col_y, stride_col_dst,
585 nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y,
586 stride_channel_dst, nsamples_x, nsamples_dst, stride_sample_x, stride_sample_y, stride_sample_dst,
587 nsamples_dst, ids_stride, stream);
588 break;
589 case 7:
590 launch_mul_mat_vec_f_cuda<T, type_acc, 7>
591 (x, y, ids, fusion, dst, ncols, nrows, stride_row, stride_col_y, stride_col_dst,
592 nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y,
593 stride_channel_dst, nsamples_x, nsamples_dst, stride_sample_x, stride_sample_y, stride_sample_dst,
594 nsamples_dst, ids_stride, stream);
595 break;
596 case 8:
597 launch_mul_mat_vec_f_cuda<T, type_acc, 8>
598 (x, y, ids, fusion, dst, ncols, nrows, stride_row, stride_col_y, stride_col_dst,
599 nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y,
600 stride_channel_dst, nsamples_x, nsamples_dst, stride_sample_x, stride_sample_y, stride_sample_dst,
601 nsamples_dst, ids_stride, stream);
602 break;
603 default:
604 GGML_ABORT("fatal error");
605 }
606}
607
608template<typename T>
609static void mul_mat_vec_f_cuda(
610 const T * x, const float * y, const int32_t * ids, const ggml_cuda_mm_fusion_args_device fusion, float * dst,
611 const int64_t ncols, const int64_t nrows, const int64_t ncols_dst,
612 const int64_t stride_row, const int64_t stride_col_y, const int stride_col_dst,
613 const int64_t nchannels_x, const int64_t nchannels_y, const int64_t nchannels_dst,
614 const int64_t stride_channel_x, const int64_t stride_channel_y, const int64_t stride_channel_dst, const int64_t nsamples_x,
615 const int64_t nsamples_dst, const int64_t stride_sample_x, const int64_t stride_sample_y, const int64_t stride_sample_dst,
616 const int64_t ids_stride, enum ggml_prec prec, cudaStream_t stream) {
617
618 if constexpr(std::is_same_v<T, half>) {
619 if (prec == GGML_PREC_DEFAULT) {
620 mul_mat_vec_f_cuda_switch_ncols_dst<T, half>
621 (x, y, ids, fusion, dst, ncols, nrows, ncols_dst, stride_row, stride_col_y, stride_col_dst,
622 nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y,
623 stride_channel_dst, nsamples_x, nsamples_dst, stride_sample_x, stride_sample_y, stride_sample_dst, ids_stride, stream);
624 return;
625 }
626 }
627 mul_mat_vec_f_cuda_switch_ncols_dst<T, float>
628 (x, y, ids, fusion, dst, ncols, nrows, ncols_dst, stride_row, stride_col_y, stride_col_dst,
629 nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y,
630 stride_channel_dst, nsamples_x, nsamples_dst, stride_sample_x, stride_sample_y, stride_sample_dst, ids_stride, stream);
631}
632
633void ggml_cuda_mul_mat_vec_f(ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * ids, ggml_tensor * dst,
634 const ggml_cuda_mm_fusion_args_host * fusion) {
635 GGML_ASSERT( src1->type == GGML_TYPE_F32);
636 GGML_ASSERT(!ids || ids->type == GGML_TYPE_I32);
637 GGML_ASSERT( dst->type == GGML_TYPE_F32);
638
639 GGML_TENSOR_BINARY_OP_LOCALS;
640
641 const size_t ts_src0 = ggml_type_size(src0->type);
642 const size_t ts_src1 = ggml_type_size(src1->type);
643 const size_t ts_dst = ggml_type_size(dst->type);
644
645 GGML_ASSERT(!ids || ne12 <= MMVF_MAX_BATCH_SIZE);
646 GGML_ASSERT(ne13 == ne3);
647
648 GGML_ASSERT( nb00 == ts_src0);
649 GGML_ASSERT( nb10 == ts_src1);
650 GGML_ASSERT(!ids || ids->nb[0] == ggml_type_size(ids->type));
651 GGML_ASSERT( nb0 == ts_dst);
652
653 const int cc = ggml_cuda_info().devices[ggml_cuda_get_device()].cc;
654 const enum ggml_prec prec = fast_fp16_available(cc) ? ggml_prec(dst->op_params[0]) : GGML_PREC_F32;
655
656 const float * src1_d = (const float *) src1->data;
657 const int32_t * ids_d = ids ? (const int32_t *) ids->data : nullptr;
658 float * dst_d = (float *) dst->data;
659
660 ggml_cuda_mm_fusion_args_device fusion_local{};
661
662 if (fusion) {
663 GGML_ASSERT( !ids || dst->ne[2] == 1);
664 GGML_ASSERT( ids || dst->ne[1] == 1);
665 if (fusion->x_bias) {
666 GGML_ASSERT(fusion->x_bias->type == GGML_TYPE_F32);
667 GGML_ASSERT(fusion->x_bias->ne[0] == dst->ne[0]);
668 GGML_ASSERT(!ids || fusion->x_bias->ne[1] == src0->ne[2]);
669 fusion_local.x_bias = fusion->x_bias->data;
670 }
671 if (fusion->gate) {
672 GGML_ASSERT(fusion->gate->type == src0->type && ggml_are_same_stride(fusion->gate, src0));
673 fusion_local.gate = fusion->gate->data;
674 }
675 if (fusion->gate_bias) {
676 GGML_ASSERT(fusion->gate_bias->type == GGML_TYPE_F32);
677 GGML_ASSERT(fusion->gate_bias->ne[0] == dst->ne[0]);
678 GGML_ASSERT(!ids || fusion->gate_bias->ne[1] == src0->ne[2]);
679 fusion_local.gate_bias = fusion->gate_bias->data;
680 }
681 fusion_local.glu_op = fusion->glu_op;
682 fusion_local.glu_limit = fusion->glu_limit;
683 }
684
685 const int64_t s01 = src0->nb[1] / ts_src0;
686 const int64_t s11 = src1->nb[1] / ts_src1;
687 const int64_t s1 = dst->nb[1] / ts_dst;
688 const int64_t s02 = src0->nb[2] / ts_src0;
689 const int64_t s12 = src1->nb[2] / ts_src1;
690 const int64_t s2 = dst->nb[2] / ts_dst;
691 const int64_t s03 = src0->nb[3] / ts_src0;
692 const int64_t s13 = src1->nb[3] / ts_src1;
693 const int64_t s3 = dst->nb[3] / ts_dst;
694
695 // For MUL_MAT_ID the memory layout is different than for MUL_MAT:
696 const int64_t ncols_dst = ids ? ne2 : ne1;
697 const int64_t nchannels_y = ids ? ne11 : ne12;
698 const int64_t nchannels_dst = ids ? ne1 : ne2;
699 const int64_t stride_col_dst = ids ? s2 : s1;
700 const int64_t stride_col_y = ids ? s12 : s11;
701 const int64_t stride_channel_dst = ids ? s1 : s2;
702 const int64_t stride_channel_y = ids ? s11 : s12;
703
704 const int64_t ids_stride = ids ? ids->nb[1] / ggml_type_size(ids->type) : 0;
705
706 switch (src0->type) {
707 case GGML_TYPE_F32: {
708 const float * src0_d = (const float *) src0->data;
709 mul_mat_vec_f_cuda(src0_d, src1_d, ids_d, fusion_local, dst_d, ne00, ne01, ncols_dst, s01, stride_col_y, stride_col_dst,
710 ne02, nchannels_y, nchannels_dst, s02, stride_channel_y, stride_channel_dst,
711 ne03, ne3, s03, s13, s3, ids_stride, prec, ctx.stream());
712 } break;
713 case GGML_TYPE_F16: {
714 const half * src0_d = (const half *) src0->data;
715 mul_mat_vec_f_cuda(src0_d, src1_d, ids_d, fusion_local, dst_d, ne00, ne01, ncols_dst, s01, stride_col_y, stride_col_dst,
716 ne02, nchannels_y, nchannels_dst, s02, stride_channel_y, stride_channel_dst,
717 ne03, ne3, s03, s13, s3, ids_stride, prec, ctx.stream());
718 } break;
719 case GGML_TYPE_BF16: {
720 const nv_bfloat16 * src0_d = (const nv_bfloat16 *) src0->data;
721 mul_mat_vec_f_cuda(src0_d, src1_d, ids_d, fusion_local, dst_d, ne00, ne01, ncols_dst, s01, stride_col_y, stride_col_dst,
722 ne02, nchannels_y, nchannels_dst, s02, stride_channel_y, stride_channel_dst,
723 ne03, ne3, s03, s13, s3, ids_stride, prec, ctx.stream());
724 } break;
725 default:
726 GGML_ABORT("unsupported type: %s", ggml_type_name(src0->type));
727 }
728}
729
730void ggml_cuda_op_mul_mat_vec_f(
731 ggml_backend_cuda_context & ctx,
732 const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
733 const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
734 const int64_t src1_padded_row_size, cudaStream_t stream) {
735
736 GGML_ASSERT(src1->type == GGML_TYPE_F32);
737 GGML_ASSERT(dst->type == GGML_TYPE_F32);
738
739 const int64_t ne00 = src0->ne[0];
740 const int64_t ne10 = src1->ne[0];
741 const int64_t ne0 = dst->ne[0];
742 const int64_t row_diff = row_high - row_low;
743
744 const int id = ggml_cuda_get_device();
745 const int cc = ggml_cuda_info().devices[id].cc;
746 const enum ggml_prec prec = fast_fp16_available(cc) ? ggml_prec(dst->op_params[0]) : GGML_PREC_F32;
747
748 // ggml_cuda_op provides single, contiguous matrices
749 const int64_t stride_row = ne00;
750 const int64_t stride_col_y = ne10;
751 const int64_t stride_col_dst = id == ctx.device ? ne0 : row_diff; // main device has larger memory buffer
752 const int64_t nchannels_x = 1;
753 const int64_t nchannels_y = 1;
754 const int64_t nchannels_dst = 1;
755 const int64_t stride_channel_x = 0;
756 const int64_t stride_channel_y = 0;
757 const int64_t stride_channel_dst = 0;
758 const int64_t nsamples_x = 1;
759 const int64_t nsamples_dst = 1;
760 const int64_t stride_sample_x = 0;
761 const int64_t stride_sample_y = 0;
762 const int64_t stride_sample_dst = 0;
763
764 ggml_cuda_mm_fusion_args_device empty{};
765 switch (src0->type) {
766 case GGML_TYPE_F32: {
767 const float * src0_d = (const float *) src0_dd_i;
768 mul_mat_vec_f_cuda(src0_d, src1_ddf_i, nullptr, empty, dst_dd_i, ne00, row_diff, src1_ncols, stride_row, stride_col_y, stride_col_dst,
769 nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst,
770 nsamples_x, nsamples_dst, stride_sample_x, stride_sample_y, stride_sample_dst, 0, prec, stream);
771 } break;
772 case GGML_TYPE_F16: {
773 const half * src0_d = (const half *) src0_dd_i;
774 mul_mat_vec_f_cuda(src0_d, src1_ddf_i, nullptr, empty, dst_dd_i, ne00, row_diff, src1_ncols, stride_row, stride_col_y, stride_col_dst,
775 nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst,
776 nsamples_x, nsamples_dst, stride_sample_x, stride_sample_y, stride_sample_dst, 0, prec, stream);
777 } break;
778 case GGML_TYPE_BF16: {
779 const nv_bfloat16 * src0_d = (const nv_bfloat16 *) src0_dd_i;
780 mul_mat_vec_f_cuda(src0_d, src1_ddf_i, nullptr, empty, dst_dd_i, ne00, row_diff, src1_ncols, stride_row, stride_col_y, stride_col_dst,
781 nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst,
782 nsamples_x, nsamples_dst, stride_sample_x, stride_sample_y, stride_sample_dst, 0, prec, stream);
783 } break;
784 default:
785 GGML_ABORT("unsupported type: %s", ggml_type_name(src0->type));
786 }
787
788 GGML_UNUSED_VARS(ctx, src1, dst, src1_ddq_i, src1_ncols, src1_padded_row_size);
789}
790
791bool ggml_cuda_should_use_mmvf(enum ggml_type type, int cc, int warp_size, const int64_t * src0_ne, const size_t * src0_nb, int64_t ne11) {
792 if (src0_ne[0] % 2 != 0) {
793 return false;
794 }
795
796 const size_t ts = ggml_type_size(type);
797 if (src0_nb[0] != ts) {
798 return false;
799 }
800
801 // Pointers not aligned to the size of half2/nv_bfloat162/float2 would result in a crash:
802 for (size_t i = 1; i < GGML_MAX_DIMS; ++i) {
803 if (src0_nb[i] % (2*ts) != 0) {
804 return false;
805 }
806 }
807
808 switch (type) {
809 case GGML_TYPE_F32:
810 if (GGML_CUDA_CC_IS_NVIDIA(cc)) {
811 if (ampere_mma_available(cc)) {
812 return ne11 <= 3;
813 }
814 if (cc >= GGML_CUDA_CC_TURING) {
815 return ne11 <= 4;
816 }
817 return ne11 <= 3;
818 } else if (GGML_CUDA_CC_IS_AMD(cc)) {
819 if (fp32_mma_hardware_available(cc)) {
820 return ne11 <= 3;
821 }
822 return ne11 <= MMVF_MAX_BATCH_SIZE;
823 }
824 return ne11 <= MMVF_MAX_BATCH_SIZE;
825 case GGML_TYPE_F16:
826 if (GGML_CUDA_CC_IS_NVIDIA(cc)) {
827 const bool src0_small = (src0_ne[1] <= 512 || src0_ne[2]*src0_ne[3] == 1);
828 // MMF needs full row tiles, for other row counts MMVF still beats cuBLAS at small batch size
829 if (src0_small && !ggml_cuda_should_use_mmf(type, cc, warp_size, src0_ne, src0_nb, ne11, /*mul_mat_id =*/ false)) {
830 return ne11 <= MMVF_MAX_BATCH_SIZE;
831 }
832 if (ampere_mma_available(cc)) {
833 return src0_small && ne11 == 1;
834 }
835 if (cc >= GGML_CUDA_CC_ADA_LOVELACE) {
836 return src0_small && ne11 <= 4;
837 }
838 if (fp16_mma_hardware_available(cc)) {
839 return src0_small && ne11 <= 3;
840 }
841 return ne11 <= MMVF_MAX_BATCH_SIZE;
842 } else if (GGML_CUDA_CC_IS_AMD(cc)) {
843 if (GGML_CUDA_CC_IS_RDNA(cc) && !ggml_cuda_should_use_mmf(type, cc, warp_size, src0_ne, src0_nb, ne11, /*mul_mat_id =*/ false)) {
844 return ne11 <= MMVF_MAX_BATCH_SIZE;
845 }
846 if (fp16_mma_hardware_available(cc)) {
847 if (GGML_CUDA_CC_IS_RDNA3(cc) || GGML_CUDA_CC_IS_RDNA4_M(cc)) {
848 return ne11 <= 3;
849 }
850 if (GGML_CUDA_CC_IS_RDNA4(cc)) {
851 return ne11 <= 5;
852 }
853 return ne11 <= 2;
854 }
855 return ne11 <= MMVF_MAX_BATCH_SIZE;
856 }
857 return ne11 <= MMVF_MAX_BATCH_SIZE;
858 case GGML_TYPE_BF16:
859 if (GGML_CUDA_CC_IS_NVIDIA(cc)) {
860 const bool src0_small = (src0_ne[1] <= 512 || src0_ne[2]*src0_ne[3] == 1);
861 // MMF needs full row tiles, for other row counts MMVF still beats cuBLAS at small batch size
862 if (src0_small && !ggml_cuda_should_use_mmf(type, cc, warp_size, src0_ne, src0_nb, ne11, /*mul_mat_id =*/ false)) {
863 return ne11 <= MMVF_MAX_BATCH_SIZE;
864 }
865 if (ampere_mma_available(cc)) {
866 return src0_small && ne11 == 1;
867 }
868 if (cc >= GGML_CUDA_CC_ADA_LOVELACE) {
869 return src0_small && ne11 <= 4;
870 }
871 if (bf16_mma_hardware_available(cc)) {
872 return src0_small && ne11 <= 3;
873 }
874 return ne11 <= MMVF_MAX_BATCH_SIZE;
875 } else if (GGML_CUDA_CC_IS_AMD(cc)) {
876 if (GGML_CUDA_CC_IS_RDNA(cc) && !ggml_cuda_should_use_mmf(type, cc, warp_size, src0_ne, src0_nb, ne11, /*mul_mat_id =*/ false)) {
877 return ne11 <= MMVF_MAX_BATCH_SIZE;
878 }
879 if (bf16_mma_hardware_available(cc)) {
880 return ne11 <= 3;
881 }
882 return ne11 <= MMVF_MAX_BATCH_SIZE;
883 }
884 return ne11 <= MMVF_MAX_BATCH_SIZE;
885 default:
886 return false;
887 }
888}