Repository navigation
fix(rotation): validate the calibration data before rotating the model (#4935) - #4977
Merged
Merged
Conversation
Qubitium
force-pushed
the
root/rotation-validate-first-4935
branch
from
November 17, 2026 20:41
275e6a5 to
d772c79
Compare
#4935) quantize() untied the embeddings, fused the layer norms and rotated the weights before it validated the calibration input. Every calibration-data error (dataset None, an empty dataset, every row at/under min_length, an unsupported row type) was therefore raised on an already-rotated model, and the online Hadamard a rotated down_proj needs is only installed at the end of a successful run. A retry on the same object fused and rotated it a second time and silently quantized from that state (#4935). Prepare the calibration input once, before fuse_layer_norms/rotate_model, with the same checks LoopProcessor.__init__ applies, and hand those batches to the processors through _already_prepared_calibration_dataset so the corpus is tokenized once instead of twice. QuantizationRunPlan.build and the checkpoint gate move up with it, so an unsupported chain cannot leave a rotated model either. Weight-only configs and rotation=None are untouched; a successful run produces a bit-identical checkpoint. tests/test_rotation_validate_first_4935.py pins the contract: after each failure mode the embedding, a decoder weight and the logits are bitwise unchanged, and a failed call followed by a valid retry matches a first-time run byte for byte.
Qubitium
force-pushed
the
root/rotation-validate-first-4935
branch
from
November 17, 2026 20:49
d772c79 to
605515c
Compare
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.
TL;DR
quantize()withrotation="hadamard"fused the layer norms and rotated theweights before it validated the calibration data, so every calibration-data
error was raised on an already-mutated model and a retry rotated it a second
time (#4935). The calibration input is now prepared and validated before
fuse_layer_norms/rotate_model, and the exact batches are reused by theprocessors. Net: +132/-24 in
gptqmodel/models/base.py, one new 8-testregression file, no API change, successful-run checkpoint bit-identical.
1. The problem
Old order on
main(c9c2d7e / 1addb9c):flowchart TD A["quantize(calibration)"] --> B["fuse_layer_norms (base.py:2533)"] B --> C["rotate_model (base.py:2546)"] C --> D["QuantizationRunPlan.build (base.py:2552)<br/>+ checkpoint gate"] D --> E{"calibration.dataset is None"} E -->|"yes"| X["ValueError (base.py:2604)<br/>model already fused and rotated"] E -->|"no"| F["LoopProcessor.__init__<br/>empty checks (loop_processor.py:562, :591)"] F -->|"empty / all rows dropped / bad row"| Y["ValueError<br/>model already fused and rotated"] F -->|"ok"| G["prepare_dataset per processor<br/>then quantize"] G --> H["_setup_rotation_online_had<br/>only on success"] style X fill:#fde2e1,stroke:#dc2626 style Y fill:#fde2e1,stroke:#dc2626mainbase.py:2533(fuse_layer_norms),base.py:2546(rotate_model)dataset is Nonecheckbase.py:2604, after the mutationmin_lengthchecksloop_processor.py:562,loop_processor.py:591, after the mutationbase.py:2552, after the mutation_setup_rotation_online_had, success path onlyConsequence (measured, see §8): after a failed call the embedding is rotated but
the
down_projonline Hadamard is absent; a retry fuses/rotates again andquantizes 16/40 state tensors differently from a first-time run, with
down_proj.qweight100% different andembed_tokens.weight98.4% different.2. The new flow
flowchart TD A["quantize(calibration)"] --> B["validate_rotation_format /<br/>validate_rotation_model_support /<br/>validate_rotation_dimensions (base.py:2504)"] B --> R["QuantizationRunPlan.build (base.py:2579)<br/>+ checkpoint gate"] R -->|"unsupported chain"| E1["NotImplementedError<br/>no weights modified"] R --> P["_prepare_calibration_before_rotation (base.py:2603)"] P -->|"dataset is None"| E2["ValueError (base.py:2994)"] P -->|"empty input / bad row / all rows dropped"| E3["ValueError (base.py:3001, :3013)"] P -->|"prepared batches"| F["fuse_layer_norms (base.py:2627)"] F --> G["rotate_model (base.py:2640)"] G --> H["_quantize_with_calibration (base.py:3021)<br/>binds _already_prepared_calibration_dataset (base.py:863)"] H --> I["_setup_rotation_online_had"] style E1 fill:#fde2e1;stroke:#dc2626 style E2 fill:#fde2e1;stroke:#dc2626 style E3 fill:#fde2e1;stroke:#dc2626_prepare_calibration_before_rotationapplies exactly the checksLoopProcessor.__init__applies —dataset is None(only for a calibratedrun plan), an empty sized input, and an empty prepared dataset after the
min_lengthfilter — plus the model's ownprepare_dataset, so unsupportedrows also fail before the mutation. It returns the prepared batches;
_quantize_with_calibrationpasses them on and binds_already_prepared_calibration_datasetso the processors reuse them instead oftokenizing the corpus twice (same one-preparation precedent as #4307).
Non-rotation runs keep
self.prepare_datasetand weight-only run plans(
run_plan.weight_only, e.g. RTN) prepare no calibration rows.3. Change inventory
gptqmodel/models/base.py_already_prepared_calibration_datasetno-op preparertests/test_rotation_validate_first_4935.pydocs/rotation-model-support.md.github/workflows/awq_cpu.ymlunitsuite4. Failure contract
rotation+CalibrationConfig()(dataset=None)ValueErrorValueErrorbefore mutationrotation+dataset=[]ValueErrorValueErrorbefore mutationmin_lengthValueErrorValueErrorbefore mutationValueErrorValueErrorbefore mutationNotImplementedErrorNotImplementedErrorbefore mutationrotation=None/ weight-only configsError messages are unchanged, so callers that match on them keep working.
5. Tests / guarantees
tests/test_rotation_validate_first_4935.py(8 tests, CPU, one-layer fp32Llama,
hidden_size=32,intermediate_size=64,GPTQConfig(bits=4, group_size=32, desc_act=False, rotation="hadamard")):test_failed_calibration_leaves_the_model_bitwise_unchanged[missing_dataset]layers[0].mlp.down_proj.weight, logits bitwise equal...[empty_dataset]...[all_rows_too_short]...[unsupported_row_type]test_retry_after_a_failed_call_matches_a_first_time_run[...](x4)state_dictbytes == first-time valid runRed on unmodified
origin/main(c9c2d7e), test file copied into/root/worktrees/root-4935-base:Green with the fix:
8 passed in 13.09s.6. Measured before/after
Same model,
dataset=None, fp32,CUDA_VISIBLE_DEVICES="":main0.03910.000016/40state tensors differ0/40model.layers.0.mlp.down_proj.qweight100%of elements differmodel.embed_tokens.weight98.4%of elements differHappy path is bit-identical:
torch.save(state_dict())of a successfulrotation="hadamard"run has sha25688bedb0c…c5273on bothmainand thisbranch;
rotation=Nonehas sha2563ed7af52…4838don both.7. Validation
CI: AWQ CPU (unit/dense/moe), ruff-check and CodeQL all pass; the new file is
in the AWQ CPU
unitlist so the regression runs on everygptqmodel/models/**change.
8. Model Support / Module Plan
Not applicable: no model definition,
MODEL_MAPentry, module plan or fusionconfig changed.
9. Logic Flow Documentation
docs/rotation-model-support.md(already linked fromREADME.md) now carriesthe new-flow Mermaid diagram, the calibration-validation section, the
before/after table above, and four new rows in its failure contract.
10. Reviewer notes
quantize()still tokenizes a rotation run's corpus exactly once; theprepared batches are shared with the native/EoRA processors.
the prepared row count for rotation runs (raw count for every other path),
because preparation happens first. Warning only.
quantize()again on an object that was alreadysuccessfully quantized (a lifecycle question, not a refused-run one).
Closes #4935.