Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions ggml/src/ggml-cuda/mmvq.cu
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -796,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) < stride_col_dst)) {
if (use_bias) {
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) {
Expand Down Expand Up @@ -848,7 +855,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<type>::bs;
const size_t off = (size_t)(kbx_offset + min(i, i_last)*stride_row_x + kbx_pf) * ggml_cuda_type_traits<type>::bs;
ggml_cuda_prefetch_l2((const char *) vx + off);
if constexpr (has_fusion) {
if (use_gate) {
Expand All @@ -865,11 +872,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);
}
}
}
Expand Down Expand Up @@ -922,7 +929,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) {
Expand Down Expand Up @@ -1043,17 +1050,19 @@ 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);
const int kqs = vdr * (threadIdx.x % (qi/vdr));

#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);
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions tests/test-backend-ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14425,6 +14425,15 @@ static std::vector<std::unique_ptr<test_case>> 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));
}
Expand Down