From afe0bb22a70cd7e4880726b9b24517a3f197493d Mon Sep 17 00:00:00 2001 From: zhong Date: Wed, 13 May 2026 10:04:44 +0800 Subject: [PATCH 1/2] fix(mtp): prevent process-wide _MTP_ACTIVE flag from leaking across model 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 --- omlx/engine/batched.py | 17 +++++++++++++++-- omlx/engine/vlm.py | 29 +++++++++++++++++++++-------- omlx/utils/model_loading.py | 7 +++++++ 3 files changed, 43 insertions(+), 10 deletions(-) diff --git a/omlx/engine/batched.py b/omlx/engine/batched.py index 42b229594b..12d41de165 100644 --- a/omlx/engine/batched.py +++ b/omlx/engine/batched.py @@ -310,8 +310,21 @@ def _load_model_sync(): try: def _load_draft(): - draft_model, _ = load(specprefill_draft) - return draft_model + from ..patches.mlx_lm_mtp import set_mtp_active + + was_mtp = False + try: + from ..patches.mlx_lm_mtp import is_mtp_active + + was_mtp = is_mtp_active() + except Exception: + pass + set_mtp_active(False) + try: + draft_model, _ = load(specprefill_draft) + return draft_model + finally: + set_mtp_active(was_mtp) draft_model = await loop.run_in_executor( get_mlx_executor(), _load_draft diff --git a/omlx/engine/vlm.py b/omlx/engine/vlm.py index 77d28a8a3b..a4ccb19108 100644 --- a/omlx/engine/vlm.py +++ b/omlx/engine/vlm.py @@ -710,15 +710,28 @@ def _load_vlm_sync(): from ..utils.model_loading import maybe_load_custom_quantization def _load_draft(): - custom_loaded = maybe_load_custom_quantization( - specprefill_draft, - is_vlm=False, - ) - if custom_loaded is not None: - draft_model, _ = custom_loaded + from ..patches.mlx_lm_mtp import set_mtp_active + + was_mtp = False + try: + from ..patches.mlx_lm_mtp import is_mtp_active + + was_mtp = is_mtp_active() + except Exception: + pass + set_mtp_active(False) + try: + custom_loaded = maybe_load_custom_quantization( + specprefill_draft, + is_vlm=False, + ) + if custom_loaded is not None: + draft_model, _ = custom_loaded + return draft_model + draft_model, _ = mlx_lm_load(specprefill_draft) return draft_model - draft_model, _ = mlx_lm_load(specprefill_draft) - return draft_model + finally: + set_mtp_active(was_mtp) draft_model = await loop.run_in_executor(get_mlx_executor(), _load_draft) self._engine.engine.scheduler.set_specprefill_draft_model( draft_model, draft_model_name=specprefill_draft diff --git a/omlx/utils/model_loading.py b/omlx/utils/model_loading.py index 5b3299e863..6e5a926603 100644 --- a/omlx/utils/model_loading.py +++ b/omlx/utils/model_loading.py @@ -29,6 +29,13 @@ def maybe_apply_pre_load_patches( Safe to call repeatedly; the patches are idempotent. """ + # Reset the process-wide MTP flag so non-MTP-compatible models (or + # models with mtp_enabled=False) are not polluted by a prior model + # load that left the flag True. + from ..patches.mlx_lm_mtp import set_mtp_active + + set_mtp_active(False) + config_path = Path(model_name) / "config.json" if not config_path.exists(): return From 03b32607757176a6598cdd8d78276078c54c9489 Mon Sep 17 00:00:00 2001 From: zhong Date: Wed, 13 May 2026 11:42:37 +0800 Subject: [PATCH 2/2] fix(cli): launch connect via localhost when server binds 0.0.0.0 0.0.0.0 is a valid bind-all address but cannot be used as a connect address from the client side. When settings.json contains host=0.0.0.0 (e.g. after starting the server with --host 0.0.0.0), `omlx launch` fails with "oMLX server is not running at http://0.0.0.0:12345". Introduce `connect_host` which falls back to 127.0.0.1 whenever the resolved host is 0.0.0.0 (or empty), while keeping the original host value for display purposes. This also fixes downstream integrations (claude, codex, pi, etc.) which receive the host via launch(). Co-Authored-By: Claude Opus 4.7 --- omlx/cli.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/omlx/cli.py b/omlx/cli.py index 68682c6c76..b7c5a5ec65 100644 --- a/omlx/cli.py +++ b/omlx/cli.py @@ -301,8 +301,13 @@ def launch_command(args): host = args.host or settings.server.host port = args.port or settings.server.port + # 0.0.0.0 is a valid bind address but not a valid connect address. + # Fall back to localhost so launch can reach the server regardless + # of which interface it was bound to. + connect_host = host if host and host != "0.0.0.0" else "127.0.0.1" + # Check if oMLX server is running - base_url = f"http://{host}:{port}" + base_url = f"http://{connect_host}:{port}" try: resp = requests.get(f"{base_url}/health", timeout=3) resp.raise_for_status() @@ -380,7 +385,7 @@ def launch_command(args): port=port, api_key=api_key, model=model, - host=host, + host=connect_host, tools_profile=tools_profile, context_window=context_window, max_tokens=max_tokens,