Repository navigation
fix(calibration): strip padding before the position budget - #4271
Merged
Merged
Conversation
`prepare_calibration_dataset` cut an over-long row purely by width: `row_ids[:max_positions]` (or the tail when `truncation_side="left"`). A left-padded row therefore spent its budget on leading pads and dropped real tokens from the end, and a row whose padding alone filled the budget survived as pure padding with an all-zero attention mask. That row still passed the minimum-length check, which measures padded width, so the Hessian received zeros for it. Strip positions the attention mask marks as padding before applying the position budget, then truncate the real tokens on `truncation_side`. A row with no real token left is dropped whether or not it needed trimming, so no all-pad row is ever emitted; the drop is counted and logged. Stripping or dropping rows can leave a multi-row item ragged. The batched and concatenation paths consume rows individually, so only the non-batched path, which stacks an item into one tensor, re-pads the item to its widest row on `padding_side`. Fixes #4238. Signed-off-by: ZX-ModelCloud <[email protected]>
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
prepare_calibration_datasetspent the model's position budget on paddinginstead of on real tokens. Over-long rows are now measured on the tokens the
attention mask marks as real, truncated on
truncation_side, and a row with noreal token left is dropped instead of being emitted as an all-pad row.
Net:
5 files changed, 264 insertions(+), 22 deletions(-); one function(
gptqmodel/utils/calibration.py) plus its tests and reference page.Fixes #4238. Related: #4008 (introduced
truncation_side), #4194.1. The problem
The trimmed length was the padded width, so a left-padded row kept its
leading pads and lost real tokens from the end. When the padding alone filled
the budget, the row came out as pure padding with an all-zero attention mask and
still passed the
calibration_data_min_lengthcheck, which also measures width.flowchart TD A["row_ids, row_mask from _pack_ids"] --> B{"padded width > max_positions"} B -- "no" --> C["row kept unchanged"] B -- "yes" --> D{"truncation_side"} D -- "right (HF default)" --> E["row_ids[:max_positions]<br/>keeps leading pads"] D -- "left" --> F["row_ids[-max_positions:]<br/>drops BOS/sink"] E --> G["real tail tokens lost"] F --> G E --> H["padding at least budget-long:<br/>all-pad row, all-zero mask"] H --> I["passes min-length check<br/>Hessian receives zeros"] style G fill:#fff1f0,stroke:#d4380d style H fill:#ffccc7,stroke:#cf1322 style I fill:#ffccc7,stroke:#cf1322Observed on
origin/main(7fa967742) and reproduced below:[[0,0,11,12]]instead of
[[11,12,13,14]], and[[0,0,0,0]]with an all-zero mask instead of[[11,12]].utils/calibration.py:474-490before this changeutils/calibration.py:498padding_side="left"front-truncates every row2. The new flow
flowchart TD A["row_ids, row_mask from _pack_ids"] --> B{"padded width > max_positions"} B -- "no" --> I B -- "yes" --> D["real_positions = positions where row_mask is truthy"] D --> E["real_ids = row_ids[real_positions]<br/>real_mask = row_mask[real_positions]"] E --> F{"truncation_side"} F -- "right (HF default)" --> G["real_ids[:max_positions]"] F -- "left" --> H["real_ids[-max_positions:]"] G --> I{"row has at least one real token"} H --> I I -- "no" --> J["dropped_padded_row_count += 1<br/>row not appended"] I -- "yes" --> K["append prepared row"] K --> L{"support_batch_quantize"} L -- "False and item is ragged" --> M["re-pad item to its widest row<br/>on padding_side"] L -- "True or uniform width" --> N["row list goes to collate_data / concat"] M --> N N --> O["batched tensors handed to the quantizer"] style J fill:#fff1f0,stroke:#d4380d style B fill:#e6f4ff,stroke:#0958d9 style F fill:#e6f4ff,stroke:#0958d9Decisions, and what each is made from:
normalized
attention_mask(_normalize_attention_mask). No new metadata.tokenizer.truncation_side, unchanged from [BUG] Rotation Hadamard crashes on CPU tensors; left-padding calibration truncation drops BOS; QQQ gs=128 export scale from W not Q; W4AFP8 Triton quantizer not bit-exact #4008.Padding direction stays independent.
any(row_mask)after trimming. A row with no truthymask position is dropped whether or not it needed trimming, because it can
only contribute zeros.
support_batch_quantizeis false and thesurviving rows of one item have different widths.
collate_dataand theconcatenation path already handle ragged rows, so nothing is padded there.
3. Failure and ambiguity contract
stateDiagram-v2 [*] --> Normalized Normalized --> Trimmed: padded width > max_positions Normalized --> Dropped: no truthy mask position Trimmed --> Dropped: no token left after truncation Trimmed --> Kept: at least one real token Dropped --> [*]: counted, WARN logged Kept --> [*]dropped_padded_row_countWARNpadding_sidewithpad_token_idpadding_side/truncation_sideinvalidValueErrorThe all-pad drop fires even when
max_positionsis unresolved, so the "all-padrow in the Hessian" hole is closed on both paths. That is the one behaviour
change beyond issue #4238's literal reproduction.
4. Change inventory
gptqmodel/utils/calibration.pytests/test_calibration_truncation.pydocs/calibration-row-trimming.mddocs/README.mdREADME.md5. Tests / guarantees
tests/test_calibration_truncation.py:test_left_padded_overlong_row_strips_padding_before_the_budget[[0,0,11,12,13,14]]→[[11,12,13,14]]test_padding_at_least_as_long_as_the_budget_keeps_real_tokens[[11,12]]test_all_padding_row_below_the_budget_is_dropped_tootest_all_padding_row_is_dropped_instead_of_kept_emptytest_padded_and_real_rows_in_one_item_keep_only_the_real_rowtest_right_padding_is_stripped_tootest_truncation_side_left_keeps_the_real_tailtruncation_side="left"selects the real tailtest_ragged_rows_after_stripping_are_re_padded_for_the_unbatched_pathsupport_batch_quantize=Falsestill stackstest_calibration_truncation.pycasestruncation_siderejectedtests/test_prepare_dataset.py::test_prepare_dataset_trims_left_padded_rows_from_the_left_edge6. Validation
The repo test that reproduces the issue failed before the change
(
assert [[0, 0, 11, 12]] == [[11, 12, 13, 14]]).End-to-end, on a real Llama-3.2-1B-Instruct tokenizer with a batch tokenized
padding="max_length", max_length=64and the position budget lowered to 8(2 rows, 11 real tokens each): the prepared batch is
[2, 8], both rows keep 8real tokens, and the retained ids equal the first 8 real tokens of each row.
The same data quantized end-to-end, saved, reloaded and generated. A second run
with a 180-wide padded batch, a 64-token budget and 4 documents included
m.quantize,m.save, reload and generation.7. Model Support / Module Plan
N/A — no new model types, aliases, or registry changes. No checkpoint format or
kernel change.
8. Reviewer notes
the budget.
padding_sideandpad_token_id; it is deliberately not applied to batched or concatenatedrows, where
collate_dataowns the layout.[B,T,T]masks collapse to one token; min-length checks only row 0 and counts padding; validation gaps #4194 covers the separatecalibration_data_min_lengthgap (it checks row 0'spadded width). This PR does not change that check; it only guarantees no
all-pad row reaches it. The two are complementary and can land independently.