Repository navigation
Description
Found on main @ 4f287eb, CPU (x86_64), transformers 5.22.1.
What is wrong
prepare_calibration_dataset honours tokenizer.padding_side == "left" when it collates batch_size > 1 rows (gptqmodel/utils/calibration.py:1093-1101 -> collate_data(..., padding_side="left")). The input capture then runs self.model(**example, use_cache=False) (gptqmodel/models/base.py:3180) with only input_ids and attention_mask; no position_ids are derived from the mask.
For models with learned absolute position embeddings that default position_ids to arange(seq_len) (GPT-2 modeling_gpt2.py:571-573, GPT-Neo modeling_gpt_neo.py:449-451), every real token of a left-padded row is embedded at position pad_len + i instead of i. Every calibration statistic for that row is then taken on hidden states the model never produces at inference: generate() builds position_ids from attention_mask.cumsum(-1) - 1. RoPE/ALiBi models are unaffected because attention depends only on relative position, and OPT builds its positions from the mask itself. Right padding is also correct.
Repro (CPU, fp32; layer-0 input captured by patching StageInputsCapture.cache_inputs)
Two rows of 21 and 12 tokens. The layer-0 input (cache.layer_inputs) of each row, with the pad positions removed, is compared against the same row run at batch_size=1:
m = GPTQModel.load("openai-community/gpt2", GPTQConfig(bits=4, group_size=128), device="cpu", dtype=torch.float32)
texts = ["The quick brown fox jumps over the lazy dog and then runs into the forest to hide from the hunters.",
"Hello world, this is a short calibration row of text."]
ref = capture(m, CalibrationConfig(dataset=texts, sort=None, batch_size=1))
for side in ("right", "left"):
m.tokenizer.padding_side = side
got = capture(m, CalibrationConfig(dataset=texts, sort=None, batch_size=2))
# compare got.layer_inputs[0][0][row][mask[row]] vs ref.layer_inputs[row][0][0]gpt2 pad=right row0 len=21 max|diff| layer0 input vs bs1 = 0.000e+00
gpt2 pad=right row1 len=12 max|diff| layer0 input vs bs1 = 0.000e+00
gpt2 pad=left row0 len=21 max|diff| layer0 input vs bs1 = 0.000e+00
gpt2 pad=left row1 len=12 max|diff| layer0 input vs bs1 = 4.255e+00 <-- shifted position embeddings
opt-125m pad=left row1 len=13 max|diff| layer0 input vs bs1 = 0.000e+00 (OPT derives positions from the mask)
Suggested fix
When a capture batch has an attention_mask and the example has no position_ids, pass position_ids = (attention_mask.long().cumsum(-1) - 1).clamp_min(0), which is what HF generate() does for left-padded batches. This is a no-op for unpadded and right-padded rows. Alternatively, force right padding for calibration collation, since causal attention makes right padding exact for every architecture. The left-padded final window of concat_size packing (calibration.py:1038-1040) has the same shift.
Test that should have caught it
tests/test_calibration_padding_mask.py checks that pad rows are masked out of the statistics, but not that the real tokens' activations are unchanged by padding. A tiny GPT-2 test comparing layer-0 inputs at batch_size=1 and at batch_size=2 with padding_side="left" would catch it.
Severity
Low-medium. Only absolute-position architectures (GPT-2, GPT-Neo and similar) with padding_side="left" and batch_size > 1 are affected, but there it is silent: every shorter row in a batch is calibrated on wrongly position-embedded activations.
🤖 Generated with Claude Code
Activity
Qubitium commentedon Nov 17, 2026
Agent claim:
agent-root-4861is working on this issue now.Qubitium commentedon Nov 17, 2026
Agent claim released:
agent-11788dff99fdfinished this issue.Pull request: #4930
capture forward: derive position_ids from attention_mask.cumsum(-1)-1 when the example has none, so left-padded calibration batches stop shifting absolute position embeddings
Qubitium commentedon Nov 17, 2026
Agent claim released:
agent-root-4861finished this issue.The fix itself landed as #4930 (
5fb25294f, merged before this agent reached the code). This agent added the end-to-end regression coverage the issue asked for: #4944 (774916ff6) asserts real GPT-2 layer-0 input parity for left-paddedbatch_size>1rows and the left-paddedcalibration_dataset_concat_sizetail, plus right-pad/OPT/no-position_idsnegative controls. Red before the fix (4 failed, 5 passed), green after (9 passed).fix(capture): keep mRoPE position ids for multimodal calibration (#4933…
fix(capture): derive position_ids from the mask for a padded calibrat…
test(calibration): assert left-padded capture reproduces unpadded lay…