Skip to content

deps: move mlx-lm to upstream ml-explore/mlx-lm - #2509

Merged
AlexCheema merged 1 commit into
mainfrom
deps/mlx-lm-upstream
Nov 1, 2026
Merged

AlexCheema merged 1 commit into
mainfrom
deps/mlx-lm-upstream

Conversation

@AlexCheema

Copy link
Copy Markdown
Contributor

Stacked on #2508 (MLX 0.32.4), which sits on #2507 (the per-family folder refactor).

Why

exo has followed an mlx-lm fork branch: upstream as of 2026-04-22 plus DeepSeek V4 and three fixes. It is 169
commits behind upstream, and the models we want next (GLM-5.2 indexer sharing, DeepSeek V3.2 sparse attention,
flint, Kimi K3, Muse Spark, MiniMax M3, Laguna) are upstream. This PR pins upstream main at
6b572ab (2026-10-30) by
rev, so uv lock --upgrade cannot move it silently.

What exo relied on from the fork, and where it went

fork-only behaviour upstream here
DeepSeek V4 model and its cache classes not upstream the fork's file, copied with a provenance header to models/deepseek_v4/model.py and loaded through the family's ModelClasses; its stub moves next to it
one [batch, vocab] log-prob array per decode step (exo's batched top-k) lists of rows exo's batch patch also patches GenerationBatch.extend
F8_E8M0 safetensors loading MLX loads F8_E8M0 itself (in exo's pin) nothing needed
Gated DeltaNet gate and beta in float32 bfloat16 nothing: measurably worse at kernel level, not against a float32 reference (below)
ArraysCache graph-growth fix fixed differently upstream nothing needed
DeepSeek V3.2 indexer with batch > 1 equivalent masking upstream nothing needed
float32 log-softmax (dropped from the fork before exo pinned it) not upstream added to exo's batch step (separate commit)

Other upstream changes this follows

  • A cache's state now holds the whole cache (an ArraysCache returns its arrays, left padding and lengths). The
    prefix cache's trim fallback and the prefill transfer adapter now use the arrays directly; both broke otherwise.
  • TokenizerWrapper builds the streaming detokenizer once and hands out copies, which is what exo's
    reuse_detokenizer patch did. The patch goes; its tests now check mlx-lm's behaviour.
  • When a request leaves enable_thinking unset, upstream's wrapper turns on the template's thinking switch. For
    DeepSeek V3.1 that switch is thinking, whose template default is off. exo keeps rendering such requests as before
    (separate commit).
  • GLM MoE DSA layers (GLM-5, GLM-5.1) now return (hidden state, top-k). Pipeline parallelism sent the whole tuple
    and failed. It now sends the hidden state and hands the rest back (separate commit, with a test).
  • transformers: upstream needs >= 5.7, and 5.7 cannot load the DeepSeek V3.2 or V4 tokenizers. Pinned to 5.13.1, the
    newest that keeps huggingface-hub 1.8 and tokenizers 0.22. The copied DeepSeek V4 file registered a stand-in
    transformers config that now shadows transformers' own and breaks the tokenizer; it only registers when
    transformers has none.
  • nix: upstream mlx-lm takes its version from git. Without setuptools-scm it installed as 0.0.0 without
    mlx_lm/_version.py, and import mlx_lm failed in the nix environment. The nix build now provides setuptools-scm
    and the locked version.

Fixed along the way

  • DeepSeek V3.2 long context. The fork's V3.2 indexer used interleaved RoPE (fixed upstream in [BUG] Runner crashed with critical exception #1431), so
    beyond index_topk (2048 tokens) it selected the wrong keys. On 4 layers of DeepSeek-V3.2 with index_topk set to
    64, KL to Hugging Face transformers drops from 4.1e-2 (top-1 74%) to 4.9e-7 (top-1 99.94%).
  • DeepSeek V3.1/V3.2 tokenizer. With transformers 5.6 they loaded as a SentencePiece-style tokenizer: "What is
    2+2?" lost the space before "2", and decoding dropped every space and all non-ASCII text. They now match their
    tokenizer.json.

Verification

Teacher-forced float32 log-probs from exo's own loading and sharding code (single device, 2x tensor parallel,
2x pipeline parallel, prefill plus 16 decode steps) against Hugging Face transformers (PyTorch, CPU, float32) on the
same weights. Large models are truncated to their first 3 to 6 layers with real weights. "Identical" means
bit-identical to main. KL is KL(reference || exo) per position.

family checkpoint single 2x TP 2x PP KL main -> this PR (single)
llama Llama-3.2-1B-Instruct identical identical identical 2.9e-4
qwen3 Qwen3-0.6B identical identical identical 8.3e-4
qwen3_moe Qwen3-30B-A3B, 4 layers identical identical identical 1.3e-3
qwen3_next Qwen3-Next-80B-A3B, 4 layers within noise within noise within noise 1.37e-4 -> 1.33e-4
qwen3_5 Qwen3.5-0.8B within noise within noise within noise 4.21e-4 -> 4.38e-4
qwen3_5 Qwen3.5-9B (32 layers) within noise - - 4.02e-4 -> 4.03e-4
qwen3_5_moe Qwen3.5-35B-A3B, 4 layers within noise within noise within noise 1.15e-4 -> 1.18e-4
qwen3_vl Qwen3-VL-4B-Instruct-4bit identical identical identical vs main only
deepseek_v3 (Kimi K2) DeepSeek-V3.1, 4 layers, YaRN identical identical identical 8.4e-5
deepseek_v32 DeepSeek-V3.2, 4 layers identical identical NaN on main and here 7.7e-8
deepseek_v32, sparse indexer same, index_topk 64 fixed fixed NaN on main and here 4.1e-2 -> 4.9e-7
glm_moe_dsa GLM-5, 4 layers identical identical identical (with the PP fix) 1.1e-5
glm_moe_dsa, sparse indexer same, index_topk 64 +1e-6 KL within noise +1e-6 KL 1.30e-5 -> 1.41e-5
glm4_moe GLM-4.5-Air, 3 layers identical identical identical 1.6e-4
glm4_moe_lite GLM-4.7-Flash, 3 layers identical identical identical 8.7e-4
minimax MiniMax-M2.7, 3 layers identical identical identical 5.3e-5
gpt_oss gpt-oss-20b (bfloat16 reference) identical identical identical 1.0e-3
gemma4 gemma-4-E2B-it identical n/a (1 KV head) n/a (PP not allowed) 5.0e-2 (pre-existing)
nemotron_h Nemotron-3-Nano-30B-A3B, 6 layers identical identical identical 1.1e-2 (pre-existing)
nemotron_h Nemotron-Nano-9B-v2-4bits identical identical (broken on main too) identical vs main only
ministral3 Ministral-3-3B-Instruct-2512 identical n/a (vision wrapper) identical 1.9e-4
step3p5 Step-3.5-Flash, 5 layers identical identical identical vs main only
deepseek_v4 DeepSeek-V4-Flash 8-bit, 4 layers identical identical identical vs main only

"Within noise": the paired difference in per-position KL to the reference has a 95% interval that contains zero.
The Gated DeltaNet families differ because upstream computes the gate in bfloat16; against the float32 reference that
is not measurable, even on the full Qwen3.5-9B. GLM-5 with the sparse indexer forced on moves by about 1e-6 KL (the
indexer sums its heads in a different order upstream), a tenth of its distance to the reference and far below bfloat16
noise. Every other family is identical to main or closer to the reference.

Also checked:

  • Chat templates and tokenizers for one card of every base model (51): identical prompt token ids except DeepSeek
    V3.1/V3.2 (fixed, above). Kimi K2.x and GLM-4.7-Flash gain one more stop token from their tokenizer config;
    Nemotron-Nano-9B-v2's <think> tokens are now recognised; Llama 3 decoding no longer removes spaces before
    punctuation.
  • Continuous batching through exo's batch generator (requests joining mid-flight, finishing at different
    lengths) on nine checkpoints, including DeepSeek V4 and DeepSeek V3.2 / GLM-5 with the sparse indexer: no errors,
    and batched output departs from solo at the same steps as on main. Log-probs are now float32.
  • A two-machine cluster running exo (Llama-3.2-1B-Instruct-4bit and gpt-oss-20b-MXFP4-Q8; one node, 2-node
    tensor, 2-node pipeline): the same greedy tokens as main for every request.
  • basedpyright, ruff check, nix fmt, pytest (1919 passed), nix build .#exo-test-env,
    uv sync --extra mlx, and PyInstaller's module collection for mlx-lm.

Hardware: two Mac Studio (M3 Ultra, 96 GB) and two Mac mini (M4 Pro, 64 GB); the references ran on their CPUs.

Not changed here (pre-existing on main)

  • Nemotron-Nano-9B-v2 under tensor parallelism produces garbage (KL ~9 nats per token); pipeline is fine.
  • MLX nemotron_h and gemma4 (E2B) are further from transformers than bfloat16 noise (KL 1e-2 and 5e-2).
  • DeepSeek V3.2 pipeline parallelism returned NaN in the two-rank test.
  • exo's YaRN override ignores truncate: false (gpt-oss).

Follow-up suggestion: computing the Gated DeltaNet gate and beta in float32 (two casts before
gated_delta_update) brought Qwen3.5-9B 7.5% closer to the reference (95% interval 2% to 12%), and was neutral on
smaller checkpoints. It belongs in the qwen3_5 family with the Qwen model work.

MLX

Upstream mlx-lm needs MLX >= 0.32.3: its quantised MoE layers pass global_scale to mx.gather_qmm. That is why
this PR sits on the MLX 0.32.4 bump, which moves Linux off the 0.32.0 wheels. On an aarch64 Linux CPU,
a 4-bit quantised MoE runs with this PR on MLX 0.32.4 and fails with a TypeError on MLX 0.32.0. On macOS the
per-family results above are unchanged on 0.32.4 (Llama, Qwen3.5 and gpt-oss rechecked in all three modes).

🤖 Generated with Claude Code

Landing commit: this PR's change on top of main, with conflicts with the PRs landed before it resolved as in the tested integration build.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@AlexCheema
AlexCheema force-pushed the deps/mlx-lm-upstream branch from d5eb3a5 to 66ea418 Compare November 1, 2026 16:02
@AlexCheema
AlexCheema merged commit a49d1e0 into main Nov 1, 2026
6 checks passed
@AlexCheema
AlexCheema deleted the deps/mlx-lm-upstream branch November 1, 2026 16:10
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.

1 participant