Skip to content

feat(inkling2): load Inkling 2 checkpoints saved by mlx-vlm - #5605

Merged
jundot merged 4 commits into
jundot:mainfrom
penta2himajin:feat/inkling2-mlx-vlm-layout
Nov 25, 2026
Merged

jundot merged 4 commits into
jundot:mainfrom
penta2himajin:feat/inkling2-mlx-vlm-layout

Conversation

@penta2himajin

@penta2himajin penta2himajin commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This adds loading support for Inkling 2 checkpoints (inkling2 and inkling2_loop) saved by mlx-vlm's inkling2 model (added in Neywa-Labs/mlx-vlm#2778; checked against d998a62). Published-layout checkpoints and oQ outputs keep their current loading paths.

mlx-vlm renames the published checkpoint and splits its fused tensors, so these builds ship language_model.model.layers.N.self_attn.q_proj, mlp.switch_mlp.{gate,up,down}_proj, vision_tower.layers.N.mlp.fc1 and so on. The Inkling 2 loader maps only the published model.llm.* layout, so they fail with Received 1849 parameters not in model: language_model.lm_head.biases, .... Example: orcarouter/Inkling-2-Mini-abliterated-MLX.

  • loader.py: sanitize_weights detects language_model.model.layers.* keys and undoes mlx-vlm's Model.sanitize (mlx_vlm/models/inkling2/inkling2.py at d998a62):
    • Attention projections are concatenated along their output rows into in_proj (sliding) or q_proj/k_proj/v_proj (global). Quantized codes are row-local, so quantized parts are concatenated as they are. Unquantized parts that share the fused module (the attention sink, the global layers' dynamic conv projections) are quantized to the fused module's spec first.
    • switch_mlp.gate_proj/up_proj are re-interleaved into w13 (mlx-vlm splits even/odd rows), and weight_scale_2 (amax / denominator) is converted back to the runtime's global_scale (amax). Gate and up must carry the same outer scale; the loader raises otherwise.
    • Mixed-precision builds can quantize the rows of one fused module at different affine bits (the orcarouter 12GB build has 2-bit gate and 3-bit up in 10 layers). The fused module takes the widest bits, and narrower rows are re-packed at that width with their scales and biases unchanged: an affine code q stands for scale * q + bias at any width, so the weights stay the same. The source width comes from the tensor shapes and the target from the config. Rows with a different group size or mode raise. (Inkling Small's compat path leaves a layer unfused when its Q/K/V/R specs differ; the Inkling 2 runtime always fuses, so the loader re-packs instead.)
    • message_queries already holds each core layer's rows in the runtime's (attn, k, v, mlp) order; the vision tower names go back under backbone.
  • model_loading.py: normalize_inkling2_mlx_vlm_quant rewrites the per-module quantization keys onto the runtime module paths before expand_per_layer_quant_keys, so nn.quantize builds the fused modules and the expert carriers with the checkpoint's specs. Modules that the runtime fuses take the widest of their affine specs when they share a group size, and raise otherwise. It only acts while the keys still name mlx-vlm's modules (self_attn, switch_mlp), so running it again on a rewritten config is a no-op.

Why only Inkling 2. Other VLMs run on mlx-vlm's own model classes (or the vendored compat copies), so mlx-vlm checkpoints already match their parameter trees. Inkling 2 is the one family served by oMLX's own module through the VLM engine adapter, with its fused layout, and the mapping is specific to that layout.

Out of scope. mlx-community/Inkling-2-Mini-4bit is saved in the layout of ml-explore/mlx-lm#2086 (model.language_model.*, fused gate_up_proj). It fails with Received 1556 parameters not in model on 0.8.2 and on main, and fails the same way with this PR. I left it out since that layout is still under review.

Validation

Tests (tests/test_inkling2_mlx_vlm_layout.py): the tiny Inkling 2 source from test_oq.py, rewritten by a fixture that mirrors mlx-vlm's sanitize at d998a62.

  • Plain and looped sources (NVFP4 + MXFP8 experts, vision tower) load with parameters identical to the published layout; global_scale to rtol=1e-6.
  • With 8-bit affine attention, every fused projection equals mx.quantize of the runtime's fused BF16 weight, including the sink and dynamic-conv rows.
  • Experts with gate/up/down at different affine bits (2/3/4, 4/2/8, 3/6/3) load with w13/w2 dequantizing exactly to the source rows.
  • Fused specs take the widest affine bits; a different group size or mode raises.
  • Running the normalizer again on a rewritten config (which by then also carries the language_model.-prefixed key variants) leaves it unchanged.

Without the fix, the load tests fail with Received 218 parameters not in model. The default suite (python -m pytest -n 8): 19,446 passed, 1 failed. The failure, test_torch_stub.py::test_xgrammar_imports_against_stub_only, fails the same way on main here because xgrammar is not installed. I ran the tests on the oMLX 0.8.2 app bundle's Python and packages (same MLX / mlx-lm / mlx-vlm pins as main) plus pytest, not through make dev.

Control run on M3 Pro 36 GB, MLX 0.32.4: main (8515583f) and this PR, each served from source with the prefix cache off, greedy, reasoning_effort=low, two prompts (Japanese prose, Python code) × 256 tokens.

Checkpoint (layout) main this PR
mlx-community/Inkling-2-Mini-oQ4e-mtp (oQ) loads loads; same output text as main on both prompts
orcarouter/Inkling-2-Mini-abliterated-MLX, 16GB (mlx-vlm) Received 1849 parameters not in model loads
mlx-community/Inkling-2-Mini-4bit (mlx-lm#2086) Received 1556 parameters not in model same error

Real model through the server: both orcarouter builds load and generate coherent Japanese and English text and code. The 16GB build has mixed NVFP4 / 4-bit affine experts; the 12GB build has 2- to 4-bit affine experts, with gate and up at different bits in 10 layers. On the 16GB build with Lightning MTP on, the bundled DSpark drafter (mtp.safetensors from the official release) loads against the converted target and its drafts are accepted (fixed depth 7: 69% on code, 38% on Japanese prose, 56% on English prose, 75% on rewriting given code).

With other features (both orcarouter builds, and mlx-community/Inkling-2-Mini-oQ4e-mtp as the control; greedy, 200 tokens, a Japanese prose prompt and a code-edit prompt):

  • Lightning MTP with the bundled DSpark drafter (adaptive depth and fixed depth 3), a prefix-cache hit on a repeated request, and a non-streaming request each give the same output as a single streamed request without MTP, on all three checkpoints.
  • Two concurrent requests diverge from their sequential outputs after 30 to 250 characters on all three, the oQ control included, with coherent text on both sides, so I attribute that to batched decoding rather than to this change.
  • Enabling DFlash is rejected by the admin API for all three (Inkling 2 is not in the DFlash model list), the same as on main.

Agreement with mlx-vlm d998a62, with the official thinkingmachines/Inkling-2-Mini checkpoint as the control (both implementations load it without any conversion):

official (control) orcarouter 16GB orcarouter 12GB
text, 245 tokens: mean KL / top-1 agreement 0.0021 / 98.8% 0.0051 / 98.8% 0.0020 / 99.6%
vision tower output on one image: cosine / relative L2 0.9997 / 2.2% 0.9997 / 2.4% 0.9997 / 2.4%
text positions after that image: mean KL / top-1 agreement 0.022 / 92.9% 0.015 / 96.4% 0.011 / 96.4%

The vision gap and the 12GB build's text gap match the control, so they lie between the two implementations rather than in this mapping. The 16GB build's text KL is above the control. Rounding the sink and dynamic-conv rows to 8 bits on the mlx-vlm side as well (what this PR does to them) only brings it to 0.0047, and I have not found the rest.

Image input: both builds answer three synthetic images correctly through the server (the colors of the left and right halves, the text "OMLX 4721", three green circles), the same as the oQ control.

Not tested: plain inkling2 checkpoints saved by mlx-vlm (only the unit tests cover them; Inkling 2 Small does not fit in 36 GB), and oQ re-quantization of mlx-vlm-layout sources.

Checklist

  • I read CONTRIBUTING.md.
  • I understand every change in this PR and can explain it in review.
  • For UI or UX changes, I attached before and after screenshots. (No UI changes.)

🤖 Generated with Claude Code

mlx-vlm's inkling2 model (inkling2 and inkling2_loop) saves checkpoints
under its own module names with split tensors, for example
orcarouter/Inkling-2-Mini-abliterated-MLX, and the Inkling 2 loader only
mapped the published layout, so they failed with "parameters not in
model". Undo mlx-vlm's sanitize in the loader: fuse the attention
projections along their rows (quantizing the unquantized sink and dynamic
conv rows to the fused spec), re-interleave gate/up into w13, convert
weight_scale_2 back to global_scale, and map message queries and vision
names. Rewrite the per-module quantization keys onto the runtime paths in
load_config so nn.quantize builds matching modules. Published-layout
checkpoints and oQ outputs keep their paths.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@penta2himajin
penta2himajin force-pushed the feat/inkling2-mlx-vlm-layout branch from ea999c4 to 70e40e3 Compare November 24, 2026 09:53
@penta2himajin penta2himajin changed the title fix(inkling2): load checkpoints in mlx-vlm's layout feat(inkling2): load Inkling 2 checkpoints saved by mlx-vlm Nov 24, 2026
Mixed-precision mlx-vlm builds can quantize the rows that the runtime
fuses at different affine bits; the orcarouter 12GB build has 2-bit gate
and 3-bit up in 10 layers. Let a fused module take the widest affine spec
among its rows when they share a group size, and re-pack narrower codes at
that width with their scales and biases unchanged, which keeps the weights
exactly. Other mixes still raise.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@penta2himajin
penta2himajin marked this pull request as ready for review November 24, 2026 10:46

@jundot jundot left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I merged main into the branch and pushed a smaller version that keeps your mapping logic as it is. The tests moved into test_oq.py and now use the sanitize from the pinned mlx-vlm instead of a copy. Thank you for adding this, merging.

@jundot
jundot merged commit a71be1b into jundot:main Nov 25, 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.

2 participants