Skip to content

deepseek41 : chunk the indexer and the attention over the ubatch - #33617

Open
urmuzov wants to merge 1 commit into
ggml-org:masterfrom
urmuzov:ds41-pp-chunk
Open

urmuzov wants to merge 1 commit into
ggml-org:masterfrom
urmuzov:ds41-pp-chunk

Conversation

@urmuzov

@urmuzov urmuzov commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Overview

This chunks the DeepSeek-V4.1 fused indexer and the dense attention over the queries of a ubatch, so the [n_comp, tokens] scores and the [n_raw + n_comp, tokens] masks are only allocated for one chunk at a time. At 640k context and -ub 4096 the compute buffer of the GPU that holds the candidate layers goes from 20138 MiB to 14768 MiB, at the same prefill speed.

The fused indexer was always called on the whole ubatch, and the attention builds its masks for all tokens of the ubatch at once:

ggml_tensor * sel_mask = build_top_k_mask(ci.kq_mask, top_k, "comp_top_k_mask", il);
ggml_tensor * kq_mask = ggml_concat(ctx0, raw_mask, sel_mask, 0);
cb(kq_mask, "comp_kq_mask", il);
// at most window + top-k visible columns per query: lets CUDA FA gather only those
const int64_t n_kv_max = sparse ? std::min<int64_t>(raw_mask->ne[0], hparams.n_swa) + k_sel : 0;
out = build_attn_mha(q, k_all, k_all, kq_mask, nullptr, nullptr, nullptr, layer.attn_sinks, nullptr, n_kv_max, kq_scale, il);

Now the indexer uses chunks of about 2^28 score elements and the attention splits the queries into equal chunks of at most about 2^28 mask cells. Each chunk is expanded into the graph before the next one, so the allocator can reuse its memory.

The chunks of the input masks are views that all layers share, like gather_mask_of:

llama.cpp/src/models/models.h

Lines 1580 to 1584 in d4adfb4

// gather path, shared by layers with the same top-k: selected K rows per (storage layer, top-k source),
// the sorted top-k and the mask per top-k source (one input copy per split)
std::map<std::pair<int32_t, int32_t>, ggml_tensor *> gather_k_of;
std::map<int32_t, ggml_tensor *> gather_sel_of;
std::map<int32_t, ggml_tensor *> gather_mask_of;

Without this, each layer makes new views of the input masks, and the scheduler copies every view to the GPU again. In my first version this added 2.2 TB of host to device copies in a 262k token pass and made prefill 9% slower.

Additional information

The output is not bit-identical, because the attention runs on smaller query batches. Perplexity is the same within the error (numbers below). The chunked path only runs with one sequence stream; with more streams the code takes the old path.

Measurements

2x RTX 5090, UD-Q4_K_XL, f16 KV, experts on the CPU (--n-cpu-moe 40 -ts 0.48,0.52), master d4adfb4 plus one local commit that adds the MXFP8 tensor type of the Unsloth engram shard (see #33316). Compute buffers from sched_reserve at -c 655360:

                 -ub 2048               -ub 4096
                 CUDA0      CUDA1       CUDA0      CUDA1
master           6740 MiB   10109 MiB   11235 MiB  20138 MiB
this PR          5492 MiB    8443 MiB    8705 MiB  14768 MiB

Whole-graph compute buffer on one device (CPU, llama-fit-params), master vs this PR:

-c 655360  -ub 2048   12925 -> 7100 MiB
-c 655360  -ub 4096   20723 -> 12078 MiB
-c 131072  -ub 2048    2577 -> 2342 MiB
-c 131072  -ub 4096    5173 -> 4324 MiB

llama-perplexity -c 262144 --chunks 1 -b 4096 -ub 4096, same machine and settings:

master    435.4 s per pass   PPL = 1.2185 +/- 0.00307
this PR   433.7 s per pass   PPL = 1.2186 +/- 0.00306

test-llama-archs -a deepseek41 passes on CPU, CUDA0 and CUDA1. The test model is too small to reach the chunk sizes, so I also ran it with the chunks forced to 1/4 of the ubatch (indexer) and 16 tokens (attention); both paths ran and the result is the same (NMSE vs. CPU 3.11e-06 on both GPUs). The Meta config could not run on this machine (NCCL fails in my container on master as well).

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: YES. Claude (Opus 5.5) wrote the change, found the extra mask copies with Nsight Systems and ran the measurements on my machine; I reviewed and tested the result.

Written with Claude Opus 5.5

This branch has not been deployed

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

Labels

model Model specific

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant