Skip to content

fix(mtp): prevent process-wide _MTP_ACTIVE flag leaking across model loads - #1217

Closed
fish0710 wants to merge 1 commit into
jundot:mainfrom
fish0710:fix/mtp-active-global-leak
Closed

fish0710 wants to merge 1 commit into
jundot:mainfrom
fish0710:fix/mtp-active-global-leak

Conversation

@fish0710

Copy link
Copy Markdown
Contributor

Summary

Fixes the process-wide _MTP_ACTIVE flag leaking from one model load to another, which caused SpecPrefill draft model loading to crash with "Native MTP is enabled but converted weights are missing mtp.* tensors".

Root cause

set_mtp_active() is only called inside _is_mtp_compatible() branch in maybe_apply_pre_load_patches(). If the current model is not MTP-compatible, the flag is never reset. This means:

  1. Load Qwen3.6-27B-oQ4-mtp with mtp_enabled=True → _MTP_ACTIVE=True
  2. Switch to Qwen3.6-35B-A3B-UD-MLX-4bit (non-MTP) → flag stays True
  3. SpecPrefill loads Qwen3.5-0.8B-MLX-4bit as draft → patched __init__ sees is_mtp_active()=True → attaches MTP → crash

Fix

  • maybe_apply_pre_load_patches(): reset _MTP_ACTIVE=False at function entry so every model load starts clean
  • BatchedEngine._load_draft(): snapshot/restore flag around draft model load
  • VLMBatchedEngine._load_draft(): same snapshot/restore guard

Test plan

  • Load MTP-enabled model, then switch to non-MTP model with SpecPrefill draft → no crash
  • Load non-MTP model directly with SpecPrefill draft → works normally

🤖 Generated with Claude Code

…odel loads

The global `_MTP_ACTIVE` flag (set via `set_mtp_active` before `mlx_lm.load()`)
persists after model load completes. This caused three failure modes:

1. **Model switch**: Loading a non-MTP-compatible model after an MTP-enabled one
   left `_MTP_ACTIVE=True`. The patched `Model.__init__` on the new model then
   incorrectly attached an MTP head, crashing with missing mtp.* weights.

2. **SpecPrefill draft model (BatchedEngine)**: A target model with MTP enabled
   leaked the flag to the draft model load, causing the same crash.

3. **SpecPrefill draft model (VLMBatchedEngine)**: Same as (2) for VLM path.

Changes:
- `maybe_apply_pre_load_patches()`: reset `_MTP_ACTIVE=False` at function entry
  before any model-specific logic, so every load starts from a known state.
- `BatchedEngine._load_draft()`: snapshot current flag, disable MTP, load draft,
  restore flag.
- `VLMBatchedEngine._load_draft()`: same snapshot/restore guard.

Closes the root cause behind "Native MTP is enabled for this model but the
converted weights are missing the mtp.* tensors" errors when using SpecPrefill
with a Qwen3.5 draft after an MTP-enabled model was previously loaded.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@jundot

jundot commented May 13, 2026

Copy link
Copy Markdown
Owner

Thanks — same MTP flag fix already shipped in #1219 (merged 04a0ce6) bundled with the launch host fix. Closing this as a duplicate.

@jundot jundot closed this May 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants