Skip to content

metal : add FA kernels for HSK=96, HSV=64 (MiniCPM3) - #28599

Merged
ggerganov merged 2 commits into
ggml-org:masterfrom
wyanzhao:codex/fa-minicpm-submit
Sep 15, 2026
Merged

ggerganov merged 2 commits into
ggml-org:masterfrom
wyanzhao:codex/fa-minicpm-submit

Conversation

@wyanzhao

@wyanzhao wyanzhao commented Sep 8, 2026 •

Copy link
Copy Markdown
Contributor

Overview

Add the missing (96, 64) tile and vec flash attention kernels for MiniCPM3. Its attention
uses HSK=96 and HSV=64; without these kernels, -fa auto crashes when Metal tries to load
kernel_flash_attn_ext_vec_f16_dk96_dv64.

Fixes #28602.

The new instantiations follow the existing (96, 96) block. The vec kernels use NE=4:
of the dispatch candidates (1, 2, 4), only NE=4 makes NL = 32/NE divide both
DK/4 = 24 and DV/4 = 16. Q=2 and Q=4 variants are included, but normal dispatch
uses Q=1 because the tuning table has no (96, 64) entries yet.

test-backend-ops now exercises (96, 64) in the FA tests and the Metal (Q, NE) vec slice.
All 4950 FLASH_ATTN_EXT cases pass on an M5 Max at 977fa66f7.

Additional information

On wikitext-2, perplexity was 13.6430 +/- 0.25098 with FA off and
13.6397 +/- 0.25091 with FA on, over 32 chunks at -c 2048.

MiniCPM3-4B Q4_K_M on an M5 Max, flash attention off against on.
Speedup is FA-on throughput divided by FA-off throughput; d is the number of tokens
already in the KV cache. Each row uses five repetitions.

Test FA off t/s FA on t/s Speedup
pp512 3299.95 3666.22 1.11
tg32 101.37 111.34 1.10
pp512 @ d4096 1367.13 1638.28 1.20
tg32 @ d4096 56.50 67.96 1.20
pp512 @ d16384 375.01 384.83 1.03
tg32 @ d16384 23.28 30.16 1.30

Prefill at d=16384 varied from 0.98x to 1.03x across clean runs; I would treat it as unchanged within noise.

llama-bench -m minicpm3-4b-q4_k_m.gguf -ngl 99 -fa off,on -p 512 -n 32 -d 0,4096,16384 -r 5 --delay 15
Reproduce with MiniCPM3-4B

Use minicpm3-4b-q4_k_m.gguf from openbmb/MiniCPM3-4B-GGUF:

./build/bin/llama-cli -m minicpm3-4b-q4_k_m.gguf \
  -ngl 99 -fa auto -ctk f16 -ctv f16 -c 512 -b 128 -ub 128 \
  -n 32 --seed 1234 --temp 0 -st -p '请用一句话介绍北京。'

The baseline (64e9bceb2) crashes on this command. With the added kernels it generates; the generated text matches -fa off for this prompt and 32-token budget.

cmake --build build --target test-backend-ops -j 8
./build/bin/test-backend-ops test -b MTL0 -o FLASH_ATTN_EXT

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: YES. AI assisted with the kernel instantiations, the test changes, and the benchmarking. I reviewed every line and I am responsible for the contribution.

@github-actions github-actions Bot added testing Everything test related ggml changes relating to the ggml tensor library for machine learning Apple Metal https://en.wikipedia.org/wiki/Metal_(API) labels Sep 8, 2026
@wyanzhao
wyanzhao marked this pull request as ready for review September 8, 2026 10:09
@wyanzhao
wyanzhao requested review from a team and ggerganov as code owners September 8, 2026 10:09
@forforever73

Copy link
Copy Markdown
Contributor

I think it would be better to add dk96_dv64 kernel rather than falling back to the no-FA path.

@wyanzhao
wyanzhao force-pushed the codex/fa-minicpm-submit branch from ae552a7 to 6f68dc4 Compare September 12, 2026 04:42
@wyanzhao
wyanzhao marked this pull request as draft September 12, 2026 04:44
MiniCPM3 sets attention.key_length to 96 and does not set
attention.value_length, which defaults to n_embd / n_head = 64. Metal had no
(96, 64) instantiation, so -fa auto aborted on the missing
kernel_flash_attn_ext_vec_f16_dk96_dv64.

Instantiate the tile kernel at (96, 64) for every K/V type that already has
(96, 96), and the vec kernel for the NE=4 configurations. Of the NE values the
vec dispatch considers, only NE=4 works here, because NL = 32/NE has to divide
both DK/4 = 24 and DV/4 = 16.
@wyanzhao
wyanzhao force-pushed the codex/fa-minicpm-submit branch from 6f68dc4 to 977fa66 Compare September 12, 2026 05:15
@wyanzhao wyanzhao changed the title metal : fix MiniCPM3 crash with automatic flash attention metal : add FA kernels for HSK=96, HSV=64 (MiniCPM3) Sep 12, 2026
@wyanzhao

wyanzhao commented Sep 12, 2026 •

Copy link
Copy Markdown
Contributor Author

@forforever73 Good call - added the (96, 64) tile and vec kernels in 977fa66f7, along with test coverage. All 4950 FA tests pass on an M5 Max. I updated the PR description with the implementation details and the benchmark results.

@wyanzhao
wyanzhao marked this pull request as ready for review September 12, 2026 05:19
Comment thread tests/test-backend-ops.cpp Resolved
@ggerganov ggerganov added the merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. label Sep 14, 2026
@ggerganov
ggerganov merged commit 1e7bcf3 into ggml-org:master Sep 15, 2026
34 of 36 checks passed
dzannotti added a commit to halo-box/llama.cpp that referenced this pull request Sep 15, 2026
* upstream/master: (72 commits)
  HIP: Enable AllReduce for ROCm (ggml-org#27825)
  opencl: choose the MoE expert matmul by batch size for speculative decoding/MTP (ggml-org#27637)
  ci: build MUSA for only 1 arch (ggml-org#28944)
  docs: Rule of thumb for AI review time [no ci] (ggml-org#28945)
  rpc : hash-cache only weights (ggml-org#28789)
  cuda: support row-contiguous SUM_ROWS (ggml-org#26308)
  models : move build_arch_graph() after graph() template specialization (ggml-org#28934)
  vulkan: support sparse Flash Attention (ggml-org#28105)
  OpenVINO: optimize stateful decode and GPU MoE inference (ggml-org#28638)
  opencl: add generic ssm_scan (ggml-org#28881)
  ci: bump kleidiai runners from 22.04 to 24.04 (ggml-org#28885)
  metal : add FA kernels for HSK=96, HSV=64 (MiniCPM3) (ggml-org#28599)
  ci: Bump CUDA Windows x64 builds to 13.4.1 (ggml-org#28930)
  ci : fix android release (ggml-org#28936)
  cuda : enable i16 and i32 for DUP (ggml-org#28897)
  cmake : use PROJECT_SOURCE_DIR instead of CMAKE_SOURCE_DIR (ggml-org#28771)
  webui: stop re-probing disabled /tools endpoint on every message (ggml-org#28646)
  ci : reuse build tag name when used instead of safe one (ggml-org#28911)
  CI: hip-quality-check: ignore spill added in bfdc321 (ggml-org#28909)
  HIP: fattn-mma: use fp32 accumulation on MFMA devices (ggml-org#28576)
  ...
quimmedes pushed a commit to quimmedes/cafe-llama.cpp that referenced this pull request Sep 16, 2026
* metal : add FA kernels for HSK=96, HSV=64 (MiniCPM3)

MiniCPM3 sets attention.key_length to 96 and does not set
attention.value_length, which defaults to n_embd / n_head = 64. Metal had no
(96, 64) instantiation, so -fa auto aborted on the missing
kernel_flash_attn_ext_vec_f16_dk96_dv64.

Instantiate the tile kernel at (96, 64) for every K/V type that already has
(96, 96), and the vec kernel for the NE=4 configurations. Of the NE values the
vec dispatch considers, only NE=4 works here, because NL = 32/NE has to divide
both DK/4 = 24 and DV/4 = 16.

* tests : avoid redundant FA vec slice coverage
@BrewTestBot BrewTestBot mentioned this pull request Sep 23, 2026
1 task done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Apple Metal https://en.wikipedia.org/wiki/Metal_(API) ggml changes relating to the ggml tensor library for machine learning merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. testing Everything test related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Misc. bug: MiniCPM3-4B crashes on Metal with automatic flash attention

4 participants