Skip to content

[BUG] Re-saving a loaded quantized MoE silently drops every expert and writes zero-filled fused dense experts (re-save shell never defused; #4495 turned the old crash into silent corruption) #4589

Closed

Description

@Qubitium

Found on main @ 4b65923, RTX 5090 (sm_120), torch 2.15 / transformers 5.22 (reproduced with defuser 0.0.28 and with the required defuser 0.0.31).

What is wrong

Re-saving a loaded quantized MoE checkpoint (GPTQModel.load(qckpt).save(out)) silently writes a corrupt checkpoint for every model whose experts Transformers 5 builds as fused 3-D tensors (OLMoE, PhiMoE and others in the defuser registry):

  • Every per-expert quantized tensor (mlp.experts.N.{gate,up,down}_proj.{qweight,qzeros,scales,g_idx}) is dropped.
  • Dense fused mlp.experts.gate_up_proj / mlp.experts.down_proj BF16 tensors are written in their place, and they are all zeros.
  • quantize_config.json / config.json still say 4-bit GPTQ, and reloading fails.

Cause: get_model_with_quantize (gptqmodel/models/writer.py:2521-2540) builds the export shell with self.loader.from_config(config, ...) and never runs _convert_model_with_defuser (gptqmodel/models/loader.py:987), which the quantize and load paths call (loader.py:2162/2167/2212/2809). The shell therefore keeps the fused OlmoeExperts / PhimoeExperts 3-D parameters. make_quant finds no per-expert nn.Linear to replace. load_checkpoint_in_model ignores the checkpoint's per-expert keys, and the fused parameters keep their zero placeholder, which is then serialized.

Regression from #4495 (crash became silent corruption)

Before #4495 (at 6d849b3c8), the same re-save failed loudly in the key-driven buffer-restore loop:

writer.py:2649 get_model_with_quantize -> utils/model.py:3319 load_checkpoint_in_model_then_tie_weights -> get_submodule
AttributeError: OlmoeExperts has no attribute `0`

#4495 (the fix for #4452) changed that loop to walk the model's own buffers so that "a checkpoint tensor without a module ... no longer crashes". For MoE that means the entire expert payload is now treated as harmless orphan keys, and save() returns successfully.

Repro

import torch
from gptqmodel import GPTQModel
q = GPTQModel.load("OLMoE-1B-7B-0924-GPTQ-4bit-gp128-gp32-dynamic", dtype=torch.float16)   # pre-quantized GPTQ MoE
q.save("resave")
GPTQModel.load("resave")
source checkpoint re-saved checkpoint
tensors 12627 371
size 3.9 GB 13 GB
model.layers.0.mlp.experts.gate_up_proj absent BF16 [64, 2048, 2048], abs max 0.0
model.layers.0.mlp.experts.0.down_proj.qweight present missing (12320 tensors missing in all)
reload ok ValueError: Missing required quantized weights in checkpoint ...: model.layers.0.mlp.experts.0.down_proj.qweight, ...

The same happens for a checkpoint GPTQModel just produced: quantize Phi-tiny-MoE-instruct (RTN 4-bit, trust_remote_code=True resolves to the native PhimoeForCausalLM per #4491), save A (6949 tensors, 2.3 GB), load A, save B. B has 869 tensors and is 6.1 GB, with model.layers.0.mlp.experts.gate_up_proj BF16 [16, 896, 4096] in place of 192 per-expert quantized tensors per layer.

Dense models round-trip byte-identically on the same build (GPTQ v1/v2/asym/desc_act, AWQ, QQQ, FP8, NF12, GGUF, BnB and EXL3 all checked), so this is specific to fused-expert MoE.

Suggested fix

  1. Run the same defuser conversion in get_model_with_quantize that the loader applies, before make_quant, so the shell has per-expert Linear modules that match the checkpoint.
  2. Fail closed: after the export shell is loaded, any checkpoint key that belongs to a quantized module (qweight/qzeros/scales/g_idx/...) and has no slot in the shell, or any floating shell parameter that the checkpoint did not provide, must raise. Do not drop them. The [BUG] Regression from #4379 fix: re-saving a checkpoint that contains a tensor with no matching module crashes (e.g. TheBloke/Llama-2-7B-GPTQ self_attn.rotary_emb.inv_freq) #4452 orphan-key tolerance should stay limited to genuinely obsolete non-quantized buffers such as rotary_emb.inv_freq.

Test that should have caught it

tests/test_resave_missing_modules_4452.py only covers the orphan inv_freq case. No re-save test covers an MoE model. A tiny fused-expert model (OLMoE/Qwen3-MoE config with 2 layers and 4 experts) round-tripped through quantize → save → load → save → load, with a tensor-set equality assertion, would catch both the missing defuse and the silent drop.

Severity

High: silent data loss. save() reports success, the output is about 3.4x larger, and it carries zeros where the experts were. Anyone re-saving a quantized MoE (to change shard size, re-publish, or after requantize) ships an unusable model.

🤖 Generated with Claude Code

Activity

  1. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Claimed. This issue is now owned by a dedicated worker; a fix PR referencing it will follow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions