Repository navigation
Add Qwen4 MoE - #2140
Draft
jundot wants to merge 1 commit into
Draft
Add Qwen4 MoE#2140jundot wants to merge 1 commit into
jundot wants to merge 1 commit into
Conversation
3 tasks done
This was referenced Dec 4, 2026
1 of 2 tasks
joshuaswarren
added a commit
to joshuaswarren/mlx-lm
that referenced
this pull request
Dec 4, 2026
* qwen4_exp: port Qwen4 MoE (qwen4_moe) model from upstream mlx-lm Ports ml-explore/mlx-lm's qwen4_exp package + PR ml-explore#2140 (FP8 expert dequant, per-expert stacking, indexed_attention layer-type alias, ngram weight_scale) onto this fork's models/ layout. Fork adaptations: old-layout imports (models.base / switch_layers / gated_delta / cache), Metal-only mlx_lm.kernels.sparse_attention imported under a guard so the gather+SDPA fallback runs on non-Metal backends, and an MtpModule that binds the checkpoint's mtp.* weights instead of dropping them (strict load keeps the draft head for speculative decoding; its forward is wired separately). test_qwen4_exp covers all four checkpoint layouts, n-gram shard merge, mtp.* retention, and prefill/decode/batch cache equivalence. * lint: pre-commit fixes for #5 + pre-existing .github/scripts failures black/isort reformats on the new qwen4_exp files and test_models.py. Also fixes .github/scripts contributor-upkeep files (added unformatted in #3, failing Check Lint on main): copyright headers, drop dead contextlib/io imports. * qwen4_exp: fix mtp pre_fc_norm_hidden size to hidden*hc_count The checkpoint normalizes the concatenated hyper-connection streams (8192 = 2048x4), not a single hidden stream.
dpalmqvist
added a commit
to motsognirr/applerum
that referenced
this pull request
Dec 4, 2026
…eckpoint layout (#800) * feat(models): serve Qwen4-35B-A3B through mlx-vlm 0.8.3, in either checkpoint layout (#798) Qwen4 (qwen4_moe) is qwen4_exp's architecture with other sizes, and mlx-vlm 0.8.3 serves it with the same Model class. It joins qwen4_exp in the native-hybrid routing and the VLM checkpoint path. The plain mlx-community Qwen4-35B-A3B-{3,4,6}bit repos were converted with mlx-lm (ml-explore/mlx-lm#2140/#2141) and use another weight layout, which mlx-vlm rejects. A sanitize hook installed in load_vlm rewrites such a checkpoint to mlx-vlm's layout at load: names, 1+w norms, the block-inject rows merged into input_mix_weight_down, per-head n-gram tables to row shards, and the flattened patch embedding. Rows are only moved, so quantized weights are not re-rounded. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> * fix(qwen4): pin the folded-norm list to mlx-vlm's classes; reject an unexpected patch-embed width Review follow-ups on #800. The folded-norm list is now tested against mlx-vlm's Qwen4ExpRMSNorm (the class that scales by 1 + weight) instead of only round-tripping against itself. A patch embedding that is not the flattened Conv3d width raises a clear error. The routing comment says why qwen4_moe is listed. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> * fix(qwen4): detect the mlx-lm layout by its per-head n-gram tables Review follow-up on #800. Detection required only the absence of mlx-vlm marks, so a text-only mlx-vlm checkpoint that stores one n-gram table (save_original_format=False) was claimed and mis-prefixed. It now needs the per-head tables only the mlx-lm port writes. The hook install also tolerates any import failure, so it cannot fail another VLM's load. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
KakkoiDev
added a commit
to KakkoiDev/agentbench
that referenced
this pull request
Dec 5, 2026
…ect into input_mix_weight_down; cut those rows back out on load (and drop the copies fetched yesterday) ml-explore/mlx-lm#2140 merges each hyper-connection's block_inject_weight into its input_mix_weight_down (260 = 256 + 4 rows); mlx-vlm keeps them apart, so these builds did not load in mlx-vlm. The rows are cut back out in the variant's sanitize (exact: rows are quantized one by one). Names and row counts checked against the model for the 3-bit (whole and table on disk) and the 4-bit table-on-disk builds; loading still to be smoke-tested (queue #6). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01MMzzHSGjhPnmLyjXfFdzE7
rromenskyi
added a commit
to ipsupport-llc/mlx-lm
that referenced
this pull request
Dec 5, 2026
Add Qwen4 MoE (text): upstream qwen4_exp package + ml-explore#2140
|
Tested this on an M4 Max with 128 GB (macOS 26.5.2), using
Speed and memory (
For scale, mlx-serve 26.12.2 decodes the same 4-bit pack at 125.0 tok/s on this machine with no draft model and no prompt lookup ( Thanks for getting this in! |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Requirements
* This is a draft because of the open PR limit. I will mark it ready for review once my other open PRs are merged.
Text only, through the qwen4_exp model. Image input and kernels will follow in separate PRs.
It also loads the FP8 checkpoint (as bf16) and configs saved by transformers.
The tokenizer is remote code and needs tiktoken and xgrammar. Its apply_chat_template does not take continue_final_message, which mlx_lm.generate always passes, so the command below writes the chat template into the prompt and uses --ignore-chat-template.
Model information
Supported checkpoints
Verification
Run mlx-lm command:
mlx_lm.generate --model Qwen/Qwen4-35B-A3B --trust-remote-code --ignore-chat-template -p '<|im_start|>system\nReasoning effort is set to high.<|im_end|>\n<|im_start|>user\nThe secret to baking a good cake is<|im_end|>\n<|im_start|>assistant\n<think>' -m 1024Output: