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
165 changes: 165 additions & 0 deletions ggml/src/ggml-cuda/mmq-load-tiles.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,171 @@ static __device__ __forceinline__ void ggml_cuda_mmq_store_tiles_q8_0(
x_df[i*sram_stride + kbxd] = d_cache[d_idx++];
}
}

// Prefetch/store split of the Q4_0 and IQ4_XS tile loaders (same math as ggml_cuda_mmq_load_tiles_q4_0 and _iq4_xs, only the
// schedule differs): the raw quants and scales of the next K tile are read into registers before the WMMA of the current tile
// and only converted and written to shared memory afterwards. qs_cache holds one int per row of the thread's row set,
// d_cache the (already combined) scale of the 4 scale rows of the thread.
template <ggml_type type, int J, bool fallback, int NQ>
static __device__ __forceinline__ void ggml_cuda_mmq_prefetch_tiles_q4_0(
const char * __restrict__ x, const int kbx0, const int i_max, const int stride,
int (&qs_cache)[NQ], float (&d_cache)[4]) {
constexpr int warp_size = ggml_cuda_get_physical_warp_size();
constexpr int nthreads = ggml_cuda_mmq_get_nthreads(type, J, fallback, GGML_PREC_Q8);
constexpr int nwarps = nthreads / warp_size;
constexpr int I = ggml_cuda_mmq_get_I(type, J, fallback, GGML_PREC_Q8);
static_assert(warp_size == 32 && nthreads == 128 && I == 64, "unexpected Q4_0 MMQ configuration");
static_assert(NQ == I/nwarps, "qs_cache has the wrong size");

constexpr int threads_per_row = MMQ_ITER_K / (4 * QR4_0);
static_assert(threads_per_row == warp_size, "one warp must cover one row");
const int kbx = threadIdx.x / QI4_0;
const int kqsx = threadIdx.x % QI4_0;

#pragma unroll
for (int i0 = 0; i0 < I; i0 += nwarps) {
int i = i0 + threadIdx.y;
if constexpr (fallback) {
i = min(i, i_max);
}

const block_q4_0 * bxi = (const block_q4_0 *) x + kbx0 + i*stride + kbx;
qs_cache[i0/nwarps] = get_int_b2(bxi->qs, kqsx);
}

constexpr int blocks_per_tile_x_row = MMQ_TILE_NE_K / QI4_0;
constexpr int rows_per_warp = warp_size / blocks_per_tile_x_row;
static_assert(I/(nwarps*rows_per_warp) <= 4, "d_cache too small");
const int kbxd = threadIdx.x % blocks_per_tile_x_row;
int d_idx = 0;
#pragma unroll
for (int i0 = 0; i0 < I; i0 += nwarps * rows_per_warp) {
int i = i0 + threadIdx.y * rows_per_warp + threadIdx.x / blocks_per_tile_x_row;
if constexpr (fallback) {
i = min(i, i_max);
}

const block_q4_0 * bxi = (const block_q4_0 *) x + kbx0 + i*stride + kbxd;
d_cache[d_idx++] = bxi->d;
}
}

template <ggml_type type, int J, bool fallback, int NQ>
static __device__ __forceinline__ void ggml_cuda_mmq_store_tiles_q4_0(
int * __restrict__ x_tile, const int (&qs_cache)[NQ], const float (&d_cache)[4]) {
constexpr int warp_size = ggml_cuda_get_physical_warp_size();
constexpr int nthreads = ggml_cuda_mmq_get_nthreads(type, J, fallback, GGML_PREC_Q8);
constexpr int nwarps = nthreads / warp_size;
constexpr int I = ggml_cuda_mmq_get_I(type, J, fallback, GGML_PREC_Q8);
constexpr int sram_stride = ggml_cuda_mmq_get_sram_stride(type, J, fallback, GGML_PREC_Q8);
static_assert(warp_size == 32 && nthreads == 128 && I == 64, "unexpected Q4_0 MMQ configuration");
static_assert(NQ == I/nwarps, "qs_cache has the wrong size");

int * x_qs = x_tile;
float * x_df = (float *) (x_tile + 2*MMQ_TILE_NE_K);

const int kbx = threadIdx.x / QI4_0;
const int kqsx = threadIdx.x % QI4_0;

#pragma unroll
for (int i0 = 0; i0 < I; i0 += nwarps) {
const int i = i0 + threadIdx.y;
const int qs0 = qs_cache[i0/nwarps];
x_qs[i*sram_stride + kbx*(2*QI4_0) + kqsx + 0] = __vsub4((qs0 >> 0) & 0x0F0F0F0F, 0x08080808);
x_qs[i*sram_stride + kbx*(2*QI4_0) + kqsx + QI4_0] = __vsub4((qs0 >> 4) & 0x0F0F0F0F, 0x08080808);
}

constexpr int blocks_per_tile_x_row = MMQ_TILE_NE_K / QI4_0;
constexpr int rows_per_warp = warp_size / blocks_per_tile_x_row;
const int kbxd = threadIdx.x % blocks_per_tile_x_row;
int d_idx = 0;
#pragma unroll
for (int i0 = 0; i0 < I; i0 += nwarps * rows_per_warp) {
const int i = i0 + threadIdx.y * rows_per_warp + threadIdx.x / blocks_per_tile_x_row;
x_df[i*sram_stride + kbxd] = d_cache[d_idx++];
}
}

template <ggml_type type, int J, bool fallback, bool has_fusion, int NQ>
static __device__ __forceinline__ void ggml_cuda_mmq_prefetch_tiles_iq4_xs(
const char * __restrict__ x, const char * __restrict__ x_gate, const int kbx0, const int i_max, const int stride,
int (&qs_cache)[NQ], float (&d_cache)[4]) {
constexpr int warp_size = ggml_cuda_get_physical_warp_size();
constexpr int nthreads = ggml_cuda_mmq_get_nthreads(type, J, fallback, GGML_PREC_Q8);
constexpr int nwarps = nthreads / warp_size;
constexpr int I = ggml_cuda_mmq_get_I(type, J, fallback, GGML_PREC_Q8);
static_assert(warp_size == 32 && nthreads == 128 && I == 64, "unexpected IQ4_XS MMQ configuration");
static_assert(NQ == I/nwarps, "qs_cache has the wrong size");

constexpr int threads_per_row = MMQ_ITER_K / (4 * QR4_XS);
static_assert(threads_per_row == warp_size, "one warp must cover one row");
const int kqsx = threadIdx.x;

#pragma unroll
for (int i0 = 0; i0 < I; i0 += nwarps) {
int i = i0 + threadIdx.y;
if constexpr (fallback) {
i = min(i, i_max);
}

const block_iq4_xs * bxi = ggml_cuda_mmq_x_row<block_iq4_xs, has_fusion>(x, x_gate, kbx0, i, stride);
qs_cache[i0/nwarps] = get_int_b4(bxi->qs, kqsx);
}

constexpr int rows_per_warp = warp_size / 8;
static_assert(I/(nwarps*rows_per_warp) <= 4, "d_cache too small");
int d_idx = 0;
#pragma unroll
for (int i0 = 0; i0 < I; i0 += nwarps * rows_per_warp) {
int i = i0 + threadIdx.y * rows_per_warp + threadIdx.x / (MMQ_TILE_NE_K/4);
if constexpr (fallback) {
i = min(i, i_max);
}

const block_iq4_xs * bxi = ggml_cuda_mmq_x_row<block_iq4_xs, has_fusion>(x, x_gate, kbx0, i, stride);

const float d = __half2float(bxi->d);

const int ls = ((bxi->scales_l[(threadIdx.x % 8)/2] >> (4*(threadIdx.x % 2))) & 0x0F)
| (((bxi->scales_h >> (2*(threadIdx.x % 8))) & 0x03) << 4);

d_cache[d_idx++] = d * (ls - 32);
}
}

