Repository navigation
Description
Summary
mlx-community/Qwen3.6-27B-OptiQ-4bit loads without error but generates incoherent tokens. This is independent of the thinking flag, so it is not #108.
Reproduction
.build/release/SwiftLM \
--model ~/.cache/huggingface/hub/models--mlx-community--Qwen3.6-27B-OptiQ-4bit/snapshots/<hash>/ \
--port 5430 --thinking --gpu-layers auto --prefill-size 64 --ctx-size 8192Prompt: Name the capital of Japan. One word.
--thinking: " (' tml_htmlnsub\x00\n\n#\n\n# &г.� c#厉 code"
chat_template_kwargs {enable_thinking:false}: "},-idthlaf�(aren系_1!"
The server starts normally, reports FULL GPU, logs no warnings, and returns HTTP 200. Only the tokens are wrong.
Isolation
The same binary on mlx-community/Qwen3.5-4B-4bit produces coherent output, so this is specific to the model rather than the build or environment.
What is different about this model
config.json describes mixed-precision quantization — 498 per-layer entries alongside the global defaults:
"quantization": {
"group_size": 64, "bits": 4, "mode": "affine",
"language_model.model.embed_tokens": { "bits": 8, "group_size": 64 },
"language_model.model.layers.0.linear_attn.in_proj_qkv": { "bits": 8, "group_size": 64 },
...
}optiq_metadata.json:
{ "method": "optiq_mixed_precision", "base_model": "Qwen/Qwen3.6-27B",
"reference": "uniform_4bit", "target_bpw": 4.5, "achieved_bpw": 4.7686,
"n_high_bits": 220, "n_low_bits": 276 }Two properties that may matter, neither confirmed as the cause:
- Mixed precision.
mlx-swift-lmdoes implement per-layer quantization (Libraries/MLXLMCommon/Load.swift:48-70), includinglanguage_model.-prefixed key fallbacks, so this ought to be handled. Whether every key in this config resolves against the actual module paths has not been checked. A silently unmatched key would fall back to the 4-bit default and dequantize the 8-bit layers wrongly — which would look exactly like this. - Architecture.
model_typeisqwen3_5and the module names includelinear_attn.in_proj_qkv/in_proj_z/in_proj_a/in_proj_b, i.e. linear-attention blocks rather than standard attention.
The repo also ships an optiq/ subdirectory (mtp.safetensors, optiq_vision.safetensors) that is outside model.safetensors.index.json. Whether those are required at inference has not been established.
Not root-caused
I did not identify the cause and did not attempt a fix. Filing so the reproduction and config analysis are recorded.
Why it matters
This model is the one in #108, and OptiQ builds of other models are published in the same family. If mixed-precision quantization is mishandled generally, the failure is silent — no error, no warning, just wrong tokens.
Environment
- SwiftLM
d5a9d11(main), release build - macOS 26.5.2, Apple M5 Pro, 68 GB
mlx-swift133864c,mlx-swift-lmb9bf50b
Activity
solderzzc commentedon Aug 6, 2026
Investigation notes. I did not reach a confirmed root cause — recording what is ruled out and the one asymmetry worth a closer look, so nobody repeats this ground.
Ruled out: extra safetensors colliding with model weights.
loadWeightsenumerates the model directory recursively (MLXLMCommon/Load.swift:22-34), so this repo'soptiq/mtp.safetensorsandoptiq/optiq_vision.safetensors— neither of which appears inmodel.safetensors.index.json— are loaded alongside the shards into oneweightsdictionary, last-write-wins. But their tensor names are namespaced (mtp.*,vision_tower.*) and do not collide withlanguage_model.*. Verified by reading the safetensors headers over HTTP range requests.Ruled out: per-layer quantization keys failing to match. The config's 498 entries are all
language_model.…-prefixed. Neither sanitize strips that prefix — the LLM one normalizes toward it (MLXLLM/Models/Qwen35.swift:826-830), the VLM one leaves it — andLoad.swift:56-62additionally falls back todict["language_model.\(path)"]. Module paths and config keys line up in both paths, so the 8-bit layers should not be silently dequantized as 4-bit.Ruled out: unmatched keys loading silently.
model.updateruns withverify: .all(.noUnusedKeysonly under expert streaming), so a wholesale key mismatch would throw rather than leave weights uninitialized. The failing run threw nothing.The asymmetry. The
+1shift applied to RMSNorm weights differs between the two implementations:MLXVLM/Models/Qwen35.swift:1264-1265) — applied unconditionally.MLXLLM/Models/Qwen35.swift:737-741, 777-782) — gated on a heuristic:If that heuristic ever misfires, every norm weight is off by one and the model emits noise while loading cleanly — exactly this symptom. For this checkpoint the MTP weights do reach
weights(that recursive enumeration again), sohasMTPWeightsshould be true and the shift should apply, which argues against it being the cause here. It remains fragile: the signal is a file that is not part of the weight index, so anything that changes which files get enumerated changes how norms are scaled.Also worth noting:
isVisioncomes from the--visionflag alone (Sources/SwiftLM/Server.swift:563), not fromvision_configin config.json. A checkpoint with a vision tower therefore loads through the text-only path unless the flag is passed, and the two paths differ as above. @Gwada's successful run of a sibling model used--vision; my failing run did not.One-command test for anyone who has the model (mine is deleted; re-downloading is 20 GB):
If
--visionis coherent and the default is not, the bug is in the text-only path for vision-capable checkpoints and the norm-shift heuristic is the first place to look. If both are garbage, the cause is elsewhere and the above is just a latent fragility.solderzzc commentedon Aug 6, 2026
Root cause found and confirmed by controlled experiment. Correcting my earlier comment: I had ruled the norm-shift heuristic unlikely. It is the cause — I had the direction backwards.
Mechanism
loadWeightsenumerates the model directory recursively (MLXLMCommon/Load.swift:22-34), sooptiq/mtp.safetensorsis loaded even though it is not inmodel.safetensors.index.json. That makeshasMTPWeightstrue inQwen35.sanitize, which enables the+1shift applied to every RMSNorm weight (MLXLLM/Models/Qwen35.swift:737-741, 777-782):This checkpoint's norms are already in final form. Adding 1 to all of them corrupts every normalization in the model — which loads cleanly and generates noise.
The heuristic uses "MTP weights are present" as a proxy for "this checkpoint's norms need shifting". The OptiQ repo breaks the proxy by shipping an MTP file outside the weight index.
Evidence
Same binary (
main), same model, same prompt. Only the presence ofoptiq/differs; the probe line is instrumentation added tosanitize: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 in
optiq/mtp.safetensors. Removing the file fixes generation; restoring it breaks it again.A second bug, same origin
--visiondoes not work around it. That path fails to load at all:MLXVLM/Models/Qwen35.swifthas no MTP module, so the same stray weights that silently corrupt the LLM path hard-fail the VLM path. Both stem from non-indexed.safetensorsbeing loaded as if they were model weights.Fix options
Not submitting a patch, since the right choice depends on intent I do not have:
Load.swift). Fixes both bugs at the source. Risk: MTP weights are loaded deliberately for--mtp(MTPConfig.retainMTPWeights), so this must keep a path for them — presumably an explicit load rather than directory sweep.Option 1 looks correct to me but option 2 is what makes the heuristic robust. Whoever added the shift will know which checkpoints depend on it.
Impact
Any repo shipping extra
.safetensorsoutside the index hits this: silent corruption on the text path, hard failure on the vision path.--stream-expertsis unaffected (it uses.noUnusedKeys), which may be why this went unnoticed.Reproduced on SwiftLM
d5a9d11,mlx-swift-lmb9bf50b, macOS 26.5.2, M5 Pro.solderzzc commentedon Aug 6, 2026
Fix opened: SharpAI/mlx-swift-lm#43 (the code lives in the submodule).
Two changes, because the file filter alone was not sufficient:
Load.swift— whenmodel.safetensors.index.jsonexists, only the files it lists are loaded.optiq/mtp.safetensorsis no longer swept in, so the MTP fingerprint stops misfiring and the VLM path no longer chokes onUnhandled keys ["mtp"]. MTP add-ons remain loadable underSWIFTLM_MTP_ENABLE.Results on
Qwen3.6-27B-OptiQ-4bit:'},-idthlaf�(aren系_1!''Tokyo'SWIFTLM_MTP_ENABLE=1'Tokyo'Unhandled keys ["mtp"]processor_config.json(separate packaging gap)No behaviour change for
Qwen3.5-4B-4bitorQwen3.6-35B-A3B-4bit; SwiftLM's unit suites pass.One caveat carried into the PR: I could not recover the history of the norm-shift fingerprint (shallow submodule clone), so I do not know which checkpoint family it was written for. The guard only blocks the shift where the weight data shows it must be wrong, but anyone who knows the intended target should sanity-check it.
Once #43 merges, SwiftLM needs a submodule bump to pick this up. @coolaj86 — this is very likely what you were hitting; #115 fixes the thinking-tag half separately.
fix: ignore .safetensors outside the weight index; guard the norm shi…
chore: advance mlx-swift-lm to include the Gemma 4 KV-shared fix
solderzzc commentedon Aug 7, 2026
Shipped in b674 — OptiQ mixed-precision models load and generate correctly on the release binary.
test: cover load-path defect shapes with synthetic checkpoints (#143)
chore: bump mlx-swift-lm for the non-indexed weights fix (SharpAI#118) (