Repository navigation
fix: ignore .safetensors outside the weight index; guard the RMSNorm shift - #43
Merged
Merged
Conversation
A repo that ships extra .safetensors in a subdirectory — e.g. an MTP head at `optiq/mtp.safetensors`, absent from model.safetensors.index.json — had those tensors merged into the model's weights by loadWeights' recursive sweep. Two failures followed, both traced from SharpAI/SwiftLM#118 with mlx-community/Qwen3.6-27B-OptiQ-4bit. 1. Silent corruption on the text path. Qwen35.sanitize infers from the mere presence of `mtp.` keys that the checkpoint stores zero-centered norms and adds 1 to every RMSNorm weight. The stray head file made that fingerprint fire on a checkpoint whose norms are already standard, so every normalization was wrong. The model loaded without error and generated noise. Confirmed by removing the file (coherent output), restoring it (noise), and removing it again. 2. Hard failure on the vision path. MLXVLM's Qwen35 has no MTP module, and its metadata fast path (format == "mlx") returned early without stripping those weights, so model.update failed with `Unhandled keys ["mtp"]`. Changes: - Load.swift: when model.safetensors.index.json exists, load only the files it lists. The index defines the model; anything else in the tree is an add-on and must not change how the model's own weights are interpreted. Skipped files are logged. MTP add-ons remain loadable when the user opts in via SWIFTLM_MTP_ENABLE, so speculative decoding is unaffected. - MLXLLM/Qwen35: gate the norm shift on the weights actually looking zero-centered. The shift is only meaningful for such checkpoints; applying it to standard norms corrupts them. Sampled norm means — OptiQ 1.96, Qwen3.5-4B 3.19, Qwen3.6-27B MTP head 1.04 — are all standard, and a checkpoint that genuinely needs the shift sits near 0, so the guard cannot suppress a shift that was required. This also covers the case where the add-on is loaded deliberately with MTP enabled. - MLXVLM/Qwen35: strip mtp weights on the metadata fast path too. Verified on Qwen3.6-27B-OptiQ-4bit: text path now answers correctly where it previously emitted noise, with MTP disabled and enabled; the vision path no longer fails on unhandled keys (it now stops on that repo's missing processor_config.json, unrelated). No change for Qwen3.5-4B-4bit or Qwen3.6-35B-A3B-4bit. Co-Authored-By: Claude Opus 5 <[email protected]>
solderzzc
added a commit
to SharpAI/SwiftLM
that referenced
this pull request
Aug 6, 2026
* chore: bump mlx-swift-lm for the non-indexed weights fix (#118) Picks up SharpAI/mlx-swift-lm#43 (e60ccfd), which stops loadWeights from sweeping .safetensors that the weight index does not list, and guards the Qwen35 RMSNorm shift on the weights actually looking zero-centered. Fixes #118. mlx-community/Qwen3.6-27B-OptiQ-4bit previously loaded without error and generated noise, because its out-of-index optiq/mtp.safetensors made sanitize add 1 to every norm weight. Verified against that model on this commit: the text path answers correctly with MTP disabled and enabled, and the vision path no longer fails with `Unhandled keys ["mtp"]`. No change for Qwen3.5-4B-4bit or Qwen3.6-35B-A3B-4bit. Co-Authored-By: Claude Opus 5 <[email protected]> * chore: advance mlx-swift-lm to include the Gemma 4 KV-shared fix Re-points the bump at 717d77f9, which now carries both upstream fixes: - #43: non-indexed .safetensors ignored + RMSNorm shift guard (fixes #118) - #44: Gemma 4 KV-shared layers + quantizable scaled projections (fixes #120) With #44 included, the audio and omni integration jobs pass locally; opencode reaches its npm-install step (already continue-on-error). dflash remains red until #121 is fixed. Co-Authored-By: Claude Opus 5 <[email protected]> --------- Co-authored-by: Claude Opus 5 <[email protected]>
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.
Root cause and fix for SharpAI/SwiftLM#118 —
mlx-community/Qwen3.6-27B-OptiQ-4bitloads without error and generates noise.The mechanism
loadWeightssweeps the model directory recursively, so any.safetensorsin a subdirectory is merged into the model's weights. That repo shipsoptiq/mtp.safetensors, an MTP head that is not listed inmodel.safetensors.index.json. Two failures follow.1 — Silent corruption on the text path.
Qwen35.sanitizefingerprints the checkpoint by the presence ofmtp.keys:and, when true, adds 1 to every RMSNorm weight. The stray head file makes that fire on a checkpoint whose norms are already standard, so every normalization in the model is wrong.
Confirmed by controlled experiment — same binary, same prompt, only the file's presence changing:
optiq/weights=1876 hasMTP=true shiftNorms=true'},-idthlaf�(aren系_1!'weights=1847 hasMTP=false shiftNorms=false'Tokyo'weights=1876 hasMTP=true shiftNorms=true'},-idthlaf�(aren系_1!'1876 − 1847 = 29, exactly the tensor count of
optiq/mtp.safetensors.2 — Hard failure on the vision path.
MLXVLM.Qwen35has no MTP module, and its metadata fast path (format == "mlx") returned early without stripping those weights, somodel.updatefailed withUnhandled keys ["mtp"].Changes
Load.swift— whenmodel.safetensors.index.jsonexists, load only the files it lists. The index defines the model; anything else in the tree is an add-on and must not change how the model's own weights are interpreted. Skipped files are logged. MTP add-ons stay loadable when the user opts in throughSWIFTLM_MTP_ENABLE, so speculative decoding is unaffected.MLXLLM/Qwen35— gate the norm shift on the weights actually looking zero-centered.MLXVLM/Qwen35— stripmtp.*on the metadata fast path too.Why the guard is safe
The
+1is only meaningful for checkpoints storing zero-centered norms. Sampledmodel.norm.weightmeans: OptiQ 1.96, Qwen3.5-4B 3.19, Qwen3.6-27B MTP head 1.04 — all standard, none needing a shift. A checkpoint that genuinely requires one sits near 0, so a< 0.5threshold cannot suppress a shift that was required. It also covers the case where the add-on is loaded deliberately with MTP enabled, which the file filter alone does not.Verification
On
mlx-community/Qwen3.6-27B-OptiQ-4bit:'},-idthlaf�(aren系_1!''Tokyo'SWIFTLM_MTP_ENABLE=1'Tokyo'Unhandled keys ["mtp"]¹ now stops on that repo's missing
processor_config.json— a separate packaging gap, unrelated to this change.No behaviour change for
mlx-community/Qwen3.5-4B-4bitormlx-community/Qwen3.6-35B-A3B-4bit(both still answer correctly). SwiftLM's unit suites pass.Note for reviewers
The norm-shift fingerprint predates this change and I could not recover its history (shallow submodule clone), so I do not know which checkpoint family it was written for. The guard is deliberately conservative — it only prevents the shift where the data shows it must be wrong. If you know the intended target checkpoints, that is worth a second look.
🤖 Generated with Claude Code