Repository navigation
fix(embed): refuse fresh endpoint quantize before calibration (#5111) - #5168
Merged
Merged
Conversation
Qubitium
force-pushed
the
fix/5111-embed-quant-fresh-quantize
branch
from
November 18, 2026 07:26
0f77351 to
cf575b8
Compare
Qubitium
marked this pull request as ready for review
November 18, 2026 07:27
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.
Summary
Fixes #5111.
quantize(embed_quant_config=...)/embed_quant_mode=...is anendpoint-requantize flow, but
quantize()accepted it on an unquantizedLazyTurtle model and then either crashed at input capture or spent a full run
before failing to re-tie a tied
lm_head. Both unsupported fresh paths are nowrefused before calibration, with the model left dense and
model.quantized=False.requantize()on a loaded quantized checkpoint(including a tied one) is unchanged.
Net: one runtime file (
gptqmodel/models/base.py, +42/−5), one regressiontest file, and a mechanism page plus its links. No checkpoint format, kernel,
or requantize path changes.
Root cause
quantize()only refused a model that was already quantized(
gptqmodel/models/base.py:2415ate2f9676c6), and the tied-head guard inModuleLoopercovered onlyquantize_config.lm_head(
gptqmodel/looper/module_looper.py:2056-2062), neverembed_quant_config.The endpoint flow assumes a loaded quantized endpoint; a fresh quantize is a
LazyTurtle shell.
Removed / replaced mechanism (before, at
e2f9676c6)INPUT/BOTHStageInputsCapture.cache_inputsskipped materializing the input embedding forINPUT/BOTH(correct for requantize, wrong for a fresh shell). The capture forward ran the embedding onmeta; transformers'create_causal_maskcalled.item()on it.gptqmodel/looper/stage_inputs_capture.py:410-414;gptqmodel/models/base.py:3555(run_input_capture)OUTPUT/BOTH, tied headembed_quant_config; every decoder layer was quantized, the tiedlm_headbecameTorchLinear, and only the final restore failed while re-tying.gptqmodel/looper/module_looper.py:2056-2062(lm_head-only check);gptqmodel/utils/offload.py:255-264Measured on GPU 1 with
Qwen/Qwen2.5-0.5B-Instruct(tied), bf16, 4 rows:New flow
flowchart TD A["BaseQModel.quantize(calibration, *, embed_quant_config / embed_quant_mode)"] --> N["_normalize_embed_quant_config"] N --> Q{"self.quantized?"} Q -- "yes: requantize on a loaded checkpoint" --> R["existing endpoint requantize flow"] Q -- "no: fresh quantize" --> T{"mode is OUTPUT or BOTH<br/>and lm_head is tied?"} T -- "yes" --> X2["NotImplementedError<br/>tied output head"] T -- "no" --> I{"mode is INPUT or BOTH?"} I -- "yes" --> X1["NotImplementedError<br/>input endpoint is requantize-only"] I -- "no: OUTPUT, untied" --> OK["capture, pack, finalize"] style X1 fill:#ffe8e8,stroke:#cc3333 style X2 fill:#ffe8e8,stroke:#cc3333 style OK fill:#d7f0d7,stroke:#2f7d32The guards are at
gptqmodel/models/base.py:2386-2418, beforefuse, backendselection, dataset preparation, rotation, or any looper work.
OUTPUTon anuntied head keeps its existing working path.
Failure paths
stateDiagram-v2 state "fresh model, dense" as Dense state "refused before calibration" as Refused state "quantizing every layer" as Running state "half-quantized, quantized=True" as Half state "requantize on loaded checkpoint" as Requant Dense --> Refused: INPUT or BOTH Dense --> Refused: OUTPUT or BOTH with a tied head Dense --> Running: OUTPUT with an untied head Running --> Half: tied re-tie fails (old behavior) Refused --> Dense: model untouched Requant --> Requant: allowedWhy refuse rather than materialize the capture embedding
Materializing the input embedding for capture was implemented and measured
first. It fixed the meta
.item()crash, but the packed endpoint stores FP16scales and emits FP16 while the dense decoder remains BF16, so the completed
model could not forward:
The endpoint-only result leaves a dense decoder behind a packed endpoint;
making that consistent needs an endpoint output-dtype contract, not a one-line
materialization. The flow is therefore refused up front instead of leaving a
non-forwardable in-session model.
Changes
gptqmodel/models/base.pyembed_quant_configbefore any mutation and refuse freshINPUT/BOTHand tiedOUTPUT/BOTH(lines 2382-2418).OUTPUTon an untied head and allrequantize()calls are unchanged.tests/test_fresh_embed_quant_5111.pyINPUT/BOTHand tiedOUTPUT/BOTH(no quantized module,quantized=False, capture never called); untiedOUTPUTcontrol; tied-checkpointrequantize(BOTH)control.docs/embed-endpoint-quantize.mddocs/quantization-features.md,docs/calibration-config.md,docs/README.md,README.mdValidation
Environment:
/root/venv/bin/python(torch 2.15.0+cu132, transformers 5.22.1),CUDA_VISIBLE_DEVICES=1,PYTHONPATH=/root/work/issue-5111,HF_HUB_OFFLINE=1.pytest tests/test_fresh_embed_quant_5111.py -q -rapytest tests/test_fresh_embed_quant_5111.py tests/test_embed_quant_api.py tests/test_requant_embed_lm_head.py tests/test_weight_only_looper.py tests/test_calibration_config.py tests/test_checkpoint_method_compatibility.py tests/test_prepare_dataset.py -q -racheckpoint_recoverymarker)pytest tests/test_stage_inputs_capture.py tests/test_stage_modules.py -q -rapyteston the 18 otherstage_inputs_capture/run_input_capturefilestest_locateanything_prepare_dataset_uses_remote_processor, reproduced one2f9676c6)uvx [email protected] check gptqmodel docs/eora tests setup.pyRed evidence: on
e2f9676c6the new file gives 4 failed, 2 passed — therefusal tests reach calibration (or finish the tied run) instead of refusing,
while the two controls pass.
format/check.shcannot run itspip installstep in this externally-managed environment; the same pinned ruff version was
run directly.
Reviewer notes
INPUT/BOTHrefusal is a deliberate behavior change: that flownever completed successfully, and materializing it alone produced a
non-forwardable model (evidence above).
OUTPUTon an untied fresh model remains supported and is covered by apassing forward control.
/monster/data/model/Qwen2.5-0.5B-Instruct-gptq-4bitend-to-end requantize accuracy test (
tests/test_requant_embed_lm_head_e2e.py)is unavailable on this host; the tied-checkpoint
requantize(BOTH)regressioncovers the same mechanism on a tiny model.
tests/test_locateanything_support.py::test_locateanything_prepare_dataset_uses_remote_processorerrors on the unmodified base revision too; unrelated to this change.