mirror of
https://github.com/ggml-org/llama.cpp.git
synced 2026-12-01 23:27:39 -06:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
02227e87ae | ||
|
|
94f17f659f |
+4
-1
@@ -1,7 +1,10 @@
|
||||
set(TARGET llama-app)
|
||||
|
||||
add_executable(${TARGET} llama.cpp download.cpp)
|
||||
set_target_properties(${TARGET} PROPERTIES OUTPUT_NAME llama)
|
||||
set_target_properties(${TARGET} PROPERTIES
|
||||
OUTPUT_NAME llama
|
||||
PDB_NAME ${TARGET}
|
||||
)
|
||||
|
||||
target_link_libraries(${TARGET} PRIVATE
|
||||
llama-server-impl
|
||||
|
||||
@@ -317,6 +317,8 @@ Currently supported values: `auto`, `q8`, `q4`.
|
||||
|
||||
NVFP4 and MXFP4 layers marked as W4A16 request 8-bit activations, so on Blackwell those layers run through the W4A8 path instead of the native W4A4 path. Set `q4` to keep the native W4A4 path for faster prompt processing at the cost of accuracy, or `q8` to use the W4A8 path for every layer, `auto` uses per-tensor prec metadata (this is the same behavior as when the environment variable is not set).
|
||||
|
||||
For dense NVFP4 layers with batches of up to 24 tokens on Blackwell GPUs with compute capability 12.x (RTX 50xx, RTX PRO Blackwell, DGX Spark), the few-row MMQ path uses W4A8 at every batch size, so that the precision of a decode batch does not depend on how many sequences or draft tokens it holds. With `q4` it uses W4A4 from 6 tokens (DGX Spark: 8 tokens). MoE experts and the dense layers that the few-row MMQ path does not support keep MMVQ (W4A8) up to 8 tokens and MMQ above.
|
||||
|
||||
### Unified Memory
|
||||
|
||||
The environment variable `GGML_CUDA_ENABLE_UNIFIED_MEMORY=1` can be used to enable unified memory in Linux. This allows swapping to system RAM instead of crashing when the GPU VRAM is exhausted. In Windows this setting is available in the NVIDIA control panel as `System Memory Fallback`.
|
||||
|
||||
@@ -175,7 +175,7 @@ bool ggml_cuda_should_fuse_mul_mat_vec_q(const ggml_tensor * tensor) {
|
||||
}
|
||||
|
||||
// do not fuse the matmuls that few-row MMQ takes in ggml_cuda_mul_mat_id
|
||||
if (tensor->op == GGML_OP_MUL_MAT_ID && ggml_cuda_should_use_mmq_few(src0, src1, tensor->src[2], cc)) {
|
||||
if (tensor->op == GGML_OP_MUL_MAT_ID && ggml_cuda_should_use_mmq_few(src0, src1, tensor->src[2], tensor, cc)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -295,7 +295,7 @@ static bool ggml_cuda_should_fuse_mul_mat_q(const ggml_tensor * up, const ggml_t
|
||||
const int64_t nchannels = is_id ? x_up->ne[2] : y->ne[2]*y->ne[3];
|
||||
const bool use_mmvq = is_id ? ncols <= get_mmvq_mmid_max_batch(x_up->type, cc) : ggml_cuda_should_use_mmvq(x_up->type, cc, ncols);
|
||||
// do not fuse the matmuls that few-row MMQ takes in ggml_cuda_mul_mat; for MUL_MAT_ID the fused MMQ keeps the expert gate and up
|
||||
if (!is_id && ggml_cuda_should_use_mmq_few(x_up, y, nullptr, cc)) {
|
||||
if (!is_id && ggml_cuda_should_use_mmq_few(x_up, y, nullptr, up, cc)) {
|
||||
return false;
|
||||
}
|
||||
if (use_mmvq || !ggml_cuda_should_use_mmq(x_up->type, cc, ncols, /*n_experts =*/ is_id ? x_up->ne[2] : 0)) {
|
||||
|
||||
@@ -2267,15 +2267,15 @@ static bool ggml_cuda_mul_mat(ggml_backend_cuda_context & ctx, const ggml_tensor
|
||||
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, cc);
|
||||
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);
|
||||
return fold;
|
||||
}
|
||||
if (use_mmq_few) {
|
||||
ggml_cuda_mul_mat_q_few(ctx, src0, src1, nullptr, dst);
|
||||
return false;
|
||||
ggml_cuda_mul_mat_q_few(ctx, src0, src1, nullptr, dst, scale_fold ? &fusion : nullptr);
|
||||
return scale_fold;
|
||||
}
|
||||
if (ggml_cuda_should_use_mmq(src0->type, cc, ne11, /*n_experts =*/ 0)) {
|
||||
ggml_cuda_mul_mat_q(ctx, src0, src1, nullptr, dst, scale_fold ? &fusion : nullptr);
|
||||
@@ -2313,7 +2313,7 @@ static bool ggml_cuda_mul_mat_id_needs_sync(const ggml_tensor * dst, const int c
|
||||
}
|
||||
}
|
||||
|
||||
if (ggml_cuda_should_use_mmq_few(src0, src1, dst->src[2], cc)) {
|
||||
if (ggml_cuda_should_use_mmq_few(src0, src1, dst->src[2], dst, cc)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -2355,7 +2355,7 @@ static bool ggml_cuda_mul_mat_id(ggml_backend_cuda_context & ctx, ggml_tensor *
|
||||
// [TAG_MUL_MAT_ID_CUDA_GRAPHS]
|
||||
if ((src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_BF16) && dst->type == GGML_TYPE_F32) {
|
||||
// up to 24 tokens few-row MMQ is faster than MMQ, on DGX Spark also faster than MMVQ
|
||||
const bool use_mmq_few = ggml_cuda_should_use_mmq_few(src0, src1, ids, cc);
|
||||
const bool use_mmq_few = ggml_cuda_should_use_mmq_few(src0, src1, ids, dst, cc);
|
||||
|
||||
static_assert(MMVQ_MAX_BATCH_SIZE == MMVF_MAX_BATCH_SIZE);
|
||||
if (!use_mmq_few && ne2 <= MMVQ_MAX_BATCH_SIZE) {
|
||||
|
||||
+256
-63
@@ -12,7 +12,7 @@
|
||||
|
||||
using namespace ggml_cuda_mma;
|
||||
|
||||
// Few-row MMQ: int8 MMA mat-mul for 2-24 src1 columns. A CUDA block has 16 src0 rows (32 for 2 column groups on large matrices).
|
||||
// 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).
|
||||
// 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).
|
||||
|
||||
@@ -36,6 +36,7 @@ static constexpr __host__ __device__ int mmq_few_unit_size(const ggml_type type)
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -46,8 +47,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.
|
||||
static constexpr __host__ __device__ int mmq_few_nunits_stage(const ggml_type type, const int ncg, const int nmt) {
|
||||
return type == GGML_TYPE_Q6_K || ncg > 2 || nmt > 1 ? 1 : 2;
|
||||
// fp4: NVFP4 with 4-bit src1, which has enough free registers for 2 units with 3 column groups.
|
||||
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;
|
||||
}
|
||||
|
||||
// A third ring stage only makes Q6_K faster, the other types get slower on large matrices.
|
||||
@@ -72,14 +74,19 @@ static constexpr __host__ __device__ int mmq_few_sram_stride(const ggml_type typ
|
||||
return mmq_few_unaligned(type, nunits_stage) && nchunks % 2 == 0 ? 16*(nchunks + 1) : 16*nchunks;
|
||||
}
|
||||
|
||||
static constexpr __host__ __device__ int mmq_few_nbytes_shared(const ggml_type type, const int ncg, const int nmt) {
|
||||
return MMQ_FEW_NWARPS*mmq_few_nstages(type)*nmt*MMQ_FEW_NROWS*mmq_few_sram_stride(type, mmq_few_nunits_stage(type, ncg, nmt));
|
||||
static constexpr __host__ __device__ int mmq_few_nbytes_shared(const ggml_type type, const int ncg, const int nmt, const bool fp4) {
|
||||
return MMQ_FEW_NWARPS*mmq_few_nstages(type)*nmt*MMQ_FEW_NROWS*mmq_few_sram_stride(type, mmq_few_nunits_stage(type, ncg, nmt, fp4));
|
||||
}
|
||||
|
||||
// With Q6_K and Q8_0 two CUDA blocks with 32-row tiles would not fit into an SM.
|
||||
static constexpr __host__ __device__ bool mmq_few_tall_supported(const ggml_type type) {
|
||||
return type == GGML_TYPE_Q3_K || type == GGML_TYPE_Q4_K || type == GGML_TYPE_Q5_K ||
|
||||
type == GGML_TYPE_IQ3_S || type == GGML_TYPE_IQ4_NL || type == GGML_TYPE_IQ4_XS;
|
||||
type == GGML_TYPE_IQ3_S || type == GGML_TYPE_IQ4_NL || type == GGML_TYPE_IQ4_XS || type == GGML_TYPE_NVFP4;
|
||||
}
|
||||
|
||||
// The MUL_MAT_ID src1 quantizer keeps the natural value order, the NVFP4 decoder needs the nvfp4 order (see quantize_mmq_few_q8_1).
|
||||
static constexpr __host__ __device__ bool mmq_few_id_supported(const ggml_type type) {
|
||||
return type != GGML_TYPE_NVFP4;
|
||||
}
|
||||
|
||||
// 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.
|
||||
@@ -106,6 +113,18 @@ static __device__ __forceinline__ void mmq_few_load_y(mmq_few_y_frag & yf, const
|
||||
}
|
||||
}
|
||||
|
||||
// The src1 fragments of a lane for NVFP4 with 4-bit src1, see MMQ_FEW_FP4_INT4: the B fragments of the 4 blocks of 64 values and the scales of column g.
|
||||
struct mmq_few_y_fp4 {
|
||||
int4 qs[2];
|
||||
int4 sc;
|
||||
};
|
||||
|
||||
static __device__ __forceinline__ void mmq_few_load_y(mmq_few_y_fp4 & yf, const int4 * y) {
|
||||
yf.qs[0] = y[threadIdx.x];
|
||||
yf.qs[1] = y[WARP_SIZE + threadIdx.x];
|
||||
yf.sc = y[2*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);
|
||||
@@ -169,12 +188,11 @@ static __device__ __forceinline__ void mmq_few_mma_add(
|
||||
}
|
||||
}
|
||||
|
||||
// Same with one k=16 MMA per half of sub-block s, the int8 scales of the halves are bytes 0 and 1 of sc[r].
|
||||
template <ggml_type type, int ncg>
|
||||
// Same with one k=16 MMA per half of sub-block s, sc_lo and sc_hi are the scales of the halves (int, or float for NVFP4).
|
||||
template <ggml_type type, int ncg, typename scale_t>
|
||||
static __device__ __forceinline__ void mmq_few_mma_k16_add(
|
||||
const int (&lo)[2], const int (&hi)[2], const int (&sc)[2], const mmq_few_y_frag (&yf)[ncg], const int s, float (&sum_d)[ncg][4]) {
|
||||
const int sc_lo[2] = {(int8_t) sc[0], (int8_t) sc[1]};
|
||||
const int sc_hi[2] = {(int8_t) (sc[0] >> 8), (int8_t) (sc[1] >> 8)};
|
||||
const int (&lo)[2], const int (&hi)[2], const scale_t (&sc_lo)[2], const scale_t (&sc_hi)[2], const mmq_few_y_frag (&yf)[ncg], const int s,
|
||||
float (&sum_d)[ncg][4]) {
|
||||
#pragma unroll
|
||||
for (int c = 0; c < ncg; ++c) {
|
||||
const int2 b = mmq_few_get_b(yf[c], s);
|
||||
@@ -189,6 +207,15 @@ static __device__ __forceinline__ void mmq_few_mma_k16_add(
|
||||
}
|
||||
}
|
||||
|
||||
// The int8 scales of the halves are bytes 0 and 1 of sc[r].
|
||||
template <ggml_type type, int ncg>
|
||||
static __device__ __forceinline__ void mmq_few_mma_k16_add(
|
||||
const int (&lo)[2], const int (&hi)[2], const int (&sc)[2], const mmq_few_y_frag (&yf)[ncg], const int s, float (&sum_d)[ncg][4]) {
|
||||
const int sc_lo[2] = {(int8_t) sc[0], (int8_t) sc[1]};
|
||||
const int sc_hi[2] = {(int8_t) (sc[0] >> 8), (int8_t) (sc[1] >> 8)};
|
||||
mmq_few_mma_k16_add<type>(lo, hi, sc_lo, sc_hi, yf, s, sum_d);
|
||||
}
|
||||
|
||||
template <int ncg>
|
||||
static __device__ __forceinline__ void mmq_few_add_scaled(float (&sum)[ncg][4], const float (&d)[2], const float (&sum_d)[ncg][4]) {
|
||||
#pragma unroll
|
||||
@@ -470,6 +497,90 @@ static __device__ __forceinline__ void mmq_few_vec_dot_iq3_s(const char * const
|
||||
mmq_few_add_scaled(sum, d, sum_d);
|
||||
}
|
||||
|
||||
// The doubled E2M1 values of the 4 nibbles in the low 16 bits of q as int8, like get_int_from_table_16.
|
||||
static __device__ __forceinline__ int mmq_few_e2m1_x4(const int q) {
|
||||
const uint32_t * table32 = (const uint32_t *) kvalues_fp4;
|
||||
const uint32_t low = __byte_perm(table32[0], table32[1], q);
|
||||
const uint32_t high = __byte_perm(table32[2], table32[3], q);
|
||||
return __byte_perm(low, high, 0x3210 | ((q & 0x8888) >> 1));
|
||||
}
|
||||
|
||||
// NVFP4 has a UE4M3 scale per 16 values: one k=16 MMA per half of a sub-block, the E2M1 values are converted to int8 with a table.
|
||||
// Byte i of 16 values holds the values i and i + 8. Lane t takes the bytes 2t and 2t + 1, so src1 has the values 2t, 2t + 8, 2t + 1, 2t + 9 in this order (nvfp4_order in quantize.cu).
|
||||
template <int ncg>
|
||||
static __device__ __forceinline__ void mmq_few_vec_dot_nvfp4(const char * const (&x)[2], const mmq_few_y_frag (&yf)[ncg], float (&sum)[ncg][4]) {
|
||||
const int t = threadIdx.x % 4;
|
||||
|
||||
#pragma unroll
|
||||
for (int ib = 0; ib < QK_K/QK_NVFP4; ++ib) {
|
||||
int q[2][4];
|
||||
float sc[2][4];
|
||||
#pragma unroll
|
||||
for (int r = 0; r < 2; ++r) {
|
||||
const block_nvfp4 * bx = (const block_nvfp4 *) x[r] + ib;
|
||||
#pragma unroll
|
||||
for (int j = 0; j < QK_NVFP4/QK_NVFP4_SUB; ++j) {
|
||||
q[r][j] = mmq_few_e2m1_x4(*(const uint16_t *) (bx->qs + 8*j + 2*t));
|
||||
}
|
||||
// The 4 UE4M3 scales, not halved like in ggml_cuda_ue4m3_to_fp32 (the write-back halves). As there, the NaN codes 0x7F and 0xFF give 0.
|
||||
#ifdef FP8_AVAILABLE
|
||||
// 2 scales per conversion
|
||||
uint32_t d4 = get_int_b4(bx->d, 0);
|
||||
const uint32_t nan = ((d4 & 0x7F7F7F7F) + 0x01010101) & 0x80808080; // 0x80 in the bytes 0x7F and 0xFF
|
||||
d4 &= ~(nan | (nan - (nan >> 7))); // these bytes to 0
|
||||
const float2 s01 = __half22float2(__half2(__nv_cvt_fp8x2_to_halfraw2((__nv_fp8x2_storage_t) (d4 & 0xFFFF), __NV_E4M3)));
|
||||
const float2 s23 = __half22float2(__half2(__nv_cvt_fp8x2_to_halfraw2((__nv_fp8x2_storage_t) (d4 >> 16), __NV_E4M3)));
|
||||
sc[r][0] = s01.x;
|
||||
sc[r][1] = s01.y;
|
||||
sc[r][2] = s23.x;
|
||||
sc[r][3] = s23.y;
|
||||
#else
|
||||
#pragma unroll
|
||||
for (int j = 0; j < QK_NVFP4/QK_NVFP4_SUB; ++j) {
|
||||
sc[r][j] = 2.0f*ggml_cuda_ue4m3_to_fp32(bx->d[j]);
|
||||
}
|
||||
#endif // FP8_AVAILABLE
|
||||
}
|
||||
#pragma unroll
|
||||
for (int h = 0; h < 2; ++h) {
|
||||
const int lo[2] = {q[0][2*h + 0], q[1][2*h + 0]};
|
||||
const int hi[2] = {q[0][2*h + 1], q[1][2*h + 1]};
|
||||
const float sc_lo[2] = {sc[0][2*h + 0], sc[1][2*h + 0]};
|
||||
const float sc_hi[2] = {sc[0][2*h + 1], sc[1][2*h + 1]};
|
||||
mmq_few_mma_k16_add<GGML_TYPE_NVFP4>(lo, hi, sc_lo, sc_hi, yf, 2*ib + h, sum);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// NVFP4 with 4-bit src1: one block scaled FP4 m16n8k64 MMA per block of 64 values, the 32 bit words of src0 and src1 are the MMA fragments.
|
||||
// Lanes with t % 2 == 0 give the scales of row g, lanes with t % 2 == 1 those of row g + 8, see mma_block_scaled_fp4.
|
||||
template <int ncg>
|
||||
static __device__ __forceinline__ void mmq_few_vec_dot_nvfp4_fp4(const char * const (&x)[2], const mmq_few_y_fp4 (&yf)[ncg], float (&sum)[ncg][4]) {
|
||||
const int t = threadIdx.x % 4;
|
||||
const char * x_sc = t % 2 == 0 ? x[0] : x[1]; // x[t % 2] would put x into local memory
|
||||
|
||||
#pragma unroll
|
||||
for (int ib = 0; ib < QK_K/QK_NVFP4; ++ib) {
|
||||
const block_nvfp4 * bx0 = (const block_nvfp4 *) x[0] + ib;
|
||||
const block_nvfp4 * bx1 = (const block_nvfp4 *) x[1] + ib;
|
||||
tile<16, 8, int> A;
|
||||
A.x[0] = get_int_b4(bx0->qs, t);
|
||||
A.x[1] = get_int_b4(bx1->qs, t);
|
||||
A.x[2] = get_int_b4(bx0->qs, t + 4);
|
||||
A.x[3] = get_int_b4(bx1->qs, t + 4);
|
||||
const uint32_t scale_A = get_int_b4(((const block_nvfp4 *) x_sc + ib)->d, 0);
|
||||
#pragma unroll
|
||||
for (int c = 0; c < ncg; ++c) {
|
||||
const int * qs = (const int *) &yf[c].qs[ib/2];
|
||||
tile<8, 8, int> B;
|
||||
B.x[0] = qs[2*(ib % 2) + 0];
|
||||
B.x[1] = qs[2*(ib % 2) + 1];
|
||||
const uint32_t scale_B = ((const int *) &yf[c].sc)[ib];
|
||||
mma_block_scaled_fp4<GGML_TYPE_NVFP4>(*(tile<16, 8, float> *) sum[c], A, B, scale_A, scale_B);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
@@ -484,6 +595,8 @@ static __device__ __forceinline__ void mmq_few_vec_dot(const char * const (&x)[2
|
||||
mmq_few_vec_dot_iq3_s<ncg>(x, yf, sum);
|
||||
} else if constexpr (type == GGML_TYPE_IQ4_XS) {
|
||||
mmq_few_vec_dot_iq4_xs<ncg>(x, yf, sum);
|
||||
} else if constexpr (type == GGML_TYPE_NVFP4) {
|
||||
mmq_few_vec_dot_nvfp4<ncg>(x, yf, sum);
|
||||
} else {
|
||||
static_assert(type == GGML_TYPE_COUNT, "unsupported type");
|
||||
}
|
||||
@@ -492,10 +605,11 @@ 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.
|
||||
template <ggml_type type, int ncg, int nmt, bool has_ids>
|
||||
// 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.
|
||||
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(
|
||||
const char * x, const int4 * y_ptr, float * dst,
|
||||
const char * x, const int4 * y_ptr, const float * y_scale, const float * x_scale, float * dst,
|
||||
const int nunits, const int nrows_x, const int ncols_dst, const int64_t stride_row_x, const int stride_col_dst,
|
||||
const int channel_ratio, const int64_t stride_channel_x, const int stride_channel_dst,
|
||||
const int sample_ratio, const int64_t stride_sample_x, const int stride_sample_dst, const bool x_is_constant, const int * pairs) {
|
||||
@@ -504,10 +618,14 @@ static __global__ void mul_mat_q_few(
|
||||
constexpr int warp_size = ggml_cuda_get_physical_warp_size();
|
||||
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");
|
||||
|
||||
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 int unit_size = mmq_few_unit_size(type);
|
||||
constexpr int nrows = nmt*MMQ_FEW_NROWS;
|
||||
constexpr int nunits_stage = mmq_few_nunits_stage(type, ncg, nmt);
|
||||
constexpr int nunits_stage = mmq_few_nunits_stage(type, ncg, nmt, fp4);
|
||||
constexpr int nstages = mmq_few_nstages(type);
|
||||
constexpr bool unaligned = mmq_few_unaligned(type, nunits_stage);
|
||||
constexpr int nchunks = mmq_few_nchunks(type, nunits_stage);
|
||||
@@ -543,7 +661,7 @@ static __global__ void mul_mat_q_few(
|
||||
x += int64_t(sample_dst/sample_ratio)*stride_sample_x + int64_t(channel_dst/channel_ratio)*stride_channel_x;
|
||||
dst += int64_t(sample_dst)*stride_sample_dst + int64_t(channel_dst)*stride_channel_dst;
|
||||
}
|
||||
const int4 * GGML_CUDA_RESTRICT y = y_ptr + (int64_t(sample_dst)*nchannels + channel_dst)*nunits*ncg*MMQ_FEW_Q8_1_INT4;
|
||||
const int4 * GGML_CUDA_RESTRICT y = y_ptr + (int64_t(sample_dst)*nchannels + channel_dst)*nunits*ncg*y_int4;
|
||||
|
||||
// No bounds check for the units, the gate makes nunits a multiple of nunits_stage.
|
||||
// The extra bytes of unaligned data are inside the allocation, which ggml aligns and pads to 128 bytes.
|
||||
@@ -613,15 +731,15 @@ static __global__ void mul_mat_q_few(
|
||||
ggml_cuda_pdl_sync();
|
||||
}
|
||||
|
||||
auto load_y = [&](mmq_few_y_frag (&yf)[ncg], const int iu) {
|
||||
auto load_y = [&](y_frag_t (&yf)[ncg], const int iu) {
|
||||
#pragma unroll
|
||||
for (int c = 0; c < ncg; ++c) {
|
||||
mmq_few_load_y(yf[c], y + (int64_t(iu)*ncg + c)*MMQ_FEW_Q8_1_INT4);
|
||||
mmq_few_load_y(yf[c], y + (int64_t(iu)*ncg + c)*y_int4);
|
||||
}
|
||||
};
|
||||
|
||||
// The next src1 fragments are loaded right after the MMAs that use the current ones, an earlier load is slower.
|
||||
mmq_few_y_frag yf[nunits_stage][ncg];
|
||||
y_frag_t yf[nunits_stage][ncg];
|
||||
if (iu_begin < nunits) {
|
||||
#pragma unroll
|
||||
for (int k = 0; k < nunits_stage; ++k) {
|
||||
@@ -653,7 +771,11 @@ static __global__ void mul_mat_q_few(
|
||||
xs + (MMQ_FEW_NROWS*m + threadIdx.x/4 + 0)*sram_stride + (off_row[2*m + 0] + off_stage) % 16 + k*unit_size,
|
||||
xs + (MMQ_FEW_NROWS*m + threadIdx.x/4 + 8)*sram_stride + (off_row[2*m + 1] + off_stage) % 16 + k*unit_size,
|
||||
};
|
||||
mmq_few_vec_dot<type, ncg>(xr, yf[k], sum[m]);
|
||||
if constexpr (fp4) {
|
||||
mmq_few_vec_dot_nvfp4_fp4<ncg>(xr, yf[k], sum[m]);
|
||||
} else {
|
||||
mmq_few_vec_dot<type, ncg>(xr, yf[k], sum[m]);
|
||||
}
|
||||
}
|
||||
if (has_next) {
|
||||
load_y(yf[k], iu0 + iu_step + k);
|
||||
@@ -692,6 +814,14 @@ static __global__ void mul_mat_q_few(
|
||||
}
|
||||
}
|
||||
|
||||
[[maybe_unused]] float scale_x = 1.0f;
|
||||
if constexpr (type == GGML_TYPE_NVFP4) {
|
||||
scale_x = (fp4 ? 1.0f : 0.5f)*(x_scale ? *x_scale : 1.0f); // 8-bit src1: halved here, as the decoder halves neither the UE4M3 scales nor the doubled E2M1 values
|
||||
if constexpr (fp4) {
|
||||
y_scale += (int64_t(sample_dst)*nchannels + channel_dst)*ncg*MMQ_FEW_NCOLS;
|
||||
}
|
||||
}
|
||||
|
||||
#pragma unroll
|
||||
for (int m = 0; m < nmt; ++m) {
|
||||
#pragma unroll
|
||||
@@ -704,6 +834,8 @@ static __global__ void mul_mat_q_few(
|
||||
if constexpr (has_ids) {
|
||||
const int e = pair[2 + j]; // token*n_expert_used + slot
|
||||
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 {
|
||||
dst[int64_t(j)*stride_col_dst + i] = sum[m][c][l];
|
||||
}
|
||||
@@ -712,7 +844,7 @@ static __global__ void mul_mat_q_few(
|
||||
}
|
||||
}
|
||||
#else
|
||||
GGML_UNUSED_VARS(x, y_ptr, dst, nunits, nrows_x, ncols_dst, stride_row_x, stride_col_dst,
|
||||
GGML_UNUSED_VARS(x, y_ptr, y_scale, x_scale, dst, nunits, nrows_x, ncols_dst, stride_row_x, stride_col_dst,
|
||||
channel_ratio, stride_channel_x, stride_channel_dst,
|
||||
sample_ratio, stride_sample_x, stride_sample_dst, x_is_constant, pairs);
|
||||
NO_DEVICE_CODE;
|
||||
@@ -1074,17 +1206,17 @@ static bool mmq_few_is_constant(const ggml_tensor * src0) {
|
||||
}
|
||||
|
||||
// MUL_MAT_ID: grid y is the pairs, the kernel takes the expert from the pair and does not use the channel and sample ratios.
|
||||
template <ggml_type type, int ncg, int nmt, bool has_ids = false>
|
||||
template <ggml_type type, int ncg, int nmt, bool has_ids = false, bool fp4 = false>
|
||||
static void launch_mul_mat_q_few(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
|
||||
const int4 * src1_q, cudaStream_t stream, const int * pairs = nullptr, const int npairs_max = 0) {
|
||||
const int4 * src1_q, const float * y_scale, const float * x_scale, cudaStream_t stream, const int * pairs = nullptr, const int npairs_max = 0) {
|
||||
GGML_TENSOR_BINARY_OP_LOCALS;
|
||||
|
||||
const int nunits = ne00 / QK_K;
|
||||
GGML_ASSERT(nunits % mmq_few_nunits_stage(type, ncg, nmt) == 0);
|
||||
GGML_ASSERT(nunits % mmq_few_nunits_stage(type, ncg, nmt, fp4) == 0);
|
||||
|
||||
const int warp_size = ggml_cuda_info().devices[ggml_cuda_get_device()].warp_size;
|
||||
const int nbytes_shared = mmq_few_nbytes_shared(type, ncg, nmt);
|
||||
CUDA_SET_SHARED_MEMORY_LIMIT((mul_mat_q_few<type, ncg, nmt, has_ids>), nbytes_shared);
|
||||
const int nbytes_shared = mmq_few_nbytes_shared(type, ncg, nmt, fp4);
|
||||
CUDA_SET_SHARED_MEMORY_LIMIT((mul_mat_q_few<type, ncg, nmt, has_ids, fp4>), nbytes_shared);
|
||||
|
||||
const bool x_is_constant = mmq_few_is_constant(src0);
|
||||
|
||||
@@ -1092,8 +1224,8 @@ static void launch_mul_mat_q_few(const ggml_tensor * src0, const ggml_tensor * s
|
||||
const dim3 block_nums((ne01 + nrows_blk - 1) / nrows_blk, has_ids ? npairs_max : ne12, ne13);
|
||||
const dim3 block_dims(warp_size, MMQ_FEW_NWARPS, 1);
|
||||
const ggml_cuda_kernel_launch_params params(block_nums, block_dims, nbytes_shared, stream);
|
||||
ggml_cuda_kernel_launch(mul_mat_q_few<type, ncg, nmt, has_ids>, params,
|
||||
(const char *) src0->data, src1_q, (float *) dst->data,
|
||||
ggml_cuda_kernel_launch(mul_mat_q_few<type, ncg, nmt, has_ids, fp4>, params,
|
||||
(const char *) src0->data, src1_q, y_scale, x_scale, (float *) dst->data,
|
||||
nunits, (int) ne01, (int) ne1, (int64_t) nb01, (int) (nb1/sizeof(float)),
|
||||
(int) (ne12/ne02), (int64_t) nb02, (int) (nb2/sizeof(float)),
|
||||
(int) (ne13/ne03), (int64_t) nb03, (int) (nb3/sizeof(float)), x_is_constant, pairs);
|
||||
@@ -1104,39 +1236,44 @@ static int mmq_few_ncg(const int64_t ncols) {
|
||||
}
|
||||
|
||||
// With 2 column groups, 32-row tiles halve the src1 loads per src0 byte. With 3 column groups they spill registers.
|
||||
static int mmq_few_nmt(const ggml_type type, const int ncg, const int64_t nrows_x) {
|
||||
// With 4-bit src1 (fp4) there are few src1 loads per unit and the 32-row tiles are slower.
|
||||
static int mmq_few_nmt(const ggml_type type, const int ncg, const int64_t nrows_x, const bool fp4) {
|
||||
const int nsm = ggml_cuda_info().devices[ggml_cuda_get_device()].nsm;
|
||||
return mmq_few_tall_supported(type) && ncg == 2 && nrows_x >= int64_t(nsm)*2*MMQ_FEW_NROWS ? 2 : 1;
|
||||
return mmq_few_tall_supported(type) && !fp4 && ncg == 2 && nrows_x >= int64_t(nsm)*2*MMQ_FEW_NROWS ? 2 : 1;
|
||||
}
|
||||
|
||||
template <ggml_type type>
|
||||
template <ggml_type type, bool fp4>
|
||||
static void launch_mul_mat_q_few_tall(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
|
||||
const int4 * src1_q, cudaStream_t stream) {
|
||||
if constexpr (mmq_few_tall_supported(type)) {
|
||||
if (mmq_few_nmt(type, 2, src0->ne[1]) == 2) {
|
||||
launch_mul_mat_q_few<type, 2, 2>(src0, src1, dst, src1_q, stream);
|
||||
const int4 * src1_q, const float * y_scale, const float * x_scale, cudaStream_t stream) {
|
||||
if constexpr (mmq_few_tall_supported(type) && !fp4) {
|
||||
if (mmq_few_nmt(type, 2, src0->ne[1], fp4) == 2) {
|
||||
launch_mul_mat_q_few<type, 2, 2, false, fp4>(src0, src1, dst, src1_q, y_scale, x_scale, stream);
|
||||
return;
|
||||
}
|
||||
}
|
||||
launch_mul_mat_q_few<type, 2, 1>(src0, src1, dst, src1_q, stream);
|
||||
launch_mul_mat_q_few<type, 2, 1, false, fp4>(src0, src1, dst, src1_q, y_scale, x_scale, stream);
|
||||
}
|
||||
|
||||
template <ggml_type type>
|
||||
template <ggml_type type, bool fp4 = false>
|
||||
static void launch_mul_mat_q_few_switch_ncg(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
|
||||
const int4 * src1_q, const int * pairs, const int npairs_max, const int ncg, cudaStream_t stream) {
|
||||
const int4 * src1_q, const float * y_scale, const float * x_scale, const int * pairs, const int npairs_max, const int ncg, cudaStream_t stream) {
|
||||
if (pairs) {
|
||||
launch_mul_mat_q_few<type, 1, 1, true>(src0, src1, dst, src1_q, stream, pairs, npairs_max);
|
||||
if constexpr (mmq_few_id_supported(type)) {
|
||||
launch_mul_mat_q_few<type, 1, 1, true>(src0, src1, dst, src1_q, y_scale, x_scale, stream, pairs, npairs_max);
|
||||
} else {
|
||||
GGML_ABORT("fatal error"); // see ggml_cuda_mmq_few_min_ncols
|
||||
}
|
||||
return;
|
||||
}
|
||||
switch (ncg) {
|
||||
case 1:
|
||||
launch_mul_mat_q_few<type, 1, 1>(src0, src1, dst, src1_q, stream);
|
||||
launch_mul_mat_q_few<type, 1, 1, false, fp4>(src0, src1, dst, src1_q, y_scale, x_scale, stream);
|
||||
break;
|
||||
case 2:
|
||||
launch_mul_mat_q_few_tall<type>(src0, src1, dst, src1_q, stream);
|
||||
launch_mul_mat_q_few_tall<type, fp4>(src0, src1, dst, src1_q, y_scale, x_scale, stream);
|
||||
break;
|
||||
case 3:
|
||||
launch_mul_mat_q_few<type, 3, 1>(src0, src1, dst, src1_q, stream);
|
||||
launch_mul_mat_q_few<type, 3, 1, false, fp4>(src0, src1, dst, src1_q, y_scale, x_scale, stream);
|
||||
break;
|
||||
default:
|
||||
GGML_ABORT("fatal error");
|
||||
@@ -1235,7 +1372,18 @@ static void ggml_cuda_mul_mat_q_few_wmma(ggml_backend_cuda_context & ctx, const
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
// NVFP4: whether src1 is 4-bit (the native FP4 MMA) or 8-bit.
|
||||
// 8-bit unless 4-bit is requested (GGML_CUDA_MMQ_PREC=q4 or by the graph): few-row batches are decode batches (one or more sequences,
|
||||
// draft tokens), whose precision should not depend on how many tokens are in the batch. 4-bit src1 changes the results much more.
|
||||
static bool mmq_few_use_fp4(const ggml_type type, const ggml_prec prec_requested, const int cc) {
|
||||
if (type != GGML_TYPE_NVFP4) {
|
||||
return false;
|
||||
}
|
||||
return ggml_cuda_mmq_get_prec_src1(type, prec_requested == GGML_PREC_UNDEFINED ? GGML_PREC_Q8 : prec_requested, cc) == GGML_PREC_Q4;
|
||||
}
|
||||
|
||||
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) {
|
||||
GGML_TENSOR_BINARY_OP_LOCALS;
|
||||
|
||||
GGML_ASSERT(src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_BF16);
|
||||
@@ -1246,15 +1394,36 @@ void ggml_cuda_mul_mat_q_few(ggml_backend_cuda_context & ctx, const ggml_tensor
|
||||
GGML_ASSERT(ne00 % QK_K == 0);
|
||||
|
||||
if (mmq_few_use_wmma(ggml_cuda_info().devices[ctx.device].cc)) {
|
||||
GGML_ASSERT(!ids);
|
||||
GGML_ASSERT(!ids && !fusion);
|
||||
ggml_cuda_mul_mat_q_few_wmma(ctx, src0, src1, dst);
|
||||
return;
|
||||
}
|
||||
|
||||
// fusion: only the scale of an NVFP4 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(!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;
|
||||
|
||||
cudaStream_t stream = ctx.stream();
|
||||
|
||||
const int nunits = ne00 / QK_K;
|
||||
const int ncg = ids ? 1 : mmq_few_ncg(ne11);
|
||||
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 (fp4) {
|
||||
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_FP4_INT4);
|
||||
ggml_cuda_pool_alloc<float> y_scale(ctx.pool(), int64_t(ne12)*ne13*ncg*MMQ_FEW_NCOLS);
|
||||
quantize_mmq_few_nvfp4_cuda(src1->data, src1->type, src1_q.get(), y_scale.get(), ne10, nb11/nb10, nb12/nb10, nb13/nb10,
|
||||
ne11, ne12, ne13, stream);
|
||||
launch_mul_mat_q_few_switch_ncg<GGML_TYPE_NVFP4, true>(src0, src1, dst, src1_q.get(), y_scale.get(), x_scale_d, nullptr, 0, ncg, stream);
|
||||
return;
|
||||
}
|
||||
|
||||
ggml_cuda_pool_alloc<int> pairs(ctx.pool());
|
||||
ggml_cuda_pool_alloc<int4> src1_q(ctx.pool());
|
||||
@@ -1280,34 +1449,37 @@ void ggml_cuda_mul_mat_q_few(ggml_backend_cuda_context & ctx, const ggml_tensor
|
||||
GGML_ASSERT(ne12 % ne02 == 0 && ne13 % ne03 == 0);
|
||||
|
||||
src1_q.alloc(int64_t(ne12)*ne13*nunits*ncg*MMQ_FEW_Q8_1_INT4);
|
||||
quantize_mmq_few_q8_1_cuda(src1->data, src1->type, src1_q.get(), mmq_few_ds_layout(src0->type), ne10, nb11/nb10, nb12/nb10, nb13/nb10,
|
||||
ne11, ne12, ne13, stream);
|
||||
quantize_mmq_few_q8_1_cuda(src1->data, src1->type, src1_q.get(), mmq_few_ds_layout(src0->type), src0->type == GGML_TYPE_NVFP4,
|
||||
ne10, nb11/nb10, nb12/nb10, nb13/nb10, ne11, ne12, ne13, stream);
|
||||
}
|
||||
|
||||
switch (src0->type) {
|
||||
case GGML_TYPE_Q8_0:
|
||||
launch_mul_mat_q_few_switch_ncg<GGML_TYPE_Q8_0>(src0, src1, dst, src1_q.get(), pairs.get(), npairs_max, ncg, stream);
|
||||
launch_mul_mat_q_few_switch_ncg<GGML_TYPE_Q8_0>(src0, src1, dst, src1_q.get(), nullptr, x_scale_d, pairs.get(), npairs_max, ncg, stream);
|
||||
break;
|
||||
case GGML_TYPE_Q3_K:
|
||||
launch_mul_mat_q_few_switch_ncg<GGML_TYPE_Q3_K>(src0, src1, dst, src1_q.get(), pairs.get(), npairs_max, ncg, stream);
|
||||
launch_mul_mat_q_few_switch_ncg<GGML_TYPE_Q3_K>(src0, src1, dst, src1_q.get(), nullptr, x_scale_d, pairs.get(), npairs_max, ncg, stream);
|
||||
break;
|
||||
case GGML_TYPE_Q4_K:
|
||||
launch_mul_mat_q_few_switch_ncg<GGML_TYPE_Q4_K>(src0, src1, dst, src1_q.get(), pairs.get(), npairs_max, ncg, stream);
|
||||
launch_mul_mat_q_few_switch_ncg<GGML_TYPE_Q4_K>(src0, src1, dst, src1_q.get(), nullptr, x_scale_d, pairs.get(), npairs_max, ncg, stream);
|
||||
break;
|
||||
case GGML_TYPE_Q5_K:
|
||||
launch_mul_mat_q_few_switch_ncg<GGML_TYPE_Q5_K>(src0, src1, dst, src1_q.get(), pairs.get(), npairs_max, ncg, stream);
|
||||
launch_mul_mat_q_few_switch_ncg<GGML_TYPE_Q5_K>(src0, src1, dst, src1_q.get(), nullptr, x_scale_d, pairs.get(), npairs_max, ncg, stream);
|
||||
break;
|
||||
case GGML_TYPE_Q6_K:
|
||||
launch_mul_mat_q_few_switch_ncg<GGML_TYPE_Q6_K>(src0, src1, dst, src1_q.get(), pairs.get(), npairs_max, ncg, stream);
|
||||
launch_mul_mat_q_few_switch_ncg<GGML_TYPE_Q6_K>(src0, src1, dst, src1_q.get(), nullptr, x_scale_d, pairs.get(), npairs_max, ncg, stream);
|
||||
break;
|
||||
case GGML_TYPE_IQ3_S:
|
||||
launch_mul_mat_q_few_switch_ncg<GGML_TYPE_IQ3_S>(src0, src1, dst, src1_q.get(), pairs.get(), npairs_max, ncg, stream);
|
||||
launch_mul_mat_q_few_switch_ncg<GGML_TYPE_IQ3_S>(src0, src1, dst, src1_q.get(), nullptr, x_scale_d, pairs.get(), npairs_max, ncg, stream);
|
||||
break;
|
||||
case GGML_TYPE_IQ4_NL:
|
||||
launch_mul_mat_q_few_switch_ncg<GGML_TYPE_IQ4_NL>(src0, src1, dst, src1_q.get(), pairs.get(), npairs_max, ncg, stream);
|
||||
launch_mul_mat_q_few_switch_ncg<GGML_TYPE_IQ4_NL>(src0, src1, dst, src1_q.get(), nullptr, x_scale_d, pairs.get(), npairs_max, ncg, stream);
|
||||
break;
|
||||
case GGML_TYPE_IQ4_XS:
|
||||
launch_mul_mat_q_few_switch_ncg<GGML_TYPE_IQ4_XS>(src0, src1, dst, src1_q.get(), pairs.get(), npairs_max, ncg, stream);
|
||||
launch_mul_mat_q_few_switch_ncg<GGML_TYPE_IQ4_XS>(src0, src1, dst, src1_q.get(), nullptr, x_scale_d, pairs.get(), npairs_max, ncg, stream);
|
||||
break;
|
||||
case GGML_TYPE_NVFP4:
|
||||
launch_mul_mat_q_few_switch_ncg<GGML_TYPE_NVFP4>(src0, src1, dst, src1_q.get(), nullptr, x_scale_d, pairs.get(), npairs_max, ncg, stream);
|
||||
break;
|
||||
default:
|
||||
GGML_ABORT("unsupported type for few-row MMQ");
|
||||
@@ -1315,7 +1487,8 @@ void ggml_cuda_mul_mat_q_few(ggml_backend_cuda_context & ctx, const ggml_tensor
|
||||
}
|
||||
|
||||
// Min. number of src1 columns (MUL_MAT_ID: tokens) for the few-row MMQ, 0 if it is not used for this type, GPU and row length.
|
||||
static int ggml_cuda_mmq_few_min_ncols(const ggml_type type, const int cc, const int64_t ne00, const bool has_ids) {
|
||||
// fp4: NVFP4 with 4-bit src1 (only when requested, see mmq_few_use_fp4).
|
||||
static int ggml_cuda_mmq_few_min_ncols(const ggml_type type, const int cc, const int64_t ne00, const bool has_ids, const bool fp4) {
|
||||
#ifdef GGML_CUDA_FORCE_CUBLAS
|
||||
return 0;
|
||||
#endif // GGML_CUDA_FORCE_CUBLAS
|
||||
@@ -1336,6 +1509,19 @@ static int ggml_cuda_mmq_few_min_ncols(const ggml_type type, const int cc, const
|
||||
if (mmq_few_unit_size(type) == 0 || (!has_ids && ne00 < 8*QK_K)) {
|
||||
return 0;
|
||||
}
|
||||
if (has_ids && !mmq_few_id_supported(type)) {
|
||||
return 0;
|
||||
}
|
||||
// NVFP4: only on Blackwell (cc 12.x), tuned on RTX 5090 and DGX Spark
|
||||
if (type == GGML_TYPE_NVFP4) {
|
||||
if (!blackwell_mma_available(cc)) {
|
||||
return 0;
|
||||
}
|
||||
if (cc == GGML_CUDA_CC_GB10) {
|
||||
return fp4 ? 8 : 9; // 8-bit src1 only gains from 9 columns
|
||||
}
|
||||
return fp4 ? 6 : 7;
|
||||
}
|
||||
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;
|
||||
}
|
||||
@@ -1355,9 +1541,15 @@ 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 int cc) {
|
||||
const int64_t ncols = ids ? ids->ne[1] : src1->ne[1];
|
||||
const int min_ncols = ggml_cuda_mmq_few_min_ncols(src0->type, cc, src0->ne[0], ids != nullptr);
|
||||
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) {
|
||||
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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
@@ -1375,8 +1567,8 @@ bool ggml_cuda_should_use_mmq_few(const ggml_tensor * src0, const ggml_tensor *
|
||||
}
|
||||
// the row must hold a whole number of ring stages of the launched kernel
|
||||
const int ncg = ids ? 1 : mmq_few_ncg(ncols);
|
||||
const int nmt = ids ? 1 : mmq_few_nmt(src0->type, ncg, src0->ne[1]);
|
||||
const int nunits_stage = mmq_few_nunits_stage(src0->type, ncg, nmt);
|
||||
const int nmt = ids ? 1 : mmq_few_nmt(src0->type, ncg, src0->ne[1], fp4);
|
||||
const int nunits_stage = mmq_few_nunits_stage(src0->type, ncg, nmt, fp4);
|
||||
if (src0->ne[0] % (nunits_stage*QK_K) != 0) {
|
||||
return false;
|
||||
}
|
||||
@@ -1387,13 +1579,14 @@ bool ggml_cuda_should_use_mmq_few(const ggml_tensor * src0, const ggml_tensor *
|
||||
|
||||
#else // !defined(GGML_USE_MUSA)
|
||||
|
||||
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) {
|
||||
GGML_UNUSED_VARS(ctx, src0, src1, ids, dst);
|
||||
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) {
|
||||
GGML_UNUSED_VARS(ctx, src0, src1, ids, dst, fusion);
|
||||
GGML_ABORT("few-row MMQ is not supported on MUSA");
|
||||
}
|
||||
|
||||
bool ggml_cuda_should_use_mmq_few(const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * ids, const int cc) {
|
||||
GGML_UNUSED_VARS(src0, src1, ids, cc);
|
||||
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) {
|
||||
GGML_UNUSED_VARS(src0, src1, ids, dst, cc);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,9 @@
|
||||
|
||||
#include "common.cuh"
|
||||
|
||||
// ids != nullptr: MUL_MAT_ID
|
||||
bool ggml_cuda_should_use_mmq_few(const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * ids, int cc);
|
||||
// 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);
|
||||
|
||||
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);
|
||||
// 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.
|
||||
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);
|
||||
|
||||
@@ -112,18 +112,24 @@ static ggml_prec ggml_cuda_mmq_get_prec_env() {
|
||||
return GGML_PREC_UNDEFINED;
|
||||
}
|
||||
|
||||
// GGML_CUDA_MMQ_PREC if set, else the src1 precision requested by the graph
|
||||
static ggml_prec ggml_cuda_mmq_get_prec(const ggml_prec prec_requested) {
|
||||
static const ggml_prec prec_env = ggml_cuda_mmq_get_prec_env();
|
||||
return prec_env != GGML_PREC_UNDEFINED ? prec_env : prec_requested;
|
||||
}
|
||||
|
||||
bool ggml_cuda_mmq_prec_src1_supported(const ggml_prec prec_requested) {
|
||||
const ggml_prec prec = ggml_cuda_mmq_get_prec(prec_requested);
|
||||
return prec == GGML_PREC_UNDEFINED || prec == GGML_PREC_Q8 || prec == GGML_PREC_Q4;
|
||||
}
|
||||
|
||||
// src1 is quantized to Q8_1 unless the FP4 types can use 4-bit activations, in which case they
|
||||
// default to the native W4A4 instructions on Blackwell.
|
||||
static ggml_prec ggml_cuda_mmq_get_prec_src1(const ggml_type type, const ggml_prec prec_requested, const int cc) {
|
||||
static const ggml_prec prec_env = ggml_cuda_mmq_get_prec_env();
|
||||
|
||||
ggml_prec prec = prec_env;
|
||||
if (prec == GGML_PREC_UNDEFINED) {
|
||||
prec = prec_requested;
|
||||
}
|
||||
ggml_prec ggml_cuda_mmq_get_prec_src1(const ggml_type type, const ggml_prec prec_requested, const int cc) {
|
||||
GGML_ASSERT(ggml_cuda_mmq_prec_src1_supported(prec_requested));
|
||||
const ggml_prec prec = ggml_cuda_mmq_get_prec(prec_requested);
|
||||
|
||||
// Q4 only for the FP4 types on Blackwell
|
||||
GGML_ASSERT(prec == GGML_PREC_UNDEFINED || prec == GGML_PREC_Q8 || prec == GGML_PREC_Q4);
|
||||
const bool can_use_q4 = (type == GGML_TYPE_NVFP4 || type == GGML_TYPE_MXFP4) && blackwell_mma_available(cc);
|
||||
if (prec == GGML_PREC_Q8 || !can_use_q4) {
|
||||
return GGML_PREC_Q8;
|
||||
|
||||
@@ -1727,5 +1727,12 @@ void ggml_cuda_mul_mat_q(
|
||||
|
||||
bool ggml_cuda_should_use_mmq(enum ggml_type type, int cc, int64_t ne11, int64_t n_experts);
|
||||
|
||||
// The src1 precision of MMQ: GGML_PREC_Q4 for MXFP4 and NVFP4 on Blackwell unless the precision is GGML_PREC_Q8, else GGML_PREC_Q8.
|
||||
// GGML_CUDA_MMQ_PREC, if set, replaces prec_requested. prec_requested must pass ggml_cuda_mmq_prec_src1_supported (asserted).
|
||||
ggml_prec ggml_cuda_mmq_get_prec_src1(ggml_type type, ggml_prec prec_requested, int cc);
|
||||
|
||||
// Whether MMQ takes this src1 precision request: UNDEFINED, Q8 or Q4 after the GGML_CUDA_MMQ_PREC override.
|
||||
bool ggml_cuda_mmq_prec_src1_supported(ggml_prec prec_requested);
|
||||
|
||||
// prec_requested: the src1 precision requested by the fused MUL_MATs.
|
||||
bool ggml_cuda_mmq_fusion_is_efficient(ggml_type type, ggml_prec prec_requested, int64_t nrows_x, int64_t ncols, int64_t nchannels, int device);
|
||||
|
||||
+274
-117
@@ -16,6 +16,18 @@ static __device__ __forceinline__ float4 quantize_load4(const src_t * x, const i
|
||||
}
|
||||
}
|
||||
|
||||
// 2 consecutive values of x as float2, x is aligned to 2 values
|
||||
template <typename src_t>
|
||||
static __device__ __forceinline__ float2 quantize_load2(const src_t * x) {
|
||||
if constexpr (std::is_same_v<src_t, nv_bfloat16>) {
|
||||
__align__(4) nv_bfloat162 v;
|
||||
ggml_cuda_memcpy_1<sizeof(v)>(&v, x);
|
||||
return ggml_cuda_cast<float2>(v);
|
||||
} else {
|
||||
return *(const float2 *) x;
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(BLACKWELL_MMA_AVAILABLE)
|
||||
// this maps to 256-bit loads in PTX on supported devices,
|
||||
// and otherwise falls back to 2 128-bit loads
|
||||
@@ -42,10 +54,14 @@ static __device__ __forceinline__ decltype(auto) quantize_load8(const src_t * x)
|
||||
}
|
||||
|
||||
#if CUDART_VERSION >= 12080
|
||||
static __device__ __forceinline__ float nvfp4_native_scale_error(
|
||||
const float vals[QK_NVFP4_SUB], const float inv_col_scale, const float inv_scale, const float scale) {
|
||||
const float scale_dequant = 2.0f * scale;
|
||||
float err = 0.0f;
|
||||
// The squared errors of the 16 values (times inv_col_scale) for each of the n scales. The n sums do not depend on each other, so they run in parallel.
|
||||
template <int n>
|
||||
static __device__ __forceinline__ void nvfp4_native_scale_error(
|
||||
const float vals[QK_NVFP4_SUB], const float inv_col_scale, const float (&inv_scale)[n], const float (&scale)[n], float (&err)[n]) {
|
||||
#pragma unroll
|
||||
for (int c = 0; c < n; ++c) {
|
||||
err[c] = 0.0f;
|
||||
}
|
||||
|
||||
#pragma unroll
|
||||
for (int k = 0; k < QK_NVFP4_SUB; k += 4) {
|
||||
@@ -54,30 +70,33 @@ static __device__ __forceinline__ float nvfp4_native_scale_error(
|
||||
const float v2 = vals[k + 2] * inv_col_scale;
|
||||
const float v3 = vals[k + 3] * inv_col_scale;
|
||||
|
||||
const __nv_fp4x4_e2m1 q(make_float4(v0 * inv_scale, v1 * inv_scale, v2 * inv_scale, v3 * inv_scale));
|
||||
const __nv_fp4x4_storage_t q_storage = q.__x;
|
||||
const __nv_fp4x2_storage_t q_lo = static_cast<__nv_fp4x2_storage_t>(q_storage);
|
||||
const __nv_fp4x2_storage_t q_hi = static_cast<__nv_fp4x2_storage_t>(q_storage >> 8U);
|
||||
#pragma unroll
|
||||
for (int c = 0; c < n; ++c) {
|
||||
const float scale_dequant = 2.0f * scale[c];
|
||||
|
||||
const __half2_raw hraw2_lo = __nv_cvt_fp4x2_to_halfraw2(q_lo, __NV_E2M1);
|
||||
const __half2_raw hraw2_hi = __nv_cvt_fp4x2_to_halfraw2(q_hi, __NV_E2M1);
|
||||
const __half2 h2_lo = static_cast<__half2>(hraw2_lo);
|
||||
const __half2 h2_hi = static_cast<__half2>(hraw2_hi);
|
||||
const float2 dq_lo = __half22float2(h2_lo);
|
||||
const float2 dq_hi = __half22float2(h2_hi);
|
||||
const __nv_fp4x4_e2m1 q(make_float4(v0 * inv_scale[c], v1 * inv_scale[c], v2 * inv_scale[c], v3 * inv_scale[c]));
|
||||
const __nv_fp4x4_storage_t q_storage = q.__x;
|
||||
const __nv_fp4x2_storage_t q_lo = static_cast<__nv_fp4x2_storage_t>(q_storage);
|
||||
const __nv_fp4x2_storage_t q_hi = static_cast<__nv_fp4x2_storage_t>(q_storage >> 8U);
|
||||
|
||||
const float err0 = fabsf(v0) - fabsf(dq_lo.x) * scale_dequant;
|
||||
const float err1 = fabsf(v1) - fabsf(dq_lo.y) * scale_dequant;
|
||||
const float err2 = fabsf(v2) - fabsf(dq_hi.x) * scale_dequant;
|
||||
const float err3 = fabsf(v3) - fabsf(dq_hi.y) * scale_dequant;
|
||||
const __half2_raw hraw2_lo = __nv_cvt_fp4x2_to_halfraw2(q_lo, __NV_E2M1);
|
||||
const __half2_raw hraw2_hi = __nv_cvt_fp4x2_to_halfraw2(q_hi, __NV_E2M1);
|
||||
const __half2 h2_lo = static_cast<__half2>(hraw2_lo);
|
||||
const __half2 h2_hi = static_cast<__half2>(hraw2_hi);
|
||||
const float2 dq_lo = __half22float2(h2_lo);
|
||||
const float2 dq_hi = __half22float2(h2_hi);
|
||||
|
||||
err = fmaf(err0, err0, err);
|
||||
err = fmaf(err1, err1, err);
|
||||
err = fmaf(err2, err2, err);
|
||||
err = fmaf(err3, err3, err);
|
||||
const float err0 = fabsf(v0) - fabsf(dq_lo.x) * scale_dequant;
|
||||
const float err1 = fabsf(v1) - fabsf(dq_lo.y) * scale_dequant;
|
||||
const float err2 = fabsf(v2) - fabsf(dq_hi.x) * scale_dequant;
|
||||
const float err3 = fabsf(v3) - fabsf(dq_hi.y) * scale_dequant;
|
||||
|
||||
err[c] = fmaf(err0, err0, err[c]);
|
||||
err[c] = fmaf(err1, err1, err[c]);
|
||||
err[c] = fmaf(err2, err2, err[c]);
|
||||
err[c] = fmaf(err3, err3, err[c]);
|
||||
}
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
#endif // CUDART_VERSION >= 12080
|
||||
#endif // defined(BLACKWELL_MMA_AVAILABLE)
|
||||
@@ -156,6 +175,119 @@ __device__ __forceinline__ uint8_t compute_e8m0_scale(float amax) {
|
||||
return static_cast<uint8_t>(biased);
|
||||
}
|
||||
|
||||
#if defined(BLACKWELL_MMA_AVAILABLE)
|
||||
// Quantizes 16 values divided by col_scale to E2M1 with the UE4M3 scale of the lowest squared error of 5 candidates, returns the scale code.
|
||||
// q0 and q1 get the 8 bytes of block_nvfp4: byte i holds the values i and i + 8 in its low and high nibble.
|
||||
static __device__ __forceinline__ uint8_t quantize_nvfp4_subblock(
|
||||
const float (&vals)[QK_NVFP4_SUB], const float col_scale, uint32_t & q0, uint32_t & q1) {
|
||||
const float inv_col_scale = col_scale > 0.0f ? 1.0f / col_scale : 0.0f;
|
||||
q0 = 0;
|
||||
q1 = 0;
|
||||
|
||||
float amax_sub = 0.0f;
|
||||
#pragma unroll
|
||||
for (int k = 0; k < QK_NVFP4_SUB; ++k) {
|
||||
amax_sub = fmaxf(amax_sub, fabsf(vals[k] * inv_col_scale));
|
||||
}
|
||||
|
||||
static constexpr int test_offsets[5] = { 0, -1, 1, -2, 2 };
|
||||
const int first_fp8_code = (int) ggml_cuda_fp32_to_ue4m3(amax_sub / 6.0f);
|
||||
|
||||
uint8_t fp8_code = (uint8_t) first_fp8_code;
|
||||
float subblock_scale = ggml_cuda_ue4m3_to_fp32(fp8_code);
|
||||
#if CUDART_VERSION >= 12080
|
||||
// the errors of all candidates at once, then compared in order; codes out of range are skipped
|
||||
bool valid[5];
|
||||
float test_scale[5];
|
||||
float test_inv_scale[5];
|
||||
#pragma unroll
|
||||
for (int i = 0; i < 5; ++i) {
|
||||
const int test_code = first_fp8_code + test_offsets[i];
|
||||
valid[i] = i == 0 || (test_code >= 0 && test_code <= 0x7e);
|
||||
test_scale[i] = ggml_cuda_ue4m3_to_fp32((uint8_t) (valid[i] ? test_code : first_fp8_code));
|
||||
test_inv_scale[i] = test_scale[i] > 0.0f ? 0.5f / test_scale[i] : 0.0f;
|
||||
}
|
||||
float err[5];
|
||||
nvfp4_native_scale_error(vals, inv_col_scale, test_inv_scale, test_scale, err);
|
||||
|
||||
float best_err = err[0];
|
||||
#pragma unroll
|
||||
for (int i = 1; i < 5; ++i) {
|
||||
if (valid[i] && err[i] < best_err) {
|
||||
best_err = err[i];
|
||||
fp8_code = (uint8_t) (first_fp8_code + test_offsets[i]);
|
||||
subblock_scale = test_scale[i];
|
||||
}
|
||||
}
|
||||
#else
|
||||
float inv_scale_err = subblock_scale > 0.0f ? 0.5f / subblock_scale : 0.0f;
|
||||
float best_err = 0.0f;
|
||||
#pragma unroll
|
||||
for (int k = 0; k < QK_NVFP4_SUB; ++k) {
|
||||
const float v = vals[k] * inv_col_scale;
|
||||
const uint8_t q = ggml_cuda_float_to_fp4_e2m1(v, inv_scale_err);
|
||||
const float err_diff = fabsf(v) - fabsf(kvalues_fp4[q & 0x7]) * subblock_scale;
|
||||
best_err = fmaf(err_diff, err_diff, best_err);
|
||||
}
|
||||
|
||||
#pragma unroll
|
||||
for (int i = 1; i < 5; ++i) {
|
||||
const int test_code = first_fp8_code + test_offsets[i];
|
||||
if (test_code < 0 || test_code > 0x7e) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const float test_scale = ggml_cuda_ue4m3_to_fp32((uint8_t) test_code);
|
||||
const float test_inv_scale = test_scale > 0.0f ? 0.5f / test_scale : 0.0f;
|
||||
float cur_err = 0.0f;
|
||||
#pragma unroll
|
||||
for (int k = 0; k < QK_NVFP4_SUB; ++k) {
|
||||
const float v = vals[k] * inv_col_scale;
|
||||
const uint8_t q = ggml_cuda_float_to_fp4_e2m1(v, test_inv_scale);
|
||||
const float err_diff = fabsf(v) - fabsf(kvalues_fp4[q & 0x7]) * test_scale;
|
||||
cur_err = fmaf(err_diff, err_diff, cur_err);
|
||||
}
|
||||
|
||||
if (cur_err < best_err) {
|
||||
best_err = cur_err;
|
||||
fp8_code = (uint8_t) test_code;
|
||||
subblock_scale = test_scale;
|
||||
}
|
||||
}
|
||||
#endif // CUDART_VERSION >= 12080
|
||||
#if CUDART_VERSION >= 12080
|
||||
const float inv_scale = subblock_scale > 0.0f ? 0.5f / subblock_scale : 0.0f;
|
||||
const float s = inv_col_scale * inv_scale;
|
||||
|
||||
__nv_fp4x4_e2m1 q0_lo(make_float4(vals[0] * s, vals[8] * s, vals[1] * s, vals[9] * s));
|
||||
__nv_fp4x4_e2m1 q0_hi(make_float4(vals[2] * s, vals[10] * s, vals[3] * s, vals[11] * s));
|
||||
__nv_fp4x4_e2m1 q1_lo(make_float4(vals[4] * s, vals[12] * s, vals[5] * s, vals[13] * s));
|
||||
__nv_fp4x4_e2m1 q1_hi(make_float4(vals[6] * s, vals[14] * s, vals[7] * s, vals[15] * s));
|
||||
|
||||
const char2 q0_lo_c = *reinterpret_cast<char2 *>(&q0_lo);
|
||||
const char2 q0_hi_c = *reinterpret_cast<char2 *>(&q0_hi);
|
||||
const char2 q1_lo_c = *reinterpret_cast<char2 *>(&q1_lo);
|
||||
const char2 q1_hi_c = *reinterpret_cast<char2 *>(&q1_hi);
|
||||
|
||||
q0 = uint32_t(uint8_t(q0_lo_c.x)) | (uint32_t(uint8_t(q0_lo_c.y)) << 8) |
|
||||
(uint32_t(uint8_t(q0_hi_c.x)) << 16) | (uint32_t(uint8_t(q0_hi_c.y)) << 24);
|
||||
q1 = uint32_t(uint8_t(q1_lo_c.x)) | (uint32_t(uint8_t(q1_lo_c.y)) << 8) |
|
||||
(uint32_t(uint8_t(q1_hi_c.x)) << 16) | (uint32_t(uint8_t(q1_hi_c.y)) << 24);
|
||||
#else
|
||||
const float inv_scale = subblock_scale > 0.0f ? 0.5f / subblock_scale : 0.0f;
|
||||
#pragma unroll
|
||||
for (int k = 0; k < QK_NVFP4_SUB / 4; ++k) {
|
||||
q0 |= uint32_t(ggml_cuda_float_to_fp4_e2m1(vals[k + 0] * inv_col_scale, inv_scale)) << (8 * k);
|
||||
q0 |= uint32_t(ggml_cuda_float_to_fp4_e2m1(vals[k + 8] * inv_col_scale, inv_scale)) << (8 * k + 4);
|
||||
q1 |= uint32_t(ggml_cuda_float_to_fp4_e2m1(vals[k + 4] * inv_col_scale, inv_scale)) << (8 * k);
|
||||
q1 |= uint32_t(ggml_cuda_float_to_fp4_e2m1(vals[k + 12] * inv_col_scale, inv_scale)) << (8 * k + 4);
|
||||
}
|
||||
#endif // CUDART_VERSION >= 12080
|
||||
|
||||
return fp8_code;
|
||||
}
|
||||
#endif // defined(BLACKWELL_MMA_AVAILABLE)
|
||||
|
||||
// scatter: grid over tokens, quantize once, write to all the token's compact rows
|
||||
template <typename src_t, bool scatter, bool use_aligned_float8>
|
||||
static __global__ void quantize_mmq_nvfp4(
|
||||
@@ -232,7 +364,6 @@ static __global__ void quantize_mmq_nvfp4(
|
||||
const int sub = (i0_base % QK_FP4_MMQ) / QK_NVFP4_SUB;
|
||||
|
||||
const float row_scale = warp_amax[0];
|
||||
const float inv_col_scale = row_scale > 0.0f ? 1.0f / row_scale : 0.0f;
|
||||
|
||||
float vals[QK_NVFP4_SUB];
|
||||
if constexpr (use_aligned_float8) {
|
||||
@@ -251,90 +382,9 @@ static __global__ void quantize_mmq_nvfp4(
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t q0 = 0;
|
||||
uint32_t q1 = 0;
|
||||
|
||||
float amax_sub = 0.0f;
|
||||
#pragma unroll
|
||||
for (int k = 0; k < QK_NVFP4_SUB; ++k) {
|
||||
amax_sub = fmaxf(amax_sub, fabsf(vals[k] * inv_col_scale));
|
||||
}
|
||||
|
||||
static constexpr int test_offsets[5] = { 0, -1, 1, -2, 2 };
|
||||
const int first_fp8_code = (int) ggml_cuda_fp32_to_ue4m3(amax_sub / 6.0f);
|
||||
|
||||
uint8_t fp8_code = (uint8_t) first_fp8_code;
|
||||
float subblock_scale = ggml_cuda_ue4m3_to_fp32(fp8_code);
|
||||
float inv_scale_err = subblock_scale > 0.0f ? 0.5f / subblock_scale : 0.0f;
|
||||
#if CUDART_VERSION >= 12080
|
||||
float best_err = nvfp4_native_scale_error(vals, inv_col_scale, inv_scale_err, subblock_scale);
|
||||
#else
|
||||
float best_err = 0.0f;
|
||||
#pragma unroll
|
||||
for (int k = 0; k < QK_NVFP4_SUB; ++k) {
|
||||
const float v = vals[k] * inv_col_scale;
|
||||
const uint8_t q = ggml_cuda_float_to_fp4_e2m1(v, inv_scale_err);
|
||||
const float err_diff = fabsf(v) - fabsf(kvalues_fp4[q & 0x7]) * subblock_scale;
|
||||
best_err = fmaf(err_diff, err_diff, best_err);
|
||||
}
|
||||
#endif // CUDART_VERSION >= 12080
|
||||
|
||||
#pragma unroll
|
||||
for (int i = 1; i < 5; ++i) {
|
||||
const int test_code = first_fp8_code + test_offsets[i];
|
||||
if (test_code < 0 || test_code > 0x7e) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const float test_scale = ggml_cuda_ue4m3_to_fp32((uint8_t) test_code);
|
||||
const float test_inv_scale = test_scale > 0.0f ? 0.5f / test_scale : 0.0f;
|
||||
#if CUDART_VERSION >= 12080
|
||||
const float cur_err = nvfp4_native_scale_error(vals, inv_col_scale, test_inv_scale, test_scale);
|
||||
#else
|
||||
float cur_err = 0.0f;
|
||||
#pragma unroll
|
||||
for (int k = 0; k < QK_NVFP4_SUB; ++k) {
|
||||
const float v = vals[k] * inv_col_scale;
|
||||
const uint8_t q = ggml_cuda_float_to_fp4_e2m1(v, test_inv_scale);
|
||||
const float err_diff = fabsf(v) - fabsf(kvalues_fp4[q & 0x7]) * test_scale;
|
||||
cur_err = fmaf(err_diff, err_diff, cur_err);
|
||||
}
|
||||
#endif // CUDART_VERSION >= 12080
|
||||
|
||||
if (cur_err < best_err) {
|
||||
best_err = cur_err;
|
||||
fp8_code = (uint8_t) test_code;
|
||||
subblock_scale = test_scale;
|
||||
}
|
||||
}
|
||||
#if CUDART_VERSION >= 12080
|
||||
const float inv_scale = subblock_scale > 0.0f ? 0.5f / subblock_scale : 0.0f;
|
||||
const float s = inv_col_scale * inv_scale;
|
||||
|
||||
__nv_fp4x4_e2m1 q0_lo(make_float4(vals[0] * s, vals[8] * s, vals[1] * s, vals[9] * s));
|
||||
__nv_fp4x4_e2m1 q0_hi(make_float4(vals[2] * s, vals[10] * s, vals[3] * s, vals[11] * s));
|
||||
__nv_fp4x4_e2m1 q1_lo(make_float4(vals[4] * s, vals[12] * s, vals[5] * s, vals[13] * s));
|
||||
__nv_fp4x4_e2m1 q1_hi(make_float4(vals[6] * s, vals[14] * s, vals[7] * s, vals[15] * s));
|
||||
|
||||
const char2 q0_lo_c = *reinterpret_cast<char2 *>(&q0_lo);
|
||||
const char2 q0_hi_c = *reinterpret_cast<char2 *>(&q0_hi);
|
||||
const char2 q1_lo_c = *reinterpret_cast<char2 *>(&q1_lo);
|
||||
const char2 q1_hi_c = *reinterpret_cast<char2 *>(&q1_hi);
|
||||
|
||||
q0 = uint32_t(uint8_t(q0_lo_c.x)) | (uint32_t(uint8_t(q0_lo_c.y)) << 8) |
|
||||
(uint32_t(uint8_t(q0_hi_c.x)) << 16) | (uint32_t(uint8_t(q0_hi_c.y)) << 24);
|
||||
q1 = uint32_t(uint8_t(q1_lo_c.x)) | (uint32_t(uint8_t(q1_lo_c.y)) << 8) |
|
||||
(uint32_t(uint8_t(q1_hi_c.x)) << 16) | (uint32_t(uint8_t(q1_hi_c.y)) << 24);
|
||||
#else
|
||||
const float inv_scale = subblock_scale > 0.0f ? 0.5f / subblock_scale : 0.0f;
|
||||
#pragma unroll
|
||||
for (int k = 0; k < QK_NVFP4_SUB / 4; ++k) {
|
||||
q0 |= uint32_t(ggml_cuda_float_to_fp4_e2m1(vals[k + 0] * inv_col_scale, inv_scale)) << (8 * k);
|
||||
q0 |= uint32_t(ggml_cuda_float_to_fp4_e2m1(vals[k + 8] * inv_col_scale, inv_scale)) << (8 * k + 4);
|
||||
q1 |= uint32_t(ggml_cuda_float_to_fp4_e2m1(vals[k + 4] * inv_col_scale, inv_scale)) << (8 * k);
|
||||
q1 |= uint32_t(ggml_cuda_float_to_fp4_e2m1(vals[k + 12] * inv_col_scale, inv_scale)) << (8 * k + 4);
|
||||
}
|
||||
#endif // CUDART_VERSION >= 12080
|
||||
uint32_t q0;
|
||||
uint32_t q1;
|
||||
const uint8_t fp8_code = quantize_nvfp4_subblock(vals, row_scale, q0, q1);
|
||||
|
||||
if constexpr (scatter) {
|
||||
#pragma unroll
|
||||
@@ -711,7 +761,8 @@ static __device__ __forceinline__ void quantize_mmq_few_store(const float4 xi, i
|
||||
}
|
||||
|
||||
// Each thread quantizes 4 values of one column, see quantize.cuh for the layouts.
|
||||
template <typename src_t, mmq_q8_1_ds_layout ds_layout, bool wmma>
|
||||
// nvfp4_order: the 4 values of a thread are 2m, 2m + 8, 2m + 1, 2m + 9 of 16 values, the values of 2 bytes of block_nvfp4.
|
||||
template <typename src_t, mmq_q8_1_ds_layout ds_layout, bool wmma, bool nvfp4_order>
|
||||
static __global__ void quantize_mmq_few_q8_1(
|
||||
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) {
|
||||
@@ -727,7 +778,19 @@ static __global__ void quantize_mmq_few_q8_1(
|
||||
|
||||
const int64_t i2 = blockIdx.z % ne2;
|
||||
const int64_t i3 = blockIdx.z / ne2;
|
||||
const float4 xi = (int) blockIdx.x < ne1 ? quantize_load4(x + i3*s03 + i2*s02 + blockIdx.x*s01 + i0, 0) : make_float4(0.0f, 0.0f, 0.0f, 0.0f);
|
||||
static_assert(!(wmma && nvfp4_order), "the WMMA layout has no nvfp4 order");
|
||||
float4 xi = 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;
|
||||
if constexpr (nvfp4_order) {
|
||||
const int64_t i16 = i0 - i0 % 16 + 2*((i0 % 16)/4);
|
||||
const float2 lo = quantize_load2(xc + i16);
|
||||
const float2 hi = quantize_load2(xc + i16 + 8);
|
||||
xi = make_float4(lo.x, hi.x, lo.y, hi.y);
|
||||
} else {
|
||||
xi = quantize_load4(xc + i0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
if constexpr (wmma) {
|
||||
const int m = blockIdx.x % MMQ_FEW_WMMA_NCOLS; // column in group
|
||||
@@ -833,21 +896,29 @@ static __global__ void quantize_mmq_few_id_q8_1(
|
||||
|
||||
template <bool wmma, typename src_t>
|
||||
static void quantize_mmq_few_q8_1_launch(
|
||||
const src_t * x, void * vy, const mmq_q8_1_ds_layout ds_layout, const int64_t ne00, const int64_t s01, const int64_t s02, const int64_t s03,
|
||||
const src_t * x, 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) {
|
||||
GGML_ASSERT(ne00 % QK_K == 0);
|
||||
GGML_ASSERT(!nvfp4_order || (ds_layout == MMQ_Q8_1_DS_LAYOUT_D4 && !wmma));
|
||||
|
||||
const int64_t block_num_y = (ne00 + 4*CUDA_QUANTIZE_BLOCK_SIZE_MMQ - 1) / (4*CUDA_QUANTIZE_BLOCK_SIZE_MMQ);
|
||||
const dim3 num_blocks(GGML_PAD(ne1, wmma ? MMQ_FEW_WMMA_NCOLS : 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);
|
||||
if constexpr (!wmma) {
|
||||
if (nvfp4_order) {
|
||||
ggml_cuda_kernel_launch(quantize_mmq_few_q8_1<src_t, MMQ_Q8_1_DS_LAYOUT_D4, false, true>, launch_params,
|
||||
x, vy, ne00, s01, s02, s03, (int) ne1, (int) ne2);
|
||||
return;
|
||||
}
|
||||
}
|
||||
switch (ds_layout) {
|
||||
case MMQ_Q8_1_DS_LAYOUT_D4:
|
||||
ggml_cuda_kernel_launch(quantize_mmq_few_q8_1<src_t, MMQ_Q8_1_DS_LAYOUT_D4, wmma>, launch_params,
|
||||
ggml_cuda_kernel_launch(quantize_mmq_few_q8_1<src_t, MMQ_Q8_1_DS_LAYOUT_D4, wmma, false>, launch_params,
|
||||
x, vy, ne00, s01, s02, s03, (int) ne1, (int) ne2);
|
||||
break;
|
||||
case MMQ_Q8_1_DS_LAYOUT_DS4:
|
||||
ggml_cuda_kernel_launch(quantize_mmq_few_q8_1<src_t, MMQ_Q8_1_DS_LAYOUT_DS4, wmma>, launch_params,
|
||||
ggml_cuda_kernel_launch(quantize_mmq_few_q8_1<src_t, MMQ_Q8_1_DS_LAYOUT_DS4, wmma, false>, launch_params,
|
||||
x, vy, ne00, s01, s02, s03, (int) ne1, (int) ne2);
|
||||
break;
|
||||
default:
|
||||
@@ -855,11 +926,97 @@ static void quantize_mmq_few_q8_1_launch(
|
||||
}
|
||||
}
|
||||
|
||||
// Each thread quantizes one sub-block of 16 values like quantize_mmq_nvfp4, columns beyond ne1 are zero.
|
||||
// A CUDA block takes a chunk of the sub-blocks of one column, so each CUDA block first reads the whole column for the column scale.
|
||||
template <typename src_t>
|
||||
static __global__ void quantize_mmq_few_nvfp4(
|
||||
const src_t * x_ptr, void * vy_ptr, float * y_scale_ptr, const int64_t ne00, const int64_t s01, const int64_t s02, const int64_t s03,
|
||||
const int ne1, const int ne2) {
|
||||
#if defined(BLACKWELL_MMA_AVAILABLE)
|
||||
ggml_cuda_pdl_lc();
|
||||
const src_t * GGML_CUDA_RESTRICT x = x_ptr;
|
||||
int * GGML_CUDA_RESTRICT vy = (int *) vy_ptr;
|
||||
float * GGML_CUDA_RESTRICT y_scale = y_scale_ptr;
|
||||
|
||||
const int col = blockIdx.x;
|
||||
const int64_t i2 = blockIdx.z % ne2;
|
||||
const int64_t i3 = blockIdx.z / ne2;
|
||||
const src_t * x_row = x + i3*s03 + i2*s02 + int64_t(col)*s01;
|
||||
|
||||
ggml_cuda_pdl_sync();
|
||||
|
||||
float amax = 0.0f;
|
||||
if (col < ne1) {
|
||||
for (int64_t i0 = 4*threadIdx.x; i0 < ne00; i0 += 4*CUDA_QUANTIZE_BLOCK_SIZE_MMQ) {
|
||||
const float4 v = quantize_load4(x_row + i0, 0);
|
||||
amax = fmaxf(amax, fmaxf(fmaxf(fabsf(v.x), fabsf(v.y)), fmaxf(fabsf(v.z), fabsf(v.w))));
|
||||
}
|
||||
}
|
||||
__shared__ float amax_warp[CUDA_QUANTIZE_BLOCK_SIZE_MMQ/WARP_SIZE];
|
||||
amax = block_reduce<block_reduce_method::MAX, CUDA_QUANTIZE_BLOCK_SIZE_MMQ>(amax, amax_warp);
|
||||
|
||||
const float col_scale = amax / (6.0f * 448.0f);
|
||||
if (blockIdx.y == 0 && threadIdx.x == 0) {
|
||||
y_scale[blockIdx.z*gridDim.x + col] = col_scale;
|
||||
}
|
||||
|
||||
const int64_t isb = int64_t(blockIdx.y)*CUDA_QUANTIZE_BLOCK_SIZE_MMQ + threadIdx.x;
|
||||
if (isb >= ne00/QK_NVFP4_SUB) {
|
||||
return;
|
||||
}
|
||||
|
||||
float vals[QK_NVFP4_SUB] = {0.0f};
|
||||
if (col < ne1) {
|
||||
#pragma unroll
|
||||
for (int k = 0; k < QK_NVFP4_SUB; k += 4) {
|
||||
const float4 v = quantize_load4(x_row + isb*QK_NVFP4_SUB + k, 0);
|
||||
vals[k + 0] = v.x;
|
||||
vals[k + 1] = v.y;
|
||||
vals[k + 2] = v.z;
|
||||
vals[k + 3] = v.w;
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t q[2];
|
||||
const uint8_t code = quantize_nvfp4_subblock(vals, col_scale, q[0], q[1]);
|
||||
|
||||
const int64_t kb = isb / (QK_NVFP4/QK_NVFP4_SUB); // block of 64 values
|
||||
const int sub = isb % (QK_NVFP4/QK_NVFP4_SUB);
|
||||
const int ib = kb % (QK_K/QK_NVFP4); // block in the unit of 256 values
|
||||
const int g = col % MMQ_FEW_NCOLS;
|
||||
int * yg = vy + 4*(((blockIdx.z*(ne00/QK_K) + kb/(QK_K/QK_NVFP4))*(gridDim.x/MMQ_FEW_NCOLS) + col/MMQ_FEW_NCOLS)*MMQ_FEW_FP4_INT4);
|
||||
#pragma unroll
|
||||
for (int h = 0; h < 2; ++h) {
|
||||
const int i = 2*sub + h; // int of the block
|
||||
yg[4*((ib/2)*WARP_SIZE + 4*g + i % 4) + 2*(ib % 2) + i/4] = q[h];
|
||||
}
|
||||
((uint8_t *) (yg + 4*(2*WARP_SIZE + g) + ib))[sub] = code;
|
||||
#else
|
||||
GGML_UNUSED_VARS(x_ptr, vy_ptr, y_scale_ptr, ne00, s01, s02, s03, ne1, ne2);
|
||||
NO_DEVICE_CODE;
|
||||
#endif // defined(BLACKWELL_MMA_AVAILABLE)
|
||||
}
|
||||
|
||||
void quantize_mmq_few_nvfp4_cuda(
|
||||
const void * x, const ggml_type type_src1, void * vy, float * y_scale, 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 nsub = ne00 / QK_NVFP4_SUB;
|
||||
const dim3 num_blocks(GGML_PAD(ne1, MMQ_FEW_NCOLS), (nsub + CUDA_QUANTIZE_BLOCK_SIZE_MMQ - 1) / CUDA_QUANTIZE_BLOCK_SIZE_MMQ, 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_nvfp4<src_t>, launch_params, x_t, vy, y_scale, 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 int64_t ne00, const int64_t s01, const int64_t s02, const int64_t s03,
|
||||
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) {
|
||||
quantize_launch_typed(x, type_src1, [&](auto * x_t) {
|
||||
quantize_mmq_few_q8_1_launch<false>(x_t, vy, ds_layout, ne00, s01, s02, s03, ne1, ne2, ne3, stream);
|
||||
quantize_mmq_few_q8_1_launch<false>(x_t, vy, ds_layout, nvfp4_order, ne00, s01, s02, s03, ne1, ne2, ne3, stream);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -867,7 +1024,7 @@ void quantize_mmq_few_wmma_q8_1_cuda(
|
||||
const void * x, const ggml_type type_src1, void * vy, const mmq_q8_1_ds_layout ds_layout, 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) {
|
||||
quantize_launch_typed(x, type_src1, [&](auto * x_t) {
|
||||
quantize_mmq_few_q8_1_launch<true>(x_t, vy, ds_layout, ne00, s01, s02, s03, ne1, ne2, ne3, stream);
|
||||
quantize_mmq_few_q8_1_launch<true>(x_t, vy, ds_layout, /*nvfp4_order =*/ false, ne00, s01, s02, s03, ne1, ne2, ne3, stream);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -30,8 +30,21 @@ void quantize_mmq_q8_1_cuda(
|
||||
static constexpr int MMQ_FEW_NCOLS = 8; // src1 columns per column group
|
||||
static constexpr int MMQ_FEW_Q8_1_INT4 = 4*WARP_SIZE + 4*4; // int4 per 256 values and column group
|
||||
|
||||
// nvfp4_order: the value order of block_nvfp4 within 16 values, see quantize_mmq_few_q8_1.
|
||||
void quantize_mmq_few_q8_1_cuda(
|
||||
const void * x, ggml_type type_src1, void * vy, mmq_q8_1_ds_layout ds_layout, int64_t ne00, int64_t s01, int64_t s02, int64_t s03,
|
||||
const void * x, ggml_type type_src1, void * vy, mmq_q8_1_ds_layout ds_layout, bool nvfp4_order, 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 NVFP4 src0 with 4-bit src1 (Blackwell), quantized like quantize_mmq_nvfp4: a float scale per column, E2M1 values with a UE4M3 scale per 16 values.
|
||||
// A 64 value block has the block_nvfp4 layout: 8 ints of packed values and an int of 4 scales. The values of int i are k = 8i .. 8i + 7 of the m16n8k64 MMA.
|
||||
// Per channel, 256 values and column group:
|
||||
// qs: int4[2][32], [h][lane] holds the B fragments of the blocks 2h and 2h+1 of lane (g, t): ints t and t + 4 of column g.
|
||||
// sc: int4[8], [g] holds the scales of the 4 blocks of column g.
|
||||
// The column scales are a float array per channel, ne1 padded to a multiple of MMQ_FEW_NCOLS.
|
||||
static constexpr int MMQ_FEW_FP4_INT4 = 2*WARP_SIZE + MMQ_FEW_NCOLS; // int4 per 256 values and column group
|
||||
|
||||
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);
|
||||
|
||||
// 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.
|
||||
|
||||
+143
-9
@@ -6688,25 +6688,28 @@ struct test_mul_mat_hadamard : public test_mul_mat {
|
||||
}
|
||||
};
|
||||
|
||||
// FP4 W4A8 path (GGML_PREC_Q8 on src1 disallows 4-bit activations)
|
||||
// FP4 W4A8 path (GGML_PREC_Q8 on src1 disallows 4-bit activations), or with prec = GGML_PREC_Q4 the W4A4 path requested by the graph
|
||||
struct test_mul_mat_w4a8 : public test_mul_mat {
|
||||
const ggml_prec prec;
|
||||
|
||||
test_mul_mat_w4a8(ggml_type type_a = GGML_TYPE_NVFP4, ggml_type type_b = GGML_TYPE_F32,
|
||||
int64_t m = 32, int64_t n = 32, int64_t k = 256,
|
||||
std::array<int64_t, 2> bs = {1, 1},
|
||||
std::array<int64_t, 2> nr = {1, 1})
|
||||
: test_mul_mat(type_a, type_b, m, n, k, bs, nr) {}
|
||||
std::array<int64_t, 2> nr = {1, 1},
|
||||
ggml_prec prec = GGML_PREC_Q8)
|
||||
: test_mul_mat(type_a, type_b, m, n, k, bs, nr), prec(prec) {}
|
||||
ggml_tensor * build_graph(ggml_context * ctx, ggml_context * ctx_weights) override {
|
||||
ggml_tensor * out = test_mul_mat::build_graph(ctx, ctx_weights);
|
||||
for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
|
||||
if (t->op == GGML_OP_MUL_MAT) {
|
||||
ggml_prec_set_src(t, GGML_PREC_Q8, 1);
|
||||
ggml_prec_set_src(t, prec, 1);
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
std::string op_desc(ggml_tensor * t) override {
|
||||
GGML_UNUSED(t);
|
||||
return "MUL_MAT_W4A8";
|
||||
return prec == GGML_PREC_Q4 ? "MUL_MAT_W4A4_REQ" : "MUL_MAT_W4A8";
|
||||
}
|
||||
};
|
||||
|
||||
@@ -9327,6 +9330,67 @@ struct test_mul_mat_pos : public test_mul_mat {
|
||||
}
|
||||
};
|
||||
|
||||
// mul_mat with 4-bit src1 requested by the graph (GGML_PREC_Q4): the FP4 W4A4 path also where the default src1 is 8-bit (CUDA few-row MMQ)
|
||||
struct test_mul_mat_src1_q4 : public test_mul_mat {
|
||||
using test_mul_mat::test_mul_mat;
|
||||
|
||||
std::string vars() override {
|
||||
return test_mul_mat::vars() + ",src1_prec=q4";
|
||||
}
|
||||
|
||||
ggml_tensor * build_graph(ggml_context * ctx, ggml_context * ctx_weights) override {
|
||||
ggml_tensor * out = test_mul_mat::build_graph(ctx, ctx_weights);
|
||||
for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
|
||||
if (t->op == GGML_OP_MUL_MAT) {
|
||||
ggml_prec_set_src(t, GGML_PREC_Q4, 1);
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
};
|
||||
|
||||
// mul_mat with 4-bit src1 and the src1 rows scaled by 1, 5, ..., 25 (period 7): with a src1 scale per row (FP4 W4A4) the scale of a wrong row changes the result.
|
||||
// Period 7 does not divide the row offsets of other channels and samples for the shapes used here, so a wrong channel or sample also changes it.
|
||||
struct test_mul_mat_rows_scaled : public test_mul_mat_src1_q4 {
|
||||
using test_mul_mat_src1_q4::test_mul_mat_src1_q4;
|
||||
|
||||
std::string vars() override {
|
||||
return test_mul_mat_src1_q4::vars() + ",src1_rows=x1..x25";
|
||||
}
|
||||
|
||||
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) {
|
||||
data[i] *= 1.0f + 4.0f*((int64_t) i / t->ne[0] % 7);
|
||||
}
|
||||
ggml_backend_tensor_set(t, data.data(), 0, ggml_nbytes(t));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// mul_mat with the default src1 precision and at most 8 columns: CUDA keeps 8-bit src1 there (MMVQ or few-row MMQ W4A8), so the W4A8 tolerance applies also on Blackwell.
|
||||
// Checks that the default does not switch to 4-bit src1.
|
||||
struct test_mul_mat_8bit_default : public test_mul_mat {
|
||||
using test_mul_mat::test_mul_mat;
|
||||
|
||||
std::string vars() override {
|
||||
return test_mul_mat::vars() + ",src1_prec=default_8bit";
|
||||
}
|
||||
|
||||
double max_nmse_err(ggml_backend_t backend) override {
|
||||
GGML_UNUSED(backend);
|
||||
GGML_ASSERT(n <= 8);
|
||||
return test_mul_mat::max_nmse_err();
|
||||
}
|
||||
};
|
||||
|
||||
enum mul_mat_add_mode {
|
||||
MUL_MAT_ADD_MM_RES, // mm + res
|
||||
MUL_MAT_ADD_RES_MM, // res + mm
|
||||
@@ -9607,6 +9671,27 @@ struct test_mul_mat_fusion_w4a8 : public test_mul_mat_fusion {
|
||||
}
|
||||
};
|
||||
|
||||
// FP4 W4A4 path requested by the graph (GGML_PREC_Q4 on src1), also where the default src1 is 8-bit (CUDA few-row MMQ)
|
||||
struct test_mul_mat_fusion_w4a4_req : public test_mul_mat_fusion {
|
||||
using test_mul_mat_fusion::test_mul_mat_fusion;
|
||||
using test_mul_mat_fusion::build_graph;
|
||||
|
||||
ggml_tensor * build_graph(ggml_context * ctx, ggml_context * ctx_weights) override {
|
||||
ggml_tensor * out = test_mul_mat_fusion::build_graph(ctx, ctx_weights);
|
||||
for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
|
||||
if (t->op == GGML_OP_MUL_MAT || t->op == GGML_OP_MUL_MAT_ID) {
|
||||
ggml_prec_set_src(t, GGML_PREC_Q4, 1);
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
std::string op_desc(ggml_tensor * t) override {
|
||||
GGML_UNUSED(t);
|
||||
return "MUL_MAT_FUSION_W4A4_REQ";
|
||||
}
|
||||
};
|
||||
|
||||
// GGML_OP_SUM
|
||||
struct test_sum : public test_case {
|
||||
const ggml_type type;
|
||||
@@ -13802,7 +13887,7 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
|
||||
}
|
||||
// CUDA few-row MMQ: 1-3 column groups, 32-row tiles (6170 rows), K = 18 units (uneven over the warps) and 9 units (one unit per ring stage)
|
||||
for (ggml_type type_a : {GGML_TYPE_Q8_0, GGML_TYPE_Q3_K, GGML_TYPE_Q4_K, GGML_TYPE_Q5_K, GGML_TYPE_Q6_K,
|
||||
GGML_TYPE_IQ3_S, GGML_TYPE_IQ4_NL, GGML_TYPE_IQ4_XS}) {
|
||||
GGML_TYPE_IQ3_S, GGML_TYPE_IQ4_NL, GGML_TYPE_IQ4_XS, GGML_TYPE_NVFP4}) {
|
||||
for (int64_t k : {4608, 2304}) {
|
||||
test_cases.emplace_back(new test_mul_mat(type_a, GGML_TYPE_F32, 1000, 8, k, {1, 1}, {1, 1}));
|
||||
test_cases.emplace_back(new test_mul_mat(type_a, GGML_TYPE_F32, 6170, 13, k, {1, 1}, {1, 1}));
|
||||
@@ -13828,6 +13913,45 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
|
||||
// CUDA few-row MMQ for MUL_MAT_ID: strided experts, more than 32 experts
|
||||
test_cases.emplace_back(new test_mul_mat_id(GGML_TYPE_Q4_K, GGML_TYPE_F32, 16, 4, true, 72, 13, 2048, 80));
|
||||
test_cases.emplace_back(new test_mul_mat_id(GGML_TYPE_Q4_K, GGML_TYPE_F32, 128, 8, false, 72, 16, 2048));
|
||||
// CUDA few-row MMQ for NVFP4 (also in the loop above): 8-bit src1 (by default and with GGML_PREC_Q8) from 7 tokens (DGX Spark: 9),
|
||||
// 4-bit src1 only with GGML_PREC_Q4, from 6 tokens (DGX Spark: 8), K = 8 units and not a multiple of 256
|
||||
for (int64_t n : {7, 8, 9, 21, 24}) {
|
||||
test_cases.emplace_back(new test_mul_mat_w4a8(GGML_TYPE_NVFP4, GGML_TYPE_F32, 1000, n, 4608));
|
||||
if (n <= 8) {
|
||||
test_cases.emplace_back(new test_mul_mat_8bit_default(GGML_TYPE_NVFP4, GGML_TYPE_F32, 1000, n, 2048, {1, 1}, {1, 1}));
|
||||
} else {
|
||||
test_cases.emplace_back(new test_mul_mat(GGML_TYPE_NVFP4, GGML_TYPE_F32, 1000, n, 2048, {1, 1}, {1, 1}));
|
||||
}
|
||||
}
|
||||
for (int64_t n : {6, 8, 9, 21, 24}) {
|
||||
test_cases.emplace_back(new test_mul_mat_w4a8(GGML_TYPE_NVFP4, GGML_TYPE_F32, 1000, n, 4608, {1, 1}, {1, 1}, GGML_PREC_Q4));
|
||||
}
|
||||
test_cases.emplace_back(new test_mul_mat_w4a8(GGML_TYPE_NVFP4, GGML_TYPE_F32, 6170, 13, 4608));
|
||||
test_cases.emplace_back(new test_mul_mat_w4a8(GGML_TYPE_NVFP4, GGML_TYPE_F32, 1000, 21, 2304));
|
||||
test_cases.emplace_back(new test_mul_mat_w4a8(GGML_TYPE_NVFP4, GGML_TYPE_F32, 1000, 8, 4160));
|
||||
test_cases.emplace_back(new test_mul_mat_w4a8(GGML_TYPE_NVFP4, GGML_TYPE_F32, 64, 13, 4608, {3, 2}, {2, 2}));
|
||||
// 4-bit src1 with broadcast: src1 rows of different magnitudes, so a wrong row, channel or sample of the src1 scales changes the result
|
||||
test_cases.emplace_back(new test_mul_mat_rows_scaled(GGML_TYPE_NVFP4, GGML_TYPE_F32, 63, 13, 4608, {3, 2}, {2, 2}));
|
||||
// 8-bit and 4-bit src1 with the weights in a WEIGHTS buffer (with PDL the kernel reads them before the wait)
|
||||
test_cases.emplace_back(new test_mul_mat (GGML_TYPE_NVFP4, GGML_TYPE_F32, 6170, 13, 4608, {1, 1}, {1, 1}, {0, 1, 2, 3}, 0, 1, false, 0, 0, 0, true));
|
||||
test_cases.emplace_back(new test_mul_mat_src1_q4(GGML_TYPE_NVFP4, GGML_TYPE_F32, 6170, 13, 4608, {1, 1}, {1, 1}, {0, 1, 2, 3}, 0, 1, false, 0, 0, 0, true));
|
||||
// 4-bit and 8-bit src1 with padded and permuted rows of src0 and src1
|
||||
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}, 5120));
|
||||
test_cases.emplace_back(new test_mul_mat_src1_q4(GGML_TYPE_NVFP4, GGML_TYPE_F32, 64, 13, 4608, {2, 3}, {1, 1}, {0, 2, 1, 3}));
|
||||
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}, 5120));
|
||||
test_cases.emplace_back(new test_mul_mat (GGML_TYPE_NVFP4, GGML_TYPE_F32, 64, 13, 4608, {2, 3}, {1, 1}, {0, 2, 1, 3}));
|
||||
// BF16 src1: 8-bit (NVFP4 value order) and 4-bit src1, contiguous and as a permuted view (rows 4*k values apart)
|
||||
for (int64_t n : {8, 13, 24}) {
|
||||
test_cases.emplace_back(new test_mul_mat (GGML_TYPE_NVFP4, GGML_TYPE_BF16, 1000, n, 4608, {1, 1}, {1, 1}));
|
||||
test_cases.emplace_back(new test_mul_mat_src1_q4(GGML_TYPE_NVFP4, GGML_TYPE_BF16, 1000, n, 4608, {1, 1}, {1, 1}));
|
||||
}
|
||||
test_cases.emplace_back(new test_mul_mat (GGML_TYPE_NVFP4, GGML_TYPE_BF16, 64, 13, 4608, {1, 1}, {4, 2}, {0, 2, 1, 3}));
|
||||
test_cases.emplace_back(new test_mul_mat_src1_q4(GGML_TYPE_NVFP4, 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_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));
|
||||
}
|
||||
|
||||
#if 0
|
||||
{
|
||||
@@ -15412,9 +15536,9 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
|
||||
test_cases.emplace_back(new test_mul_mat_fusion(GGML_TYPE_NVFP4, GGML_GLU_OP_SWIGLU, 150, 2560, 256, true, 16, 4, false, false, false, true));
|
||||
test_cases.emplace_back(new test_mul_mat_fusion_w4a8(GGML_TYPE_NVFP4, GGML_GLU_OP_SWIGLU, 150, 2560, 256, false, 1, 1, false, false, false, true, { 1, 1 }));
|
||||
test_cases.emplace_back(new test_mul_mat_fusion_w4a8(GGML_TYPE_NVFP4, GGML_GLU_OP_SWIGLU, 150, 2560, 256, true, 16, 8, false, false, false, true));
|
||||
// Few columns and tiles with a long K: split-K through the stream-k fixup, the scale is applied to the partial sums
|
||||
test_cases.emplace_back(new test_mul_mat_fusion(GGML_TYPE_NVFP4, GGML_GLU_OP_SWIGLU, 11, 64, 2048, false, 1, 1, false, false, false, true, { 1, 1 }));
|
||||
test_cases.emplace_back(new test_mul_mat_fusion_w4a8(GGML_TYPE_NVFP4, GGML_GLU_OP_SWIGLU, 11, 64, 2048, false, 1, 1, false, false, false, true, { 1, 1 }));
|
||||
// Few columns (more than few-row MMQ takes) and tiles with a long K: split-K through the stream-k fixup, the scale is applied to the partial sums
|
||||
test_cases.emplace_back(new test_mul_mat_fusion(GGML_TYPE_NVFP4, GGML_GLU_OP_SWIGLU, 32, 64, 2048, false, 1, 1, false, false, false, true, { 1, 1 }));
|
||||
test_cases.emplace_back(new test_mul_mat_fusion_w4a8(GGML_TYPE_NVFP4, GGML_GLU_OP_SWIGLU, 32, 64, 2048, false, 1, 1, false, false, false, true, { 1, 1 }));
|
||||
test_cases.emplace_back(new test_mul_mat_fusion(GGML_TYPE_Q4_K, GGML_GLU_OP_SWIGLU, 150, 2560, 256, false, 1, 1, false, false, false, true, { 1, 1 }));
|
||||
test_cases.emplace_back(new test_mul_mat_fusion(GGML_TYPE_Q4_K, GGML_GLU_OP_SWIGLU, 150, 2560, 256, true, 16, 8, false, false, false, true));
|
||||
// The same with experts (~1280 tiles each): 17 tokens with broadcast input (one MMA tile per warp), 2048 tokens without (two, several column tiles per expert).
|
||||
@@ -15431,6 +15555,16 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
|
||||
test_cases.emplace_back(new test_mul_mat_fusion(GGML_TYPE_Q4_K, GGML_GLU_OP_SWIGLU, 9, 128, 2048, false, 1, 1, false, false, true, false, { 1, 1 }));
|
||||
// on DGX Spark also the expert gate and up matmuls at 8 tokens, instead of the fused MMVQ path
|
||||
test_cases.emplace_back(new test_mul_mat_fusion(GGML_TYPE_Q4_K, GGML_GLU_OP_SWIGLU, 8, 128, 2048, true, 16, 4, true));
|
||||
// NVFP4 gate and up with weight scales at 8-24 tokens: few-row MMQ, where enabled, takes each matmul with its weight scale
|
||||
for (int64_t m : {8, 13, 24}) {
|
||||
test_cases.emplace_back(new test_mul_mat_fusion (GGML_TYPE_NVFP4, GGML_GLU_OP_SWIGLU, m, 1000, 4096, false, 1, 1, false, false, true, true, { 1, 1 }));
|
||||
test_cases.emplace_back(new test_mul_mat_fusion_w4a8 (GGML_TYPE_NVFP4, GGML_GLU_OP_SWIGLU, m, 1000, 4096, false, 1, 1, false, false, true, true, { 1, 1 }));
|
||||
test_cases.emplace_back(new test_mul_mat_fusion_w4a4_req(GGML_TYPE_NVFP4, GGML_GLU_OP_SWIGLU, m, 1000, 4096, false, 1, 1, false, false, true, true, { 1, 1 }));
|
||||
}
|
||||
test_cases.emplace_back(new test_mul_mat_fusion (GGML_TYPE_NVFP4, GGML_GLU_OP_SWIGLU, 13, 64, 4096, false, 1, 1, false, false, true, true, { 3, 2 }));
|
||||
test_cases.emplace_back(new test_mul_mat_fusion_w4a4_req(GGML_TYPE_NVFP4, GGML_GLU_OP_SWIGLU, 13, 64, 4096, false, 1, 1, false, false, true, true, { 3, 2 }));
|
||||
// 8-bit src1 with 32-row tiles and the weight scale
|
||||
test_cases.emplace_back(new test_mul_mat_fusion_w4a8(GGML_TYPE_NVFP4, GGML_GLU_OP_SWIGLU, 13, 6170, 4096, false, 1, 1, false, false, false, true, { 1, 1 }));
|
||||
// BF16 input to MUL_MAT_ID: fused gate/up + GLU on MMVF (BF16 experts with more than 512 rows) and on MMQ (broadcast input)
|
||||
test_cases.emplace_back(new test_mul_mat_fusion(GGML_TYPE_BF16, GGML_GLU_OP_SWIGLU, 1, 1024, 256, true, 16, 8, false, false, true, false, { 1, 1 }, false, GGML_TYPE_BF16));
|
||||
test_cases.emplace_back(new test_mul_mat_fusion(GGML_TYPE_Q4_K, GGML_GLU_OP_SWIGLU, 17, 5120, 256, true, 16, 8, true, false, true, false, { 4, 2 }, false, GGML_TYPE_BF16));
|
||||
|
||||
Reference in New Issue
Block a user