Repository navigation
Conversation
…n bypass) llama.cpp MTP speculative decoding (--spec-type draft-mtp) cannot run with --mmproj or -np > 1 (llama.cpp #22673; Qwen3.6 MTP guide). Unsloth's *-MTP GGUF repos (e.g. unsloth/Qwen3.6-27B-MTP-GGUF) still ship an mmproj projector, so load_model resolves it, sets effective_is_vision=True, and the existing draft-mtp auto-promotion (gated `not effective_is_vision`) is suppressed. Net: the headline MTP speedup never engages out of the box for exactly the repos that advertise it. Add a narrowly-scoped, temporary shim: when we positively detect an MTP GGUF (nextn_predict_layers metadata or -mtp name), the bundled llama-server actually advertises the MTP spec type, and the caller has not taken over speculative decoding, fall back to text-only (drop mmproj, effective_is_vision=False, single slot) so draft-mtp can auto-enable. Decision is a pure helper _mtp_should_force_text_only so it is unit testable. Scoped so it cannot regress anything else: - non-MTP GGUFs: never affected (mtp_detected False short-circuits). - outdated binary without MTP: unchanged (keeps mmproj/vision). - explicit user --spec-type / --spec-default via extra_args: respected. - speculative_type "off" or any non-MTP value (e.g. ngram-mod): opt-out, vision/mmproj preserved. Remove once upstream llama.cpp supports mmproj + MTP together. Tests: 17 new gating cases in test_llama_cpp_mtp_detection.py covering engage/skip/opt-out branches; full existing MTP + llama_server_args suites still green (151 passed).
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: a782fa5836
ℹ️ 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".
| effective_is_vision = False | ||
| n_parallel = 1 |
There was a problem hiding this comment.
Keep load dedupe consistent after forcing text-only mode
Setting effective_is_vision = False here makes MTP vision repos report as non-vision while requests still come in with is_vision=True, which breaks the duplicate-load short-circuit logic: load_model._already_in_target_state() coerces request spec to "off" when is_vision is true, but backend spec is now "draft-mtp", so identical follow-up loads no longer match and trigger unnecessary reloads; the route-level matcher in studio/backend/routes/inference.py (_request_matches_loaded_settings) similarly compares request spec against backend spec based on llama_backend.is_vision, so this mismatch can repeatedly restart an already-loaded model.
Useful? React with 👍 / 👎.
| ) | ||
| launch_mmproj_path = None | ||
| effective_is_vision = False | ||
| n_parallel = 1 |
There was a problem hiding this comment.
Recalculate VRAM/context fit after overriding parallel slots
The shim forces n_parallel = 1 only after GPU selection and context fitting have already used the original n_parallel in KV-cache estimates, so for requests that started with n_parallel > 1 the selected effective_ctx, use_fit, and max_available_ctx can be needlessly conservative and remain reduced even though the launched server now runs one slot; this introduces avoidable context/performance regression specifically in the new MTP-vision path.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Code Review
This pull request introduces a temporary shim, _mtp_should_force_text_only, to enable MTP (Multi-Token Prediction) in llama.cpp for GGUF models that bundle vision projectors, which would otherwise prevent MTP from engaging. The shim forces a text-only, single-slot configuration when an MTP model is detected. Reviewers identified a potential infinite reload loop caused by state mismatches in _already_in_target_state and recommended extending the shim to handle the n_parallel > 1 limitation for all MTP models, not just vision-enabled ones. Additional tests were suggested to cover these scenarios.
| if _mtp_should_force_text_only( | ||
| mtp_detected = bool(self._nextn_predict_layers) | ||
| or _is_mtp_model_name(model_identifier, model_path), | ||
| effective_is_vision = effective_is_vision, | ||
| supports_mtp = bool( | ||
| self.probe_server_capabilities(binary).get("supports_mtp") | ||
| ), | ||
| speculative_type = speculative_type, | ||
| extra_args = extra_args, | ||
| ): |
There was a problem hiding this comment.
This shim will cause an infinite reload loop for MTP vision models because _already_in_target_state (lines 3185-3186) is not aware of it. When the shim engages, it sets self._is_vision = False and self._speculative_type = "draft-mtp". However, _already_in_target_state checks the original request's is_vision=True and forces req_spec = "off", creating a mismatch with the running backend. You should update _already_in_target_state to use _mtp_should_force_text_only to determine the expected state correctly.
if _mtp_should_force_text_only(
mtp_detected = bool(self._nextn_predict_layers)
or _is_mtp_model_name(model_identifier, model_path),
effective_is_vision = effective_is_vision,
n_parallel = n_parallel,
supports_mtp = bool(
self.probe_server_capabilities(binary).get("supports_mtp")
),
speculative_type = speculative_type,
extra_args = extra_args,
):| def _mtp_should_force_text_only( | ||
| *, | ||
| mtp_detected: bool, | ||
| effective_is_vision: bool, | ||
| supports_mtp: bool, | ||
| speculative_type: Optional[str], | ||
| extra_args: Optional[Iterable[str]], | ||
| ) -> bool: | ||
| """Temporary shim: should an MTP GGUF drop mmproj/vision to engage MTP? | ||
|
|
||
| llama.cpp MTP speculative decoding (``--spec-type draft-mtp``) does not | ||
| support ``--mmproj`` or ``-np > 1`` (llama.cpp #22673; the Qwen3.6 MTP | ||
| guide). Unsloth's ``*-MTP`` GGUF repos still ship an mmproj projector, | ||
| which pins ``effective_is_vision=True`` and therefore suppresses the | ||
| draft-mtp auto-promotion in ``load_model`` -- so the headline MTP | ||
| speedup never engages out of the box for those repos. | ||
|
|
||
| Return True only when we positively detect an MTP GGUF that would | ||
| otherwise be forced into vision mode, the bundled llama-server actually | ||
| advertises the MTP spec type, and the caller has not taken over | ||
| speculative decoding. Deliberately narrow so non-MTP GGUFs are never | ||
| affected, an explicit user ``--spec-type`` (or ``speculative_type`` | ||
| other than auto/default/draft-mtp, including ``"off"``) opts out, and | ||
| an outdated binary falls back to today's behavior. Remove once | ||
| upstream llama.cpp supports mmproj + MTP together. | ||
| """ | ||
| if not (mtp_detected and effective_is_vision and supports_mtp): | ||
| return False |
There was a problem hiding this comment.
The llama.cpp MTP implementation also lacks support for multiple parallel slots (-np > 1). While the docstring mentions this, the current logic only triggers the shim if effective_is_vision is True. Non-vision MTP models loaded with n_parallel > 1 will still attempt to engage MTP and likely fail to start. I suggest updating the shim to also engage when n_parallel > 1.
def _mtp_should_force_text_only(
*,
mtp_detected: bool,
effective_is_vision: bool,
n_parallel: int,
supports_mtp: bool,
speculative_type: Optional[str],
extra_args: Optional[Iterable[str]],
) -> bool:
"""Temporary shim: should an MTP GGUF drop mmproj/vision to engage MTP?
llama.cpp MTP speculative decoding (``--spec-type draft-mtp``) does not
support ``--mmproj`` or ``-np > 1`` (llama.cpp #22673; the Qwen3.6 MTP
guide). Unsloth's ``*-MTP`` GGUF repos still ship an mmproj projector,
which pins ``effective_is_vision=True`` and therefore suppresses the
draft-mtp auto-promotion in ``load_model`` -- so the headline MTP
speedup never engages out of the box for those repos.
Return True only when we positively detect an MTP GGUF that would
otherwise be forced into vision mode or multiple slots, the bundled
llama-server actually advertises the MTP spec type, and the caller
has not taken over speculative decoding. Deliberately narrow so
non-MTP GGUFs are never affected, an explicit user ``--spec-type``
(or ``speculative_type`` other than auto/default/draft-mtp,
including ``"off"``) opts out, and an outdated binary falls back to
today's behavior. Remove once upstream llama.cpp supports mmproj +
MTP together.
"""
if not (mtp_detected and supports_mtp and (effective_is_vision or n_parallel > 1)):
return False| def _shim(**over): | ||
| """Base = the case the shim exists for: MTP GGUF whose repo also | ||
| ships an mmproj, recent binary, no caller spec override.""" | ||
| kw = dict( | ||
| mtp_detected = True, | ||
| effective_is_vision = True, | ||
| supports_mtp = True, | ||
| speculative_type = None, | ||
| extra_args = None, | ||
| ) | ||
| kw.update(over) | ||
| return _mtp_should_force_text_only(**kw) |
There was a problem hiding this comment.
Update the test helper to include n_parallel.
| def _shim(**over): | |
| """Base = the case the shim exists for: MTP GGUF whose repo also | |
| ships an mmproj, recent binary, no caller spec override.""" | |
| kw = dict( | |
| mtp_detected = True, | |
| effective_is_vision = True, | |
| supports_mtp = True, | |
| speculative_type = None, | |
| extra_args = None, | |
| ) | |
| kw.update(over) | |
| return _mtp_should_force_text_only(**kw) | |
| def _shim(**over): | |
| """Base = the case the shim exists for: MTP GGUF whose repo also | |
| ships an mmproj, recent binary, no caller spec override.""" | |
| kw = dict( | |
| mtp_detected=True, | |
| effective_is_vision=True, | |
| n_parallel=1, | |
| supports_mtp=True, | |
| speculative_type=None, | |
| extra_args=None, | |
| ) | |
| kw.update(over) | |
| return _mtp_should_force_text_only(**kw) |
| def test_shim_passes_through_unrelated_extra_args(): | ||
| assert _shim(extra_args = ["--threads", "8", "--no-warmup"]) is True |
There was a problem hiding this comment.
Add a test case for the n_parallel > 1 limitation.
| def test_shim_passes_through_unrelated_extra_args(): | |
| assert _shim(extra_args = ["--threads", "8", "--no-warmup"]) is True | |
| def test_shim_passes_through_unrelated_extra_args(): | |
| assert _shim(extra_args=["--threads", "8", "--no-warmup"]) is True | |
| def test_shim_engages_for_multi_slot_non_vision_mtp_model(): | |
| # MTP does not support -np > 1 even without vision. | |
| assert _shim(effective_is_vision=False, n_parallel=4) is True |
Summary
llama.cpp MTP speculative decoding (
--spec-type draft-mtp) does not support--mmprojor-np > 1(llama.cpp #22673; the Qwen3.6 MTP guide). Unsloth's*-MTPGGUF repos (for exampleunsloth/Qwen3.6-27B-MTP-GGUF) still ship anmmproj-*.ggufprojector. On a normal load,LlamaCppBackend.load_modelresolves that mmproj, setseffective_is_vision=True, and the existing draft-mtp auto-promotion (gatednot effective_is_vision) is therefore suppressed.Net effect today: the headline MTP speedup never engages out of the box for exactly the repos that advertise MTP.
--spec-type draft-mtpis wired and tested, but for these vision-capable MTP repos the mmproj wins the gate and MTP is silently never used.Change
Add a narrowly-scoped, explicitly temporary shim. In
load_model, right aftereffective_is_visionis computed and before the command is built: when we positively detect an MTP GGUF, the bundled llama-server actually advertises the MTP spec type, and the caller has not taken over speculative decoding, fall back to text-only for that session so draft-mtp can auto-enable:launch_mmproj_path = Noneeffective_is_vision = Falsen_parallel = 1A clear
logger.warningstates image input is unavailable for the session and that this is temporary until upstream supports mmproj + MTP together.The decision is factored into a pure module-level helper
_mtp_should_force_text_only(...)(same pattern as the existing_is_mtp_model_name/_extra_args_set_spec_type) so the contract is unit-testable without standing up a server.Why this cannot regress anything else
The helper returns
Trueonly when all of these hold, so it is inert outside the exact broken case:mtp_detectedis true (nextn_predict_layersGGUF metadata or-mtpin the name). Non-MTP GGUFs short-circuit immediately and are never touched.effective_is_visionis true. If no usable mmproj would be attached there is nothing to bypass; the existing draft-mtp auto-promotion already handles that case unchanged.supports_mtpis true (probed from the bundled binary's--help). An outdated prebuilt keeps today's exact behavior (mmproj/vision preserved, existing outdated-binary warning still fires) rather than sacrificing image input for an MTP path the binary cannot run.--spec-type/--spec-defaultviaextra_args, andspeculative_typeis one of auto/empty/default/draft-mtp. An explicit user spec choice, any non-MTPspeculative_type(for examplengram-mod), or"off"opts out and preserves vision/mmproj.Testing
studio/backend/tests/test_llama_cpp_mtp_detection.pycovering engage, the three skip conditions (non-MTP model, not effective-vision, binary lacks MTP), and the opt-outs (off, explicit non-MTP spec, user-managed--spec-type/--spec-defaultvia extra args, plus unrelated extra args passing through).test_llama_cpp_mtp_detection.py+test_llama_server_args.pysuites still green: 151 passed.py_compileclean.-m <mtp.gguf> ... -np 1 --spec-type draft-mtp --spec-draft-n-max 6, no--mmproj) was separately verified end to end on the bundled prebuilt binary (llama.cpp b9190) withunsloth/Qwen3.6-27B-MTP-GGUF:UD-Q4_K_XLon a B200: the server log confirmscreating MTP draft context/adding speculative implementation 'draft-mtp'with draft acceptance around 0.93 and a measured decode-throughput gain over the baseline.Notes
This is deliberately a temporary shim (it disables image input for MTP vision repos). It should be removed once upstream llama.cpp supports
--mmprojtogether with MTP. The scope is intentionally minimal so a future revert is a clean delete of one helper and one guarded block.Test plan
py_compileclean