Repository navigation
Studio: auto-enable MTP speculative decoding for MTP GGUFs - #5527
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces auto-detection and support for Multi-Token Prediction (MTP) speculative decoding using the draft-mtp type. It implements logic to identify MTP-capable models through GGUF metadata or filename markers and automatically configures the backend with optimized presets for GPU and CPU/Mac environments. Feedback was provided regarding a discrepancy in the state-matching logic where metadata-based detection was omitted, which could cause unnecessary model reloads; a code suggestion was provided to synchronize this check with the primary loading logic.
| req_spec = "off" | ||
| else: | ||
| req_spec = _norm(speculative_type) or "off" | ||
| raw_spec = _norm(speculative_type) | ||
| req_spec = raw_spec or "off" | ||
| # Mirror load_model's auto-promotion to draft-mtp for MTP |
There was a problem hiding this comment.
The MTP auto-promotion logic in _already_in_target_state is missing the metadata-based signal (self._nextn_predict_layers), unlike the implementation in load_model (line 2417). This discrepancy will cause unnecessary reloads for MTP models that are identified via GGUF metadata but do not contain the -MTP fallback marker in their name or identifier. Including the metadata check ensures that repeat /load requests for these models correctly short-circuit when already running with draft-mtp.
| req_spec = "off" | |
| else: | |
| req_spec = _norm(speculative_type) or "off" | |
| raw_spec = _norm(speculative_type) | |
| req_spec = raw_spec or "off" | |
| # Mirror load_model's auto-promotion to draft-mtp for MTP | |
| if ( | |
| raw_spec in (None, "default") | |
| and (bool(self._nextn_predict_layers) or _is_mtp_model_name(model_identifier, gguf_path)) | |
| and not _extra_args_set_spec_type(extra_args) | |
| ): |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: f67fa5ceb1
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| if ( | ||
| raw_spec in (None, "default") | ||
| and _is_mtp_model_name(model_identifier, gguf_path) | ||
| and not _extra_args_set_spec_type(extra_args) |
There was a problem hiding this comment.
Reuse metadata signal when matching MTP reloads
When an MTP GGUF is detected only from metadata and its repo/file name does not contain -MTP, this comparator never mirrors the load_model auto-promotion: raw_spec=None stays off while the running backend has _speculative_type == "draft-mtp". Because the backend still has _nextn_predict_layers from the loaded model at this point, include that signal here as well; otherwise a no-op repeat /load for such metadata-only MTP models unnecessarily kills and restarts the server even though the later command construction would auto-enable draft-mtp again.
Useful? React with 👍 / 👎.
|
Just FYI, as of today mtp has almost 50% pp hit |
Detect Unsloth's MTP (multi-token-prediction) GGUFs and auto-emit the
right --spec-type draft-mtp flags for llama-server (llama.cpp PR
#22673), so users get the speedup without configuration.
Detection prefers the GGUF metadata field <arch>.nextn_predict_layers
(verified on Qwen3.6-27B-MTP-GGUF / qwen35 and Qwen3.6-35B-A3B-MTP-GGUF
/ qwen35moe). Falls back to a -MTP marker in the identifier / filename
so HF-mode loads can detect MTP from the repo name before the GGUF is
downloaded.
Flag presets follow the Unsloth MTP guide:
GPU: --spec-type draft-mtp --spec-draft-n-max 6
CPU/Mac: --spec-type draft-mtp --spec-draft-n-max 3 \
--spec-type ngram-mod --spec-ngram-mod-n-match 24 \
--spec-ngram-mod-n-min 48 --spec-ngram-mod-n-max 6
User overrides win: if the caller passes --spec-type / --spec-default
via unsloth run / unsloth studio run pass-through (or HTTP
llama_extra_args), the auto-emit steps aside so llama-server only sees
the user's flag. Scalar tuning knobs like --spec-draft-n-max compose
with the auto preset via llama-server's last-wins parsing.
_already_in_target_state mirrors the same promotion so a repeat /load
with unchanged settings against an MTP backend running draft-mtp
short-circuits cleanly instead of forcing a reload.
f67fa5c to
6599968
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 105d878e59
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| raw_spec = _norm(speculative_type) | ||
| req_spec = raw_spec or "off" | ||
| # Mirror load_model's auto-promotion so repeat /load matches. |
There was a problem hiding this comment.
Suppress spec comparison when extra args own spec type
load_model now explicitly lets llama_extra_args win when users pass --spec-type/--spec-default by clearing self._speculative_type, but _already_in_target_state still computes req_spec from speculative_type first. In requests that include both fields (for example a stale speculative_type="default" plus llama_extra_args=["--spec-type","none"]), the effective runtime config is unchanged, yet this comparator reports a spec mismatch and forces an unnecessary reload on every repeat /load. This is introduced by the new override behavior and should mirror the same “user owns spec type” rule during comparison.
Useful? React with 👍 / 👎.
|
@Mithras Best to open a llama.cpp issue - for now in general generation speed is definitely better. |
* Studio: warn when llama.cpp prebuilt is too old for MTP Layered on unslothai#5527. Adds a one-shot llama-server --help capability probe so users get a clear signal when their prebuilt is missing MTP support, plus a graceful fallback if they load an MTP GGUF against an outdated binary. What's surfaced: 1. Startup log + stderr line in main.py:lifespan() if MTP isn't advertised: WARNING: llama.cpp prebuilt is missing MTP support (--spec-type mtp / draft-mtp). Run `unsloth studio update` to refresh it. MTP GGUFs will load without speculative decoding. 2. Load-time graceful fallback in load_model's spec block: skip the auto-emit and log a clear warning instead of letting llama-server fail with an unknown-flag error. 3. /api/inference/status now returns llama_cpp_supports_mtp: bool so the frontend can show a banner / popup. Probe internals: - Class-level cache keyed on (binary_path, mtime). One subprocess call the first time, instant thereafter. Touching the binary (e.g. via `unsloth studio update`) invalidates the cache automatically because the mtime changes, so the new build is picked up without restarting the server. - Recognises both upstream naming forms: the original draft-mtp from llama.cpp PR #22673 and the renamed mtp variant in later commits. - Spec block uses whichever token the binary accepts so we emit the right value regardless of which release the user has. Tests: - 6 new cases in test_llama_cpp_mtp_detection.py covering each probe variant (draft-mtp, renamed mtp, pre-MTP build, missing binary, mtime-based cache invalidation). - Existing 38 MTP detection cases still pass; broader 188-test regression suite (server args, reload inheritance, gguf metadata, load progress, context fit, model validation) still green. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
For agentic use pp is more important. I wouldn't default to something that halves pp |
Summary
Detect Unsloth's MTP (multi-token-prediction) GGUFs and auto-emit the right
--spec-type draft-mtpflags forllama-server(llama.cpp PR #22673), so users get the 1.4-2x speedup without configuration.{arch}.nextn_predict_layers > 0. Verified on the published quants:unsloth/Qwen3.6-27B-MTP-GGUF(archqwen35,qwen35.nextn_predict_layers = 1)unsloth/Qwen3.6-35B-A3B-MTP-GGUF(archqwen35moe,qwen35moe.nextn_predict_layers = 1)-MTPsegment in the identifier or filename, used pre-download for HF-mode loads (the comparator runs before the GGUF is on disk) and for local-file loads where the filename still carries the marker.-MTPsubstring from the name and detection still works because metadata is the authoritative signal.Flag presets
Match the Unsloth MTP guide:
--spec-type draft-mtp --spec-draft-n-max 6--spec-type draft-mtp --spec-draft-n-max 3 --spec-type ngram-mod --spec-ngram-mod-n-match 24 --spec-ngram-mod-n-min 48 --spec-ngram-mod-n-max 6CPU/Mac detection uses the existing
_get_gpu_free_memory()probe (empty means no NVIDIA/ROCm device, which covers Apple Silicon).User overrides win
The spec block runs before user pass-through
extra_argsare appended, but--spec-typeis one of the few llama-server flags that accumulates when repeated (that's how the CPU MTP chain works:--spec-type draft-mtp ... --spec-type ngram-mod ...). So a user--spec-type nonewould compose rather than override.Fix:
_extra_args_set_spec_typescansextra_argsfor--spec-type/--spec-default. If the user supplied either, Studio skips the auto-emit andllama-serversees only the user's flag.-MTPGGUFunsloth run --model unsloth/Qwen3.6-27B-MTP-GGUFunsloth run ... --spec-type noneunsloth run ... --spec-type ngram-mod --draft-min 48 --draft-max 64unsloth run ... --spec-type draft-mtp --spec-draft-n-max 8unsloth run ... --spec-draft-n-max 8(scalar tuning, no--spec-type)LoadRequest.speculative_type = "off"Same semantics apply through HTTP
LoadRequest.llama_extra_args.Reload comparator
_already_in_target_statemirrors the promotion so a repeat/loadwithspeculative_type=None(or"default") against an MTP backend runningdraft-mtpshort-circuits cleanly instead of forcing a reload. Suppressed when the caller'sextra_argscarries--spec-type, matching the load-time skip.Performance
Detection cost is well below the existing GGUF-load floor:
_is_mtp_model_name: 0.1-2.5 us / call_extra_args_set_spec_type: 0.05-0.5 us / callnextn_predict_layersentry inarch_keys: one extra dict-membership check per KV pair inside the existing header parse, plus one 4-byte read on MTP GGUFs. Below the noise floor of the 55-80 us total parse.No new I/O on the load hot path.
llama_server_args.py
--spec-draft-n-max,--spec-draft-n-min,--spec-ngram-mod-n-match,--spec-ngram-mod-n-min,--spec-ngram-mod-n-maxadded to_SPEC_FLAGSso inheritedllama_extra_argsget stripped onspeculative_typere-application (matches the existing semantics for the other spec flags). They stay pass-through (not on the hard denylist).Test plan
pytest studio/backend/tests/test_llama_cpp_mtp_detection.py(38 new cases): helper detector, override detector, comparator integration (4 paths: None / "default" / "off" / user override),nextn_predict_layerscapture acrossqwen35/qwen35moe/qwen3moe/ hypothetical future arch, zero-value falsy semantics, unload resetpytest studio/backend/tests/test_llama_server_args.py(90 cases, 5 new): MTP flag pass-through, strip-shadowing for the new flag family,is_managed_flagassertionspytest studio/backend/tests/test_gguf_reload_inheritance.py(8 cases): no regressionspytest studio/backend/tests/test_gguf_metadata.py(12 cases): no regressionstest_llama_cpp_load_progress,test_llama_cpp_context_fit,test_llama_cpp_max_context_threshold,test_llama_cpp_no_context_shift,test_inference_model_validation,test_native_context_length,test_kv_cache_estimation-> 220 passedReferences