Skip to content

metal : optimize argmax and pool_1d - #30862

Merged
ggerganov merged 3 commits into
masterfrom
gg/metal-argmax-pool1d
Oct 20, 2026
Merged

ggerganov merged 3 commits into
masterfrom
gg/metal-argmax-pool1d

Conversation

@ggerganov

@ggerganov ggerganov commented Oct 20, 2026 •

Copy link
Copy Markdown
Member

Overview

rel #30861

Optimize the Metal implementations of ggml_argmax and ggml_pool_1d.

  • Add a two-pass partial reduction for ggml_argmax on large rows: a part kernel writes per-chunk partial maxima/indices to a scratch buffer, and a final kernel reduces them per row.
  • Use cooperative threadgroup reductions for ggml_pool_1d, so large kernel windows are reduced across up to 256 threads instead of one thread per output.
  • Allocate scratch space for the argmax partials through the Metal buffer allocator.

Additional information

  • The original single-pass argmax is kept for small rows.
  • A memory barrier is inserted between the part and final argmax dispatches to avoid reading stale partials.
  • Added representative POOL_1D perf cases for the MoE down-projection amax shapes.

Performance

Measured with test-backend-ops perf -b MTL0 -o "ARGMAX,POOL_1D".

Apple M2 Ultra

POOL_1D max, full-width reduction:

shape base PR
2048,8,1 247.51 us 6.35 us
2048,8,512 2303.73 us 36.70 us
2048,16,1 253.73 us 5.76 us
2048,16,512 2379.66 us 80.76 us
4096,8,1 497.65 us 8.76 us
4096,8,512 4594.63 us 76.58 us
4096,16,1 506.07 us 8.74 us
4096,16,512 4795.36 us 187.21 us
8192,8,1 978.55 us 14.65 us
8192,8,512 9162.98 us 194.02 us
8192,16,1 1022.32 us 14.53 us
8192,16,512 9613.05 us 368.94 us

ARGMAX:

shape base PR
32,10 3.01 us 3.00 us
1024,10 15.43 us 15.61 us
32000,512 453.17 us 78.87 us

Apple M5 Max

POOL_1D max, full-width reduction:

shape base PR
2048,8,1 110.52 us 2.79 us
2048,8,512 316.13 us 35.37 us
2048,16,1 115.61 us 2.77 us
2048,16,512 340.83 us 95.75 us
4096,8,1 223.35 us 3.55 us
4096,8,512 524.96 us 96.65 us
4096,16,1 229.88 us 3.56 us
4096,16,512 583.43 us 225.43 us
8192,8,1 440.14 us 5.37 us
8192,8,512 982.32 us 228.77 us
8192,16,1 453.22 us 5.39 us
8192,16,512 1050.02 us 455.96 us

ARGMAX:

shape base PR
32,10 1.77 us 1.75 us
1024,10 5.50 us 5.51 us
32000,512 287.33 us 96.25 us

Requirements

Add a two-pass partial reduction for large-row argmax and use cooperative threadgroup reductions for pool_1d. Allocate scratch space for the argmax partials through the Metal buffer allocator.

Assisted-by: pi:llama.cpp/ggml-org/DeepSeek-V4-Flash-Vision-Exp-GGUF (Mac Studio, M2 Ultra, 192GB)
@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 20, 2026
Add representative POOL_1D max cases for the MoE down-projection amax shapes, reducing the whole intermediate dim.

Assisted-by: pi:llama.cpp/ggml-org/DeepSeek-V4-Flash-Vision-Exp-GGUF (Mac Studio, M2 Ultra, 192GB)
@github-actions github-actions Bot added the testing Everything test related label Oct 20, 2026
@ggerganov
ggerganov marked this pull request as ready for review October 20, 2026 09:07
@ggerganov
ggerganov requested a review from a team as a code owner October 20, 2026 09:07
@ggerganov
ggerganov merged commit df11702 into master Oct 20, 2026
39 of 41 checks passed
@ggerganov
ggerganov deleted the gg/metal-argmax-pool1d branch October 20, 2026 13:46
edwardyoon pushed a commit to edwardyoon/focus-llama that referenced this pull request Oct 24, 2026
* metal : optimize argmax and pool_1d

Add a two-pass partial reduction for large-row argmax and use cooperative threadgroup reductions for pool_1d. Allocate scratch space for the argmax partials through the Metal buffer allocator.

Assisted-by: pi:llama.cpp/ggml-org/DeepSeek-V4-Flash-Vision-Exp-GGUF (Mac Studio, M2 Ultra, 192GB)

* tests : add pool_1d perf cases

Add representative POOL_1D max cases for the MoE down-projection amax shapes, reducing the whole intermediate dim.

Assisted-by: pi:llama.cpp/ggml-org/DeepSeek-V4-Flash-Vision-Exp-GGUF (Mac Studio, M2 Ultra, 192GB)

* cont : minor fix
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 testing Everything test related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant