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

Aperta
#3,262 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
8/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
rust
Ambito
backend

Direzione di ricerca

This is a blocked design-heavy feature: it depends on the V4.1 text port (#2386) that adds src/models/deepseek_v41*.rs, which does not exist yet. Start by reading the DFlash drafter code in src/lib/mlxcel-core/src/drafter/dflash/markov.rs and src/server/batch/dflash_target.rs, and the detection pattern in src/lib/mlxcel-core/src/drafter/mod.rs. Done means the listed _tests.rs cases pass and the real-checkpoint generate run reports non-zero acceptance with byte-identical output.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

arch:hybrid area:inference modelsize:xlarge modeltype:text priority:low status:blocked type:enhancement

Summary

Run the DSpark drafter bundled in every DeepSeek-V4.1 checkpoint (mtp.0 to mtp.2, 3,584 tensors in the MLX 2-bit pack) against the V4.1 target, with the same checkpoint passed as target and --draft-model. This is the "MTP and DSpark drafting" item #2386 lists as out of scope. It is filed separately and blocked by #2386 because every draft stage is a V4.1 block (V4.1 attention, HyperConnections, the V4.1 MoE and fake-quant helpers), and the target needs a transactional cache that only makes sense once the V4.1 cache exists.

Upstream reference

Neywa-Labs/mlx-vlm#2678 (merge 10199b59, commit dee11be3):

Architecture

Config (mlx-community/DeepSeek-V4.1-Flash-MLX-2bit): num_nextn_predict_layers 3 (three stages), dspark_block_size 5 (proposals; verify block 5 + 1 = 6), dspark_noise_token_id 128799 (mask token), dspark_target_layer_ids [37, 38, 39], dspark_markov_rank 256, dspark_n_routed_experts 128, dspark_num_experts_per_tok 3, hidden_size 5120, hc_mult 4, o_groups 8, o_lora_rank 1024, sliding_window 128. runtime_block_size defaults to min(2, block_size), one proposal per round, because exact verification pays for each retained attention window; an explicit --draft-block-size wins.

  • Target hidden tap: the target appends h.mean(axis=2) (the mean over the 4 HyperConnection copies) at the input of each layer in [37, 38, 39], giving [B, L, 3 * 5120].
  • Context: context = main_norm(main_proj(fp8(target_hidden))) on stage 0 only, where fp8 is the V4.1 fake_quant_fp8_ue8m0 round trip and main_proj maps 3 * 5120 to 5120.
  • Draft input: [anchor, mask, ..., mask] (proposal_length tokens) embedded with the target's embedding table, broadcast to [B, L, 4, 5120], with an identity pre-mix. Each stage is a V4.1 block built from a draft copy of the config: one layer, compress_ratios [0], no kv, index or engram sources, 128 experts top-3. The stage output is norm(hc_pre(h, pre_mix)) after the last stage, then the target head.
  • DSparkAttention: kv = fp8(rope(kv_norm(wkv(fp8(context))), offset)) is appended to the stage's cache and cut to the last 128 rows; q = rope(wq_b(fp8(q_norm(wq_a(fp8(x))))), offset + count); block KV from x at offset + count is concatenated after the context rows; non-causal SDPA with the learned attn_sink, inverse RoPE on the output, the grouped wo_a (o_groups x o_lora_rank) and wo_b, each input fake-quantized.
  • Stage-specific tensors: on the 2-bit pack main_proj and main_norm exist only on mtp.0, while norm, markov_head and confidence_head exist only on the last stage, mtp.2.
  • Proposal: VanillaMarkov block sampling over the base logits, chained from the anchor (markov_head.embed is markov_w1, markov_head.head is markov_w2). confidence_head.* is dropped.
  • Sanitize: mtp.N.* becomes stages.N.*; native .scale sidecars are repacked with the V4.1 _pack_source_weight; the MoE is stacked with V4.1 sanitize_moe_weights at 128 experts; ffn.gate.weight is kept f32; a 2-D attn.wo_a is reshaped to [o_groups, o_lora_rank, -1]. On the MLX 2-bit pack the Markov factors are quantized (markov_head.embed.{weight,scales,biases}); upstream casts every Markov tensor to f32, which is only meaningful for dense factors.
  • Target transaction: during speculation the V4.1 cache snapshots its whole state before the round and after every token, and verification advances one token at a time (compression groups, window eviction and the engram history all have token boundaries); commit restores the snapshot at the accepted length, abort restores length 0.

Current mlxcel behavior

  • No V4.1 code exists yet; #2386 adds src/models/deepseek_v41*.rs and drops mtp.* for the text model. There is no V4 DSpark drafter either (only src/models/deepseek_v4*.rs, the target).
  • mlxcel's DSpark support is the LFM2 drafter (#1339): DFlashConfig::is_dspark (src/lib/mlxcel-core/src/drafter/dflash/config.rs:320), VanillaMarkovHead (src/lib/mlxcel-core/src/drafter/dflash/markov.rs:43, which already loads quantized markov_w1/markov_w2 through the unified loaders), and the DFlash-family burst in src/server/batch/dflash_target.rs, where DFlashTargetModel (:184) and DFlashDrafterFamily (:141, Dflash | DSpark | MuseAssistant | Laguna) gate which drafter a target accepts.

Required changes

  1. src/models/deepseek_v41_dspark.rs (new, + _tests.rs): DeepseekV41DsparkDrafter implementing the core Drafter trait as a DFlash-kind drafter, built from the V4.1 block, attention, MoE, HyperConnection and fake-quant code that #2386 adds. Import those pieces, never copy them. Load from the target checkpoint (model_type deepseek_v41 / deepseek_v41_text) reading only mtp.* through load_weights_from_dir_index_filtered, or from a split directory (model_type: deepseek_v41_dspark). Reuse VanillaMarkovHead::from_weights under the renamed prefix, so a quantized Markov head stays quantized instead of being cast to f32.
  2. V4.1 target (src/models/deepseek_v41.rs from #2386): a capture_layer_ids tap returning the HyperConnection-mean input of each requested layer, and a speculation transaction on the V4.1 cache (start(len), per-token snapshot, commit(accepted), abort()), with verification run as single-token steps. Starting a second transaction while one is active, or committing outside 0..=len, is an error, not a silent reset.
  3. Burst wiring: add DFlashDrafterFamily::DeepseekV41DSpark, implement DFlashTargetModel for the V4.1 model with required_drafter_family() = Some(DeepseekV41DSpark), first_hidden_rows() = EveryPromptRow, supports_batched() = false, and an exactness_allows probe like the other families. Wire the CLI (mlxcel generate --draft-model), mlxcel-server --model-draft and speculative_bench through the binary-side src/models/drafter_loader.rs, because the stages are binary-crate blocks.
  4. Detection: in core resolve_drafter_kind, a deepseek_v41 or deepseek_v41_text directory resolves to the DFlash kind only when the last stage's mtp.{num_nextn_predict_layers - 1}.markov_head.* tensors exist (mtp.2.* on the published packs) (tensor-presence check, the has_qwen4_exp_mtp_tensors pattern at src/lib/mlxcel-core/src/drafter/mod.rs:615), and core load_drafter refuses it with BinaryCrateDrafter.

Acceptance criteria

  • src/models/deepseek_v41_dspark_tests.rs config_derives_block_and_runtime_width: the published config gives 3 stages, verify block 6, runtime block 2, mask 128799, taps [37, 38, 39]; an explicit block size overrides the runtime default.
  • Same file, sanitize_maps_stages_and_drops_confidence_head: a toy mtp.{0,1}.* key set maps to stages.{0,1}.*, stacks experts, reshapes 2-D wo_a to [o_groups, o_lora_rank, -1], and drops confidence_head.*.
  • Same file, quantized_markov_head_loads_without_cast: quantized markov_head.embed / head load through VanillaMarkovHead and match the dequantized f32 reference within 1e-3.
  • src/models/deepseek_v41_tests.rs speculation_commit_equals_shorter_forward: on a toy config with compression and engram, starting a 4-token transaction and committing n = 0..=4 leaves the cache equal to a plain forward over n tokens; a nested start and an out-of-range commit both return errors.
  • src/server/batch/dflash_target.rs tests: the V4.1 target requires DeepseekV41DSpark and declines an LFM2 DSpark drafter with a message naming both.
  • Real checkpoint, integrated: mlxcel generate -m models/DeepSeek-V4.1-Flash-MLX-2bit --draft-model models/DeepSeek-V4.1-Flash-MLX-2bit -p "Explain the Sinkhorn algorithm briefly." -n 128 --temp 0 reports a non-zero acceptance rate and its output is byte-identical to the run without --draft-model. The same pairing works on mlxcel-server --model-draft.

Validation

mlx-community/DeepSeek-V4.1-Flash-MLX-2bit is 238.8 GB (Hub API, 2026-11-23) and needs 256 GB+ unified memory, the same host #2386 needs. The 4-bit pack is 552.9 GB.

make verify-test
cargo test --profile test-fast deepseek_v41
cargo clippy --workspace --all-targets -- -D warnings
cargo fmt --check
python3 scripts/ci/check_cross_repo_refs.py

Related

#2386 (blocker: V4.1 text port), #1339 (LFM2 DSpark drafter and VanillaMarkovHead), #3261 (official Speculators and OpenBMB DSpark checkpoints, Neywa-Labs/mlx-vlm#2676; independent of V4.1).

Lingua principale
Rust
Stelle
505
Fork
81
Merge medio
14h 2m
PR unite (30g)
448

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di lablup/mlxcel

Tutte le issue di lablup/mlxcel

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.