Commit 922cff91e for llama.cpp
commit 922cff91e9f60db1afc5c00cef444f3af7f882c4
Author: losewayy <losemywayy@gmail.com>
Date: Tue Nov 24 22:20:58 2026 +0800
cuda : fuse RMS_NORM + MUL + MROPE/IMROPE and tune block size (#32500)
* cuda : support MROPE/IMROPE and tune block size in rms_norm_mul_rope
* cuda : use 64 threads for multi-token ncols <= 256 in rms_norm_mul_rope
Assisted-by: Antigravity
diff --git a/ggml/src/ggml-cuda/ggml-cuda.cu b/ggml/src/ggml-cuda/ggml-cuda.cu
index a07e9a622..9116830ea 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 657b58a4d..09891d01c 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");
}
diff --git a/tests/test-backend-ops.cpp b/tests/test-backend-ops.cpp
index a7380dcb1..0b468fda2 100644
--- a/tests/test-backend-ops.cpp
+++ b/tests/test-backend-ops.cpp
@@ -3026,6 +3026,7 @@ struct test_rms_norm_mul_rope : public test_case {
const bool yarn;
const bool truncate;
const ggml_type type; // type of a, the weights are F32
+ const int n_rot;
std::string op_desc(ggml_tensor * t) override {
GGML_UNUSED(t);
@@ -3039,15 +3040,19 @@ struct test_rms_norm_mul_rope : public test_case {
if (type != GGML_TYPE_F32) {
s += "," + VAR_TO_STR(type);
}
+ if (n_rot > 0) {
+ s += "," + VAR_TO_STR(n_rot);
+ }
return s;
}
test_rms_norm_mul_rope(std::array<int64_t, 4> ne, float eps = 1e-6f, bool multi_add = false,
bool set_rows = false, bool broadcast = false, int mode = GGML_ROPE_TYPE_NORMAL,
bool mul = true, bool rope = true, ggml_type set_rows_type = GGML_TYPE_F16,
- bool yarn = false, bool truncate = true, ggml_type type = GGML_TYPE_F32)
+ bool yarn = false, bool truncate = true, ggml_type type = GGML_TYPE_F32,
+ int n_rot = 0)
: ne(ne), eps(eps), multi_add(multi_add), mul(mul), rope(rope), set_rows(set_rows), broadcast(broadcast),
- set_rows_type(set_rows_type), mode(mode), yarn(yarn), truncate(truncate), type(type) {}
+ set_rows_type(set_rows_type), mode(mode), yarn(yarn), truncate(truncate), type(type), n_rot(n_rot) {}
ggml_tensor * build_graph(ggml_context * ctx) override {
ggml_tensor * a = ggml_new_tensor_4d(ctx, type, ne[0], ne[1], ne[2], ne[3]);
@@ -3084,14 +3089,21 @@ struct test_rms_norm_mul_rope : public test_case {
const float freq_scale = yarn ? 1.0f/256 : 1.0f;
const float ext_factor = yarn ? 1.0f : 0.0f;
+ const int n_dims = n_rot > 0 ? n_rot : (int) ne[0];
+
if (is_mrope) {
- const int n_dims = ne[0];
- int sections[4] = { n_dims/3, n_dims/3, n_dims/3, 0 };
+ const int pairs = n_dims / 2;
+ int sections[4] = {
+ n_rot > 0 ? pairs - 2 * (pairs / 3) : n_dims / 3,
+ n_rot > 0 ? pairs / 3 : n_dims / 3,
+ n_rot > 0 ? pairs / 3 : n_dims / 3,
+ 0
+ };
a = ggml_rope_multi(ctx, a, pos, nullptr, n_dims, sections, mode, n_ctx_orig, freq_base, freq_scale, ext_factor, 1.0f, 32.0f, 1.0f);
} else if (yarn) {
- a = ggml_rope_ext(ctx, a, pos, nullptr, ne[0], mode, n_ctx_orig, freq_base, freq_scale, ext_factor, 1.0f, 32.0f, 1.0f);
+ a = ggml_rope_ext(ctx, a, pos, nullptr, n_dims, mode, n_ctx_orig, freq_base, freq_scale, ext_factor, 1.0f, 32.0f, 1.0f);
} else {
- a = ggml_rope(ctx, a, pos, ne[0], mode);
+ a = ggml_rope(ctx, a, pos, n_dims, mode);
}
ggml_rope_set_truncate(a, truncate);
}
@@ -12268,7 +12280,7 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
for (auto multi_add : {false, true}) {
for (auto set_rows : {false, true}) {
for (auto broadcast : {false, true}) {
- for (auto rope : {GGML_ROPE_TYPE_NORMAL, GGML_ROPE_TYPE_NEOX, GGML_ROPE_TYPE_IMROPE}) {
+ for (auto rope : {GGML_ROPE_TYPE_NORMAL, GGML_ROPE_TYPE_NEOX, GGML_ROPE_TYPE_MROPE, GGML_ROPE_TYPE_IMROPE}) {
test_cases.emplace_back(new test_rms_norm_mul_rope({768, 1, 1, 1}, 1e-6f, multi_add, set_rows, broadcast, rope));
test_cases.emplace_back(new test_rms_norm_mul_rope({768, 3, 1, 1}, 1e-6f, multi_add, set_rows, broadcast, rope));
test_cases.emplace_back(new test_rms_norm_mul_rope({768, 3, 5, 1}, 1e-6f, multi_add, set_rows, broadcast, rope));
@@ -12282,12 +12294,17 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
}
}
}
+ test_cases.emplace_back(new test_rms_norm_mul_rope({128, 8, 1, 1}, 1e-6f, false, false, true, GGML_ROPE_TYPE_NEOX));
+ test_cases.emplace_back(new test_rms_norm_mul_rope({128, 8, 1, 1}, 1e-6f, false, true, true, GGML_ROPE_TYPE_NEOX));
+ test_cases.emplace_back(new test_rms_norm_mul_rope({256, 8, 1, 1}, 1e-6f, false, false, true, GGML_ROPE_TYPE_NEOX));
+ test_cases.emplace_back(new test_rms_norm_mul_rope({256, 8, 1, 1}, 1e-6f, false, true, true, GGML_ROPE_TYPE_NEOX));
+
// ne[2] > 65535 exceeds the CUDA gridDim.y limit (#27901)
test_cases.emplace_back(new test_rms_norm_mul_rope({4, 1, 65536, 1}, 1e-6f, false, false, false, GGML_ROPE_TYPE_NORMAL));
test_cases.emplace_back(new test_rms_norm_mul_rope({4, 1, 65536, 1}, 1e-6f, false, true, false, GGML_ROPE_TYPE_NEOX));
- for (int mode : {GGML_ROPE_TYPE_NORMAL, GGML_ROPE_TYPE_NEOX}) {
- for (int64_t n : {128, 1024}) {
+ for (int mode : {GGML_ROPE_TYPE_NORMAL, GGML_ROPE_TYPE_NEOX, GGML_ROPE_TYPE_MROPE, GGML_ROPE_TYPE_IMROPE}) {
+ for (int64_t n : {64, 128, 256, 1024}) {
for (bool truncate : {false, true}) {
test_cases.emplace_back(new test_rms_norm_mul_rope({n, 2, 3, 1}, 1e-6f, false, false, true, mode, true, true, GGML_TYPE_F16, true, truncate));
for (ggml_type type : {GGML_TYPE_F32, GGML_TYPE_F16}) {
@@ -12295,6 +12312,11 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
}
}
}
+ // partial rotation (e.g. Qwen3.5 rotates 64 of 256 or 128 head dims)
+ for (int64_t n : {128, 256}) {
+ test_cases.emplace_back(new test_rms_norm_mul_rope({n, 8, 5, 1}, 1e-6f, false, false, true, mode, true, true, GGML_TYPE_F16, false, true, GGML_TYPE_F32, 64));
+ test_cases.emplace_back(new test_rms_norm_mul_rope({n, 8, 5, 1}, 1e-6f, false, true, true, mode, true, true, GGML_TYPE_F16, false, true, GGML_TYPE_F32, 64));
+ }
}
for (int64_t d_conv : {2, 3, 4, 7, 9}) {