Skip to content
Merged
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
74 changes: 53 additions & 21 deletions ggml/src/ggml-cuda/mmvq.cu
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "vecdotq.cuh"

#include <cstdint>
#include <cstdlib>
#include <type_traits>

// only enabled on DGX Spark, where it is a gain on every type below. On the higher-bandwidth parts the kernel
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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 <ggml_type type, int ncols_dst, bool has_fusion, bool small_k = false, bool halve_iters = false,
ggml_type type_shared = GGML_TYPE_COUNT>
__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,
Expand All @@ -801,8 +807,8 @@ static __global__ void mul_mat_vec_q(
constexpr int qi = ggml_cuda_type_traits<type>::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);
Expand Down Expand Up @@ -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<ggml_type type>
static std::pair<dim3, dim3> 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<ggml_type type, int c_ncols_dst, bool small_k = false, bool halve_iters = false>
template<ggml_type type, int c_ncols_dst, bool small_k = false, bool halve_iters = false, bool q6k_lanes = false>
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,
Expand All @@ -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<type, c_ncols_dst, true, false, false, GGML_TYPE_Q8_0>, launch_params,
vx, vy, ids, fusion, dst, ncols_x, nchannels_y, stride_row_x, stride_col_y, stride_col_dst,
Expand All @@ -1303,15 +1330,15 @@ 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<type, c_ncols_dst, true, small_k, halve_iters>, launch_params,
ggml_cuda_kernel_launch(mul_mat_vec_q<type, c_ncols_dst, true, small_k, halve_iters, GGML_TYPE_COUNT, q6k_lanes>, 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);
return;
}

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<type, c_ncols_dst, false, small_k, halve_iters>, launch_params,
ggml_cuda_kernel_launch(mul_mat_vec_q<type, c_ncols_dst, false, small_k, halve_iters, GGML_TYPE_COUNT, q6k_lanes>, 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);
Expand Down Expand Up @@ -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) !=
Expand All @@ -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<dim3, dim3> dims = calc_launch_params<type>(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<type, c_ncols_dst, c_small_k, c_halve_iters>(
nsamples_dst, warp_size, table_id, c_small_k, c_halve_iters, c_q6k_lanes);
mul_mat_vec_q_switch_fusion<type, c_ncols_dst, c_small_k, c_halve_iters, c_q6k_lanes>(
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: {
Expand Down
22 changes: 22 additions & 0 deletions tests/test-backend-ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17458,6 +17458,17 @@ static std::vector<std::unique_ptr<test_case>> 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<int64_t, int64_t>{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
Expand Down Expand Up @@ -18297,6 +18308,17 @@ static std::vector<std::unique_ptr<test_case>> 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<int64_t, int64_t>{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;
}

Expand Down
Loading