Commit 2142df34 for whisper.cpp
commit 2142df347bb3302c59a31ddcde5b4c7ce8ff3dee
Author: losewayy <losemywayy@gmail.com>
Date: Wed Nov 25 12:32:30 2026 +0200
cuda : fuse RMS_NORM + MUL + MROPE/IMROPE and tune block size (llama/32500)
diff --git a/ggml/src/ggml-cuda/ggml-cuda.cu b/ggml/src/ggml-cuda/ggml-cuda.cu
index a07e9a62..9116830e 100644
--- a/ggml/src/ggml-cuda/ggml-cuda.cu
+++ b/ggml/src/ggml-cuda/ggml-cuda.cu
@@ -3566,12 +3566,21 @@ static bool ggml_cuda_should_fuse_rms_norm_mul_rope(const ggml_tensor * rms_norm
return false;
}
- // the fused kernel handles the norm/neox rope modes only
const int mode = ((const int32_t *) rope->op_params)[2];
- if (mode != GGML_ROPE_TYPE_NORMAL && mode != GGML_ROPE_TYPE_NEOX) {
+ if (mode != GGML_ROPE_TYPE_NORMAL && mode != GGML_ROPE_TYPE_NEOX &&
+ mode != GGML_ROPE_TYPE_MROPE && mode != GGML_ROPE_TYPE_IMROPE) {
return false;
}
+ if (mode & GGML_ROPE_TYPE_MROPE) {
+ int sections[4];
+ memcpy(sections, (const int32_t *) rope->op_params + 11, sizeof(int) * 4);
+ const int sect_dims = sections[0] + sections[1] + sections[2] + sections[3];
+ if ((sections[0] <= 0 && sections[1] <= 0 && sections[2] <= 0) || sect_dims <= 0) {
+ return false;
+ }
+ }
+
const int n_dims = ((const int32_t *) rope->op_params)[1];
if (n_dims % 2 != 0 || rope->src[0]->ne[0] % 2 != 0) {
return false;
diff --git a/ggml/src/ggml-cuda/rope.cu b/ggml/src/ggml-cuda/rope.cu
index 657b58a4..09891d01 100644
--- a/ggml/src/ggml-cuda/rope.cu
+++ b/ggml/src/ggml-cuda/rope.cu
@@ -199,6 +199,36 @@ static __global__ void rope_neox(const T * x,
dst[idst + n_offs/2 + n_dims / 2] = ggml_cuda_cast<D>(x0 * sin_theta + x1 * cos_theta);
}
+static __device__ __forceinline__ int rope_mrope_pos_idx(
+ const int iw, const int channel, const int nchannels,
+ const mrope_sections sections, const bool is_imrope) {
+ const int sect_dims = sections.v[0] + sections.v[1] + sections.v[2] + sections.v[3];
+ const int sec_w = sections.v[1] + sections.v[0];
+ const int sector = (iw / 2) % sect_dims;
+
+ if (is_imrope) {
+ if (sector % 3 == 1 && sector < 3 * sections.v[1]) {
+ return channel + nchannels * 1;
+ } else if (sector % 3 == 2 && sector < 3 * sections.v[2]) {
+ return channel + nchannels * 2;
+ } else if (sector % 3 == 0 && sector < 3 * sections.v[0]) {
+ return channel;
+ } else {
+ return channel + nchannels * 3;
+ }
+ } else {
+ if (sector < sections.v[0]) {
+ return channel;
+ } else if (sector < sec_w) {
+ return channel + nchannels * 1;
+ } else if (sector < sec_w + sections.v[2]) {
+ return channel + nchannels * 2;
+ } else {
+ return channel + nchannels * 3;
+ }
+ }
+}
+
template <bool forward, bool has_ff, typename T, typename D>
static __global__ void rope_multi(const T * x,
D * dst,
@@ -261,32 +291,7 @@ static __global__ void rope_multi(const T * x,
const int iw = i0 - n_offs; // relative idx
- const int sect_dims = sections.v[0] + sections.v[1] + sections.v[2] + sections.v[3];
- const int sec_w = sections.v[1] + sections.v[0];
- const int sector = (iw / 2) % sect_dims;
-
- float theta_base = 0.0;
- if (is_imrope) {
- if (sector % 3 == 1 && sector < 3 * sections.v[1]) { // h
- theta_base = pos[i2 + ne02 * 1] * powf(theta_scale, iw / 2.0f);
- } else if (sector % 3 == 2 && sector < 3 * sections.v[2]) { // w
- theta_base = pos[i2 + ne02 * 2] * powf(theta_scale, iw / 2.0f);
- } else if (sector % 3 == 0 && sector < 3 * sections.v[0]) { // t
- theta_base = pos[i2] * powf(theta_scale, iw / 2.0f);
- } else {
- theta_base = pos[i2 + ne02 * 3] * powf(theta_scale, iw / 2.0f);
- }
- } else {
- if (sector < sections.v[0]) {
- theta_base = pos[i2] * powf(theta_scale, iw / 2.0f);
- } else if (sector >= sections.v[0] && sector < sec_w) {
- theta_base = pos[i2 + ne02 * 1] * powf(theta_scale, iw / 2.0f);
- } else if (sector >= sec_w && sector < sec_w + sections.v[2]) {
- theta_base = pos[i2 + ne02 * 2] * powf(theta_scale, iw / 2.0f);
- } else if (sector >= sec_w + sections.v[2]) {
- theta_base = pos[i2 + ne02 * 3] * powf(theta_scale, iw / 2.0f);
- }
- }
+ const float theta_base = pos[rope_mrope_pos_idx(iw, i2, ne02, sections, is_imrope)] * powf(theta_scale, iw / 2.0f);
const float freq_factor = has_ff ? freq_factors[iw/2] : 1.0f;
@@ -762,7 +767,7 @@ void ggml_cuda_op_rope_fused(ggml_backend_cuda_context & ctx, ggml_tensor * rope
// fused RMS_NORM + MUL + ROPE (+ VIEW + SET_ROWS)
// one block per row: block_reduce gives the norm scale, then each thread applies mul and rope to the elements it owns
-template <int block_size, bool has_ff, typename D>
+template <int block_size, bool has_ff, typename D, bool supports_mrope>
static __global__ void rms_norm_mul_rope_f32(
const float * x, D * dst, const int ncols, const int nchannels, const int nsamples,
const int64_t s01, const int64_t s02, const int64_t s03,
@@ -776,8 +781,9 @@ static __global__ void rms_norm_mul_rope_f32(
const float freq_scale, const float ext_factor, const float attn_factor,
const rope_corr_dims corr_dims, const float theta_scale,
const float * freq_factors,
+ const mrope_sections sections,
const int64_t * row_indices, const int set_rows_stride,
- const bool is_neox) {
+ const bool is_neox, const bool is_mrope, const bool is_imrope) {
ggml_cuda_pdl_lc();
const int row = blockIdx.x;
const int tid = threadIdx.x;
@@ -816,7 +822,7 @@ static __global__ void rms_norm_mul_rope_f32(
for (int i0 = 2*tid; i0 < ncols; i0 += 2*block_size) {
int ix0;
int ix1;
- if (is_neox && i0 < n_dims) {
+ if ((is_neox || (supports_mrope && is_mrope)) && i0 < n_dims) {
ix0 = i0/2;
ix1 = i0/2 + n_dims/2;
} else {
@@ -833,7 +839,9 @@ static __global__ void rms_norm_mul_rope_f32(
continue;
}
- const float theta_base = pos[channel]*powf(theta_scale, i0/2.0f);
+ const int pos_idx = supports_mrope && is_mrope ? rope_mrope_pos_idx(i0, channel, nchannels, sections, is_imrope) : channel;
+
+ const float theta_base = pos[pos_idx]*powf(theta_scale, i0/2.0f);
const float freq_factor = has_ff ? freq_factors[i0/2] : 1.0f;
float cos_theta;
@@ -867,8 +875,9 @@ static void rms_norm_mul_rope_cuda(
const float freq_scale, const float freq_base, const float ext_factor, const float attn_factor,
const rope_corr_dims corr_dims,
const float * freq_factors,
+ const mrope_sections sections,
const int64_t * row_indices, const int set_rows_stride,
- const bool is_neox, cudaStream_t stream) {
+ const bool is_neox, const bool is_mrope, const bool is_imrope, cudaStream_t stream) {
GGML_ASSERT(ncols % 2 == 0);
const dim3 blocks_num(nrows, MIN(nchannels, UINT16_MAX), MIN(nsamples, UINT16_MAX));
@@ -880,38 +889,44 @@ static void rms_norm_mul_rope_cuda(
const uint3 mul_nchannels_packed = init_fastdiv_values(mul_nchannels);
const uint3 mul_nsamples_packed = init_fastdiv_values(mul_nsamples);
- if (ncols < 1024) {
- const dim3 block_dims(256, 1, 1);
- const ggml_cuda_kernel_launch_params launch_params = {blocks_num, block_dims, 32*sizeof(float), stream};
+ auto launch_block = [&](auto bs_tag, auto mrope_tag) {
+ constexpr int block_size = decltype(bs_tag)::value;
+ constexpr bool supports_mrope = decltype(mrope_tag)::value;
+ const dim3 block_dims(block_size, 1, 1);
+ const size_t shmem = block_size > WARP_SIZE ? 32 * sizeof(float) : 0;
+ const ggml_cuda_kernel_launch_params launch_params = {blocks_num, block_dims, shmem, stream};
if (freq_factors == nullptr) {
- ggml_cuda_kernel_launch(rms_norm_mul_rope_f32<256, false, D>, launch_params,
+ ggml_cuda_kernel_launch(rms_norm_mul_rope_f32<block_size, false, D, supports_mrope>, launch_params,
x, dst, ncols, nchannels, nsamples, s01, s02, s03, s1, s2, s3, eps, mul, mul_s01, mul_s02, mul_s03,
mul_ncols_packed, mul_nrows_packed, mul_nchannels_packed, mul_nsamples_packed,
n_dims, pos, freq_scale, ext_factor, attn_factor, corr_dims, theta_scale,
- freq_factors, row_indices, set_rows_stride, is_neox);
+ freq_factors, sections, row_indices, set_rows_stride, is_neox, is_mrope, is_imrope);
} else {
- ggml_cuda_kernel_launch(rms_norm_mul_rope_f32<256, true, D>, launch_params,
+ ggml_cuda_kernel_launch(rms_norm_mul_rope_f32<block_size, true, D, supports_mrope>, launch_params,
x, dst, ncols, nchannels, nsamples, s01, s02, s03, s1, s2, s3, eps, mul, mul_s01, mul_s02, mul_s03,
mul_ncols_packed, mul_nrows_packed, mul_nchannels_packed, mul_nsamples_packed,
n_dims, pos, freq_scale, ext_factor, attn_factor, corr_dims, theta_scale,
- freq_factors, row_indices, set_rows_stride, is_neox);
+ freq_factors, sections, row_indices, set_rows_stride, is_neox, is_mrope, is_imrope);
}
- } else {
- const dim3 block_dims(1024, 1, 1);
- const ggml_cuda_kernel_launch_params launch_params = {blocks_num, block_dims, 32*sizeof(float), stream};
- if (freq_factors == nullptr) {
- ggml_cuda_kernel_launch(rms_norm_mul_rope_f32<1024, false, D>, launch_params,
- x, dst, ncols, nchannels, nsamples, s01, s02, s03, s1, s2, s3, eps, mul, mul_s01, mul_s02, mul_s03,
- mul_ncols_packed, mul_nrows_packed, mul_nchannels_packed, mul_nsamples_packed,
- n_dims, pos, freq_scale, ext_factor, attn_factor, corr_dims, theta_scale,
- freq_factors, row_indices, set_rows_stride, is_neox);
+ };
+
+ // Keep the wider launch and compile out multi-axis indexing for single-token NEOX.
+ if (is_neox && !is_mrope && nchannels == 1 && nsamples == 1 && ncols <= 8 * WARP_SIZE) {
+ launch_block(std::integral_constant<int, 256>{}, std::false_type{});
+ } else if (ncols <= 2 * WARP_SIZE) {
+ launch_block(std::integral_constant<int, WARP_SIZE>{}, std::true_type{});
+ } else if (ncols <= 4 * WARP_SIZE) {
+ launch_block(std::integral_constant<int, 2 * WARP_SIZE>{}, std::true_type{});
+ } else if (ncols <= 8 * WARP_SIZE) {
+ if (nchannels == 1 && nsamples == 1) {
+ launch_block(std::integral_constant<int, 256>{}, std::true_type{});
} else {
- ggml_cuda_kernel_launch(rms_norm_mul_rope_f32<1024, true, D>, launch_params,
- x, dst, ncols, nchannels, nsamples, s01, s02, s03, s1, s2, s3, eps, mul, mul_s01, mul_s02, mul_s03,
- mul_ncols_packed, mul_nrows_packed, mul_nchannels_packed, mul_nsamples_packed,
- n_dims, pos, freq_scale, ext_factor, attn_factor, corr_dims, theta_scale,
- freq_factors, row_indices, set_rows_stride, is_neox);
+ launch_block(std::integral_constant<int, 2 * WARP_SIZE>{}, std::true_type{});
}
+ } else if (ncols < 1024) {
+ launch_block(std::integral_constant<int, 256>{}, std::true_type{});
+ } else {
+ launch_block(std::integral_constant<int, 1024>{}, std::true_type{});
}
}
@@ -950,6 +965,7 @@ void ggml_cuda_op_rms_norm_mul_rope_fused(ggml_backend_cuda_context & ctx,
float attn_factor;
float beta_fast;
float beta_slow;
+ mrope_sections sections;
memcpy(&freq_base, (const int32_t *) rope->op_params + 5, sizeof(float));
memcpy(&freq_scale, (const int32_t *) rope->op_params + 6, sizeof(float));
@@ -957,8 +973,11 @@ void ggml_cuda_op_rms_norm_mul_rope_fused(ggml_backend_cuda_context & ctx,
memcpy(&attn_factor, (const int32_t *) rope->op_params + 8, sizeof(float));
memcpy(&beta_fast, (const int32_t *) rope->op_params + 9, sizeof(float));
memcpy(&beta_slow, (const int32_t *) rope->op_params + 10, sizeof(float));
+ memcpy(§ions.v, (const int32_t *) rope->op_params + 11, sizeof(int) * 4);
- const bool is_neox = mode & GGML_ROPE_TYPE_NEOX;
+ const bool is_neox = mode & GGML_ROPE_TYPE_NEOX;
+ const bool is_mrope = (mode & GGML_ROPE_TYPE_MROPE) != 0;
+ const bool is_imrope = mode == GGML_ROPE_TYPE_IMROPE;
const int32_t * pos = (const int32_t *) rope->src[1]->data;
@@ -992,14 +1011,14 @@ void ggml_cuda_op_rms_norm_mul_rope_fused(ggml_backend_cuda_context & ctx,
(const float *) mul_src->data, mul_s01, mul_s02, mul_s03,
mul_src->ne[0], mul_src->ne[1], mul_src->ne[2], mul_src->ne[3],
n_dims, pos, freq_scale, freq_base, ext_factor, attn_factor, corr_dims,
- freq_factors, row_indices, set_rows_stride, is_neox, stream);
+ freq_factors, sections, row_indices, set_rows_stride, is_neox, is_mrope, is_imrope, stream);
} else if (dst_type == GGML_TYPE_F16) {
rms_norm_mul_rope_cuda((const float *) x->data, (half *) dst_d,
x->ne[0], x->ne[1], x->ne[2], x->ne[3], s01, s02, s03, s1, s2, s3, eps,
(const float *) mul_src->data, mul_s01, mul_s02, mul_s03,
mul_src->ne[0], mul_src->ne[1], mul_src->ne[2], mul_src->ne[3],
n_dims, pos, freq_scale, freq_base, ext_factor, attn_factor, corr_dims,
- freq_factors, row_indices, set_rows_stride, is_neox, stream);
+ freq_factors, sections, row_indices, set_rows_stride, is_neox, is_mrope, is_imrope, stream);
} else {
GGML_ABORT("fatal error");
}