Skip to content

Add Qwen4 MoE (text): upstream qwen4_exp package + #2140 - #68

Merged
rromenskyi merged 3 commits into
mainfrom
qwen4-moe
Dec 5, 2026
Merged

rromenskyi merged 3 commits into
mainfrom
qwen4-moe

Conversation

@rromenskyi

@rromenskyi rromenskyi commented Dec 5, 2026 •

Copy link
Copy Markdown

Adds Qwen4-35B-A3B (model_type: qwen4_moe, text only).

Upstream implements Qwen4 MoE on the qwen4_exp model, which it has since rewritten as a package with QSA sparse attention (ml-explore#2082, ml-explore#2112). This fork still had the single file from ml-explore#1788.

  • models/qwen4_exp/: upstream's package plus Add Qwen4 MoE ml-explore/mlx-lm#2140 (Qwen4 MoE configs, FP8 and per-expert checkpoints). The old qwen4_exp.py is removed; its only fork commit was lint.
  • kernels/sparse_attention.py: upstream's indexed attention kernels.
  • layers/attention.py, layers/recurrent.py: upstream's import paths, re-exporting the fork's models.base / models.gated_delta helpers (same signatures).
  • --mmap-lookup-tables now covers Qwen4's n-gram tables (~11 GB at 4 bits). They are a list of embeddings, so map_lookup_tables also matches list entries by the list's name.
  • reasoning_effort: "off" (or "none") sets enable_thinking false when the request doesn't. Qwen4's template rejects enable_thinking=true with effort off, and the wrapper defaulted it to true.

Checks:

  • Logits match jundot's Add Qwen4 MoE ml-explore/mlx-lm#2140 branch to 5e-7 (full pass and cached decode) on a tiny random model with the same weights.
  • The 2271 tensors of mlx-community/Qwen4-35B-A3B-4bit-text match the model tree (keys and shapes, read from the safetensors headers).
  • The fork server with the real Qwen4 tokenizer (tokenizer-compat) on a tiny random model, under mlx 0.32.4 and transformers 5.24.0: streaming, tools, a tool result, thinking budget, --kv-bits 8, 4 concurrent requests. Tool parser <tool_call>, <think>/</think> and EOS 248044/248046 are inferred.
  • Tests: upstream's qwen4_exp and QSA tests, a mapped n-gram tables test, a reasoning-effort-off test. test_models, test_tokenizers, test_server, test_prompt_cache, test_batch_quantized_kv and test_generate pass.

Known behavior:

  • --kv-bits leaves Qwen4's KV cache in full precision. Its CacheList (attention KV plus the indexer's cache) is skipped by the KV quantization paths, as upstream does. Only 10 of the 40 layers have KV, with 2 KV heads.
  • The reasoning_effort change also applies to other templates: a Qwen3 template that ignores reasoning_effort now gets thinking off when a request sends off or none without enable_thinking.
  • A mapped table whose tensors or shapes differ from its module stays loaded, so the strict load reports it (codex).

Not included: image input (ml-explore#2141) and the MTP head.

Reviews: codex (findings above addressed or listed). muse could not run: its quota is used up until 2026-12-07.

🤖 Generated with Claude Code

Roman Romenskyi and others added 3 commits December 5, 2026 09:51
Qwen4-35B-A3B (model_type qwen4_moe) runs on the qwen4_exp model, which
upstream rewrote as a package with QSA sparse attention (ml-explore#2082, ml-explore#2112)
after the single file this fork took in ml-explore#1788.

- models/qwen4_exp/: upstream's package with ml-explore#2140
  (Qwen4 MoE configs, FP8 and per-expert checkpoints); the old
  qwen4_exp.py is gone.
- kernels/sparse_attention.py: upstream's indexed attention kernels.
- layers/attention.py, layers/recurrent.py: upstream's import paths,
  re-exporting the fork's models.base / models.gated_delta helpers
  (same signatures).
- --mmap-lookup-tables reads Qwen4's n-gram tables (a list of
  embeddings, ~11 GB at 4 bits) from the file: map_lookup_tables
  matches list entries by the list's name.
- reasoning_effort "off" (or "none") now sets enable_thinking false
  when the request doesn't: Qwen4's template rejects enable_thinking
  true with effort off.

Checked: logits equal to upstream's branch (5e-7, full and cached
decode) on a tiny random model; the mlx-community 4-bit checkpoint's
2271 tensors match the model tree (keys and shapes, from the file
headers); the fork server with the real Qwen4 tokenizer on a tiny
model: streaming, tools, tool results, thinking budget, --kv-bits 8,
4 concurrent requests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…odex)

map_lookup_tables removes the tensors before the strict load: a table of
the wrong shape, or without the biases its module has, was mapped
unchecked. It now stays loaded and load_weights reports it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@rromenskyi
rromenskyi merged commit b966a30 into main Dec 5, 2026
2 checks passed
@rromenskyi
rromenskyi deleted the qwen4-moe branch December 5, 2026 17:08
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