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

970 lines · 47017 bytes Raw Blame History
1#include "convert.cuh"
2#include "ggml-cuda/common.cuh"
3#include "ggml.h"
4#include "rope.cuh"
5
6struct rope_corr_dims {
7 float v[2];
8};
9
10
11struct mrope_sections {
12 int v[4];
13};
14
15static __device__ float rope_yarn_ramp(const float low, const float high, const int i0) {
16 const float y = (i0 / 2 - low) / max(0.001f, high - low);
17 return 1.0f - min(1.0f, max(0.0f, y));
18}
19
20// YaRN algorithm based on LlamaYaRNScaledRotaryEmbedding.py from https://github.com/jquesnelle/yarn
21// MIT licensed. Copyright (c) 2023 Jeffrey Quesnelle and Bowen Peng.
22template<bool forward>
23static __device__ void rope_yarn(
24 const float theta_extrap, const float freq_scale, const rope_corr_dims corr_dims, const int64_t i0, const float ext_factor,
25 float mscale, float & cos_theta, float & sin_theta) {
26 // Get n-d rotational scaling corrected for extrapolation
27 float theta_interp = freq_scale * theta_extrap;
28 float theta = theta_interp;
29 if (ext_factor != 0.0f) {
30 float ramp_mix = rope_yarn_ramp(corr_dims.v[0], corr_dims.v[1], i0) * ext_factor;
31 theta = theta_interp * (1 - ramp_mix) + theta_extrap * ramp_mix;
32
33 // Get n-d magnitude scaling corrected for interpolation
34 mscale *= 1.0f + 0.1f * logf(1.0f / freq_scale);
35 }
36 cos_theta = cosf(theta) * mscale;
37 sin_theta = sinf(theta) * mscale;
38 if (!forward) {
39 sin_theta *= -1.0f;
40 }
41}
42
43template <bool forward, bool has_ff, typename T, typename D>
44static __global__ void rope_norm(const T * x,
45 D * dst,
46 const int ne00,
47 const int ne01,
48 const int ne02,
49 const int s01,
50 const int s02,
51 const int s03,
52 const int s1,
53 const int s2,
54 const int s3,
55 const int n_dims,
56 const int n_offs,
57 const int32_t * pos,
58 const float freq_scale,
59 const float ext_factor,
60 const float attn_factor,
61 const rope_corr_dims corr_dims,
62 const float theta_scale,
63 const float * freq_factors,
64 const int64_t * row_indices,
65 const int set_rows_stride,
66 const bool inplace) {
67 const int i0 = 2*(blockDim.y*blockIdx.y + threadIdx.y);
68
69 if (i0 >= ne00) {
70 return;
71 }
72
73 const int row_dst = blockDim.x*blockIdx.x + threadIdx.x;
74
75 const uint32_t i3 = row_dst / (ne01 * ne02);
76 const uint32_t i2 = (row_dst - i3 * ne01 * ne02) / ne01;
77 const uint32_t i1 = row_dst - i3 * ne01 * ne02 - i2 * ne01;
78
79 int idst = i0 + i1 * s1 + i2 * s2 + i3 * s3;
80 const int ix = i0 + i1 * s01 + i2 * s02 + i3 * s03;
81 // Fusion optimization: ROPE + VIEW + SET_ROWS.
82 // The rope output is viewed as a 1D tensor and offset based on a row index in row_indices.
83 if (set_rows_stride != 0) {
84 idst = i1 * s1 + i0;
85 idst += row_indices[i2] * set_rows_stride;
86 }
87
88 const auto & store_coaelsced = [&](float x0, float x1) {
89 if constexpr (std::is_same_v<float, D>) {
90 float2 v = make_float2(x0, x1);
91 ggml_cuda_memcpy_1<8>(dst + idst, &v);
92 } else if constexpr (std::is_same_v<half, D>) {
93 half2 v = make_half2(x0, x1);
94 ggml_cuda_memcpy_1<4>(dst + idst, &v);
95 }
96 };
97 if (i0 < n_offs || i0 >= n_offs + n_dims) {
98 if (inplace) {
99 return;
100 }
101 store_coaelsced(x[ix + 0], x[ix + 1]);
102 return;
103 }
104
105 const int iw = i0 - n_offs; // relative idx
106
107 const float theta_base = pos[i2]*powf(theta_scale, iw/2.0f);
108
109 const float freq_factor = has_ff ? freq_factors[iw/2] : 1.0f;
110
111 float cos_theta;
112 float sin_theta;
113
114 rope_yarn<forward>(theta_base/freq_factor, freq_scale, corr_dims, iw, ext_factor, attn_factor, cos_theta, sin_theta);
115
116 const float x0 = x[ix + 0];
117 const float x1 = x[ix + 1];
118
119 store_coaelsced(x0 * cos_theta - x1 * sin_theta, x0 * sin_theta + x1 * cos_theta);
120}
121
122template <bool forward, bool has_ff, typename T, typename D>
123static __global__ void rope_neox(const T * x,
124 D * dst,
125 const int ne00,
126 const int ne01,
127 const int ne02,
128 const int s01,
129 const int s02,
130 const int s03,
131 const int s1,
132 const int s2,
133 const int s3,
134 const int n_dims,
135 const int n_offs,
136 const int32_t * pos,
137 const float freq_scale,
138 const float ext_factor,
139 const float attn_factor,
140 const rope_corr_dims corr_dims,
141 const float theta_scale,
142 const float * freq_factors,
143 const int64_t * row_indices,
144 const int set_rows_stride,
145 const bool inplace) {
146 ggml_cuda_pdl_lc();
147 const int i0 = 2*(blockDim.y*blockIdx.y + threadIdx.y);
148
149 if (i0 >= ne00) {
150 return;
151 }
152
153 const int row_dst = blockDim.x*blockIdx.x + threadIdx.x;
154
155 const uint32_t i3 = row_dst / (ne01 * ne02);
156 const uint32_t i2 = (row_dst - i3 * ne01 * ne02) / ne01;
157 const uint32_t i1 = row_dst - i3 * ne01 * ne02 - i2 * ne01;
158
159 int idst = i0 / 2 + i1 * s1 + i2 * s2 + i3 * s3;
160 const int ix = i0 / 2 + i1 * s01 + i2 * s02 + i3 * s03;
161 ggml_cuda_pdl_sync();
162
163 // Fusion optimization: ROPE + VIEW + SET_ROWS.
164 // The rope output is viewed as a 1D tensor and offset based on a row index in row_indices.
165 if (set_rows_stride != 0) {
166 idst = i1 * s1 + i0 / 2;
167 idst += row_indices[i2] * set_rows_stride;
168 }
169
170 if (i0 < n_offs || i0 >= n_offs + n_dims) {
171 if (inplace) {
172 return;
173 }
174 dst[idst + i0 / 2 + 0] = ggml_cuda_cast<D>(x[ix + i0 / 2 + 0]);
175 dst[idst + i0 / 2 + 1] = ggml_cuda_cast<D>(x[ix + i0 / 2 + 1]);
176
177 return;
178 }
179
180 const int iw = i0 - n_offs; // relative idx
181
182 const float theta_base = pos[i2]*powf(theta_scale, iw/2.0f);
183
184 const float freq_factor = has_ff ? freq_factors[iw/2] : 1.0f;
185
186 float cos_theta;
187 float sin_theta;
188
189 rope_yarn<forward>(theta_base/freq_factor, freq_scale, corr_dims, iw, ext_factor, attn_factor, cos_theta, sin_theta);
190
191 // idst/ix point at channel i0/2; the first channel of the rotated pair is n_offs + iw/2 = i0/2 + n_offs/2
192 const float x0 = x[ix + n_offs/2 + 0];
193 const float x1 = x[ix + n_offs/2 + n_dims/2];
194
195 dst[idst + n_offs/2 + 0] = ggml_cuda_cast<D>(x0 * cos_theta - x1 * sin_theta);
196 dst[idst + n_offs/2 + n_dims / 2] = ggml_cuda_cast<D>(x0 * sin_theta + x1 * cos_theta);
197}
198
199template <bool forward, bool has_ff, typename T, typename D>
200static __global__ void rope_multi(const T * x,
201 D * dst,
202 const int ne00,
203 const int ne01,
204 const int ne02,
205 const int s01,
206 const int s02,
207 const int s03,
208 const int s1,
209 const int s2,
210 const int s3,
211 const int n_dims,
212 const int n_offs,
213 const int32_t * pos,
214 const float freq_scale,
215 const float ext_factor,
216 const float attn_factor,
217 const rope_corr_dims corr_dims,
218 const float theta_scale,
219 const float * freq_factors,
220 const mrope_sections sections,
221 const bool is_imrope,
222 const int64_t * row_indices,
223 const int set_rows_stride,
224 const bool inplace) {
225 const int i0 = 2 * (blockDim.y * blockIdx.y + threadIdx.y);
226
227 if (i0 >= ne00) {
228 return;
229 }
230
231 const int row_dst = blockDim.x*blockIdx.x + threadIdx.x;
232
233 const uint32_t i3 = row_dst / (ne01 * ne02);
234 const uint32_t i2 = (row_dst - i3 * ne01 * ne02) / ne01;
235 const uint32_t i1 = row_dst - i3 * ne01 * ne02 - i2 * ne01;
236
237 int idst = i0 / 2 + i1 * s1 + i2 * s2 + i3 * s3;
238 const int ix = i0 / 2 + i1 * s01 + i2 * s02 + i3 * s03;
239
240 ggml_cuda_pdl_sync();
241
242 // Fusion optimization: ROPE + VIEW + SET_ROWS.
243 // The rope output is viewed as a 1D tensor and offset based on a row index in row_indices.
244 if (set_rows_stride != 0) {
245 idst = i1 * s1 + i0 / 2;
246 idst += row_indices[i2] * set_rows_stride;
247 }
248
249 if (i0 < n_offs || i0 >= n_offs + n_dims) {
250 if (inplace) {
251 return;
252 }
253 dst[idst + i0/2 + 0] = ggml_cuda_cast<D>(x[ix + i0/2 + 0]);
254 dst[idst + i0/2 + 1] = ggml_cuda_cast<D>(x[ix + i0/2 + 1]);
255
256 return;
257 }
258
259 const int iw = i0 - n_offs; // relative idx
260
261 const int sect_dims = sections.v[0] + sections.v[1] + sections.v[2] + sections.v[3];
262 const int sec_w = sections.v[1] + sections.v[0];
263 const int sector = (iw / 2) % sect_dims;
264
265 float theta_base = 0.0;
266 if (is_imrope) {
267 if (sector % 3 == 1 && sector < 3 * sections.v[1]) { // h
268 theta_base = pos[i2 + ne02 * 1] * powf(theta_scale, iw / 2.0f);
269 } else if (sector % 3 == 2 && sector < 3 * sections.v[2]) { // w
270 theta_base = pos[i2 + ne02 * 2] * powf(theta_scale, iw / 2.0f);
271 } else if (sector % 3 == 0 && sector < 3 * sections.v[0]) { // t
272 theta_base = pos[i2] * powf(theta_scale, iw / 2.0f);
273 } else {
274 theta_base = pos[i2 + ne02 * 3] * powf(theta_scale, iw / 2.0f);
275 }
276 } else {
277 if (sector < sections.v[0]) {
278 theta_base = pos[i2] * powf(theta_scale, iw / 2.0f);
279 } else if (sector >= sections.v[0] && sector < sec_w) {
280 theta_base = pos[i2 + ne02 * 1] * powf(theta_scale, iw / 2.0f);
281 } else if (sector >= sec_w && sector < sec_w + sections.v[2]) {
282 theta_base = pos[i2 + ne02 * 2] * powf(theta_scale, iw / 2.0f);
283 } else if (sector >= sec_w + sections.v[2]) {
284 theta_base = pos[i2 + ne02 * 3] * powf(theta_scale, iw / 2.0f);
285 }
286 }
287
288 const float freq_factor = has_ff ? freq_factors[iw/2] : 1.0f;
289
290 float cos_theta;
291 float sin_theta;
292
293 rope_yarn<forward>(theta_base/freq_factor, freq_scale, corr_dims, iw, ext_factor, attn_factor, cos_theta, sin_theta);
294
295 // idst/ix point at channel i0/2; the first channel of the rotated pair is n_offs + iw/2 = i0/2 + n_offs/2
296 const float x0 = x[ix + n_offs/2 + 0];
297 const float x1 = x[ix + n_offs/2 + n_dims/2];
298
299 dst[idst + n_offs/2 + 0] = ggml_cuda_cast<D>(x0*cos_theta - x1*sin_theta);
300 dst[idst + n_offs/2 + n_dims/2] = ggml_cuda_cast<D>(x0*sin_theta + x1*cos_theta);
301}
302
303template <bool forward, bool has_ff, typename T>
304static __global__ void rope_vision(const T * x,
305 T * dst,
306 const int ne00,
307 const int ne01,
308 const int ne02,
309 const int s01,
310 const int s02,
311 const int s03,
312 const int s1,
313 const int s2,
314 const int s3,
315 const int n_dims,
316 const int32_t * pos,
317 const float freq_scale,
318 const float ext_factor,
319 const float attn_factor,
320 const rope_corr_dims corr_dims,
321 const float theta_scale,
322 const float * freq_factors,
323 const mrope_sections sections) {
324 const int i0 = 2*(blockDim.y*blockIdx.y + threadIdx.y);
325
326 if (i0 >= ne00) {
327 return;
328 }
329
330 const int row_dst = blockDim.x*blockIdx.x + threadIdx.x;
331
332 const uint32_t i3 = row_dst / (ne01 * ne02);
333 const uint32_t i2 = (row_dst - i3 * ne01 * ne02) / ne01;
334 const uint32_t i1 = row_dst - i3 * ne01 * ne02 - i2 * ne01;
335
336 int idst = i0 / 2 + i1 * s1 + i2 * s2 + i3 * s3;
337 const int ix = i0 / 2 + i1 * s01 + i2 * s02 + i3 * s03;
338
339 ggml_cuda_pdl_sync();
340 const int sect_dims = sections.v[0] + sections.v[1];
341 const int sec_w = sections.v[1] + sections.v[0];
342 const int sector = (i0 / 2) % sect_dims;
343
344 float theta_base = 0.0;
345 if (sector < sections.v[0]) {
346 const int p = sector;
347 theta_base = pos[i2] * powf(theta_scale, p);
348 } else if (sector >= sections.v[0] && sector < sec_w) {
349 const int p = sector - sections.v[0];
350 theta_base = pos[i2 + ne02] * powf(theta_scale, p);
351 }
352
353 const float freq_factor = has_ff ? freq_factors[i0/2] : 1.0f;
354
355 float cos_theta;
356 float sin_theta;
357
358 rope_yarn<forward>(theta_base/freq_factor, freq_scale, corr_dims, i0, ext_factor, attn_factor, cos_theta, sin_theta);
359
360 const float x0 = x[ix + 0];
361 const float x1 = x[ix + n_dims];
362
363 dst[idst + 0] = x0*cos_theta - x1*sin_theta;
364 dst[idst + n_dims] = x0*sin_theta + x1*cos_theta;
365}
366
367template <bool forward, typename T, typename D>
368static void rope_norm_cuda(const T * x,
369 D * dst,
370 const int ne00,
371 const int ne01,
372 const int ne02,
373 const int s01,
374 const int s02,
375 const int s03,
376 const int s1,
377 const int s2,
378 const int s3,
379 const int n_dims,
380 const int n_offs,
381 const int nr,
382 const int32_t * pos,
383 const float freq_scale,
384 const float freq_base,
385 const float ext_factor,
386 const float attn_factor,
387 const rope_corr_dims corr_dims,
388 const float * freq_factors,
389 const int64_t * row_indices,
390 const int set_rows_stride,
391 const bool inplace,
392 cudaStream_t stream) {
393 GGML_ASSERT(ne00 % 2 == 0);
394 const dim3 block_dims(1, CUDA_ROPE_BLOCK_SIZE, 1);
395 const int n_blocks_x = (ne00 + 2 * CUDA_ROPE_BLOCK_SIZE - 1) / (2 * CUDA_ROPE_BLOCK_SIZE);
396 const dim3 block_nums(nr, n_blocks_x, 1);
397
398 const float theta_scale = powf(freq_base, -2.0f / n_dims);
399
400 if (freq_factors == nullptr) {
401 rope_norm<forward, false><<<block_nums, block_dims, 0, stream>>>(
402 x, dst, ne00, ne01, ne02, s01, s02, s03, s1, s2, s3, n_dims, n_offs, pos, freq_scale, ext_factor,
403 attn_factor, corr_dims, theta_scale, freq_factors, row_indices, set_rows_stride, inplace);
404 } else {
405 rope_norm<forward, true><<<block_nums, block_dims, 0, stream>>>(
406 x, dst, ne00, ne01, ne02, s01, s02, s03, s1, s2, s3, n_dims, n_offs, pos, freq_scale, ext_factor,
407 attn_factor, corr_dims, theta_scale, freq_factors, row_indices, set_rows_stride, inplace);
408 }
409}
410
411template <bool forward, typename T, typename D>
412static void rope_neox_cuda(const T * x,
413 D * dst,
414 const int ne00,
415 const int ne01,
416 const int ne02,
417 const int s01,
418 const int s02,
419 const int s03,
420 const int s1,
421 const int s2,
422 const int s3,
423 const int n_dims,
424 const int n_offs,
425 const int nr,
426 const int32_t * pos,
427 const float freq_scale,
428 const float freq_base,
429 const float ext_factor,
430 const float attn_factor,
431 const rope_corr_dims corr_dims,
432 const float * freq_factors,
433 const int64_t * row_indices,
434 const int set_rows_stride,
435 const bool inplace,
436 cudaStream_t stream) {
437 GGML_ASSERT(ne00 % 2 == 0);
438 const dim3 block_dims(1, CUDA_ROPE_BLOCK_SIZE, 1);
439 const int n_blocks_x = (ne00 + 2 * CUDA_ROPE_BLOCK_SIZE - 1) / (2 * CUDA_ROPE_BLOCK_SIZE);
440 const dim3 block_nums(nr, n_blocks_x, 1);
441
442 const float theta_scale = powf(freq_base, -2.0f / n_dims);
443 const ggml_cuda_kernel_launch_params launch_params = {block_nums, block_dims, 0, stream};
444
445 if (freq_factors == nullptr) {
446 ggml_cuda_kernel_launch(rope_neox<forward, false, T, D>, launch_params,
447 x, dst, ne00, ne01, ne02, s01, s02, s03, s1, s2, s3, n_dims, n_offs, pos, freq_scale, ext_factor,
448 attn_factor, corr_dims, theta_scale, freq_factors, row_indices, set_rows_stride, inplace);
449 } else {
450 ggml_cuda_kernel_launch(rope_neox<forward, true, T, D>, launch_params,
451 x, dst, ne00, ne01, ne02, s01, s02, s03, s1, s2, s3, n_dims, n_offs, pos, freq_scale, ext_factor,
452 attn_factor, corr_dims, theta_scale, freq_factors, row_indices, set_rows_stride, inplace);
453 }
454}
455
456template <bool forward, typename T, typename D>
457static void rope_multi_cuda(const T * x,
458 D * dst,
459 const int ne00,
460 const int ne01,
461 const int ne02,
462 const int s01,
463 const int s02,
464 const int s03,
465 const int s1,
466 const int s2,
467 const int s3,
468 const int n_dims,
469 const int n_offs,
470 const int nr,
471 const int32_t * pos,
472 const float freq_scale,
473 const float freq_base,
474 const float ext_factor,
475 const float attn_factor,
476 const rope_corr_dims corr_dims,
477 const float * freq_factors,
478 const mrope_sections sections,
479 const bool is_imrope,
480 const int64_t * row_indices,
481 const int set_rows_stride,
482 const bool inplace,
483 cudaStream_t stream) {
484 GGML_ASSERT(ne00 % 2 == 0);
485 const dim3 block_dims(1, CUDA_ROPE_BLOCK_SIZE, 1);
486 const int n_blocks_x = (ne00 + 2 * CUDA_ROPE_BLOCK_SIZE - 1) / (2 * CUDA_ROPE_BLOCK_SIZE);
487 const dim3 block_nums(nr, n_blocks_x, 1);
488
489 const float theta_scale = powf(freq_base, -2.0f / n_dims);
490
491 if (freq_factors == nullptr) {
492 const ggml_cuda_kernel_launch_params launch_params = ggml_cuda_kernel_launch_params(block_nums, block_dims, 0, stream);
493 ggml_cuda_kernel_launch(rope_multi<forward, false, T, D>, launch_params,
494 x, dst, ne00, ne01, ne02, s01, s02, s03, s1, s2, s3, n_dims, n_offs, pos, freq_scale, ext_factor,
495 attn_factor, corr_dims, theta_scale, freq_factors, sections, is_imrope, row_indices, set_rows_stride, inplace);
496 } else {
497 const ggml_cuda_kernel_launch_params launch_params = ggml_cuda_kernel_launch_params(block_nums, block_dims, 0, stream);
498 ggml_cuda_kernel_launch(rope_multi<forward, true, T, D>, launch_params,
499 x, dst, ne00, ne01, ne02, s01, s02, s03, s1, s2, s3, n_dims, n_offs, pos, freq_scale, ext_factor,
500 attn_factor, corr_dims, theta_scale, freq_factors, sections, is_imrope, row_indices, set_rows_stride, inplace);
501 }
502}
503
504template <bool forward, typename T>
505static void rope_vision_cuda(const T * x,
506 T * dst,
507 const int ne00,
508 const int ne01,
509 const int ne02,
510 const int s01,
511 const int s02,
512 const int s03,
513 const int s1,
514 const int s2,
515 const int s3,
516 const int n_dims,
517 const int nr,
518 const int32_t * pos,
519 const float freq_scale,
520 const float freq_base,
521 const float ext_factor,
522 const float attn_factor,
523 const rope_corr_dims corr_dims,
524 const float * freq_factors,
525 const mrope_sections sections,
526 cudaStream_t stream) {
527 GGML_ASSERT(ne00 % 2 == 0);
528 const dim3 block_dims(1, CUDA_ROPE_BLOCK_SIZE, 1);
529 const int n_blocks_x = (ne00 + 2 * CUDA_ROPE_BLOCK_SIZE - 1) / (2 * CUDA_ROPE_BLOCK_SIZE);
530 const dim3 block_nums(nr, n_blocks_x, 1);
531 // break down (head_dim, heads, seq) into (CUDA_ROPE_BLOCK_SIZE, x, heads * seq)
532 // where x ~= ceil(head_dim / CUDA_ROPE_BLOCK_SIZE);
533
534 const float theta_scale = powf(freq_base, -2.0f/n_dims);
535
536 if (freq_factors == nullptr) {
537 rope_vision<forward, false, T><<<block_nums, block_dims, 0, stream>>>(
538 x, dst, ne00, ne01, ne02, s01, s02, s03, s1, s2, s3, n_dims, pos, freq_scale, ext_factor,
539 attn_factor, corr_dims, theta_scale, freq_factors, sections);
540 } else {
541 rope_vision<forward, true, T><<<block_nums, block_dims, 0, stream>>>(
542 x, dst, ne00, ne01, ne02, s01, s02, s03, s1, s2, s3, n_dims, pos, freq_scale, ext_factor,
543 attn_factor, corr_dims, theta_scale, freq_factors, sections);
544 }
545}
546
547template <bool forward>
548void ggml_cuda_op_rope_impl(ggml_backend_cuda_context & ctx,
549 ggml_tensor * dst,
550 const ggml_tensor * set_rows = nullptr) {
551 const ggml_tensor * src0 = dst->src[0];
552 const ggml_tensor * src1 = dst->src[1];
553 const ggml_tensor * src2 = dst->src[2];
554
555 const float * src0_d = (const float *)src0->data;
556 const float * src1_d = (const float *)src1->data;
557
558 void * dst_d = dst->data;
559 const int64_t * row_indices = nullptr;
560 ggml_type dst_type = dst->type;
561 int set_rows_stride = 0;
562
563 if (set_rows != nullptr) {
564 GGML_ASSERT(forward);
565 dst_d = set_rows->data;
566 row_indices = (const int64_t *) set_rows->src[1]->data;
567 dst_type = set_rows->type;
568 set_rows_stride = set_rows->nb[1] / ggml_type_size(set_rows->type);
569 }
570 cudaStream_t stream = ctx.stream();
571
572 GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16);
573 GGML_ASSERT( dst->type == GGML_TYPE_F32 || dst->type == GGML_TYPE_F16);
574 // When not fused, src0 and dst types must match
575 // When fused (ROPE+VIEW+SET_ROWS), src0 may be F32 and dst may be F16
576 GGML_ASSERT(src0->type == dst->type || (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F16));
577
578 const int64_t ne00 = src0->ne[0]; // head dims
579 const int64_t ne01 = src0->ne[1]; // num heads
580 const int64_t ne02 = src0->ne[2]; // num heads
581 const int64_t nr = ggml_nrows(src0);
582
583 const size_t s01 = src0->nb[1] / ggml_type_size(src0->type);
584 const size_t s02 = src0->nb[2] / ggml_type_size(src0->type);
585 const size_t s03 = src0->nb[3] / ggml_type_size(src0->type);
586
587 const size_t s1 = dst->nb[1] / ggml_type_size(dst->type);
588 const size_t s2 = dst->nb[2] / ggml_type_size(dst->type);
589 const size_t s3 = dst->nb[3] / ggml_type_size(dst->type);
590
591 //const int n_past = ((int32_t *) dst->op_params)[0];
592 const int n_dims = ((int32_t *) dst->op_params)[1];
593 const int mode = ((int32_t *) dst->op_params)[2];
594 //const int n_ctx = ((int32_t *) dst->op_params)[3];
595 const int n_ctx_orig = ((int32_t *) dst->op_params)[4];
596 const int n_offs = ((int32_t *) dst->op_params)[15];
597 mrope_sections sections;
598
599 // when dst aliases src0, the channels outside the rotated window already hold the correct data
600 const bool inplace = dst_d == src0->data;
601
602 // RoPE alteration for extended context
603 float freq_base;
604 float freq_scale;
605 float ext_factor;
606 float attn_factor;
607 float beta_fast;
608 float beta_slow;
609
610 memcpy(&freq_base, (int32_t *) dst->op_params + 5, sizeof(float));
611 memcpy(&freq_scale, (int32_t *) dst->op_params + 6, sizeof(float));
612 memcpy(&ext_factor, (int32_t *) dst->op_params + 7, sizeof(float));
613 memcpy(&attn_factor, (int32_t *) dst->op_params + 8, sizeof(float));
614 memcpy(&beta_fast, (int32_t *) dst->op_params + 9, sizeof(float));
615 memcpy(&beta_slow, (int32_t *) dst->op_params + 10, sizeof(float));
616 memcpy(&sections.v, (int32_t *) dst->op_params + 11, sizeof(int)*4);
617
618 const bool is_neox = mode & GGML_ROPE_TYPE_NEOX;
619 const bool is_mrope = mode & GGML_ROPE_TYPE_MROPE;
620 const bool is_imrope = mode == GGML_ROPE_TYPE_IMROPE;
621 const bool is_vision = mode == GGML_ROPE_TYPE_VISION;
622
623 if (is_mrope) {
624 GGML_ASSERT(sections.v[0] > 0 || sections.v[1] > 0 || sections.v[2] > 0);
625 }
626
627 if (is_vision) {
628 GGML_ASSERT(n_dims == ne00/2);
629 GGML_ASSERT(n_offs == 0); // offset not supported for vision, as the rotated pairs span the whole row
630 }
631
632 const int32_t * pos = (const int32_t *) src1_d;
633
634 const float * freq_factors = nullptr;
635 if (src2 != nullptr) {
636 freq_factors = (const float *) src2->data;
637 }
638
639 rope_corr_dims corr_dims;
640 ggml_rope_yarn_corr_dims(n_dims, n_ctx_orig, freq_base, beta_fast, beta_slow, corr_dims.v);
641
642 // compute
643 if (is_neox) {
644 if (src0->type == GGML_TYPE_F32 && dst_type == GGML_TYPE_F32) {
645 rope_neox_cuda<forward, float, float>((const float *) src0_d, (float *) dst_d, ne00, ne01, ne02, s01, s02,
646 s03, s1, s2, s3, n_dims, n_offs, nr, pos, freq_scale, freq_base,
647 ext_factor, attn_factor, corr_dims, freq_factors, row_indices,
648 set_rows_stride, inplace, stream);
649 } else if (src0->type == GGML_TYPE_F32 && dst_type == GGML_TYPE_F16) {
650 rope_neox_cuda<forward, float, half>((const float *) src0_d, (half *) dst_d, ne00, ne01, ne02, s01, s02,
651 s03, s1, s2, s3, n_dims, n_offs, nr, pos, freq_scale, freq_base,
652 ext_factor, attn_factor, corr_dims, freq_factors, row_indices,
653 set_rows_stride, inplace, stream);
654 } else if (src0->type == GGML_TYPE_F16 && dst_type == GGML_TYPE_F16) {
655 rope_neox_cuda<forward, half, half>((const half *) src0_d, (half *) dst_d, ne00, ne01, ne02, s01, s02,
656 s03, s1, s2, s3, n_dims, n_offs, nr, pos, freq_scale, freq_base,
657 ext_factor, attn_factor, corr_dims, freq_factors, row_indices,
658 set_rows_stride, inplace, stream);
659 } else {
660 GGML_ABORT("fatal error");
661 }
662 } else if (is_mrope && !is_vision) {
663 if (src0->type == GGML_TYPE_F32 && dst_type == GGML_TYPE_F32) {
664 rope_multi_cuda<forward, float, float>((const float *) src0_d, (float *) dst_d, ne00, ne01, ne02, s01, s02,
665 s03, s1, s2, s3, n_dims, n_offs, nr, pos, freq_scale, freq_base,
666 ext_factor, attn_factor, corr_dims, freq_factors, sections, is_imrope,
667 row_indices, set_rows_stride, inplace, stream);
668 } else if (src0->type == GGML_TYPE_F32 && dst_type == GGML_TYPE_F16) {
669 rope_multi_cuda<forward, float, half>((const float *) src0_d, (half *) dst_d, ne00, ne01, ne02, s01, s02,
670 s03, s1, s2, s3, n_dims, n_offs, nr, pos, freq_scale, freq_base,
671 ext_factor, attn_factor, corr_dims, freq_factors, sections, is_imrope,
672 row_indices, set_rows_stride, inplace, stream);
673 } else if (src0->type == GGML_TYPE_F16 && dst_type == GGML_TYPE_F16) {
674 rope_multi_cuda<forward, half, half>((const half *) src0_d, (half *) dst_d, ne00, ne01, ne02, s01, s02,
675 s03, s1, s2, s3, n_dims, n_offs, nr, pos, freq_scale, freq_base,
676 ext_factor, attn_factor, corr_dims, freq_factors, sections, is_imrope,
677 row_indices, set_rows_stride, inplace, stream);
678 } else {
679 GGML_ABORT("fatal error");
680 }
681 } else if (is_vision) {
682 if (src0->type == GGML_TYPE_F32) {
683 rope_vision_cuda<forward>((const float *) src0_d, (float *) dst_d, ne00, ne01, ne02, s01, s02, s03, s1,
684 s2, s3, n_dims, nr, pos, freq_scale, freq_base, ext_factor, attn_factor,
685 corr_dims, freq_factors, sections, stream);
686 } else if (src0->type == GGML_TYPE_F16) {
687 rope_vision_cuda<forward>((const half *) src0_d, (half *) dst_d, ne00, ne01, ne02, s01, s02, s03, s1,
688 s2, s3, n_dims, nr, pos, freq_scale, freq_base, ext_factor, attn_factor,
689 corr_dims, freq_factors, sections, stream);
690 } else {
691 GGML_ABORT("fatal error");
692 }
693 } else {
694 if (src0->type == GGML_TYPE_F32 && dst_type == GGML_TYPE_F32) {
695 rope_norm_cuda<forward, float, float>((const float *) src0_d, (float *) dst_d, ne00, ne01, ne02, s01, s02,
696 s03, s1, s2, s3, n_dims, n_offs, nr, pos, freq_scale, freq_base,
697 ext_factor, attn_factor, corr_dims, freq_factors, row_indices,
698 set_rows_stride, inplace, stream);
699 } else if (src0->type == GGML_TYPE_F32 && dst_type == GGML_TYPE_F16) {
700 rope_norm_cuda<forward, float, half>((const float *) src0_d, (half *) dst_d, ne00, ne01, ne02, s01, s02,
701 s03, s1, s2, s3, n_dims, n_offs, nr, pos, freq_scale, freq_base,
702 ext_factor, attn_factor, corr_dims, freq_factors, row_indices,
703 set_rows_stride, inplace, stream);
704 } else if (src0->type == GGML_TYPE_F16 && dst_type == GGML_TYPE_F16) {
705 rope_norm_cuda<forward, half, half>((const half *) src0_d, (half *) dst_d, ne00, ne01, ne02, s01, s02,
706 s03, s1, s2, s3, n_dims, n_offs, nr, pos, freq_scale, freq_base,
707 ext_factor, attn_factor, corr_dims, freq_factors, row_indices,
708 set_rows_stride, inplace, stream);
709 } else {
710 GGML_ABORT("fatal error");
711 }
712 }
713}
714
715void ggml_cuda_op_rope(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
716 ggml_cuda_op_rope_impl<true>(ctx, dst);
717}
718
719void ggml_cuda_op_rope_back(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
720 ggml_cuda_op_rope_impl<false>(ctx, dst);
721}
722
723void ggml_cuda_op_rope_fused(ggml_backend_cuda_context & ctx, ggml_tensor * rope, ggml_tensor * set_rows) {
724 ggml_cuda_op_rope_impl<true>(ctx, rope, set_rows);
725}
726
727// fused RMS_NORM + MUL + ROPE (+ VIEW + SET_ROWS)
728// one block per row: block_reduce gives the norm scale, then each thread applies mul and rope to the elements it owns
729template <int block_size, bool has_ff, typename D>
730static __global__ void rms_norm_mul_rope_f32(
731 const float * x, D * dst, const int ncols, const int nchannels, const int nsamples,
732 const int64_t s01, const int64_t s02, const int64_t s03,
733 const int64_t s1, const int64_t s2, const int64_t s3,
734 const float eps,
735 const float * mul,
736 const int64_t mul_s01, const int64_t mul_s02, const int64_t mul_s03,
737 const uint3 mul_ncols_packed, const uint3 mul_nrows_packed,
738 const uint3 mul_nchannels_packed, const uint3 mul_nsamples_packed,
739 const int n_dims, const int32_t * pos,
740 const float freq_scale, const float ext_factor, const float attn_factor,
741 const rope_corr_dims corr_dims, const float theta_scale,
742 const float * freq_factors,
743 const int64_t * row_indices, const int set_rows_stride,
744 const bool is_neox) {
745 ggml_cuda_pdl_lc();
746 const int row = blockIdx.x;
747 const int tid = threadIdx.x;
748
749 extern __shared__ float s_sum[];
750
751 ggml_cuda_pdl_sync();
752
753 // grid.y and grid.z are clamped to the CUDA limit, iterate over the excess channels/samples
754 for (int sample = blockIdx.z; sample < nsamples; sample += gridDim.z) {
755 for (int channel = blockIdx.y; channel < nchannels; channel += gridDim.y) {
756 const float * xc = x + sample*s03 + channel*s02 + row*s01;
757
758 const uint32_t mul_row = fastmodulo(row, mul_nrows_packed);
759 const uint32_t mul_channel = fastmodulo(channel, mul_nchannels_packed);
760 const uint32_t mul_sample = fastmodulo(sample, mul_nsamples_packed);
761 const float * mulc = mul + mul_sample*mul_s03 + mul_channel*mul_s02 + mul_row*mul_s01;
762
763 float tmp = 0.0f;
764
765 for (int col = tid; col < ncols; col += block_size) {
766 const float xi = xc[col];
767 tmp += xi * xi;
768 }
769
770 tmp = block_reduce<block_reduce_method::SUM, block_size>(tmp, s_sum);
771
772 const float scale = rsqrtf(tmp/ncols + eps);
773
774 int64_t idst = sample*s3 + channel*s2 + row*s1;
775 if (set_rows_stride != 0) {
776 idst = row*s1 + row_indices[channel]*set_rows_stride;
777 }
778 D * dstc = dst + idst;
779
780 for (int i0 = 2*tid; i0 < ncols; i0 += 2*block_size) {
781 int ix0;
782 int ix1;
783 if (is_neox && i0 < n_dims) {
784 ix0 = i0/2;
785 ix1 = i0/2 + n_dims/2;
786 } else {
787 ix0 = i0 + 0;
788 ix1 = i0 + 1;
789 }
790
791 const float x0 = scale * xc[ix0] * mulc[fastmodulo(ix0, mul_ncols_packed)];
792 const float x1 = scale * xc[ix1] * mulc[fastmodulo(ix1, mul_ncols_packed)];
793
794 if (i0 >= n_dims) {
795 dstc[ix0] = ggml_cuda_cast<D>(x0);
796 dstc[ix1] = ggml_cuda_cast<D>(x1);
797 continue;
798 }
799
800 const float theta_base = pos[channel]*powf(theta_scale, i0/2.0f);
801 const float freq_factor = has_ff ? freq_factors[i0/2] : 1.0f;
802
803 float cos_theta;
804 float sin_theta;
805 rope_yarn<true>(theta_base/freq_factor, freq_scale, corr_dims, i0, ext_factor, attn_factor, cos_theta, sin_theta);
806
807 dstc[ix0] = ggml_cuda_cast<D>(x0*cos_theta - x1*sin_theta);
808 dstc[ix1] = ggml_cuda_cast<D>(x0*sin_theta + x1*cos_theta);
809 }
810
811 if constexpr (block_size > WARP_SIZE) {
812 // sync is needed as we reuse s_sum across block_reduce invocations, see #26385
813 __syncthreads();
814 }
815 }
816 }
817}
818
819template <typename D>
820static void rms_norm_mul_rope_cuda(
821 const float * x, D * dst,
822 const int ncols, const int nrows, const int nchannels, const int nsamples,
823 const int64_t s01, const int64_t s02, const int64_t s03,
824 const int64_t s1, const int64_t s2, const int64_t s3,
825 const float eps,
826 const float * mul,
827 const int64_t mul_s01, const int64_t mul_s02, const int64_t mul_s03,
828 const uint32_t mul_ncols, const uint32_t mul_nrows,
829 const uint32_t mul_nchannels, const uint32_t mul_nsamples,
830 const int n_dims, const int32_t * pos,
831 const float freq_scale, const float freq_base, const float ext_factor, const float attn_factor,
832 const rope_corr_dims corr_dims,
833 const float * freq_factors,
834 const int64_t * row_indices, const int set_rows_stride,
835 const bool is_neox, cudaStream_t stream) {
836 GGML_ASSERT(ncols % 2 == 0);
837
838 const dim3 blocks_num(nrows, MIN(nchannels, UINT16_MAX), MIN(nsamples, UINT16_MAX));
839
840 const float theta_scale = powf(freq_base, -2.0f/n_dims);
841
842 const uint3 mul_ncols_packed = init_fastdiv_values(mul_ncols);
843 const uint3 mul_nrows_packed = init_fastdiv_values(mul_nrows);
844 const uint3 mul_nchannels_packed = init_fastdiv_values(mul_nchannels);
845 const uint3 mul_nsamples_packed = init_fastdiv_values(mul_nsamples);
846
847 if (ncols < 1024) {
848 const dim3 block_dims(256, 1, 1);
849 const ggml_cuda_kernel_launch_params launch_params = {blocks_num, block_dims, 32*sizeof(float), stream};
850 if (freq_factors == nullptr) {
851 ggml_cuda_kernel_launch(rms_norm_mul_rope_f32<256, false, D>, launch_params,
852 x, dst, ncols, nchannels, nsamples, s01, s02, s03, s1, s2, s3, eps, mul, mul_s01, mul_s02, mul_s03,
853 mul_ncols_packed, mul_nrows_packed, mul_nchannels_packed, mul_nsamples_packed,
854 n_dims, pos, freq_scale, ext_factor, attn_factor, corr_dims, theta_scale,
855 freq_factors, row_indices, set_rows_stride, is_neox);
856 } else {
857 ggml_cuda_kernel_launch(rms_norm_mul_rope_f32<256, true, D>, launch_params,
858 x, dst, ncols, nchannels, nsamples, s01, s02, s03, s1, s2, s3, eps, mul, mul_s01, mul_s02, mul_s03,
859 mul_ncols_packed, mul_nrows_packed, mul_nchannels_packed, mul_nsamples_packed,
860 n_dims, pos, freq_scale, ext_factor, attn_factor, corr_dims, theta_scale,
861 freq_factors, row_indices, set_rows_stride, is_neox);
862 }
863 } else {
864 const dim3 block_dims(1024, 1, 1);
865 const ggml_cuda_kernel_launch_params launch_params = {blocks_num, block_dims, 32*sizeof(float), stream};
866 if (freq_factors == nullptr) {
867 ggml_cuda_kernel_launch(rms_norm_mul_rope_f32<1024, false, D>, launch_params,
868 x, dst, ncols, nchannels, nsamples, s01, s02, s03, s1, s2, s3, eps, mul, mul_s01, mul_s02, mul_s03,
869 mul_ncols_packed, mul_nrows_packed, mul_nchannels_packed, mul_nsamples_packed,
870 n_dims, pos, freq_scale, ext_factor, attn_factor, corr_dims, theta_scale,
871 freq_factors, row_indices, set_rows_stride, is_neox);
872 } else {
873 ggml_cuda_kernel_launch(rms_norm_mul_rope_f32<1024, true, D>, launch_params,
874 x, dst, ncols, nchannels, nsamples, s01, s02, s03, s1, s2, s3, eps, mul, mul_s01, mul_s02, mul_s03,
875 mul_ncols_packed, mul_nrows_packed, mul_nchannels_packed, mul_nsamples_packed,
876 n_dims, pos, freq_scale, ext_factor, attn_factor, corr_dims, theta_scale,
877 freq_factors, row_indices, set_rows_stride, is_neox);
878 }
879 }
880}
881
882void ggml_cuda_op_rms_norm_mul_rope_fused(ggml_backend_cuda_context & ctx,
883 ggml_tensor * rms_norm, ggml_tensor * mul, ggml_tensor * rope, ggml_tensor * set_rows) {
884 const ggml_tensor * x = rms_norm->src[0];
885 const ggml_tensor * mul_src = mul->src[0] == rms_norm ? mul->src[1] : mul->src[0];
886
887 float eps = 0.0f;
888 memcpy(&eps, rms_norm->op_params, sizeof(float));
889 GGML_ASSERT(eps >= 0.0f);
890
891 GGML_ASSERT(x->type == GGML_TYPE_F32);
892 GGML_ASSERT(mul_src->type == GGML_TYPE_F32);
893 GGML_ASSERT(rope->type == GGML_TYPE_F32);
894
895 void * dst_d = rope->data;
896 ggml_type dst_type = rope->type;
897 const int64_t * row_indices = nullptr;
898 int set_rows_stride = 0;
899
900 if (set_rows != nullptr) {
901 dst_d = set_rows->data;
902 dst_type = set_rows->type;
903 row_indices = (const int64_t *) set_rows->src[1]->data;
904 set_rows_stride = set_rows->nb[1] / ggml_type_size(set_rows->type);
905 }
906
907 const int n_dims = ((const int32_t *) rope->op_params)[1];
908 const int mode = ((const int32_t *) rope->op_params)[2];
909 const int n_ctx_orig = ((const int32_t *) rope->op_params)[4];
910
911 float freq_base;
912 float freq_scale;
913 float ext_factor;
914 float attn_factor;
915 float beta_fast;
916 float beta_slow;
917
918 memcpy(&freq_base, (const int32_t *) rope->op_params + 5, sizeof(float));
919 memcpy(&freq_scale, (const int32_t *) rope->op_params + 6, sizeof(float));
920 memcpy(&ext_factor, (const int32_t *) rope->op_params + 7, sizeof(float));
921 memcpy(&attn_factor, (const int32_t *) rope->op_params + 8, sizeof(float));
922 memcpy(&beta_fast, (const int32_t *) rope->op_params + 9, sizeof(float));
923 memcpy(&beta_slow, (const int32_t *) rope->op_params + 10, sizeof(float));
924
925 const bool is_neox = mode & GGML_ROPE_TYPE_NEOX;
926
927 const int32_t * pos = (const int32_t *) rope->src[1]->data;
928
929 const float * freq_factors = rope->src[2] != nullptr ? (const float *) rope->src[2]->data : nullptr;
930
931 rope_corr_dims corr_dims;
932 ggml_rope_yarn_corr_dims(n_dims, n_ctx_orig, freq_base, beta_fast, beta_slow, corr_dims.v);
933
934 const size_t ts0 = ggml_type_size(x->type);
935 GGML_ASSERT(x->nb[0] == ts0);
936 const int64_t s01 = x->nb[1] / ts0;
937 const int64_t s02 = x->nb[2] / ts0;
938 const int64_t s03 = x->nb[3] / ts0;
939
940 const size_t ts_mul = ggml_type_size(mul_src->type);
941 GGML_ASSERT(mul_src->nb[0] == ts_mul);
942 const int64_t mul_s01 = mul_src->nb[1] / ts_mul;
943 const int64_t mul_s02 = mul_src->nb[2] / ts_mul;
944 const int64_t mul_s03 = mul_src->nb[3] / ts_mul;
945
946 const size_t ts_dst = ggml_type_size(rope->type);
947 const int64_t s1 = rope->nb[1] / ts_dst;
948 const int64_t s2 = rope->nb[2] / ts_dst;
949 const int64_t s3 = rope->nb[3] / ts_dst;
950
951 cudaStream_t stream = ctx.stream();
952
953 if (dst_type == GGML_TYPE_F32) {
954 rms_norm_mul_rope_cuda((const float *) x->data, (float *) dst_d,
955 x->ne[0], x->ne[1], x->ne[2], x->ne[3], s01, s02, s03, s1, s2, s3, eps,
956 (const float *) mul_src->data, mul_s01, mul_s02, mul_s03,
957 mul_src->ne[0], mul_src->ne[1], mul_src->ne[2], mul_src->ne[3],
958 n_dims, pos, freq_scale, freq_base, ext_factor, attn_factor, corr_dims,
959 freq_factors, row_indices, set_rows_stride, is_neox, stream);
960 } else if (dst_type == GGML_TYPE_F16) {
961 rms_norm_mul_rope_cuda((const float *) x->data, (half *) dst_d,
962 x->ne[0], x->ne[1], x->ne[2], x->ne[3], s01, s02, s03, s1, s2, s3, eps,
963 (const float *) mul_src->data, mul_s01, mul_s02, mul_s03,
964 mul_src->ne[0], mul_src->ne[1], mul_src->ne[2], mul_src->ne[3],
965 n_dims, pos, freq_scale, freq_base, ext_factor, attn_factor, corr_dims,
966 freq_factors, row_indices, set_rows_stride, is_neox, stream);
967 } else {
968 GGML_ABORT("fatal error");
969 }
970}