Skip to content

Add tensor parallel sharding for Qwen3 MoE - #1774

Merged
nastya236 merged 1 commit into
ml-explore:mainfrom
azamamirza:qwen3-moe-tensor-parallel
Aug 28, 2026
Merged

nastya236 merged 1 commit into
ml-explore:mainfrom
azamamirza:qwen3-moe-tensor-parallel

Conversation

@azamamirza

Copy link
Copy Markdown
Contributor

Add tensor parallel sharding for Qwen3 MoE

Problem

qwen3_moe has no Model.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 models
already can.

Change

Follows the established glm4_moe pattern exactly:

  • Qwen3MoeSparseMoeBlock gains a sharding_group; when set, the block
    applies sum_gradients on the input and mx.distributed.all_sum on the
    output. The router gate stays replicated. The expert-weighted sum gets
    .astype(y.dtype) for dtype parity with glm4_moe.
  • Model.shard(group): attention q/k/v_proj sharded all-to-sharded,
    o_proj sharded-to-all, with n_heads / n_kv_heads divided by the
    group size; dense-MLP layers (mlp_only_layers / decoder_sparse_step)
    sharded via shard_linear; MoE layers sharded in place on
    switch_mlp.{gate,down,up}_proj via shard_inplace.
  • Adds a tiny qwen3_moe config (including a dense mlp_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 new
    qwen3_moe subtest shards with a size-1 group and matches the unsharded
    output).
  • tests/test_models.py -k qwen3: 3 passed.
  • Hardware verification of the same change (originally applied on top of
    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=black clean.

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.
@zcbenz
zcbenz requested a review from nastya236 August 28, 2026 11:15
@nastya236

Copy link
Copy Markdown
Collaborator

Thank you for your contribution! Will test it as soon as possible.

@nastya236

Copy link
Copy Markdown
Collaborator

Could you please provide the model name from hugging face that you are using for testing?

@azamamirza

Copy link
Copy Markdown
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.

@nastya236

Copy link
Copy Markdown
Collaborator

Looks good to me, tested on 4 m5 ultras.

@nastya236
nastya236 merged commit ff1bca2 into ml-explore:main Aug 28, 2026
2 checks passed
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.
@azamamirza
azamamirza deleted the qwen3-moe-tensor-parallel branch September 1, 2026 22:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants