Skip to content

metal : add GGML_OP_TOP_K_MASK - #31230

Merged
ggerganov merged 2 commits into
masterfrom
gg/metal-top-k-mask
Oct 27, 2026
Merged

ggerganov merged 2 commits into
masterfrom
gg/metal-top-k-mask

Conversation

@ggerganov

Copy link
Copy Markdown
Member

Overview

Port GGML_OP_TOP_K_MASK from the CUDA backend (#30640) to the Metal backend. This is the fused QSA sparse-attention mask: in each output row, a cell keeps its mask value when it belongs to a live pick (a top_k index in range, with score > -inf when score is given, expanded to the blocks cells when blocks is given) or to an extra cell; every other cell becomes -inf.

The op is a single graph node with 2 dispatches:

  • kernel_top_k_mask_fill - grid-stride fill of the whole output with -inf, 4 cells per thread when the output layout permits it
  • kernel_top_k_mask - one threadgroup per row, copies the mask cells of the live picks and of the extra cells over the fill

Additional information

The graph builder already emits the fused form (build_fused_top_k_mask in llama-graph.cpp); with this PR the node resolves on MTL0 instead of falling back to CPU.

Requirements

YES

  • Harness: dsh:llama.cpp
  • Hardware: MacBook, M5 Max, 128GB
  • Model: ggml-org/Qwen3.8-Flash-Next-GGUF
  • Stats: ◷ 1.4h · ⚙ 25.3m · ↑ 523k · ↓ 242k · ⇈ 735.3 tok/s · ⇊ 57.4 tok/s · ≡ 619 · ⌨ 49

Port the fused top-k mask from the CUDA backend: a grid-stride kernel fills the output with -inf, then one
threadgroup per row copies the mask values of the kept cells over it. Function constants drop the score,
blocks and extra paths that a given graph does not use.

Assisted-by: dsh:llama.cpp/ggml-org/Qwen3.8-Flash-Next-GGUF (MacBook, M5 Max, 128GB)
@ggerganov
ggerganov marked this pull request as ready for review October 27, 2026 06:25
@ggerganov
ggerganov requested a review from a team as a code owner October 27, 2026 06:25
@github-actions github-actions Bot added ggml changes relating to the ggml tensor library for machine learning Apple Metal https://en.wikipedia.org/wiki/Metal_(API) labels Oct 27, 2026
Assisted-by: dsh:llama.cpp/ggml-org/Qwen3.8-Flash-Next-GGUF (MacBook, M5 Max, 128GB)
@ggerganov
ggerganov merged commit 2763785 into master Oct 27, 2026
26 checks passed
@ggerganov
ggerganov deleted the gg/metal-top-k-mask branch October 27, 2026 10:13
auRose94 pushed a commit to auRose94/llama.cpp that referenced this pull request Oct 28, 2026
* metal : add GGML_OP_TOP_K_MASK

Port the fused top-k mask from the CUDA backend: a grid-stride kernel fills the output with -inf, then one
threadgroup per row copies the mask values of the kept cells over it. Function constants drop the score,
blocks and extra paths that a given graph does not use.

Assisted-by: dsh:llama.cpp/ggml-org/Qwen3.8-Flash-Next-GGUF (MacBook, M5 Max, 128GB)

* metal : clean up top-k mask kernel formatting

Assisted-by: dsh:llama.cpp/ggml-org/Qwen3.8-Flash-Next-GGUF (MacBook, M5 Max, 128GB)
@BrewTestBot BrewTestBot mentioned this pull request Oct 28, 2026
1 task done
yzhou51 pushed a commit to yzhou51/llama.cpp that referenced this pull request Oct 30, 2026
* metal : add GGML_OP_TOP_K_MASK

Port the fused top-k mask from the CUDA backend: a grid-stride kernel fills the output with -inf, then one
threadgroup per row copies the mask values of the kept cells over it. Function constants drop the score,
blocks and extra paths that a given graph does not use.

Assisted-by: dsh:llama.cpp/ggml-org/Qwen3.8-Flash-Next-GGUF (MacBook, M5 Max, 128GB)

* metal : clean up top-k mask kernel formatting

Assisted-by: dsh:llama.cpp/ggml-org/Qwen3.8-Flash-Next-GGUF (MacBook, M5 Max, 128GB)
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant