Skip to content

Commit 683f0c7

Browse files
authored
Only index by compile times + always multiply/add (ggml-org#25445)
The first one avoids relying on compile to optimize local memory away, and the second is cheaper than issuing control flow statements
1 parent 259f2e2 commit 683f0c7

1 file changed

Lines changed: 23 additions & 32 deletions

File tree

ggml/src/ggml-cuda/mmvq.cu

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -549,8 +549,8 @@ static __global__ void mul_mat_vec_q(
549549

550550
[[maybe_unused]] float x_biases[ncols_dst] = { 0.0f };
551551
[[maybe_unused]] float gate_biases[ncols_dst] = { 0.0f };
552-
[[maybe_unused]] float x_scales;
553-
[[maybe_unused]] float gate_scales;
552+
[[maybe_unused]] float x_scales = 1.0f;
553+
[[maybe_unused]] float gate_scales = 1.0f;
554554
if constexpr (has_fusion) {
555555
// 1. Hide latency by prefetching bias, gates and scales here
556556
// 2. load only on threads that won't die after partial sum calculation
@@ -655,47 +655,38 @@ static __global__ void mul_mat_vec_q(
655655
tmp_gate[j][i] = warp_reduce_sum<warp_size>(tmp_gate[j][i]);
656656
}
657657
}
658-
}
659658

660-
if (threadIdx.x < rows_per_cuda_block && (rows_per_cuda_block == 1 || uint32_t(row0 + threadIdx.x) < stride_col_dst)) {
661-
float result = tmp[j][threadIdx.x];
662-
if constexpr (has_fusion) {
663-
if constexpr (type == GGML_TYPE_NVFP4) {
664-
if (use_scale) {
659+
if (threadIdx.x == i && (rows_per_cuda_block == 1 || uint32_t(row0 + i) < stride_col_dst)) {
660+
float result = tmp[j][i];
661+
if constexpr (has_fusion) {
662+
if constexpr (type == GGML_TYPE_NVFP4) {
665663
result *= x_scales;
666664
}
667-
}
668-
if (use_bias) {
669665
result += x_biases[j];
670-
}
671-
if (use_gate) {
672-
float gate_value = tmp_gate[j][threadIdx.x];
673-
if constexpr (type == GGML_TYPE_NVFP4) {
674-
if (use_gate_scale) {
666+
if (use_gate) {
667+
float gate_value = tmp_gate[j][i];
668+
if constexpr (type == GGML_TYPE_NVFP4) {
675669
gate_value *= gate_scales;
676670
}
677-
}
678-
if (use_gate_bias) {
679671
gate_value += gate_biases[j];
680-
}
681-
switch (active_glu) {
682-
case GGML_GLU_OP_SWIGLU:
683-
result *= ggml_cuda_op_silu_single(gate_value);
684-
break;
685-
case GGML_GLU_OP_GEGLU:
686-
result *= ggml_cuda_op_gelu_single(gate_value);
687-
break;
688-
case GGML_GLU_OP_SWIGLU_OAI: {
689-
result = ggml_cuda_op_swiglu_oai_single(gate_value, result);
690-
break;
672+
switch (active_glu) {
673+
case GGML_GLU_OP_SWIGLU:
674+
result *= ggml_cuda_op_silu_single(gate_value);
675+
break;
676+
case GGML_GLU_OP_GEGLU:
677+
result *= ggml_cuda_op_gelu_single(gate_value);
678+
break;
679+
case GGML_GLU_OP_SWIGLU_OAI:
680+
result = ggml_cuda_op_swiglu_oai_single(gate_value, result);
681+
break;
682+
default:
683+
result = result * gate_value;
684+
break;
691685
}
692-
default:
693-
result = result * gate_value;
694-
break;
695686
}
696687
}
688+
dst[j*stride_col_dst + i] = result;
697689
}
698-
dst[j*stride_col_dst + threadIdx.x] = result;
699690
}
700691
}
701692

0 commit comments

Comments
 (0)