Repository navigation
fix(jamba): stack per-expert weights under feed_forward.switch_mlp - #223
Merged
Merged
Conversation
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>
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.
Proposed changes
Fixes #200
Defect.
Libraries/MLXLLM/Models/Jamba.swift:537-558(main 0871943).sanitize(weights:)looks formodel.layers.0.block_sparse_moe.experts.0.w1.weight(line 537) and writes the stacked tensors tomodel.layers.N.block_sparse_moe.switch_mlp.*(line 557). The module path isfeed_forward(line 373) withswitch_mlpinside it (line 340). A Hugging Face checkpoint withfeed_forward.experts.N.{gate,up,down}_projtensors fails the strict update, and the model does not load.Reference.
mlx_lm/models/jamba.py:355-369(ml-explore/mlx-lm@a9bd8af) stacksmodel.layers.L.feed_forward.experts.E.<proj>.<name>forgate_proj,down_proj,up_projandweight,bias,scales,biasesintomodel.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 underfeed_forward.switch_mlp.Test.
Tests/MLXLMTests/JambaSanitizeExpertsTests.swift,loaderStacksPerExpertWeights, copied fromjambaLoaderStacksPerExpertWeightsof PR #185 without itswithKnownIssue. It splits the stacked experts of a tiny model (seed 5) into one tensor per expert, writes a.safetensorsfile, loads it into a second model (seed 6) throughloadWeights(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 areprivateto 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
withKnownIssuewrapper (keep its body) injambaLoaderStacksPerExpertWeights,Tests/MLXLMTests/Kernel/LLM/TextModelForwardPassTests.swift@cb1406a8:486-501. The pull request that merges second (this one or #185) must remove the namedwithKnownIssuewrapper in the same change. A known issue that is not intermittent and no longer fails is reported as a failure.Checklist
pre-commit run --all-filesto format my code / installed pre-commit prior to committing changes (I ranswift formaton the changed Swift files only.)fork.yaml)🤖 Generated with Claude Code