Skip to content

metal : reserve the top-k scratch only for short rows - #32942

Merged
ggerganov merged 1 commit into
masterfrom
gg/metal-top-k-scratch
Nov 24, 2026
Merged

ggerganov merged 1 commit into
masterfrom
gg/metal-top-k-scratch

Conversation

@ggerganov

@ggerganov ggerganov commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

Overview

The bitonic top-k path is only faster for short rows. Benchmarks on M5 and M2 Ultra show it wins up to 128 columns; above that the radix path is faster. Use the radix path for ncols > 128.

The radix path selects in place and needs no scratch, but the buffer type always reserved 2*sizeof(int32_t) per source element, which is 8 GiB for a 524288 x 2048 score. Reserve the bitonic worst case at the 128-column threshold instead: 2*sizeof(int32_t)*128*nrows, clamped to at least the output tensor size. This keeps the allocation bounded and consistent with the bitonic path.

Additional information

The threshold is a local function ggml_metal_op_top_k_bitonic_ncols_thold; the allocation logic is in ggml_metal_op_top_k_extra.

The 524288 x 2048 sparse-attention score now allocates 2 MiB for k=64 and 16 MiB for k=2048, instead of 8 GiB.

Benchmarks (test-backend-ops perf, Metal, M5 Ultra and M2 Ultra):

ncols M5 Ultra M2 Ultra
16 bitonic 12.8× (nrows=8192, k=16) bitonic 11.2× (nrows=8192, k=8)
128 bitonic 1.88× (nrows=8192, k=16) bitonic 2.7× (nrows=8192, k=8)
256 radix 1.79× (nrows=2048, k=8) bitonic 1.14× (nrows=2048, k=8)
512 radix 3.29× (nrows=2048, k=8) radix 1.9× (nrows=2048, k=8)
1024 radix 6.71× (nrows=2048, k=8) radix 4.3× (nrows=2048, k=8)

The 256-column case is marginal on M2 and already favors radix on M5, so 128 is the safer cross-machine threshold.

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: YES
    • Harness: dsh:llama.cpp
    • Hardware: Mac Studio, M5 Ultra, 256GB
    • Model: ggml-org/DeepSeek-V4.1-Flash-1017-GGUF
    • Stats:
      • 82f8e5d6 · ◷ 1.4h · ⚙ 12.2m · ↑ 385k · ↓ 243k · ⇈ 404.8 tok/s · ⇊ 60.8 tok/s · ≡ 431 · ⌨ 37

@ggerganov
ggerganov marked this pull request as ready for review November 24, 2026 12:19
@ggerganov
ggerganov requested a review from a team as a code owner November 24, 2026 12:19
@ggerganov
ggerganov force-pushed the gg/metal-top-k-scratch branch from 021d67c to cd96199 Compare November 24, 2026 12:26
@ggerganov
ggerganov marked this pull request as draft November 24, 2026 12:27
The bitonic top-k path is only faster for short rows. On M5 and M2 Ultra it
wins up to 128 columns; above that the radix path is faster. Use the radix
path for ncols > 128.

The radix path selects in place and needs no scratch, but the buffer type
always reserved 2*4 bytes per source element, which is 8 GiB for a
524288 x 2048 score. Reserve the bitonic worst case at the 128-column
threshold instead: 2*4*128*nrows, clamped to at least the output tensor size.
This keeps the allocation bounded and consistent with the bitonic path.

Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
@ggerganov
ggerganov force-pushed the gg/metal-top-k-scratch branch from cd96199 to 14552c7 Compare November 24, 2026 12:30
@ggerganov
ggerganov marked this pull request as ready for review November 24, 2026 12:30
@ggerganov
ggerganov marked this pull request as draft November 24, 2026 12:30
@ggerganov
ggerganov marked this pull request as ready for review November 24, 2026 12:30
@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 Nov 24, 2026
@ggerganov
ggerganov merged commit 3090e45 into master Nov 24, 2026
23 of 25 checks passed
abeloin added a commit to abeloin/llama.cpp-mix that referenced this pull request Nov 25, 2026
…owner 2026-11-25)

Base b12723 (merge a940ab9), pin ggml-org#401 removed (80211b4), RoPE
multi-section fusion from upstream ggml-org#32500 (owner's decision), MIX.md
(3563400). Build gate: test-backend-ops ROCm 21878/21878, bench r17
(llama-mix-notes, mesures/2026.md): Inkling tg +4.5 % [bon], Qwen3.6 and
Gemma 4 without regression.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01QBjnvFCJ83vkmygsLNv5cu

* mix/wip-b12723: (55 commits)
  mix: MIX.md — base b12723 (branche wip), retrait du pin ggml-org#401, conflit RoPE tranché (amont ggml-org#32500)
  revert: pin: CUDA: fuse the row L2 clip chain (unslothai#401, commit 6c0e84d)
  model : inkling2 graph with fewer ops in mix() and control() (ggml-org#32948)
  opencl: add clc coopmat moe kernel for q4_0/q4_1/q4_k/q5_0/q5_1/q5_k/q6_k/mxfp4 (ggml-org#32971)
  tests : fix MSVC static build of test-batch-alloc and test-server-component (ggml-org#32870)
  feat: Inkling 2 DSpark support (ggml-org#32218)
  cuda : fix int overflow of the MMVF matrix offsets (ggml-org#32965)
  docs : describe SpacemiT runtime environment variables (ggml-org#32452)
  ggml : fix SpacemiT flash attention null mask, ALiBi and softcap (ggml-org#32182)
  cmake : include the llguidance header from the source tree (ggml-org#32928)
  ci : move some jobs to gb10 runners (ggml-org#32968)
  ui: run Llama Chat apart from llama-server, named in config.json or npx --server (ggml-org#32945)
  ui: fix prebuilt UI download in shallow clones (ggml-org#32952)
  convert : fix qwen3-omni conversion (ggml-org#32941)
  ggml-cpu: repack kernels for targets without dotprod (ARMv8.0 64-bit) for Q4_0/Q8_0 (ggml-org#32142)
  ui: expect the Llama Chat name in the PWA manifest e2e test (ggml-org#32959)
  ggml-cpu: add SVE-optimized q5_0 and q5_1 dot-product kernels (ggml-org#30867)
  server : include MCP image and audio data in tool results (ggml-org#32064)
  context : prevent excessive CPU polling without OpenMP (ggml-org#32378)
  metal : reserve the top-k scratch only for short rows (ggml-org#32942)
  ...
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