Skip to content

model : deepseek41 DSpark speculative draft - #296

Open
danielhanchen wants to merge 9 commits into
dsv41-perffrom
dsv41-dspark
Open

danielhanchen wants to merge 9 commits into
dsv41-perffrom
dsv41-dspark

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Adds the DeepSeek-V4.1 DSpark speculative draft. V4.1 ships a 3-stage DSpark block drafter (mtp.0..2); it is exported as a separate DFLASH GGUF with convert_hf_to_gguf.py --dspark and used with --spec-type draft-dspark, like the DeepSeek-V4 DSpark draft.

Stacked on the performance PR (#295, dsv41-perf). The diff is only this change: 10 files, +425/-9.

What it adds

  • DFLASH graph for V4.1 (src/models/dflash.cpp), flagged by dflash.hyper_connection.single_pass:
    • Single-pass mHC: each sublayer collapses with the previous sublayer's pre-mix, and the output with the last FFN pre-mix, with no hc_head.
    • No per-head q norm and plain RoPE. The Markov and confidence heads are as in V4.
    • Block queries see the main-KV window up to the anchor plus the whole block. n_swa grows by one so that the first query's mask row reaches window_size positions before the anchor.
  • Target features: deepseek41 exports the mean over the HC copies of the layer input via embeddings_layer_inp.
  • Converter (DeepseekV41DSparkModel): MXFP4 experts bit-exact, dense weights BF16, conf_proj F32. The embeddings and LM head come from the target model.

Verification

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

  • Converter: the draft GGUF (78 tensors) matches the archived conversion: KV keys and values equal, tensor names and order equal, 0 data or type mismatches.
  • Exactness on the fixture (CPU): greedy llama-server output with the draft is identical to output without it, 300/300 tokens.
  • Acceptance on the real model (the published UD-Q8_K_XL GGUF, 6 prompts): 939 of 1,935 drafted tokens accepted (0.485). Earlier runs gave 0.415 to 0.477.
  • No effect without a draft: with the draft off, logits are bit-identical to the parent PR on CPU and CUDA fixtures.
  • Revision: the patches are unchanged from the previously gated revision; this revision only rebases onto the parent.

Known gaps

  • Greedy identity on CUDA: with real weights, greedy output with and without the draft diverges at near ties. Without a draft, the target itself disagrees with a batched re-score of its own output on 18/1024 tokens (19/1024 with the draft, the same maximum top-2 gap of 0.874), so this is batch-size numerics of the target, not a draft defect. On the all-F32 fixture the outputs are identical.
  • Speedup not measured: the base tokens/s arm was skipped because the host was heavily loaded and the experts ran on CPU, so there is no end-to-end speedup number yet.
  • Platforms: validated on CPU and CUDA only. No ROCm, Metal, Vulkan, Windows or macOS validation.

…ariant)

DeepSeek-V4.1 ships a 3-stage DSpark block drafter (mtp.0..2). It is
exported as a separate DFLASH GGUF with convert_hf_to_gguf.py --dspark and
used with --spec-type draft-dspark, like the V4 DSpark draft.

- DFLASH graph_dsv41: single-pass mHC (each sublayer collapses with the
  previous sublayer's pre-mix, output with the last FFN pre-mix, no
  hc_head), no per-head q norm, plain rope; markov and confidence heads
  as V4. Flagged by dflash.hyper_connection.single_pass.
- Block queries see the main-KV window up to the anchor plus the whole
  block; n_swa grows by one so the first query's mask row reaches
  window_size positions before the anchor.
- deepseek41 exports the target features (mean over the hc copies of the
  layer input) via embeddings_layer_inp.
- Converter: DeepseekV41DSparkModel (MXFP4 experts bit-exact, dense BF16,
  conf_proj F32; embeddings and lm head come from the target).
@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-19T07:56:50.063881Z 983f7aa PR opened
🔒 Security Review ✅ Completed 2026-10-19T07:54:27.598319Z 983f7aa 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.

…context

Drafts that borrow token_embd/output from the target (DFlash, DSpark, Eagle3)
could not be measured during --fit: their context requires ctx_other, so fit
logged 'failed to measure the memory of the extra model, fitting without it'
and the draft weights, KV cache and compute buffers were never reserved.

Measure a separate draft next to an allocation-free target context passed as
ctx_other (its memory stays counted with the main model) and apply what the
speculative decoder sets on the draft after creating it (backend top-k
samplers, nextn embeddings) before reading the breakdown, so the larger
compute graph reserved after speculative init is counted too. Normal
inference without ctx_other still throws.
@danielhanchen

Copy link
Copy Markdown
Member Author

Fix for --fit with a DSpark / DFlash draft (99a68fc14, merged up into #297 and #298).

Problem. A DSpark/DFlash draft borrows tok_embd / output from the target, so its context cannot be created without ctx_other. During --fit the extra-model measurement therefore failed with failed to measure the memory of the extra model, fitting without it, and nothing was reserved for the drafter (weights, KV, compute). The target was packed up to the margin and the drafter then hit cudaMalloc failed: out of memory, unless --fit-target was hand-tuned per device.

Fix.

  • A separate draft is now measured next to an allocation-free target context passed as ctx_other. Only the draft's own buffers are counted; the borrowed tensors stay charged to the target once. Drafts that do not borrow tensors ignore ctx_other, and their measurement is unchanged.
  • Before reading the breakdown, fit applies what the speculative decoder sets on the draft after creating it: the per-sequence backend top-k sampler (when --spec-draft-backend-sampling is on, the default) and nextn embeddings. Then it runs the pending reserve through a new ext call, llama_sched_reserve(). Without this step the larger graph reserved after speculative init (about n_ubatch x n_vocab floats for top-k) was still missed.
  • Unchanged:
    • Normal inference without ctx_other still throws.
    • The MTP path is untouched.
    • If the target context cannot be built, the draft is measured on its own as before.
  • The change avoids the lines common : add --moe-cache-mib auto to size the MoE expert cache from the fit #251 rewrites in common/fit.cpp, so the pin set still merges.

Evidence.

Tiny DSV4.1 + DSpark fixture, base vs head:

  • Base logs fitting without it.
  • Head reserves draft model 121.52 + KV 4.50 + compute 543.50 MiB. That matches the real allocations exactly on CUDA (B200) and on CPU (542.25 MiB).
  • Without a draft, the fit projection and greedy completions (with n_probs) are bitwise identical. With the draft, completions are also bitwise identical.

UD-IQ1_S + dspark-...-Q8_0 on one B200, with a 12000 MiB device budget:

fit result target on GPU draft on GPU total
base fitting without it 11882 MiB 8332 MiB 20214 MiB, 8218 MiB over budget (the drafter OOM)
head draft reserved 1 layer, 1836 MiB 8332 MiB, exactly as measured (7602 + 4 + 725) 10168 MiB, within budget

On -ot "per_layer_token_embd.weight=CPU": with -ot set, --fit stops on purpose (tensor_buft_overrides already set by user). For deepseek41 that override is not needed: the loader already keeps every per_layer_token_embd (Engram) tensor in host memory, and fit sees the same placement. Drop the -ot and use --fit on (plus --fit-target only for extra headroom).

Not covered: a multi-GPU split (tested on one device only) and a real 3090.

@danielhanchen

Copy link
Copy Markdown
Member Author

Pushed cf6d1a9 (merges the #289 output LoRA fix first). The DSpark draft now normalises the router weights as weights / (sum + 1e-20), like Gate.forward in the V4.1 reference. Before, the DFLASH arch fell through to the F16 clamp of 6.1e-5 on the sum.

With two selected router logits of -24, the reference gives [0.5, 0.5]. The DSpark graph gave [0.1007, 0.1007] (sum 0.201) before this change and [0.5, 0.5] after. The condition is keyed on dsv4_hc_single_pass, so other DFLASH drafters keep the clamp.

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