From c4a1766b5d71f9c4ac996131f99b07c1ed2b430f Mon Sep 17 00:00:00 2001 From: rafasmour <54501936+rafasmour@users.noreply.github.com> Date: Fri, 4 Dec 2026 12:29:42 +0000 Subject: [PATCH] mmvq: software-pipelined kbx loop for IQ2_S/IQ3_S/IQ3_XXS (and opt-in IQ4_XS) on RDNA3 On RDNA3 (RX 7900 XT) the 1-column mat-vec of the grid IQ types waits on DRAM for the weight block of each loop trip and only then issues the dependent grid-table gather, with no overlap (nothing prefetches on this arch, the L2 prefetch is GB10 only). Split vec_dot_iq{2_s,3_xxs,3_s,4_xs} into a load stage and an arithmetic stage and issue the loads of trip n+1 together with the gathers of trip n (register double buffer, last trip peeled). Same operations in the same order per thread, so the result is bit-identical. - plain, MUL_MAT_ID (ids) and fused gate/up kernels, ncols_dst == 1, !small_k, rows_per_block == 1 (RDNA3_0 table), via a new template bool on mul_mat_vec_q - kill switch GGML_CUDA_NO_IQ_MMVQ_PIPELINE=1 (host side, selects the old kernel) - IQ4_XS has no table gather (v_perm on registers): opt-in with GGML_CUDA_IQ4_XS_MMVQ_PIPELINE=1 until measured - test-backend-ops: eval cases for every trip count / tail of the loop Assisted-by: Claude Sonnet 5.5 --- ggml/src/ggml-cuda/mmvq.cu | 161 +++++++++++++++++++++++- ggml/src/ggml-cuda/vecdotq.cuh | 219 +++++++++++++++++++++++++++++++++ tests/test-backend-ops.cpp | 12 ++ 3 files changed, 388 insertions(+), 4 deletions(-) diff --git a/ggml/src/ggml-cuda/mmvq.cu b/ggml/src/ggml-cuda/mmvq.cu index b62b6bce1768..2783ee6ff220 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 @@ -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 +struct mmvq_iq_pipe { + static constexpr bool eligible = false; +}; + +#if defined(GGML_USE_HIP) +template <> struct mmvq_iq_pipe { + 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 { + 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 { + 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 { + 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 +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; + 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 +static bool mmvq_iq_pipe_enabled() { + if constexpr (!mmvq_iq_pipe::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; @@ -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_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, @@ -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::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(vx, vgate, y, kbx_offset, kbx_first, blocks_per_row_x, kqs, tmp[0][0], tmp_gate[0][0]); + } else { + mmvq_iq_pipe_loop(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 @@ -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::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; @@ -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, 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, 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, @@ -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, 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, 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, @@ -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, 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, 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, @@ -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(); + // 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; @@ -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)) { diff --git a/ggml/src/ggml-cuda/vecdotq.cuh b/ggml/src/ggml-cuda/vecdotq.cuh index 9151eb2edf49..738fd07e3e55 100644 --- a/ggml/src/ggml-cuda/vecdotq.cuh +++ b/ggml/src/ggml-cuda/vecdotq.cuh @@ -1549,3 +1549,222 @@ static __device__ __forceinline__ float vec_dot_iq4_xs_q8_1( const float d = __half2float(bq4->d) * __low2float(bq8_1[iqs/4].ds); return d * sumi; } + +#if defined(GGML_USE_HIP) +// Split dot products for the software-pipelined 1-column mat-vec on RDNA3 (see mmvq.cu, mmvq_iq_pipe). +// The vec_dot_iq*_q8_1 functions above load a few ints of the weight block, gather from the grid table with them +// and only then touch the q8_1 block, so each loop trip of the kernel waits on DRAM, then on the gather, with +// nothing else in flight. Here the loads (mmvq_iq_load_*) and the arithmetic (mmvq_iq_dot_*) are separate, so the +// kernel can issue the loads of the next trip before it computes the current one. The arithmetic is the same +// operation by operation as in the vec_dot functions above, the results are bit-identical. + +struct mmvq_iq_ld_iq2_s { + int qs_packed; + int qh; + int signs_packed_32; + int sc; + int u[8]; + ggml_half d; + ggml_half2 ds; +}; + +static __device__ __forceinline__ mmvq_iq_ld_iq2_s mmvq_iq_load_iq2_s( + const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) { + + const block_iq2_s * bq2 = (const block_iq2_s *) vbq + kbx; + + mmvq_iq_ld_iq2_s r; + r.qs_packed = get_int_b2(bq2->qs, iqs/2); + r.qh = bq2->qh[iqs/2]; + r.signs_packed_32 = get_int_b2(bq2->qs, QK_K/32 + iqs/2); + r.sc = bq2->scales[iqs/2]; +#pragma unroll + for (int l = 0; l < 8; ++l) { + r.u[l] = get_int_b4(bq8_1[iqs/2].qs, l); + } + r.d = bq2->d; + r.ds = bq8_1[iqs/2].ds; + return r; +} + +static __device__ __forceinline__ float mmvq_iq_dot_iq2_s(const mmvq_iq_ld_iq2_s & r) { + const uint8_t * qs = (const uint8_t *) &r.qs_packed; + const uint8_t * signs_packed_8 = (const uint8_t *) &r.signs_packed_32; + const int qh = r.qh; + const int ls0 = r.sc & 0x0F; + const int ls1 = r.sc >> 4; + + int sumi0 = 0; + int sumi1 = 0; +#pragma unroll + for (int l0 = 0; l0 < 8; l0 += 2) { + const int * grid_pos = (const int *)(iq2s_grid + (qs[l0/2] | ((qh << (8-l0)) & 0x300))); + + const int grid_l = apply_signs_lo(grid_pos[0], signs_packed_8[l0/2]); + const int grid_h = apply_signs_hi(grid_pos[1], signs_packed_8[l0/2]); + + const int u0 = r.u[l0 + 0]; + const int u1 = r.u[l0 + 1]; + + if (l0 < 4) { + sumi0 = ggml_cuda_dp4a(grid_l, u0, sumi0); + sumi0 = ggml_cuda_dp4a(grid_h, u1, sumi0); + } else { + sumi1 = ggml_cuda_dp4a(grid_l, u0, sumi1); + sumi1 = ggml_cuda_dp4a(grid_h, u1, sumi1); + } + } + const int sumi = (sumi0*ls0 + sumi1*ls1 + (sumi0 + sumi1)/2)/4; + + const float d = __half2float(r.d) * __low2float(r.ds); + return d * sumi; +} + +struct mmvq_iq_ld_iq3_xxs { + int2 q3_packed; + int aux32; + int u[8]; + ggml_half d; + ggml_half2 ds; +}; + +static __device__ __forceinline__ mmvq_iq_ld_iq3_xxs mmvq_iq_load_iq3_xxs( + const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) { + + const block_iq3_xxs * bq3 = (const block_iq3_xxs *) vbq + kbx; + + mmvq_iq_ld_iq3_xxs r; + r.q3_packed = make_int2(get_int_b2(bq3->qs, iqs), get_int_b2(bq3->qs, iqs+1)); + r.aux32 = get_int_b2(bq3->qs, QK_K/16 + iqs/2); +#pragma unroll + for (int l = 0; l < 8; ++l) { + r.u[l] = get_int_b4(bq8_1[iqs/2].qs, l); + } + r.d = bq3->d; + r.ds = bq8_1[iqs/2].ds; + return r; +} + +static __device__ __forceinline__ float mmvq_iq_dot_iq3_xxs(const mmvq_iq_ld_iq3_xxs & r) { + const uint8_t * q3 = (const uint8_t *) &r.q3_packed; + const uint32_t aux32 = r.aux32; + + int sumi = 0; +#pragma unroll + for (int l0 = 0; l0 < 8; l0 += 2) { + const int2 grid_pos = make_int2(iq3xxs_grid[q3[l0 + 0]], iq3xxs_grid[q3[l0 + 1]]); + const uint32_t signs = unpack_ksigns(aux32 >> (7*l0/2)); + + const int grid_l = apply_ksigns_lo(grid_pos.x, signs); + const int grid_h = apply_ksigns_hi(grid_pos.y, signs); + + sumi = ggml_cuda_dp4a(grid_l, r.u[l0 + 0], sumi); + sumi = ggml_cuda_dp4a(grid_h, r.u[l0 + 1], sumi); + } + + const int ls = aux32 >> 28; + sumi = (ls*sumi + sumi/2)/2; + const float d = __half2float(r.d) * __low2float(r.ds); + return d * sumi; +} + +struct mmvq_iq_ld_iq3_s { + int2 qs_packed; + int qh; + int signs_packed_32; + int sc; + int u[8]; + ggml_half d; + ggml_half2 ds; +}; + +static __device__ __forceinline__ mmvq_iq_ld_iq3_s mmvq_iq_load_iq3_s( + const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) { + + const block_iq3_s * bq3 = (const block_iq3_s *) vbq + kbx; + + mmvq_iq_ld_iq3_s r; + r.qs_packed = make_int2(get_int_b2(bq3->qs, iqs + 0), get_int_b2(bq3->qs, iqs + 1)); + r.qh = bq3->qh[iqs/2]; + r.signs_packed_32 = get_int_b2(bq3->signs, iqs/2); + r.sc = bq3->scales[iqs/4]; +#pragma unroll + for (int l = 0; l < 8; ++l) { + r.u[l] = get_int_b4(bq8_1[iqs/2].qs, l); + } + r.d = bq3->d; + r.ds = bq8_1[iqs/2].ds; + return r; +} + +static __device__ __forceinline__ float mmvq_iq_dot_iq3_s(const mmvq_iq_ld_iq3_s & r, const int & iqs) { + const uint8_t * qs = (const uint8_t *) &r.qs_packed; + const uint8_t * signs_packed_8 = (const uint8_t *) &r.signs_packed_32; + const int qh = r.qh; + + int sumi = 0; +#pragma unroll + for (int l0 = 0; l0 < 8; l0 += 2) { + const int2 grid_pos = make_int2( + iq3s_grid[qs[l0 + 0] | ((qh << (8 - l0)) & 0x100)], + iq3s_grid[qs[l0 + 1] | ((qh << (7 - l0)) & 0x100)]); + + const int grid_l = apply_signs_lo(grid_pos.x, signs_packed_8[l0/2]); + const int grid_h = apply_signs_hi(grid_pos.y, signs_packed_8[l0/2]); + + sumi = ggml_cuda_dp4a(grid_l, r.u[l0 + 0], sumi); + sumi = ggml_cuda_dp4a(grid_h, r.u[l0 + 1], sumi); + } + + sumi *= 1 + 2*((r.sc >> ((iqs << 1) & 0x04)) & 0x0F); + + const float d = __half2float(r.d) * __low2float(r.ds); + return d * sumi; +} + +struct mmvq_iq_ld_iq4_xs { + int aux_q4[4]; + int u0[4]; + int u1[4]; + int sl; + int sh; + ggml_half d; + ggml_half2 ds; +}; + +static __device__ __forceinline__ mmvq_iq_ld_iq4_xs mmvq_iq_load_iq4_xs( + const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) { + + const block_iq4_xs * bq4 = (const block_iq4_xs *) vbq + kbx; + + mmvq_iq_ld_iq4_xs r; +#pragma unroll + for (int j = 0; j < 4; ++j) { + r.aux_q4[j] = get_int_b4(bq4->qs, iqs + j); + r.u0[j] = get_int_b4(bq8_1[iqs/4].qs, j + 0); + r.u1[j] = get_int_b4(bq8_1[iqs/4].qs, j + 4); + } + r.sl = bq4->scales_l[iqs/8]; + r.sh = bq4->scales_h; + r.d = bq4->d; + r.ds = bq8_1[iqs/4].ds; + return r; +} + +static __device__ __forceinline__ float mmvq_iq_dot_iq4_xs(const mmvq_iq_ld_iq4_xs & r, const int & iqs) { + int sumi = 0; +#pragma unroll + for (int j = 0; j < 4; ++j) { + const int2 v = get_int_from_table_16(r.aux_q4[j], kvalues_iq4nl); + + sumi = ggml_cuda_dp4a(v.x, r.u0[j], sumi); + sumi = ggml_cuda_dp4a(v.y, r.u1[j], sumi); + } + + const int ls = ((r.sl >> (iqs & 0x04)) & 0x0F) | (((r.sh >> (iqs/2)) & 0x03) << 4); + sumi *= ls - 32; + + const float d = __half2float(r.d) * __low2float(r.ds); + return d * sumi; +} +#endif // defined(GGML_USE_HIP) diff --git a/tests/test-backend-ops.cpp b/tests/test-backend-ops.cpp index c3aa490e347b..3035001020bd 100644 --- a/tests/test-backend-ops.cpp +++ b/tests/test-backend-ops.cpp @@ -17147,6 +17147,18 @@ static std::vector> make_test_cases_eval() { } } + // iq-mmvq-latency: software-pipelined 1-column mat-vec of the IQ types on RDNA3. K of 1..21 blocks gives every trip + // count and tail of the double-buffered kbx loop (a single trip, a tail trip with idle threads, an exact multiple), + // plain, with expert ids and with the fused gate (up/gate GLU). + for (ggml_type type_a : {GGML_TYPE_IQ2_S, GGML_TYPE_IQ3_S, GGML_TYPE_IQ3_XXS, GGML_TYPE_IQ4_XS}) { + for (int nb : {1, 2, 3, 4, 5, 7, 8, 13, 21}) { + test_cases.emplace_back(new test_mul_mat(type_a, GGML_TYPE_F32, 192, 1, 256*nb, {1, 1}, {1, 1})); + test_cases.emplace_back(new test_mul_mat_id(type_a, GGML_TYPE_F32, 8, 4, false, 192, 1, 256*nb)); + } + test_cases.emplace_back(new test_mul_mat_id_fusion(type_a, GGML_TYPE_F32, 16, 4, false, 192, 1, 256*5, 3)); + test_cases.emplace_back(new test_mul_mat_id_fusion(type_a, GGML_TYPE_F32, 16, 4, false, 192, 1, 256*21, 3)); + } + return test_cases; } #ifdef _MSC_VER