Commit 8f50b1db9 for llama.cpp
commit 8f50b1db9a53059029f35bb6ae0084c01cee7b0b
Author: anujj <ajalota@nvidia.com>
Date: Sat Dec 5 15:40:04 2026 +0530
CUDA: few-row MMQ for F8_E4M3 weights (#33584)
* CUDA: few-row MMQ for F8_E4M3
* CUDA: few-row MMQ for F8_E4M3 on Ada and Ampere
* CUDA: few-row gate checks dim-0 strides and F8 src1 precision
diff --git a/ggml/src/ggml-cuda/ggml-cuda.cu b/ggml/src/ggml-cuda/ggml-cuda.cu
index fbac41fdb..01c07a3df 100644
--- a/ggml/src/ggml-cuda/ggml-cuda.cu
+++ b/ggml/src/ggml-cuda/ggml-cuda.cu
@@ -2220,9 +2220,9 @@ static bool ggml_cuda_mul_mat(ggml_backend_cuda_context & ctx, const ggml_tensor
return false;
}
- // mmvq and mmq can fold the weight scale (src[2]) into their epilogue for NVFP4 per-tensor scales
+ // mmvq and mmq can fold the weight scale (src[2]) into their epilogue for NVFP4 per-tensor scales, few-row mmq for F8_E4M3
const bool scale_fold = ggml_get_src_scale(dst, 0) != NULL &&
- src0->type == GGML_TYPE_NVFP4 && ggml_nelements(ggml_get_src_scale(dst, 0)) == 1;
+ (src0->type == GGML_TYPE_NVFP4 || src0->type == GGML_TYPE_F8_E4M3) && ggml_nelements(ggml_get_src_scale(dst, 0)) == 1;
ggml_cuda_mm_fusion_args_host fusion = {};
if (scale_fold) {
fusion.x_scale = ggml_get_src_scale(dst, 0);
@@ -2243,7 +2243,10 @@ static bool ggml_cuda_mul_mat(ggml_backend_cuda_context & ctx, const ggml_tensor
const int cc = ggml_cuda_info().devices[ctx.device].cc;
const int warp_size = ggml_cuda_info().devices[ctx.device].warp_size;
- if (ggml_cuda_should_use_mmvf(src0, src1->type, cc, warp_size, ne11, /*mul_mat_id =*/ false)) {
+ // from a per-type number of src1 columns up to 24, few-row MMQ is faster than MMVF (F8_E4M3), MMVQ and MMQ
+ const bool use_mmq_few = ggml_cuda_should_use_mmq_few(src0, src1, nullptr, dst, cc);
+
+ if (!use_mmq_few && ggml_cuda_should_use_mmvf(src0, src1->type, cc, warp_size, ne11, /*mul_mat_id =*/ false)) {
// The custom vector kernel can be used over batched cuBLAS GEMM.
// But this is only faster for GPUs without tensor cores or with a thin src0 matrix (particularly KQV in attention)
ggml_cuda_mul_mat_vec_f(ctx, src0, src1, nullptr, dst);
@@ -2267,8 +2270,6 @@ static bool ggml_cuda_mul_mat(ggml_backend_cuda_context & ctx, const ggml_tensor
ggml_cuda_mul_mat_f(ctx, src0, src1, nullptr, dst);
return false;
}
- // from a per-type number of src1 columns up to 24, few-row MMQ is faster than MMVQ and MMQ
- const bool use_mmq_few = ggml_cuda_should_use_mmq_few(src0, src1, nullptr, dst, cc);
if (!use_mmq_few && ggml_cuda_should_use_mmvq(src0->type, cc, ne11)) {
const bool fold = scale_fold && ggml_cuda_should_fuse_mul_mat_vec_q(dst);
ggml_cuda_mul_mat_vec_q(ctx, src0, src1, nullptr, dst, fold ? &fusion : nullptr);
diff --git a/ggml/src/ggml-cuda/mmq-few.cu b/ggml/src/ggml-cuda/mmq-few.cu
index 632ce4b71..ab258f182 100644
--- a/ggml/src/ggml-cuda/mmq-few.cu
+++ b/ggml/src/ggml-cuda/mmq-few.cu
@@ -12,7 +12,8 @@
using namespace ggml_cuda_mma;
-// Few-row MMQ: int8 MMA mat-mul for 2-24 src1 columns (block scaled FP4 MMA for NVFP4 with 4-bit src1). A CUDA block has 16 src0 rows (32 for 2 column groups on large matrices, not with 4-bit src1).
+// Few-row MMQ: int8 MMA mat-mul for 2-24 src1 columns (block scaled FP4 MMA for NVFP4 with 4-bit src1, FP16 MMA with FP16 src1 for F8_E4M3).
+// A CUDA block has 16 src0 rows (32 for 2 column groups on large matrices, not with 4-bit src1).
// Its warps split K in units of 256 values and copy raw src0 rows into their own cp.async ring, src1 is quantized to the MMA fragment layout.
// MUL_MAT_ID: one column group per expert and CUDA block, for short rows the warps split the rows instead of K (64 rows per block).
@@ -28,16 +29,17 @@ static constexpr __host__ __device__ int mmq_few_unit_size_of() {
// src0 bytes per 256 values, 0 for the types without a few-row decoder.
static constexpr __host__ __device__ int mmq_few_unit_size(const ggml_type type) {
switch (type) {
- case GGML_TYPE_Q8_0: return mmq_few_unit_size_of<GGML_TYPE_Q8_0>();
- case GGML_TYPE_Q3_K: return mmq_few_unit_size_of<GGML_TYPE_Q3_K>();
- 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_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>();
- case GGML_TYPE_NVFP4: return mmq_few_unit_size_of<GGML_TYPE_NVFP4>();
- default: return 0;
+ case GGML_TYPE_Q8_0: return mmq_few_unit_size_of<GGML_TYPE_Q8_0>();
+ case GGML_TYPE_Q3_K: return mmq_few_unit_size_of<GGML_TYPE_Q3_K>();
+ 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_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>();
+ case GGML_TYPE_NVFP4: return mmq_few_unit_size_of<GGML_TYPE_NVFP4>();
+ case GGML_TYPE_F8_E4M3: return QK_K;
+ default: return 0;
}
}
@@ -48,8 +50,9 @@ static constexpr __host__ __device__ mmq_q8_1_ds_layout mmq_few_ds_layout(const
// Units of 256 values per ring stage: 1 for Q6_K, 3 column groups (fewer free registers) and 32-row tiles (2 blocks per SM), else 2.
// fp4: NVFP4 with 4-bit src1, which has enough free registers for 2 units with 3 column groups.
+// F8_E4M3: 1 unit, else a block needs 72 KB of shared memory and only one fits into an SM.
static constexpr __host__ __device__ int mmq_few_nunits_stage(const ggml_type type, const int ncg, const int nmt, const bool fp4) {
- return type == GGML_TYPE_Q6_K || (ncg > 2 && !fp4) || nmt > 1 ? 1 : 2;
+ return type == GGML_TYPE_Q6_K || type == GGML_TYPE_F8_E4M3 || (ncg > 2 && !fp4) || nmt > 1 ? 1 : 2;
}
// A third ring stage only makes Q6_K faster, the other types get slower on large matrices.
@@ -69,8 +72,12 @@ static constexpr __host__ __device__ int mmq_few_nchunks(const ggml_type type, c
}
// Unaligned rows are padded to an odd number of 16 byte chunks against bank conflicts, for aligned rows the padding costs more than it saves.
+// F8_E4M3: the 8 lanes of each phase of a 16-byte load read 64 bytes from each of 2 adjacent rows, a padding of 64 bytes puts the 2 rows into different banks.
static constexpr __host__ __device__ int mmq_few_sram_stride(const ggml_type type, const int nunits_stage) {
const int nchunks = mmq_few_nchunks(type, nunits_stage);
+ if (type == GGML_TYPE_F8_E4M3) {
+ return 16*nchunks + 64;
+ }
return mmq_few_unaligned(type, nunits_stage) && nchunks % 2 == 0 ? 16*(nchunks + 1) : 16*nchunks;
}
@@ -85,8 +92,9 @@ static constexpr __host__ __device__ bool mmq_few_tall_supported(const ggml_type
}
// The MUL_MAT_ID src1 quantizer keeps the natural value order, the NVFP4 decoder needs the nvfp4 order (see quantize_mmq_few_q8_1).
+// F8_E4M3 needs FP16 src1, which has no MUL_MAT_ID quantizer.
static constexpr __host__ __device__ bool mmq_few_id_supported(const ggml_type type) {
- return type != GGML_TYPE_NVFP4;
+ return type != GGML_TYPE_NVFP4 && type != GGML_TYPE_F8_E4M3;
}
// MUL_MAT_ID with fewer than 2 units of 256 values per warp: the warps split the rows, each warp has its own 16 rows.
@@ -125,6 +133,20 @@ static __device__ __forceinline__ void mmq_few_load_y(mmq_few_y_fp4 & yf, const
yf.sc = y[2*WARP_SIZE + threadIdx.x/4];
}
+// The src1 fragments of a lane for F8_E4M3, see MMQ_FEW_F16_INT4: the B fragments of the 4 blocks of 64 values and the scales of the columns 2t and 2t + 1.
+struct mmq_few_y_f16 {
+ int4 h[8];
+ float2 d;
+};
+
+static __device__ __forceinline__ void mmq_few_load_y(mmq_few_y_f16 & yf, const int4 * y) {
+#pragma unroll
+ for (int j = 0; j < 8; ++j) {
+ yf.h[j] = y[j*WARP_SIZE + threadIdx.x];
+ }
+ yf.d = ((const float2 *) (y + 8*WARP_SIZE))[threadIdx.x % 4];
+}
+
static __device__ __forceinline__ int2 mmq_few_get_b(const mmq_few_y_frag & yf, const int s) {
const int4 q = yf.qs[s/2];
return s % 2 == 0 ? make_int2(q.x, q.y) : make_int2(q.z, q.w);
@@ -581,6 +603,60 @@ static __device__ __forceinline__ void mmq_few_vec_dot_nvfp4_fp4(const char * co
}
}
+// The 2 E4M3 values in the low 16 bits of q as half2, exact.
+static __device__ __forceinline__ int mmq_few_e4m3x2_to_half2(const int q) {
+#ifdef FP8_AVAILABLE
+ // sm_89 and newer: one instruction, older GPUs: software conversion in the CUDA headers
+ const __half2_raw h = __nv_cvt_fp8x2_to_halfraw2((__nv_fp8x2_storage_t) (q & 0xFFFF), __NV_E4M3);
+ return int(h.x) | (int(h.y) << 16);
+#else
+ // CUDA < 11.8 has no cuda_fp8.h
+ const half2 h = __floats2half2_rn(ggml_cuda_f8_e4m3_to_fp32(q & 0xFF), ggml_cuda_f8_e4m3_to_fp32((q >> 8) & 0xFF));
+ return *(const int *) &h;
+#endif // FP8_AVAILABLE
+}
+
+// F8_E4M3: FP16 m16n8k16 MMAs with FP32 accumulation, the E4M3 values are converted to FP16. The scale of src1 is per unit and column.
+// In each block of 64 values the lane takes the 16 bytes [16t, 16t + 16) of its rows, int j of them is MMA j. src1 has the same k order (MMQ_FEW_F16_INT4).
+template <int ncg>
+static __device__ __forceinline__ void mmq_few_vec_dot_f8(const char * const (&x)[2], const mmq_few_y_f16 (&yf)[ncg], float (&sum)[ncg][4]) {
+ const int t = threadIdx.x % 4;
+
+ tile<16, 8, float> C[ncg];
+#pragma unroll
+ for (int b = 0; b < QK_K/64; ++b) {
+ const int4 q0 = *(const int4 *) (x[0] + 64*b + 16*t);
+ const int4 q1 = *(const int4 *) (x[1] + 64*b + 16*t);
+ const int q[2][4] = {{q0.x, q0.y, q0.z, q0.w}, {q1.x, q1.y, q1.z, q1.w}};
+#pragma unroll
+ for (int j = 0; j < 4; ++j) {
+ // A: k = 2t, 2t + 1 of the rows g and g + 8 (low 16 bits), then k = 2t + 8, 2t + 9 (high 16 bits)
+ tile<16, 8, half2> A;
+ int * Ai = (int *) A.x;
+ Ai[0] = mmq_few_e4m3x2_to_half2(q[0][j]);
+ Ai[1] = mmq_few_e4m3x2_to_half2(q[1][j]);
+ Ai[2] = mmq_few_e4m3x2_to_half2(q[0][j] >> 16);
+ Ai[3] = mmq_few_e4m3x2_to_half2(q[1][j] >> 16);
+#pragma unroll
+ for (int c = 0; c < ncg; ++c) {
+ const int * h = (const int *) &yf[c].h[2*b + j/2];
+ tile<8, 8, half2> B;
+ int * Bi = (int *) B.x;
+ Bi[0] = h[2*(j % 2) + 0];
+ Bi[1] = h[2*(j % 2) + 1];
+ mma(C[c], A, B);
+ }
+ }
+ }
+#pragma unroll
+ for (int c = 0; c < ncg; ++c) {
+ sum[c][0] += yf[c].d.x*C[c].x[0];
+ sum[c][1] += yf[c].d.y*C[c].x[1];
+ sum[c][2] += yf[c].d.x*C[c].x[2];
+ sum[c][3] += yf[c].d.y*C[c].x[3];
+ }
+}
+
template <ggml_type type, int ncg>
static __device__ __forceinline__ void mmq_few_vec_dot(const char * const (&x)[2], const mmq_few_y_frag (&yf)[ncg], float (&sum)[ncg][4]) {
if constexpr (type == GGML_TYPE_Q8_0 || type == GGML_TYPE_IQ4_NL) {
@@ -605,7 +681,7 @@ static __device__ __forceinline__ void mmq_few_vec_dot(const char * const (&x)[2
#endif // defined(CP_ASYNC_AVAILABLE)
// has_ids: MUL_MAT_ID, blockIdx.y is a pair of quantize_mmq_few_id_q8_1, its expert is the src0 channel and dst dims 1 and 2 are slot and token.
-// fp4: NVFP4 src0 with 4-bit src1, which has a scale per column in y_scale. x_scale is an optional scale of all of src0.
+// fp4: NVFP4 src0 with 4-bit src1, which has a scale per column in y_scale. x_scale is an optional scale of all of src0 (NVFP4, F8_E4M3).
template <ggml_type type, int ncg, int nmt, bool has_ids, bool fp4>
__launch_bounds__(MMQ_FEW_NWARPS*ggml_cuda_get_physical_warp_size(), 1)
static __global__ void mul_mat_q_few(
@@ -619,9 +695,11 @@ static __global__ void mul_mat_q_few(
static_assert(warp_size == WARP_SIZE, "unsupported warp size");
static_assert(!has_ids || (ncg == 1 && nmt == 1), "unsupported MUL_MAT_ID config");
static_assert(!fp4 || (type == GGML_TYPE_NVFP4 && !has_ids), "4-bit src1 is only supported for NVFP4 MUL_MAT");
+ static_assert(type != GGML_TYPE_F8_E4M3 || !has_ids, "F8_E4M3 is only supported for MUL_MAT");
- using y_frag_t = std::conditional_t<fp4, mmq_few_y_fp4, mmq_few_y_frag>;
- constexpr int y_int4 = fp4 ? MMQ_FEW_FP4_INT4 : MMQ_FEW_Q8_1_INT4;
+ constexpr bool f16_y = type == GGML_TYPE_F8_E4M3; // FP16 src1
+ using y_frag_t = std::conditional_t<fp4, mmq_few_y_fp4, std::conditional_t<f16_y, mmq_few_y_f16, mmq_few_y_frag>>;
+ constexpr int y_int4 = fp4 ? MMQ_FEW_FP4_INT4 : (f16_y ? MMQ_FEW_F16_INT4 : MMQ_FEW_Q8_1_INT4);
constexpr int unit_size = mmq_few_unit_size(type);
constexpr int nrows = nmt*MMQ_FEW_NROWS;
@@ -773,6 +851,8 @@ static __global__ void mul_mat_q_few(
};
if constexpr (fp4) {
mmq_few_vec_dot_nvfp4_fp4<ncg>(xr, yf[k], sum[m]);
+ } else if constexpr (f16_y) {
+ mmq_few_vec_dot_f8<ncg>(xr, yf[k], sum[m]);
} else {
mmq_few_vec_dot<type, ncg>(xr, yf[k], sum[m]);
}
@@ -820,6 +900,8 @@ static __global__ void mul_mat_q_few(
if constexpr (fp4) {
y_scale += (int64_t(sample_dst)*nchannels + channel_dst)*ncg*MMQ_FEW_NCOLS;
}
+ } else if constexpr (type == GGML_TYPE_F8_E4M3) {
+ scale_x = x_scale ? *x_scale : 1.0f;
}
#pragma unroll
@@ -836,6 +918,8 @@ static __global__ void mul_mat_q_few(
dst[int64_t(e / ncols_dst)*stride_channel_dst + int64_t(e % ncols_dst)*stride_col_dst + i] = sum[m][c][l];
} else if constexpr (type == GGML_TYPE_NVFP4) {
dst[int64_t(j)*stride_col_dst + i] = (fp4 ? y_scale[j]*scale_x : scale_x)*sum[m][c][l];
+ } else if constexpr (type == GGML_TYPE_F8_E4M3) {
+ dst[int64_t(j)*stride_col_dst + i] = scale_x*sum[m][c][l];
} else {
dst[int64_t(j)*stride_col_dst + i] = sum[m][c][l];
}
@@ -1399,10 +1483,10 @@ void ggml_cuda_mul_mat_q_few(ggml_backend_cuda_context & ctx, const ggml_tensor
return;
}
- // fusion: only the scale of an NVFP4 src0
+ // fusion: only the scale of an NVFP4 or F8_E4M3 src0
const ggml_tensor * x_scale = fusion ? fusion->x_scale : nullptr;
- GGML_ASSERT(!fusion || (!ids && src0->type == GGML_TYPE_NVFP4 && !fusion->x_bias && !fusion->gate && !fusion->gate_bias && !fusion->gate_scale &&
- !fusion->shared_up && !fusion->shared_gate && !fusion->shared_dst));
+ GGML_ASSERT(!fusion || (!ids && (src0->type == GGML_TYPE_NVFP4 || src0->type == GGML_TYPE_F8_E4M3) &&
+ !fusion->x_bias && !fusion->gate && !fusion->gate_bias && !fusion->gate_scale && !fusion->shared_up && !fusion->shared_gate && !fusion->shared_dst));
GGML_ASSERT(!x_scale || (x_scale->type == GGML_TYPE_F32 && ggml_nelements(x_scale) == 1));
const float * x_scale_d = x_scale ? (const float *) x_scale->data : nullptr;
@@ -1413,6 +1497,17 @@ void ggml_cuda_mul_mat_q_few(ggml_backend_cuda_context & ctx, const ggml_tensor
const int cc = ggml_cuda_info().devices[ctx.device].cc;
const bool fp4 = !ids && mmq_few_use_fp4(src0->type, fusion ? fusion->prec_src1 : ggml_prec_get_src(dst, 1), cc);
+ if (src0->type == GGML_TYPE_F8_E4M3) {
+ GGML_ASSERT(!ids);
+ GGML_ASSERT(ne11 <= MMQ_FEW_MAX_NCG*MMQ_FEW_NCOLS);
+ GGML_ASSERT(ne12 % ne02 == 0 && ne13 % ne03 == 0);
+
+ ggml_cuda_pool_alloc<int4> src1_q(ctx.pool(), int64_t(ne12)*ne13*nunits*ncg*MMQ_FEW_F16_INT4);
+ quantize_mmq_few_f16_cuda(src1->data, src1->type, src1_q.get(), ne10, nb11/nb10, nb12/nb10, nb13/nb10, ne11, ne12, ne13, stream);
+ launch_mul_mat_q_few_switch_ncg<GGML_TYPE_F8_E4M3>(src0, src1, dst, src1_q.get(), nullptr, x_scale_d, nullptr, 0, ncg, stream);
+ return;
+ }
+
if (fp4) {
GGML_ASSERT(ne11 <= MMQ_FEW_MAX_NCG*MMQ_FEW_NCOLS);
GGML_ASSERT(ne12 % ne02 == 0 && ne13 % ne03 == 0);
@@ -1522,6 +1617,20 @@ static int ggml_cuda_mmq_few_min_ncols(const ggml_type type, const int cc, const
}
return fp4 ? 6 : 7;
}
+ // F8_E4M3: tuned on RTX 5090 and DGX Spark (Blackwell), RTX 4090 (Ada) and RTX 3090 Ti (Ampere)
+ if (type == GGML_TYPE_F8_E4M3) {
+ if (blackwell_mma_available(cc)) {
+ return 2;
+ }
+ // Ada needs its own device code, as the E4M3 to FP16 conversion has a hardware instruction only from sm_89
+ if (cc == GGML_CUDA_CC_ADA_LOVELACE && ggml_cuda_highest_compiled_arch(cc) == GGML_CUDA_CC_ADA_LOVELACE) {
+ return 4; // equal to MMVF at 2-3 columns
+ }
+ if (cc == GGML_CUDA_CC_AMPERE_GA10X) {
+ return 6; // slower than MMVF at 2-4 columns, equal at 5
+ }
+ return 0;
+ }
if (cc == GGML_CUDA_CC_GB10) { // tuned on DGX Spark GB10, also for MUL_MAT_ID
return type == GGML_TYPE_IQ4_XS || type == GGML_TYPE_IQ4_NL ? 8 : 2;
}
@@ -1542,17 +1651,28 @@ static int ggml_cuda_mmq_few_min_ncols(const ggml_type type, const int cc, const
}
bool ggml_cuda_should_use_mmq_few(const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * ids, const ggml_tensor * dst, const int cc) {
+ if (src0->nb[0] != ggml_type_size(src0->type) || src1->nb[0] != ggml_type_size(src1->type)) {
+ return false;
+ }
const int64_t ncols = ids ? ids->ne[1] : src1->ne[1];
const ggml_prec prec_requested = ggml_prec_get_src(dst, 1);
// NVFP4: src1 precisions that MMQ does not take (e.g. F16 without GGML_CUDA_MMQ_PREC) stay with MMVQ and MMQ, like without few-row
if (src0->type == GGML_TYPE_NVFP4 && !ggml_cuda_mmq_prec_src1_supported(prec_requested)) {
return false;
}
+ // F8_E4M3: few-row converts src1 to F16, F32 and BF16 requests do not allow that
+ if (src0->type == GGML_TYPE_F8_E4M3 && (prec_requested == GGML_PREC_F32 || prec_requested == GGML_PREC_BF16)) {
+ return false;
+ }
const bool fp4 = !ids && mmq_few_use_fp4(src0->type, prec_requested, cc);
const int min_ncols = ggml_cuda_mmq_few_min_ncols(src0->type, cc, src0->ne[0], ids != nullptr, fp4);
if (min_ncols == 0 || ncols < min_ncols || ncols > MMQ_FEW_MAX_NCG*MMQ_FEW_NCOLS) {
return false;
}
+ // MUL_MAT: the src1 quantizer launches ne12*ne13 CUDA blocks in z, the limit is 65535
+ if (!ids && src1->ne[2]*src1->ne[3] > 65535) {
+ return false;
+ }
// MUL_MAT_ID: the pairs are built in shared memory
if (ids && (2*src0->ne[2] + 1)*sizeof(int) > ggml_cuda_info().devices[ggml_cuda_get_device()].smpb) {
return false;
diff --git a/ggml/src/ggml-cuda/mmq-few.cuh b/ggml/src/ggml-cuda/mmq-few.cuh
index 7ef09c245..8a1a4db8f 100644
--- a/ggml/src/ggml-cuda/mmq-few.cuh
+++ b/ggml/src/ggml-cuda/mmq-few.cuh
@@ -5,6 +5,6 @@
// ids != nullptr: MUL_MAT_ID. dst is the MUL_MAT or MUL_MAT_ID node, it holds the src1 precision requested by the graph.
bool ggml_cuda_should_use_mmq_few(const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * ids, const ggml_tensor * dst, int cc);
-// fusion: only the scalar weight scale of an NVFP4 src0 (x_scale) and the requested src1 precision of the MUL_MAT, applied in the write-back.
+// fusion: only the scalar weight scale of an NVFP4 or F8_E4M3 src0 (x_scale) and the requested src1 precision of the MUL_MAT, applied in the write-back.
void ggml_cuda_mul_mat_q_few(ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * ids, ggml_tensor * dst,
const ggml_cuda_mm_fusion_args_host * fusion = nullptr);
diff --git a/ggml/src/ggml-cuda/quantize.cu b/ggml/src/ggml-cuda/quantize.cu
index 875a6dd72..c2f963e6d 100644
--- a/ggml/src/ggml-cuda/quantize.cu
+++ b/ggml/src/ggml-cuda/quantize.cu
@@ -1012,6 +1012,74 @@ void quantize_mmq_few_nvfp4_cuda(
});
}
+// A warp converts one unit of 256 values of one column, each thread 8 values. Columns beyond ne1 are zero.
+// The scale is a power of 2, so the FP16 values are the src1 values rounded to 11 bits, without overflow.
+template <typename src_t>
+static __global__ void quantize_mmq_few_f16(
+ const src_t * x_ptr, void * vy_ptr, const int64_t ne00, const int64_t s01, const int64_t s02, const int64_t s03,
+ const int ne1, const int ne2) {
+ ggml_cuda_pdl_lc();
+ const src_t * GGML_CUDA_RESTRICT x = x_ptr;
+ int4 * GGML_CUDA_RESTRICT vy = (int4 *) vy_ptr;
+ const int64_t i0 = ((int64_t)blockDim.x*blockIdx.y + threadIdx.x)*8;
+
+ if (i0 >= ne00) { // ne00 % QK_K == 0, so all threads of a warp return
+ return;
+ }
+ ggml_cuda_pdl_sync();
+
+ const int64_t i2 = blockIdx.z % ne2;
+ const int64_t i3 = blockIdx.z / ne2;
+ float4 lo = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
+ float4 hi = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
+ if ((int) blockIdx.x < ne1) {
+ const src_t * xc = x + i3*s03 + i2*s02 + blockIdx.x*s01;
+ lo = quantize_load4(xc + i0, 0);
+ hi = quantize_load4(xc + i0, 1);
+ }
+
+ float amax = fmaxf(fmaxf(fmaxf(fabsf(lo.x), fabsf(lo.y)), fmaxf(fabsf(lo.z), fabsf(lo.w))),
+ fmaxf(fmaxf(fabsf(hi.x), fabsf(hi.y)), fmaxf(fabsf(hi.z), fabsf(hi.w))));
+ amax = warp_reduce_max<WARP_SIZE>(amax);
+
+ // amax/d in [2^14, 2^15) for amax in [2^-110, 2^128), e is the exponent of amax
+ const int e = min(max((__float_as_int(amax) >> 23) - 127, -110), 127);
+ const float d_inv = amax > 0.0f ? __int_as_float((127 + 14 - e) << 23) : 0.0f;
+ const float d = amax > 0.0f ? __int_as_float((127 - 14 + e) << 23) : 0.0f;
+
+ __align__(16) half2 h[4];
+ h[0] = __floats2half2_rn(lo.x*d_inv, lo.y*d_inv);
+ h[1] = __floats2half2_rn(lo.z*d_inv, lo.w*d_inv);
+ h[2] = __floats2half2_rn(hi.x*d_inv, hi.y*d_inv);
+ h[3] = __floats2half2_rn(hi.z*d_inv, hi.w*d_inv);
+
+ const int g = blockIdx.x % MMQ_FEW_NCOLS; // column in group
+ const int iv = threadIdx.x % WARP_SIZE; // the values [8iv, 8iv + 8) of the unit
+ const int b = iv / 8; // block of 64 values
+ const int t = (iv % 8) / 2;
+ const int q = iv % 2;
+ int4 * yg = vy + ((blockIdx.z*(ne00/QK_K) + i0/QK_K)*(gridDim.x/MMQ_FEW_NCOLS) + blockIdx.x/MMQ_FEW_NCOLS)*MMQ_FEW_F16_INT4;
+ yg[(2*b + q)*WARP_SIZE + 4*g + t] = *(const int4 *) h;
+ if (iv == 0) {
+ ((float *) (yg + 8*WARP_SIZE))[g] = d;
+ }
+}
+
+void quantize_mmq_few_f16_cuda(
+ const void * x, const ggml_type type_src1, void * vy, const int64_t ne00, const int64_t s01, const int64_t s02, const int64_t s03,
+ const int64_t ne1, const int64_t ne2, const int64_t ne3, cudaStream_t stream) {
+ GGML_ASSERT(ne00 % QK_K == 0);
+
+ const int64_t block_num_y = (ne00 + 8*CUDA_QUANTIZE_BLOCK_SIZE_MMQ - 1) / (8*CUDA_QUANTIZE_BLOCK_SIZE_MMQ);
+ const dim3 num_blocks(GGML_PAD(ne1, MMQ_FEW_NCOLS), block_num_y, ne2*ne3);
+ const dim3 block_size(CUDA_QUANTIZE_BLOCK_SIZE_MMQ, 1, 1);
+ const ggml_cuda_kernel_launch_params launch_params = ggml_cuda_kernel_launch_params(num_blocks, block_size, 0, stream);
+ quantize_launch_typed(x, type_src1, [&](auto * x_t) {
+ using src_t = std::remove_cv_t<std::remove_pointer_t<decltype(x_t)>>;
+ ggml_cuda_kernel_launch(quantize_mmq_few_f16<src_t>, launch_params, x_t, vy, ne00, s01, s02, s03, (int) ne1, (int) ne2);
+ });
+}
+
void quantize_mmq_few_q8_1_cuda(
const void * x, const ggml_type type_src1, void * vy, const mmq_q8_1_ds_layout ds_layout, const bool nvfp4_order, const int64_t ne00, const int64_t s01, const int64_t s02, const int64_t s03,
const int64_t ne1, const int64_t ne2, const int64_t ne3, cudaStream_t stream) {
diff --git a/ggml/src/ggml-cuda/quantize.cuh b/ggml/src/ggml-cuda/quantize.cuh
index a73c1f1a3..55e0b7fa8 100644
--- a/ggml/src/ggml-cuda/quantize.cuh
+++ b/ggml/src/ggml-cuda/quantize.cuh
@@ -47,6 +47,18 @@ void quantize_mmq_few_nvfp4_cuda(
const void * x, ggml_type type_src1, void * vy, float * y_scale, int64_t ne00, int64_t s01, int64_t s02, int64_t s03,
int64_t ne1, int64_t ne2, int64_t ne3, cudaStream_t stream);
+// src1 of the few-row MMQ for F8_E4M3 src0 (FP16 m16n8k16 MMA): FP16 values with a power of 2 scale per 256 values and column.
+// In a block of 64 values lane (g, t) takes the values [16t, 16t + 16), so that the src0 bytes of a lane are contiguous.
+// Value 16t + 4j + i is k = 2t + i (i < 2) or k = 2t + 8 + i - 2 (i >= 2) of MMA j of the block.
+// Per channel, 256 values and column group:
+// h: int4[8][32], [2b + q][lane] holds the values 16t + 8q + [0, 8) of block b of column g, divided by the scale.
+// d: int4[2], the scales of the 8 columns as float.
+static constexpr int MMQ_FEW_F16_INT4 = 8*WARP_SIZE + 2; // int4 per 256 values and column group
+
+void quantize_mmq_few_f16_cuda(
+ const void * x, ggml_type type_src1, void * vy, int64_t ne00, int64_t s01, int64_t s02, int64_t s03,
+ int64_t ne1, int64_t ne2, int64_t ne3, cudaStream_t stream);
+
// MUL_MAT_ID: a pair is an expert and one column group of up to MMQ_FEW_NCOLS of its (token, slot) entries. The src1 of pair p is channel p above, its columns past the entries are not written.
// A pair is MMQ_FEW_PAIR_INTS ints: expert, number of entries (0 for an unused pair), then the entries token*n_expert_used + slot.
static constexpr int MMQ_FEW_PAIR_INTS = 2 + MMQ_FEW_NCOLS;
diff --git a/tests/test-backend-ops.cpp b/tests/test-backend-ops.cpp
index ee481f5f3..93234deca 100644
--- a/tests/test-backend-ops.cpp
+++ b/tests/test-backend-ops.cpp
@@ -6560,8 +6560,9 @@ struct test_mul_mat : public test_case {
backend_has_feature(backend, "BLACKWELL_NATIVE_FP4")) {
return 2e-2;
}
+ // src1 is converted to FP8 (cuBLASLt) only above 8 columns, MMVF (F32) and few-row MMQ (FP16) keep it precise
if (type_a == GGML_TYPE_F8_E4M3 && backend_has_feature(backend, "NATIVE_FP8")) {
- return 5e-3;
+ return n > 8 ? 5e-3 : 1e-4;
}
return max_nmse_err();
}
@@ -6614,7 +6615,8 @@ struct test_mul_mat : public test_case {
a = ggml_new_tensor_4d(ctx_a, type_a, ne_a[per[0]], ne_a[per[1]], ne_a[per[2]], ne_a[per[3]]);
b = ggml_new_tensor_4d(ctx, type_b, ne_b[per[0]], ne_b[per[1]], ne_b[per[2]], ne_b[per[3]]);
if (!ggml_is_quantized(type_a)) {
- if (bs[1] == 1 && nr[1] == 1) {
+ // with weights, a is not a param: the backends do not treat params as constant weights
+ if (bs[1] == 1 && nr[1] == 1 && !weights) {
ggml_set_param(a);
}
ggml_set_param(b);
@@ -6647,7 +6649,8 @@ struct test_mul_mat : public test_case {
b = ggml_new_tensor_4d(ctx, type_b, k_physical, n, bs[0]*nr[0], bs[1]*nr[1]);
if (!ggml_is_quantized(type_a)) {
- if (bs[1] == 1 && nr[1] == 1) {
+ // with weights, a is not a param: the backends do not treat params as constant weights
+ if (bs[1] == 1 && nr[1] == 1 && !weights) {
ggml_set_param(a);
}
ggml_set_param(b);
@@ -6941,8 +6944,9 @@ struct test_mul_mat_id : public test_case {
backend_has_feature(backend, "BLACKWELL_NATIVE_FP4")) {
return 2e-2;
}
+ // src1 is converted to FP8 (cuBLASLt) only above 8 columns, MMVF (F32) and few-row MMQ (FP16) keep it precise
if (type_a == GGML_TYPE_F8_E4M3 && backend_has_feature(backend, "NATIVE_FP8")) {
- return 5e-3;
+ return n > 8 ? 5e-3 : 1e-4;
}
return max_nmse_err();
}
@@ -9459,6 +9463,34 @@ struct test_mul_mat_8bit_default : public test_mul_mat {
}
};
+// mul_mat with the src1 values scaled by 2^(r % 3 + u % 3), r the src1 row and u the block of 256 values in the row.
+// CUDA few-row MMQ for F8_E4M3 converts src1 to F16 with a power of 2 scale per 256 values and src1 row. With these values the scales differ, so a wrong or missing scale changes the result.
+struct test_mul_mat_src1_pow2 : public test_mul_mat {
+ using test_mul_mat::test_mul_mat;
+
+ std::string vars() override {
+ return test_mul_mat::vars() + ",src1=x2^(r%3+u%3)";
+ }
+
+ void initialize_tensors(ggml_context * ctx) override {
+ for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != nullptr; t = ggml_get_next_tensor(ctx, t)) {
+ init_tensor_uniform(t);
+ if (std::string(ggml_get_name(t)) != "b") {
+ continue;
+ }
+ GGML_ASSERT(t->type == GGML_TYPE_F32 && ggml_is_contiguous(t));
+ std::vector<float> data(ggml_nelements(t));
+ ggml_backend_tensor_get(t, data.data(), 0, ggml_nbytes(t));
+ for (size_t i = 0; i < data.size(); ++i) {
+ const int64_t r = (int64_t) i / t->ne[0];
+ const int64_t u = (int64_t) i % t->ne[0] / 256;
+ data[i] = ldexpf(data[i], (int) (r % 3 + u % 3));
+ }
+ ggml_backend_tensor_set(t, data.data(), 0, ggml_nbytes(t));
+ }
+ }
+};
+
enum mul_mat_add_mode {
MUL_MAT_ADD_MM_RES, // mm + res
MUL_MAT_ADD_RES_MM, // res + mm
@@ -14115,6 +14147,43 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
test_cases.emplace_back(new test_mul_mat (GGML_TYPE_NVFP4, GGML_TYPE_F32, 1000, 13, 4608, {1, 1}, {1, 1}, {0, 1, 2, 3}, 0, 1, false, 0, 0, 0, false, scale_gran, true));
test_cases.emplace_back(new test_mul_mat_src1_q4(GGML_TYPE_NVFP4, GGML_TYPE_F32, 1000, 13, 4608, {1, 1}, {1, 1}, {0, 1, 2, 3}, 0, 1, false, 0, 0, 0, false, scale_gran, true));
}
+ // CUDA few-row MMQ for F8_E4M3 with F16 src1 (Blackwell): 2-24 columns, 1-3 column groups, partial row tiles, K = 18 units (uneven over the warps) and K = 9 units (odd, one unit per ring stage)
+ for (int64_t n : {2, 3, 5, 8, 9, 13, 16, 17, 21, 24}) {
+ test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F8_E4M3, GGML_TYPE_F32, 1000, n, 4608, {1, 1}, {1, 1}));
+ }
+ test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F8_E4M3, GGML_TYPE_F32, 6170, 13, 4608, {1, 1}, {1, 1}));
+ for (int64_t n : {8, 13, 21}) {
+ test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F8_E4M3, GGML_TYPE_F32, 1000, n, 2304, {1, 1}, {1, 1}));
+ }
+ // src1 scales that differ per src1 row and 256 values, also with broadcast
+ for (int64_t n : {4, 13, 24}) {
+ test_cases.emplace_back(new test_mul_mat_src1_pow2(GGML_TYPE_F8_E4M3, GGML_TYPE_F32, 1000, n, 4608, {1, 1}, {1, 1}));
+ }
+ test_cases.emplace_back(new test_mul_mat_src1_pow2(GGML_TYPE_F8_E4M3, GGML_TYPE_F32, 63, 13, 4608, {3, 2}, {2, 2}));
+ // broadcast, permuted src0 and src1, padded src0 rows, src0 in a weights buffer
+ test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F8_E4M3, GGML_TYPE_F32, 64, 13, 4608, {3, 2}, {2, 2}));
+ test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F8_E4M3, GGML_TYPE_F32, 64, 13, 4608, {2, 3}, {1, 1}, {0, 2, 1, 3}));
+ test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F8_E4M3, GGML_TYPE_F32, 1000, 13, 4608, {1, 1}, {1, 1}, {0, 1, 2, 3}, 5120));
+ test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F8_E4M3, GGML_TYPE_F32, 6170, 13, 4608, {1, 1}, {1, 1}, {0, 1, 2, 3}, 0, 1, false, 0, 0, 0, true));
+ // BF16 src1, contiguous and as a permuted view (rows 4*k values apart)
+ for (int64_t n : {4, 13, 24}) {
+ test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F8_E4M3, GGML_TYPE_BF16, 1000, n, 4608, {1, 1}, {1, 1}));
+ }
+ test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F8_E4M3, GGML_TYPE_BF16, 64, 13, 4608, {1, 1}, {4, 2}, {0, 2, 1, 3}));
+ // weight scale as a strided view: per tensor (applied in the few-row write-back) and per output channel (a separate pass)
+ for (int scale_gran : {1, 2}) {
+ test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F8_E4M3, GGML_TYPE_F32, 1000, 13, 4608, {1, 1}, {1, 1}, {0, 1, 2, 3}, 0, 1, false, 0, 0, 0, false, scale_gran, true));
+ }
+ // KQV with an F8_E4M3 V cache without flash attention: V^T rows padded to the cache size (8192 > n_kv 4096), GQA broadcast 6
+ for (int64_t n : {8, 16}) {
+ test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F8_E4M3, GGML_TYPE_F32, 256, n, 4096, {4, 1}, {6, 1}, {0, 1, 2, 3}, 8192));
+ }
+ // MUL_MAT_ID with more than 8 tokens runs a MUL_MAT per expert, few-row MMQ for the experts with 2-24 tokens: all experts with all tokens, random routing, strided experts with per-channel scales
+ for (int64_t n : {9, 16, 24}) {
+ test_cases.emplace_back(new test_mul_mat_id(GGML_TYPE_F8_E4M3, GGML_TYPE_F32, 4, 4, false, 72, n, 2304));
+ }
+ test_cases.emplace_back(new test_mul_mat_id(GGML_TYPE_F8_E4M3, GGML_TYPE_F32, 8, 2, false, 72, 16, 2304));
+ test_cases.emplace_back(new test_mul_mat_id(GGML_TYPE_F8_E4M3, GGML_TYPE_F32, 4, 4, false, 72, 16, 2304, 80, false, false, 2));
#if 0
{