Add MTP speculative decoding for DeepSeek-V4#15
Draft
0xClandestine wants to merge 2 commits intoBlaizzy:pc/add-deepseekv4flash-modelfrom
Draft
Add MTP speculative decoding for DeepSeek-V4#150xClandestine wants to merge 2 commits intoBlaizzy:pc/add-deepseekv4flash-modelfrom
0xClandestine wants to merge 2 commits intoBlaizzy:pc/add-deepseekv4flash-modelfrom
Conversation
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.
9df263e to
33d04f8
Compare
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