From 0ffe5c015935a1683b83dc3556c927ada731f441 Mon Sep 17 00:00:00 2001 From: rafasmour <54501936+rafasmour@users.noreply.github.com> Date: Fri, 4 Dec 2026 11:31:54 +0000 Subject: [PATCH] mmvq: opt-in Q6_K single-warp 4-row variant on RDNA3 (GGML_CUDA_MMVQ_Q6K_LANES) For Q6_K, one column, RDNA3_0: rows of 9..11 superblocks (K=2304/2560/2816) run one warp per row with four rows per block instead of four warps on one row (the third trip of the 4 warp block is part idle), and rows of 1..2 superblocks (K<=512, e.g. Qwen4 hc_up 8192x256) get four rows per block instead of one. Needs nrows % 4 == 0, otherwise the old path is used. Fused gate/up and the shared expert go through the same kernel. Opt-in because the row reduction order changes. Adds MUL_MAT / MUL_MAT_FUSION cases for these shapes (eval and perf). Assisted-by: Claude Sonnet 5.5 --- ggml/src/ggml-cuda/mmvq.cu | 74 +++++++++++++++++++++++++++----------- tests/test-backend-ops.cpp | 22 ++++++++++++ 2 files changed, 75 insertions(+), 21 deletions(-) diff --git a/ggml/src/ggml-cuda/mmvq.cu b/ggml/src/ggml-cuda/mmvq.cu index 6247ba2f77f3..8f94039d664d 100644 --- a/ggml/src/ggml-cuda/mmvq.cu +++ b/ggml/src/ggml-cuda/mmvq.cu @@ -4,6 +4,7 @@ #include "vecdotq.cuh" #include +#include #include // only enabled on DGX Spark, where it is a gain on every type below. On the higher-bandwidth parts the kernel @@ -547,7 +548,8 @@ static constexpr __device__ int get_mmvq_mmid_max_batch_for_device() { #endif } -static constexpr __host__ __device__ int calc_nwarps(ggml_type type, int ncols_dst, mmvq_parameter_table_id table_id, bool small_k = false, bool halve_iters = false) { +// q6k_lanes: RDNA3 Q6_K ncols_dst == 1 variant with one warp per row and 4 rows per block, see mmvq_q6k_lanes_shape +static constexpr __host__ __device__ int calc_nwarps(ggml_type type, int ncols_dst, mmvq_parameter_table_id table_id, bool small_k = false, bool halve_iters = false, bool q6k_lanes = false) { if (table_id == MMVQ_PARAMETERS_GENERIC || table_id == MMVQ_PARAMETERS_PASCAL_DP4A) { // Pascal cc 6.1/6.2 decode is bandwidth-bound on small SMs: 2 warps beat 4 for single-token. if (table_id == MMVQ_PARAMETERS_PASCAL_DP4A && ncols_dst == 1) { @@ -610,7 +612,7 @@ static constexpr __host__ __device__ int calc_nwarps(ggml_type type, int ncols_d } if (table_id == MMVQ_PARAMETERS_RDNA3_0) { // RDNA3 (RX 7900 XT): rows shorter than one loop trip of the block (small_k) and the IQ types want a single warp. - if (ncols_dst == 1 && !small_k) { + if (ncols_dst == 1 && !small_k && !(q6k_lanes && type == GGML_TYPE_Q6_K)) { switch (type) { case GGML_TYPE_Q4_0: case GGML_TYPE_Q4_1: @@ -714,7 +716,7 @@ static constexpr __host__ __device__ bool mmvq_two_rows_ncols1(ggml_type type) { } } -static constexpr __host__ __device__ int calc_rows_per_block(ggml_type type, int ncols_dst, int table_id, bool small_k = false, int nwarps = 1) { +static constexpr __host__ __device__ int calc_rows_per_block(ggml_type type, int ncols_dst, int table_id, bool small_k = false, int nwarps = 1, bool q6k_lanes = false) { if (table_id == MMVQ_PARAMETERS_GENERIC || table_id == MMVQ_PARAMETERS_PASCAL_DP4A || table_id == MMVQ_PARAMETERS_GCN || table_id == MMVQ_PARAMETERS_TURING || table_id == MMVQ_PARAMETERS_GB10 || table_id == MMVQ_PARAMETERS_RDNA1) { switch (ncols_dst) { case 1: @@ -737,6 +739,10 @@ static constexpr __host__ __device__ int calc_rows_per_block(ggml_type type, int return 1; } } + if (table_id == MMVQ_PARAMETERS_RDNA3_0) { + // One warp covers each Q6_K row on its own, four rows per block share the q8_1 loads and keep four weight loads in flight. + return q6k_lanes && type == GGML_TYPE_Q6_K && ncols_dst == 1 ? 4 : 1; + } if (table_id == MMVQ_PARAMETERS_ORIN) { switch (ncols_dst) { case 1: @@ -783,8 +789,8 @@ static constexpr __host__ __device__ bool mmvq_mixed_shared_expert(const ggml_ty // type_shared: type of the shared expert if it differs from type, see mmvq_mixed_shared_expert template -__launch_bounds__(calc_nwarps(type, ncols_dst, get_device_table_id(), small_k, halve_iters)*ggml_cuda_get_physical_warp_size(), 1) + ggml_type type_shared = GGML_TYPE_COUNT, bool q6k_lanes = false> +__launch_bounds__(calc_nwarps(type, ncols_dst, get_device_table_id(), small_k, halve_iters, q6k_lanes)*ggml_cuda_get_physical_warp_size(), 1) static __global__ void mul_mat_vec_q( const void * vx_ptr, const void * vy_ptr, const int32_t * ids_ptr, const ggml_cuda_mm_fusion_args_device fusion, float * dst_ptr, const uint32_t ncols_x, const uint3 nchannels_y, const uint32_t stride_row_x, const uint32_t stride_col_y, @@ -801,8 +807,8 @@ static __global__ void mul_mat_vec_q( constexpr int qi = ggml_cuda_type_traits::qi; constexpr mmvq_parameter_table_id table_id = get_device_table_id(); constexpr int vdr = get_vdr_mmvq(type, ncols_dst, table_id); - constexpr int nwarps = calc_nwarps(type, ncols_dst, table_id, small_k, halve_iters); - constexpr int rows_per_cuda_block = calc_rows_per_block(type, ncols_dst, table_id, small_k, nwarps); + constexpr int nwarps = calc_nwarps(type, ncols_dst, table_id, small_k, halve_iters, q6k_lanes); + constexpr int rows_per_cuda_block = calc_rows_per_block(type, ncols_dst, table_id, small_k, nwarps, q6k_lanes); constexpr int warp_size = ggml_cuda_get_physical_warp_size(); constexpr vec_dot_q_cuda_t vec_dot_q_cuda = get_vec_dot_q_cuda(type, vdr); @@ -1265,19 +1271,40 @@ static __global__ void mul_mat_vec_q_moe( } } +// Opt-in (GGML_CUDA_MMVQ_Q6K_LANES=1): the row reduction order of the affected Q6_K shapes changes. +static bool mmvq_q6k_lanes_enabled() { + static const bool enabled = [] { + const char * env = getenv("GGML_CUDA_MMVQ_Q6K_LANES"); + return env != nullptr && atoi(env) != 0; + }(); + return enabled; +} + +// Q6_K shapes for the variant. Rows of 9 to 11 superblocks (K = 2304, 2560, 2816) take three trips of the 4 warp block with +// the last one part idle, one warp per row walks them with every lane busy. Rows of 1 to 2 superblocks (K <= 512) already +// run one warp per row through small_k, the variant adds three more rows per block (fewer blocks, more loads in flight). +// A superblock is covered by exactly one warp (qi == 32). +static bool mmvq_q6k_lanes_shape(const int blocks_per_row, const int nrows) { + if (nrows % 4 != 0) { + return false; // the variant reads 4 rows per block without a row bound + } + return blocks_per_row <= 2 || (blocks_per_row >= 9 && blocks_per_row <= 11); +} + template static std::pair calc_launch_params( const int ncols_dst, const int nrows_x, const int nchannels_dst, const int nsamples_or_ntokens, - const int warp_size, const mmvq_parameter_table_id table_id, const bool small_k = false, const bool halve_iters = false) { - const int nwarps = calc_nwarps(type, ncols_dst, table_id, small_k, halve_iters); - const int rpb = calc_rows_per_block(type, ncols_dst, table_id, small_k, nwarps); + const int warp_size, const mmvq_parameter_table_id table_id, const bool small_k = false, const bool halve_iters = false, + const bool q6k_lanes = false) { + const int nwarps = calc_nwarps(type, ncols_dst, table_id, small_k, halve_iters, q6k_lanes); + const int rpb = calc_rows_per_block(type, ncols_dst, table_id, small_k, nwarps, q6k_lanes); const int64_t nblocks = (nrows_x + rpb - 1) / rpb; const dim3 block_nums(nblocks, nchannels_dst, nsamples_or_ntokens); const dim3 block_dims(warp_size, nwarps, 1); return {block_nums, block_dims}; } -template +template static void mul_mat_vec_q_switch_fusion( const void * vx, const void * vy, const int32_t * ids, const ggml_cuda_mm_fusion_args_device fusion, float * dst, const uint32_t ncols_x, const uint3 nchannels_y, const uint32_t stride_row_x, const uint32_t stride_col_y, @@ -1292,7 +1319,7 @@ static void mul_mat_vec_q_switch_fusion( if (has_fusion) { const ggml_cuda_kernel_launch_params launch_params = ggml_cuda_kernel_launch_params(block_nums, block_dims, nbytes_shared, stream); #ifdef GGML_USE_HIP - if constexpr (c_ncols_dst == 1 && !small_k && !halve_iters && mmvq_mixed_shared_expert(type)) { + if constexpr (c_ncols_dst == 1 && !small_k && !halve_iters && !q6k_lanes && mmvq_mixed_shared_expert(type)) { if (fusion.shared_up != nullptr && fusion.shared_type == GGML_TYPE_Q8_0) { ggml_cuda_kernel_launch(mul_mat_vec_q, launch_params, vx, vy, ids, fusion, dst, ncols_x, nchannels_y, stride_row_x, stride_col_y, stride_col_dst, @@ -1303,7 +1330,7 @@ static void mul_mat_vec_q_switch_fusion( } #endif // GGML_USE_HIP GGML_ASSERT(fusion.shared_type == GGML_TYPE_COUNT); - ggml_cuda_kernel_launch(mul_mat_vec_q, launch_params, + ggml_cuda_kernel_launch(mul_mat_vec_q, launch_params, vx, vy, ids, fusion, dst, ncols_x, nchannels_y, stride_row_x, stride_col_y, stride_col_dst, channel_ratio, stride_channel_x, stride_channel_y, stride_channel_dst, sample_ratio, stride_sample_x, stride_sample_y, stride_sample_dst, ids_stride); @@ -1311,7 +1338,7 @@ static void mul_mat_vec_q_switch_fusion( } const ggml_cuda_kernel_launch_params launch_params = ggml_cuda_kernel_launch_params(block_nums, block_dims, nbytes_shared, stream); - ggml_cuda_kernel_launch(mul_mat_vec_q, launch_params, + ggml_cuda_kernel_launch(mul_mat_vec_q, launch_params, vx, vy, ids, fusion, dst, ncols_x, nchannels_y, stride_row_x, stride_col_y, stride_col_dst, channel_ratio, stride_channel_x, stride_channel_y, stride_channel_dst, sample_ratio, stride_sample_x, stride_sample_y, stride_sample_dst, ids_stride); @@ -1456,8 +1483,10 @@ static void mul_mat_vec_q_switch_ncols_dst( static constexpr int c_ncols_dst = 1; // Tag types keep the flags compile-time, so __launch_bounds__ matches what is launched. - const auto launch = [&](auto small_k_tag, auto halve_iters_tag) { + const auto launch = [&](auto small_k_tag, auto halve_iters_tag, auto q6k_lanes_tag) { constexpr bool c_small_k = decltype(small_k_tag)::value; + // Only Q6_K has the variant, other types would compile an identical kernel. + constexpr bool c_q6k_lanes = decltype(q6k_lanes_tag)::value && type == GGML_TYPE_Q6_K; // Types the table does not promote would compile a second, identical kernel. constexpr bool c_promoted = calc_nwarps(type, c_ncols_dst, MMVQ_PARAMETERS_GB10, false, true) != @@ -1466,20 +1495,23 @@ static void mul_mat_vec_q_switch_ncols_dst( constexpr bool c_halve_iters = decltype(halve_iters_tag)::value && c_promoted; const std::pair dims = calc_launch_params(c_ncols_dst, nrows_x, nchannels_dst + (fusion.shared_up != nullptr), - nsamples_dst, warp_size, table_id, c_small_k, c_halve_iters); - mul_mat_vec_q_switch_fusion( + nsamples_dst, warp_size, table_id, c_small_k, c_halve_iters, c_q6k_lanes); + mul_mat_vec_q_switch_fusion( vx, vy, ids, fusion, dst, ncols_x, nchannels_y_fd, stride_row_x, stride_col_y, stride_col_dst, channel_ratio_fd, stride_channel_x, stride_channel_y, stride_channel_dst, sample_ratio_fd, stride_sample_x, stride_sample_y, stride_sample_dst, dims.first, dims.second, 0, ids_stride, stream); }; - if (should_use_small_k(c_ncols_dst)) { - launch(std::true_type{}, std::false_type{}); + if (type == GGML_TYPE_Q6_K && table_id == MMVQ_PARAMETERS_RDNA3_0 && mmvq_q6k_lanes_enabled() && + mmvq_q6k_lanes_shape(blocks_per_row_x, nrows_x)) { + launch(std::false_type{}, std::false_type{}, std::true_type{}); + } else if (should_use_small_k(c_ncols_dst)) { + launch(std::true_type{}, std::false_type{}, std::false_type{}); } else if (should_halve_iters()) { - launch(std::false_type{}, std::true_type{}); + launch(std::false_type{}, std::true_type{}, std::false_type{}); } else { - launch(std::false_type{}, std::false_type{}); + launch(std::false_type{}, std::false_type{}, std::false_type{}); } } break; case 2: { diff --git a/tests/test-backend-ops.cpp b/tests/test-backend-ops.cpp index 35074227fed1..58b723a80543 100644 --- a/tests/test-backend-ops.cpp +++ b/tests/test-backend-ops.cpp @@ -17458,6 +17458,17 @@ static std::vector> make_test_cases_eval() { } } + // [w2 q6k-lane-waste] Q6_K mat-vec, one column: rows of 1 to 2 and of 9 to 11 superblocks (RDNA3 GGML_CUDA_MMVQ_Q6K_LANES), + // with the default shapes and a row count that is not a multiple of 4 next to them. + for (auto [rows, k] : {std::pair{10240, 2560}, {2560, 2560}, {16384, 2304}, {8192, 256}, {2048, 512}, {4096, 2816}, {4099, 2560}, {10240, 3072}, {2560, 5120}}) { + test_cases.emplace_back(new test_mul_mat(GGML_TYPE_Q6_K, GGML_TYPE_F32, rows, 1, k, {1, 1}, {1, 1})); + } + // fused gate/up + GLU on the same path, then the same with experts + for (int64_t k : {2560, 2304, 256}) { + test_cases.emplace_back(new test_mul_mat_fusion(GGML_TYPE_Q6_K, GGML_GLU_OP_SWIGLU, 1, 10240, k, false, 1, 1, false, false, true, false, {1, 1})); + } + test_cases.emplace_back(new test_mul_mat_fusion(GGML_TYPE_Q6_K, GGML_GLU_OP_SWIGLU, 1, 512, 2560, true, 16, 8, false, false, true, false)); + return test_cases; } #ifdef _MSC_VER @@ -18297,6 +18308,17 @@ static std::vector> make_test_cases_perf() { } + // [w2 q6k-lane-waste] Q6_K mat-vec, one column: rows of 1 to 2 and of 9 to 11 superblocks (RDNA3 GGML_CUDA_MMVQ_Q6K_LANES), + // with the default shapes and a row count that is not a multiple of 4 next to them. + for (auto [rows, k] : {std::pair{10240, 2560}, {2560, 2560}, {16384, 2304}, {8192, 256}, {2048, 512}, {4096, 2816}, {4099, 2560}, {10240, 3072}, {2560, 5120}}) { + test_cases.emplace_back(new test_mul_mat(GGML_TYPE_Q6_K, GGML_TYPE_F32, rows, 1, k, {1, 1}, {1, 1})); + } + // fused gate/up + GLU on the same path, then the same with experts + for (int64_t k : {2560, 2304, 256}) { + test_cases.emplace_back(new test_mul_mat_fusion(GGML_TYPE_Q6_K, GGML_GLU_OP_SWIGLU, 1, 10240, k, false, 1, 1, false, false, true, false, {1, 1})); + } + test_cases.emplace_back(new test_mul_mat_fusion(GGML_TYPE_Q6_K, GGML_GLU_OP_SWIGLU, 1, 512, 2560, true, 16, 8, false, false, true, false)); + return test_cases; }