template <ggml_type type, int J, bool fallback, int NQ>
static __device__ __forceinline__ void ggml_cuda_mmq_store_tiles_iq4_xs(
int * __restrict__ x_tile, const int (&qs_cache)[NQ], const float (&d_cache)[4]) {
constexpr int warp_size = ggml_cuda_get_physical_warp_size();
constexpr int nthreads = ggml_cuda_mmq_get_nthreads(type, J, fallback, GGML_PREC_Q8);
constexpr int nwarps = nthreads / warp_size;
constexpr int I = ggml_cuda_mmq_get_I(type, J, fallback, GGML_PREC_Q8);
constexpr int sram_stride = ggml_cuda_mmq_get_sram_stride(type, J, fallback, GGML_PREC_Q8);
static_assert(warp_size == 32 && nthreads == 128 && I == 64, "unexpected IQ4_XS MMQ configuration");
static_assert(NQ == I/nwarps, "qs_cache has the wrong size");

int * x_qs = x_tile;
float * x_df = (float *) (x_qs + MMQ_TILE_NE_K*2);

const int kqsx = threadIdx.x;
const int k0 = 8 * (kqsx / 4) + kqsx % 4;

#pragma unroll
for (int i0 = 0; i0 < I; i0 += nwarps) {
const int i = i0 + threadIdx.y;
const int2 v = get_int_from_table_16(qs_cache[i0/nwarps], kvalues_iq4nl);
x_qs[i*sram_stride + k0 + 0] = v.x;
x_qs[i*sram_stride + k0 + 4] = v.y;
}

constexpr int rows_per_warp = warp_size / 8;
int d_idx = 0;
#pragma unroll
for (int i0 = 0; i0 < I; i0 += nwarps * rows_per_warp) {
const int i = i0 + threadIdx.y * rows_per_warp + threadIdx.x / (MMQ_TILE_NE_K/4);
x_df[i*sram_stride + threadIdx.x % 8] = d_cache[d_idx++];
}
}
#endif // defined(RDNA3)

template <ggml_type type, int J, bool fallback, bool has_fusion> static __device__ __forceinline__ void ggml_cuda_mmq_load_tiles_q5_K(
Expand Down
74 changes: 74 additions & 0 deletions ggml/src/ggml-cuda/mmq.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,14 @@ static __device__ __forceinline__ const block_t * ggml_cuda_mmq_x_row(
}
}

// Register prefetch of the next weight tile for Q4_0 and IQ4_XS on RDNA3 (bit-identical, only the load schedule differs).
// Compile with -DGGML_CUDA_MMQ_NO_EXTRA_PREFETCH to switch it off for an A/B build.
#if defined(GGML_CUDA_MMQ_NO_EXTRA_PREFETCH)
static constexpr bool ggml_cuda_mmq_extra_prefetch = false;
#else
static constexpr bool ggml_cuda_mmq_extra_prefetch = true;
#endif // defined(GGML_CUDA_MMQ_NO_EXTRA_PREFETCH)

#include "mmq-load-tiles.cuh"
#include "mmq-vec-dot.cuh"

Expand Down Expand Up @@ -1084,6 +1092,72 @@ static __device__ __forceinline__ void mul_mat_q_process_tile(
ggml_cuda_mmq_store_tiles_q8_0<type, J, fallback>(tile_x, qs_cache, d_cache);
}

__syncthreads();
}
} else if constexpr (ggml_cuda_mmq_extra_prefetch && prec_src1 == GGML_PREC_Q8 && I == 64 && J >= 32 &&
(type == GGML_TYPE_Q4_0 || type == GGML_TYPE_IQ4_XS) &&
ggml_cuda_mmq_get_nthreads(type, J, fallback, prec_src1) == 128) {
// Same schedule as above for Q4_0 and IQ4_XS (also the fused gate/up IQ4_XS tile of the expert matmuls).
// Not for J = 16: the tile is so small that the prefetch only inflates the VGPR count there (Q4_0 182 -> 253, IQ4_XS spills).
constexpr int qs_cache_size = I/nwarps;

load_tiles(x, x_gate, tile_x, offset_x + kb0_start, tile_x_i_max, stride_row_x);

for (int kb0 = kb0_start; kb0 < kb0_stop; kb0 += blocks_per_iter) {
{
const int * by0 = y + ncols_y * (kb0 * qk / ne_block) * sz;
#pragma unroll
for (int l0 = 0; l0 < J * MMQ_TILE_Y_K; l0 += nwarps * warp_size) {
int l = l0 + threadIdx.y*warp_size + threadIdx.x;

tile_y[l] = by0[l];
}
}

__syncthreads();

vec_dot(tile_x, tile_y, sum, 0);

__syncthreads();

{
const int * by0 = y + ncols_y * ((kb0 * qk / ne_block) * sz + sz);
#pragma unroll
for (int l0 = 0; l0 < J * MMQ_TILE_Y_K; l0 += nwarps * warp_size) {
int l = l0 + threadIdx.y*warp_size + threadIdx.x;

tile_y[l] = by0[l];
}
}

__syncthreads();

int qs_cache[qs_cache_size];
float d_cache[4];
const int kb0_next = kb0 + blocks_per_iter;
const bool have_next = kb0_next < kb0_stop;
if (have_next) {
if constexpr (type == GGML_TYPE_Q4_0) {
ggml_cuda_mmq_prefetch_tiles_q4_0<type, J, fallback>(
x, offset_x + kb0_next, tile_x_i_max, stride_row_x, qs_cache, d_cache);
} else {
ggml_cuda_mmq_prefetch_tiles_iq4_xs<type, J, fallback, has_fusion>(
x, x_gate, offset_x + kb0_next, tile_x_i_max, stride_row_x, qs_cache, d_cache);
}
}

vec_dot(tile_x, tile_y, sum, MMQ_TILE_NE_K);

__syncthreads();

if (have_next) {
if constexpr (type == GGML_TYPE_Q4_0) {
ggml_cuda_mmq_store_tiles_q4_0<type, J, fallback>(tile_x, qs_cache, d_cache);
} else {
ggml_cuda_mmq_store_tiles_iq4_xs<type, J, fallback>(tile_x, qs_cache, d_cache);
}
}

__syncthreads();
}
} else
Expand Down
12 changes: 12 additions & 0 deletions tests/test-backend-ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17147,6 +17147,18 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
}
}

// [w2 mmq-int8-efficiency] register prefetch of the next weight tile in the RDNA3 MMQ for Q4_0 and IQ4_XS: several K iterations
// (K = 1024..4096), the 64/96/128 wide tiles and unaligned row counts (fallback tile), plus the expert matmuls.
for (ggml_type type_a : {GGML_TYPE_Q4_0, GGML_TYPE_IQ4_XS}) {
for (int n : {16, 33, 64, 96, 128, 512}) {
test_cases.emplace_back(new test_mul_mat(type_a, GGML_TYPE_F32, 4096, n, 4096, {1, 1}, {1, 1}));
test_cases.emplace_back(new test_mul_mat(type_a, GGML_TYPE_F32, 1000, n, 2048, {1, 1}, {1, 1}));
test_cases.emplace_back(new test_mul_mat(type_a, GGML_TYPE_F32, 128, n, 1024, {1, 1}, {1, 1}));
}
test_cases.emplace_back(new test_mul_mat_id(type_a, GGML_TYPE_F32, 32, 8, false, 512, 256, 2048));
test_cases.emplace_back(new test_mul_mat_id(type_a, GGML_TYPE_F32, 16, 4, true, 1000, 129, 1024));
}

return test_cases;
}
#ifdef _MSC_VER
Expand Down
Loading