Skip to content

metal: deepseek v4 sparse prefill - #26512

Closed
tarruda wants to merge 2 commits into
ggml-org:masterfrom
tarruda:dsv4-metal-sparse-attention
Closed

metal: deepseek v4 sparse prefill#26512
tarruda wants to merge 2 commits into
ggml-org:masterfrom
tarruda:dsv4-metal-sparse-attention

Conversation

@tarruda

@tarruda tarruda commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Overview

Picking up initial sparse prefill work from my branch

Additional information

This will greatly improve prefill performance as context grows. Here's -d 0,10000,20000,30000 -n 128 -p 2048 -b 2048 -ub 2048 on a M1 ultra before these changes:

| model                          |       size |     params | backend    | threads | n_ubatch |  fa |            test |                  t/s |
| ------------------------------ | ---------: | ---------: | ---------- | ------: | -------: | --: | --------------: | -------------------: |
| deepseek4 ?B Q8_0              |  91.94 GiB |   284.33 B | MTL,BLAS   |      16 |     2048 |   1 |          pp2048 |        196.04 ± 0.92 |
| deepseek4 ?B Q8_0              |  91.94 GiB |   284.33 B | MTL,BLAS   |      16 |     2048 |   1 |           tg128 |         21.62 ± 0.06 |
| deepseek4 ?B Q8_0              |  91.94 GiB |   284.33 B | MTL,BLAS   |      16 |     2048 |   1 | pp2048 @ d10000 |        100.29 ± 0.30 |
| deepseek4 ?B Q8_0              |  91.94 GiB |   284.33 B | MTL,BLAS   |      16 |     2048 |   1 |  tg128 @ d10000 |         19.47 ± 0.04 |
| deepseek4 ?B Q8_0              |  91.94 GiB |   284.33 B | MTL,BLAS   |      16 |     2048 |   1 | pp2048 @ d20000 |         68.30 ± 0.23 |
| deepseek4 ?B Q8_0              |  91.94 GiB |   284.33 B | MTL,BLAS   |      16 |     2048 |   1 |  tg128 @ d20000 |         17.52 ± 0.04 |
| deepseek4 ?B Q8_0              |  91.94 GiB |   284.33 B | MTL,BLAS   |      16 |     2048 |   1 | pp2048 @ d30000 |         53.02 ± 0.14 |
| deepseek4 ?B Q8_0              |  91.94 GiB |   284.33 B | MTL,BLAS   |      16 |     2048 |   1 |  tg128 @ d30000 |         16.77 ± 0.03 |

After:

| model                          |       size |     params | backend    | threads | n_ubatch |  fa |            test |                  t/s |
| ------------------------------ | ---------: | ---------: | ---------- | ------: | -------: | --: | --------------: | -------------------: |
| deepseek4 ?B Q8_0              |  91.94 GiB |   284.33 B | MTL,BLAS   |      16 |     2048 |   1 |          pp2048 |        194.72 ± 0.88 |
| deepseek4 ?B Q8_0              |  91.94 GiB |   284.33 B | MTL,BLAS   |      16 |     2048 |   1 |           tg128 |         21.78 ± 0.13 |
| deepseek4 ?B Q8_0              |  91.94 GiB |   284.33 B | MTL,BLAS   |      16 |     2048 |   1 | pp2048 @ d10000 |        189.77 ± 0.63 |
| deepseek4 ?B Q8_0              |  91.94 GiB |   284.33 B | MTL,BLAS   |      16 |     2048 |   1 |  tg128 @ d10000 |         19.50 ± 0.04 |
| deepseek4 ?B Q8_0              |  91.94 GiB |   284.33 B | MTL,BLAS   |      16 |     2048 |   1 | pp2048 @ d20000 |        181.26 ± 0.51 |
| deepseek4 ?B Q8_0              |  91.94 GiB |   284.33 B | MTL,BLAS   |      16 |     2048 |   1 |  tg128 @ d20000 |         17.58 ± 0.05 |
| deepseek4 ?B Q8_0              |  91.94 GiB |   284.33 B | MTL,BLAS   |      16 |     2048 |   1 | pp2048 @ d30000 |        174.03 ± 0.57 |
| deepseek4 ?B Q8_0              |  91.94 GiB |   284.33 B | MTL,BLAS   |      16 |     2048 |   1 |  tg128 @ d30000 |         16.55 ± 0.18 |

The official inference code was used as reference for this.

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: Yes, 100% GPT 5.6 Sol. I'm not a ML kernel programmer and have no idea what I'm doing. If anyone wants to create a new PR that cleans up, improves or rewrite these changes, they have my blessing to do so.

tarruda added 2 commits August 3, 2026 08:58
Replace dense compressed-attention scans during DeepSeek V4 prefill with
a backend-neutral sparse path that packs the sliding window and
Lightning Indexer selections into a compact working set.

- Add DSV4_SPARSE_PACK with a CPU reference and backend capability
  probes.
- Extend Flash Attention with broadcast masks and row-indexed sinks.
- Select sparse prefill graphs when supported while preserving dense
  fallback.
- Test sparse packing and tiled and padded Flash Attention paths.

Assisted-by: Codex
Implement the sparse DeepSeek V4 prefill path on Metal so Flash
Attention consumes compact per-token key sets instead of scanning the
full compressed cache.

- Add a DSV4_SPARSE_PACK kernel for raw-window and selected keys.
- Add an exact radix TOP_K specialization for 512 Lightning Indexer
  results.
- Extend tiled Flash Attention with broadcast masks and row-indexed
  sinks.
- Register backend support for the fused packing operation.

Assisted-by: Codex
@tarruda

tarruda commented Aug 3, 2026

Copy link
Copy Markdown
Contributor Author

The commits are split into common/metal, but can also create separate PRs if desired.

@github-actions github-actions Bot added model Model specific testing Everything test related ggml changes relating to the ggml tensor library for machine learning Apple Metal https://en.wikipedia.org/wiki/Metal_(API) labels Aug 3, 2026
Comment thread ggml/include/ggml.h
Comment on lines +2611 to +2621
// Packs per-token raw-window and Lightning-Indexer selections into the
// strided K + mask storage consumed by DeepSeek V4 sparse flash attention.
GGML_API struct ggml_tensor * ggml_dsv4_sparse_pack(
struct ggml_context * ctx,
struct ggml_tensor * raw_k,
struct ggml_tensor * comp_k,
struct ggml_tensor * raw_mask,
struct ggml_tensor * comp_mask,
struct ggml_tensor * comp_idx,
int64_t n_raw);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Earlier I didn't notice that this involves a new ggml OP. So, likely will not get merged as I think we are already working on similar functionality in another PR.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ggerganov are you talking about #25917?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

@tarruda

tarruda commented Aug 3, 2026

Copy link
Copy Markdown
Contributor Author

@ggerganov closing it then. My branch still has optimizations on top of this PR. For reference here are the current numbers:

| model                          |       size |     params | backend    | threads | n_ubatch |  fa |            test |                  t/s |
| ------------------------------ | ---------: | ---------: | ---------- | ------: | -------: | --: | --------------: | -------------------: |
| deepseek4 ?B Q8_0              |  91.94 GiB |   284.33 B | MTL,BLAS   |      16 |     2048 |   1 |          pp2048 |        235.00 ± 0.63 |
| deepseek4 ?B Q8_0              |  91.94 GiB |   284.33 B | MTL,BLAS   |      16 |     2048 |   1 |           tg128 |         25.70 ± 0.07 |
| deepseek4 ?B Q8_0              |  91.94 GiB |   284.33 B | MTL,BLAS   |      16 |     2048 |   1 | pp2048 @ d10000 |        222.04 ± 0.71 |
| deepseek4 ?B Q8_0              |  91.94 GiB |   284.33 B | MTL,BLAS   |      16 |     2048 |   1 |  tg128 @ d10000 |         22.98 ± 0.06 |
| deepseek4 ?B Q8_0              |  91.94 GiB |   284.33 B | MTL,BLAS   |      16 |     2048 |   1 | pp2048 @ d20000 |        213.28 ± 0.71 |
| deepseek4 ?B Q8_0              |  91.94 GiB |   284.33 B | MTL,BLAS   |      16 |     2048 |   1 |  tg128 @ d20000 |         22.41 ± 0.04 |
| deepseek4 ?B Q8_0              |  91.94 GiB |   284.33 B | MTL,BLAS   |      16 |     2048 |   1 | pp2048 @ d30000 |       188.02 ± 16.30 |
| deepseek4 ?B Q8_0              |  91.94 GiB |   284.33 B | MTL,BLAS   |      16 |     2048 |   1 |  tg128 @ d30000 |         21.96 ± 0.13 |

After the new op is implemented on master, I can re-evaluate there are still things that can be cherry-picked to contribute

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Apple Metal https://en.wikipedia.org/wiki/Metal_(API) ggml changes relating to the ggml tensor library for machine learning model Model specific testing Everything test related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants