Skip to content

[BUG] quantize() with rotation='hadamard' rotates the model before validating calibration data: a failed call (dataset None/empty/too short) leaves it rotated and a retry rotates twice (+3.8 nll, silent) #4935

Closed

Description

@Qubitium

Found on main @ 3867c82, CPU (Llama-3.2-1B-Instruct truncated to 2 layers, fp32).

What is wrong

With GPTQConfig(rotation="hadamard"), BaseQModel.quantize() fuses the layer norms and rotates the weights (gptqmodel/models/base.py ~2509-2544, fuse_layer_norms + rotate_model) before it checks the calibration data. Every calibration-data error is raised after the model has been rotated in place:

  • calibration.dataset is None -> ValueError: CalibrationConfig.dataset is required ... (base.py ~2582)
  • dataset=[] -> ValueError: Calibration dataset must not be empty. (raised in LoopProcessor.__init__)
  • every row at or under min_length -> ValueError: Calibration dataset is empty after CalibrationConfig.prepare_dataset removed all samples ...
  • an unsupported row type -> ValueError from prepare_dataset

After any of these errors the in-session model is already broken: its weights are rotated, but the online Hadamard for down_proj is installed only at the end of a successful run (_setup_rotation_online_had). If the user then fixes the data and calls quantize() again on the same object (the normal notebook/service flow), the model is fused and rotated a second time and quantized from that state. The run completes without a warning, but quality is much worse.

#4666/#4701 moved the rotation format/backend validation ahead of the mutation for the same reason ("a refused run left an unusable in-session model"). The calibration checks are still after it.

Repro

import torch
from gptqmodel import GPTQModel
from gptqmodel.quantization.config import GPTQConfig, CalibrationConfig

m = GPTQModel.load("Llama-3.2-1B-Instruct (2 layers)", GPTQConfig(bits=4, group_size=128, rotation="hadamard"), dtype=torch.float32)
ids = m.tokenizer("The capital of France is Paris. The capital of Germany is Berlin. The capital of Italy is", return_tensors="pt").input_ids
emb0 = m.model.get_input_embeddings().weight.detach().clone()
with torch.no_grad(): base = m.model(ids).logits
try:
    m.quantize(CalibrationConfig())          # forgot the dataset (or dataset=[] / too-short rows)
except Exception as e:
    print("first call:", type(e).__name__, e)
print("embedding changed by failed call:", not torch.equal(emb0, m.model.get_input_embeddings().weight))
with torch.no_grad(): print("fp logits maxdiff after failed call:", (m.model(ids).logits - base).abs().max().item())
m.quantize(CalibrationConfig(dataset=c4_texts_32, batch_size=4))   # retry with data
# compare nll vs. a fresh process that calls quantize(...) once

Output:

first call: ValueError CalibrationConfig.dataset is required unless a weight-only quantize config is configured.
embedding changed by failed call: True maxdiff 0.3748
fp logits maxdiff after failed call: 16.58

                       nll (fp base 19.62)
quantize once          19.40
failed call + retry    23.20     <- +3.8 nats, no warning

dataset=[] and dataset=[{"input_ids": [1, 2], "attention_mask": [1, 1]}] give the same result: ValueError, and the embedding has changed.

Suggested fix

Validate the calibration input before any model mutation. At minimum, move the calibration.dataset is None check (and the weight-only branch decision it depends on) above the rotation block. Better: prepare the dataset (or at least run the empty / too-short checks) before fuse_layer_norms/rotate_model. Or record that rotation was applied (for example a flag on the model) and make a later quantize() refuse instead of rotating again.

Test that should have caught it

tests/test_calibration_config.py checks the dataset is None ValueError but not that the model is left untouched. Add a rotation case: after a failed quantize() (None / empty / too-short dataset), assert that the embedding and lm_head weights equal their pre-call values.

Severity

Medium. This is silent accuracy loss on retry after the most common user error. The failed call alone also leaves an unusable in-session model (logits off by 16 in fp).

🤖 Generated with Claude Code

Activity

  1. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim: agent-root-4935 is working on this issue now.

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