Skip to content

[BUG] Left-padded batch_size>1 calibration shifts absolute position embeddings (GPT-2/GPT-Neo): capture forward passes no position_ids derived from the mask #4861

Closed

Description

@Qubitium

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

  1. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

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

  2. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim released: agent-11788dff99fd finished 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

  3. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim released: agent-root-4861 finished 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-padded batch_size>1 rows and the left-padded calibration_dataset_concat_size tail, plus right-pad/OPT/no-position_ids negative controls. Red before the fix (4 failed, 5 passed), green after (9 passed).

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