Skip to content

fix(jamba): stack per-expert weights under feed_forward.switch_mlp - #223

Merged
Gajesh2007 merged 2 commits into
mainfrom
fix/jamba-sanitize-experts
Oct 2, 2026
Merged

Gajesh2007 merged 2 commits into
mainfrom
fix/jamba-sanitize-experts

Conversation

@davidtai

@davidtai davidtai commented Oct 1, 2026 •

Copy link
Copy Markdown

Proposed changes

Fixes #200

Defect. Libraries/MLXLLM/Models/Jamba.swift:537-558 (main 0871943). sanitize(weights:) looks for model.layers.0.block_sparse_moe.experts.0.w1.weight (line 537) and writes the stacked tensors to model.layers.N.block_sparse_moe.switch_mlp.* (line 557). The module path is feed_forward (line 373) with switch_mlp inside it (line 340). A Hugging Face checkpoint with feed_forward.experts.N.{gate,up,down}_proj tensors fails the strict update, and the model does not load.

Reference. mlx_lm/models/jamba.py:355-369 (ml-explore/mlx-lm@a9bd8af) stacks model.layers.L.feed_forward.experts.E.<proj>.<name> for gate_proj, down_proj, up_proj and weight, bias, scales, biases into model.layers.L.feed_forward.switch_mlp.<proj>.<name>.

Fix. sanitize(weights:) does the same as the reference: for each layer, projection and tensor name it removes the per-expert tensors that are present and stacks them under feed_forward.switch_mlp.

Test. Tests/MLXLMTests/JambaSanitizeExpertsTests.swift, loaderStacksPerExpertWeights, copied from jambaLoaderStacksPerExpertWeights of PR #185 without its withKnownIssue. It splits the stacked experts of a tiny model (seed 5) into one tensor per expert, writes a .safetensors file, loads it into a second model (seed 6) through loadWeights (sanitize, then a strict update), and requires the same logits (difference 0). This proves a per-expert checkpoint loads into the right module path.

The test file has private copies of a few helpers from the kernel test support of PR #183 (Tests/MLXLMTests/Kernel/Support/SyntheticModel.swift): configuration decoding, seeded random weights, token rows, the logits call. They are private to the file, so they do not collide with #183 or with the other fix PRs. The per-expert split is written inline.

Merge note. After this PR and #185 both merge, remove the withKnownIssue wrapper (keep its body) in jambaLoaderStacksPerExpertWeights, Tests/MLXLMTests/Kernel/LLM/TextModelForwardPassTests.swift@cb1406a8:486-501. The pull request that merges second (this one or #185) must remove the named withKnownIssue wrapper in the same change. A known issue that is not intermittent and no longer fails is reported as a failure.

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 ran swift format on the changed Swift files only.)
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the necessary documentation (if needed) (fork.yaml)

🤖 Generated with Claude Code

davidtai and others added 2 commits September 30, 2026 18:38
sanitize(weights:) looked for block_sparse_moe.experts.N.w1 and wrote
block_sparse_moe.switch_mlp, but the module path is feed_forward, so a
checkpoint with one tensor per expert did not load. It now stacks
feed_forward.experts.N.<proj>.<name> into feed_forward.switch_mlp, as
mlx-lm jamba.py does.

Fixes #200

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Gajesh2007
Gajesh2007 merged commit 8adbd26 into main Oct 2, 2026
2 checks passed
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.

Jamba: sanitize writes stacked experts to block_sparse_moe, but the module path is feed_forward

2 participants