Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions mlx_engine/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
construct_user_cancelled_result,
)
from mlx_engine.utils.set_seed import set_seed
from mlx_engine.utils.unsupported_checkpoints import raise_if_unsupported_checkpoint
from mlx_engine.utils.speculative_decoding import (
determine_draft_model_for_generation,
configure_num_draft_tokens_in_generate_args,
Expand Down Expand Up @@ -242,6 +243,8 @@ def load_model(
prefill_step_size = validate_prefill_step_size(prefill_step_size)
model_path = Path(model_path)
config_json = json.loads((model_path / "config.json").read_text())
if not vocab_only:
raise_if_unsupported_checkpoint(model_path, config_json)
parallel_requested = max_seq_nums is not None and max_seq_nums > 1

def warn_if_parallel(reason: str) -> None:
Expand Down
35 changes: 35 additions & 0 deletions mlx_engine/utils/unsupported_checkpoints.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""Fail fast on checkpoint layouts that the MLX engine cannot load."""

import json
from pathlib import Path

from safetensors import safe_open

# mlx-lm saves Inkling 2 Mini (`inkling2_loop`) with tensor names under
# `model.language_model.`. mlx-vlm, which loads all vision models here, only
# reads the original checkpoint names and its own converted names, so it would
# otherwise fail with a long list of unrecognized parameters.
_MLX_LM_INKLING2_PREFIX = "model.language_model."
UNSUPPORTED_INKLING2_MESSAGE = (
"Unsupported Inkling 2 Mini format, try lmstudio-community/Inkling-2-Mini-MLX-8bit."
Comment thread
samir-lms marked this conversation as resolved.
Resolved
Comment thread
samir-lms marked this conversation as resolved.
Resolved
)


def _weight_names(model_path: Path) -> list[str]:
index_path = model_path / "model.safetensors.index.json"
if index_path.exists():
return list(json.loads(index_path.read_text())["weight_map"])
names = []
for path in sorted(model_path.glob("*.safetensors")):
with safe_open(path, framework="numpy") as file:
names.extend(file.keys())
return names


def raise_if_unsupported_checkpoint(model_path: Path, config_json: dict) -> None:
if config_json.get("model_type") != "inkling2_loop":
return
if any(
name.startswith(_MLX_LM_INKLING2_PREFIX) for name in _weight_names(model_path)
):
raise ValueError(UNSUPPORTED_INKLING2_MESSAGE)
78 changes: 78 additions & 0 deletions tests/test_unsupported_checkpoints.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import json
import struct
from pathlib import Path

import pytest

import mlx_engine.generate as generate_module
from mlx_engine.utils.unsupported_checkpoints import (
UNSUPPORTED_INKLING2_MESSAGE,
raise_if_unsupported_checkpoint,
)

MLX_LM_INKLING2_NAME = "model.language_model.layers.0.mlp.experts.gate_up_proj.weight"
MLX_VLM_INKLING2_NAME = "language_model.model.layers.0.mlp.switch_mlp.gate_proj.weight"
ORIGINAL_INKLING2_NAME = "model.llm.layers.0.mlp.experts.w13_weight"


def _write_checkpoint(tmp_path: Path, model_type: str, weight_name: str) -> Path:
(tmp_path / "config.json").write_text(json.dumps({"model_type": model_type}))
(tmp_path / "model.safetensors.index.json").write_text(
json.dumps({"weight_map": {weight_name: "model-00001-of-00001.safetensors"}})
)
return tmp_path


def _write_single_file_checkpoint(tmp_path: Path, weight_name: str) -> Path:
(tmp_path / "config.json").write_text(json.dumps({"model_type": "inkling2_loop"}))
header = json.dumps(
{
"__metadata__": {"format": "mlx"},
weight_name: {"dtype": "BF16", "shape": [1], "data_offsets": [0, 2]},
}
).encode()
(tmp_path / "model.safetensors").write_bytes(
struct.pack("<Q", len(header)) + header + b"\x00\x00"
)
return tmp_path


def test_rejects_mlx_lm_inkling2_checkpoint(tmp_path):
model_path = _write_checkpoint(tmp_path, "inkling2_loop", MLX_LM_INKLING2_NAME)

with pytest.raises(ValueError, match=UNSUPPORTED_INKLING2_MESSAGE):
raise_if_unsupported_checkpoint(model_path, {"model_type": "inkling2_loop"})


def test_rejects_single_file_mlx_lm_inkling2_checkpoint(tmp_path):
model_path = _write_single_file_checkpoint(tmp_path, MLX_LM_INKLING2_NAME)

with pytest.raises(ValueError, match=UNSUPPORTED_INKLING2_MESSAGE):
raise_if_unsupported_checkpoint(model_path, {"model_type": "inkling2_loop"})


@pytest.mark.parametrize(
"model_type, weight_name",
[
("inkling2_loop", MLX_VLM_INKLING2_NAME),
("inkling2_loop", ORIGINAL_INKLING2_NAME),
("qwen3_vl", MLX_LM_INKLING2_NAME),
],
)
def test_allows_supported_checkpoints(tmp_path, model_type, weight_name):
model_path = _write_checkpoint(tmp_path, model_type, weight_name)

raise_if_unsupported_checkpoint(model_path, {"model_type": model_type})


def test_load_model_rejects_before_creating_a_kit(monkeypatch, tmp_path):
model_path = _write_checkpoint(tmp_path, "inkling2_loop", MLX_LM_INKLING2_NAME)

def fail_if_created(*_args, **_kwargs):
raise AssertionError("No model kit should be created")

monkeypatch.setattr(generate_module, "BatchedVisionModelKit", fail_if_created)
monkeypatch.setattr(generate_module, "ModelKit", fail_if_created)

with pytest.raises(ValueError, match=UNSUPPORTED_INKLING2_MESSAGE):
generate_module.load_model(model_path)
Loading