Add MTP speculative decoding for DeepSeek-V4 - #15
Draft
0xClandestine wants to merge 2 commits into
Draft
Conversation
Blaizzy
force-pushed
the
pc/add-deepseekv4flash-model
branch
3 times, most recently
from
April 26, 2026 09:38
fdede5d to
16f7205
Compare
Implement native MTP support following the HF reference architecture and ml-explore/mlx-lm PR ml-explore#990 patterns: Model (deepseek_v4.py): - MTPBlock wrapping DeepseekV4Block with e_proj, h_proj, enorm, hnorm, norm, and per-block HyperHead - return_hidden support in Model.__call__ for exposing raw 4D hidden state - mtp_forward() and make_mtp_cache() on Model - Weight sanitization: keep and remap MTP weights, stack MTP experts Generation (generate.py): - mtp_generate_step() speculative decoding loop with draft/verify cycle - Greedy exact-match and probabilistic acceptance modes - --mtp CLI flag with graceful fallback warning Server (server.py): - --mtp CLI flag and stream_generate integration
Quantized checkpoints (e.g. 4-bit) typically strip MTP weights to save ~3.2 GB. Detect this in sanitize() and delete self.mtp so the --mtp flag falls back gracefully with a warning.
0xClandestine
force-pushed
the
feat/add-ds4-mtp
branch
from
April 26, 2026 09:54
9df263e to
33d04f8
Compare
jundot
added a commit
to jundot/omlx
that referenced
this pull request
May 6, 2026
Wires mlx-lm PR 990 + PR 15 as runtime monkey-patches inside GenerationBatch. oQ preserves mtp.* via a new -mtp suffix; mlx-vlm sanitize is patched to keep MTP norm shift + language_model prefix so quantized VLM checkpoints load with correct per-tensor bits. Source PRs: - mlx-lm#990 (Qwen3.5/3.6 MTP) by AirRunner ml-explore/mlx-lm#990 - mlx-lm fork PR #15 (DeepSeek-V4 MTP) by 0xClandestine Blaizzy/mlx-lm#15
0xClandestine
added a commit
to Layr-Labs/mlx-swift-lm
that referenced
this pull request
May 8, 2026
* feat: port DeepSeek-V4 model + MTP speculative decoding Adds DeepseekV4Model (base model port) and MTPCapable conformance via DeepseekV4MTPBlock, following the same omlx patterns as the Qwen3.5 MTP integration. Registers "deepseek_v4" in LLMTypeRegistry. Source PRs: - mlx-lm base model by Blaizzy, 0xClandestine, angeloskath, pcuenca, eauchs ml-explore/mlx-lm#1192 - mlx-lm fork MTP support by 0xClandestine Blaizzy/mlx-lm#15 * fix: resolve all compilation errors in DeepSeek-V4 port - Replace BaseKVCache subclass with direct KVCache conformance (init/offset/maxSize are not open outside MLXLMCommon) - Rename MultiLinear → DeepseekV4MultiLinear to avoid ambiguity with the identically-named class in GLM4MOELite.swift - Fix .bool_ → .bool and use MLXArray.ones([L,P], dtype: .bool) - Split mixed var/let compound declarations in overlapCompressKV - Fix full(values:) to accept MLXArray(-Float.infinity) - Fix stacked(stacked) → MLX.stacked(stacked) in sanitize - Fix expandedDimensions(axis: [0,1]) → chained single-axis calls - Add @unknown default to SDPA mask switch - MTP: pass layerIdx to DeepseekV4Block init - MTP: pass inputIds to block.callAsFunction - MTP: update createAttentionMask to non-deprecated single-cache API
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.
Summary
MTPBlockwrapsDeepseekV4Blockwith projection layers (e_proj,h_proj), norms, and per-blockHyperHead— following the HF reference architecturemtp_generate_step()in generate.py implements the draft/verify loop: backbone predicts token t+1, MTP drafts t+2, next backbone step verifies the draft — emitting up to 2 tokens per backbone forward pass--mtpCLI flag for bothgenerateandservercommands, with graceful fallback warning for models without an MTP headReference: ml-explore/mlx-lm#990 (Qwen3.5 MTP pattern)
Test plan
mlx_lm.generate --mtpand confirm speculative decoding produces coherent output--mtpflag on a model without MTP head → confirm fallback warning--mtpflag