Repository navigation
Metal: tune DeepSeek V4.1 IQ2 prefill tiles on M3 Ultra - #1385
Open
forest1040 wants to merge 1 commit into
Open
forest1040 wants to merge 1 commit into
forest1040 wants to merge 1 commit into
Conversation
Reuse the compact 32-by-32 IQ2_XXS gate/up tile and skip empty half-tiles in the Q2_K down projection. Keep GPU routing and the accumulation order for valid outputs.
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are recorded for the Metal prefill kernel-selection changes.
No open issues 🎉
What changed in this PR
Tunes DeepSeek V4.1’s resident IQ2_XXS/Q2_K Metal prefill path for M3 Ultra using existing kernel specializations.
Changes:
- Exposes compact 32×32 fused gate/up and tail-culling down kernels.
- Selects them for eligible fused FP16-intermediate prefill batches of at least 2,048 tokens.
- Adds an environment flag to restore the previous kernels.
| File | Description |
|---|---|
| metal/moe.metal | Exposes IQ2_XXS compact gate/up and Q2_K tail-culling down specializations. |
| ds4_metal.m | Adds guarded kernel selection and matching compact-tile dispatch. |
Review effort: Balanced
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.
Summary
On M3 Ultra, use the existing 32×32 fused gate/up template for DeepSeek V4.1's IQ2_XXS weights and the existing tail-culling template for its Q2_K down projection. The down kernel skips MMA for empty 16-row halves. Both paths preserve the accumulation order for valid outputs and use the same route map and buffers.
Automatic selection is restricted to the resident V4.1 shape with this quantization pair and at least 2,048 tokens in the fused FP16-intermediate prefill path.
DS4_METAL_DISABLE_V41_IQ2_PREFILL_TILES=1selects the previous kernels.Full-model comparison
Mac Studio M3 Ultra 512 GiB, Metal,
DeepSeek-V4.1-Flash-1017-IQ2_XXS-Q2_K-imatrix.gguf, andspeed-bench/promessi_sposi.txt. Each build ran once with a 196,608-token capacity. The sequence reaches 32, 64, and 128 Ki in one session. The prefill rate measures each new input interval. Steady decode uses the last 31 of 32 generated tokens at each frontier.Static context buffers were 10,017.03 MiB in both runs. Peak process footprint was 11.55 GiB in both runs, with an absolute difference below 5 MiB. Weight mappings are accounted for separately.
At 128 Ki, the baseline Metal trace attributed 36.88 GPU seconds to fused gate/up and 17.92 seconds to down, together 32.3% of the traced GPU time. In an 8,192-row synthetic probe using the production dimensions, making identical expert weights share addresses reduced each kernel's time by at most 1.1%. The compact gate/up tile and down tail culling reduced their separate kernel times by 5.5% and 3.7%, respectively, with bitwise-identical outputs.
A separate four-way test also combined this commit with the attention change in #1384. Together they improved prefill by 4.46–4.56% at the same frontiers, and all four configurations produced bitwise-identical frontier logits. This PR is based directly on
main.Validation
make -j8 ds4-bench tests/test_deepseek41_metaland./tests/test_deepseek41_metalpassed.To compare throughput from one build, run the following with the disable flag set and then with it unset: