Repository navigation
Metal: reuse DeepSeek V4.1 indexer keys across 16 queries on M3 Ultra - #1387
Open
forest1040 wants to merge 2 commits into
Open
forest1040 wants to merge 2 commits into
forest1040 wants to merge 2 commits into
Conversation
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.
There was a problem hiding this comment.
🟡 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=1keeps 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
| /* 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 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") : |
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, 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=1selects 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.ggufandspeed-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.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_metaland./tests/test_deepseek41_metalpassed 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