Repository navigation
fix(quantization): validate embedding quant config before replay (#5124) - #5136
Merged
Merged
Conversation
QuantizeEmbedConfig was a bare dataclass, and _normalize_embed_quant_config returned a passed instance unchanged. A misspelled embed_quant_mode matched none of the looper branches, so the run replayed calibration over every layer, quantized nothing, and returned a normal result dict. embed_quant_mode=None failed later with an unrelated missing-layer error, and embed_only="False" was truthy. Add __post_init__ that coerces embed_quant_mode through QuantizeEmbed (raising ValueError listing the valid values) and validates embed_only with the #4668 _require_bool_value helper. Re-validate a passed instance at both entry points so a mutated config is rejected before dataset preparation, offload, or any layer replay. Adds tests/test_embed_quant_api.py regressions (CPU construction and both entry points, plus a cached-checkpoint CUDA case) and the mechanism page docs/embedding-quantization-config.md linked from README.md.
Qubitium
force-pushed
the
fix/5124-embed-quant-config-validation
branch
from
November 18, 2026 05:45
21e96de to
aafca0a
Compare
Qubitium
marked this pull request as ready for review
November 18, 2026 05:58
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 #5124.
QuantizeEmbedConfigwas a plain@dataclasswith no__post_init__, andBaseQModel._normalize_embed_quant_configreturned a passed instance unchanged(
gptqmodel/models/base.py:2884-2901ate2f9676c6). The loopers then comparethe stored mode against enum members (
in (QuantizeEmbed.INPUT, ...)) and testembed_onlyfor truthiness. A value that matched no branch therefore started afull calibration replay, quantized nothing, and returned a normal result dict
with no warning.
This PR validates the config where it is constructed and again where it enters
quantize()/requantize(), so a bad mode or a non-boolembed_onlyfailsbefore dataset preparation, offload, or any layer replay.
Net diff: +96/−2 in
gptqmodel/(one validator, one__post_init__, onere-validation call), plus tests and the mechanism page. No API removal.
Root cause and reproduced behaviour
Cached
Qwen/Qwen2.5-0.5B-Instruct-GPTQ-Int4, CUDA, four short calibrationrows, pre-fix
e2f9676c6:requantize(..., QuantizeEmbedConfig(embed_quant_mode="inptu")){"gptq": [...]}in 3.6 s;embed_tokensstayedEmbedding; nothing quantizedrequantize(..., QuantizeEmbedConfig(embed_quant_mode="input"))embed_tokensbecameTorchQuantEmbeddingsrequantize(..., QuantizeEmbedConfig(embed_quant_mode=None))ValueError: layer module item 'self_attn.q_proj' not found in modelafter 0.9 sQuantizeEmbedConfig(embed_only="False")stage_layer.py:1051(embed_only is not False) andweight_only_looper.py:1083(bool(...) and self.embed_only) treated it asTrueThe removed decision mechanism, with
file:lineevidence:gptqmodel/looper/module_looper.py:2023-2029is not Noneenters the embedding block, but a typo builds norequested_endpointsgptqmodel/looper/module_looper.py:2168-2169,2222in (...)is False, whilereplay_all_layers = embed_quant_mode is not Noneis Truegptqmodel/looper/stage_layer.py:575-576gptqmodel/looper/weight_only_looper.py:842-848gptqmodel/looper/stage_layer.py:1051embed_only is not Falseaccepts a truthy non-boolgptqmodel/looper/weight_only_looper.py:1083bool(embedding_targets) and self.embed_onlyaccepts a truthy non-boolWhat Changed
gptqmodel/quantization/config.py: add_normalize_embed_quant_mode(), whichcoerces through
QuantizeEmbed(value)and raisesValueErrornaming thefield and the valid values (
'input','output','both') for anythingelse, including
None. AddQuantizeEmbedConfig.__post_init__using thathelper and the existing
_require_bool_value([BUG] Config validation still silently accepts bad input (#4052 incomplete): 20+ bool fields take "False" as True (mock_quantization="False" runs the mock solver), and typo'd keys in dynamic overrides are ignored and persisted #4668) forembed_only, plusvalidate()for re-validation.gptqmodel/models/base.py:_normalize_embed_quant_configcallsembed_quant_config.validate()instead of returning the instance unchanged.Both
quantize()(base.py:2387) andrequantize()(base.py:2921) callthe normalizer before any calibration or layer work.
tests/test_embed_quant_api.py: construction/coercion/rejection tests, mutatedinstances through both entry points with
quantize()/_quantize_with_calibrationreplaced by failing spies, and a cached-checkpointCUDA case.
docs/embedding-quantization-config.md: mechanism page with the new flow, theremoved flow, and the failure/ambiguity paths; linked from
README.mdanddocs/README.md.The other
QuantizeEmbedConfigfield isembed_only; both fields are covered.No neighbouring
teacher/dtype/calibration fields exist on this dataclass, sonothing else was in scope.
Logic Flow Documentation
file:lineevidence.docs/embedding-quantization-config.mdrepeats the diagrams, is titled forthe mechanism, and is linked from
README.md.New flow
flowchart TD A["quantize(embed_quant_config=...)<br/>requantize(embed_quant_config=...)"] --> B["_normalize_embed_quant_config<br/>base.py:2884"] B --> C{"both embed_quant_config<br/>and embed_quant_mode set"} C -- "yes" --> X1["ValueError: pass only one"] C -- "no" --> D{"embed_quant_config type"} D -- "QuantizeEmbed member" --> E["QuantizeEmbedConfig(embed_quant_mode=member)"] D -- "QuantizeEmbedConfig" --> F["embed_quant_config.validate()"] D -- "other" --> X2["TypeError"] D -- "None with embed_quant_mode" --> G{"embed_quant_mode is QuantizeEmbed"} G -- "no" --> X3["TypeError"] G -- "yes" --> E E --> H["__post_init__<br/>config.py:269"] F --> H H --> I{"QuantizeEmbed(embed_quant_mode)"} I -- "unknown or None" --> X4["ValueError: mode must be<br/>input, output or both"] I -- "known" --> J{"type(embed_only) is bool"} J -- "no" --> X5["TypeError: embed_only<br/>must be a bool"] J -- "yes" --> K["validated QuantizeEmbedConfig"] K --> L["ModuleLooper / WeightOnlyLooper<br/>endpoint selection and replay"] L --> M["replay_all_layers = mode is not None<br/>module_looper.py:2222"] L --> N["mode in INPUT, BOTH / OUTPUT, BOTH<br/>module_looper.py:2168, stage_layer.py:575,<br/>weight_only_looper.py:842"] style X1 fill:#ffe8e8,stroke:#cc3333 style X2 fill:#ffe8e8,stroke:#cc3333 style X3 fill:#ffe8e8,stroke:#cc3333 style X4 fill:#ffe8e8,stroke:#cc3333 style X5 fill:#ffe8e8,stroke:#cc3333Removed mechanism
flowchart TD A["QuantizeEmbedConfig(embed_quant_mode=value)<br/>plain dataclass, no validation"] --> B["_normalize_embed_quant_config<br/>base.py:2884-2901 returns instance unchanged"] B --> C["ModuleLooper.embed_quant_mode = value<br/>module_looper.py:254"] C --> D{"value in INPUT, BOTH<br/>or OUTPUT, BOTH"} D -- "known" --> E["quantize the requested endpoint"] D -- "typo, whitespace, Both" --> F["requested_endpoints stays empty<br/>module_looper.py:2023-2029"] F --> G["replay_all_layers = value is not None<br/>module_looper.py:2222"] G --> H["stage_layer.py:575-576 both False<br/>weight_only_looper.py:842-848 both False"] H --> I["replay every layer, quantize nothing,<br/>return a normal result dict"] C --> J{"value is None"} J -- "yes" --> K["embedding block skipped<br/>is not None guards"] K --> L["planner fails later with<br/>layer module item self_attn.q_proj not found"] C --> M["embed_only is not False<br/>stage_layer.py:1051"] M --> N["embed_only truthiness<br/>weight_only_looper.py:1083"] style I fill:#ffe8e8,stroke:#cc3333 style L fill:#ffe8e8,stroke:#cc3333 style N fill:#ffe8e8,stroke:#cc3333Failure and ambiguity paths
stateDiagram-v2 [*] --> Constructed Constructed --> Validated: QuantizeEmbedConfig.__post_init__ Validated --> Rejected: unknown mode or non-bool embed_only Validated --> Normalized: quantize() / requantize() Normalized --> Rejected: validate() sees a mutated field Normalized --> Replaying: looper selects endpoints Replaying --> [*] Rejected --> [*] state "Rejected (ValueError / TypeError before any layer work)" as RejectedTests
Red/green evidence (same test file; source reverted with
git stashfor the redrun):
Post-fix behaviour on the same cached checkpoint:
Skip / limitation:
tests/test_requant_embed_lm_head_e2e.pywas not used asevidence. It requires
/monster/data/model/Qwen2.5-0.5B-Instruct-gptq-4bitanddownloads
allenai/ai2_arc; underHF_HUB_OFFLINE=1it fails indatasetswith
ConnectionError, before touching embedding config validation.Model Support / Module Plan
Not applicable: no model, alias, module plan, fusion or AWQ change.
Review Requirements
_require_bool_value, the[BUG] Config validation still silently accepts bad input (#4052 incomplete): 20+ bool fields take "False" as True (mock_quantization="False" runs the mock solver), and typo'd keys in dynamic overrides are ignored and persisted #4668 error format,
CalibrationConfig.__post_init__style).quantize()/_quantize_with_calibrationwith failing spies.TorchQuantEmbeddingson the real checkpoint.Kernel Accuracy Requirements
Not applicable: no kernel or numerical change.
Notes
validate()re-runs__post_init__, so a caller that mutates a config afterconstruction is rejected at the entry point rather than silently ignored.
embed_quant_mode=keyword still requires aQuantizeEmbedmember; a bare string there keeps raisingTypeError(useQuantizeEmbedConfig(embed_quant_mode="input")).QuantizeEmbedConfigis not serialized into checkpoints, so nomigration is needed for existing files.