Skip to content

Metal: reuse DeepSeek V4.1 indexer keys across 16 queries on M3 Ultra - #1387

Open
forest1040 wants to merge 2 commits into
antirez:mainfrom
forest1040:deepseek-v41-metal-indexer-mma-main
Open

forest1040 wants to merge 2 commits into
antirez:mainfrom
forest1040:deepseek-v41-metal-indexer-mma-main

Conversation

@forest1040

Copy link
Copy Markdown

Summary

On M3 Ultra, retain each DeepSeek V4.1 indexer key fragment in registers across two groups of eight queries. The variant uses the same 8-query MMA, per-query causal eligibility, and head reduction order. It is selected for at least 16 queries and 8,192 keys. DS4_METAL_DISABLE_V41_INDEX_MMA_REUSE_K=1 selects the original 8-query path.

The existing causal test already skips whole invisible tiles. On the 256 Ki trace it skipped 2.93% of the populated key tiles. The new kernel performs the same number of live MMAs and roughly halves logical key-row loads.

Standalone performance on main

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 262,400-token allocation, a fresh 256 Ki prefill, and 32 generated tokens. Engram file pages were warmed before timing.

Metric main main + indexer reuse
Prefill (tok/s) 743.43 754.08 (+1.43%)
Prefill time (s) 352.61 347.63
Steady decode (tok/s) 38.31 38.42
Peak process footprint (GiB) 11.858 11.858

The full 129,280-token frontier logit vectors match bit for bit. Static context buffers are 10,789.07 MiB in both runs.

Indexer validation

In a separate comparison using the same input and both the attention (#1384) and MoE (#1385) improvements, scoring took 10.13 → 8.91 GPU s at 128 Ki and 37.08 → 32.17 GPU s at 256 Ki. Causal top-512 sorting took 3.32 → 3.29 and 11.39 → 11.36 GPU s, respectively. Ordered candidate-ID hashes matched across 609,564,672 IDs at the two tested contexts. Frontier logits also matched. The standalone change in this PR applies directly to main.

Synthetic tests covered partially visible tiles, odd query counts, both compression ratios, and tied scores. All scores and ordered top-512 IDs matched bit for bit. make -j8 ds4-bench tests/test_deepseek41_metal and ./tests/test_deepseek41_metal passed on the PR branch.

Run this on the baseline and on the PR branch:

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 262144 --ctx-max 262144 \
  --ctx-alloc 262400 --gen-tokens 32

Keep the existing eight-query MMA and causal masking order while retaining each key fragment for a second query tile. On the 256 Ki fixture, index scoring falls from 37.08 to 32.17 GPU seconds and all ordered top-512 IDs and frontier logits match.
Copilot AI balanced review requested due to automatic review settings December 4, 2026 07:29

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.

🟡 Changes recommended

On M3 Ultra, eligible prefills can fail with older shaders because the reuse path lacks a fallback, and the existing test never exercises that path.

2 open issues
What changed in this PR

This PR speeds up DeepSeek V4.1 Metal prefill on M3 Ultra by reusing indexer keys across 16 queries. The PR reports 1.43% higher prefill throughput at 256 Ki tokens, with bit-for-bit identical frontier logits.

Changes:

  • Reuse each key fragment across two groups of eight queries without changing causal masking or arithmetic order.
  • Select the new kernel on M3 Ultra for batches with at least 16 queries and 8,192 keys. DS4_METAL_DISABLE_V41_INDEX_MMA_REUSE_K=1 keeps the original eight-query path.
File Description
metal/​dsv4_misc.metal Adds a 16-query scoring variant that reuses key fragments.
ds4_metal.m Selects the scoring kernel and sets the matching dispatch size.

Review effort: Balanced

Comment thread ds4_metal.m Outdated
/* V4.1's f32 tiles on simdgroup matrices, K^T held in registers */
const bool use_mma = use_tiled && use_tiled_f32 && n_tokens >= 8u &&
!getenv("DS4_METAL_DISABLE_V41_INDEX_MMA");
const bool reuse_keys = use_mma && n_tokens >= 16u && n_rows >= 8192u &&
Comment thread ds4_metal.m Outdated
Comment on lines +37744 to +37745
use_mma ? ds4_gpu_get_pipeline(reuse_keys ? "kernel_dsv41_indexer_scores_mma_reuse_k" :
"kernel_dsv41_indexer_scores_mma") :
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