Skip to content

fix: ignore .safetensors outside the weight index; guard the RMSNorm shift - #43

Merged
solderzzc merged 1 commit into
mainfrom
fix/non-indexed-safetensors-corrupt-load
Aug 6, 2026
Merged

solderzzc merged 1 commit into
mainfrom
fix/non-indexed-safetensors-corrupt-load

Conversation

@solderzzc

Copy link
Copy Markdown
Member

Root cause and fix for SharpAI/SwiftLM#118 — mlx-community/Qwen3.6-27B-OptiQ-4bit loads without error and generates noise.

The mechanism

loadWeights sweeps the model directory recursively, so any .safetensors in a subdirectory is merged into the model's weights. That repo ships optiq/mtp.safetensors, an MTP head that is not listed in model.safetensors.index.json. Two failures follow.

1 — Silent corruption on the text path. Qwen35.sanitize fingerprints the checkpoint by the presence of mtp. keys:

let hasMTPWeights = weights.keys.contains { $0.contains("mtp.") }
let shouldShiftNormWeights = hasMTPWeights || hasUnsanitizedConv1d

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/ probe output
present weights=1876 hasMTP=true shiftNorms=true '},-idthlaf�(aren系_1!'
moved out weights=1847 hasMTP=false shiftNorms=false 'Tokyo'
restored 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.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 stay loadable when the user opts in through SWIFTLM_MTP_ENABLE, so speculative decoding is unaffected.
  • MLXLLM/Qwen35 — gate the norm shift on the weights actually looking zero-centered.
  • MLXVLM/Qwen35 — strip mtp.* on the metadata fast path too.

Why the guard is safe

The +1 is only meaningful for checkpoints storing zero-centered norms. Sampled model.norm.weight means: 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.5 threshold 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:

case before after
text path '},-idthlaf�(aren系_1!' 'Tokyo'
text path, SWIFTLM_MTP_ENABLE=1 noise 'Tokyo'
vision path Unhandled keys ["mtp"] loads past it¹

¹ 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-4bit or mlx-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

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
solderzzc merged commit e60ccfd into main Aug 6, 2026
@solderzzc
solderzzc deleted the fix/non-indexed-safetensors-corrupt-load branch August 6, 2026 20:38
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]>
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.

1 participant