From be979738bfff07c8f74568957de7242f0715043c Mon Sep 17 00:00:00 2001 From: Daniel Han Date: Sat, 5 Dec 2026 23:44:34 -0800 Subject: [PATCH 1/3] CUDA: bound the partial last row block of mul_mat_vec_q by the matrix rows With small K (rows per block = warps) and rows that are not a multiple of the rows per block, the last row block of a matrix: - for a single-column MUL_MAT_ID, guarded its stores with the token stride instead of the expert's rows, so it wrote rows past the expert into the next slot's first rows, which that slot's own block also writes: the output depended on which block wrote last, and the last slot of the last token wrote past dst; - in mul_mat_vec_q and mul_mat_vec_q_moe, read whole rows past the end of the matrix (only the stores were guarded), past the weight buffer for its last matrix. The store bound is now the slot stride (the rows of dst) for MUL_MAT_ID at one column, the column stride as before otherwise. The rows of the last block past the last row read the last row again; their sums are not stored and the stored rows read what they read before, so every output keeps its bytes. test-backend-ops: MUL_MAT_ID cases with 66 / 70 rows at K 1024 / 2048. --- ggml/src/ggml-cuda/mmvq.cu | 19 +++++++++++++------ tests/test-backend-ops.cpp | 9 +++++++++ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/ggml/src/ggml-cuda/mmvq.cu b/ggml/src/ggml-cuda/mmvq.cu index 5a4a6108bf15..fb8c52cefc88 100644 --- a/ggml/src/ggml-cuda/mmvq.cu +++ b/ggml/src/ggml-cuda/mmvq.cu @@ -757,6 +757,11 @@ static __global__ void mul_mat_vec_q( channel_y = ncols_dst == 1 && ids ? fastmodulo(channel_dst, nchannels_y) : channel_dst; sample_dst = blockIdx.z; + // MUL_MAT_ID at one column: stride_col_dst is the token stride, the rows of a slot end at stride_channel_dst + const uint32_t nrows_dst = ncols_dst == 1 && ids && !shared_expert ? stride_channel_dst : stride_col_dst; + // rows of a partial last block past the matrix re-read its last row (their sums are not stored) + const int i_last = rows_per_cuda_block == 1 ? 0 : max(0, min(rows_per_cuda_block, (int) nrows_dst - row0) - 1); + const uint32_t sample_x = fastdiv(sample_dst, sample_ratio); const uint32_t sample_y = sample_dst; @@ -848,7 +853,7 @@ static __global__ void mul_mat_vec_q( if (kbx_pf < blocks_per_row_x) { #pragma unroll for (int i = 0; i < rows_per_cuda_block; ++i) { - const size_t off = (size_t)(kbx_offset + i*stride_row_x + kbx_pf) * ggml_cuda_type_traits::bs; + const size_t off = (size_t)(kbx_offset + min(i, i_last)*stride_row_x + kbx_pf) * ggml_cuda_type_traits::bs; ggml_cuda_prefetch_l2((const char *) vx + off); if constexpr (has_fusion) { if (use_gate) { @@ -865,11 +870,11 @@ static __global__ void mul_mat_vec_q( #pragma unroll for (int i = 0; i < rows_per_cuda_block; ++i) { tmp[j][i] += vec_dot_q_cuda( - vx, &y[j*stride_col_y + kby], kbx_offset + i*stride_row_x + kbx, kqs); + vx, &y[j*stride_col_y + kby], kbx_offset + min(i, i_last)*stride_row_x + kbx, kqs); if constexpr (has_fusion) { if (use_gate) { tmp_gate[j][i] += vec_dot_q_cuda( - vgate, &y[j*stride_col_y + kby], kbx_offset + i*stride_row_x + kbx, kqs); + vgate, &y[j*stride_col_y + kby], kbx_offset + min(i, i_last)*stride_row_x + kbx, kqs); } } } @@ -922,7 +927,7 @@ static __global__ void mul_mat_vec_q( } } - if (threadIdx.x == i && (rows_per_cuda_block == 1 || uint32_t(row0 + i) < stride_col_dst)) { + if (threadIdx.x == i && (rows_per_cuda_block == 1 || uint32_t(row0 + i) < nrows_dst)) { float result = tmp[j][i]; if constexpr (has_fusion) { if constexpr (type == GGML_TYPE_NVFP4) { @@ -1043,6 +1048,8 @@ static __global__ void mul_mat_vec_q_moe( // partial sum for each thread float tmp[c_rows_per_block] = {0.0f}; float tmp_gate[c_rows_per_block] = {0.0f}; + // rows of a partial last block past the expert re-read its last row (their sums are not stored) + const int i_last = c_rows_per_block == 1 ? 0 : max(0, min(c_rows_per_block, (int) nrows_x - row0) - 1); for (int kbx = threadIdx.x / (qi/vdr); kbx < blocks_per_row_x; kbx += blocks_per_iter) { const int kby = kbx * (qk/QK8_1); @@ -1050,10 +1057,10 @@ static __global__ void mul_mat_vec_q_moe( #pragma unroll for (int i = 0; i < c_rows_per_block; ++i) { - tmp[i] += vec_dot_q_cuda(vx, &y[kby], kbx_offset + i*stride_row_x + kbx, kqs); + tmp[i] += vec_dot_q_cuda(vx, &y[kby], kbx_offset + min(i, i_last)*stride_row_x + kbx, kqs); if constexpr (has_fusion) { if (use_gate) { - tmp_gate[i] += vec_dot_q_cuda(vgate, &y[kby], kbx_offset + i*stride_row_x + kbx, kqs); + tmp_gate[i] += vec_dot_q_cuda(vgate, &y[kby], kbx_offset + min(i, i_last)*stride_row_x + kbx, kqs); } } } diff --git a/tests/test-backend-ops.cpp b/tests/test-backend-ops.cpp index 520f3147080e..6de579ebf50f 100644 --- a/tests/test-backend-ops.cpp +++ b/tests/test-backend-ops.cpp @@ -14425,6 +14425,15 @@ static std::vector> make_test_cases_eval() { // For issue 27873 test_cases.emplace_back(new test_mul_mat_id(GGML_TYPE_IQ2_XXS, GGML_TYPE_F32, 1, 1, false, 1, 8192, 4096)); + // expert rows not a multiple of the small-K mat-vec rows per block + for (ggml_type type_a : {GGML_TYPE_Q4_0, GGML_TYPE_IQ4_XS, GGML_TYPE_Q4_K, GGML_TYPE_Q8_0}) { + for (int n : {1, 2, 4}) { + test_cases.emplace_back(new test_mul_mat_id(type_a, GGML_TYPE_F32, 32, 16, false, 66, n, 1024)); + test_cases.emplace_back(new test_mul_mat_id(type_a, GGML_TYPE_F32, 40, 32, false, 70, n, 2048)); + test_cases.emplace_back(new test_mul_mat_id(type_a, GGML_TYPE_F32, 32, 16, true, 66, n, 1024)); + } + } + for (int k : {1, 63, 65}) { test_cases.emplace_back(new test_mul_mat_id(GGML_TYPE_F16, GGML_TYPE_F32, 1, 1, false, 8, 16, k)); } From e66befc8c74be9c1d4a357dfd52a73a2a31a43d1 Mon Sep 17 00:00:00 2001 From: Daniel Han <23090290+danielhanchen@users.noreply.github.com> Date: Sun, 6 Dec 2026 00:44:30 -0800 Subject: [PATCH 2/3] CUDA: bound the fused bias loads of mul_mat_vec_q by the same rows as the stores --- ggml/src/ggml-cuda/mmvq.cu | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ggml/src/ggml-cuda/mmvq.cu b/ggml/src/ggml-cuda/mmvq.cu index fb8c52cefc88..4924558e276b 100644 --- a/ggml/src/ggml-cuda/mmvq.cu +++ b/ggml/src/ggml-cuda/mmvq.cu @@ -805,7 +805,7 @@ static __global__ void mul_mat_vec_q( // 2. load only on threads that won't die after partial sum calculation const uint32_t channel_bias = ids ? channel_x : channel_dst; if (threadIdx.x < rows_per_cuda_block && threadIdx.y == 0 && - (rows_per_cuda_block == 1 || uint32_t(row0 + threadIdx.x) < stride_col_dst)) { + (rows_per_cuda_block == 1 || uint32_t(row0 + threadIdx.x) < nrows_dst)) { if (use_bias) { x_bias = x_bias + sample_dst * stride_sample_dst + channel_bias * stride_channel_dst + row0; #pragma unroll From 310e3c530eb7236e3e7beb3128e55b335b48c7c8 Mon Sep 17 00:00:00 2001 From: Daniel Han <23090290+danielhanchen@users.noreply.github.com> Date: Sun, 6 Dec 2026 00:55:10 -0800 Subject: [PATCH 3/3] CUDA: bound the fused bias loads inside the prefetch guard so the guard line stays as upstream --- ggml/src/ggml-cuda/mmvq.cu | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/ggml/src/ggml-cuda/mmvq.cu b/ggml/src/ggml-cuda/mmvq.cu index 4924558e276b..79b25ff5eb88 100644 --- a/ggml/src/ggml-cuda/mmvq.cu +++ b/ggml/src/ggml-cuda/mmvq.cu @@ -801,19 +801,21 @@ static __global__ void mul_mat_vec_q( [[maybe_unused]] float x_scales = 1.0f; [[maybe_unused]] float gate_scales = 1.0f; if constexpr (has_fusion) { + // bias rows past the stored rows belong to the next expert or past the tensor + const bool bias_row_ok = rows_per_cuda_block == 1 || uint32_t(row0 + threadIdx.x) < nrows_dst; // 1. Hide latency by prefetching bias, gates and scales here // 2. load only on threads that won't die after partial sum calculation const uint32_t channel_bias = ids ? channel_x : channel_dst; if (threadIdx.x < rows_per_cuda_block && threadIdx.y == 0 && - (rows_per_cuda_block == 1 || uint32_t(row0 + threadIdx.x) < nrows_dst)) { - if (use_bias) { + (rows_per_cuda_block == 1 || uint32_t(row0 + threadIdx.x) < stride_col_dst)) { + if (use_bias && bias_row_ok) { x_bias = x_bias + sample_dst * stride_sample_dst + channel_bias * stride_channel_dst + row0; #pragma unroll for (int j = 0; j < ncols_dst; ++j) { x_biases[j] = x_bias[j * stride_col_dst + threadIdx.x]; } } - if (use_gate_bias) { + if (use_gate_bias && bias_row_ok) { gate_bias = gate_bias + sample_dst * stride_sample_dst + channel_bias * stride_channel_dst + row0; #pragma unroll for (int j = 0; j < ncols_dst; ++j) {