Repository navigation
Add tensor parallel sharding for Qwen3 MoE - #1774
Merged
Merged
Conversation
azamamirza
added a commit
to azamamirza/mlx-lm
that referenced
this pull request
Aug 27, 2026
Rebased onto upstream/main 74e7cf9 (adds ml-explore#1673 ChunkedKVCache valid-length trim — which supersedes and retires our own fix — plus ml-explore#1772 GenerationBatch filter, ml-explore#1775 decode-loop thread-safety, and new model support). Our two qwen3_moe patches (tensor-parallel ml-explore#1774 submitted, pipeline staged) ride on top unchanged and rebased clean.
Collaborator
|
Thank you for your contribution! Will test it as soon as possible. |
Collaborator
|
Could you please provide the model name from hugging face that you are using for testing? |
Contributor
Author
|
We validated it on mlx-community/Qwen3-235B-A22B-4bit (https://huggingface.co/mlx-community/Qwen3-235B-A22B-4bit) - tensor-parallel across a 2-node ring. Since that one needs multiple machines, the same qwen3_moe code path also runs on the much smaller mlx-community/Qwen3-30B-A3B-4bit (https://huggingface.co/mlx-community/Qwen3-30B-A3B-4bit), which is easy to reproduce with mlx.launch ... --backend ring. Both are Qwen3MoeForCausalLM. |
Collaborator
|
Looks good to me, tested on 4 m5 ultras. |
nastya236
approved these changes
Aug 28, 2026
azamamirza
added a commit
to azamamirza/mlx-lm
that referenced
this pull request
Sep 1, 2026
Regenerated from upstream/main e93eaf7. Both qwen3_moe patches are now upstream (ml-explore#1774 tensor parallel, merged 2026-08-22; ml-explore#1801 pipeline, merged 2026-08-29 - the base commit itself), so this branch carries ZERO code patches: upstream + this document + the version marker. Zero functional divergence for the first time since the fork was created.
4 tasks
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.
Add tensor parallel sharding for Qwen3 MoE
Problem
qwen3_moehas noModel.shard(), so Qwen3 MoE checkpoints (e.g.Qwen3-30B-A3B, Qwen3-235B-A22B) cannot be tensor-parallel sharded across
multiple machines the way
glm4_moe,deepseek_v3, and other MoE modelsalready can.
Change
Follows the established
glm4_moepattern exactly:Qwen3MoeSparseMoeBlockgains asharding_group; when set, the blockapplies
sum_gradientson the input andmx.distributed.all_sumon theoutput. The router gate stays replicated. The expert-weighted sum gets
.astype(y.dtype)for dtype parity withglm4_moe.Model.shard(group): attentionq/k/v_projsharded all-to-sharded,o_projsharded-to-all, withn_heads/n_kv_headsdivided by thegroup size; dense-MLP layers (
mlp_only_layers/decoder_sparse_step)sharded via
shard_linear; MoE layers sharded in place onswitch_mlp.{gate,down,up}_projviashard_inplace.qwen3_moeconfig (including a densemlp_only_layers=[0]layer so both MLP branches are exercised) to
tests/model_parallel_tests.py::test_shard.Verification
tests/model_parallel_tests.py: 1 passed, 4 subtests passed (the newqwen3_moe subtest shards with a size-1 group and matches the unsharded
output).
tests/test_models.py -k qwen3: 3 passed.the 0.31.3 release): bit-exact vs unsharded at group size 1 (float and
4-bit); max abs logit diff ~3e-8 at group size 2 on a local ring (float
and 4-bit); sustained 2-node run of mlx-community/Qwen3-235B-A22B-4bit
sharded across two Apple Silicon machines over RDMA (Thunderbolt 5),
producing coherent generations.
black/isort --profile=blackclean.