Repository navigation
Conversation
Spec 030 phase 1a — protocol surface, iterator, variant-B registry, and
DeepseekV4 sanitize-strip gating. No model conformance for variant A
yet (DeepseekV4MTP head deferred to a follow-up where it can be
validated against a running model + MTPLX reference outputs).
Adds:
- `MTPInjector` protocol + `MTPContract` (per-family hidden_variant +
concat_order) + `MTPLoader` (key-prefix probing, env gate).
- `MTPSelfSpeculativeTokenIterator` (variant A) — greedy-only cycle
of AR forward → MTP head draft → batched verify → accept/trim.
Per-head KV caches, per-cycle accept/proposed metrics on the
standard `TokenIteratorProtocol` surface.
- `mtpRouteDecision` for auto-routing in
`MLXLMCommon.generate(...)`. MTP takes precedence over n-gram when
a model conforms to `MTPInjector` with heads loaded; init-failure
falls through cleanly to the next route.
- `AssistantDraftRegistry` (variant B) — Gemma-4
`*-it-assistant-bf16` mappings (E2B / E4B / 26B-A4B / 31B). Caller
resolves the entry, loads the draft via the existing factory, and
constructs `SpeculativeTokenIterator` with the registered
`recommendedNumDraftTokens` (6 single, 3 batched per the model
card). Org-strip fallback for local bundles.
- `GenerateParameters`: `mtpEnabled`, `mtpDraftCount`,
`mtpAssistantDraftId`. Default off — opt in via Swift parameters
or `MLX_MTP_ENABLED=1`.
- `DeepseekV4.sanitize`: stops dropping `mtp.*` and the layer-N MTP
keys when `MLXLoader.mtpLoadEnabledFromEnv` (driven by
`MLX_MTP_ENABLED=1`). Default behaviour unchanged.
Tests: 18 unit tests in `Tests/MLXLMTests/MTPTests.swift` cover route
decision, registry resolution, key-prefix probing, parameter
threading, and contract value-type. All pass under `swift test`.
Variant A is end-to-end ready against any future `MTPInjector`
conformance. Variant B is end-to-end ready today — bench harness can
import the registry and resolve the draft explicitly.
DeepseekV4 conformance + Qwen 3.5/3.6/Qwen3-Next conformance follow
in subsequent PRs (the latter blocked on spec 020 tape-replay).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
ekryski
marked this pull request as draft
May 12, 2026 08:34
div0-space
pushed a commit
to LibraxisAI/mlx-swift-lm
that referenced
this pull request
Nov 29, 2026
* fix(mimo): accept bounded PCM WAV audio at native sample rates * fix(mimo): decode bounded AAC video audio at its source rate
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
Spec 030 phase 1a — multi-token prediction (MTP) self-speculative decoding scaffolding for both variants:
MTPInjectorprotocol +MTPSelfSpeculativeTokenIterator(greedy mode) + auto-routing inMLXLMCommon.generate(...). Reference: youssofal/MTPLX.AssistantDraftRegistrymapping target HF id → assistant draft id + recommendednumDraftTokens. Caller wires the resolved draft into the existingSpeculativeTokenIterator.Phase 1a deliberately stops short of the per-model
MTPInjectorconformance. The DeepseekV4MTP head implementation needs validation against a running model + MTPLX reference outputs before we can ship it confidently — that's a focused follow-up where the architecture choices (concat order, hidden-variant norm, eh_proj shape) can be measured rather than guessed. The scaffolding here is the substantial work; conformance is comparatively small once you have a model in hand.What ships in this PR
MTPContract(per-familyhiddenVariant/concatOrder/maxHeads/family) +MTPLoader(key-prefix probing + env-gate helpers).MTPSelfSpeculativeTokenIterator— greedy AR + MTP-draft + batched-verify + accept/trim cycle. StandardTokenIteratorProtocolsurface; reportsspecDecodeProposed/specDecodeAcceptedlike the existing iterators.mtpRouteDecisionfor auto-routing eligibility.*-it-assistant-bf16mappings. Prefix-match + org-strip resolver for local bundles.GenerateParameters.{mtpEnabled, mtpDraftCount, mtpAssistantDraftId}(default off). Variant-A auto-routing block in the standardgenerate(...)— engages before n-gram when a conforming model is present.MLX_MTP_ENABLED=1, themtp.*and layer-N MTP keys are now PRESERVED throughsanitize(weights:). Default behaviour unchanged.Auto-routing precedence
Spec 030 + 015 ordering — MTP takes precedence over n-gram (trained-head proposals carry a higher acceptance ceiling):
Variant A engages only when (1) the model conforms to
MTPInjector, (2)isMTPLoadedis true, (3) the trunk cache is fully trimmable, and (4)temperature == 0. Failed inits fall through cleanly.Variant B doesn't ride the same auto-routing because
MLXLMCommon.generate(...)doesn't have access to a downloader — the caller resolves viaAssistantDraftRegistryexplicitly, then passes the loaded draft to the speculativegenerate(...)overload.How to use
Variant A (once a model conforms)
Variant B (Gemma 4 assistant — works today)
On the MTP-head pruning question
The DeepSeek-V4 sanitize gate is the load-bearing change for variant A. The
mlx-community/DeepSeek-V4-Flash-2bit-DQbundle ships the MTP layer atmodel.layers.{numHiddenLayers}.*— our sanitize had been actively dropping it. WithMLX_MTP_ENABLED=1the layer is now preserved through load, ready to bind to a futureDeepseekV4MTPmodule.For Qwen 3.5/3.6 the same sanitize-gate pattern needs replication (and is blocked on spec 020 tape-replay before the iterator can engage on those hybrid GDN trunks). For Gemma 4, the original bundles don't ship in-trunk heads — variant B is the path there.
Test plan
swift build— library + tests target both compile cleanswift test --filter MTPTests— 18/18 passResolveTestsetc. still pass)AssistantDraftRegistry.resolve(...)(follow-up — can be slotted into the next bench-harness PR)DeepseekV4MTPconformance lands (separate PR — needs running model for validation)Follow-ups
scripts/mtp_convert.py) — for in-trunk families where mlx-community bundles already pruned the heads (DeepSeek-V3 4-bit, MiMo).--mtpflag — drive variant B on Gemma 4 from the existing sweep tooling.