llama.cpp
a0292428 - metal : add native F8_E4M3 support (#33533)

Commit
4 hours ago
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)
Author
Parents
Loading