Skip to content

Metal: share selected KV across 16 heads in DeepSeek V4.1 prefill on M3 Ultra - #1384

Open
forest1040 wants to merge 1 commit into
antirez:mainfrom
forest1040:deepseek-v41-metal-prefill-attention-main
Open

forest1040 wants to merge 1 commit into
antirez:mainfrom
forest1040:deepseek-v41-metal-prefill-attention-main

Conversation

@forest1040

Copy link
Copy Markdown

Enable the existing 16-head indexed mixed attention kernel for DeepSeek V4.1 prefill on M3 Ultra. Sharing the selected KV rows across 16 heads cuts repeated staging while preserving the existing key traversal and the arithmetic order per head. The M3 selector is limited to ratios 1/2, 32/64 heads, a 512-row shortlist, a 128-row local window, and head dimension 512. The single-token decode and quality selectors retain their current behavior.

Measurements

Mac Studio M3 Ultra, 512 GiB, DeepSeek-V4.1-Flash-1017-IQ2_XXS-Q2_K-imatrix.gguf, and speed-bench/promessi_sposi.txt. Each frontier generated 32 greedy tokens. Steady decode covers the last 31. The 64 Ki row is the median of two runs per mode and measures the second 32 Ki tokens in the same session. The 32 Ki row uses the second run in each mode because the first baseline showed a cold-start outlier. The 128 Ki row measures one full-input run per mode.

Context frontier Prefill tok/s, baseline → dual Prefill change Steady decode tok/s, baseline → dual Static buffers MiB, baseline → dual Process peak MiB, baseline → dual
32 Ki 812.47 → 832.24 +2.43% 39.25 → 39.19 9,248.00 → 9,248.00 10,888.47 → 10,887.55
64 Ki 777.18 → 797.78 +2.65% 39.06 → 39.09 9,248.00 → 9,248.00 10,888.99 → 10,887.75
128 Ki 792.00 → 812.71 +2.61% 38.85 → 38.76 10,017.03 → 10,017.03 11,397.38 → 11,396.57

Process peak is the macOS time -l footprint. The ds4 static context-buffer measurement is reported separately. The candidate-mask compression in #1383 was tested separately. This patch applies directly to main.

Verification

  • make -j8 ds4-bench tests/test_deepseek41_metal
  • ./tests/test_deepseek41_metal --tp-attention passed on M3 Ultra.
  • With this GGUF, the two modes produced bitwise-identical full-vocabulary logits at 16,384, 16,385, 32,768, and 32,769 tokens, covering prefill and the following decode. A diagnostic build also compared the complete ordered selected-key ID streams produced before the attention kernels.

Reproduce

Take the existing eight-head path by setting DS4_METAL_DISABLE_V41_PREFILL_INDEXED_DUAL=1; unset it for the new default.

./ds4-bench --metal -m "$MODEL" --prompt-file speed-bench/promessi_sposi.txt \
  --ctx-start 32768 --ctx-max 65536 --step-incr 32768 --ctx-alloc 131072 \
  --gen-tokens 32 --csv results.csv

For the 128 Ki full-input run, use --ctx-start 131072 --ctx-max 131072 --ctx-alloc 196608.

Copilot AI balanced review requested due to automatic review settings December 4, 2026 00:58

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues block the M3 Ultra prefill-kernel selection change.

No open issues 🎉

What changed in this PR

Enables the existing 16-head indexed attention kernel for DeepSeek V4.1 prefill on M3 Ultra, reducing repeated KV staging.

Changes:

  • Adds M3 Ultra eligibility for ratios 1/2 with the specified head counts and attention dimensions.
  • Provides an environment-variable opt-out for the new path.
  • Preserves existing decode, quality-mode, and MPP selection behavior.
File Description
ds4_metal.m Extends the prefill kernel selector to supported M3 Ultra configurations.

Review effort: Balanced

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants