From de62fd6ed4d8bb1c7d9094f181054549332788f5 Mon Sep 17 00:00:00 2001 From: rafasmour <54501936+rafasmour@users.noreply.github.com> Date: Thu, 3 Dec 2026 11:03:17 +0000 Subject: [PATCH 1/4] cuda : two RMS norms in a row in one kernel (post-norm blocks) rms_norm*w1 + r [*scale] -> rms_norm*w2 runs as one launch: the residual (add or scale node) and the second product are written, the second product also as q8_1 blocks when a quantized mat-vec reads it next. Gemma 4 has this chain twice per layer (post-attention norm -> FFN norm, post-PLE norm + layer scale -> next attention norm), LFM3 as well. The block sizes are those of rms_norm_f32, so both sums are made in the same order as by the unfused kernels. GGML_CUDA_NO_RMS_NORM2 turns it off. Tests: test-backend-ops RMS_NORM2 (with and without the scale, with a Q8_0 mat-mul that reads both results). Assisted-by: Claude Opus 5.5 --- ggml/src/ggml-cuda/fusion.cu | 78 ++++++++++++++ ggml/src/ggml-cuda/norm.cu | 196 +++++++++++++++++++++++++++++++++++ ggml/src/ggml-cuda/norm.cuh | 5 + tests/test-backend-ops.cpp | 61 +++++++++++ 4 files changed, 340 insertions(+) diff --git a/ggml/src/ggml-cuda/fusion.cu b/ggml/src/ggml-cuda/fusion.cu index 3c1e89b5a560..766d3c47acb1 100644 --- a/ggml/src/ggml-cuda/fusion.cu +++ b/ggml/src/ggml-cuda/fusion.cu @@ -2215,6 +2215,78 @@ static bool ggml_cuda_match_dot_sigmoid_gate_add( return !overlaps(w, dst) && !overlaps(x, dst); } +// rms_norm*w1 + r [*scale] -> rms_norm*w2: two norms in a row (post-norm blocks: Gemma 4, LFM3), one launch for both. +// The residual (add or scale) and the second product are the outputs. +static int ggml_cuda_try_fuse_rms_norm2(ggml_backend_cuda_context * cuda_ctx, ggml_cgraph * cgraph, int i) { + static const bool disabled = getenv("GGML_CUDA_NO_RMS_NORM2") != nullptr; + if (disabled) { + return 0; + } + ggml_tensor ** n = cgraph->nodes + i; + int n1 = 0; + if (i + 5 < cgraph->n_nodes && n[0]->op == GGML_OP_RMS_NORM && n[1]->op == GGML_OP_MUL && n[2]->op == GGML_OP_ADD && + n[3]->op == GGML_OP_MUL && n[4]->op == GGML_OP_RMS_NORM && n[5]->op == GGML_OP_MUL && n[4]->src[0] == n[3] && + (n[3]->src[0] == n[2] || n[3]->src[1] == n[2])) { + n1 = 4; + } else if (i + 4 < cgraph->n_nodes && n[0]->op == GGML_OP_RMS_NORM && n[1]->op == GGML_OP_MUL && n[2]->op == GGML_OP_ADD && + n[3]->op == GGML_OP_RMS_NORM && n[4]->op == GGML_OP_MUL && n[3]->src[0] == n[2]) { + n1 = 3; + } + if (n1 == 0) { + return 0; + } + ggml_tensor * norm1 = n[0]; + ggml_tensor * mul1 = n[1]; + ggml_tensor * add = n[2]; + ggml_tensor * scale = n1 == 4 ? n[3] : nullptr; + ggml_tensor * norm2 = n[n1]; + ggml_tensor * mul2 = n[n1 + 1]; + if ((mul1->src[0] != norm1 && mul1->src[1] != norm1) || (add->src[0] != mul1 && add->src[1] != mul1) || + (mul2->src[0] != norm2 && mul2->src[1] != norm2)) { + return 0; + } + const ggml_op ops3[] = { GGML_OP_RMS_NORM, GGML_OP_MUL, GGML_OP_ADD, GGML_OP_RMS_NORM, GGML_OP_MUL }; + const ggml_op ops4[] = { GGML_OP_RMS_NORM, GGML_OP_MUL, GGML_OP_ADD, GGML_OP_MUL, GGML_OP_RMS_NORM, GGML_OP_MUL }; + const int outputs[] = { i + n1 - 1, i + n1 + 1 }; + if (!ggml_can_fuse_subgraph(cgraph, i, n1 + 2, n1 == 4 ? ops4 : ops3, outputs, 2)) { + return 0; + } + // A thread writes only values of the row it has read, after the reads (block barriers in between), so an output may take + // the exact place of x or r (in-place add), but not overlap them otherwise. + { + const ggml_tensor * x = norm1->src[0]; + const ggml_tensor * r = add->src[0] == mul1 ? add->src[1] : add->src[0]; + const ggml_tensor * w1 = mul1->src[0] == norm1 ? mul1->src[1] : mul1->src[0]; + const ggml_tensor * w2 = mul2->src[0] == norm2 ? mul2->src[1] : mul2->src[0]; + const ggml_tensor * y = scale ? scale : add; + auto overlap = [](const ggml_tensor * a, const ggml_tensor * b) { + const char * a0 = (const char *) a->data; const char * a1 = a0 + ggml_nbytes(a); + const char * b0 = (const char *) b->data; const char * b1 = b0 + ggml_nbytes(b); + return a0 < b1 && b0 < a1; + }; + auto exact = [](const ggml_tensor * a, const ggml_tensor * b) { + return a->data == b->data && ggml_nbytes(a) == ggml_nbytes(b) && a->nb[1] == b->nb[1]; + }; + for (const ggml_tensor * out : { y, (const ggml_tensor *) mul2 }) { + for (const ggml_tensor * in : { x, r }) { + if (overlap(out, in) && !exact(out, in)) { + return 0; + } + } + if (overlap(out, w1) || overlap(out, w2)) { + return 0; + } + } + if (overlap(y, mul2)) { + return 0; + } + } + if (!ggml_cuda_op_rms_norm2(*cuda_ctx, norm1, mul1, add, scale, norm2, mul2, ggml_cuda_q8_1_consumer(cuda_ctx, cgraph, i + n1 + 1, true, true))) { + return 0; + } + return n1 + 1; +} + // try and fuse nodes and return the number of nodes to skip int ggml_cuda_try_fuse(ggml_backend_cuda_context * cuda_ctx, ggml_cgraph * cgraph, int i) { if (int n = ggml_cuda_try_fuse_inkling2(cuda_ctx, cgraph, i)) { @@ -2869,6 +2941,12 @@ int ggml_cuda_try_fuse(ggml_backend_cuda_context * cuda_ctx, ggml_cgraph * cgrap return 2; } + if (node->op == GGML_OP_RMS_NORM) { + if (int n = ggml_cuda_try_fuse_rms_norm2(cuda_ctx, cgraph, i)) { + return n; + } + } + if (node->op == GGML_OP_RMS_NORM && ggml_cuda_can_fuse(cgraph, i, { GGML_OP_RMS_NORM, GGML_OP_MUL, GGML_OP_ADD, GGML_OP_MUL }, {})) { ggml_cuda_op_rms_norm_fused_add(*cuda_ctx, node, cgraph->nodes[i + 1], cgraph->nodes[i + 2], nullptr, cgraph->nodes[i + 3]); return 3; diff --git a/ggml/src/ggml-cuda/norm.cu b/ggml/src/ggml-cuda/norm.cu index ded810e6ce20..61b57504aeb6 100644 --- a/ggml/src/ggml-cuda/norm.cu +++ b/ggml/src/ggml-cuda/norm.cu @@ -2,6 +2,7 @@ #include "convert.cuh" #include "mmvq.cuh" #include +#include // sum over groups of `width` consecutive lanes (warp_reduce_sum reduces the whole warp on some devices) template @@ -320,6 +321,108 @@ static __global__ void rms_norm_f32(const T * x, } } +// two RMS norms in a row, as after a block with post-norm: y = (rms_norm(x)*w1 + r) [*scale], z = rms_norm(y)*w2. +// y is the residual stream and z the input of the next block, both are written; z also as q8_1 blocks when do_quantize. +// Rows are contiguous F32, w1 and w2 are one row each. A thread keeps the same values of the row for both norms. +template +static __global__ void rms_norm2_f32(const float * x, const float * w1, const float * r, const float * scale_ptr, float * y, + const float * w2, float * z, void * q8_1, const int ncols, const int64_t stride_x, + const int64_t stride_r, const float eps1, const float eps2) { + ggml_cuda_pdl_lc(); + const int row = blockIdx.x; + const int tid = threadIdx.x; + const int ncols4 = ncols / 4; + + extern __shared__ float s_sum[]; + + ggml_cuda_pdl_sync(); + + const float4 * x4 = (const float4 *) (x + row*stride_x); + const float4 * r4 = (const float4 *) (r + row*stride_r); + const float4 * w14 = (const float4 *) w1; + const float4 * w24 = (const float4 *) w2; + float4 * y4 = (float4 *) (y + (int64_t) row*ncols); + float4 * z4 = (float4 *) (z + (int64_t) row*ncols); + + float4 v[vpt]; + float tmp = 0.0f; +#pragma unroll + for (int k = 0; k < vpt; k++) { + const int i = tid + k*block_size; + if (i < ncols4) { + v[k] = x4[i]; + tmp += v[k].x*v[k].x + v[k].y*v[k].y + v[k].z*v[k].z + v[k].w*v[k].w; + } + } + tmp = block_reduce(tmp, s_sum); + const float scale1 = rsqrtf(tmp / ncols + eps1); + + [[maybe_unused]] float scale_out = 1.0f; + if constexpr (do_scale) { + scale_out = *scale_ptr; + } + + tmp = 0.0f; +#pragma unroll + for (int k = 0; k < vpt; k++) { + const int i = tid + k*block_size; + if (i < ncols4) { + const float4 m = w14[i]; + const float4 a = r4[i]; + float4 t = make_float4(scale1*v[k].x, scale1*v[k].y, scale1*v[k].z, scale1*v[k].w); + t.x *= m.x; t.y *= m.y; t.z *= m.z; t.w *= m.w; + t.x += a.x; t.y += a.y; t.z += a.z; t.w += a.w; + if constexpr (do_scale) { + t.x *= scale_out; t.y *= scale_out; t.z *= scale_out; t.w *= scale_out; + } + v[k] = t; + y4[i] = t; + tmp += t.x*t.x + t.y*t.y + t.z*t.z + t.w*t.w; + } + } + if constexpr (block_size > WARP_SIZE) { + __syncthreads(); // s_sum is reused + } + tmp = block_reduce(tmp, s_sum); + const float scale2 = rsqrtf(tmp / ncols + eps2); + +#pragma unroll + for (int k = 0; k < vpt; k++) { + const int i = tid + k*block_size; + if (i < ncols4) { + const float4 m = w24[i]; + float4 o = make_float4(scale2*v[k].x, scale2*v[k].y, scale2*v[k].z, scale2*v[k].w); + o.x *= m.x; o.y *= m.y; o.z *= m.z; o.w *= m.w; + z4[i] = o; + + if constexpr (do_quantize) { + // same arithmetic as quantize_q8_1, see rms_norm_f32 + float amax = fmaxf(fmaxf(fabsf(o.x), fabsf(o.y)), fmaxf(fabsf(o.z), fabsf(o.w))); + amax = warp_reduce_max<8>(amax); + + const float d = amax / 127.0f; + const int8_t q0 = amax == 0.0f ? 0 : roundf(o.x / d); + const int8_t q1 = amax == 0.0f ? 0 : roundf(o.y / d); + const int8_t q2 = amax == 0.0f ? 0 : roundf(o.z / d); + const int8_t q3 = amax == 0.0f ? 0 : roundf(o.w / d); + + const int sum = lane_group_reduce_sum<8>((int) q0 + (int) q1 + (int) q2 + (int) q3); + + block_q8_1 * yq = (block_q8_1 *) q8_1 + (int64_t) row*(GGML_PAD(ncols, MATRIX_ROW_PADDING)/QK8_1) + i / (QK8_1/4); + const int iqs = 4*(i % (QK8_1/4)); + + yq->qs[iqs + 0] = q0; + yq->qs[iqs + 1] = q1; + yq->qs[iqs + 2] = q2; + yq->qs[iqs + 3] = q3; + if (iqs == 0) { + yq->ds = make_half2(d, d * sum); + } + } + } + } +} + template static __global__ void rms_norm_back_f32( const float * grad, const float * xf, float * dst, const int ncols, const float eps) { @@ -1135,6 +1238,99 @@ void ggml_cuda_op_rms_norm_fused_add(ggml_backend_cuda_context & ctx, } } +bool ggml_cuda_op_rms_norm2(ggml_backend_cuda_context & ctx, const ggml_tensor * norm1, const ggml_tensor * mul1, const ggml_tensor * add, + const ggml_tensor * scale, const ggml_tensor * norm2, ggml_tensor * mul2, const ggml_tensor * q8_1_src1) { + const ggml_tensor * x = norm1->src[0]; + const ggml_tensor * w1 = mul1->src[0] == norm1 ? mul1->src[1] : mul1->src[0]; + const ggml_tensor * r = add->src[0] == mul1 ? add->src[1] : add->src[0]; + const ggml_tensor * w2 = mul2->src[0] == norm2 ? mul2->src[1] : mul2->src[0]; + ggml_tensor * y = (ggml_tensor *) (scale ? scale : add); + + float eps1 = 0.0f; + float eps2 = 0.0f; + memcpy(&eps1, norm1->op_params, sizeof(float)); + memcpy(&eps2, norm2->op_params, sizeof(float)); + + const int64_t ncols = x->ne[0]; + const int64_t nrows = ggml_nrows(x); + const int n4 = ncols / 4; + + const bool rows_ok = [&](const ggml_tensor * t) { + return t->type == GGML_TYPE_F32 && t->nb[0] == sizeof(float) && t->ne[0] == ncols && ggml_nrows(t) == nrows && + t->nb[2] == t->nb[1]*t->ne[1] && t->nb[3] == t->nb[2]*t->ne[2] && ggml_cuda_is_aligned(t, 16) && t->nb[1] % 16 == 0; + }(x) && [&](const ggml_tensor * t) { + return t->type == GGML_TYPE_F32 && t->nb[0] == sizeof(float) && t->ne[0] == ncols && ggml_nrows(t) == nrows && + t->nb[2] == t->nb[1]*t->ne[1] && t->nb[3] == t->nb[2]*t->ne[2] && ggml_cuda_is_aligned(t, 16) && t->nb[1] % 16 == 0; + }(r); + const bool weights_ok = w1->type == GGML_TYPE_F32 && w2->type == GGML_TYPE_F32 && ggml_nelements(w1) == ncols && ggml_nelements(w2) == ncols && + ggml_is_contiguous(w1) && ggml_is_contiguous(w2) && ggml_cuda_is_aligned(w1, 16) && ggml_cuda_is_aligned(w2, 16); + const bool outs_ok = y->type == GGML_TYPE_F32 && mul2->type == GGML_TYPE_F32 && ggml_is_contiguous(y) && ggml_is_contiguous(mul2) && + ggml_nelements(y) == ncols*nrows && ggml_nelements(mul2) == ncols*nrows && + ggml_cuda_is_aligned(y, 16) && ggml_cuda_is_aligned(mul2, 16); + const bool scale_ok = scale == nullptr || (scale->type == GGML_TYPE_F32 && + ggml_nelements(scale->src[0] == add ? scale->src[1] : scale->src[0]) == 1); + if (ncols % 4 != 0 || n4 > 4096 || nrows > INT_MAX || !rows_ok || !weights_ok || !outs_ok || !scale_ok) { + return false; + } + + const float * scale_ptr = scale ? (const float *) (scale->src[0] == add ? scale->src[1] : scale->src[0])->data : nullptr; + + void * q8_1 = nullptr; + int64_t q_args[8]; + const bool quantize = q8_1_src1 != nullptr && ncols % QK8_1 == 0; + if (quantize) { + GGML_ASSERT(q8_1_src1->data == mul2->data && ggml_nelements(q8_1_src1) == ggml_nelements(mul2)); + ggml_cuda_mmvq_q8_1_args(q8_1_src1, q_args); + ctx.q8_1_cache.clear(); + ctx.q8_1_cache.buf = std::make_unique>( + ctx.pool(), q_args[7]*q_args[6]*q_args[5]*q_args[4] * sizeof(block_q8_1) / QK8_1); + q8_1 = ctx.q8_1_cache.buf->get(); + } + + cudaStream_t stream = ctx.stream(); + const int64_t stride_x = x->nb[1] / sizeof(float); + const int64_t stride_r = r->nb[1] / sizeof(float); + const dim3 blocks_num(nrows, 1, 1); + +#define RMS_NORM2_LAUNCH(BS, VPT) \ + do { \ + const dim3 block_dims(BS, 1, 1); \ + const ggml_cuda_kernel_launch_params launch_params{ blocks_num, block_dims, BS > WARP_SIZE ? 32 * sizeof(float) : 0, stream }; \ + if (scale_ptr) { \ + if (quantize) ggml_cuda_kernel_launch(rms_norm2_f32, launch_params, (const float *) x->data, (const float *) w1->data, (const float *) r->data, scale_ptr, (float *) y->data, (const float *) w2->data, (float *) mul2->data, q8_1, (int) ncols, stride_x, stride_r, eps1, eps2); \ + else ggml_cuda_kernel_launch(rms_norm2_f32, launch_params, (const float *) x->data, (const float *) w1->data, (const float *) r->data, scale_ptr, (float *) y->data, (const float *) w2->data, (float *) mul2->data, q8_1, (int) ncols, stride_x, stride_r, eps1, eps2); \ + } else { \ + if (quantize) ggml_cuda_kernel_launch(rms_norm2_f32, launch_params, (const float *) x->data, (const float *) w1->data, (const float *) r->data, scale_ptr, (float *) y->data, (const float *) w2->data, (float *) mul2->data, q8_1, (int) ncols, stride_x, stride_r, eps1, eps2); \ + else ggml_cuda_kernel_launch(rms_norm2_f32, launch_params, (const float *) x->data, (const float *) w1->data, (const float *) r->data, scale_ptr, (float *) y->data, (const float *) w2->data, (float *) mul2->data, q8_1, (int) ncols, stride_x, stride_r, eps1, eps2); \ + } \ + } while (0) + + // the block sizes of rms_norm_f32_dispatch, so that both norms sum in the same order as the unfused kernels + if (n4 <= 32) { + RMS_NORM2_LAUNCH(32, 1); + } else if (n4 <= 128) { + RMS_NORM2_LAUNCH(128, 1); + } else if (n4 <= 256) { + RMS_NORM2_LAUNCH(256, 1); + } else if (n4 <= 512) { + RMS_NORM2_LAUNCH(512, 1); + } else if (n4 <= 1024) { + RMS_NORM2_LAUNCH(1024, 1); + } else { + RMS_NORM2_LAUNCH(1024, 4); + } +#undef RMS_NORM2_LAUNCH + + if (q8_1_src1 != nullptr) { + if (quantize) { + ctx.q8_1_cache.set(q8_1_src1, q_args, mul2); + } else { + ctx.q8_1_cache.clear(); + } + } + return true; +} + void ggml_cuda_op_rms_norm_back(ggml_backend_cuda_context & ctx, ggml_tensor * dst) { const ggml_tensor * grad = dst->src[0]; // gradients const ggml_tensor * src0f = dst->src[1]; // src0 from forward pass diff --git a/ggml/src/ggml-cuda/norm.cuh b/ggml/src/ggml-cuda/norm.cuh index dd51dbcd0a78..4594b51f0bfa 100644 --- a/ggml/src/ggml-cuda/norm.cuh +++ b/ggml/src/ggml-cuda/norm.cuh @@ -22,6 +22,11 @@ void ggml_cuda_op_rms_norm_fused_add(ggml_backend_cuda_context & ctx, const ggml_tensor * q8_1_src1 = nullptr, ggml_tensor * scale_tensor = nullptr); +// rms_norm*w1 + r [*scale] -> rms_norm*w2 in one kernel (two norms in a row): writes the residual (add or scale node) and mul2. +// Returns false (nothing launched) when the shapes are not supported. +bool ggml_cuda_op_rms_norm2(ggml_backend_cuda_context & ctx, const ggml_tensor * norm1, const ggml_tensor * mul1, const ggml_tensor * add, + const ggml_tensor * scale, const ggml_tensor * norm2, ggml_tensor * mul2, const ggml_tensor * q8_1_src1 = nullptr); + void ggml_cuda_op_rms_norm_back(ggml_backend_cuda_context & ctx, ggml_tensor * dst); void ggml_cuda_op_l2_norm(ggml_backend_cuda_context & ctx, ggml_tensor * dst); diff --git a/tests/test-backend-ops.cpp b/tests/test-backend-ops.cpp index 94e490476473..2496bb75c574 100644 --- a/tests/test-backend-ops.cpp +++ b/tests/test-backend-ops.cpp @@ -9842,6 +9842,55 @@ struct test_rms_norm_mul_add_scale : public test_case { } }; +// rms_norm*w1 + r [*s] -> rms_norm*w2: two norms in a row, both results are read later +struct test_rms_norm2 : public test_case { + const int64_t k; + const int64_t n; + const bool scale; + const ggml_type type_mm; // GGML_TYPE_COUNT: no mat-mul reads the results + + test_rms_norm2(int64_t k, int64_t n, bool scale, ggml_type type_mm) : k(k), n(n), scale(scale), type_mm(type_mm) {} + + std::string vars() override { + return VARS_TO_STR4(k, n, scale, type_mm); + } + + std::string op_desc(ggml_tensor * t) override { + GGML_UNUSED(t); + return "RMS_NORM2"; + } + + bool run_whole_graph() override { return true; } + + double max_nmse_err() override { + return type_mm == GGML_TYPE_COUNT ? 1e-7 : 5e-4; + } + + ggml_tensor * build_graph(ggml_context * ctx) override { + ggml_tensor * x = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, k, n); + ggml_tensor * r = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, k, n); + ggml_tensor * w1 = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, k); + ggml_tensor * w2 = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, k); + ggml_tensor * s = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 1); + + ggml_tensor * y = ggml_add(ctx, ggml_mul(ctx, ggml_rms_norm(ctx, x, 1e-6f), w1), r); + if (scale) { + y = ggml_mul(ctx, y, s); + } + ggml_tensor * z = ggml_mul(ctx, ggml_rms_norm(ctx, y, 1e-6f), w2); + + ggml_tensor * out; + if (type_mm == GGML_TYPE_COUNT) { + out = ggml_add(ctx, y, z); + } else { + ggml_tensor * a = ggml_new_tensor_2d(ctx, type_mm, k, 96); + out = ggml_add(ctx, ggml_mul_mat(ctx, a, z), ggml_mul_mat(ctx, a, y)); + } + ggml_set_name(out, "out"); + return out; + } +}; + struct test_rms_norm_mul_mat : public test_case { const ggml_type type_a; const int64_t m; @@ -14597,6 +14646,18 @@ static std::vector> make_test_cases_eval() { } } + // two norms in a row (post-norm blocks), every block size of the fused kernel and the rows that it does not take + for (int64_t k : {30, 64, 256, 512, 1024, 2048, 2560, 4096, 5376, 20000}) { + for (int64_t n : {1, 4, 7, 32}) { + for (bool sc : { false, true }) { + test_cases.emplace_back(new test_rms_norm2(k, n, sc, GGML_TYPE_COUNT)); + if (k % 32 == 0) { + test_cases.emplace_back(new test_rms_norm2(k, n, sc, GGML_TYPE_Q8_0)); + } + } + } + } + // norm of a layer input followed by quantized mat-vecs: row lengths with and without padding of the q8_1 copy, // every block size of the vectorized norm kernel, mat-vec (<= 8 tokens) and mat-mul for (ggml_type type_a : {GGML_TYPE_Q4_0, GGML_TYPE_Q8_0, GGML_TYPE_Q4_K}) { From 036e26e53f667d2ca18fdc3c2e9cdd9f329b2543 Mon Sep 17 00:00:00 2001 From: rafasmour <54501936+rafasmour@users.noreply.github.com> Date: Thu, 3 Dec 2026 11:23:17 +0000 Subject: [PATCH 2/4] tests : RMS_NORM2 names the mat-mul type only when there is one Assisted-by: Claude Opus 5.5 (cherry picked from commit ddb427435ea0b11bc7ce7c71a1f7802e5d7b406a) --- tests/test-backend-ops.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/test-backend-ops.cpp b/tests/test-backend-ops.cpp index 2496bb75c574..dc8b09ecea71 100644 --- a/tests/test-backend-ops.cpp +++ b/tests/test-backend-ops.cpp @@ -9852,7 +9852,11 @@ struct test_rms_norm2 : public test_case { test_rms_norm2(int64_t k, int64_t n, bool scale, ggml_type type_mm) : k(k), n(n), scale(scale), type_mm(type_mm) {} std::string vars() override { - return VARS_TO_STR4(k, n, scale, type_mm); + std::string s = VARS_TO_STR3(k, n, scale); + if (type_mm != GGML_TYPE_COUNT) { + s += "," + VAR_TO_STR(type_mm); + } + return s; } std::string op_desc(ggml_tensor * t) override { From bee224e6f6c7a03e3be7b4dfca613d25b9a42c68 Mon Sep 17 00:00:00 2001 From: rafasmour <54501936+rafasmour@users.noreply.github.com> Date: Fri, 4 Dec 2026 01:52:58 +0000 Subject: [PATCH 3/4] cuda : RMS_NORM2 only for weights of one row and an F32 scale The fused kernel reads w1 and w2 as one row of ncols values. The check compared only the number of values and the 16-byte alignment, so a weight broadcast over the rows with ncols values in another shape (e.g. [4, ncols/4]) was fused and the result was wrong. The scale was read as float without a check of its type. Check ne[0] of the weights and the type of the scale, and do not fuse when an output overlaps the scale. New test cases: weights broadcast over the rows, x as a view of longer rows, rows in channels. Assisted-by: Claude Opus 5.5 --- ggml/src/ggml-cuda/fusion.cu | 3 ++- ggml/src/ggml-cuda/norm.cu | 10 ++++---- tests/test-backend-ops.cpp | 45 ++++++++++++++++++++++++++++++++---- 3 files changed, 48 insertions(+), 10 deletions(-) diff --git a/ggml/src/ggml-cuda/fusion.cu b/ggml/src/ggml-cuda/fusion.cu index 766d3c47acb1..de520946567a 100644 --- a/ggml/src/ggml-cuda/fusion.cu +++ b/ggml/src/ggml-cuda/fusion.cu @@ -2258,6 +2258,7 @@ static int ggml_cuda_try_fuse_rms_norm2(ggml_backend_cuda_context * cuda_ctx, gg const ggml_tensor * r = add->src[0] == mul1 ? add->src[1] : add->src[0]; const ggml_tensor * w1 = mul1->src[0] == norm1 ? mul1->src[1] : mul1->src[0]; const ggml_tensor * w2 = mul2->src[0] == norm2 ? mul2->src[1] : mul2->src[0]; + const ggml_tensor * s = scale ? (scale->src[0] == add ? scale->src[1] : scale->src[0]) : nullptr; const ggml_tensor * y = scale ? scale : add; auto overlap = [](const ggml_tensor * a, const ggml_tensor * b) { const char * a0 = (const char *) a->data; const char * a1 = a0 + ggml_nbytes(a); @@ -2273,7 +2274,7 @@ static int ggml_cuda_try_fuse_rms_norm2(ggml_backend_cuda_context * cuda_ctx, gg return 0; } } - if (overlap(out, w1) || overlap(out, w2)) { + if (overlap(out, w1) || overlap(out, w2) || (s && overlap(out, s))) { return 0; } } diff --git a/ggml/src/ggml-cuda/norm.cu b/ggml/src/ggml-cuda/norm.cu index 61b57504aeb6..dda3faaa37d2 100644 --- a/ggml/src/ggml-cuda/norm.cu +++ b/ggml/src/ggml-cuda/norm.cu @@ -1262,18 +1262,20 @@ bool ggml_cuda_op_rms_norm2(ggml_backend_cuda_context & ctx, const ggml_tensor * return t->type == GGML_TYPE_F32 && t->nb[0] == sizeof(float) && t->ne[0] == ncols && ggml_nrows(t) == nrows && t->nb[2] == t->nb[1]*t->ne[1] && t->nb[3] == t->nb[2]*t->ne[2] && ggml_cuda_is_aligned(t, 16) && t->nb[1] % 16 == 0; }(r); - const bool weights_ok = w1->type == GGML_TYPE_F32 && w2->type == GGML_TYPE_F32 && ggml_nelements(w1) == ncols && ggml_nelements(w2) == ncols && + // w1 and w2 are read as one row of ncols values: a weight broadcast over the rows can have ncols values in another shape + const bool weights_ok = w1->type == GGML_TYPE_F32 && w2->type == GGML_TYPE_F32 && w1->ne[0] == ncols && w2->ne[0] == ncols && + ggml_nelements(w1) == ncols && ggml_nelements(w2) == ncols && ggml_is_contiguous(w1) && ggml_is_contiguous(w2) && ggml_cuda_is_aligned(w1, 16) && ggml_cuda_is_aligned(w2, 16); const bool outs_ok = y->type == GGML_TYPE_F32 && mul2->type == GGML_TYPE_F32 && ggml_is_contiguous(y) && ggml_is_contiguous(mul2) && ggml_nelements(y) == ncols*nrows && ggml_nelements(mul2) == ncols*nrows && ggml_cuda_is_aligned(y, 16) && ggml_cuda_is_aligned(mul2, 16); - const bool scale_ok = scale == nullptr || (scale->type == GGML_TYPE_F32 && - ggml_nelements(scale->src[0] == add ? scale->src[1] : scale->src[0]) == 1); + const ggml_tensor * scale_src = scale ? (scale->src[0] == add ? scale->src[1] : scale->src[0]) : nullptr; + const bool scale_ok = scale == nullptr || (scale->type == GGML_TYPE_F32 && scale_src->type == GGML_TYPE_F32 && ggml_nelements(scale_src) == 1); if (ncols % 4 != 0 || n4 > 4096 || nrows > INT_MAX || !rows_ok || !weights_ok || !outs_ok || !scale_ok) { return false; } - const float * scale_ptr = scale ? (const float *) (scale->src[0] == add ? scale->src[1] : scale->src[0])->data : nullptr; + const float * scale_ptr = scale_src ? (const float *) scale_src->data : nullptr; void * q8_1 = nullptr; int64_t q_args[8]; diff --git a/tests/test-backend-ops.cpp b/tests/test-backend-ops.cpp index dc8b09ecea71..5ddd101fc06f 100644 --- a/tests/test-backend-ops.cpp +++ b/tests/test-backend-ops.cpp @@ -9848,14 +9848,28 @@ struct test_rms_norm2 : public test_case { const int64_t n; const bool scale; const ggml_type type_mm; // GGML_TYPE_COUNT: no mat-mul reads the results + const int w_bc; // 1 / 2: w1 / w2 has the shape [w_ne0, k/w_ne0] and is broadcast over the rows (n % (k/w_ne0) == 0) + const int64_t w_ne0; + const bool x_view; // x is a view of the first k values of longer rows + const int64_t n2; // rows are [k, n, n2] - test_rms_norm2(int64_t k, int64_t n, bool scale, ggml_type type_mm) : k(k), n(n), scale(scale), type_mm(type_mm) {} + test_rms_norm2(int64_t k, int64_t n, bool scale, ggml_type type_mm, int w_bc = 0, int64_t w_ne0 = 1, bool x_view = false, int64_t n2 = 1) + : k(k), n(n), scale(scale), type_mm(type_mm), w_bc(w_bc), w_ne0(w_ne0), x_view(x_view), n2(n2) {} std::string vars() override { std::string s = VARS_TO_STR3(k, n, scale); if (type_mm != GGML_TYPE_COUNT) { s += "," + VAR_TO_STR(type_mm); } + if (w_bc != 0) { + s += "," + VARS_TO_STR2(w_bc, w_ne0); + } + if (x_view) { + s += "," + VAR_TO_STR(x_view); + } + if (n2 != 1) { + s += "," + VAR_TO_STR(n2); + } return s; } @@ -9871,10 +9885,16 @@ struct test_rms_norm2 : public test_case { } ggml_tensor * build_graph(ggml_context * ctx) override { - ggml_tensor * x = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, k, n); - ggml_tensor * r = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, k, n); - ggml_tensor * w1 = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, k); - ggml_tensor * w2 = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, k); + ggml_tensor * x; + if (x_view) { + ggml_tensor * xl = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, k + 4, n, n2); + x = ggml_view_3d(ctx, xl, k, n, n2, xl->nb[1], xl->nb[2], 0); + } else { + x = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, k, n, n2); + } + ggml_tensor * r = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, k, n, n2); + ggml_tensor * w1 = w_bc == 1 ? ggml_new_tensor_2d(ctx, GGML_TYPE_F32, w_ne0, k/w_ne0) : ggml_new_tensor_1d(ctx, GGML_TYPE_F32, k); + ggml_tensor * w2 = w_bc == 2 ? ggml_new_tensor_2d(ctx, GGML_TYPE_F32, w_ne0, k/w_ne0) : ggml_new_tensor_1d(ctx, GGML_TYPE_F32, k); ggml_tensor * s = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 1); ggml_tensor * y = ggml_add(ctx, ggml_mul(ctx, ggml_rms_norm(ctx, x, 1e-6f), w1), r); @@ -14661,6 +14681,21 @@ static std::vector> make_test_cases_eval() { } } } + // norm weights of k values broadcast over the rows (not fused), x a view of longer rows, rows in channels + for (bool sc : { false, true }) { + for (int64_t k : {32, 64, 256}) { + for (int w_bc : {1, 2}) { + test_cases.emplace_back(new test_rms_norm2(k, 2*k, sc, GGML_TYPE_COUNT, w_bc, 1)); + test_cases.emplace_back(new test_rms_norm2(k, 2*k, sc, GGML_TYPE_COUNT, w_bc, 4)); + } + } + for (int64_t k : {64, 2560, 5376}) { + test_cases.emplace_back(new test_rms_norm2(k, 7, sc, GGML_TYPE_COUNT, 0, 1, true)); + test_cases.emplace_back(new test_rms_norm2(k, 4, sc, GGML_TYPE_Q8_0, 0, 1, true)); + test_cases.emplace_back(new test_rms_norm2(k, 3, sc, GGML_TYPE_COUNT, 0, 1, false, 2)); + test_cases.emplace_back(new test_rms_norm2(k, 2, sc, GGML_TYPE_Q8_0, 0, 1, false, 3)); + } + } // norm of a layer input followed by quantized mat-vecs: row lengths with and without padding of the q8_1 copy, // every block size of the vectorized norm kernel, mat-vec (<= 8 tokens) and mat-mul From 1828731dd4a418e49de675e9da2f9a6e5949904c Mon Sep 17 00:00:00 2001 From: rafasmour <54501936+rafasmour@users.noreply.github.com> Date: Fri, 4 Dec 2026 09:35:48 +0000 Subject: [PATCH 4/4] cuda : RMS_NORM2 not fused when an inner node of the chain is also an operand ggml_can_fuse_subgraph allows more than one use of an inner node as long as every use is inside the chain. The fused kernel does not write norm1, mul1, norm2 (and the add before a scale), so when one of them is also r, w1 or w2 the kernel read a buffer that nothing wrote. Require one use for each inner node. New test cases: RMS_NORM2_ALIAS (y = mul1 + mul1, y = mul1 + norm1, mul1 = norm1*norm1, z = norm2*norm2, z = norm2*mul1, with and without the scale; 0/20 before, 20/20 with the fix) and RMS_NORM2_EDGE (shapes at the edges of the block sizes, 30/30). Assisted-by: Claude Opus 5.5 --- ggml/src/ggml-cuda/fusion.cu | 6 +++ tests/test-backend-ops.cpp | 78 ++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) diff --git a/ggml/src/ggml-cuda/fusion.cu b/ggml/src/ggml-cuda/fusion.cu index de520946567a..81329d96cf1f 100644 --- a/ggml/src/ggml-cuda/fusion.cu +++ b/ggml/src/ggml-cuda/fusion.cu @@ -2251,6 +2251,12 @@ static int ggml_cuda_try_fuse_rms_norm2(ggml_backend_cuda_context * cuda_ctx, gg if (!ggml_can_fuse_subgraph(cgraph, i, n1 + 2, n1 == 4 ? ops4 : ops3, outputs, 2)) { return 0; } + // the kernel does not write norm1, mul1, norm2 (and the add before a scale): each must have one use, not be an operand of the chain too + for (int k = 0; k <= n1; k++) { + if (i + k != outputs[0] && ggml_node_get_use_count(cgraph, i + k) != 1) { + return 0; + } + } // A thread writes only values of the row it has read, after the reads (block barriers in between), so an output may take // the exact place of x or r (in-place add), but not overlap them otherwise. { diff --git a/tests/test-backend-ops.cpp b/tests/test-backend-ops.cpp index 5ddd101fc06f..ea148e7b8e08 100644 --- a/tests/test-backend-ops.cpp +++ b/tests/test-backend-ops.cpp @@ -9915,6 +9915,62 @@ struct test_rms_norm2 : public test_case { } }; +// RMS_NORM2: shapes at the edges of the block sizes, under the name RMS_NORM2_EDGE +struct test_rms_norm2_edge : public test_rms_norm2 { + using test_rms_norm2::test_rms_norm2; + std::string op_desc(ggml_tensor * t) override { + GGML_UNUSED(t); + return "RMS_NORM2_EDGE"; + } +}; + +// RMS_NORM2: an operand of the chain is itself a node of the chain, which the fused kernel +// does not write. mode 0: r = mul1, 1: r = norm1, 2: w1 = norm1 (n = 1), 3: w2 = norm2 (n = 1), 4: w2 = mul1 (n = 1) +struct test_rms_norm2_alias : public test_case { + const int64_t k; + const int64_t n; + const bool scale; + const int mode; + + test_rms_norm2_alias(int64_t k, int64_t n, bool scale, int mode) : k(k), n(n), scale(scale), mode(mode) {} + + std::string vars() override { + return VARS_TO_STR4(k, n, scale, mode); + } + + std::string op_desc(ggml_tensor * t) override { + GGML_UNUSED(t); + return "RMS_NORM2_ALIAS"; + } + + bool run_whole_graph() override { return true; } + + double max_nmse_err() override { + return 1e-7; + } + + ggml_tensor * build_graph(ggml_context * ctx) override { + ggml_tensor * x = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, k, n); + ggml_tensor * r = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, k, n); + ggml_tensor * w1 = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, k); + ggml_tensor * w2 = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, k); + ggml_tensor * s = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 1); + + ggml_tensor * norm1 = ggml_rms_norm(ctx, x, 1e-6f); + ggml_tensor * mul1 = ggml_mul(ctx, norm1, mode == 2 ? norm1 : w1); + ggml_tensor * y = ggml_add(ctx, mul1, mode == 0 ? mul1 : mode == 1 ? norm1 : r); + if (scale) { + y = ggml_mul(ctx, y, s); + } + ggml_tensor * norm2 = ggml_rms_norm(ctx, y, 1e-6f); + ggml_tensor * z = ggml_mul(ctx, norm2, mode == 3 ? norm2 : mode == 4 ? mul1 : w2); + + ggml_tensor * out = ggml_add(ctx, y, z); + ggml_set_name(out, "out"); + return out; + } +}; + struct test_rms_norm_mul_mat : public test_case { const ggml_type type_a; const int64_t m; @@ -14697,6 +14753,28 @@ static std::vector> make_test_cases_eval() { } } + // RMS_NORM2 at the edges of the block sizes, and chains where an inner node is also an operand (must not be fused) + for (bool sc : { false, true }) { + for (int64_t k : {4, 132, 1028, 4100, 16384, 16388}) { + for (int64_t n : {1, 5}) { + test_cases.emplace_back(new test_rms_norm2_edge(k, n, sc, GGML_TYPE_COUNT)); + } + } + test_cases.emplace_back(new test_rms_norm2_edge(16384, 3, sc, GGML_TYPE_Q8_0)); + test_cases.emplace_back(new test_rms_norm2_edge(2560, 512, sc, GGML_TYPE_COUNT)); + test_cases.emplace_back(new test_rms_norm2_edge(5376, 8, sc, GGML_TYPE_Q4_0)); + for (int mode : {0, 1}) { + for (int64_t k : {256, 2560}) { + test_cases.emplace_back(new test_rms_norm2_alias(k, 4, sc, mode)); + } + } + for (int mode : {2, 3, 4}) { + for (int64_t k : {256, 2560}) { + test_cases.emplace_back(new test_rms_norm2_alias(k, 1, sc, mode)); + } + } + } + // norm of a layer input followed by quantized mat-vecs: row lengths with and without padding of the q8_1 copy, // every block size of the vectorized norm kernel, mat-vec (<= 8 tokens) and mat-mul for (ggml_type type_a : {GGML_TYPE_Q4_0, GGML_TYPE_Q8_0, GGML_TYPE_Q4_K}) {