Repository navigation
Metal: share selected KV across 16 heads in DeepSeek V4.1 prefill on M3 Ultra - #1384
Open
forest1040 wants to merge 1 commit into
Open
forest1040 wants to merge 1 commit into
forest1040 wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
🟢 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
This was referenced Dec 4, 2026
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.
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, andspeed-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.Process peak is the macOS
time -lfootprint. The ds4 static context-buffer measurement is reported separately. The candidate-mask compression in #1383 was tested separately. This patch applies directly tomain.Verification
make -j8 ds4-bench tests/test_deepseek41_metal./tests/test_deepseek41_metal --tp-attentionpassed on M3 Ultra.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.csvFor the 128 Ki full-input run, use
--ctx-start 131072 --ctx-max 131072 --ctx-alloc 196608.