Skip to content

fix(writer): warn when a defused dense gate/up MLP cannot load in HF (#6166) - #6233

Closed
Qubitium wants to merge 1 commit into
mainfrom
fix/6166-issue
Closed

Qubitium wants to merge 1 commit into
mainfrom
fix/6166-issue

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #6166

What changed

save() now warns when a GPTQ/AWQ checkpoint contains a dense MLP that Defuser split from the HF fused mlp.gate_up_proj into mlp.gate_proj + mlp.up_proj. The warning names the exact checkpoint keys, says Transformers reports the split keys as UNEXPECTED and the fused key as MISSING, and points at GPTQModel.load.

Detection is generic and comes from Defuser's own conversion state, not a model list:

  • _defuser_split_gate_up_runtime (set by defuser.modeling.runtime_defusion.patch_split_gate_up_mlp()), and
  • Defuser's _STANDARD_SPLIT_GATE_UP_CLASSES registry for sources that were already defused before loading.

The fused/shard suffixes are read from the existing gptqmodel.utils.model.FUSED_PROJECTION_UNITS mapping.

Why this is the warning path, not re-fusion

contracts/model/refusion.md R2 forbids merging quantized kernel modules back into a fused container ("Quantized kernel modules ... are never merged back into a packed container"). Re-fusing gate_proj.qweight/up_proj.qweight into gate_up_proj.qweight would also require an inverse split in GPTQModel.load. The issue explicitly accepts the save-time warning as the minimum fix, and docs/hf-interop.md already documents structural rewrites as a plain-HF hazard reported by hf_compat_warnings().

Reproduction at HEAD

Tiny 1-layer Phi-3 (fused source model.layers.0.mlp.gate_up_proj.weight), CPU GPTQ 4-bit g32:

CUDA_VISIBLE_DEVICES= PYTHONPATH=/root/openai-audit/issue-6166 \
  /root/venv/bin/python /tmp/6166_repro/repro.py

Saved keys before the fix:

model.layers.0.mlp.gate_proj.qweight
model.layers.0.mlp.up_proj.qweight
...
# no model.layers.0.mlp.gate_up_proj.*

AutoModelForCausalLM.from_pretrained(out) loads the config but forward fails on the uninitialized fused projection:

File ".../transformers/models/phi3/modeling_phi3.py", line 58, in forward
    up_states = self.gate_up_proj(hidden_states)
...
IndexError: index 870388450 is out of bounds for dimension 0 with size 1

Tests

New nodes (extend the existing checkpoint-compat/defused-writer family):

  • tests/test_checkpoint_compat.py::test_hf_compat_warning_for_defused_dense_gate_up_module
  • tests/test_checkpoint_compat.py::test_defused_dense_gate_up_checkpoint_warns_and_keeps_split_keys

The second test builds a real tiny Phi-3, quantizes and saves it on CPU, asserts the checkpoint still carries the defused gate_proj/up_proj keys and no gate_up_proj, then asserts the warning names model.layers.0.mlp.gate_up_proj, gate_proj.*, and up_proj.*.

Red

Reverted only gptqmodel/models/writer.py (tests kept):

cd /root/openai-audit/issue-6166/tests
CUDA_VISIBLE_DEVICES= PYTHONPATH=/root/openai-audit/issue-6166:/root/openai-audit/issue-6166/tests \
  /root/venv/bin/python -m pytest \
  test_checkpoint_compat.py::test_hf_compat_warning_for_defused_dense_gate_up_module \
  test_checkpoint_compat.py::test_defused_dense_gate_up_checkpoint_warns_and_keeps_split_keys -q -rs

Result: 2 failed; both fail at assert len(warnings) == 1 with assert 0 == 1 (no warning before the fix). The end-to-end test already proves the split keys were written.

Green

cd /root/openai-audit/issue-6166/tests
CUDA_VISIBLE_DEVICES= PYTHONPATH=/root/openai-audit/issue-6166:/root/openai-audit/issue-6166/tests \
  /root/venv/bin/python -m pytest \
  test_checkpoint_compat.py::test_hf_compat_warning_for_defused_dense_gate_up_module \
  test_checkpoint_compat.py::test_defused_dense_gate_up_checkpoint_warns_and_keeps_split_keys -q -rs
# 2 passed

Affected family:

cd /root/openai-audit/issue-6166/tests
CUDA_VISIBLE_DEVICES= PYTHONPATH=/root/openai-audit/issue-6166:/root/openai-audit/issue-6166/tests \
  /root/venv/bin/python -m pytest test_checkpoint_compat.py -q -rs
# 24 passed, 3 skipped

Adjacent interop family:

CUDA_VISIBLE_DEVICES= PYTHONPATH=/root/openai-audit/issue-6166:/root/openai-audit/issue-6166/tests \
  /root/venv/bin/python -m pytest test_checkpoint_interop_4053.py -q -rs
# 19 passed, 6 skipped

Lint:

/root/venv/bin/ruff check gptqmodel/models/writer.py tests/test_checkpoint_compat.py
# All checks passed!

Skips

  • test_checkpoint_compat.py: 3 lm_head quantization tests need an accelerator.
  • test_checkpoint_interop_4053.py: 6 Marlin/ExLlamaV2/GPTQ-Triton tests need CUDA.

Unverified

  • Real microsoft/Phi-3.5-mini-instruct, Phi-4, GLM/GLM-4 and zai-org/GLM-OCR checkpoints on GPU. The fix is exercised on a real tiny Phi-3 port on CPU; GLM-OCR's multimodal calibration path was not run.
  • The class-registry fallback covers sources already defused before loading; the marker path is the one exercised by the tiny Phi-3 end-to-end test.

Overlap note

gptqmodel/models/writer.py is also touched by #6182 (block_name_to_quantize) and #6190 (AWQ exclusions). This branch is rebased onto current main (2d3261d) and adds a separate helper block before _has_per_expert_quantized_modules() plus a separate warning block; it does not rewrite block-name or modules-in-block emission.

Phi-3/Phi-3.5/Phi-4, GLM/GLM-4, GLM-OCR and Zamba2 definitions expose one fused mlp.gate_up_proj in their HF module, but Defuser splits it into gate_proj/up_proj before quantization. The writer then serializes the split tensors, so AutoModelForCausalLM.from_pretrained reports them UNEXPECTED, leaves gate_up_proj MISSING and random-initializes it without raising.

Re-fusing quantized kernel modules into a fused container is forbidden by contracts/model/refusion.md R2, so report the hazard through hf_compat_warnings() instead. Detection comes from Defuser's own split marker/class registry and the existing FUSED_PROJECTION_UNITS mapping, not a per-model list.

Fixes #6166
@Qubitium

Copy link
Copy Markdown
Collaborator Author

Consolidated into #6269, which combines #6230, #6232 and #6233 on one branch because all three insert a helper into the same gptqmodel/models/writer.py block. All changes and tests from this PR are preserved there. Closing this draft in favor of the consolidated PR.

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