Repository navigation
metal : add FA kernels for HSK=96, HSV=64 (MiniCPM3) - #28599
Merged
Merged
Conversation
wyanzhao
marked this pull request as ready for review
September 8, 2026 10:09
Contributor
|
I think it would be better to add dk96_dv64 kernel rather than falling back to the no-FA path. |
wyanzhao
force-pushed
the
codex/fa-minicpm-submit
branch
from
September 12, 2026 04:42
ae552a7 to
6f68dc4
Compare
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
force-pushed
the
codex/fa-minicpm-submit
branch
from
September 12, 2026 05:15
6f68dc4 to
977fa66
Compare
Contributor
Author
|
@forforever73 Good call - added the (96, 64) tile and vec kernels in |
wyanzhao
marked this pull request as ready for review
September 12, 2026 05:19
forforever73
approved these changes
Sep 14, 2026
ggerganov
reviewed
Sep 14, 2026
ggerganov
approved these changes
Sep 14, 2026
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
1 task done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 autocrashes when Metal tries to loadkernel_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/NEdivide bothDK/4 = 24andDV/4 = 16. Q=2 and Q=4 variants are included, but normal dispatchuses Q=1 because the tuning table has no (96, 64) entries yet.
test-backend-opsnow exercises (96, 64) in the FA tests and the Metal (Q, NE) vec slice.All 4950
FLASH_ATTN_EXTcases pass on an M5 Max at977fa66f7.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;
dis the number of tokensalready in the KV cache. Each row uses five repetitions.
Prefill at d=16384 varied from 0.98x to 1.03x across clean runs; I would treat it as unchanged within noise.
Reproduce with MiniCPM3-4B
Use
minicpm3-4b-q4_k_m.gguffrom 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 offfor 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_EXTRequirements