Commit a0292428f for llama.cpp
commit a0292428f74eb01d5b2c8a79ef032afdbd43a4e0
Author: Georgi Gerganov <ggerganov@gmail.com>
Date: Sun Dec 6 15:06:29 2026 +0200
metal : add native F8_E4M3 support (#33533)
* metal : add native F8_E4M3 support
Add F8_E4M3 dequantization and kernels for MUL_MAT, MUL_MAT_ID,
GET_ROWS, CPY, ADD and FLASH_ATTN_EXT. F8 MUL_MAT with unaligned k
falls back to the mat-vec path. FLASH_ATTN_EXT covers the F16 FA
head-size list for 32-aligned K/V.
This makes the Qwen3.8-27B-NVFP4 model run fully on Metal: graph
splits drop from 290 to 2, prompt processing goes from 4.5 t/s to
~340 t/s and generation from ~3 t/s to ~25 t/s.
Validation:
- test-backend-ops F8_E4M3: 556/556 passed
- test-backend-ops MUL_MAT: 2952/2952 passed
- test-backend-ops MUL_MAT_ID: 1296/1296 passed
- test-backend-ops GET_ROWS,CPY: 499/499 passed
- test-backend-ops ADD: 107/107 passed
- test-backend-ops FLASH_ATTN_EXT: 5604/5604 passed
Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
* metal : fix whitespace in F8 mul_mm kernels
Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
* metal : use rint for F8 rounding
Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
* metal : use a threadgroup LUT for F8 mat-vec
Tune the F8 mat-vec kernel to 4 simdgroups and 8 values per thread, matching the Q8_0 layout. Replace the per-value F8 conversion with a 256-entry threadgroup LUT, initialized once per threadgroup. This makes F8 faster than Q8_0 at decode (n=1).
Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
* metal : add F8 tensor path with threadgroup dequant
Enable the F8 tensor path for MUL_MAT by dequantizing the weights into
threadgroup memory, reusing the existing dequantize path. Add the f32,
f16 and bf16 tensor-kernel instantiations; the bf16 variant dequantizes
to bfloat to match the legacy mul_mm path.
Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
* metal : add F8 MMA path for small-batch mul_mat
Add a specialized F8 MMA kernel for src1=f32 with 2..16 rows. It uses
32-weight blocks and a 256-entry half LUT in threadgroup memory, and
feeds half simdgroup matrices. The path requires 32-aligned K and an
even weight pointer, and falls back otherwise.
At 4096x14336 on M5 Max the F8 MMA path reaches 2.10/3.27/4.01/5.03/7.11
TFLOPS for 2/3/4/5/8 rows, comparable to Q8_0.
Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
* metal : use a threadgroup LUT for F8 mat-mat dequant
Stage a 256-entry half LUT in threadgroup memory for the F8 mat-mat
dequant in both the legacy kernel_mul_mm and kernel_mul_mm_tensor
paths. The LUT replaces the branchy f8_e4m3_to_fp32 conversion.
On M2 Ultra the legacy mat-mat path goes from 13.65 to 17.85 TFLOPS,
matching Q8_0 (17.80). On M5 Max the tensor path goes from 35.09 to
36.90 TFLOPS, closing the gap to Q8_0 (38.10) to ~97%.
Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
* metal : simplify the MMA fusion predicate
Make may_use_mma the graph-shape predicate (type, K alignment, contiguous)
and have use_mma call it plus the runtime constraints. This removes the
F8 special case and the duplicated type checks.
The F8 k=4 case is now rejected by the shared type_ok check for all types.
Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
* tests : update the MTL fusion baseline
The MMA predicate cleanup adds the K-alignment check to the structural
MUL_MAT+ADD fusion check. For metis16 this changes the graph order and
allows one ADD+ADD fusion; the numerical checks accept it.
Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
* metal : drop redundant F8 guard from small-batch path
The small-batch mat-mv type whitelist does not include F8_E4M3, so the
extra guard has no effect.
Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
* metal : group the binary op support checks
Group ADD/SUB/MUL/DIV into one case with a condition matching the bin
kernels: f32/f16 same-type, or f8 with f32/f16/bf16 src1. ADD_ID is
separate and only supports f32 with i32 ids.
The f8 binary ops are supported for all four ops, not just ADD.
Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
* metal : group the cpy support checks
CPY, DUP and CONT all use the cpy kernel, so share one type check.
SET uses two cpy pipelines, so check both src0 and src1.
The f8 conversions are supported for all of them; the previous branch
was CPY-only.
Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
* metal : keep the MMA fusion predicate broad
may_use_mma is only a structural hint for the MUL_MAT+ADD fusion, so
keep it broad (type supported + src1 F32). Move the K alignment and
contiguous-K check to use_mma, which is the actual kernel selection.
This restores the minimax-01 graph order and avoids the scheduler
reallocation. The metis16 ADD+ADD fusion count returns to the previous
value, so update the MTL baseline.
Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
* cont : minor
* metal : add F8_E4M3 support for SET_ROWS
Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
* metal : fix F8_E4M3 flash attention shared memory
F8 is not flagged as quantized, so the non-vec FA path did not reserve the block scratch. Also use 4 simdgroups when the F8 scratch exceeds the threadgroup limit.
Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
* metal : add F8_E4M3 MUL_MAT_ID tensor path
Add the F8 instantiations for the indirect tensor mat-mul kernels and use the 256-entry LUT on the half and bfloat paths. The tensor path is about 1.5x faster than the non-tensor path. The bfloat LUT gives about 5% for MUL_MAT_ID and 11-18% for MUL_MAT.
Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
* metal : simplify F8 path selection
Reject F8 MUL_MAT and MUL_MAT_ID when the K dimension is not a multiple of 32 in supports_op, and remove the alignment guards from the shared use_mm predicates and dispatch. Rename the MMA shared-memory LUT flag and the flash-attention scratch flag.
Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
diff --git a/ggml/src/ggml-metal/CMakeLists.txt b/ggml/src/ggml-metal/CMakeLists.txt
index 0ecc07c45..587ff569c 100644
--- a/ggml/src/ggml-metal/CMakeLists.txt
+++ b/ggml/src/ggml-metal/CMakeLists.txt
@@ -38,6 +38,7 @@ set(METALLIB_KERNELS_FA_SHARED
set(METALLIB_KERNEL_SOURCES
kernels/fa_aux.metal
+ kernels/fa_f8_e4m3.metal
kernels/fa_f16.metal
kernels/fa_f32.metal
kernels/fa_q4_0.metal
@@ -46,6 +47,7 @@ set(METALLIB_KERNEL_SOURCES
kernels/fa_q5_1.metal
kernels/fa_q8_0.metal
kernels/fa_tensor.metal
+ kernels/fa_vec_f8_e4m3.metal
kernels/fa_vec_f16.metal
kernels/fa_vec_f32.metal
kernels/fa_vec_q4_0.metal
diff --git a/ggml/src/ggml-metal/ggml-metal-common.cpp b/ggml/src/ggml-metal/ggml-metal-common.cpp
index e8c3cdcc1..60a6f6dbb 100644
--- a/ggml/src/ggml-metal/ggml-metal-common.cpp
+++ b/ggml/src/ggml-metal/ggml-metal-common.cpp
@@ -59,7 +59,7 @@ static constexpr int64_t GGML_METAL_MMA_TILE_ROWS = 8;
static constexpr int64_t GGML_METAL_MMA_K_CHUNK = 64;
enum ggml_metal_mma_kind ggml_metal_mul_mv_mma_kind(enum ggml_type type, int rt) {
- if (type == GGML_TYPE_Q4_0 || (type == GGML_TYPE_Q8_0 && rt == 1)) {
+ if (type == GGML_TYPE_Q4_0 || (type == GGML_TYPE_Q8_0 && rt == 1) || type == GGML_TYPE_F8_E4M3) {
return GGML_METAL_MMA_KIND_BLK;
}
return type == GGML_TYPE_Q5_K ? GGML_METAL_MMA_KIND_Q5_K : GGML_METAL_MMA_KIND_GEN;
@@ -98,6 +98,7 @@ static bool ggml_metal_mul_mv_mma_type_supported(enum ggml_type type) {
case GGML_TYPE_IQ4_NL:
case GGML_TYPE_IQ4_XS:
case GGML_TYPE_TQ2_0:
+ case GGML_TYPE_F8_E4M3:
return true;
default:
return false;
@@ -108,10 +109,17 @@ int64_t ggml_metal_mul_mv_mma_k_step(enum ggml_type type, int rt) {
if (!ggml_metal_mul_mv_mma_type_supported(type)) {
return 0;
}
+ if (type == GGML_TYPE_F8_E4M3) {
+ return 32;
+ }
return ggml_metal_mul_mv_mma_kind(type, rt) == GGML_METAL_MMA_KIND_BLK ? ggml_blck_size(type) : GGML_METAL_MMA_K_CHUNK;
}
-static bool ggml_metal_mul_mat_mma_type_ok(const struct ggml_tensor * op) {
+static bool ggml_metal_op_mul_mat_mma_type_supported(const struct ggml_tensor * op) {
+ return ggml_metal_mul_mv_mma_k_step(op->src[0]->type, ggml_metal_mul_mv_mma_rt(op)) > 0;
+}
+
+static bool ggml_metal_op_mul_mat_mma_type_ok(const struct ggml_tensor * op) {
const ggml_tensor * src0 = op->src[0];
const int64_t step = ggml_metal_mul_mv_mma_k_step(src0->type, ggml_metal_mul_mv_mma_rt(op));
@@ -149,14 +157,15 @@ bool ggml_metal_op_mul_mat_use_mma(const struct ggml_tensor * op) {
// the batch shape goes into int16 function constants
const bool batch_ok = src1->ne[2] <= INT16_MAX && src1->ne[2]/src0->ne[2] <= INT16_MAX && src1->ne[3]/src0->ne[3] <= INT16_MAX;
- return ggml_metal_mul_mat_mma_type_ok(op) && batch_ok &&
- src1->type == GGML_TYPE_F32 && src1->ne[1] >= ggml_metal_mul_mv_mma_rows_min(src0->type) && src1->ne[1] <= GGML_METAL_MMA_ROWS_MAX &&
+ return ggml_metal_op_mul_mat_may_use_mma(op) && ggml_metal_op_mul_mat_mma_type_ok(op) && batch_ok &&
+ ((uintptr_t) src0->data) % 2 == 0 &&
+ src1->ne[1] >= ggml_metal_mul_mv_mma_rows_min(src0->type) && src1->ne[1] <= GGML_METAL_MMA_ROWS_MAX &&
!ggml_is_transposed(src0) && !ggml_is_transposed(src1) &&
src1->nb[0] == sizeof(float) && src1->nb[1] % 16 == 0 && src1->nb[2] % 16 == 0 && src1->nb[3] % 16 == 0;
}
bool ggml_metal_op_mul_mat_may_use_mma(const struct ggml_tensor * op) {
- return ggml_metal_mul_mv_mma_type_supported(op->src[0]->type) && op->src[1]->type == GGML_TYPE_F32;
+ return ggml_metal_op_mul_mat_mma_type_supported(op) && op->src[1]->type == GGML_TYPE_F32;
}
// represents a memory range (i.e. an interval from a starting address p0 to an ending address p1 in a given buffer pb)
diff --git a/ggml/src/ggml-metal/ggml-metal-device.cpp b/ggml/src/ggml-metal/ggml-metal-device.cpp
index 6af80a175..bf9808eb9 100644
--- a/ggml/src/ggml-metal/ggml-metal-device.cpp
+++ b/ggml/src/ggml-metal/ggml-metal-device.cpp
@@ -891,10 +891,10 @@ static int ggml_metal_halve_to_limit(int n, int64_t limit) {
return n;
}
-static size_t ggml_metal_mul_mv_mma_smem(int nsg, int nt, int rt) {
+static size_t ggml_metal_mul_mv_mma_smem(int nsg, int nt, int rt, bool lut256) {
// one 8x8 float simdgroup matrix per output tile
constexpr size_t tile_bytes = 8*8*sizeof(float);
- return (size_t) nsg*nt*rt*tile_bytes;
+ return (size_t) nsg*nt*rt*tile_bytes + (lut256 ? 256*sizeof(uint16_t) : 0);
}
// fewer src0 rows need more simdgroups per threadgroup (a finer K split) to fill the GPU.
@@ -917,7 +917,7 @@ static ggml_metal_mma_tiling ggml_metal_op_mul_mat_mma_tiling(const ggml_tensor
}
res.nsg = ggml_metal_halve_to_limit(nsg, n_steps);
- const int64_t nt_smem = GGML_METAL_MMA_SMEM_MAX/(int64_t) ggml_metal_mul_mv_mma_smem(res.nsg, 1, res.rt);
+ const int64_t nt_smem = GGML_METAL_MMA_SMEM_MAX/(int64_t) ggml_metal_mul_mv_mma_smem(res.nsg, 1, res.rt, op->src[0]->type == GGML_TYPE_F8_E4M3);
const int64_t nt_rows = ne01/(GGML_METAL_MIN_THREADGROUPS*GGML_METAL_MMA_TILE);
res.nt = ggml_metal_halve_to_limit(GGML_METAL_MMA_NT_MAX, std::min(nt_smem, nt_rows));
@@ -972,7 +972,7 @@ ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_mul_mv_mma(ggml_
}
res.nsg = nsg;
- res.smem = ggml_metal_mul_mv_mma_smem(nsg, nt, rt);
+ res.smem = ggml_metal_mul_mv_mma_smem(nsg, nt, rt, tsrc0 == GGML_TYPE_F8_E4M3);
res.nr0 = GGML_METAL_MMA_TILE*nt;
res.nr1 = GGML_METAL_MMA_TILE*rt;
@@ -1042,11 +1042,19 @@ ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_mul_mm(ggml_meta
const size_t smem_a = NRA * N_MM_NK_TOTAL * sizeof(ggml_fp16_t);
res.smem = smem_a;
+
+ if (tsrc0 == GGML_TYPE_F8_E4M3) {
+ res.smem += 256*sizeof(uint16_t);
+ }
} else {
res.nr0 = 64;
res.nr1 = 32;
res.smem = prec_f32 ? 12288 : (bc_out ? 8192 : (4096 + 2048));
+
+ if (tsrc0 == GGML_TYPE_F8_E4M3 && !prec_f32) {
+ res.smem += 256*sizeof(uint16_t);
+ }
}
res.nsg = N_MM_SIMD_GROUP_X * N_MM_SIMD_GROUP_Y;
@@ -1139,6 +1147,12 @@ ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_mul_mv(ggml_meta
nsg = N_SG_NVFP4;
nr0 = N_R0_NVFP4;
} break;
+ case GGML_TYPE_F8_E4M3:
+ {
+ nsg = N_SG_F8_E4M3;
+ nr0 = N_R0_F8_E4M3;
+ smem = 32*sizeof(float)*nr0 + 256*sizeof(float);
+ } break;
case GGML_TYPE_Q2_K:
{
nsg = N_SG_Q2_K;
@@ -1344,7 +1358,7 @@ ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_mul_mm_id(ggml_m
const bool prec_bf16 = props_dev->has_bfloat &&
ggml_prec_get_src(op, 1) == GGML_PREC_BF16 && tsrc1 == GGML_TYPE_F32 &&
- tsrc0 != GGML_TYPE_BF16 && tsrc0 != GGML_TYPE_MXFP4 && tsrc0 != GGML_TYPE_NVFP4;
+ tsrc0 != GGML_TYPE_BF16 && tsrc0 != GGML_TYPE_MXFP4 && tsrc0 != GGML_TYPE_NVFP4 && tsrc0 != GGML_TYPE_F8_E4M3;
const bool prec_f32 = ggml_prec_get_src(op, 1) == GGML_PREC_F32 && tsrc1 == GGML_TYPE_F32;
@@ -1451,6 +1465,12 @@ ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_mul_mv_id(ggml_m
nsg = N_SG_NVFP4;
nr0 = N_R0_NVFP4;
} break;
+ case GGML_TYPE_F8_E4M3:
+ {
+ nsg = N_SG_F8_E4M3;
+ nr0 = N_R0_F8_E4M3;
+ smem = 32*sizeof(float)*nr0 + 256*sizeof(float);
+ } break;
case GGML_TYPE_Q2_K:
{
nsg = N_SG_Q2_K;
@@ -2213,7 +2233,7 @@ ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_bin(ggml_metal_l
const char * t1_str = ggml_type_name(op->src[1]->type);
const char * t_str = ggml_type_name(op->type);
- const bool is_c4 = (op->src[0]->ne[0] % 4 == 0) && (op->src[1]->ne[0] % 4 == 0);
+ const bool is_c4 = (op->src[0]->ne[0] % 4 == 0) && (op->src[1]->ne[0] % 4 == 0) && op->src[0]->type != GGML_TYPE_F8_E4M3;
const bool is_cb = op->src[0]->ne[0] != op->src[1]->ne[0];
const bool is_rb = ggml_is_contiguous(op->src[0]) && ggml_is_contiguous(op->src[1]) && (ggml_nrows(op->src[1]) == 1) && ggml_nelements(op) < 65536;
diff --git a/ggml/src/ggml-metal/ggml-metal-device.m b/ggml/src/ggml-metal/ggml-metal-device.m
index 3db087e4a..d6a293bd2 100644
--- a/ggml/src/ggml-metal/ggml-metal-device.m
+++ b/ggml/src/ggml-metal/ggml-metal-device.m
@@ -114,6 +114,7 @@ int ggml_metal_pipeline_max_theads_per_threadgroup(struct ggml_metal_pipeline_wi
// ggml_metallib_<name>_{start,end} embed-symbol stem.
#define GGML_METAL_LIBS \
X(FA_AUX, fa_aux) \
+ X(FA_F8_E4M3, fa_f8_e4m3) \
X(FA_F16, fa_f16) \
X(FA_F32, fa_f32) \
X(FA_Q4_0, fa_q4_0) \
@@ -122,6 +123,7 @@ int ggml_metal_pipeline_max_theads_per_threadgroup(struct ggml_metal_pipeline_wi
X(FA_Q5_1, fa_q5_1) \
X(FA_Q8_0, fa_q8_0) \
X(FA_TENSOR, fa_tensor) \
+ X(FA_VEC_F8_E4M3, fa_vec_f8_e4m3) \
X(FA_VEC_F16, fa_vec_f16) \
X(FA_VEC_F32, fa_vec_f32) \
X(FA_VEC_Q4_0, fa_vec_q4_0) \
@@ -1544,11 +1546,14 @@ static bool ggml_metal_supports_mul_mat_op(
bool src0_f16_has_mv,
bool mm_path) {
if (!has_simdgroup_reduction ||
- op->src[0]->type == GGML_TYPE_F8_E4M3 ||
op->src[0]->type == GGML_TYPE_TQ1_0) {
return false;
}
+ if (op->src[0]->type == GGML_TYPE_F8_E4M3) {
+ return true;
+ }
+
// BF16 src1 kernels: mul_mv for F32 and BF16 src0, mul_mm for BF16 src0
if (op->src[1]->type == GGML_TYPE_BF16) {
return op->op == GGML_OP_MUL_MAT && (op->src[0]->type == GGML_TYPE_BF16 || (op->src[0]->type == GGML_TYPE_F32 && !mm_path));
@@ -1569,6 +1574,77 @@ static bool ggml_metal_supports_mul_mat_op(
return mm_path;
}
+static bool ggml_metal_supports_cpy_type(enum ggml_type tsrc, enum ggml_type tdst) {
+ if (tsrc == GGML_TYPE_F8_E4M3) {
+ return tdst == GGML_TYPE_F8_E4M3 ||
+ tdst == GGML_TYPE_F32 ||
+ tdst == GGML_TYPE_F16 ||
+ tdst == GGML_TYPE_BF16;
+ }
+ if (tdst == GGML_TYPE_F8_E4M3) {
+ return tsrc == GGML_TYPE_F32 ||
+ tsrc == GGML_TYPE_F16 ||
+ tsrc == GGML_TYPE_BF16;
+ }
+ switch (tsrc) {
+ case GGML_TYPE_F32:
+ switch (tdst) {
+ case GGML_TYPE_F32:
+ case GGML_TYPE_F16:
+ case GGML_TYPE_BF16:
+ case GGML_TYPE_Q8_0:
+ case GGML_TYPE_Q1_0:
+ case GGML_TYPE_Q2_0:
+ case GGML_TYPE_Q4_0:
+ case GGML_TYPE_Q4_1:
+ case GGML_TYPE_Q5_0:
+ case GGML_TYPE_Q5_1:
+ case GGML_TYPE_IQ4_NL:
+ case GGML_TYPE_TQ2_0:
+ case GGML_TYPE_I32:
+ return true;
+ default:
+ return false;
+ }
+ case GGML_TYPE_F16:
+ switch (tdst) {
+ case GGML_TYPE_F32:
+ case GGML_TYPE_F16:
+ return true;
+ default:
+ return false;
+ }
+ case GGML_TYPE_BF16:
+ switch (tdst) {
+ case GGML_TYPE_F32:
+ case GGML_TYPE_BF16:
+ return true;
+ default:
+ return false;
+ }
+ case GGML_TYPE_Q1_0:
+ case GGML_TYPE_Q2_0:
+ case GGML_TYPE_Q4_0:
+ case GGML_TYPE_Q4_1:
+ case GGML_TYPE_Q5_0:
+ case GGML_TYPE_Q5_1:
+ case GGML_TYPE_Q8_0:
+ case GGML_TYPE_TQ2_0:
+ case GGML_TYPE_NVFP4:
+ switch (tdst) {
+ case GGML_TYPE_F32:
+ case GGML_TYPE_F16:
+ return true;
+ default:
+ return false;
+ }
+ case GGML_TYPE_I32:
+ return tdst == GGML_TYPE_F32 || tdst == GGML_TYPE_I32;
+ default:
+ return false;
+ }
+}
+
bool ggml_metal_device_supports_op(ggml_metal_device_t dev, const struct ggml_tensor * op) {
const bool has_simdgroup_mm = dev->props.has_simdgroup_mm;
const bool has_simdgroup_reduction = dev->props.has_simdgroup_reduction;
@@ -1683,8 +1759,25 @@ bool ggml_metal_device_supports_op(ggml_metal_device_t dev, const struct ggml_te
case GGML_OP_SUB:
case GGML_OP_MUL:
case GGML_OP_DIV:
+ if (!ggml_is_contiguous_rows(op->src[0]) || !ggml_is_contiguous_rows(op->src[1])) {
+ return false;
+ }
+ if (op->src[0]->type == GGML_TYPE_F8_E4M3) {
+ return op->type == GGML_TYPE_F8_E4M3 &&
+ (op->src[1]->type == GGML_TYPE_F32 ||
+ op->src[1]->type == GGML_TYPE_F16 ||
+ (op->src[1]->type == GGML_TYPE_BF16 && has_bfloat));
+ }
+ return op->src[0]->type == op->src[1]->type &&
+ op->src[0]->type == op->type &&
+ (op->src[0]->type == GGML_TYPE_F32 || op->src[0]->type == GGML_TYPE_F16);
case GGML_OP_ADD_ID:
- return ggml_is_contiguous_rows(op->src[0]) && ggml_is_contiguous_rows(op->src[1]) && (op->src[0]->type == GGML_TYPE_F32 || op->src[0]->type == GGML_TYPE_F16) && (op->src[0]->type == op->src[1]->type);
+ return ggml_is_contiguous_rows(op->src[0]) &&
+ ggml_is_contiguous_rows(op->src[1]) &&
+ op->src[0]->type == GGML_TYPE_F32 &&
+ op->src[1]->type == GGML_TYPE_F32 &&
+ op->type == GGML_TYPE_F32 &&
+ op->src[2]->type == GGML_TYPE_I32;
case GGML_OP_ACC:
return ggml_is_contiguous_rows(op->src[0]) && ggml_is_contiguous_rows(op->src[1]) && op->src[0]->type == GGML_TYPE_F32;
case GGML_OP_REPEAT:
@@ -1843,6 +1936,11 @@ bool ggml_metal_device_supports_op(ggml_metal_device_t dev, const struct ggml_te
case GGML_TYPE_Q5_0:
case GGML_TYPE_Q5_1:
break;
+ case GGML_TYPE_F8_E4M3:
+ if (op->src[1]->ne[0] % 32 != 0 || op->src[2]->ne[0] % 32 != 0) {
+ return false;
+ }
+ break;
case GGML_TYPE_BF16:
if (!has_bfloat) {
return false;
@@ -1938,6 +2036,9 @@ bool ggml_metal_device_supports_op(ggml_metal_device_t dev, const struct ggml_te
if (!ggml_metal_supports_mul_mat_src1_prec(op, dev->props.has_bfloat)) {
return false;
}
+ if (op->src[0]->type == GGML_TYPE_F8_E4M3 && op->src[0]->ne[0] % 32 != 0) {
+ return false;
+ }
// the FWHT kernels read an F16 source directly; every other F16 src1 path
// still goes through ggml_metal_supports_mul_mat_op
if (op->src[0]->type == GGML_TYPE_F32 && op->src[1]->type == GGML_TYPE_F16 &&
@@ -1951,79 +2052,30 @@ bool ggml_metal_device_supports_op(ggml_metal_device_t dev, const struct ggml_te
if (!ggml_metal_supports_mul_mat_src1_prec(op, dev->props.has_bfloat)) {
return false;
}
+ if (op->src[0]->type == GGML_TYPE_F8_E4M3 && op->src[0]->ne[0] % 32 != 0) {
+ return false;
+ }
return ggml_metal_supports_mul_mat_op(
has_simdgroup_reduction, op, false,
ggml_metal_op_mul_mat_id_use_mm(op, has_simdgroup_mm));
- case GGML_OP_SET:
case GGML_OP_CPY:
case GGML_OP_DUP:
case GGML_OP_CONT:
- {
- switch (op->src[0]->type) {
- case GGML_TYPE_F32:
- switch (op->type) {
- case GGML_TYPE_F32:
- case GGML_TYPE_F16:
- case GGML_TYPE_BF16:
- case GGML_TYPE_Q8_0:
- case GGML_TYPE_Q1_0:
- case GGML_TYPE_Q2_0:
- case GGML_TYPE_Q4_0:
- case GGML_TYPE_Q4_1:
- case GGML_TYPE_Q5_0:
- case GGML_TYPE_Q5_1:
- case GGML_TYPE_IQ4_NL:
- case GGML_TYPE_TQ2_0:
- case GGML_TYPE_I32:
- return true;
- default:
- return false;
- }
- case GGML_TYPE_F16:
- switch (op->type) {
- case GGML_TYPE_F32:
- case GGML_TYPE_F16:
- return true;
- default:
- return false;
- }
- case GGML_TYPE_BF16:
- switch (op->type) {
- case GGML_TYPE_F32:
- case GGML_TYPE_BF16:
- return true;
- default:
- return false;
- }
- case GGML_TYPE_Q1_0:
- case GGML_TYPE_Q2_0:
- case GGML_TYPE_Q4_0:
- case GGML_TYPE_Q4_1:
- case GGML_TYPE_Q5_0:
- case GGML_TYPE_Q5_1:
- case GGML_TYPE_Q8_0:
- case GGML_TYPE_TQ2_0:
- case GGML_TYPE_NVFP4:
- switch (op->type) {
- case GGML_TYPE_F32:
- case GGML_TYPE_F16:
- return true;
- default:
- return false;
- }
- case GGML_TYPE_I32:
- return op->type == GGML_TYPE_F32 || op->type == GGML_TYPE_I32;
- default:
- return false;
- };
- }
+ return ggml_metal_supports_cpy_type(op->src[0]->type, op->type);
+ case GGML_OP_SET:
+ return ggml_metal_supports_cpy_type(op->src[0]->type, op->type) &&
+ ggml_metal_supports_cpy_type(op->src[1]->type, op->type);
case GGML_OP_GET_ROWS:
- return op->src[0]->type != GGML_TYPE_TQ1_0 &&
- op->src[0]->type != GGML_TYPE_F8_E4M3;
+ return op->src[0]->type != GGML_TYPE_TQ1_0;
case GGML_OP_SET_ROWS:
{
if (op->src[0]->type == GGML_TYPE_F16) {
- return op->type == GGML_TYPE_F16;
+ return op->type == GGML_TYPE_F16 ||
+ op->type == GGML_TYPE_F8_E4M3;
+ }
+
+ if (op->src[0]->type == GGML_TYPE_BF16) {
+ return op->type == GGML_TYPE_F8_E4M3;
}
if (op->src[0]->type != GGML_TYPE_F32) {
@@ -2034,6 +2086,7 @@ bool ggml_metal_device_supports_op(ggml_metal_device_t dev, const struct ggml_te
case GGML_TYPE_F32:
case GGML_TYPE_F16:
case GGML_TYPE_BF16:
+ case GGML_TYPE_F8_E4M3:
case GGML_TYPE_Q8_0:
case GGML_TYPE_Q4_0:
case GGML_TYPE_Q4_1:
diff --git a/ggml/src/ggml-metal/ggml-metal-impl.h b/ggml/src/ggml-metal/ggml-metal-impl.h
index 8a5713709..79c0c499f 100644
--- a/ggml/src/ggml-metal/ggml-metal-impl.h
+++ b/ggml/src/ggml-metal/ggml-metal-impl.h
@@ -54,6 +54,9 @@
#define N_R0_NVFP4 2
#define N_SG_NVFP4 2
+#define N_R0_F8_E4M3 2
+#define N_SG_F8_E4M3 4
+
#define N_R0_Q2_K 4
#define N_SG_Q2_K 2
diff --git a/ggml/src/ggml-metal/ggml-metal-ops.cpp b/ggml/src/ggml-metal/ggml-metal-ops.cpp
index fb9e08008..284ec8485 100644
--- a/ggml/src/ggml-metal/ggml-metal-ops.cpp
+++ b/ggml/src/ggml-metal/ggml-metal-ops.cpp
@@ -2760,7 +2760,8 @@ static int ggml_metal_op_mul_mat_mv(ggml_metal_op_t ctx, int idx) {
if (op->src[0]->type == GGML_TYPE_F32 ||
op->src[0]->type == GGML_TYPE_F16 ||
op->src[0]->type == GGML_TYPE_BF16 ||
- op->src[0]->type == GGML_TYPE_Q8_0) {
+ op->src[0]->type == GGML_TYPE_Q8_0 ||
+ op->src[0]->type == GGML_TYPE_F8_E4M3) {
ggml_metal_encoder_dispatch_threadgroups(enc, ((ne01 + nr0 - 1)/(nr0)), ((ne11 + nr1 - 1)/nr1), ne12*ne13, 32, nsg, 1);
} else {
ggml_metal_encoder_dispatch_threadgroups(enc, ((ne01 + nr0*nsg - 1)/(nr0*nsg)), ((ne11 + nr1 - 1)/nr1), ne12*ne13, 32, nsg, 1);
@@ -3247,7 +3248,8 @@ static int ggml_metal_op_mul_mat_id_impl(ggml_metal_op_t ctx, int idx) {
if (op->src[0]->type == GGML_TYPE_F32 ||
op->src[0]->type == GGML_TYPE_F16 ||
op->src[0]->type == GGML_TYPE_BF16 ||
- op->src[0]->type == GGML_TYPE_Q8_0) {
+ op->src[0]->type == GGML_TYPE_Q8_0 ||
+ op->src[0]->type == GGML_TYPE_F8_E4M3) {
ggml_metal_encoder_dispatch_threadgroups(enc, (ne01 + nr0 - 1)/(nr0), (_ne1 + nr1 - 1)/nr1, ne123, 32, nsg, 1);
} else {
ggml_metal_encoder_dispatch_threadgroups(enc, (ne01 + nr0*nsg - 1)/(nr0*nsg), (_ne1 + nr1 - 1)/nr1, ne123, 32, nsg, 1);
@@ -4169,21 +4171,26 @@ int ggml_metal_op_flash_attn_ext(ggml_metal_op_t ctx, int idx) {
ggml_metal_op_concurrency_reset(ctx);
}
- const int is_q = ggml_is_quantized(kv_type) ? 1 : 0;
+ const int need_scratch = (ggml_is_quantized(kv_type) || kv_type == GGML_TYPE_F8_E4M3) ? 1 : 0;
// shared memory layout (halfs unless noted):
// queries/attn/result: Q*(DK + 2*PAD2(DV,64) + 4*C)
- // quantized KV scratch: 16*32*NSG (only when is_q)
+ // quantized KV scratch: 16*32*NSG (only when need_scratch)
const int64_t dv_pad = GGML_PAD(ne20, 64);
auto fa_smem = [&](int32_t nsg) -> size_t {
- const size_t smem_half = nqptg*(ne00 + 2*dv_pad + 4*ncpsg) + is_q*(16*32*nsg);
+ const size_t smem_half = nqptg*(ne00 + 2*dv_pad + 4*ncpsg) + need_scratch*(16*32*nsg);
return GGML_PAD(smem_half*sizeof(ggml_fp16_t), 16);
};
// simdgroups per threadgroup (a.k.a. warps)
int32_t nsg = ne00 >= 512 ? 8 : 4;
+ // the quantized KV scratch can exceed the threadgroup limit for large heads
+ if (fa_smem(nsg) > props_dev->max_theadgroup_memory_size) {
+ nsg = 4;
+ }
+
const size_t smem = fa_smem(nsg);
const int32_t ns10 = nb11_attn/nb10_attn;
diff --git a/ggml/src/ggml-metal/kernels/binbcast.metal b/ggml/src/ggml-metal/kernels/binbcast.metal
index 955739728..34e5cac38 100644
--- a/ggml/src/ggml-metal/kernels/binbcast.metal
+++ b/ggml/src/ggml-metal/kernels/binbcast.metal
@@ -171,6 +171,92 @@ template [[host_name("kernel_bin_fuse_f32_f32_f32_4")]] kernel kernel_bin_fuse_t
template [[host_name("kernel_bin_fuse_f16_f16_f16")]] kernel kernel_bin_fuse_t kernel_bin_fuse_impl<half, half, half>;
template [[host_name("kernel_bin_fuse_f16_f16_f16_4")]] kernel kernel_bin_fuse_t kernel_bin_fuse_impl<half4, half4, half4>;
+template<typename T1>
+kernel void kernel_bin_fuse_f8_e4m3(
+ constant ggml_metal_kargs_bin & args,
+ device const char * src0,
+ device const char * src1,
+ device char * dst,
+ uint3 tgpig[[threadgroup_position_in_grid]],
+ ushort3 tpitg[[thread_position_in_threadgroup]],
+ ushort3 ntg[[threads_per_threadgroup]]) {
+#define FC_OP FC_bin_op
+#define FC_F FC_bin_f
+#define FC_RB FC_bin_rb
+#define FC_CB FC_bin_cb
+
+ if (FC_RB) {
+ const uint i = tgpig.x*ntg.x + tpitg.x;
+ if (i >= (uint) args.ne0) {
+ return;
+ }
+
+ const uint i0 = tgpig.y*args.ne00 + i;
+ const uint i1 = FC_CB ? i%args.ne10 : i;
+
+ device const uint8_t * src0_row = (device const uint8_t *) (src0);
+ device uint8_t * dst_row = (device uint8_t *) (dst);
+
+ float x = f8_e4m3_to_fp32(src0_row[i0]);
+
+ FOR_UNROLL (short j = 0; j < FC_F; ++j) {
+ const float y = (float) ((device const T1 *) (src1 + args.o1[j]))[i1];
+
+ if (FC_OP == 0) { x += y; }
+ if (FC_OP == 1) { x -= y; }
+ if (FC_OP == 2) { x *= y; }
+ if (FC_OP == 3) { x /= y; }
+ }
+
+ dst_row[i0] = fp32_to_f8_e4m3(x);
+ } else {
+ const int i03 = tgpig.z;
+ const int i02 = tgpig.y;
+ const int i01 = tgpig.x;
+
+ if (i01 >= args.ne01) {
+ return;
+ }
+
+ const int i13 = i03%args.ne13;
+ const int i12 = i02%args.ne12;
+ const int i11 = i01%args.ne11;
+
+ device const uint8_t * src0_ptr = (device const uint8_t *) (src0 + i03*args.nb03 + i02*args.nb02 + i01*args.nb01 + args.offs);
+ device uint8_t * dst_ptr = (device uint8_t *) (dst + i03*args.nb3 + i02*args.nb2 + i01*args.nb1 + args.offs);
+
+ for (int i0 = tpitg.x; i0 < args.ne0; i0 += ntg.x) {
+ const int i10 = FC_CB ? i0%args.ne10 : i0;
+
+ float x = f8_e4m3_to_fp32(src0_ptr[i0]);
+
+ FOR_UNROLL (short j = 0; j < FC_F; ++j) {
+ const float y = (float) ((device const T1 *) (src1 + args.o1[j] + i13*args.nb13 + i12*args.nb12 + i11*args.nb11))[i10];
+
+ if (FC_OP == 0) { x += y; }
+ if (FC_OP == 1) { x -= y; }
+ if (FC_OP == 2) { x *= y; }
+ if (FC_OP == 3) { x /= y; }
+ }
+
+ dst_ptr[i0] = fp32_to_f8_e4m3(x);
+ }
+ }
+
+#undef FC_OP
+#undef FC_F
+#undef FC_RB
+#undef FC_CB
+}
+
+typedef decltype(kernel_bin_fuse_f8_e4m3<float>) kernel_bin_fuse_f8_e4m3_t;
+
+template [[host_name("kernel_bin_fuse_f8_e4m3_f32_f8_e4m3")]] kernel kernel_bin_fuse_f8_e4m3_t kernel_bin_fuse_f8_e4m3<float>;
+template [[host_name("kernel_bin_fuse_f8_e4m3_f16_f8_e4m3")]] kernel kernel_bin_fuse_f8_e4m3_t kernel_bin_fuse_f8_e4m3<half>;
+#if defined(GGML_METAL_HAS_BF16)
+template [[host_name("kernel_bin_fuse_f8_e4m3_bf16_f8_e4m3")]] kernel kernel_bin_fuse_f8_e4m3_t kernel_bin_fuse_f8_e4m3<bfloat>;
+#endif
+
kernel void kernel_add_id(
constant ggml_metal_kargs_add_id & args,
device const char * src0,
diff --git a/ggml/src/ggml-metal/kernels/common.h b/ggml/src/ggml-metal/kernels/common.h
index 51d447bfc..01052266d 100644
--- a/ggml/src/ggml-metal/kernels/common.h
+++ b/ggml/src/ggml-metal/kernels/common.h
@@ -81,6 +81,58 @@ static inline float ue4m3_to_fp32(uint8_t x) {
return e == 0 ? m*(1.0f/512.0f) : as_type<float>((uint32_t) (((e + 120) << 23) | (m << 20)));
}
+// OCP FP8 E4M3: signed, 4 exponent bits with bias 7, 3 mantissa bits
+static inline float f8_e4m3_to_fp32(uint8_t x) {
+ const uint8_t ax = x & 0x7F;
+ if (ax == 0x7F) {
+ return NAN;
+ }
+
+ const uint32_t exp = (ax >> 3) & 0xF;
+ const uint32_t man = ax & 0x7;
+ const float value = exp == 0
+ ? (float) man * (1.0f/512.0f)
+ : as_type<float>(((exp + 120) << 23) | (man << 20));
+
+ return as_type<float>(as_type<uint32_t>(value) | ((uint32_t) (x & 0x80) << 24));
+}
+
+static inline int f8_e4m3_round_to_nearest_even(float x) {
+ return (int) rint(x);
+}
+
+// saturating conversion only; infinities and overflow clamp to +/-448
+static inline uint8_t fp32_to_f8_e4m3(float x) {
+ const uint8_t sign = signbit(x) ? 0x80 : 0;
+ x = fabs(x);
+
+ if (isnan(x)) {
+ return 0x7F;
+ }
+ if (x == 0.0f) {
+ return sign;
+ }
+ if (isinf(x) || x >= 448.0f) {
+ return sign | 0x7E;
+ }
+ if (x < 0.015625f) {
+ return sign | (uint8_t) f8_e4m3_round_to_nearest_even(x * 512.0f);
+ }
+
+ int exp;
+ const float mantissa = frexp(x, exp) * 2.0f;
+ int encoded_exp = exp + 6;
+ int encoded_man = f8_e4m3_round_to_nearest_even((mantissa - 1.0f) * 8.0f);
+ if (encoded_man == 8) {
+ encoded_man = 0;
+ encoded_exp++;
+ }
+ if (encoded_exp > 15 || (encoded_exp == 15 && encoded_man > 6)) {
+ return sign | 0x7E;
+ }
+ return sign | (uint8_t) (encoded_exp << 3) | (uint8_t) encoded_man;
+}
+
static inline float dot(float x, float y) {
return x*y;
}
diff --git a/ggml/src/ggml-metal/kernels/dequantize.h b/ggml/src/ggml-metal/kernels/dequantize.h
index d99a48666..de11d2466 100644
--- a/ggml/src/ggml-metal/kernels/dequantize.h
+++ b/ggml/src/ggml-metal/kernels/dequantize.h
@@ -45,6 +45,37 @@ void dequantize_bf16_t4(device const bfloat4 * src, short il, thread type4 & reg
}
#endif
+#define QK_F8_E4M3 32
+typedef struct {
+ uint8_t qs[QK_F8_E4M3];
+} block_f8_e4m3;
+
+template <typename type4x4>
+void dequantize_f8_e4m3(device const block_f8_e4m3 * xb, short il, thread type4x4 & reg) {
+ device const uint8_t * q = xb->qs + 16*il;
+
+ float4x4 reg_f;
+
+ for (int i = 0; i < 4; ++i) {
+ reg_f[i][0] = f8_e4m3_to_fp32(q[4*i + 0]);
+ reg_f[i][1] = f8_e4m3_to_fp32(q[4*i + 1]);
+ reg_f[i][2] = f8_e4m3_to_fp32(q[4*i + 2]);
+ reg_f[i][3] = f8_e4m3_to_fp32(q[4*i + 3]);
+ }
+
+ reg = (type4x4) reg_f;
+}
+
+template <typename type4>
+void dequantize_f8_e4m3_t4(device const block_f8_e4m3 * xb, short il, thread type4 & reg) {
+ device const uint8_t * q = xb->qs + 4*il;
+
+ reg[0] = f8_e4m3_to_fp32(q[0]);
+ reg[1] = f8_e4m3_to_fp32(q[1]);
+ reg[2] = f8_e4m3_to_fp32(q[2]);
+ reg[3] = f8_e4m3_to_fp32(q[3]);
+}
+
template <typename type4x4>
void dequantize_q1_0(device const block_q1_0 * xb, short il, thread type4x4 & reg) {
device const uint8_t * qs = xb->qs;
diff --git a/ggml/src/ggml-metal/kernels/fa_f8_e4m3.metal b/ggml/src/ggml-metal/kernels/fa_f8_e4m3.metal
new file mode 100644
index 000000000..e3408bac6
--- /dev/null
+++ b/ggml/src/ggml-metal/kernels/fa_f8_e4m3.metal
@@ -0,0 +1,45 @@
+#include "common.h"
+#include "dequantize.h"
+#include "fa_common.metal"
+
+#define FA_TYPES \
+ half, \
+ half, \
+ half, \
+ float, \
+ float, \
+ float
+
+#define FA_TYPES_BF \
+ bfloat, \
+ bfloat, \
+ bfloat, \
+ float, \
+ float, \
+ half
+
+#define FA_TYPES_F32 \
+ half, \
+ float, \
+ float, \
+ float, \
+ float, \
+ float
+
+typedef decltype(kernel_flash_attn_ext<FA_TYPES, half4x4, 1, dequantize_f16, half4x4, 1, dequantize_f16, 64, 64>) flash_attn_ext_t;
+
+template [[host_name("kernel_flash_attn_ext_f8_e4m3_dk32_dv32" )]] kernel flash_attn_ext_t kernel_flash_attn_ext<FA_TYPES, block_f8_e4m3, 2, dequantize_f8_e4m3, block_f8_e4m3, 2, dequantize_f8_e4m3, 32, 32>;
+template [[host_name("kernel_flash_attn_ext_f8_e4m3_dk64_dv64" )]] kernel flash_attn_ext_t kernel_flash_attn_ext<FA_TYPES, block_f8_e4m3, 2, dequantize_f8_e4m3, block_f8_e4m3, 2, dequantize_f8_e4m3, 64, 64>;
+template [[host_name("kernel_flash_attn_ext_f8_e4m3_dk96_dv96" )]] kernel flash_attn_ext_t kernel_flash_attn_ext<FA_TYPES, block_f8_e4m3, 2, dequantize_f8_e4m3, block_f8_e4m3, 2, dequantize_f8_e4m3, 96, 96>;
+template [[host_name("kernel_flash_attn_ext_f8_e4m3_dk96_dv64" )]] kernel flash_attn_ext_t kernel_flash_attn_ext<FA_TYPES, block_f8_e4m3, 2, dequantize_f8_e4m3, block_f8_e4m3, 2, dequantize_f8_e4m3, 96, 64>;
+template [[host_name("kernel_flash_attn_ext_f8_e4m3_dk128_dv128")]] kernel flash_attn_ext_t kernel_flash_attn_ext<FA_TYPES, block_f8_e4m3, 2, dequantize_f8_e4m3, block_f8_e4m3, 2, dequantize_f8_e4m3, 128, 128>;
+template [[host_name("kernel_flash_attn_ext_f8_e4m3_dk128_dv96" )]] kernel flash_attn_ext_t kernel_flash_attn_ext<FA_TYPES, block_f8_e4m3, 2, dequantize_f8_e4m3, block_f8_e4m3, 2, dequantize_f8_e4m3, 128, 96>;
+template [[host_name("kernel_flash_attn_ext_f8_e4m3_dk192_dv192")]] kernel flash_attn_ext_t kernel_flash_attn_ext<FA_TYPES, block_f8_e4m3, 2, dequantize_f8_e4m3, block_f8_e4m3, 2, dequantize_f8_e4m3, 192, 192>;
+template [[host_name("kernel_flash_attn_ext_f8_e4m3_dk192_dv128")]] kernel flash_attn_ext_t kernel_flash_attn_ext<FA_TYPES, block_f8_e4m3, 2, dequantize_f8_e4m3, block_f8_e4m3, 2, dequantize_f8_e4m3, 192, 128>;
+template [[host_name("kernel_flash_attn_ext_f8_e4m3_dk256_dv256")]] kernel flash_attn_ext_t kernel_flash_attn_ext<FA_TYPES, block_f8_e4m3, 2, dequantize_f8_e4m3, block_f8_e4m3, 2, dequantize_f8_e4m3, 256, 256>;
+template [[host_name("kernel_flash_attn_ext_f8_e4m3_dk320_dv256")]] kernel flash_attn_ext_t kernel_flash_attn_ext<FA_TYPES, block_f8_e4m3, 2, dequantize_f8_e4m3, block_f8_e4m3, 2, dequantize_f8_e4m3, 320, 256>;
+template [[host_name("kernel_flash_attn_ext_f8_e4m3_dk512_dv512")]] kernel flash_attn_ext_t kernel_flash_attn_ext<FA_TYPES, block_f8_e4m3, 2, dequantize_f8_e4m3, block_f8_e4m3, 2, dequantize_f8_e4m3, 512, 512>;
+template [[host_name("kernel_flash_attn_ext_f8_e4m3_dk576_dv512")]] kernel flash_attn_ext_t kernel_flash_attn_ext<FA_TYPES, block_f8_e4m3, 2, dequantize_f8_e4m3, block_f8_e4m3, 2, dequantize_f8_e4m3, 576, 512>;
+#undef FA_TYPES
+#undef FA_TYPES_BF
+#undef FA_TYPES_F32
diff --git a/ggml/src/ggml-metal/kernels/fa_vec_f8_e4m3.metal b/ggml/src/ggml-metal/kernels/fa_vec_f8_e4m3.metal
new file mode 100644
index 000000000..1d7a141b6
--- /dev/null
+++ b/ggml/src/ggml-metal/kernels/fa_vec_f8_e4m3.metal
@@ -0,0 +1,36 @@
+#include "common.h"
+#include "dequantize.h"
+#include "fa_vec_common.metal"
+
+#define FA_TYPES \
+ half, \
+ half, \
+ half, \
+ float, \
+ float, \
+ float
+
+#define FA_TYPES_F32 \
+ half, \
+ float, \
+ float, \
+ float, \
+ float, \
+ float
+
+typedef decltype(kernel_flash_attn_ext_vec<FA_TYPES, half4, 1, dequantize_f16_t4, half4, 1, dequantize_f16_t4, 128, 128, 4>) flash_attn_ext_vec_t;
+
+template [[host_name("kernel_flash_attn_ext_vec_f8_e4m3_dk32_dv32")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, block_f8_e4m3, 8, dequantize_f8_e4m3_t4, block_f8_e4m3, 8, dequantize_f8_e4m3_t4, 32, 32, 4>;
+template [[host_name("kernel_flash_attn_ext_vec_f8_e4m3_dk64_dv64")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, block_f8_e4m3, 8, dequantize_f8_e4m3_t4, block_f8_e4m3, 8, dequantize_f8_e4m3_t4, 64, 64, 2>;
+template [[host_name("kernel_flash_attn_ext_vec_f8_e4m3_dk96_dv96")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, block_f8_e4m3, 8, dequantize_f8_e4m3_t4, block_f8_e4m3, 8, dequantize_f8_e4m3_t4, 96, 96, 4>;
+template [[host_name("kernel_flash_attn_ext_vec_f8_e4m3_dk96_dv64")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, block_f8_e4m3, 8, dequantize_f8_e4m3_t4, block_f8_e4m3, 8, dequantize_f8_e4m3_t4, 96, 64, 4>;
+template [[host_name("kernel_flash_attn_ext_vec_f8_e4m3_dk128_dv128")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, block_f8_e4m3, 8, dequantize_f8_e4m3_t4, block_f8_e4m3, 8, dequantize_f8_e4m3_t4, 128, 128, 1>;
+template [[host_name("kernel_flash_attn_ext_vec_f8_e4m3_dk128_dv96")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, block_f8_e4m3, 8, dequantize_f8_e4m3_t4, block_f8_e4m3, 8, dequantize_f8_e4m3_t4, 128, 96, 4>;
+template [[host_name("kernel_flash_attn_ext_vec_f8_e4m3_dk192_dv192")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, block_f8_e4m3, 8, dequantize_f8_e4m3_t4, block_f8_e4m3, 8, dequantize_f8_e4m3_t4, 192, 192, 2>;
+template [[host_name("kernel_flash_attn_ext_vec_f8_e4m3_dk192_dv128")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, block_f8_e4m3, 8, dequantize_f8_e4m3_t4, block_f8_e4m3, 8, dequantize_f8_e4m3_t4, 192, 128, 2>;
+template [[host_name("kernel_flash_attn_ext_vec_f8_e4m3_dk256_dv256")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, block_f8_e4m3, 8, dequantize_f8_e4m3_t4, block_f8_e4m3, 8, dequantize_f8_e4m3_t4, 256, 256, 1>;
+template [[host_name("kernel_flash_attn_ext_vec_f8_e4m3_dk320_dv256")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, block_f8_e4m3, 8, dequantize_f8_e4m3_t4, block_f8_e4m3, 8, dequantize_f8_e4m3_t4, 320, 256, 2>;
+template [[host_name("kernel_flash_attn_ext_vec_f8_e4m3_dk512_dv512")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, block_f8_e4m3, 8, dequantize_f8_e4m3_t4, block_f8_e4m3, 8, dequantize_f8_e4m3_t4, 512, 512, 1>;
+template [[host_name("kernel_flash_attn_ext_vec_f8_e4m3_dk576_dv512")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, block_f8_e4m3, 8, dequantize_f8_e4m3_t4, block_f8_e4m3, 8, dequantize_f8_e4m3_t4, 576, 512, 2>;
+#undef FA_TYPES
+#undef FA_TYPES_F32
diff --git a/ggml/src/ggml-metal/kernels/mul_mm.metal b/ggml/src/ggml-metal/kernels/mul_mm.metal
index 1d47b9166..e88648c9d 100644
--- a/ggml/src/ggml-metal/kernels/mul_mm.metal
+++ b/ggml/src/ggml-metal/kernels/mul_mm.metal
@@ -9,6 +9,28 @@ constant short FC_mul_mm_r2 [[function_constant(FC_MUL_MM + 4)]];
constant short FC_mul_mm_r3 [[function_constant(FC_MUL_MM + 5)]];
constant int32_t FC_mul_mm_id_map0_ne02 [[function_constant(FC_MUL_MM + 7)]];
+template<
+ typename S0, typename block_q, short nl,
+ void (*dequantize_func)(device const block_q *, short, thread typename value_type_4x4<S0>::type &),
+ typename T0, typename T1>
+struct mul_mm_dequant {
+ static inline void apply(device const block_q * x, short il, thread typename value_type_4x4<S0>::type & reg, threadgroup const half * lut) {
+ dequantize_func(x, il, reg);
+ }
+};
+
+template<short nl, void (*dequantize_func)(device const block_f8_e4m3 *, short, thread half4x4 &), typename T0, typename T1>
+struct mul_mm_dequant<half, block_f8_e4m3, nl, dequantize_func, T0, T1> {
+ static inline void apply(device const block_f8_e4m3 * x, short il, thread half4x4 & reg, threadgroup const half * lut) {
+ device const uint8_t * q = x->qs + 16*il;
+ for (int i = 0; i < 4; ++i) {
+ for (int j = 0; j < 4; ++j) {
+ reg[i][j] = lut[q[4*i + j]];
+ }
+ }
+ }
+};
+
// each block_q contains 16*nl weights
template<
@@ -42,6 +64,18 @@ kernel void kernel_mul_mm(
threadgroup S0 * sa = (threadgroup S0 *)(shmem);
threadgroup S1 * sb = (threadgroup S1 *)(shmem + NR0*NK*sizeof(S0));
+ constexpr bool use_lut = is_same<S0, half>::value && is_same<block_q, block_f8_e4m3>::value;
+
+ const size_t smem_lut = FC_mul_mm_bc_out ? 8192 : (NR0*NK*sizeof(S0) + NR1*NK*sizeof(S1));
+ threadgroup half * lut = (threadgroup half *)(shmem + smem_lut);
+
+ if (use_lut) {
+ for (int i = tiitg; i < 256; i += 128) {
+ lut[i] = (half) f8_e4m3_to_fp32(i);
+ }
+ threadgroup_barrier(mem_flags::mem_threadgroup);
+ }
+
const int im = tgpig.z;
const int r0 = tgpig.y*NR0;
const int r1 = tgpig.x*NR1;
@@ -104,7 +138,7 @@ kernel void kernel_mul_mm(
}
} else {
S0_4x4 temp_a;
- dequantize_func(x, il, temp_a);
+ mul_mm_dequant<S0, block_q, nl, dequantize_func, T0, T1>::apply(x, il, temp_a, lut);
threadgroup_barrier(mem_flags::mem_threadgroup);
@@ -678,6 +712,7 @@ template [[host_name("kernel_mul_mm_q5_1_f32_prec_bf16")]] kernel mul_mm_t ke
template [[host_name("kernel_mul_mm_q8_0_f32_prec_bf16")]] kernel mul_mm_t kernel_mul_mm<bfloat, bfloat, block_q8_0, 2, dequantize_q8_0, float, float>;
template [[host_name("kernel_mul_mm_mxfp4_f32_prec_bf16")]] kernel mul_mm_t kernel_mul_mm<bfloat, bfloat, block_mxfp4, 2, dequantize_mxfp4, float, float>;
template [[host_name("kernel_mul_mm_nvfp4_f32_prec_bf16")]] kernel mul_mm_t kernel_mul_mm<bfloat, bfloat, block_nvfp4, 4, dequantize_nvfp4, float, float>;
+template [[host_name("kernel_mul_mm_f8_e4m3_f32_prec_bf16")]] kernel mul_mm_t kernel_mul_mm<bfloat, bfloat, block_f8_e4m3, 2, dequantize_f8_e4m3, float, float>;
template [[host_name("kernel_mul_mm_q2_K_f32_prec_bf16")]] kernel mul_mm_t kernel_mul_mm<bfloat, bfloat, block_q2_K, QK_NL, dequantize_q2_K, float, float>;
template [[host_name("kernel_mul_mm_q3_K_f32_prec_bf16")]] kernel mul_mm_t kernel_mul_mm<bfloat, bfloat, block_q3_K, QK_NL, dequantize_q3_K, float, float>;
template [[host_name("kernel_mul_mm_q4_K_f32_prec_bf16")]] kernel mul_mm_t kernel_mul_mm<bfloat, bfloat, block_q4_K, QK_NL, dequantize_q4_K, float, float>;
@@ -705,6 +740,7 @@ template [[host_name("kernel_mul_mm_q5_1_f32_prec_f32")]] kernel mul_mm_t ker
template [[host_name("kernel_mul_mm_q8_0_f32_prec_f32")]] kernel mul_mm_t kernel_mul_mm<float, float, block_q8_0, 2, dequantize_q8_0, float, float>;
template [[host_name("kernel_mul_mm_mxfp4_f32_prec_f32")]] kernel mul_mm_t kernel_mul_mm<float, float, block_mxfp4, 2, dequantize_mxfp4, float, float>;
template [[host_name("kernel_mul_mm_nvfp4_f32_prec_f32")]] kernel mul_mm_t kernel_mul_mm<float, float, block_nvfp4, 4, dequantize_nvfp4, float, float>;
+template [[host_name("kernel_mul_mm_f8_e4m3_f32_prec_f32")]] kernel mul_mm_t kernel_mul_mm<float, float, block_f8_e4m3, 2, dequantize_f8_e4m3, float, float>;
template [[host_name("kernel_mul_mm_q2_K_f32_prec_f32")]] kernel mul_mm_t kernel_mul_mm<float, float, block_q2_K, QK_NL, dequantize_q2_K, float, float>;
template [[host_name("kernel_mul_mm_q3_K_f32_prec_f32")]] kernel mul_mm_t kernel_mul_mm<float, float, block_q3_K, QK_NL, dequantize_q3_K, float, float>;
template [[host_name("kernel_mul_mm_q4_K_f32_prec_f32")]] kernel mul_mm_t kernel_mul_mm<float, float, block_q4_K, QK_NL, dequantize_q4_K, float, float>;
@@ -735,6 +771,10 @@ template [[host_name("kernel_mul_mm_q5_1_f32")]] kernel mul_mm_t kernel_mul_m
template [[host_name("kernel_mul_mm_q8_0_f32")]] kernel mul_mm_t kernel_mul_mm<half, half, block_q8_0, 2, dequantize_q8_0, float, float>;
template [[host_name("kernel_mul_mm_mxfp4_f32")]] kernel mul_mm_t kernel_mul_mm<half, half, block_mxfp4, 2, dequantize_mxfp4, float, float>;
template [[host_name("kernel_mul_mm_nvfp4_f32")]] kernel mul_mm_t kernel_mul_mm<half, half, block_nvfp4, 4, dequantize_nvfp4, float, float>;
+template [[host_name("kernel_mul_mm_f8_e4m3_f32")]] kernel mul_mm_t kernel_mul_mm<half, half, block_f8_e4m3, 2, dequantize_f8_e4m3, float, float>;
+#if defined(GGML_METAL_HAS_BF16)
+template [[host_name("kernel_mul_mm_f8_e4m3_bf16")]] kernel mul_mm_t kernel_mul_mm<bfloat, bfloat, block_f8_e4m3, 2, dequantize_f8_e4m3, float, bfloat>;
+#endif
template [[host_name("kernel_mul_mm_q2_K_f32")]] kernel mul_mm_t kernel_mul_mm<half, half, block_q2_K, QK_NL, dequantize_q2_K, float, float>;
template [[host_name("kernel_mul_mm_q3_K_f32")]] kernel mul_mm_t kernel_mul_mm<half, half, block_q3_K, QK_NL, dequantize_q3_K, float, float>;
template [[host_name("kernel_mul_mm_q4_K_f32")]] kernel mul_mm_t kernel_mul_mm<half, half, block_q4_K, QK_NL, dequantize_q4_K, float, float>;
@@ -762,6 +802,7 @@ template [[host_name("kernel_mul_mm_q5_1_f16")]] kernel mul_mm_t kernel_mul_m
template [[host_name("kernel_mul_mm_q8_0_f16")]] kernel mul_mm_t kernel_mul_mm<half, half, block_q8_0, 2, dequantize_q8_0, float, half>;
template [[host_name("kernel_mul_mm_mxfp4_f16")]] kernel mul_mm_t kernel_mul_mm<half, half, block_mxfp4, 2, dequantize_mxfp4, float, half>;
template [[host_name("kernel_mul_mm_nvfp4_f16")]] kernel mul_mm_t kernel_mul_mm<half, half, block_nvfp4, 4, dequantize_nvfp4, float, half>;
+template [[host_name("kernel_mul_mm_f8_e4m3_f16")]] kernel mul_mm_t kernel_mul_mm<half, half, block_f8_e4m3, 2, dequantize_f8_e4m3, float, half>;
template [[host_name("kernel_mul_mm_q2_K_f16")]] kernel mul_mm_t kernel_mul_mm<half, half, block_q2_K, QK_NL, dequantize_q2_K, float, half>;
template [[host_name("kernel_mul_mm_q3_K_f16")]] kernel mul_mm_t kernel_mul_mm<half, half, block_q3_K, QK_NL, dequantize_q3_K, float, half>;
template [[host_name("kernel_mul_mm_q4_K_f16")]] kernel mul_mm_t kernel_mul_mm<half, half, block_q4_K, QK_NL, dequantize_q4_K, float, half>;
@@ -797,11 +838,16 @@ template [[host_name("kernel_mul_mm_id_q5_0_f32")]] kernel mul_mm_id kernel_m
template [[host_name("kernel_mul_mm_id_q5_1_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half, block_q5_1, 2, dequantize_q5_1, float, float>;
template [[host_name("kernel_mul_mm_id_q8_0_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half, block_q8_0, 2, dequantize_q8_0, float, float>;
#if defined(GGML_METAL_HAS_BF16)
-template [[host_name("kernel_mul_mm_id_mxfp4_f32")]] kernel mul_mm_id kernel_mul_mm_id<bfloat, bfloat, block_mxfp4, 2, dequantize_mxfp4, float, float>;
-template [[host_name("kernel_mul_mm_id_nvfp4_f32")]] kernel mul_mm_id kernel_mul_mm_id<bfloat, bfloat, block_nvfp4, 4, dequantize_nvfp4, float, float>;
+template [[host_name("kernel_mul_mm_id_mxfp4_f32")]] kernel mul_mm_id kernel_mul_mm_id<bfloat, bfloat, block_mxfp4, 2, dequantize_mxfp4, float, float>;
+template [[host_name("kernel_mul_mm_id_nvfp4_f32")]] kernel mul_mm_id kernel_mul_mm_id<bfloat, bfloat, block_nvfp4, 4, dequantize_nvfp4, float, float>;
+template [[host_name("kernel_mul_mm_id_f8_e4m3_f32")]] kernel mul_mm_id kernel_mul_mm_id<bfloat, bfloat, block_f8_e4m3, 2, dequantize_f8_e4m3, float, float>;
+template [[host_name("kernel_mul_mm_id_f8_e4m3_f16")]] kernel mul_mm_id kernel_mul_mm_id<bfloat, bfloat, block_f8_e4m3, 2, dequantize_f8_e4m3, float, half>;
+template [[host_name("kernel_mul_mm_id_f8_e4m3_bf16")]] kernel mul_mm_id kernel_mul_mm_id<bfloat, bfloat, block_f8_e4m3, 2, dequantize_f8_e4m3, float, bfloat>;
#else
-template [[host_name("kernel_mul_mm_id_mxfp4_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half, block_mxfp4, 2, dequantize_mxfp4, float, float>;
-template [[host_name("kernel_mul_mm_id_nvfp4_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half, block_nvfp4, 4, dequantize_nvfp4, float, float>;
+template [[host_name("kernel_mul_mm_id_mxfp4_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half, block_mxfp4, 2, dequantize_mxfp4, float, float>;
+template [[host_name("kernel_mul_mm_id_nvfp4_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half, block_nvfp4, 4, dequantize_nvfp4, float, float>;
+template [[host_name("kernel_mul_mm_id_f8_e4m3_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half, block_f8_e4m3, 2, dequantize_f8_e4m3, float, float>;
+template [[host_name("kernel_mul_mm_id_f8_e4m3_f16")]] kernel mul_mm_id kernel_mul_mm_id<half, half, block_f8_e4m3, 2, dequantize_f8_e4m3, float, half>;
#endif
template [[host_name("kernel_mul_mm_id_q2_K_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half, block_q2_K, QK_NL, dequantize_q2_K, float, float>;
template [[host_name("kernel_mul_mm_id_q3_K_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half, block_q3_K, QK_NL, dequantize_q3_K, float, float>;
@@ -859,6 +905,7 @@ template [[host_name("kernel_mul_mm_id_q5_0_f32_prec_f32")]] kernel mul_mm_id
template [[host_name("kernel_mul_mm_id_q5_1_f32_prec_f32")]] kernel mul_mm_id kernel_mul_mm_id<float, float, block_q5_1, 2, dequantize_q5_1, float, float>;
template [[host_name("kernel_mul_mm_id_q8_0_f32_prec_f32")]] kernel mul_mm_id kernel_mul_mm_id<float, float, block_q8_0, 2, dequantize_q8_0, float, float>;
template [[host_name("kernel_mul_mm_id_mxfp4_f32_prec_f32")]] kernel mul_mm_id kernel_mul_mm_id<float, float, block_mxfp4, 2, dequantize_mxfp4, float, float>;
+template [[host_name("kernel_mul_mm_id_f8_e4m3_f32_prec_f32")]]kernel mul_mm_id kernel_mul_mm_id<float, float, block_f8_e4m3, 2, dequantize_f8_e4m3, float, float>;
template [[host_name("kernel_mul_mm_id_nvfp4_f32_prec_f32")]] kernel mul_mm_id kernel_mul_mm_id<float, float, block_nvfp4, 4, dequantize_nvfp4, float, float>;
template [[host_name("kernel_mul_mm_id_q2_K_f32_prec_f32")]] kernel mul_mm_id kernel_mul_mm_id<float, float, block_q2_K, QK_NL, dequantize_q2_K, float, float>;
template [[host_name("kernel_mul_mm_id_q3_K_f32_prec_f32")]] kernel mul_mm_id kernel_mul_mm_id<float, float, block_q3_K, QK_NL, dequantize_q3_K, float, float>;
diff --git a/ggml/src/ggml-metal/kernels/mul_mm_tensor.metal b/ggml/src/ggml-metal/kernels/mul_mm_tensor.metal
index 2ccce5f41..f2540381c 100644
--- a/ggml/src/ggml-metal/kernels/mul_mm_tensor.metal
+++ b/ggml/src/ggml-metal/kernels/mul_mm_tensor.metal
@@ -12,6 +12,28 @@ constant short FC_mul_mm_r3 [[function_constant(FC_MUL_MM + 5)]];
constant int32_t FC_mul_mm_id_map0_ne02 [[function_constant(FC_MUL_MM + 7)]];
// each block_q contains 16*nl weights
+template<
+ typename SA, typename block_q, short nl,
+ void (*dequantize_func)(device const block_q *, short, thread typename value_type_4x4<SA>::type &),
+ typename T0, typename T1>
+struct mul_mm_tensor_dequant {
+ static inline void apply(device const block_q * x, short il, thread typename value_type_4x4<SA>::type & reg, threadgroup const SA * lut) {
+ dequantize_func(x, il, reg);
+ }
+};
+
+template<typename SA, short nl, void (*dequantize_func)(device const block_f8_e4m3 *, short, thread typename value_type_4x4<SA>::type &), typename T0, typename T1>
+struct mul_mm_tensor_dequant<SA, block_f8_e4m3, nl, dequantize_func, T0, T1> {
+ static inline void apply(device const block_f8_e4m3 * x, short il, thread typename value_type_4x4<SA>::type & reg, threadgroup const SA * lut) {
+ device const uint8_t * q = x->qs + 16*il;
+ for (int i = 0; i < 4; ++i) {
+ for (int j = 0; j < 4; ++j) {
+ reg[i][j] = lut[q[4*i + j]];
+ }
+ }
+ }
+};
+
template<
typename SA, typename SB,
typename block_q, short nl, void (*dequantize_func)(device const block_q *, short, thread typename value_type_4x4<SA>::type &),
@@ -62,6 +84,20 @@ kernel void kernel_mul_mm_tensor(
constexpr int A_WORK_ITEMS = NRA * N_MM_NK;
constexpr int NUM_THREADS = N_SIMDWIDTH * N_MM_SIMD_GROUP_X * N_MM_SIMD_GROUP_Y;
+#if defined(GGML_METAL_HAS_BF16)
+ constexpr bool use_lut = is_same<block_q, block_f8_e4m3>::value && (is_same<SA, half>::value || is_same<SA, bfloat>::value);
+#else
+ constexpr bool use_lut = is_same<SA, half>::value && is_same<block_q, block_f8_e4m3>::value;
+#endif
+
+ threadgroup SA * lut = (threadgroup SA *)(shmem + NRA*N_MM_NK_TOTAL*sizeof(SA));
+ if (use_lut) {
+ for (int i = tiitg; i < 256; i += NUM_THREADS) {
+ lut[i] = (SA) f8_e4m3_to_fp32(i);
+ }
+ threadgroup_barrier(mem_flags::mem_threadgroup);
+ }
+
// tA wraps threadgroup memory
auto tA = tensor(sa, dextents<int32_t, 2>(N_MM_NK_TOTAL, NRA));
@@ -110,7 +146,7 @@ kernel void kernel_mul_mm_tensor(
device const block_q * row_ptr = (device const block_q *)(srcA + args.nb01 * (ra + row) + offset0);
SA_4x4 temp_a;
- dequantize_func(row_ptr + block_idx, il, temp_a);
+ mul_mm_tensor_dequant<SA, block_q, nl, dequantize_func, T0, T1>::apply(row_ptr + block_idx, il, temp_a, lut);
FOR_UNROLL (short i = 0; i < 16; i++) {
// Zero-pad A for K positions beyond valid range (handles partial K iterations)
@@ -184,6 +220,20 @@ kernel void kernel_mul_mm_id_tensor(
threadgroup float * sc = (threadgroup float *)(shmem);
+#if defined(GGML_METAL_HAS_BF16)
+ constexpr bool use_lut = is_same<block_q, block_f8_e4m3>::value && (is_same<S0, half>::value || is_same<S0, bfloat>::value);
+#else
+ constexpr bool use_lut = is_same<S0, half>::value && is_same<block_q, block_f8_e4m3>::value;
+#endif
+
+ threadgroup S0 * lut = (threadgroup S0 *)(shmem + NR0*NK*sizeof(S0) + NR1*NK*sizeof(S1));
+ if (use_lut) {
+ for (int i = tiitg; i < 256; i += N_SIMDWIDTH*4) {
+ lut[i] = (S0) f8_e4m3_to_fp32(i);
+ }
+ threadgroup_barrier(mem_flags::mem_threadgroup);
+ }
+
const int tile = tgpig.x; // entry tile, described by the prepass
const int r0 = tgpig.y*NR0;
@@ -272,7 +322,7 @@ kernel void kernel_mul_mm_id_tensor(
}
} else {
S0_4x4 temp_a;
- dequantize_func(x, il, temp_a);
+ mul_mm_tensor_dequant<S0, block_q, nl, dequantize_func, T0, T1>::apply(x, il, temp_a, lut);
threadgroup_barrier(mem_flags::mem_threadgroup);
@@ -384,6 +434,7 @@ template [[host_name("kernel_mul_mm_tensor_f32_f32_prec_bf16")]] kernel mul_mm_t
#if defined(GGML_METAL_HAS_BF16)
template [[host_name("kernel_mul_mm_tensor_bf16_f32")]] kernel mul_mm_tensor_t kernel_mul_mm_tensor<bfloat, bfloat, bfloat4x4, 1, dequantize_bf16, bfloat, float>;
template [[host_name("kernel_mul_mm_tensor_bf16_bf16")]] kernel mul_mm_tensor_t kernel_mul_mm_tensor<bfloat, bfloat, bfloat4x4, 1, dequantize_bf16, bfloat, bfloat>;
+template [[host_name("kernel_mul_mm_tensor_f8_e4m3_bf16")]]kernel mul_mm_tensor_t kernel_mul_mm_tensor<bfloat, bfloat, block_f8_e4m3, 2, dequantize_f8_e4m3, float, bfloat>;
#endif
template [[host_name("kernel_mul_mm_tensor_q1_0_f32")]] kernel mul_mm_tensor_t kernel_mul_mm_tensor<half, half, block_q1_0, 8, dequantize_q1_0, float, float>;
template [[host_name("kernel_mul_mm_tensor_q2_0_f32")]] kernel mul_mm_tensor_t kernel_mul_mm_tensor<half, half, block_q2_0, 4, dequantize_q2_0, float, float>;
@@ -392,6 +443,7 @@ template [[host_name("kernel_mul_mm_tensor_q4_1_f32")]] kernel mul_mm_tensor_
template [[host_name("kernel_mul_mm_tensor_q5_0_f32")]] kernel mul_mm_tensor_t kernel_mul_mm_tensor<half, half, block_q5_0, 2, dequantize_q5_0, float, float>;
template [[host_name("kernel_mul_mm_tensor_q5_1_f32")]] kernel mul_mm_tensor_t kernel_mul_mm_tensor<half, half, block_q5_1, 2, dequantize_q5_1, float, float>;
template [[host_name("kernel_mul_mm_tensor_q8_0_f32")]] kernel mul_mm_tensor_t kernel_mul_mm_tensor<half, half, block_q8_0, 2, dequantize_q8_0, float, float>;
+template [[host_name("kernel_mul_mm_tensor_f8_e4m3_f32")]] kernel mul_mm_tensor_t kernel_mul_mm_tensor<half, half, block_f8_e4m3, 2, dequantize_f8_e4m3, float, float>;
template [[host_name("kernel_mul_mm_tensor_mxfp4_f32")]] kernel mul_mm_tensor_t kernel_mul_mm_tensor<half, half, block_mxfp4, 2, dequantize_mxfp4, float, float>;
template [[host_name("kernel_mul_mm_tensor_nvfp4_f32")]] kernel mul_mm_tensor_t kernel_mul_mm_tensor<half, half, block_nvfp4, 4, dequantize_nvfp4, float, float>;
template [[host_name("kernel_mul_mm_tensor_q2_K_f32")]] kernel mul_mm_tensor_t kernel_mul_mm_tensor<half, half, block_q2_K, QK_NL, dequantize_q2_K, float, float>;
@@ -419,6 +471,7 @@ template [[host_name("kernel_mul_mm_tensor_q4_1_f16")]] kernel mul_mm_tensor_
template [[host_name("kernel_mul_mm_tensor_q5_0_f16")]] kernel mul_mm_tensor_t kernel_mul_mm_tensor<half, half, block_q5_0, 2, dequantize_q5_0, float, half>;
template [[host_name("kernel_mul_mm_tensor_q5_1_f16")]] kernel mul_mm_tensor_t kernel_mul_mm_tensor<half, half, block_q5_1, 2, dequantize_q5_1, float, half>;
template [[host_name("kernel_mul_mm_tensor_q8_0_f16")]] kernel mul_mm_tensor_t kernel_mul_mm_tensor<half, half, block_q8_0, 2, dequantize_q8_0, float, half>;
+template [[host_name("kernel_mul_mm_tensor_f8_e4m3_f16")]] kernel mul_mm_tensor_t kernel_mul_mm_tensor<half, half, block_f8_e4m3, 2, dequantize_f8_e4m3, float, half>;
template [[host_name("kernel_mul_mm_tensor_mxfp4_f16")]] kernel mul_mm_tensor_t kernel_mul_mm_tensor<half, half, block_mxfp4, 2, dequantize_mxfp4, float, half>;
template [[host_name("kernel_mul_mm_tensor_nvfp4_f16")]] kernel mul_mm_tensor_t kernel_mul_mm_tensor<half, half, block_nvfp4, 4, dequantize_nvfp4, float, half>;
template [[host_name("kernel_mul_mm_tensor_q2_K_f16")]] kernel mul_mm_tensor_t kernel_mul_mm_tensor<half, half, block_q2_K, QK_NL, dequantize_q2_K, float, half>;
@@ -455,6 +508,7 @@ template [[host_name("kernel_mul_mm_id_tensor_q4_1_f32")]] kernel mul_mm_id_t
template [[host_name("kernel_mul_mm_id_tensor_q5_0_f32")]] kernel mul_mm_id_tensor_t kernel_mul_mm_id_tensor<half, half, block_q5_0, 2, dequantize_q5_0, float, float>;
template [[host_name("kernel_mul_mm_id_tensor_q5_1_f32")]] kernel mul_mm_id_tensor_t kernel_mul_mm_id_tensor<half, half, block_q5_1, 2, dequantize_q5_1, float, float>;
template [[host_name("kernel_mul_mm_id_tensor_q8_0_f32")]] kernel mul_mm_id_tensor_t kernel_mul_mm_id_tensor<half, half, block_q8_0, 2, dequantize_q8_0, float, float>;
+template [[host_name("kernel_mul_mm_id_tensor_f8_e4m3_f32")]] kernel mul_mm_id_tensor_t kernel_mul_mm_id_tensor<half, half, block_f8_e4m3, 2, dequantize_f8_e4m3, float, float>;
#if defined(GGML_METAL_HAS_BF16)
template [[host_name("kernel_mul_mm_id_tensor_mxfp4_f32")]] kernel mul_mm_id_tensor_t kernel_mul_mm_id_tensor<bfloat, bfloat, block_mxfp4, 2, dequantize_mxfp4, float, float>;
template [[host_name("kernel_mul_mm_id_tensor_nvfp4_f32")]] kernel mul_mm_id_tensor_t kernel_mul_mm_id_tensor<bfloat, bfloat, block_nvfp4, 4, dequantize_nvfp4, float, float>;
@@ -517,6 +571,7 @@ template [[host_name("kernel_mul_mm_id_tensor_q4_1_f16")]] kernel mul_mm_id_t
template [[host_name("kernel_mul_mm_id_tensor_q5_0_f16")]] kernel mul_mm_id_tensor_t kernel_mul_mm_id_tensor<half, half, block_q5_0, 2, dequantize_q5_0, float, half>;
template [[host_name("kernel_mul_mm_id_tensor_q5_1_f16")]] kernel mul_mm_id_tensor_t kernel_mul_mm_id_tensor<half, half, block_q5_1, 2, dequantize_q5_1, float, half>;
template [[host_name("kernel_mul_mm_id_tensor_q8_0_f16")]] kernel mul_mm_id_tensor_t kernel_mul_mm_id_tensor<half, half, block_q8_0, 2, dequantize_q8_0, float, half>;
+template [[host_name("kernel_mul_mm_id_tensor_f8_e4m3_f16")]] kernel mul_mm_id_tensor_t kernel_mul_mm_id_tensor<half, half, block_f8_e4m3, 2, dequantize_f8_e4m3, float, half>;
#if defined(GGML_METAL_HAS_BF16)
template [[host_name("kernel_mul_mm_id_tensor_mxfp4_f16")]] kernel mul_mm_id_tensor_t kernel_mul_mm_id_tensor<bfloat, bfloat, block_mxfp4, 2, dequantize_mxfp4, float, half>;
template [[host_name("kernel_mul_mm_id_tensor_nvfp4_f16")]] kernel mul_mm_id_tensor_t kernel_mul_mm_id_tensor<bfloat, bfloat, block_nvfp4, 4, dequantize_nvfp4, float, half>;
@@ -540,4 +595,8 @@ template [[host_name("kernel_mul_mm_id_tensor_iq4_nl_f16")]] kernel mul_mm_id_t
template [[host_name("kernel_mul_mm_id_tensor_iq4_xs_f16")]] kernel mul_mm_id_tensor_t kernel_mul_mm_id_tensor<half, half, block_iq4_xs, QK_NL, dequantize_iq4_xs, float, half>;
template [[host_name("kernel_mul_mm_id_tensor_tq2_0_f16")]] kernel mul_mm_id_tensor_t kernel_mul_mm_id_tensor<half, half, block_tq2_0, QK_NL, dequantize_tq2_0, float, half>;
+#if defined(GGML_METAL_HAS_BF16)
+template [[host_name("kernel_mul_mm_id_tensor_f8_e4m3_bf16")]] kernel mul_mm_id_tensor_t kernel_mul_mm_id_tensor<bfloat, bfloat, block_f8_e4m3, 2, dequantize_f8_e4m3, float, bfloat>;
+#endif
+
#endif // GGML_METAL_HAS_TENSOR
diff --git a/ggml/src/ggml-metal/kernels/mul_mv.metal b/ggml/src/ggml-metal/kernels/mul_mv.metal
index 08873502e..f0081dd8e 100644
--- a/ggml/src/ggml-metal/kernels/mul_mv.metal
+++ b/ggml/src/ggml-metal/kernels/mul_mv.metal
@@ -1179,6 +1179,115 @@ template [[host_name("kernel_mul_mv_bf16_bf16_4")]] kernel mul_mv_t_t_4 kernel_m
template [[host_name("kernel_mul_mv_f32_bf16_4")]] kernel mul_mv_t_t_4 kernel_mul_mv_t_t_4<float, bfloat>;
#endif
+template<short NR0, typename T1, typename args_t>
+void kernel_mul_mv_f8_e4m3_impl(
+ args_t args,
+ device const char * src0,
+ device const char * src1,
+ device char * dst,
+ threadgroup char * shmem,
+ uint3 tgpig,
+ ushort tiisg,
+ ushort sgitg) {
+ const short NSG = FC_mul_mv_nsg;
+
+ constexpr short NW = N_SIMDWIDTH;
+ constexpr short NB = 32;
+ constexpr short NF = 8;
+
+ threadgroup float * lut = (threadgroup float *) shmem;
+ for (int i = tiisg + sgitg*NW; i < 256; i += NSG*NW) {
+ lut[i] = f8_e4m3_to_fp32((uint8_t) i);
+ }
+ threadgroup_barrier(mem_flags::mem_threadgroup);
+
+ const int nb = args.ne00/NB;
+
+ const int r0 = tgpig.x*NR0;
+ const int r1 = tgpig.y;
+ const int im = tgpig.z;
+
+ const uint i12 = im%FC_mul_mv_ne12;
+ const uint i13 = im/FC_mul_mv_ne12;
+
+ const uint64_t offset1 = r1*args.nb11 + (i12)*args.nb12 + (i13)*args.nb13;
+
+ device const T1 * y = (device const T1 *) (src1 + offset1);
+
+ device const uint8_t * ax[NR0];
+ FOR_UNROLL (short row = 0; row < NR0; ++row) {
+ const uint64_t offset0 = (r0 + row)*args.nb01 + (i12/FC_mul_mv_r2)*args.nb02 + (i13/FC_mul_mv_r3)*args.nb03;
+ ax[row] = (device const uint8_t *) ((device char *) src0 + offset0);
+ }
+
+ float sumf[NR0] = { 0.f };
+
+ const short ix = tiisg/(NW/NF);
+ const short il = tiisg%(NW/NF);
+
+ const int ib0 = sgitg*NF + ix;
+
+ for (int ib = ib0; ib < nb; ib += NSG*NF) {
+ for (short row = 0; row < NR0; row++) {
+ device const uint8_t * xb = ax[row] + ib*NB + il*NF;
+ device const T1 * yb = y + ib*NB + il*NF;
+
+ float sumq = 0.f;
+ FOR_UNROLL (short i = 0; i < NF; ++i) {
+ sumq += lut[xb[i]] * (float) yb[i];
+ }
+
+ sumf[row] += sumq;
+ }
+ }
+
+ for (int i = nb*NB + sgitg*NW + tiisg; i < args.ne00; i += NW*NSG) {
+ for (short row = 0; row < NR0; row++) {
+ sumf[row] += lut[ax[row][i]] * (float) y[i];
+ }
+ }
+
+ device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0;
+
+ helper_mv_reduce_and_write<NR0>(dst_f32, sumf, r0, args.ne01, tiisg, sgitg, shmem + 256*sizeof(float));
+}
+
+template<typename T1, typename args_t>
+void kernel_mul_mv_f8_e4m3_disp(
+ args_t args,
+ device const char * src0,
+ device const char * src1,
+ device char * dst,
+ threadgroup char * shmem,
+ uint3 tgpig,
+ ushort tiisg,
+ ushort sgitg) {
+ switch (args.nr0) {
+ case 2: kernel_mul_mv_f8_e4m3_impl<2, T1, args_t>(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg); break;
+ }
+}
+
+template<typename T1>
+kernel void kernel_mul_mv_f8_e4m3(
+ constant ggml_metal_kargs_mul_mv & args,
+ device const char * src0,
+ device const char * src1,
+ device char * dst,
+ threadgroup char * shmem [[threadgroup(0)]],
+ uint3 tgpig[[threadgroup_position_in_grid]],
+ ushort tiisg[[thread_index_in_simdgroup]],
+ ushort sgitg[[simdgroup_index_in_threadgroup]]) {
+ kernel_mul_mv_f8_e4m3_disp<T1, constant ggml_metal_kargs_mul_mv &>(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg);
+}
+
+typedef decltype(kernel_mul_mv_f8_e4m3<float>) mul_mv_f8_e4m3;
+
+template [[host_name("kernel_mul_mv_f8_e4m3_f32")]] kernel mul_mv_f8_e4m3 kernel_mul_mv_f8_e4m3<float>;
+template [[host_name("kernel_mul_mv_f8_e4m3_f16")]] kernel mul_mv_f8_e4m3 kernel_mul_mv_f8_e4m3<half>;
+#if defined(GGML_METAL_HAS_BF16)
+template [[host_name("kernel_mul_mv_f8_e4m3_bf16")]] kernel mul_mv_f8_e4m3 kernel_mul_mv_f8_e4m3<bfloat>;
+#endif
+
template<typename T0, typename T1, typename args_t>
void kernel_mul_mv_t_t_short_impl(
args_t args,
@@ -3523,6 +3632,12 @@ template [[host_name("kernel_mul_mv_id_f16_f32_4")]] kernel kernel_mul_mv_id_4
template [[host_name("kernel_mul_mv_id_bf16_f32_4")]] kernel kernel_mul_mv_id_4_t kernel_mul_mv_id<mmv_fn<kernel_mul_mv_t_t_4_disp<bfloat, float>>>;
#endif
+template [[host_name("kernel_mul_mv_id_f8_e4m3_f32")]] kernel kernel_mul_mv_id_t kernel_mul_mv_id<mmv_fn<kernel_mul_mv_f8_e4m3_disp<float>>>;
+template [[host_name("kernel_mul_mv_id_f8_e4m3_f16")]] kernel kernel_mul_mv_id_t kernel_mul_mv_id<mmv_fn<kernel_mul_mv_f8_e4m3_disp<half>>>;
+#if defined(GGML_METAL_HAS_BF16)
+template [[host_name("kernel_mul_mv_id_f8_e4m3_bf16")]] kernel kernel_mul_mv_id_t kernel_mul_mv_id<mmv_fn<kernel_mul_mv_f8_e4m3_disp<bfloat>>>;
+#endif
+
template [[host_name("kernel_mul_mv_id_q8_0_f32")]] kernel kernel_mul_mv_id_t kernel_mul_mv_id<mmv_fn<kernel_mul_mv_q8_0_f32_impl<N_R0_Q8_0>>>;
template [[host_name("kernel_mul_mv_id_q1_0_f32")]] kernel kernel_mul_mv_id_t kernel_mul_mv_id<mmv_fn<kernel_mul_mv_q1_0_f32_impl<N_R0_Q1_0>>>;
diff --git a/ggml/src/ggml-metal/kernels/mul_mv_mma.metal b/ggml/src/ggml-metal/kernels/mul_mv_mma.metal
index 287834e3e..623de6602 100644
--- a/ggml/src/ggml-metal/kernels/mul_mv_mma.metal
+++ b/ggml/src/ggml-metal/kernels/mul_mv_mma.metal
@@ -301,6 +301,154 @@ kernel void kernel_mul_mv_mma_blk(
mul_mv_mma_store<NT, RT>(acc, args, src2, dst, shmem, tile, tiisg, sgitg);
}
+
+// f8_e4m3: 32 weights per block, no scale. a lane (m, j) holds bytes 4*j .. 4*j + 7
+// (j even); each byte is looked up in a 256-entry half table staged in threadgroup memory.
+struct mul_mv_mma_f8_e4m3 {
+ typedef block_f8_e4m3 block;
+ typedef ushort4 quants;
+
+ enum { qk = 32, b1 = 2 };
+
+ static short a_off(short fn) { return 2*fn; }
+ static short b_off(short fm) { return 8*(fm/2) + 2*(fm%2); }
+
+ static quants load(device const ushort * qs) { return ushort4(qs[0], qs[1], qs[2], qs[3]); }
+ static quants prep(quants q) { return q; }
+ static float2 prep_b1(float2 v) { return v; }
+
+ static bool b1_step(short s) { return s >= 2; }
+ static bool y_step (short s) { return s % 2 != 0; }
+
+ static half2 frag(quants q, short s, threadgroup const half * lut) {
+ const ushort2 w = s < 2 ? q.xy : q.zw;
+ const ushort2 qq = s % 2 == 0 ? w : w >> 8;
+ return half2(lut[qq.x & 0xFF], lut[qq.y & 0xFF]);
+ }
+};
+
+template<short NT, short RT>
+kernel void kernel_mul_mv_mma_f8(
+ constant ggml_metal_kargs_mul_mv_ext & args,
+ device const char * src0,
+ device const char * src1,
+ device char * dst,
+ device const char * src2,
+ threadgroup char * shmem [[threadgroup(0)]],
+ uint3 tgpig[[threadgroup_position_in_grid]],
+ ushort tiisg[[thread_index_in_simdgroup]],
+ ushort sgitg[[simdgroup_index_in_threadgroup]]) {
+ using Q = mul_mv_mma_f8_e4m3;
+
+ const short NSG = FC_mul_mv_mma_nsg;
+
+ const mul_mv_mma_tile tile = mul_mv_mma_tile_init<NT, RT>(args, tgpig, tiisg);
+
+ threadgroup half * lut = (threadgroup half *)(shmem + NSG*NT*RT*64*sizeof(float));
+ for (int i = sgitg*32 + tiisg; i < 256; i += NSG*32) {
+ lut[i] = (half) f8_e4m3_to_fp32(i);
+ }
+ threadgroup_barrier(mem_flags::mem_threadgroup);
+
+ device const ushort * x[NT];
+ FOR_UNROLL (short t = 0; t < NT; ++t) {
+ x[t] = (device const ushort *) mul_mv_mma_src0_row(tile, args, src0, t) + Q::a_off(tile.fn);
+ }
+
+ device const float2 * y[RT][2];
+ FOR_UNROLL (short rt = 0; rt < RT; ++rt) {
+ FOR_UNROLL (short e = 0; e < 2; ++e) {
+ y[rt][e] = (device const float2 *) (mul_mv_mma_src1_row(tile, args, src1, rt, e) + Q::b_off(tile.fm));
+ }
+ }
+
+ float acc[RT][NT][2] = {};
+
+ const int nb = FC_mul_mv_mma_ne00/Q::qk;
+ constexpr short us_blk = sizeof(block_f8_e4m3)/2;
+
+ Q::quants q[NT];
+ float2 b0[RT][2];
+ float2 b1[RT][2];
+
+ const int ib0 = min((int) sgitg, nb - 1);
+ FOR_UNROLL (short t = 0; t < NT; ++t) {
+ device const ushort * qs = x[t] + ib0*us_blk;
+ q[t] = Q::load(qs);
+ }
+ load_mma_blk_b<Q, RT>(y, ib0, b0, b1);
+
+ for (int ib = sgitg; ib < nb; ib += NSG) {
+ Q::quants qc[NT];
+ float2 b0c[RT][2];
+ float2 b1c[RT][2];
+
+ FOR_UNROLL (short t = 0; t < NT; ++t) {
+ qc[t] = Q::prep(q[t]);
+ }
+ FOR_UNROLL (short rt = 0; rt < RT; ++rt) {
+ FOR_UNROLL (short e = 0; e < 2; ++e) {
+ b0c[rt][e] = b0[rt][e];
+ b1c[rt][e] = Q::prep_b1(b1[rt][e]);
+ }
+ }
+
+ const int ibn = min(ib + NSG, nb - 1);
+ FOR_UNROLL (short t = 0; t < NT; ++t) {
+ device const ushort * qs = x[t] + ibn*us_blk;
+ q[t] = Q::load(qs);
+ }
+ load_mma_blk_b<Q, RT>(y, ibn, b0, b1);
+
+ simdgroup_float8x8 mp[RT][NT];
+ FOR_UNROLL (short rt = 0; rt < RT; ++rt) {
+ FOR_UNROLL (short t = 0; t < NT; ++t) {
+ mp[rt][t] = make_filled_simdgroup_matrix<float, 8>(0.0f);
+ }
+ }
+
+ FOR_UNROLL (short s = 0; s < 4; ++s) {
+ simdgroup_float8x8 mb[RT];
+ FOR_UNROLL (short rt = 0; rt < RT; ++rt) {
+ const float2 v0 = Q::b1_step(s) ? b1c[rt][0] : b0c[rt][0];
+ const float2 v1 = Q::b1_step(s) ? b1c[rt][1] : b0c[rt][1];
+ mb[rt].thread_elements()[0] = Q::y_step(s) ? v0.y : v0.x;
+ mb[rt].thread_elements()[1] = Q::y_step(s) ? v1.y : v1.x;
+ }
+
+ FOR_UNROLL (short t = 0; t < NT; ++t) {
+ const half2 h = Q::frag(qc[t], s, lut);
+
+ simdgroup_half8x8 ma;
+ ma.thread_elements()[0] = h.x;
+ ma.thread_elements()[1] = h.y;
+
+ FOR_UNROLL (short rt = 0; rt < RT; ++rt) {
+ simdgroup_multiply_accumulate(mp[rt][t], ma, mb[rt], mp[rt][t]);
+ }
+ }
+ }
+
+ FOR_UNROLL (short t = 0; t < NT; ++t) {
+ FOR_UNROLL (short rt = 0; rt < RT; ++rt) {
+ acc[rt][t][0] += mp[rt][t].thread_elements()[0];
+ acc[rt][t][1] += mp[rt][t].thread_elements()[1];
+ }
+ }
+ }
+
+ mul_mv_mma_store<NT, RT>(acc, args, src2, dst, shmem, tile, tiisg, sgitg);
+}
+
+typedef decltype(kernel_mul_mv_mma_f8<4, 1>) mul_mv_mma_f8_t;
+
+template [[host_name("kernel_mul_mv_mma_f8_e4m3_f32_nt1_rt1")]] kernel mul_mv_mma_f8_t kernel_mul_mv_mma_f8<1, 1>;
+template [[host_name("kernel_mul_mv_mma_f8_e4m3_f32_nt2_rt1")]] kernel mul_mv_mma_f8_t kernel_mul_mv_mma_f8<2, 1>;
+template [[host_name("kernel_mul_mv_mma_f8_e4m3_f32_nt4_rt1")]] kernel mul_mv_mma_f8_t kernel_mul_mv_mma_f8<4, 1>;
+template [[host_name("kernel_mul_mv_mma_f8_e4m3_f32_nt1_rt2")]] kernel mul_mv_mma_f8_t kernel_mul_mv_mma_f8<1, 2>;
+template [[host_name("kernel_mul_mv_mma_f8_e4m3_f32_nt2_rt2")]] kernel mul_mv_mma_f8_t kernel_mul_mv_mma_f8<2, 2>;
+template [[host_name("kernel_mul_mv_mma_f8_e4m3_f32_nt4_rt2")]] kernel mul_mv_mma_f8_t kernel_mul_mv_mma_f8<4, 2>;
+
typedef decltype(kernel_mul_mv_mma_blk<4, 1, mul_mv_mma_q4_0>) mul_mv_mma_t;
template [[host_name("kernel_mul_mv_mma_q4_0_f32_nt1_rt1")]] kernel mul_mv_mma_t kernel_mul_mv_mma_blk<1, 1, mul_mv_mma_q4_0>;
diff --git a/ggml/src/ggml-metal/kernels/quantize.metal b/ggml/src/ggml-metal/kernels/quantize.metal
index d2940ed76..84d525894 100644
--- a/ggml/src/ggml-metal/kernels/quantize.metal
+++ b/ggml/src/ggml-metal/kernels/quantize.metal
@@ -52,6 +52,119 @@ template [[host_name("kernel_cpy_bf16_f32")]] kernel kernel_cpy_t kernel_cpy_t_
template [[host_name("kernel_cpy_bf16_bf16")]] kernel kernel_cpy_t kernel_cpy_t_t<bfloat, bfloat>;
#endif
+template<typename T1>
+kernel void kernel_cpy_f8_e4m3_t(
+ constant ggml_metal_kargs_cpy & args,
+ device const char * src0,
+ device char * dst,
+ uint3 tgpig[[threadgroup_position_in_grid]],
+ ushort3 tpitg[[thread_position_in_threadgroup]],
+ ushort3 ntg[[threads_per_threadgroup]]) {
+ const int32_t i03 = tgpig[2];
+ const int32_t i02 = tgpig[1];
+ const int32_t i01 = ntg[1] == 1 ? tgpig[0]%args.ne01 : tgpig[0]*ntg[1] + tpitg.y;
+ const int32_t iw0 = ntg[1] == 1 ? tgpig[0]/args.ne01 : 0;
+
+ if (i01 >= args.ne01) {
+ return;
+ }
+
+ const int64_t n = i03*args.ne02*args.ne01*args.ne00 + i02*args.ne01*args.ne00 + i01*args.ne00;
+
+ const int32_t i3 = n/(args.ne2*args.ne1*args.ne0);
+ const int32_t i2 = (n - i3*args.ne2*args.ne1*args.ne0)/(args.ne1*args.ne0);
+ const int32_t i1 = (n - i3*args.ne2*args.ne1*args.ne0 - i2*args.ne1*args.ne0)/args.ne0;
+ const int32_t i0 = (n - i3*args.ne2*args.ne1*args.ne0 - i2*args.ne1*args.ne0 - i1*args.ne0);
+
+ device char * dst_data = dst + i3*args.nb3 + i2*args.nb2 + i1*args.nb1 + i0*args.nb0;
+
+ for (int32_t i00 = iw0*ntg[0] + tpitg.x; i00 < args.ne00;) {
+ device const uint8_t * src = (device const uint8_t *)(src0 + i03*args.nb03 + i02*args.nb02 + i01*args.nb01 + i00*args.nb00);
+ *(device T1 *)(dst_data + i00*args.nb0) = (T1) f8_e4m3_to_fp32(src[0]);
+ break;
+ }
+}
+
+template<typename T0>
+kernel void kernel_cpy_t_f8_e4m3(
+ constant ggml_metal_kargs_cpy & args,
+ device const char * src0,
+ device char * dst,
+ uint3 tgpig[[threadgroup_position_in_grid]],
+ ushort3 tpitg[[thread_position_in_threadgroup]],
+ ushort3 ntg[[threads_per_threadgroup]]) {
+ const int32_t i03 = tgpig[2];
+ const int32_t i02 = tgpig[1];
+ const int32_t i01 = ntg[1] == 1 ? tgpig[0]%args.ne01 : tgpig[0]*ntg[1] + tpitg.y;
+ const int32_t iw0 = ntg[1] == 1 ? tgpig[0]/args.ne01 : 0;
+
+ if (i01 >= args.ne01) {
+ return;
+ }
+
+ const int64_t n = i03*args.ne02*args.ne01*args.ne00 + i02*args.ne01*args.ne00 + i01*args.ne00;
+
+ const int32_t i3 = n/(args.ne2*args.ne1*args.ne0);
+ const int32_t i2 = (n - i3*args.ne2*args.ne1*args.ne0)/(args.ne1*args.ne0);
+ const int32_t i1 = (n - i3*args.ne2*args.ne1*args.ne0 - i2*args.ne1*args.ne0)/args.ne0;
+ const int32_t i0 = (n - i3*args.ne2*args.ne1*args.ne0 - i2*args.ne1*args.ne0 - i1*args.ne0);
+
+ device char * dst_data = dst + i3*args.nb3 + i2*args.nb2 + i1*args.nb1 + i0*args.nb0;
+
+ for (int32_t i00 = iw0*ntg[0] + tpitg.x; i00 < args.ne00;) {
+ device const T0 * src = (device const T0 *)(src0 + i03*args.nb03 + i02*args.nb02 + i01*args.nb01 + i00*args.nb00);
+ *(device uint8_t *)(dst_data + i00*args.nb0) = fp32_to_f8_e4m3((float) src[0]);
+ break;
+ }
+}
+
+[[host_name("kernel_cpy_f8_e4m3_f8_e4m3")]]
+kernel void kernel_cpy_f8_e4m3_f8_e4m3(
+ constant ggml_metal_kargs_cpy & args,
+ device const char * src0,
+ device char * dst,
+ uint3 tgpig[[threadgroup_position_in_grid]],
+ ushort3 tpitg[[thread_position_in_threadgroup]],
+ ushort3 ntg[[threads_per_threadgroup]]) {
+ const int32_t i03 = tgpig[2];
+ const int32_t i02 = tgpig[1];
+ const int32_t i01 = ntg[1] == 1 ? tgpig[0]%args.ne01 : tgpig[0]*ntg[1] + tpitg.y;
+ const int32_t iw0 = ntg[1] == 1 ? tgpig[0]/args.ne01 : 0;
+
+ if (i01 >= args.ne01) {
+ return;
+ }
+
+ const int64_t n = i03*args.ne02*args.ne01*args.ne00 + i02*args.ne01*args.ne00 + i01*args.ne00;
+
+ const int32_t i3 = n/(args.ne2*args.ne1*args.ne0);
+ const int32_t i2 = (n - i3*args.ne2*args.ne1*args.ne0)/(args.ne1*args.ne0);
+ const int32_t i1 = (n - i3*args.ne2*args.ne1*args.ne0 - i2*args.ne1*args.ne0)/args.ne0;
+ const int32_t i0 = (n - i3*args.ne2*args.ne1*args.ne0 - i2*args.ne1*args.ne0 - i1*args.ne0);
+
+ device char * dst_data = dst + i3*args.nb3 + i2*args.nb2 + i1*args.nb1 + i0*args.nb0;
+
+ for (int32_t i00 = iw0*ntg[0] + tpitg.x; i00 < args.ne00;) {
+ device const uint8_t * src = (device const uint8_t *)(src0 + i03*args.nb03 + i02*args.nb02 + i01*args.nb01 + i00*args.nb00);
+ *(device uint8_t *)(dst_data + i00*args.nb0) = src[0];
+ break;
+ }
+}
+
+typedef decltype(kernel_cpy_f8_e4m3_t<float>) cpy_f8_e4m3_t;
+typedef decltype(kernel_cpy_t_f8_e4m3<float>) cpy_t_f8_e4m3_t;
+
+template [[host_name("kernel_cpy_f8_e4m3_f32")]] kernel cpy_f8_e4m3_t kernel_cpy_f8_e4m3_t<float>;
+template [[host_name("kernel_cpy_f8_e4m3_f16")]] kernel cpy_f8_e4m3_t kernel_cpy_f8_e4m3_t<half>;
+#if defined(GGML_METAL_HAS_BF16)
+template [[host_name("kernel_cpy_f8_e4m3_bf16")]] kernel cpy_f8_e4m3_t kernel_cpy_f8_e4m3_t<bfloat>;
+#endif
+template [[host_name("kernel_cpy_f32_f8_e4m3")]] kernel cpy_t_f8_e4m3_t kernel_cpy_t_f8_e4m3<float>;
+template [[host_name("kernel_cpy_f16_f8_e4m3")]] kernel cpy_t_f8_e4m3_t kernel_cpy_t_f8_e4m3<half>;
+#if defined(GGML_METAL_HAS_BF16)
+template [[host_name("kernel_cpy_bf16_f8_e4m3")]] kernel cpy_t_f8_e4m3_t kernel_cpy_t_f8_e4m3<bfloat>;
+#endif
+
template<short QK,
typename block_q,
void (*quantize_func)(device const float *, device block_q &)>
@@ -395,6 +508,34 @@ template [[host_name("kernel_get_rows_i32")]] kernel get_rows_f_t kernel_get_ro
template [[host_name("kernel_get_rows_bf16")]] kernel get_rows_f_t kernel_get_rows_f<bfloat, float>;
#endif
+[[host_name("kernel_get_rows_f8_e4m3")]]
+kernel void kernel_get_rows_f8_e4m3(
+ constant ggml_metal_kargs_get_rows & args,
+ device const void * src0,
+ device const void * src1,
+ device void * dst,
+ uint3 tgpig[[threadgroup_position_in_grid]],
+ ushort tiitg[[thread_index_in_threadgroup]],
+ ushort3 ntg [[threads_per_threadgroup]]) {
+ const int32_t iw0 = tgpig.x/args.ne10;
+ const int32_t i10 = tgpig.x%args.ne10;
+ const int32_t i11 = tgpig.y;
+ const int32_t i12 = tgpig.z;
+
+ const int32_t r = ((const device int32_t *) ((const device char *) src1 + i12*args.nb12 + i11*args.nb11 + i10*args.nb10))[0];
+
+ const int32_t i02 = i11;
+ const int32_t i03 = i12;
+
+ auto psrc = (const device uint8_t *) ((const device char *) src0 + i03*args.nb03 + i02*args.nb02 + r*args.nb01);
+ auto pdst = ( device float *) (( device char *) dst + i12*args.nb3 + i11*args.nb2 + i10*args.nb1);
+
+ for (int ind = iw0*ntg.x + tiitg; ind < args.ne00t;) {
+ pdst[ind] = f8_e4m3_to_fp32(psrc[ind]);
+ break;
+ }
+}
+
typedef decltype(kernel_get_rows_q<block_q4_0, 2, dequantize_q4_0>) get_rows_q_t;
template [[host_name("kernel_get_rows_q1_0")]] kernel get_rows_q_t kernel_get_rows_q<block_q1_0, 8, dequantize_q1_0>;
@@ -484,6 +625,37 @@ kernel void kernel_set_rows_q32(
}
}
+template<typename TS, typename TI>
+kernel void kernel_set_rows_f8_e4m3(
+ constant ggml_metal_kargs_set_rows & args,
+ device const void * src0,
+ device const void * src1,
+ device void * dst,
+ uint3 tgpig[[threadgroup_position_in_grid]],
+ uint tiitg[[thread_index_in_threadgroup]],
+ uint3 tptg [[threads_per_threadgroup]]) {
+ const int32_t i03 = tgpig.z;
+ const int32_t i02 = tgpig.y;
+
+ const int32_t i12 = i03%args.ne12;
+ const int32_t i11 = i02%args.ne11;
+
+ const int32_t i01 = tgpig.x*tptg.y + tiitg/tptg.x;
+ if (i01 >= args.ne01) {
+ return;
+ }
+
+ const int32_t i10 = i01;
+ const TI i1 = ((const device TI *) ((const device char *) src1 + i10*args.nb10 + i11*args.nb11 + i12*args.nb12))[0];
+
+ device uint8_t * dst_row = ( device uint8_t *) (( device char *) dst + i1*args.nb1 + i02*args.nb2 + i03*args.nb3);
+ const device TS * src_row = (const device TS *) ((const device char *) src0 + i01*args.nb01 + i02*args.nb02 + i03*args.nb03);
+
+ for (int ind = tiitg%tptg.x; ind < args.nk0; ind += tptg.x) {
+ dst_row[ind] = fp32_to_f8_e4m3((float) src_row[ind]);
+ }
+}
+
template<typename TS, typename TI, typename TD>
kernel void kernel_set_rows_f(
constant ggml_metal_kargs_set_rows & args,
@@ -533,6 +705,17 @@ template [[host_name("kernel_set_rows_bf16_i64_bf16")]] kernel set_rows_f_t kern
template [[host_name("kernel_set_rows_bf16_i32_bf16")]] kernel set_rows_f_t kernel_set_rows_f<bfloat, int32_t, bfloat>;
#endif
+typedef decltype(kernel_set_rows_f8_e4m3<float, int64_t>) set_rows_f8_e4m3_t;
+
+template [[host_name("kernel_set_rows_f32_i64_f8_e4m3")]] kernel set_rows_f8_e4m3_t kernel_set_rows_f8_e4m3<float, int64_t>;
+template [[host_name("kernel_set_rows_f32_i32_f8_e4m3")]] kernel set_rows_f8_e4m3_t kernel_set_rows_f8_e4m3<float, int32_t>;
+template [[host_name("kernel_set_rows_f16_i64_f8_e4m3")]] kernel set_rows_f8_e4m3_t kernel_set_rows_f8_e4m3<half, int64_t>;
+template [[host_name("kernel_set_rows_f16_i32_f8_e4m3")]] kernel set_rows_f8_e4m3_t kernel_set_rows_f8_e4m3<half, int32_t>;
+#if defined(GGML_METAL_HAS_BF16)
+template [[host_name("kernel_set_rows_bf16_i64_f8_e4m3")]] kernel set_rows_f8_e4m3_t kernel_set_rows_f8_e4m3<bfloat, int64_t>;
+template [[host_name("kernel_set_rows_bf16_i32_f8_e4m3")]] kernel set_rows_f8_e4m3_t kernel_set_rows_f8_e4m3<bfloat, int32_t>;
+#endif
+
typedef decltype(kernel_set_rows_q32<float, int64_t, block_q8_0, quantize_q8_0>) set_rows_q32_t;
template [[host_name("kernel_set_rows_f32_i64_q8_0")]] kernel set_rows_q32_t kernel_set_rows_q32<float, int64_t, block_q8_0, quantize_q8_0>;
diff --git a/tests/test-backend-ops.cpp b/tests/test-backend-ops.cpp
index 83be72dff..ddcba7a11 100644
--- a/tests/test-backend-ops.cpp
+++ b/tests/test-backend-ops.cpp
@@ -12629,6 +12629,7 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
test_cases.emplace_back(new test_set_rows(GGML_TYPE_F32, GGML_TYPE_F32, GGML_TYPE_I64, { 1, 8, 1, 3 }, { 1, 1 }, 2, false));
test_cases.emplace_back(new test_set_rows(GGML_TYPE_F32, GGML_TYPE_F32, GGML_TYPE_I32, { 1, 8, 1, 3 }, { 1, 1 }, 2, false));
+ test_cases.emplace_back(new test_set_rows(GGML_TYPE_F32, GGML_TYPE_F8_E4M3, GGML_TYPE_I64, { 512, 256, 1, 1 }, { 1, 1 }, 128, false));
test_cases.emplace_back(new test_set_rows(GGML_TYPE_F32, GGML_TYPE_Q8_0, GGML_TYPE_I32, { 256, 5, 1, 3 }, { 1, 1, }, 1, false));
for (ggml_type src_type : {GGML_TYPE_F16, GGML_TYPE_F32}) {
for (ggml_type type : all_types) {
@@ -15397,15 +15398,23 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
test_cases.emplace_back(new test_flash_attn_ext(64, 64, 4, {1, 1}, 128, 2, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F8_E4M3, GGML_TYPE_F8_E4M3));
test_cases.emplace_back(new test_flash_attn_ext(128, 128, 4, {1, 1}, 128, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F8_E4M3, GGML_TYPE_F8_E4M3));
test_cases.emplace_back(new test_flash_attn_ext(256, 256, 4, {1, 1}, 128, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F8_E4M3, GGML_TYPE_F8_E4M3));
+ test_cases.emplace_back(new test_flash_attn_ext(64, 64, 4, {1, 1}, 256, 32, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F8_E4M3, GGML_TYPE_F8_E4M3));
+ test_cases.emplace_back(new test_flash_attn_ext(512, 512, 4, {1, 1}, 256, 32, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F8_E4M3, GGML_TYPE_F8_E4M3));
test_cases.emplace_back(new test_flash_attn_ext(64, 64, 4, {1, 1}, 128, 2, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F8_E4M3, GGML_TYPE_F16));
test_cases.emplace_back(new test_flash_attn_ext(64, 64, 4, {1, 1}, 128, 2, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F8_E4M3));
- for (int hs : {64, 128, 256}) {
+ for (int hs : {32, 64, 96, 128, 192, 256, 512}) {
for (ggml_type type_K : {GGML_TYPE_F16, GGML_TYPE_F8_E4M3}) {
test_cases.emplace_back(new test_flash_attn_ext(hs, hs, 4, {1, 1}, 256, 1, true, false, 0, 0, GGML_PREC_F32, type_K, GGML_TYPE_F8_E4M3));
}
}
+ test_cases.emplace_back(new test_flash_attn_ext( 96, 64, 4, {1, 1}, 256, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F8_E4M3, GGML_TYPE_F8_E4M3));
+ test_cases.emplace_back(new test_flash_attn_ext(128, 96, 4, {1, 1}, 256, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F8_E4M3, GGML_TYPE_F8_E4M3));
+ test_cases.emplace_back(new test_flash_attn_ext(192, 128, 4, {1, 1}, 256, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F8_E4M3, GGML_TYPE_F8_E4M3));
+ test_cases.emplace_back(new test_flash_attn_ext(320, 256, 4, {1, 1}, 256, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F8_E4M3, GGML_TYPE_F8_E4M3));
+ test_cases.emplace_back(new test_flash_attn_ext(576, 512, 4, {1, 1}, 256, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F8_E4M3, GGML_TYPE_F8_E4M3));
+
// q8_0 KV cases: decode and prompt batches, KV pad, permuted KV, feature flags, and long context
test_cases.emplace_back(new test_flash_attn_ext(256, 256, 2, {16, 1}, 113, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q8_0, GGML_TYPE_Q8_0));
test_cases.emplace_back(new test_flash_attn_ext(256, 256, 2, {16, 1}, 1024, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q8_0, GGML_TYPE_Q8_0));
@@ -16429,6 +16438,12 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_perf() {
}
}
+ test_cases.emplace_back(new test_mul_mat_id(GGML_TYPE_F8_E4M3, GGML_TYPE_BF16, 128, 8, false, 768, 64, 2048));
+ test_cases.emplace_back(new test_mul_mat_id(GGML_TYPE_F8_E4M3, GGML_TYPE_F32, 128, 8, false, 768, 64, 2048));
+ test_cases.emplace_back(new test_mul_mat_id(GGML_TYPE_F8_E4M3, GGML_TYPE_F32, 128, 8, false, 768, 512, 2048));
+ test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F8_E4M3, GGML_TYPE_BF16, 768, 64, 2048, {1, 1}, {1, 1}));
+ test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F8_E4M3, GGML_TYPE_BF16, 768, 512, 2048, {1, 1}, {1, 1}));
+
// qwen3-30b-a3b
for (int bs : {1, 4, 8, 32, 64, 128, 256, 512}) {
for (ggml_type type_a : {GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_Q4_0, GGML_TYPE_Q8_0, GGML_TYPE_Q2_K, GGML_TYPE_Q3_K, GGML_TYPE_Q4_K, GGML_TYPE_Q6_K, GGML_TYPE_IQ2_XS, GGML_TYPE_IQ4_XS}) {