Repository navigation
fix(models): defuse fused-expert MoE shell on re-save (#4589) - #4608
Merged
Merged
Conversation
A loaded quantized MoE was re-saved through a shell built with from_config, which keeps the fused 3-D expert parameters. make_quant could not replace them with quantized kernels, the checkpoint's per-expert quantized tensors were treated as orphan keys, and save() published the fused zero shell instead of the experts. Run the same Defuser conversion the quantized-load path uses before make_quant: park the fused tensors on meta, split them into per-expert linears, and materialize empty host storage. Then fail closed if any checkpoint quantization payload still has no module in the rebuilt shell, so a future mismatch raises instead of silently dropping tensors. Add a tiny CPU OLMoE regression that quantizes, saves, reloads, re-saves, and asserts the expert payload is bitwise identical and non-zero, plus a test that the fail-closed guard rejects an un-defused shell. Document the re-save lifecycle and link it from the README.
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.
Fixes #4589
TL;DR
GPTQModel.load(quantized_moe).save(out)rebuilt its export shell withfrom_config, which keeps Transformers 5's fused 3-D expert parameters. Theshell was never defused,
make_quanthad no per-expertnn.Linearto replace,and the checkpoint's per-expert quantized tensors were skipped as orphan keys.
save()then serialized the fused zero/garbage placeholder: silent data loss.The re-save shell now runs the same Defuser conversion as the quantized-load
path, and a fail-closed check refuses to save if any checkpoint quantization
payload still has no module in the rebuilt shell.
1. The problem (old flow)
flowchart TD A[GPTQModel.load quantized MoE] --> B[model.save] B --> C[get_model_with_quantize] C --> D[from_config keeps fused 3-D experts] D --> E[make_quant finds no per-expert nn.Linear] E --> F[load_checkpoint_in_model treats experts.N.* as orphan keys] F --> G[save writes fused placeholder gate_up_proj and down_proj] style F fill:#ffebee style G fill:#ffebeeBefore #4495 the same path crashed loudly (
OlmoeExperts has no attribute 0).#4495 made the restore loop walk the model's buffers so
rotary_emb.inv_freqwould not crash
save(); for fused-expert MoE that turned the crash intosuccessful corruption.
Measured on
main(68cc727f0) with a tiny OLMoE (4experts, 1 layer, 4-bitg32), quantize → save A → load A → save B:
gate_up_projbf16[4,128,64],down_projbf16[4,64,64]48 expert tensors (
qweight/qzeros/scales/g_idx× 12 projections) weremissing and 2 fused placeholders appeared. On this host the placeholders held
uninitialized memory (
absmax=nan); the issue's run serialized zeros. ReloadingB loses every expert.
2. The new flow
flowchart TD A[GPTQModel.load quantized MoE] --> B[model.save] B --> C[get_model_with_quantize] C --> D[from_config keeps fused 3-D experts] D --> E[_meta_fused_expert_params<br/>fused projections -> meta] E --> F[_convert_model_with_defuser<br/>same conversion as the load path] F --> G[_materialize_meta_expert_linears<br/>empty host storage] G --> H[make_quant replaces per-expert nn.Linear] H --> I[load_checkpoint_in_model fills packed weights] I --> J{checkpoint quantized key<br/>has no shell slot?} J -- yes --> K[raise ValueError<br/>no checkpoint written] J -- no --> L[save writes every expert payload] style K fill:#ffebeegptqmodel/models/writer.pynow mirrors the load path beforefind_modules:_meta_fused_expert_params→_convert_model_with_defuser(type(self), ...)→_materialize_meta_expert_linears. Parking the fused tensors onmetakeepsDefuser from copying them to the host (the same optimization as #4400), then
the per-expert shells are materialized and replaced by
make_quant.3. Fail-closed guard
After the checkpoint load, every checkpoint tensor whose leaf name is a
quantization payload (
qweight,qzeros,scales,g_idx, and the rest of_QUANTIZED_TENSOR_SUFFIXES) must resolve to a shell module. Otherwise_validate_export_shell_quantized_slotsraises:Non-quantized orphan buffers (
rotary_emb.inv_freq, #4452) stay tolerated, sodense re-saves are unchanged.
ValueError, nothing written4. Change inventory
gptqmodel/models/writer.pyfind_modules;_validate_export_shell_quantized_slots+_QUANTIZED_TENSOR_SUFFIXEStests/test_moe_resave_preserves_experts_4589.pydocs/quantized-moe-resave-defuse.mdREADME.md5. Tests / guarantees
tests/test_moe_resave_preserves_experts_4589.py:test_resave_quantized_olmoe_keeps_every_expert— saves a quantized tinyOLMoE, reloads, re-saves, then asserts the key sets are equal, no fused
gate_up_proj/down_projexists, all 48 expert tensors are bitwise equal,the expert payload is non-zero, and the re-saved checkpoint reloads.
test_resave_refuses_when_expert_shell_is_not_defused— monkeypatches theDefuser conversion to a no-op (simulating the pre-fix shell) and asserts
save()raisesValueErrorand writes no safetensors file.Fail-before (writer hunk stashed): 2 failed.
After: 2 passed.
6. Validation
pytest -q tests/test_moe_resave_preserves_experts_4589.py(CUDA venv, CPU)pytest -q tests/test_moe_resave_preserves_experts_4589.py(ROCm venv, CPU)pytest -qon the new file +test_export_shell_gemv_resave_4305.py,test_resave_preserves_buffer_dtypes_4303.py,test_resave_preserves_checkpoint_dtypes_4379.py,test_resave_dense_parameter_dtype_4466.py,test_resave_missing_modules_4452.py,test_save_state_dict_buffer_collection.py,test_moe_prune_nf12_save_4349.py,test_tiny_moe_quant_smoke.pyruff check gptqmodel docs/eora tests setup.pyAfter the fix, the same A→B round trip gives: B
total=72,expert_quant=48,fused=0, missing keys0,bitwise_equal=True,nonzero_A=8256, and Breloads through
GPTQModel.load.Hardware: CPU only (Linux x86_64). No GPU was used; the CUDA/ROCm venvs were
used for CPU isolation.
7. Limitations
checkpoint from the issue, and not a GPU. It exercises the same fused-expert
→ Defuser → quantized-shell path.
general "every checkpoint key must exist in the shell" rule would reject
valid dense saves with tied weights or legacy non-persistent buffers.
_load_native_w4a_for_savecompleteness check; this guard runs on the standard re-save branch.