Skip to content

feat(speculative): add DeepSeek-V4.1 bundled DSpark drafter (#3262) - #3686

Draft
inureyes wants to merge 1 commit into
mainfrom
feat/issue-3262-deepseek-v41-dspark
Draft

inureyes wants to merge 1 commit into
mainfrom
feat/issue-3262-deepseek-v41-dspark

Conversation

@inureyes

@inureyes inureyes commented Dec 2, 2026

Copy link
Copy Markdown
Member

Summary

Every DeepSeek-V4.1 checkpoint ships a three-stage DSpark drafter under mtp.0 to mtp.2. This PR makes the same checkpoint usable as --draft-model / --model-draft, so DFlash speculative decoding runs against the V4.1 target.

  • src/models/deepseek_v41_dspark.rs (plus _tests.rs) implements the Drafter trait as a DFlash-kind drafter. It imports the V4.1 decoder pieces (V41Moe, V41Rope, fake_quant_fp8_ue8m0, the HyperConnections, hc_collapse) rather than copying them, canonicalizes mtp.N.* onto stages.N.*, repacks native scale sidecars, stacks the MoE experts, splits the flattened grouped wo_a into [o_groups, o_lora_rank, -1], drops the unused confidence head, and keeps a quantized Markov head packed by loading it through VanillaMarkovHead::from_weights (with the markov_head.embed / markov_head.head rename).
  • src/models/deepseek_v41.rs gains the capture_layer_ids tap (the HyperConnection-mean input, taken after the engram write) and a speculation transaction on the model-owned V4.1 cache: per-token snapshots, commit(accepted), abort(), and errors for a nested start or an out-of-range commit. Verification replays each row as one classic decode step, so temperature-0 output is byte-identical by construction.
  • Core detection resolves a deepseek_v41 / deepseek_v41_text directory to DrafterKind::Dflash when the last stage's mtp.{n-1}.markov_head.* tensors exist. That arm sits above the generic num_nextn_predict_layers rule from feat: Load bundled Qwen 3.5 MTP drafters and detect native MTP heads #2400, and core load_drafter refuses the family with BinaryCrateDrafter.
  • Server and CLI wiring: DFlashDrafterFamily::DeepseekV41DSpark, DFlashTargetModel for the text and VL models (EveryPromptRow, supports_batched() = false, exactness allowed at every width), the binary-side drafter_loader, block-size peeking (so the default is the runtime min(2, block_size)), and the offline mlxcel generate --draft-model and server --model-draft paths.

Validation

Unit tests run on this host:

  • deepseek_v41_dspark_tests::config_derives_block_and_runtime_width (3 stages, verify width 6, runtime width 2, mask 128799, taps [37, 38, 39], explicit runtime width wins).
  • deepseek_v41_dspark_tests::sanitize_maps_stages_and_drops_confidence_head (stage rename, expert stacking, grouped wo_a, f32 router, confidence head dropped).
  • deepseek_v41_dspark_tests::quantized_markov_head_loads_without_cast (packed factors stay packed and match the dense reference within 1e-3).
  • deepseek_v41_dspark_tests::a_v41_checkpoint_with_markov_tensors_resolves_to_dflash_before_generic_mtp (the ordering requirement: DFlash, not Mtp, and core refuses with BinaryCrateDrafter).
  • deepseek_v41_dspark_tests::without_markov_tensors_the_generic_mtp_rule_still_captures_a_v41_declaration (the control that makes the ordering meaningful).
  • deepseek_v41_tests::speculation_commit_equals_shorter_forward (committing n = 0..=4 of a 4-token transaction equals a plain forward over n tokens, with compression and engram state).
  • deepseek_v41_tests::speculation_rejects_a_nested_start_and_an_out_of_range_commit.
  • dflash_target::a_v41_target_requires_its_bundled_dspark_and_declines_lfm2.

Also green: cargo fmt --check; cargo clippy -p mlxcel-core --lib --tests -- -D warnings; cargo clippy -p mlxcel --lib --tests --bins -- -D warnings; check_env_var_docs.py, check_cross_repo_refs.py, check_kernel_dtype_keys.py, check_crate_versions.py, check_technical_reports_layout.py.

DEFERRED: the integrated real-checkpoint acceptance criterion (non-zero acceptance rate and byte-identical temperature-0 output for mlxcel generate -m DeepSeek-V4.1-Flash-MLX-2bit --draft-model <same>, and the same pairing through mlxcel-server --model-draft) needs mlx-community/DeepSeek-V4.1-Flash-MLX-2bit (238.8 GB) on a 256 GB or larger unified-memory host. This work was developed and unit-tested on a 128 GB M1 Ultra, which cannot load that checkpoint, so the end-to-end check is deferred to a 256 GB+ host.

Known pre-existing failure, unrelated to this PR: deepseek_v41_fakequant_tests::fp4_e4m3_scale_ties_round_to_the_even_grid_point fails on this host when run alone. The test and deepseek_v41_fakequant.rs are byte-identical to main (git diff main is empty for both), and the failure is inside the existing e4m3 block-scale path, not in any file this PR touches.

Closes #3262

DeepSeek-V4.1 checkpoints bundle a three-stage DSpark head under `mtp.0..2` and no mlxcel path could run it: `resolve_drafter_kind` sent the family through the generic `num_nextn_predict_layers` rule to `Mtp`, core `load_drafter` had no builder for the V4.1 block stages, and the model-owned V4.1 cache could not rewind a verify block to its accepted prefix.

Add `src/models/deepseek_v41_dspark.rs` (+ tests), a binary-side DFlash-kind drafter that imports the V4.1 block, attention, MoE, HyperConnection and fake-quant code instead of copying it. Add the `capture_layer_ids` hidden tap and a per-token speculation transaction (`start` / snapshot / `commit` / `abort`) on the V4.1 cache, with verification replayed as single-token steps. Resolve the family to `DrafterKind::Dflash` from the last stage's `markov_head.*` tensors above the generic MTP rule, refuse it in core with `BinaryCrateDrafter`, and wire the server family, `DFlashTargetModel` for the text and VL models, the drafter loader, block-size peeking and the offline/server drive paths.

The integrated real-checkpoint check is deferred: it needs the 238.8 GB `mlx-community/DeepSeek-V4.1-Flash-MLX-2bit` pack on a 256 GB+ unified-memory host, while this work was developed and unit-tested on a 128 GB M1 Ultra. The PR body lists the unit tests that cover every other acceptance criterion and the pre-existing, unrelated fake-quant test failure observed on this host.
@inureyes inureyes added type:enhancement New features, capabilities, or significant additions priority:low Low priority area:inference Generation, sampling, decoding (incl. speculative, DRY) modelsize:xlarge 4-bit checkpoint > 100GB; exceeds a 128GB dev box, needs bigger hardware modeltype:text Text-only language model arch:hybrid Hybrid attention stack (linear/sliding/full or attention+SSM interleave) status:done Completed labels Dec 2, 2026
@inureyes inureyes self-assigned this Dec 2, 2026

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

arch:hybrid Hybrid attention stack (linear/sliding/full or attention+SSM interleave) area:inference Generation, sampling, decoding (incl. speculative, DRY) modelsize:xlarge 4-bit checkpoint > 100GB; exceeds a 128GB dev box, needs bigger hardware modeltype:text Text-only language model priority:low Low priority status:done Completed type:enhancement New features, capabilities, or significant additions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(speculative): DeepSeek-V4.1 bundled DSpark drafter

1 participant