Repository navigation
model : deepseek41 sparse attention, per-device KV mirrors, host gather - #295
Open
danielhanchen wants to merge 7 commits into
Open
danielhanchen wants to merge 7 commits into
danielhanchen wants to merge 7 commits into
Conversation
…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.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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
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.
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
LLAMA_DSV41_SPARSE=0restores the reference graph, with logits bit-identical to it):ggml_lightning_indexeron the strided index-K view and scores the ubatch as one chunk, with the same top-k selection.n_kv_max = window + top-k, so the CUDA sparse FA path runs.[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.-ctk q8_0/q4_0) takes the same paths. Without flash attention nothing changes.-sm layer(LLAMA_DSV41_KV_MIRROR=0disables them):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.LLAMA_DSV41_HOST_GATHER=0restores the in-graphGET_ROWS):GET_ROWSon them ran as its own CPU split (7 splits per decoded token on 2 cards).set_input, with the sameto_float/memcpyas the CPUGET_ROWS, and enter the graph as F32 inputs: one split per device. It falls back toGET_ROWSfor 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):
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:
Ops:
FLASH_ATTN_EXT4004/4004 on CUDA; the other CUDA op tests pass.State: 48/48 cross-split state loads pass (saved under one
-tssplit, 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