Skip to content

Add Mixtral (sparse MoE) model - #378

Merged
davidkoski merged 1 commit into
ml-explore:mainfrom
GoodOlClint:pr/mixtral
Jun 29, 2026
Merged

davidkoski merged 1 commit into
ml-explore:mainfrom
GoodOlClint:pr/mixtral

Conversation

@GoodOlClint

Copy link
Copy Markdown
Contributor

Proposed changes

Adds the Mixtral sparse-MoE architecture (model_type: "mixtral") to MLXLLM — a port of mlx-lm's mixtral.py. This covers the Mixtral-8x7B / 8x22B family, which currently has no Swift implementation.

Structure mirrors the in-tree PhiMoE: Mistral-style attention (GQA, RoPE, bias-free) with a sparse MoE block — gate (no bias) → top-k → softmax over the selected expert logits → SwitchGLU. It differs from PhiMoE in using RMSNorm (not LayerNorm), plain RoPE (not SuScaled), bias-free projections, and optional tied embeddings.

sanitize() stacks the per-expert w1/w2/w3 tensors (weight/scales/biases) into switch_mlp.gate_proj/down_proj/up_proj, so both full-precision and pre-quantized Mixtral checkpoints load.

Tests (MixtralTests, runs without downloading a model): forward-pass logit shape on a synthetic 1-layer config, and sanitize() expert-stacking shape/key remap. Verified via xcodebuild test ... -only-testing:MLXLMTests/MixtralTests → 2 tests, 0 failures, TEST SUCCEEDED.

Checklist

  • I have read the CONTRIBUTING document
  • I have run pre-commit run --all-files to format my code / installed pre-commit prior to committing changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the necessary documentation (if needed)

Port of mlx-lm's mixtral.py: Mistral-style attention (GQA, RoPE, no
bias) with a sparse MoE block — gate (no bias) → top-k → softmax over
the selected expert logits → SwitchGLU. Mirrors the in-tree PhiMoE
structure; differs in using RMSNorm (not LayerNorm), plain RoPE (not
SuScaled), bias-free projections, and optional tied embeddings.

sanitize() stacks per-expert w1/w2/w3 (weight/scales/biases) into
switch_mlp.gate_proj/down_proj/up_proj, so both full-precision and
pre-quantized Mixtral checkpoints load. Registered as "mixtral".

Tests (MixtralTests): forward-pass logits shape on a synthetic 1-layer
config, and sanitize() expert-stacking shape/key remap.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@GoodOlClint

Copy link
Copy Markdown
Contributor Author

End-to-end validation (real weights). Loaded mlx-community/Mixtral-8x7B-Instruct-v0.1-4bit and ran a forward pass on a fixed token sequence [1..8], comparing the per-position argmax + last-position top-5 against the Python mlx_lm reference:

argmax top5(last)
Python mlx_lm [1247,28730,28721,3,13,13,13,13] [13,12,28819,3,28841]
this PR (Swift) [1247,28730,28721,3,13,13,13,13] [13,12,28819,3,28841]

Byte-exact match including the quantized expert-stacking sanitize path.

@davidkoski davidkoski left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thank you!

@davidkoski
davidkoski merged commit ccdc386 into ml-explore:main Jun 29, 2026
2 checks passed
@GoodOlClint
GoodOlClint deleted the pr/mixtral branch June 29, 2026 20:06
dkgv pushed a commit to dkgv/mlx-swift-lm that referenced this pull request Jul 30, 2026
Port of mlx-lm's mixtral.py: Mistral-style attention (GQA, RoPE, no
bias) with a sparse MoE block — gate (no bias) → top-k → softmax over
the selected expert logits → SwitchGLU. Mirrors the in-tree PhiMoE
structure; differs in using RMSNorm (not LayerNorm), plain RoPE (not
SuScaled), bias-free projections, and optional tied embeddings.

sanitize() stacks per-expert w1/w2/w3 (weight/scales/biases) into
switch_mlp.gate_proj/down_proj/up_proj, so both full-precision and
pre-quantized Mixtral checkpoints load. Registered as "mixtral".

Tests (MixtralTests): forward-pass logits shape on a synthetic 1-layer
config, and sanitize() expert-stacking shape/key remap.

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
div0-space pushed a commit to LibraxisAI/mlx-swift-lm that referenced this pull request Nov 29, 2026
Brings ml-explore#378 up to the SDK the provider pins for 0.10.12. No conflicts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
div0-space pushed a commit to LibraxisAI/mlx-swift-lm that referenced this pull request Nov 29, 2026
…d-buffers

No conflicts: ml-explore#398 touches none of ml-explore#378's files.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
div0-space pushed a commit to LibraxisAI/mlx-swift-lm that referenced this pull request Nov 29, 2026
…-command-buffers

CBv2: Nemotron 3.5 plain decode commits by operation count; complete checkpoints stay native
div0-space pushed a commit to LibraxisAI/mlx-swift-lm that referenced this pull request Nov 29, 2026
…ed-row-reads

ml-explore#378 (Nemotron 3.5 Lightning: plain decode commits by operation count;
complete checkpoints stay native) merged at 19:11Z. Its four files are
disjoint from this branch's; Lightning's checkpoints now stay native, so they
do not take the coded path this branch changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
div0-space pushed a commit to LibraxisAI/mlx-swift-lm that referenced this pull request Nov 29, 2026
…t-batched-row-reads

CBv2: a lossy checkpoint export reads rows in batches and codes them on several cores (same bytes). Merged by an agent with the owner's approval (11-10) after the independent review (review-stream-tail.md: approve, merge after ml-explore#378 as a merge commit); Fork Tests 42882853010 on 781c382: success, all 10 shards.
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.

2 participants