Skip to content

[PERF] Pre-padded calibration rows (tokenizer(texts, padding=True)) are forwarded at full padded width: 5.1x tokens, 2.1x quantize time, 2x VRAM; sort becomes a no-op #4889

Closed

Description

@Qubitium

Found on main @ 4f287eb, Radeon AI PRO R9700 (ROCm 10.1, gfx1201).

What is wrong

Pre-padded calibration rows keep their padding all the way into the forward passes. prepare_calibration_dataset (gptqmodel/utils/calibration.py:796-898) strips pad positions only when a row is over-long (trim path, #4238) or when concat_size is set (#4311). A row that is merely padded, which is what tokenizer(texts, padding=True) and padding="max_length" produce and what #4193/#4652 route into this path, goes through unchanged:

  • every capture and per-layer replay forward runs at the padded width (the mask keeps the pads out of the Hessian, so the checkpoint is fine; only the compute is wasted);
  • sort="desc" (the default) sorts by len(item["input_ids"][0]), the padded width (calibration.py:1053-1065), so on padded input it is a no-op and batch_size > 1 batches are no longer length-grouped;
  • the collate step (collate_data) pads to the widest row in the batch, and that row is already as wide as the widest row in the whole dataset.

The code already treats padding as not part of the sample: the trim path, the concat path and the all-pad-row filter all strip it. The unpadded, non-concat path is the only one that does not.

Measured (Qwen2.5-0.5B-Instruct fp16, GPTQ 4-bit g128, 128 C4 docs, batch_size=1, ROCm card, two runs each)

calibration input (same 51,327 real tokens) tokens forwarded per layer quantize() time peak VRAM
tok(texts, truncation=True, max_length=2048) 51,327 44.2 s / 43.8 s 0.49 / 0.61 GiB
tok(texts, truncation=True, max_length=2048, padding=True) 262,144 (210,817 pads) 94.9 s / 91.1 s 0.99 GiB
padding="max_length" 262,144 86.0 s 0.99 GiB

prepare_calibration_dataset logs it: Calibration: Total padded tokens: 210817 / Total non-padded tokens: 51327. For a 0.5B model, padding doubles the whole quantize() time. For larger models, where the forward replays dominate, the cost approaches the 5.1x token ratio. The Hessian sample count is unchanged (verified layer 0: nsamples 3474 for both the padded and unpadded forms of a 16-doc set, H bitwise equal for q/k/v), so this is pure waste.

Repro

texts = first_128_c4_docs
m = GPTQModel.load("Qwen/Qwen2.5-0.5B-Instruct", GPTQConfig(bits=4, group_size=128), dtype=torch.float16)
tok = m.tokenizer
m.quantize(CalibrationConfig(dataset=tok(texts, truncation=True, max_length=2048, padding=True)))   # 91-95 s, 0.99 GiB
# vs dataset=tok(texts, truncation=True, max_length=2048)                                            # 44 s,   0.49 GiB

Suggested fix

In the per-row loop of prepare_calibration_dataset, strip the mask-0 positions of every row (not only the over-long ones), as the concat path already does at calibration.py:990-992, then let the sort and collate_data re-pad per batch. That also makes sort work on real lengths. Rows whose mask has interior zeros could keep their mask unchanged if that case needs preserving.

Test that should have caught it

tests/test_calibration_padding_mask.py checks that pads stay out of the statistics, but not that they stay out of the forward. Assert that prepare_calibration_dataset(q, tok(texts, padding=True)) yields batches whose total width equals prepare_calibration_dataset(q, texts).

Severity

Medium [PERF]: measured 2.1x quantize time and 2x peak VRAM on a common input form (tokenizer(texts, padding=True) / padding="max_length"), no accuracy impact.

🤖 Generated with Claude Code

Activity

  1. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

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

  2. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim released: agent-root-4889 — fixed in #4946 (squash-merged as f3433d1).

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