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
161 changes: 157 additions & 4 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 @@ -33,6 +34,102 @@ static constexpr __host__ __device__ bool mmvq_should_prefetch(ggml_type type) {

typedef float (*vec_dot_q_cuda_t)(const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs);

// Software-pipelined 1-column mat-vec on RDNA3 (RX 7900 XT) for the IQ types: the loads of the weight block and of the q8_1
// block of the next loop trip are issued before the arithmetic of the current one (register double buffer), so the DRAM wait of
// a trip no longer sits in front of the dependent grid-table gather of the same trip. Bit-identical to the plain loop.
// ld_t holds one trip's loads, load() issues them, dot() is the arithmetic (see vecdotq.cuh).
template <ggml_type type>
struct mmvq_iq_pipe {
static constexpr bool eligible = false;
};

#if defined(GGML_USE_HIP)
template <> struct mmvq_iq_pipe<GGML_TYPE_IQ2_S> {
static constexpr bool eligible = true;
using ld_t = mmvq_iq_ld_iq2_s;
static __device__ __forceinline__ ld_t load(const void * vbq, const block_q8_1 * bq8_1, const int & kbx, const int & iqs) {
return mmvq_iq_load_iq2_s(vbq, bq8_1, kbx, iqs);
}
static __device__ __forceinline__ float dot(const ld_t & r, const int &) { return mmvq_iq_dot_iq2_s(r); }
};
template <> struct mmvq_iq_pipe<GGML_TYPE_IQ3_XXS> {
static constexpr bool eligible = true;
using ld_t = mmvq_iq_ld_iq3_xxs;
static __device__ __forceinline__ ld_t load(const void * vbq, const block_q8_1 * bq8_1, const int & kbx, const int & iqs) {
return mmvq_iq_load_iq3_xxs(vbq, bq8_1, kbx, iqs);
}
static __device__ __forceinline__ float dot(const ld_t & r, const int &) { return mmvq_iq_dot_iq3_xxs(r); }
};
template <> struct mmvq_iq_pipe<GGML_TYPE_IQ3_S> {
static constexpr bool eligible = true;
using ld_t = mmvq_iq_ld_iq3_s;
static __device__ __forceinline__ ld_t load(const void * vbq, const block_q8_1 * bq8_1, const int & kbx, const int & iqs) {
return mmvq_iq_load_iq3_s(vbq, bq8_1, kbx, iqs);
}
static __device__ __forceinline__ float dot(const ld_t & r, const int & iqs) { return mmvq_iq_dot_iq3_s(r, iqs); }
};
// IQ4_XS has no table gather (the 16-entry table is a v_perm_b32 on registers), so it only gains the overlap of the
// next trip's DRAM loads with the current arithmetic. Opt-in, see mmvq_iq_pipe_enabled.
template <> struct mmvq_iq_pipe<GGML_TYPE_IQ4_XS> {
static constexpr bool eligible = true;
using ld_t = mmvq_iq_ld_iq4_xs;
static __device__ __forceinline__ ld_t load(const void * vbq, const block_q8_1 * bq8_1, const int & kbx, const int & iqs) {
return mmvq_iq_load_iq4_xs(vbq, bq8_1, kbx, iqs);
}
static __device__ __forceinline__ float dot(const ld_t & r, const int & iqs) { return mmvq_iq_dot_iq4_xs(r, iqs); }
};
#endif // defined(GGML_USE_HIP)

// The kbx loop of one thread, software-pipelined: the loads of the next trip are issued together with the grid gathers of the
// current trip, so the DRAM wait of trip n+1 overlaps the gather and the dp4a of trip n instead of following them. The last trip
// is peeled, so the paths need no merge of an in-flight load through a register copy (which would wait for it). The machine
// scheduler issues the gathers of the current trip ahead of the loads of the next one (checked in the ISA), which keeps the
// in-order vmcnt wait of the arithmetic from also covering the loads of the next trip. Two register buffers at once cost
// 50..56 VGPRs (89..95 with the gate), a third or a barrier that forces the loads first cost up to 183.
template <ggml_type type, bool gate, int q8_per_block, int blocks_per_iter>
static __device__ __forceinline__ void mmvq_iq_pipe_loop(
const void * vx, const void * vgate, const block_q8_1 * y, const int kbx_offset, int kbx, const int blocks_per_row_x,
const int kqs, float & tmp, float & tmp_gate) {
using pipe_t = mmvq_iq_pipe<type>;
if (kbx >= blocks_per_row_x) {
return;
}
auto cur = pipe_t::load(vx, &y[kbx * q8_per_block], kbx_offset + kbx, kqs);
auto cur_gate = [&]() { if constexpr (gate) { return pipe_t::load(vgate, &y[kbx * q8_per_block], kbx_offset + kbx, kqs); } else { return 0; } }();
while (kbx + blocks_per_iter < blocks_per_row_x) {
kbx += blocks_per_iter;
const auto nxt = pipe_t::load(vx, &y[kbx * q8_per_block], kbx_offset + kbx, kqs);
[[maybe_unused]] const auto nxt_gate = [&]() { if constexpr (gate) { return pipe_t::load(vgate, &y[kbx * q8_per_block], kbx_offset + kbx, kqs); } else { return 0; } }();
tmp += pipe_t::dot(cur, kqs);
cur = nxt;
if constexpr (gate) {
tmp_gate += pipe_t::dot(cur_gate, kqs);
cur_gate = nxt_gate;
}
}
tmp += pipe_t::dot(cur, kqs);
if constexpr (gate) {
tmp_gate += pipe_t::dot(cur_gate, kqs);
}
}

// Host side: whether the pipelined kernel is used for this type. GGML_CUDA_NO_IQ_MMVQ_PIPELINE=1 selects the plain loop for all of
// them. IQ4_XS stays on the plain loop unless GGML_CUDA_IQ4_XS_MMVQ_PIPELINE=1 (no gather to hide, to be measured).
template <ggml_type type>
static bool mmvq_iq_pipe_enabled() {
if constexpr (!mmvq_iq_pipe<type>::eligible) {
return false;
} else {
static const bool off = []() { const char * e = getenv("GGML_CUDA_NO_IQ_MMVQ_PIPELINE"); return e != nullptr && atoi(e) != 0; }();
if constexpr (type == GGML_TYPE_IQ4_XS) {
static const bool on = []() { const char * e = getenv("GGML_CUDA_IQ4_XS_MMVQ_PIPELINE"); return e != nullptr && atoi(e) != 0; }();
return on && !off;
} else {
return !off;
}
}
}

static constexpr __device__ vec_dot_q_cuda_t get_vec_dot_q_cuda(ggml_type type) {
switch (type) {
case GGML_TYPE_Q1_0: return vec_dot_q1_0_q8_1;
Expand Down Expand Up @@ -772,8 +869,9 @@ 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
// iq_pipe: software-pipelined kbx loop of the IQ types on RDNA3, see mmvq_iq_pipe
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>
ggml_type type_shared = GGML_TYPE_COUNT, bool iq_pipe = false>
__launch_bounds__(calc_nwarps(type, ncols_dst, get_device_table_id(), small_k, halve_iters)*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,
Expand Down Expand Up @@ -924,7 +1022,29 @@ static __global__ void mul_mat_vec_q(
}
}

for (int kbx = shared_done ? blocks_per_row_x : tid / (qi/vdr); kbx < blocks_per_row_x; kbx += blocks_per_iter) {
#if defined(GGML_USE_HIP) && defined(RDNA3_0)
constexpr bool pipelined = iq_pipe && ncols_dst == 1 && rows_per_cuda_block == 1 && !small_k && !halve_iters &&
mmvq_iq_pipe<type>::eligible;
#else
constexpr bool pipelined = false;
#endif
if constexpr (pipelined) {
// The loads of trip n+1 are issued before the arithmetic of trip n. Same operations in the same order per thread
// as the plain loop below, so the result does not change.
const int kqs = vdr * (tid % (qi/vdr));
const int kbx_first = shared_done ? blocks_per_row_x : tid / (qi/vdr);
bool gate = false;
if constexpr (has_fusion) {
gate = use_gate;
}
if (gate) {
mmvq_iq_pipe_loop<type, true, qk/QK8_1, blocks_per_iter>(vx, vgate, y, kbx_offset, kbx_first, blocks_per_row_x, kqs, tmp[0][0], tmp_gate[0][0]);
} else {
mmvq_iq_pipe_loop<type, false, qk/QK8_1, blocks_per_iter>(vx, vx, y, kbx_offset, kbx_first, blocks_per_row_x, kqs, tmp[0][0], tmp_gate[0][0]);
}
}

for (int kbx = (pipelined || shared_done) ? blocks_per_row_x : tid / (qi/vdr); kbx < blocks_per_row_x; kbx += blocks_per_iter) {
const int kby = kbx * (qk/QK8_1); // y block index that aligns with kbx

// x block quant index when casting the quants to int
Expand Down Expand Up @@ -1275,7 +1395,10 @@ static void mul_mat_vec_q_switch_fusion(
const uint32_t stride_channel_y, const uint32_t stride_channel_dst, const uint3 sample_ratio,
const uint32_t stride_sample_x, const uint32_t stride_sample_y, const uint32_t stride_sample_dst,
const dim3 & block_nums, const dim3 & block_dims, const int nbytes_shared,
const uint32_t ids_stride, cudaStream_t stream) {
const uint32_t ids_stride, cudaStream_t stream, const bool iq_pipe = false) {

// the software-pipelined kernel only exists for the IQ types, 1 column, no small_k (see mmvq_iq_pipe)
constexpr bool c_pipe_type = c_ncols_dst == 1 && !small_k && !halve_iters && mmvq_iq_pipe<type>::eligible;

const bool has_fusion = fusion.gate != nullptr || fusion.x_bias != nullptr || fusion.gate_bias != nullptr ||
fusion.x_scale != nullptr || fusion.gate_scale != nullptr || fusion.unary_op != GGML_UNARY_OP_COUNT;
Expand All @@ -1284,6 +1407,15 @@ static void mul_mat_vec_q_switch_fusion(
#ifdef GGML_USE_HIP
if constexpr (c_ncols_dst == 1 && !small_k && !halve_iters && mmvq_mixed_shared_expert(type)) {
if (fusion.shared_up != nullptr && fusion.shared_type == GGML_TYPE_Q8_0) {
if constexpr (c_pipe_type) {
if (iq_pipe) {
ggml_cuda_kernel_launch(mul_mat_vec_q<type, c_ncols_dst, true, false, false, GGML_TYPE_Q8_0, true>, 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;
}
}
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,
channel_ratio, stride_channel_x, stride_channel_y, stride_channel_dst,
Expand All @@ -1293,6 +1425,15 @@ static void mul_mat_vec_q_switch_fusion(
}
#endif // GGML_USE_HIP
GGML_ASSERT(fusion.shared_type == GGML_TYPE_COUNT);
if constexpr (c_pipe_type) {
if (iq_pipe) {
ggml_cuda_kernel_launch(mul_mat_vec_q<type, c_ncols_dst, true, small_k, halve_iters, GGML_TYPE_COUNT, true>, 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;
}
}
ggml_cuda_kernel_launch(mul_mat_vec_q<type, c_ncols_dst, true, small_k, halve_iters>, 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,
Expand All @@ -1301,6 +1442,15 @@ 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);
if constexpr (c_pipe_type) {
if (iq_pipe) {
ggml_cuda_kernel_launch(mul_mat_vec_q<type, c_ncols_dst, false, small_k, halve_iters, GGML_TYPE_COUNT, true>, 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;
}
}
ggml_cuda_kernel_launch(mul_mat_vec_q<type, c_ncols_dst, false, small_k, halve_iters>, 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,
Expand Down Expand Up @@ -1445,6 +1595,9 @@ static void mul_mat_vec_q_switch_ncols_dst(
// static, else MSVC lambda capture breaks the constexpr uses below
static constexpr int c_ncols_dst = 1;

// RDNA3 (RX 7900 XT) only: software-pipelined kbx loop for the IQ types, see mmvq_iq_pipe
const bool use_iq_pipe = table_id == MMVQ_PARAMETERS_RDNA3_0 && mmvq_iq_pipe_enabled<type>();

// 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) {
constexpr bool c_small_k = decltype(small_k_tag)::value;
Expand All @@ -1461,7 +1614,7 @@ static void mul_mat_vec_q_switch_ncols_dst(
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);
stream, use_iq_pipe);
};

if (should_use_small_k(c_ncols_dst)) {
Expand Down
Loading
Loading