Skip to content

Metal: tune DeepSeek V4.1 IQ2 prefill tiles on M3 Ultra - #1385

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

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

Conversation

@forest1040

Copy link
Copy Markdown

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=1 selects the previous kernels.

Full-model comparison

Mac Studio M3 Ultra 512 GiB, Metal, DeepSeek-V4.1-Flash-1017-IQ2_XXS-Q2_K-imatrix.gguf, and speed-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.

Context reached Prefill before → after (tok/s) Change Steady decode before → after (tok/s)
32 Ki 810.92 → 825.24 +1.77% 39.04 → 39.18
64 Ki 775.54 → 789.70 +1.83% 38.86 → 38.99
128 Ki 753.14 → 766.54 +1.78% 38.61 → 38.70

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

  • All 129,280 frontier logits matched bitwise at 32, 64, and 128 Ki.
  • A separate 32 Ki audit matched 4,077,060 ordered expert IDs across 40 layers. The full float32 logits immediately after prefill and after evaluating the same next token also matched bitwise.
  • make -j8 ds4-bench tests/test_deepseek41_metal and ./tests/test_deepseek41_metal passed.

To compare throughput from one build, run the following with the disable flag set and then with it unset:

export DS4_METAL_DISABLE_V41_IQ2_PREFILL_TILES=1  # Remove this export for the optimized run.
DS4_V41_ENGRAM_WARM_WAIT=1 DS4_METAL_MEMORY_REPORT=1 \
  /usr/bin/time -l ./ds4-bench --metal -m "$MODEL" \
  --prompt-file speed-bench/promessi_sposi.txt --ctx-start 32768 --ctx-max 131072 \
  --step-mul 2 --ctx-alloc 196608 --gen-tokens 32

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.
Copilot AI balanced review requested due to automatic review settings December 4, 2026 04:44

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 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

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