Skip to content

model : deepseek41 sparse attention, per-device KV mirrors, host gather - #295

Open
danielhanchen wants to merge 7 commits into
dsv41-chatfrom
dsv41-perf
Open

danielhanchen wants to merge 7 commits into
dsv41-chatfrom
dsv41-perf

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Makes DeepSeek-V4.1 decode fast at long context. With flash attention, attention runs over the window plus the top-k selected rows instead of every compressed row, the shared compressed KV is mirrored per device under layer split, and the token and Engram rows are gathered on the host without CPU graph splits. Each piece has a kill switch that restores the reference path.

Stacked on the chat PR (#294, dsv41-chat). The diff is only this change: 6 files, +375/-64.

What it adds

  • Sparse attention (flash attention only; LLAMA_DSV41_SPARSE=0 restores the reference graph, with logits bit-identical to it):
    • The indexer uses ggml_lightning_indexer on the strided index-K view and scores the ubatch as one chunk, with the same top-k selection.
    • Flash attention gets n_kv_max = window + top-k, so the CUDA sparse FA path runs.
    • Ubatches with at most 32 queries per stream gather only each query's top-k compressed rows and attend over [window | selected], one FA stream per query. Keys and mask values match the dense path; only the key order changes. Gathered rows and masks are shared by all layers that use the same top-k.
    • A quantized K cache (-ctk q8_0 / q4_0) takes the same paths. Without flash attention nothing changes.
  • Per-device KV mirrors under -sm layer (LLAMA_DSV41_KV_MIRROR=0 disables them):
    • Layers read the compressed KV and index K of their KV source layer. When a source and its consumers sat on different devices, the scheduler copied the whole compressed-cache view across devices once per consumer every ubatch (740 MB per decoded token at 64k depth on 2 cards).
    • The first consumer on another device now owns a mirror layer in the same llama_kv_cache (shared cell bookkeeping, sequence ops and clears). The source's new rows are appended to it in that consumer's graph, and every consumer on the device reads the local copy. Compressor state stays with the source.
    • The state format does not depend on the split: only KV source layers are serialized, and mirrors are refilled from their source on load. State saved under one split loads under any other, and in a build without mirrors.
  • Host gather (LLAMA_DSV41_HOST_GATHER=0 restores the in-graph GET_ROWS):
    • The token embedding and the Engram table live in host memory, so each GET_ROWS on them ran as its own CPU split (7 splits per decoded token on 2 cards).
    • The rows are now gathered and converted to F32 on the host in set_input, with the same to_float / memcpy as the CPU GET_ROWS, and enter the graph as F32 inputs: one split per device. It falls back to GET_ROWS for a device-resident table, LoRA on the embedding, or mixed batches.

Verification

All results are local, on B200 (CUDA sm_100a) and x86 CPU.

  • Decode speed (batch 1, tg128, median of 3, 2 x B200, exclusive cards, a third-party Q2_K GGUF, development builds of these commits):

    depth with this PR (t/s) without (t/s)
    0 78.0 68.5
    4k 74.3
    16k 72.5 25.0
    32k 72.7
    64k 72.6 8.2
    128k 73.1
    256k 71.7

    On this stack with one of our quants, tg128 is 64-70 t/s flat to 64k, against 66 / 25 / 9 t/s at 0 / 16k / 64k on the parent.

  • Prefill (pp4096, development build, same setup): 805 / 602 / 329 t/s at depth 0 / 16k / 64k before, 868 / 812 / 767 after.

  • Memory at 64k context (CUDA, flash attention on): compute buffers 820 MiB at ubatch 512 and 2,636 MiB at ubatch 2048, against 5,726 and 21,939 MiB on the reference path. No abort.

  • Accuracy:

    • Without flash attention, CPU and CUDA logits are bit-identical to the parent PR, including the long fixture at ubatch 2048.
    • Each kill switch gives logits bit-identical to the parent.
    • With flash attention the key order differs, so logits differ from the parent; fixture KL against the official F32 reference stays in the parent's band (CUDA, ubatch 512: identical KL to the parent, 8.0e-5 to 5.1e-4; ubatch 7: 3.3e-4 to 6.5e-4 against the parent's 3.8e-4 to 7.8e-4). The top-k selection sets are equal.
    • Full model at the top of the stack (defaults, flash attention, the published UD-Q8_K_XL GGUF on 2 x B200): mean KL 5.95e-3 on wiki1100 and 3.10e-3 on long17k against the official F32 reference, bit-identical across runs. UD-Q4_K_XL: 9.81e-3 / 4.44e-3. The official native kernels score 2.62e-2 / 1.50e-2.
  • Ops: FLASH_ATTN_EXT 4004/4004 on CUDA; the other CUDA op tests pass.

  • State: 48/48 cross-split state loads pass (saved under one -ts split, loaded under another).

  • The quantized-cache gate passes on the sparse and gather paths.

  • Revision: the patches are unchanged from the previously gated revision; this revision only rebases onto the parent.

Known gaps

  • The speed table was measured on development builds of these commits with a third-party Q2_K GGUF, not on this exact revision; the stack-level 64-70 t/s numbers use one of our quants.
  • Layer split runs the two cards one after the other. At batch 1 on the same two cards, vLLM with TP2 is about 2.25x faster.
  • The sparse path needs flash attention; without it the reference path runs.
  • Platforms: validated on CPU and CUDA only. No ROCm, Metal, Vulkan, Windows or macOS validation.

…er layer split

Layers read the compressed KV (and index K) of their kv source layer.
With -sm layer a source and its consumers can sit on different devices,
and the scheduler then copied the whole compressed-cache view across
devices once per consumer every ubatch (740 MB per decoded token at 64k
depth on 2 cards).

The first consumer on a device other than its source's now owns a
mirror cache layer in the same llama_kv_cache (shared cell bookkeeping,
seq ops and clears). The source's new compressed rows are appended to it
in that consumer's graph and every consumer on the device reads the
local copy. llama_dsv41_kv_read_layer() is the single definition used
by the cache and the graph. Compressor state stays with the source. No
change on one device or without KV offload; LLAMA_DSV41_KV_MIRROR=0
disables it.

The state format does not depend on the split: only kv source layers
are serialized and every mirror is refilled from its source on read, so
state saved under one split loads under any other and by a build
without mirrors.
…r, bounded FA, decode gather)

The reference graph attends over [window | every compressed row] with
the top-k as a mask, scores the indexer with an unfused chain over an
[n_idx_head, n_tokens, n_comp] F32 intermediate and passes n_kv_max = 0
to flash attention. With flash attention (F16 masks):

- the indexer uses ggml_lightning_indexer on the strided index-K view,
  scoring the ubatch as one chunk with the same top-k selection
- FA gets n_kv_max = window + top-k, so the CUDA sparse FA path runs
- ubatches with at most 32 queries per stream gather only each query's
  top-k compressed rows and attend over [window | selected] with one FA
  stream per query; keys and mask values match the dense path, only the
  key order changes. Gathered rows and masks are shared by all layers
  using the same top-k.

A quantized K cache (-ctk q8_0 / q4_0) takes the same paths. Without
flash attention nothing changes. LLAMA_DSV41_SPARSE=0 restores the
reference path (logits bitwise equal to it).
…PU graph splits)

The token embedding and the Engram table live in host memory, so each
GET_ROWS on them ran as its own CPU split between device splits (7
splits per decode token on 2 cards). The rows are now gathered and
converted to F32 on the host in set_input (same to_float / memcpy as the
CPU GET_ROWS, so bitwise identical) and enter the graph as F32 inputs,
leaving one split per device. build_inp_embd() gets an opt-in
host_gather flag that falls back to GET_ROWS when it cannot apply
(device-resident table, LoRA on the embedding, Hadamard-latent table,
mixed batches).

LLAMA_DSV41_HOST_GATHER=0 restores the in-graph GET_ROWS.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-19T08:02:30.665684Z ac17c1c PR opened
🔒 Security Review ✅ Completed 2026-10-19T07:59:36.661705Z ac17c1c PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant