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
12 changes: 11 additions & 1 deletion ggml/src/ggml-cuda/fusion.cu
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,15 @@ bool ggml_cuda_should_fuse_mul_mat_vec_q(
return false;
}

// The opt-in few-row WMMA MMQ for the I-quants (GGML_CUDA_MMQ_FEW_IQ) has no fused gate/GLU: it takes these matmuls, which then run
// unfused, unless the opt-in multi-row mat-vec (GGML_CUDA_MMVQ_IQ_MULTIROW) covers them: with both on the fused mat-vec wins.
// Without GGML_CUDA_MMQ_FEW_IQ few-row MMQ does not take I-quants on RDNA3, so this does not change anything there.
if (tensor->op == GGML_OP_MUL_MAT && GGML_CUDA_CC_IS_RDNA3_0(cc) &&
(src0->type == GGML_TYPE_IQ2_S || src0->type == GGML_TYPE_IQ3_XXS || src0->type == GGML_TYPE_IQ3_S) &&
ggml_cuda_should_use_mmq_few(src0, src1, nullptr, tensor, cc) && !ggml_cuda_mmvq_iq_multirow(src0->type, cc, src1->ne[1])) {
return false;
}

return use_mul_mat_vec_q;
}

Expand All @@ -218,9 +227,10 @@ static const ggml_tensor * ggml_cuda_q8_1_consumer(const ggml_backend_cuda_conte
const ggml_tensor * src0 = n->src[0];
const ggml_tensor * src1 = n->src[1];
// MUL_MAT_ID shares the copy only with one src1 row per token, see ggml_cuda_mul_mat_vec_q
// few-row MMQ quantizes src1 itself and goes before MMVQ in ggml_cuda_mul_mat
const bool mmvq = n->op == GGML_OP_MUL_MAT_ID
? n->ne[2] <= get_mmvq_mmid_max_batch(src0->type, cc) && src1->ne[1] == 1
: ggml_cuda_should_use_mmvq(src0->type, cc, src1->ne[1]);
: ggml_cuda_should_use_mmvq(src0->type, cc, src1->ne[1]) && !ggml_cuda_should_use_mmq_few(src0, src1, nullptr, n, cc);
const bool bad_padding_clear = ggml_backend_buffer_get_usage(src0->buffer) == GGML_BACKEND_BUFFER_USAGE_COMPUTE &&
ggml_nbytes(src0) != ggml_backend_buffer_get_alloc_size(src0->buffer, src0) &&
src0->view_src;
Expand Down
275 changes: 271 additions & 4 deletions ggml/src/ggml-cuda/mmq-few.cu
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "quantize.cuh"
#include "vecdotq.cuh"

#include <cstdlib>
#include <type_traits>

// mul_mat_q_few for NVIDIA uses the int8 MMA fragment layout and cp.async, mul_mat_q_few_wmma for RDNA the WMMA layout. Not on MUSA.
Expand Down Expand Up @@ -33,6 +34,8 @@ static constexpr __host__ __device__ int mmq_few_unit_size(const ggml_type type)
case GGML_TYPE_Q4_K: return mmq_few_unit_size_of<GGML_TYPE_Q4_K>();
case GGML_TYPE_Q5_K: return mmq_few_unit_size_of<GGML_TYPE_Q5_K>();
case GGML_TYPE_Q6_K: return mmq_few_unit_size_of<GGML_TYPE_Q6_K>();
case GGML_TYPE_IQ2_S: return mmq_few_unit_size_of<GGML_TYPE_IQ2_S>();
case GGML_TYPE_IQ3_XXS: return mmq_few_unit_size_of<GGML_TYPE_IQ3_XXS>();
case GGML_TYPE_IQ3_S: return mmq_few_unit_size_of<GGML_TYPE_IQ3_S>();
case GGML_TYPE_IQ4_NL: return mmq_few_unit_size_of<GGML_TYPE_IQ4_NL>();
case GGML_TYPE_IQ4_XS: return mmq_few_unit_size_of<GGML_TYPE_IQ4_XS>();
Expand Down Expand Up @@ -860,7 +863,8 @@ static __global__ void mul_mat_q_few(
// A second group of 16 columns spills registers with ROCm 7.2.

static constexpr __host__ __device__ bool mmq_few_wmma_supported(const ggml_type type) {
return type == GGML_TYPE_Q8_0 || type == GGML_TYPE_IQ4_NL || type == GGML_TYPE_Q4_K || type == GGML_TYPE_Q5_K || type == GGML_TYPE_Q6_K;
return type == GGML_TYPE_Q8_0 || type == GGML_TYPE_IQ4_NL || type == GGML_TYPE_Q4_K || type == GGML_TYPE_Q5_K || type == GGML_TYPE_Q6_K ||
type == GGML_TYPE_IQ2_S || type == GGML_TYPE_IQ3_XXS || type == GGML_TYPE_IQ3_S;
}

// the reduction over the warps
Expand Down Expand Up @@ -1121,9 +1125,246 @@ static __device__ __forceinline__ void mmq_few_wmma_vec_dot_q8_0_iq4_nl(const ch
}
}

#if defined(AMD_WMMA_256B_AVAILABLE)
// RDNA3: half-warp h decoded the values [16h, 16h + 16) of a sub-block, the other 16 values come from the other half-warp.
static __device__ __forceinline__ void mmq_few_wmma_exchange_halves(const int (&own)[4], int (&b_lo)[4], int (&b_hi)[4]) {
const bool h1 = threadIdx.x >= 16;
#pragma unroll
for (int i = 0; i < 4; ++i) {
const int other = __builtin_amdgcn_permlanex16(own[i], own[i], 0x76543210, 0xfedcba98, false, false);
b_lo[i] = h1 ? other : own[i];
b_hi[i] = h1 ? own[i] : other;
}
}
#endif // defined(AMD_WMMA_256B_AVAILABLE)

// IQ3_XXS: d, then 8 grid indices (4 values each) per sub-block of 32 values, then per sub-block a uint32 with 4 x 7 sign bits
// (8 values each, the 8th sign is the parity) and a 4 bit scale. The units are 98 bytes, only 2 byte aligned.
// The gathers from the grid table make the decoding the expensive part: on RDNA3, where both half-warps need the same 32 values of a
// sub-block, each half-warp decodes 16 of them and takes the other 16 from the other half-warp (v_permlanex16).
template <bool half>
static __device__ __forceinline__ void mmq_few_wmma_vec_dot_iq3_xxs(const char * x, const int4 * y, float (&sum)[8]) {
const block_iq3_xxs * bx = (const block_iq3_xxs *) x;
const float d = __half2float(bx->d);

float sum_d[8] = {0.0f};
#if defined(AMD_WMMA_256B_AVAILABLE) // RDNA3, MMQ_FEW_WMMA_NE == 4
const int h = threadIdx.x / 16;
#pragma unroll
for (int s = 0; s < 8; ++s) {
const int q3 = get_int_b2(bx->qs, 2*s + h); // grid indices of the values [16h, 16h + 16)
const uint32_t aux32 = get_int_b2(bx->qs, QK_K/16 + s); // signs and scale of the sub-block
const uint32_t sgn = aux32 >> (14*h); // sign groups 2h and 2h + 1

int own[4];
#pragma unroll
for (int l = 0; l < 2; ++l) {
const uint32_t signs = unpack_ksigns(sgn >> (7*l));
own[2*l + 0] = apply_ksigns_lo(iq3xxs_grid[(q3 >> (16*l + 0)) & 0xFF], signs);
own[2*l + 1] = apply_ksigns_hi(iq3xxs_grid[(q3 >> (16*l + 8)) & 0xFF], signs);
}
int b_lo[4]; // values [0, 16)
int b_hi[4]; // values [16, 32)
mmq_few_wmma_exchange_halves(own, b_lo, b_hi);
const float ls = float(aux32 >> 28) + 0.5f;

const mmq_few_wmma_y yf = mmq_few_wmma_load_y<half>(y, s);
mmq_few_tile_c C;
mmq_few_wmma(C, yf.qs[0], b_lo);
mmq_few_wmma(C, yf.qs[1], b_hi);
#pragma unroll
for (int v = 0; v < mmq_few_wmma_nv<half>(); ++v) {
sum_d[v] += mmq_few_wmma_get_d<MMQ_Q8_1_DS_LAYOUT_D4>(yf, v) * (ls*float(C.x[v]));
}
}
#else // RDNA4: the lane needs the values [8*(l/16), 8*(l/16) + 8) of each half of the sub-block, i.e. the sign groups l/16 and 2 + l/16
const int i0 = (4 - MMQ_FEW_WMMA_NE)*(threadIdx.x / 16);
#pragma unroll
for (int s = 0; s < 8; ++s) {
const int2 q3 = make_int2(get_int_b2(bx->qs, 2*s), get_int_b2(bx->qs, 2*s + 1));
const uint32_t aux32 = get_int_b2(bx->qs, QK_K/16 + s);
int b[8];
#pragma unroll
for (int l = 0; l < 4; ++l) {
const int q = l < 2 ? q3.x : q3.y;
const uint32_t signs = unpack_ksigns(aux32 >> (7*l));
b[2*l + 0] = apply_ksigns_lo(iq3xxs_grid[(q >> (16*(l % 2) + 0)) & 0xFF], signs);
b[2*l + 1] = apply_ksigns_hi(iq3xxs_grid[(q >> (16*(l % 2) + 8)) & 0xFF], signs);
}
const float ls = float(aux32 >> 28) + 0.5f;

const mmq_few_wmma_y yf = mmq_few_wmma_load_y<half>(y, s);
mmq_few_tile_c C;
mmq_few_wmma(C, yf.qs[0], b + i0 + 0);
mmq_few_wmma(C, yf.qs[1], b + i0 + 4);
#pragma unroll
for (int v = 0; v < mmq_few_wmma_nv<half>(); ++v) {
sum_d[v] += mmq_few_wmma_get_d<MMQ_Q8_1_DS_LAYOUT_D4>(yf, v) * (ls*float(C.x[v]));
}
}
#endif // defined(AMD_WMMA_256B_AVAILABLE)
#pragma unroll
for (int v = 0; v < 8; ++v) {
sum[v] += 0.5f*d*sum_d[v];
}
}

// IQ2_S: d, 32 bytes with the low 8 bits of the grid indices (8 values each), 32 sign bytes (8 values each), the high 2 bits of
// the indices (a byte per sub-block) and two 4 bit scales per sub-block (values [0, 16) and [16, 32)). 82 byte units.
template <bool half>
static __device__ __forceinline__ void mmq_few_wmma_vec_dot_iq2_s(const char * x, const int4 * y, float (&sum)[8]) {
const block_iq2_s * bx = (const block_iq2_s *) x;
const float d = __half2float(bx->d);

float sum_d[8] = {0.0f};
#if defined(AMD_WMMA_256B_AVAILABLE)
const int h = threadIdx.x / 16;
#pragma unroll
for (int s = 0; s < 8; ++s) {
const int qs = get_int_b2(bx->qs, s); // low bits of the 4 grid indices of the sub-block
const int signs = get_int_b2(bx->qs, QK_K/32 + s); // their 4 sign bytes
const int qh = bx->qh[s];
const int sc = bx->scales[s];

int own[4];
#pragma unroll
for (int l = 0; l < 2; ++l) {
const int g = 2*h + l; // the values [8g, 8g + 8)
const int2 grid = ((const int2 *) iq2s_grid)[((qs >> (8*g)) & 0xFF) | ((qh << (8 - 2*g)) & 0x300)];
const uint8_t sg = signs >> (8*g);
own[2*l + 0] = apply_signs_lo(grid.x, sg);
own[2*l + 1] = apply_signs_hi(grid.y, sg);
}
int b_lo[4];
int b_hi[4];
mmq_few_wmma_exchange_halves(own, b_lo, b_hi);
const float ls_lo = float(sc & 0x0F) + 0.5f;
const float ls_hi = float(sc >> 4) + 0.5f;

const mmq_few_wmma_y yf = mmq_few_wmma_load_y<half>(y, s);
mmq_few_tile_c C_lo;
mmq_few_tile_c C_hi;
mmq_few_wmma(C_lo, yf.qs[0], b_lo);
mmq_few_wmma(C_hi, yf.qs[1], b_hi);
#pragma unroll
for (int v = 0; v < mmq_few_wmma_nv<half>(); ++v) {
sum_d[v] += mmq_few_wmma_get_d<MMQ_Q8_1_DS_LAYOUT_D4>(yf, v) * (ls_lo*float(C_lo.x[v]) + ls_hi*float(C_hi.x[v]));
}
}
#else
const int i0 = (4 - MMQ_FEW_WMMA_NE)*(threadIdx.x / 16);
#pragma unroll
for (int s = 0; s < 8; ++s) {
const int qs = get_int_b2(bx->qs, s);
const int signs = get_int_b2(bx->qs, QK_K/32 + s);
const int qh = bx->qh[s];
const int sc = bx->scales[s];

int b[8];
#pragma unroll
for (int g = 0; g < 4; ++g) {
const int2 grid = ((const int2 *) iq2s_grid)[((qs >> (8*g)) & 0xFF) | ((qh << (8 - 2*g)) & 0x300)];
const uint8_t sg = signs >> (8*g);
b[2*g + 0] = apply_signs_lo(grid.x, sg);
b[2*g + 1] = apply_signs_hi(grid.y, sg);
}
const float ls_lo = float(sc & 0x0F) + 0.5f;
const float ls_hi = float(sc >> 4) + 0.5f;

const mmq_few_wmma_y yf = mmq_few_wmma_load_y<half>(y, s);
mmq_few_tile_c C_lo;
mmq_few_tile_c C_hi;
mmq_few_wmma(C_lo, yf.qs[0], b + i0 + 0);
mmq_few_wmma(C_hi, yf.qs[1], b + i0 + 4);
#pragma unroll
for (int v = 0; v < mmq_few_wmma_nv<half>(); ++v) {
sum_d[v] += mmq_few_wmma_get_d<MMQ_Q8_1_DS_LAYOUT_D4>(yf, v) * (ls_lo*float(C_lo.x[v]) + ls_hi*float(C_hi.x[v]));
}
}
#endif // defined(AMD_WMMA_256B_AVAILABLE)
#pragma unroll
for (int v = 0; v < 8; ++v) {
sum[v] += 0.25f*d*sum_d[v];
}
}

// IQ3_S: d, 64 bytes with the low 8 bits of the grid indices (4 values each), a byte per sub-block with their high bits,
// 32 sign bytes (8 values each) and a 4 bit scale per sub-block. 110 byte units.
template <bool half>
static __device__ __forceinline__ void mmq_few_wmma_vec_dot_iq3_s(const char * x, const int4 * y, float (&sum)[8]) {
const block_iq3_s * bx = (const block_iq3_s *) x;
const float d = __half2float(bx->d);

float sum_d[8] = {0.0f};
#if defined(AMD_WMMA_256B_AVAILABLE)
const int h = threadIdx.x / 16;
#pragma unroll
for (int s = 0; s < 8; ++s) {
const int qs = get_int_b2(bx->qs, 2*s + h); // low bits of the grid indices 4h .. 4h + 3
const int qh = bx->qh[s] >> (4*h); // their high bits
const int signs = get_int_b2(bx->signs, s) >> (16*h); // their sign nibbles

int own[4];
#pragma unroll
for (int k = 0; k < 4; ++k) {
const int grid = iq3s_grid[((qs >> (8*k)) & 0xFF) | (((qh >> k) & 1) << 8)];
own[k] = apply_signs_lo(grid, (uint8_t) (signs >> (4*k)));
}
int b_lo[4];
int b_hi[4];
mmq_few_wmma_exchange_halves(own, b_lo, b_hi);
const int ls = 1 + 2*((bx->scales[s/2] >> (4*(s % 2))) & 0x0F);

const mmq_few_wmma_y yf = mmq_few_wmma_load_y<half>(y, s);
mmq_few_tile_c C;
mmq_few_wmma(C, yf.qs[0], b_lo);
mmq_few_wmma(C, yf.qs[1], b_hi);
#pragma unroll
for (int v = 0; v < mmq_few_wmma_nv<half>(); ++v) {
sum_d[v] += mmq_few_wmma_get_d<MMQ_Q8_1_DS_LAYOUT_D4>(yf, v) * float(C.x[v]*ls);
}
}
#else
const int i0 = (4 - MMQ_FEW_WMMA_NE)*(threadIdx.x / 16);
#pragma unroll
for (int s = 0; s < 8; ++s) {
const int2 qs = make_int2(get_int_b2(bx->qs, 2*s), get_int_b2(bx->qs, 2*s + 1));
const int qh = bx->qh[s];
const int signs = get_int_b2(bx->signs, s);

int b[8];
#pragma unroll
for (int k = 0; k < 8; ++k) {
const int grid = iq3s_grid[(((k < 4 ? qs.x : qs.y) >> (8*(k % 4))) & 0xFF) | (((qh >> k) & 1) << 8)];
b[k] = apply_signs_lo(grid, (uint8_t) (signs >> (4*k)));
}
const int ls = 1 + 2*((bx->scales[s/2] >> (4*(s % 2))) & 0x0F);

const mmq_few_wmma_y yf = mmq_few_wmma_load_y<half>(y, s);
mmq_few_tile_c C;
mmq_few_wmma(C, yf.qs[0], b + i0 + 0);
mmq_few_wmma(C, yf.qs[1], b + i0 + 4);
#pragma unroll
for (int v = 0; v < mmq_few_wmma_nv<half>(); ++v) {
sum_d[v] += mmq_few_wmma_get_d<MMQ_Q8_1_DS_LAYOUT_D4>(yf, v) * float(C.x[v]*ls);
}
}
#endif // defined(AMD_WMMA_256B_AVAILABLE)
#pragma unroll
for (int v = 0; v < 8; ++v) {
sum[v] += d*sum_d[v];
}
}

template <ggml_type type, bool half>
static __device__ __forceinline__ void mmq_few_wmma_vec_dot(const char * x, const int4 * y, float (&sum)[8]) {
if constexpr (type == GGML_TYPE_Q8_0 || type == GGML_TYPE_IQ4_NL) {
if constexpr (type == GGML_TYPE_IQ2_S) {
mmq_few_wmma_vec_dot_iq2_s<half>(x, y, sum);
} else if constexpr (type == GGML_TYPE_IQ3_S) {
mmq_few_wmma_vec_dot_iq3_s<half>(x, y, sum);
} else if constexpr (type == GGML_TYPE_IQ3_XXS) {
mmq_few_wmma_vec_dot_iq3_xxs<half>(x, y, sum);
} else if constexpr (type == GGML_TYPE_Q8_0 || type == GGML_TYPE_IQ4_NL) {
mmq_few_wmma_vec_dot_q8_0_iq4_nl<type, half>(x, y, sum);
} else if constexpr (type == GGML_TYPE_Q4_K || type == GGML_TYPE_Q5_K) {
mmq_few_wmma_vec_dot_q45_K<type, half>(x, y, sum);
Expand Down Expand Up @@ -1332,6 +1573,21 @@ static int2 mmq_few_wmma_ncols_range(const ggml_type type, const int cc) {
case GGML_TYPE_Q6_K: return make_int2(5, 16);
case GGML_TYPE_IQ4_NL: return make_int2(5, 16);
case GGML_TYPE_Q8_0: return make_int2(9, 16); // fork: tuned on the RX 7900 XT, MMQ is faster above 16 columns
case GGML_TYPE_IQ2_S:
case GGML_TYPE_IQ3_XXS:
case GGML_TYPE_IQ3_S: { // fork, opt-in: MMVQ decodes the grid once per row but its per-column work grows with the columns
// GGML_CUDA_MMQ_FEW_IQ=1 enables it from 7 columns (verify batches), GGML_CUDA_MMQ_FEW_IQ_MIN=<n> changes the minimum (2 to 5 was
// break-even or worse in the prototype). Not used for the fused gate/up mat-vec of MMVQ, see GGML_CUDA_MMVQ_IQ_MULTIROW.
static const int min_ncols = [] {
const char * e = getenv("GGML_CUDA_MMQ_FEW_IQ");
if (!e || atoi(e) == 0) {
return 0;
}
const char * m = getenv("GGML_CUDA_MMQ_FEW_IQ_MIN");
return m ? std::max(atoi(m), 0) : 7;
}();
return min_ncols > 0 ? make_int2(min_ncols, 16) : make_int2(0, 0);
}
default: return make_int2(0, 0);
}
}
Expand Down Expand Up @@ -1377,6 +1633,15 @@ static void ggml_cuda_mul_mat_q_few_wmma(ggml_backend_cuda_context & ctx, const
case GGML_TYPE_IQ4_NL:
launch_mul_mat_q_few_wmma_switch_ncols<GGML_TYPE_IQ4_NL>(src0, src1, dst, src1_q.get(), stream);
break;
case GGML_TYPE_IQ2_S:
launch_mul_mat_q_few_wmma_switch_ncols<GGML_TYPE_IQ2_S>(src0, src1, dst, src1_q.get(), stream);
break;
case GGML_TYPE_IQ3_XXS:
launch_mul_mat_q_few_wmma_switch_ncols<GGML_TYPE_IQ3_XXS>(src0, src1, dst, src1_q.get(), stream);
break;
case GGML_TYPE_IQ3_S:
launch_mul_mat_q_few_wmma_switch_ncols<GGML_TYPE_IQ3_S>(src0, src1, dst, src1_q.get(), stream);
break;
default:
GGML_ABORT("unsupported type for few-row MMQ with WMMA");
}
Expand Down Expand Up @@ -1509,7 +1774,8 @@ static int ggml_cuda_mmq_few_min_ncols(const ggml_type type, const int cc, const
}
const int min_ncols = mmq_few_wmma_ncols_range(type, cc).x;
// fork: on the RX 7900 XT the mat-vec kernels with their fused variants are as fast or faster up to 6 columns
return GGML_CUDA_CC_IS_RDNA3_0(cc) && min_ncols != 0 ? std::max(min_ncols, 7) : min_ncols;
const bool iq = type == GGML_TYPE_IQ2_S || type == GGML_TYPE_IQ3_XXS || type == GGML_TYPE_IQ3_S;
return GGML_CUDA_CC_IS_RDNA3_0(cc) && min_ncols != 0 && !iq ? std::max(min_ncols, 7) : min_ncols;
}

// NVIDIA Ampere to Blackwell
Expand All @@ -1521,7 +1787,8 @@ static int ggml_cuda_mmq_few_min_ncols(const ggml_type type, const int cc, const
return 0;
}
// with fewer than 8 units of 256 values per row the warps idle, MMVQ and MMQ are better there (MUL_MAT_ID splits the rows instead)
if (mmq_few_unit_size(type) == 0 || (!has_ids && ne00 < 8*QK_K)) {
// IQ2_S and IQ3_XXS only have a WMMA decoder
if (mmq_few_unit_size(type) == 0 || type == GGML_TYPE_IQ2_S || type == GGML_TYPE_IQ3_XXS || (!has_ids && ne00 < 8*QK_K)) {
return 0;
}
if (has_ids && !mmq_few_id_supported(type)) {
Expand Down
Loading
Loading