Skip to content

fix(models): defuse fused-expert MoE shell on re-save (#4589) - #4608

Merged
Qubitium merged 2 commits into
mainfrom
fix/issue-4589-moe-resave-defuse
Nov 17, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/issue-4589-moe-resave-defuse

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4589

TL;DR

GPTQModel.load(quantized_moe).save(out) rebuilt its export shell with
from_config, which keeps Transformers 5's fused 3-D expert parameters. The
shell was never defused, make_quant had no per-expert nn.Linear to 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:#ffebee
Loading

Before #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_freq
would not crash save(); for fused-expert MoE that turned the crash into
successful corruption.

Measured on main (68cc727f0) with a tiny OLMoE (4 experts, 1 layer, 4-bit
g32), quantize → save A → load A → save B:

Checkpoint tensors per-expert quant tensors fused expert tensors
A (quantize) 72 48 0
B (re-save, before) 26 0 gate_up_proj bf16 [4,128,64], down_proj bf16 [4,64,64]

48 expert tensors (qweight/qzeros/scales/g_idx × 12 projections) were
missing and 2 fused placeholders appeared. On this host the placeholders held
uninitialized memory (absmax=nan); the issue's run serialized zeros. Reloading
B 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:#ffebee
Loading

gptqmodel/models/writer.py now mirrors the load path before find_modules:
_meta_fused_expert_params → _convert_model_with_defuser(type(self), ...) →
_materialize_meta_expert_linears. Parking the fused tensors on meta keeps
Defuser 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_slots raises:

ValueError: Refusing to re-save a quantized checkpoint whose export shell has no
module for 48 quantized tensor(s): ... Saving would silently drop those tensors
and write an unusable checkpoint.

Non-quantized orphan buffers (rotary_emb.inv_freq, #4452) stay tolerated, so
dense re-saves are unchanged.

Situation Before After
Fused-expert MoE re-save fused shell, experts dropped defused shell, experts preserved
Quantized key with no shell slot silently dropped ValueError, nothing written
Non-quantized orphan buffer skipped still skipped
Dense quantized re-save unchanged unchanged
EXL3 re-save passthrough plan unchanged

4. Change inventory

File Change
gptqmodel/models/writer.py +77: defuse the export shell before find_modules; _validate_export_shell_quantized_slots + _QUANTIZED_TENSOR_SUFFIXES
tests/test_moe_resave_preserves_experts_4589.py new: tiny OLMoE quantize→save→load→save→reload, bitwise expert comparison; guard test
docs/quantized-moe-resave-defuse.md new: lifecycle diagrams and fail-closed contract
README.md link to the new page

5. Tests / guarantees

tests/test_moe_resave_preserves_experts_4589.py:

  • test_resave_quantized_olmoe_keeps_every_expert — saves a quantized tiny
    OLMoE, reloads, re-saves, then asserts the key sets are equal, no fused
    gate_up_proj/down_proj exists, 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 the
    Defuser conversion to a no-op (simulating the pre-fix shell) and asserts
    save() raises ValueError and writes no safetensors file.

Fail-before (writer hunk stashed): 2 failed.
After: 2 passed.

6. Validation

Command Result
pytest -q tests/test_moe_resave_preserves_experts_4589.py (CUDA venv, CPU) 2 passed
pytest -q tests/test_moe_resave_preserves_experts_4589.py (ROCm venv, CPU) 2 passed
pytest -q on 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.py 30 passed
ruff check gptqmodel docs/eora tests setup.py All checks passed

After the fix, the same A→B round trip gives: B total=72, expert_quant=48,
fused=0, missing keys 0, bitwise_equal=True, nonzero_A=8256, and B
reloads through GPTQModel.load.

Hardware: CPU only (Linux x86_64). No GPU was used; the CUDA/ROCm venvs were
used for CPU isolation.

7. Limitations

  • The end-to-end run uses a tiny OLMoE fixture, not the 3.9 GB OLMoE-1B-7B
    checkpoint from the issue, and not a GPU. It exercises the same fused-expert
    → Defuser → quantized-shell path.
  • The guard is intentionally limited to quantization payload leaf names. A
    general "every checkpoint key must exist in the shell" rule would reject
    valid dense saves with tied weights or legacy non-persistent buffers.
  • The native W4A branch keeps its existing _load_native_w4a_for_save
    completeness check; this guard runs on the standard re-save branch.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant