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

825 lines · 35982 bytes Raw Blame History
1#include "norm.cuh"
2#include <cstdint>
3
4template <int block_size>
5static __global__ void norm_f32(
6 const float * x, float * dst, const int ncols, const int nchannels, const int nsamples,
7 const int64_t stride_row, const int64_t stride_channel, const int64_t stride_sample, const float eps) {
8 const int nrows = gridDim.x;
9 const int row = blockIdx.x;
10 const int tid = threadIdx.x;
11
12 extern __shared__ float2 s_sum2[];
13
14 ggml_cuda_pdl_sync();
15
16 // grid.y and grid.z are clamped to the CUDA limit, iterate over the excess channels/samples
17 for (int sample = blockIdx.z; sample < nsamples; sample += gridDim.z) {
18 for (int channel = blockIdx.y; channel < nchannels; channel += gridDim.y) {
19 const float * xc = x + sample*stride_sample + channel*stride_channel + row*stride_row;
20 float * dstc = dst + ((sample*nchannels + channel)*nrows + row)*ncols;
21
22 float2 mean_var = make_float2(0.0f, 0.0f);
23
24 for (int col = tid; col < ncols; col += block_size) {
25 const float xi = xc[col];
26 mean_var.x += xi;
27 mean_var.y += xi * xi;
28 }
29
30 // sum up partial sums
31 mean_var = block_reduce<block_reduce_method::SUM, block_size>(mean_var, s_sum2);
32
33 const float mean = mean_var.x / ncols;
34 const float var = mean_var.y / ncols - mean * mean;
35 const float inv_std = rsqrtf(var + eps);
36
37 for (int col = tid; col < ncols; col += block_size) {
38 dstc[col] = (xc[col] - mean) * inv_std;
39 }
40
41 if constexpr (block_size > WARP_SIZE) {
42 // sync is needed as we reuse s_sum2 across block_reduce invocations, see #26385
43 __syncthreads();
44 }
45 }
46 }
47}
48
49template <int block_size>
50static __global__ void group_norm_f32(const float * x, float * dst, const int group_size, const int ne_elements, const float eps) {
51 // blockIdx.x: num_groups idx
52 // threadIdx.x: block_size idx
53 const int start = blockIdx.x*group_size + threadIdx.x;
54 const int end = min(blockIdx.x*group_size + group_size, ne_elements);
55
56 float tmp = 0.0f; // partial sum for thread in warp
57
58 ggml_cuda_pdl_sync();
59 for (int j = start; j < end; j += block_size) {
60 tmp += x[j];
61 }
62
63 extern __shared__ float s_sum[];
64 tmp = block_reduce<block_reduce_method::SUM, block_size>(tmp, s_sum);
65
66 const float mean = tmp / group_size;
67 tmp = 0.0f;
68
69 for (int j = start; j < end; j += block_size) {
70 const float xi = x[j] - mean;
71 dst[j] = xi;
72 tmp += xi * xi;
73 }
74
75 tmp = block_reduce<block_reduce_method::SUM, block_size>(tmp, s_sum + 32);
76
77 const float variance = tmp / group_size;
78 const float scale = rsqrtf(variance + eps);
79 for (int j = start; j < end; j += block_size) {
80 dst[j] *= scale;
81 }
82}
83
84template <int block_size, int vpt, bool do_vec, bool do_multiply = false, bool do_add = false, bool do_scale = false>
85static __global__ void rms_norm_f32(const float * x,
86 float * dst,
87 const int ncols,
88 const int nchannels,
89 const int nsamples,
90 const int64_t stride_row,
91 const int64_t stride_channel,
92 const int64_t stride_sample,
93 const float eps,
94 const float * mul = nullptr,
95 const int64_t mul_stride_row = 0,
96 const int64_t mul_stride_channel = 0,
97 const int64_t mul_stride_sample = 0,
98 const uint3 mul_ncols_packed = make_uint3(0, 0, 0),
99 const uint3 mul_nrows_packed = make_uint3(0, 0, 0),
100 const uint3 mul_nchannels_packed = make_uint3(0, 0, 0),
101 const uint3 mul_nsamples_packed = make_uint3(0, 0, 0),
102 const float * add = nullptr,
103 const int64_t add_stride_row = 0,
104 const int64_t add_stride_channel = 0,
105 const int64_t add_stride_sample = 0,
106 const uint3 add_ncols_packed = make_uint3(0, 0, 0),
107 const uint3 add_nrows_packed = make_uint3(0, 0, 0),
108 const uint3 add_nchannels_packed = make_uint3(0, 0, 0),
109 const uint3 add_nsamples_packed = make_uint3(0, 0, 0),
110 const float scale_out = 1.0f) {
111 ggml_cuda_pdl_lc();
112 const int nrows = gridDim.x;
113 const int row = blockIdx.x;
114 const int tid = threadIdx.x;
115
116 static_assert(!do_add || do_multiply, "fusing add is not supported without multiplying");
117 static_assert(!do_scale || !do_multiply, "fusing scale is not supported with multiplying");
118
119 extern __shared__ float s_sum[];
120
121 ggml_cuda_pdl_sync();
122
123 // grid.y and grid.z are clamped to the CUDA limit, iterate over the excess channels/samples
124 for (int sample = blockIdx.z; sample < nsamples; sample += gridDim.z) {
125 for (int channel = blockIdx.y; channel < nchannels; channel += gridDim.y) {
126 const float * xc = x + sample*stride_sample + channel*stride_channel + row*stride_row;
127 float * dstc = dst + ((sample*nchannels + channel)*nrows + row)*ncols;
128
129 [[maybe_unused]] const float * mulc = nullptr;
130 if constexpr (do_multiply) {
131 const uint32_t mul_row = fastmodulo(row, mul_nrows_packed);
132 const uint32_t mul_channel = fastmodulo(channel, mul_nchannels_packed);
133 const uint32_t mul_sample = fastmodulo(sample, mul_nsamples_packed);
134 mulc = mul + mul_sample * mul_stride_sample + mul_channel * mul_stride_channel + mul_row * mul_stride_row;
135 }
136
137 [[maybe_unused]] const float * addc = nullptr;
138 if constexpr (do_add) {
139 const int add_row = fastmodulo(row, add_nrows_packed);
140 const int add_channel = fastmodulo(channel, add_nchannels_packed);
141 const int add_sample = fastmodulo(sample, add_nsamples_packed);
142 addc = add + add_sample * add_stride_sample + add_channel * add_stride_channel + add_row * add_stride_row;
143 }
144
145 if constexpr (do_vec) {
146 const int ncols4 = ncols / 4;
147 const float4 * x4 = (const float4 *) xc;
148 const float4 * mul4 = (const float4 *) mulc;
149 const float4 * add4 = (const float4 *) addc;
150 float4 * dst4 = (float4 *) dstc;
151
152 float4 xs[vpt];
153 float tmp = 0.0f;
154
155#pragma unroll
156 for (int k = 0; k < vpt; k++) {
157 const int i = tid + k*block_size;
158 if (i < ncols4) {
159 xs[k] = x4[i];
160 tmp += xs[k].x*xs[k].x + xs[k].y*xs[k].y + xs[k].z*xs[k].z + xs[k].w*xs[k].w;
161 }
162 }
163 tmp = block_reduce<block_reduce_method::SUM, block_size>(tmp, s_sum);
164 const float scale = rsqrtf(tmp / ncols + eps);
165
166#pragma unroll
167 for (int k = 0; k < vpt; k++) {
168 const int i = tid + k*block_size;
169 if (i < ncols4) {
170 float4 r = make_float4(scale*xs[k].x, scale*xs[k].y, scale*xs[k].z, scale*xs[k].w);
171 if constexpr (do_multiply) {
172 const float4 m = mul4[i];
173 r.x *= m.x;
174 r.y *= m.y;
175 r.z *= m.z;
176 r.w *= m.w;
177 }
178 if constexpr (do_add) {
179 const float4 a = add4[i];
180 r.x += a.x;
181 r.y += a.y;
182 r.z += a.z;
183 r.w += a.w;
184 }
185 if constexpr (do_scale) {
186 r.x *= scale_out;
187 r.y *= scale_out;
188 r.z *= scale_out;
189 r.w *= scale_out;
190 }
191 dst4[i] = r;
192 }
193 }
194 } else {
195 float tmp = 0.0f; // partial sum for thread in warp
196
197 for (int col = tid; col < ncols; col += block_size) {
198 const float xi = xc[col];
199 tmp += xi * xi;
200 }
201
202 // sum up partial sums
203 tmp = block_reduce<block_reduce_method::SUM, block_size>(tmp, s_sum);
204
205 const float mean = tmp / ncols;
206 const float scale = rsqrtf(mean + eps);
207
208 for (int col = tid; col < ncols; col += block_size) {
209 if constexpr (do_multiply && do_add) {
210 const int mul_col = fastmodulo(col, mul_ncols_packed);
211 const int add_col = fastmodulo(col, add_ncols_packed);
212 dstc[col] = scale * xc[col] * mulc[mul_col] + addc[add_col];
213 } else if constexpr (do_multiply) {
214 const int mul_col = fastmodulo(col, mul_ncols_packed);
215 dstc[col] = scale * xc[col] * mulc[mul_col];
216 } else if constexpr (do_scale) {
217 dstc[col] = scale_out * (scale * xc[col]);
218 } else {
219 dstc[col] = scale * xc[col];
220 }
221 }
222 }
223
224 if constexpr (block_size > WARP_SIZE) {
225 // sync is needed as we reuse s_sum across block_reduce invocations, see #26385
226 __syncthreads();
227 }
228 }
229 }
230}
231
232template <int block_size>
233static __global__ void rms_norm_back_f32(
234 const float * grad, const float * xf, float * dst, const int ncols, const float eps) {
235 const int row = blockIdx.x*blockDim.y + threadIdx.y;
236 const int tid = threadIdx.x;
237
238 grad += int64_t(row)*ncols;
239 xf += int64_t(row)*ncols;
240 dst += int64_t(row)*ncols;
241
242 float sum_xx = 0.0f; // sum for squares of x, equivalent to forward pass
243 float sum_xg = 0.0f; // sum for x * gradient, needed because RMS norm mixes inputs
244
245 ggml_cuda_pdl_sync();
246 for (int col = tid; col < ncols; col += block_size) {
247 const float xfi = xf[col];
248 sum_xx += xfi * xfi;
249 sum_xg += xfi * grad[col];
250 }
251
252 // sum up partial sums
253 sum_xx = warp_reduce_sum(sum_xx);
254 sum_xg = warp_reduce_sum(sum_xg);
255 if constexpr (block_size > WARP_SIZE) {
256 static_assert(block_size == 1024, "unexpected block_size");
257 __shared__ float s_sum_xx[32];
258 __shared__ float s_sum_xg[32];
259 const int warp_id = threadIdx.x / WARP_SIZE;
260 const int lane_id = threadIdx.x % WARP_SIZE;
261 if (lane_id == 0) {
262 s_sum_xx[warp_id] = sum_xx;
263 s_sum_xg[warp_id] = sum_xg;
264 }
265 __syncthreads();
266
267 sum_xx = s_sum_xx[lane_id];
268 sum_xx = warp_reduce_sum(sum_xx);
269
270 sum_xg = s_sum_xg[lane_id];
271 sum_xg = warp_reduce_sum(sum_xg);
272 }
273
274 const float mean_eps = sum_xx / ncols + eps;
275 const float sum_eps = sum_xx + ncols*eps;
276
277 const float scale_grad = rsqrtf(mean_eps);
278 const float scale_x = -scale_grad * sum_xg/sum_eps;
279
280 for (int col = tid; col < ncols; col += block_size) {
281 dst[col] = scale_grad*grad[col] + scale_x*xf[col];
282 }
283}
284
285// template <int block_size>
286// static __global__ void l2_norm_f32(const float * x, float * dst, const int ncols, const float eps) {
287// const int row = blockIdx.x*blockDim.y + threadIdx.y;
288// const int tid = threadIdx.x;
289
290// float tmp = 0.0f; // partial sum for thread in warp
291
292// for (int col = tid; col < ncols; col += block_size) {
293// const float xi = x[row*ncols + col];
294// tmp += xi * xi;
295// }
296
297// // sum up partial sums
298// tmp = warp_reduce_sum(tmp);
299// if (block_size > WARP_SIZE) {
300// __shared__ float s_sum[32];
301// int warp_id = threadIdx.x / WARP_SIZE;
302// int lane_id = threadIdx.x % WARP_SIZE;
303// if (lane_id == 0) {
304// s_sum[warp_id] = tmp;
305// }
306// __syncthreads();
307// tmp = s_sum[lane_id];
308// tmp = warp_reduce_sum(tmp);
309// }
310
311// // from https://pytorch.org/docs/stable/generated/torch.nn.functional.normalize.html
312// const float scale = rsqrtf(fmaxf(tmp, eps * eps));
313
314// for (int col = tid; col < ncols; col += block_size) {
315// dst[row*ncols + col] = scale * x[row*ncols + col];
316// }
317// }
318
319template <int block_size>
320static __global__ void l2_norm_f32(
321 const float * x, float * dst, const int ncols, const int nchannels, const int nsamples,
322 const int64_t stride_row, const int64_t stride_channel, const int64_t stride_sample, const float eps) {
323 const int nrows = gridDim.x;
324 const int row = blockIdx.x;
325 const int tid = threadIdx.x;
326
327 extern __shared__ float s_sum[];
328
329 ggml_cuda_pdl_sync();
330
331 // grid.y and grid.z are clamped to the CUDA limit, iterate over the excess channels/samples
332 for (int sample = blockIdx.z; sample < nsamples; sample += gridDim.z) {
333 for (int channel = blockIdx.y; channel < nchannels; channel += gridDim.y) {
334 const float * xc = x + sample*stride_sample + channel*stride_channel + row*stride_row;
335 float * dstc = dst + ((sample*nchannels + channel)*nrows + row)*ncols;
336
337 float tmp = 0.0f; // partial sum for thread in warp
338
339 for (int col = tid; col < ncols; col += block_size) {
340 const float xi = xc[col];
341 tmp += xi * xi;
342 }
343
344 // sum up partial sums
345 tmp = block_reduce<block_reduce_method::SUM, block_size>(tmp, s_sum);
346
347 // from https://pytorch.org/docs/stable/generated/torch.nn.functional.normalize.html
348 const float scale = rsqrtf(fmaxf(tmp, eps * eps));
349
350 for (int col = tid; col < ncols; col += block_size) {
351 dstc[col] = scale * xc[col];
352 }
353
354 if constexpr (block_size > WARP_SIZE) {
355 // sync is needed as we reuse s_sum across block_reduce invocations, see #26385
356 __syncthreads();
357 }
358 }
359 }
360}
361
362static void norm_f32_cuda(
363 const float * x, float * dst, const int ncols, const int nrows, const int nchannels, const int nsamples,
364 const int64_t stride_row, const int64_t stride_channel, const int64_t stride_sample, const float eps, cudaStream_t stream) {
365 const dim3 blocks_num(nrows, MIN(nchannels, UINT16_MAX), MIN(nsamples, UINT16_MAX));
366 if (ncols < 1024) {
367 const dim3 block_dims(WARP_SIZE, 1, 1);
368 norm_f32<WARP_SIZE><<<blocks_num, block_dims, 0, stream>>>(x, dst, ncols, nchannels, nsamples, stride_row, stride_channel, stride_sample, eps);
369 } else {
370 const dim3 block_dims(1024, 1, 1);
371 norm_f32<1024><<<blocks_num, block_dims, block_dims.x > WARP_SIZE ? 32 * sizeof(float2): 0, stream>>>(x, dst, ncols, nchannels, nsamples, stride_row, stride_channel, stride_sample, eps);
372 }
373}
374
375static void group_norm_f32_cuda(
376 const float * x, float * dst, const int num_groups, const float eps, const int group_size, const int ne_elements, cudaStream_t stream) {
377 if (group_size < 1024) {
378 const dim3 block_dims(WARP_SIZE, 1, 1);
379 group_norm_f32<WARP_SIZE><<<num_groups, block_dims, 0, stream>>>(x, dst, group_size, ne_elements, eps);
380 } else {
381 const dim3 block_dims(1024, 1, 1);
382 group_norm_f32<1024><<<num_groups, block_dims, block_dims.x > WARP_SIZE ? 2 * 32 * sizeof(float): 0, stream>>>(x, dst, group_size, ne_elements, eps);
383 }
384}
385
386#define RMS_NORM_LAUNCH(BLOCK_SIZE, VPT, VEC) \
387 do { \
388 const dim3 block_dims(BLOCK_SIZE, 1, 1); \
389 const ggml_cuda_kernel_launch_params launch_params{ blocks_num, block_dims, block_dims.x > WARP_SIZE ? 32 * sizeof(float) : 0, stream }; \
390 ggml_cuda_kernel_launch(rms_norm_f32<BLOCK_SIZE, VPT, VEC, do_multiply, do_add, do_scale>, launch_params, \
391 x, dst, ncols, nchannels, nsamples, stride_row, stride_channel, stride_sample, eps, \
392 mul, mul_stride_row, mul_stride_channel, mul_stride_sample, \
393 mul_ncols_packed, mul_nrows_packed, mul_nchannels_packed, mul_nsamples_packed, \
394 add, add_stride_row, add_stride_channel, add_stride_sample, \
395 add_ncols_packed, add_nrows_packed, add_nchannels_packed, add_nsamples_packed, scale_out); \
396 } while (0)
397
398template <bool do_multiply, bool do_add, bool do_scale>
399static void rms_norm_f32_dispatch(
400 const dim3 blocks_num,
401 const ggml_tensor * x_t, float * dst, const int ncols, const int nchannels, const int nsamples,
402 const int64_t stride_row, const int64_t stride_channel, const int64_t stride_sample, const float eps,
403 const ggml_tensor * mul_t, const int64_t mul_stride_row, const int64_t mul_stride_channel, const int64_t mul_stride_sample,
404 const uint3 mul_ncols_packed, const uint3 mul_nrows_packed, const uint3 mul_nchannels_packed, const uint3 mul_nsamples_packed,
405 const ggml_tensor * add_t, const int64_t add_stride_row, const int64_t add_stride_channel, const int64_t add_stride_sample,
406 const uint3 add_ncols_packed, const uint3 add_nrows_packed, const uint3 add_nchannels_packed, const uint3 add_nsamples_packed,
407 cudaStream_t stream, const float scale_out = 1.0f) {
408 static_assert(!do_add || do_multiply, "fusing add is not supported without multiplying");
409 static_assert(!do_scale || !do_multiply, "fusing scale is not supported with multiplying");
410
411 const float * x = (const float *) x_t->data;
412 const float * mul = mul_t ? (const float *) mul_t->data : nullptr;
413 const float * add = add_t ? (const float *) add_t->data : nullptr;
414
415 // dst is written at row * ncols, i.e. always at a multiple of ncols, so only its base needs checking here
416 bool vec = ncols % 4 == 0 && ncols > 0 && ((uintptr_t) dst & 15) == 0;
417 vec = vec && ggml_cuda_is_aligned(x_t, 16);
418 if constexpr (do_multiply) {
419 vec = vec && mul_ncols_packed.z == (uint32_t) ncols
420 && ggml_cuda_is_aligned(mul_t, 16);
421 }
422 if constexpr (do_add) {
423 vec = vec && add_ncols_packed.z == (uint32_t) ncols
424 && ggml_cuda_is_aligned(add_t, 16);
425 }
426
427 const int n4 = ncols / 4;
428 if (vec) {
429 if (n4 <= 32) {
430 RMS_NORM_LAUNCH(32, 1, true);
431 } else if (n4 <= 128) {
432 RMS_NORM_LAUNCH(128, 1, true);
433 } else if (n4 <= 256) {
434 RMS_NORM_LAUNCH(256, 1, true);
435 } else if (n4 <= 512) {
436 RMS_NORM_LAUNCH(512, 1, true);
437 } else if (n4 <= 1024) {
438 RMS_NORM_LAUNCH(1024, 1, true);
439 } else if (n4 <= 4096) {
440 RMS_NORM_LAUNCH(1024, 4, true);
441 } else {
442 RMS_NORM_LAUNCH(1024, 1, false);
443 }
444 } else if (ncols < 1024) {
445 RMS_NORM_LAUNCH(256, 1, false);
446 } else {
447 RMS_NORM_LAUNCH(1024, 1, false);
448 }
449}
450#undef RMS_NORM_LAUNCH
451
452template <bool do_scale = false>
453static void rms_norm_f32_cuda(
454 const ggml_tensor * x_t, float * dst, const int ncols, const int nrows, const int nchannels, const int nsamples,
455 const int64_t stride_row, const int64_t stride_channel, const int64_t stride_sample, const float eps, cudaStream_t stream,
456 const float scale_out = 1.0f) {
457 const dim3 blocks_num(nrows, MIN(nchannels, UINT16_MAX), MIN(nsamples, UINT16_MAX));
458 rms_norm_f32_dispatch</*do_multiply*/false, /*do_add*/false, do_scale>(
459 blocks_num, x_t, dst, ncols, nchannels, nsamples, stride_row, stride_channel, stride_sample, eps,
460 nullptr, 0, 0, 0, make_uint3(0, 0, 0), make_uint3(0, 0, 0), make_uint3(0, 0, 0), make_uint3(0, 0, 0),
461 nullptr, 0, 0, 0, make_uint3(0, 0, 0), make_uint3(0, 0, 0), make_uint3(0, 0, 0), make_uint3(0, 0, 0),
462 stream, scale_out);
463}
464
465static void rms_norm_mul_f32_cuda(const ggml_tensor * x_t,
466 const ggml_tensor * mul_t,
467 const ggml_tensor * add_t,
468 float * dst,
469 const int ncols,
470 const int nrows,
471 const int nchannels,
472 const int nsamples,
473 const int64_t stride_row,
474 const int64_t stride_channel,
475 const int64_t stride_sample,
476 const int64_t mul_stride_row,
477 const int64_t mul_stride_channel,
478 const int64_t mul_stride_sample,
479 const uint32_t mul_ncols,
480 const uint32_t mul_nrows,
481 const uint32_t mul_nchannels,
482 const uint32_t mul_nsamples,
483 const int64_t add_stride_row,
484 const int64_t add_stride_channel,
485 const int64_t add_stride_sample,
486 const uint32_t add_ncols,
487 const uint32_t add_nrows,
488 const uint32_t add_nchannels,
489 const uint32_t add_nsamples,
490 const float eps,
491 cudaStream_t stream) {
492 const dim3 blocks_num(nrows, MIN(nchannels, UINT16_MAX), MIN(nsamples, UINT16_MAX));
493 if (mul_t == nullptr) {
494 rms_norm_f32_cuda(x_t, dst, ncols, nrows, nchannels, nsamples, stride_row, stride_channel, stride_sample, eps, stream);
495 return;
496 }
497 const uint3 mul_ncols_packed = init_fastdiv_values(mul_ncols);
498 const uint3 mul_nrows_packed = init_fastdiv_values(mul_nrows);
499 const uint3 mul_nchannels_packed = init_fastdiv_values(mul_nchannels);
500 const uint3 mul_nsamples_packed = init_fastdiv_values(mul_nsamples);
501 const uint3 zero_packed = make_uint3(0, 0, 0);
502
503 if (add_t == nullptr) {
504 rms_norm_f32_dispatch</*do_multiply*/true, /*do_add*/false, /*do_scale*/false>(
505 blocks_num, x_t, dst, ncols, nchannels, nsamples, stride_row, stride_channel, stride_sample, eps,
506 mul_t, mul_stride_row, mul_stride_channel, mul_stride_sample,
507 mul_ncols_packed, mul_nrows_packed, mul_nchannels_packed, mul_nsamples_packed,
508 nullptr, 0, 0, 0, zero_packed, zero_packed, zero_packed, zero_packed, stream);
509 } else {
510 const uint3 add_ncols_packed = init_fastdiv_values(add_ncols);
511 const uint3 add_nrows_packed = init_fastdiv_values(add_nrows);
512 const uint3 add_nchannels_packed = init_fastdiv_values(add_nchannels);
513 const uint3 add_nsamples_packed = init_fastdiv_values(add_nsamples);
514 rms_norm_f32_dispatch</*do_multiply*/true, /*do_add*/true, /*do_scale*/false>(
515 blocks_num, x_t, dst, ncols, nchannels, nsamples, stride_row, stride_channel, stride_sample, eps,
516 mul_t, mul_stride_row, mul_stride_channel, mul_stride_sample,
517 mul_ncols_packed, mul_nrows_packed, mul_nchannels_packed, mul_nsamples_packed,
518 add_t, add_stride_row, add_stride_channel, add_stride_sample,
519 add_ncols_packed, add_nrows_packed, add_nchannels_packed, add_nsamples_packed, stream);
520 }
521}
522
523static void rms_norm_back_f32_cuda(const float * grad, const float * xf, float * dst, const int ncols, const int nrows, const float eps, cudaStream_t stream) {
524 if (ncols < 1024) {
525 const dim3 block_dims(WARP_SIZE, 1, 1);
526 rms_norm_back_f32<WARP_SIZE><<<nrows, block_dims, 0, stream>>>(grad, xf, dst, ncols, eps);
527 } else {
528 const dim3 block_dims(1024, 1, 1);
529 rms_norm_back_f32<1024><<<nrows, block_dims, 0, stream>>>(grad, xf, dst, ncols, eps);
530 }
531}
532
533static void l2_norm_f32_cuda(
534 const float * x, float * dst, const int ncols, const int nrows, const int nchannels, const int nsamples,
535 const int64_t stride_row, const int64_t stride_channel, const int64_t stride_sample, const float eps, cudaStream_t stream) {
536 const dim3 blocks_num(nrows, MIN(nchannels, UINT16_MAX), MIN(nsamples, UINT16_MAX));
537 if (ncols < 1024) {
538 const dim3 block_dims(WARP_SIZE, 1, 1);
539 const ggml_cuda_kernel_launch_params launch_params = ggml_cuda_kernel_launch_params{blocks_num, block_dims, 0, stream};
540 ggml_cuda_kernel_launch(l2_norm_f32<WARP_SIZE>, launch_params, x, dst, ncols, nchannels, nsamples, stride_row, stride_channel, stride_sample, eps);
541 } else {
542 const dim3 block_dims(1024, 1, 1);
543 const ggml_cuda_kernel_launch_params launch_params = ggml_cuda_kernel_launch_params{blocks_num, block_dims, block_dims.x > WARP_SIZE ? 32 * sizeof(float): 0, stream};
544 ggml_cuda_kernel_launch(l2_norm_f32<1024>, launch_params, x, dst, ncols, nchannels, nsamples, stride_row, stride_channel, stride_sample, eps);
545 }
546}
547
548void ggml_cuda_op_norm(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
549 const ggml_tensor * src0 = dst->src[0];
550 const float * src0_d = (const float *) src0->data;
551 float * dst_d = (float *) dst->data;
552 cudaStream_t stream = ctx.stream();
553
554 GGML_ASSERT(src0->type == GGML_TYPE_F32);
555 GGML_ASSERT( dst->type == GGML_TYPE_F32);
556
557 GGML_TENSOR_UNARY_OP_LOCALS;
558
559 float eps;
560 memcpy(&eps, dst->op_params, sizeof(float));
561 GGML_ASSERT(eps >= 0.0f);
562
563 const size_t ts0 = ggml_type_size(src0->type);
564 GGML_ASSERT(nb00 == ts0);
565 const int64_t s01 = nb01 / ts0;
566 const int64_t s02 = nb02 / ts0;
567 const int64_t s03 = nb03 / ts0;
568
569 norm_f32_cuda(src0_d, dst_d, ne00, ne01, ne02, ne03, s01, s02, s03, eps, stream);
570}
571
572void ggml_cuda_op_group_norm(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
573 const ggml_tensor * src0 = dst->src[0];
574 const float * src0_d = (const float *)src0->data;
575 float * dst_d = (float *)dst->data;
576 cudaStream_t stream = ctx.stream();
577
578 GGML_ASSERT(src0->type == GGML_TYPE_F32);
579 GGML_ASSERT( dst->type == GGML_TYPE_F32);
580
581 int num_groups = dst->op_params[0];
582
583 float eps;
584 memcpy(&eps, dst->op_params + 1, sizeof(float));
585 GGML_ASSERT(eps >= 0.0f);
586
587 int group_size = src0->ne[0] * src0->ne[1] * ((src0->ne[2] + num_groups - 1) / num_groups);
588 group_norm_f32_cuda(src0_d, dst_d, num_groups * src0->ne[3], eps, group_size, ggml_nelements(src0), stream);
589}
590
591void ggml_cuda_op_rms_norm(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
592 const ggml_tensor * src0 = dst->src[0];
593 float * dst_d = (float *) dst->data;
594 cudaStream_t stream = ctx.stream();
595
596 GGML_ASSERT(src0->type == GGML_TYPE_F32);
597 GGML_ASSERT( dst->type == GGML_TYPE_F32);
598
599 GGML_TENSOR_UNARY_OP_LOCALS;
600
601 float eps;
602 memcpy(&eps, dst->op_params, sizeof(float));
603 GGML_ASSERT(eps >= 0.0f);
604
605 const size_t ts0 = ggml_type_size(src0->type);
606 GGML_ASSERT(nb00 == ts0);
607 const int64_t s01 = nb01 / ts0;
608 const int64_t s02 = nb02 / ts0;
609 const int64_t s03 = nb03 / ts0;
610
611 rms_norm_f32_cuda(src0, dst_d, ne00, ne01, ne02, ne03, s01, s02, s03, eps, stream);
612}
613
614void ggml_cuda_op_rms_norm_scale_fused(ggml_backend_cuda_context & ctx, ggml_tensor * dst, ggml_tensor * scale_tensor) {
615 const ggml_tensor * src0 = dst->src[0];
616 float * dst_d = (float *) scale_tensor->data;
617 cudaStream_t stream = ctx.stream();
618
619 GGML_ASSERT(src0->type == GGML_TYPE_F32);
620 GGML_ASSERT(scale_tensor->type == GGML_TYPE_F32);
621
622 GGML_TENSOR_UNARY_OP_LOCALS;
623
624 float eps;
625 memcpy(&eps, dst->op_params, sizeof(float));
626 GGML_ASSERT(eps >= 0.0f);
627
628 float scale;
629 memcpy(&scale, (const float *) scale_tensor->op_params + 0, sizeof(float));
630
631 const size_t ts0 = ggml_type_size(src0->type);
632 GGML_ASSERT(nb00 == ts0);
633 const int64_t s01 = nb01 / ts0;
634 const int64_t s02 = nb02 / ts0;
635 const int64_t s03 = nb03 / ts0;
636
637 rms_norm_f32_cuda<true>(src0, dst_d, ne00, ne01, ne02, ne03, s01, s02, s03, eps, stream, scale);
638}
639
640void ggml_cuda_op_rms_norm_fused(ggml_backend_cuda_context & ctx, ggml_tensor * dst, ggml_tensor * mul_tensor) {
641 const ggml_tensor * rms_norm_src = (ggml_tensor *) dst->src[0];
642 float eps = 0.0f;
643
644 memcpy(&eps, dst->op_params, sizeof(float));
645
646 const ggml_tensor * mul_src = nullptr;
647
648 if (mul_tensor->src[0] == dst) {
649 mul_src = mul_tensor->src[1];
650 } else if(mul_tensor->src[1] == dst) {
651 mul_src = mul_tensor->src[0];
652 } else {
653 GGML_ASSERT(false);
654 }
655
656 float * dst_d = (float *) mul_tensor->data;
657 cudaStream_t stream = ctx.stream();
658
659 GGML_ASSERT(rms_norm_src->type == GGML_TYPE_F32);
660 GGML_ASSERT(dst->type == GGML_TYPE_F32);
661 GGML_ASSERT(mul_tensor->type == GGML_TYPE_F32);
662 GGML_ASSERT(eps >= 0.0f);
663
664 const int64_t ne00 = rms_norm_src->ne[0];
665 const int64_t ne01 = rms_norm_src->ne[1];
666 const int64_t ne02 = rms_norm_src->ne[2];
667 const int64_t ne03 = rms_norm_src->ne[3];
668
669 const size_t ts0 = ggml_type_size(rms_norm_src->type);
670 GGML_ASSERT(rms_norm_src->nb[0] == ts0);
671 const int64_t s01 = rms_norm_src->nb[1] / ts0;
672 const int64_t s02 = rms_norm_src->nb[2] / ts0;
673 const int64_t s03 = rms_norm_src->nb[3] / ts0;
674
675 const size_t ts_mul = ggml_type_size(mul_src->type);
676 GGML_ASSERT(mul_src->nb[0] == ts_mul);
677 const int64_t mul_s01 = mul_src->nb[1] / ts_mul;
678 const int64_t mul_s02 = mul_src->nb[2] / ts_mul;
679 const int64_t mul_s03 = mul_src->nb[3] / ts_mul;
680
681 const int mul_ncols = mul_src->ne[0];
682 const int mul_nrows = mul_src->ne[1];
683 const int mul_nchannels = mul_src->ne[2];
684 const int mul_nsamples = mul_src->ne[3];
685
686 rms_norm_mul_f32_cuda(rms_norm_src, mul_src, nullptr, dst_d,
687 ne00, ne01, ne02, ne03,
688 /*s00*/ s01, s02, s03,
689 /*mul_s00*/ mul_s01, mul_s02, mul_s03,
690 mul_ncols, mul_nrows, mul_nchannels, mul_nsamples,
691 /*add_s00*/ 0, 0, 0,
692 0, 0, 0, 0,
693 eps, stream);
694}
695
696void ggml_cuda_op_rms_norm_fused_add(ggml_backend_cuda_context & ctx,
697 ggml_tensor * dst,
698 ggml_tensor * mul_tensor,
699 ggml_tensor * add_tensor) {
700 const ggml_tensor * rms_norm_src = (ggml_tensor *) dst->src[0];
701 float eps = 0.0f;
702
703 memcpy(&eps, dst->op_params, sizeof(float));
704
705 const ggml_tensor * mul_src = nullptr;
706
707 if (mul_tensor->src[0] == dst) {
708 mul_src = mul_tensor->src[1];
709 } else if (mul_tensor->src[1] == dst) {
710 mul_src = mul_tensor->src[0];
711 } else {
712 GGML_ASSERT(false);
713 }
714
715 const ggml_tensor * add_src = nullptr;
716
717 if (add_tensor->src[0] == mul_tensor) {
718 add_src = add_tensor->src[1];
719 } else if (add_tensor->src[1] == mul_tensor) {
720 add_src = add_tensor->src[0];
721 } else {
722 GGML_ASSERT(false);
723 }
724
725 float * dst_d = (float *) add_tensor->data;
726 cudaStream_t stream = ctx.stream();
727
728 GGML_ASSERT(rms_norm_src->type == GGML_TYPE_F32);
729 GGML_ASSERT(dst->type == GGML_TYPE_F32);
730 GGML_ASSERT(mul_tensor->type == GGML_TYPE_F32);
731 GGML_ASSERT(add_tensor->type == GGML_TYPE_F32);
732 GGML_ASSERT(eps >= 0.0f);
733
734 const int64_t ne00 = rms_norm_src->ne[0];
735 const int64_t ne01 = rms_norm_src->ne[1];
736 const int64_t ne02 = rms_norm_src->ne[2];
737 const int64_t ne03 = rms_norm_src->ne[3];
738
739 const size_t ts0 = ggml_type_size(rms_norm_src->type);
740 GGML_ASSERT(rms_norm_src->nb[0] == ts0);
741 const int64_t s01 = rms_norm_src->nb[1] / ts0;
742 const int64_t s02 = rms_norm_src->nb[2] / ts0;
743 const int64_t s03 = rms_norm_src->nb[3] / ts0;
744
745 const size_t ts_mul = ggml_type_size(mul_src->type);
746 GGML_ASSERT(mul_src->nb[0] == ts_mul);
747 const int64_t mul_s01 = mul_src->nb[1] / ts_mul;
748 const int64_t mul_s02 = mul_src->nb[2] / ts_mul;
749 const int64_t mul_s03 = mul_src->nb[3] / ts_mul;
750
751 const int mul_ncols = mul_src->ne[0];
752 const int mul_nrows = mul_src->ne[1];
753 const int mul_nchannels = mul_src->ne[2];
754 const int mul_nsamples = mul_src->ne[3];
755
756 const size_t ts_add = ggml_type_size(add_src->type);
757 GGML_ASSERT(add_src->nb[0] == ts_add);
758 const int64_t add_s01 = add_src->nb[1] / ts_add;
759 const int64_t add_s02 = add_src->nb[2] / ts_add;
760 const int64_t add_s03 = add_src->nb[3] / ts_add;
761
762 const int add_ncols = add_src->ne[0];
763 const int add_nrows = add_src->ne[1];
764 const int add_nchannels = add_src->ne[2];
765 const int add_nsamples = add_src->ne[3];
766
767 rms_norm_mul_f32_cuda(rms_norm_src, mul_src,add_src,dst_d,
768 ne00,ne01, ne02, ne03,
769 /*s00*/ s01, s02, s03,
770 /*mul_s00*/ mul_s01, mul_s02, mul_s03,
771 mul_ncols, mul_nrows, mul_nchannels, mul_nsamples,
772 /*add_s00*/ add_s01, add_s02, add_s03,
773 add_ncols, add_nrows, add_nchannels, add_nsamples,
774 eps, stream);
775}
776
777void ggml_cuda_op_rms_norm_back(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
778 const ggml_tensor * grad = dst->src[0]; // gradients
779 const ggml_tensor * src0f = dst->src[1]; // src0 from forward pass
780
781 const float * grad_d = (const float *) grad->data;
782 const float * src0f_d = (const float *) src0f->data;
783 float * dst_d = (float *) dst->data;
784
785 cudaStream_t stream = ctx.stream();
786
787 GGML_ASSERT(ggml_is_contiguous(grad));
788
789 GGML_ASSERT( grad->type == GGML_TYPE_F32);
790 GGML_ASSERT(src0f->type == GGML_TYPE_F32);
791 GGML_ASSERT( dst->type == GGML_TYPE_F32);
792
793 const int64_t ne00 = src0f->ne[0];
794 const int64_t nrows = ggml_nrows(src0f);
795
796 float eps;
797 memcpy(&eps, dst->op_params, sizeof(float));
798 GGML_ASSERT(eps >= 0.0f);
799
800 rms_norm_back_f32_cuda(grad_d, src0f_d, dst_d, ne00, nrows, eps, stream);
801}
802
803void ggml_cuda_op_l2_norm(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
804 const ggml_tensor * src0 = dst->src[0];
805 const float * src0_d = (const float *) src0->data;
806 float * dst_d = (float *) dst->data;
807 cudaStream_t stream = ctx.stream();
808
809 GGML_ASSERT(src0->type == GGML_TYPE_F32);
810 GGML_ASSERT( dst->type == GGML_TYPE_F32);
811
812 GGML_TENSOR_UNARY_OP_LOCALS;
813
814 float eps;
815 memcpy(&eps, dst->op_params, sizeof(float));
816 GGML_ASSERT(eps >= 0.0f);
817
818 const size_t ts0 = ggml_type_size(src0->type);
819 GGML_ASSERT(nb00 == ts0);
820 const int64_t s01 = nb01 / ts0;
821 const int64_t s02 = nb02 / ts0;
822 const int64_t s03 = nb03 / ts0;
823
824 l2_norm_f32_cuda(src0_d, dst_d, ne00, ne01, ne02, ne03, s01, s02, s03, eps, stream);
825}