Repository navigation
perf(calibration): strip pre-padded rows before sorting and batching (#4889) - #4946
Merged
Merged
Conversation
…4889) prepare_calibration_dataset strips pad positions only inside the over-long branch (#4238) and in the concat packer (#4311). A row that was merely padded - what tokenizer(texts, padding=True) and padding="max_length" produce - went through unchanged, so: * every capture and per-layer replay forward ran at the padded width, though the mask kept the pads out of the Hessian; * sort="desc" (default) keyed on len(input_ids[0]), the padded width, so on pre-padded input it was a no-op and batch_size > 1 batches were no longer length-grouped; * collate_data padded every batch to the widest row of the whole dataset. Strip the edge pad positions of every row, not only the over-long ones, so the sort and collate_data see real lengths and re-pad per batch. A row whose truthy mask positions are not one contiguous run carries interior mask zeros (a caller-built mask, not tokenizer padding) and is left untouched. The unpadded path is bit-identical: nothing changes when the mask has no edge zeros. 128 wikitext docs, SmolLM2-135M tokenizer, max_length=2048, padding=True: forwarded tokens base 59,648 for 20,882 real (2.86x) -> 20,882 (1.00x) sort="desc" sorted by padded width (no-op) -> sorted by real length Quantize, SmolLM2-135M fp16, GPTQ 4-bit g128, CPU, 8 docs, max_length=128, batch_size=1 (host load average ~250, so times are noisy): forwarded tokens base 768 (708 real) -> 708 (708 real) quantize() base 286.0 s -> 277.5 s
Qubitium
force-pushed
the
root/calib-strip-pad-4889
branch
from
November 17, 2026 19:42
24c6967 to
68f7212
Compare
This was referenced Nov 17, 2026
Qubitium
added a commit
that referenced
this pull request
Nov 29, 2026
A6/A13 were already realigned on main by #5535 (95c4ad9), which is why the pretokenized VLM calibration nodes are green today. The realigned tests cover the leading edge, an interior caller mask, and one qwen3.5 trailing case, but no test asserts the trailing edge or the all-padding drop across every VLM `prepare_dataset` override. Add `test_both_edge_pads_are_stripped_and_all_pad_rows_dropped` over the same introspection-discovered definition set. It asserts that a trailing tokenizer pad run is stripped, an interior mask zero survives verbatim, and an all-padding row is dropped instead of reaching the looper. With the strip disabled the new node fails 37/37, so it guards the #4946 contract rather than restating the shape oracle.
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.
Summary
Fixes #4889.
prepare_calibration_datasetstripped pad positions only when a rowwas over the model's position budget (#4238) or when
concat_sizewas set(#4311). A row that was merely pre-padded — what
tokenizer(texts, padding=True)andpadding="max_length"produce, and what#4193/#4652 route into this path — kept its padding all the way into the forward
passes. Three consequences:
keeps the pads out of the Hessian, so the checkpoint was fine — pure waste);
sort="desc"(the default) keyed onlen(item["input_ids"][0]), the paddedwidth, so on pre-padded input it was a no-op and
batch_size > 1batches wereno longer length-grouped;
collate_datapadded each batch to the widest row of the whole dataset.+24/−0 in
gptqmodel/utils/calibration.py(oneelsebranch in the per-rowloop), one new test module, one docs section.
What Changed
gptqmodel/utils/calibration.py: in the per-row loop, a row that is notover the position budget now has its contiguous edge pad positions stripped
before the all-pad/min-length filters, the sort, and
collate_data.rows_changedis set only when something was actually removed, so theunpadded path is bit-identical.
zeros, i.e. a caller-built mask rather than tokenizer padding) are left
untouched, as the issue suggested. Documented in the new docs section.
concat_sizebehaviour is untouched: the concat packer already stripped pads,and it now receives already-stripped rows, producing the same windows.
real subject (ids precedence, rank-4 mask collapse); they now assert the
stripped rows. No API change.
Tests
Red,
origin/main@c2a76ce1c, library unchanged, new test module only:Green, with the fix:
Delta on the requested suites (same host, CPU-only,
CUDA_VISIBLE_DEVICES=""):$ python /tmp/root-rt.py tests/test_calibration_padding_mask.py tests/test_prepare_dataset.py \ tests/test_calibration*.py tests/test_generator_calibration_4307.py -q base (origin/main @ c2a76ce1c) 2 failed, 248 passed, 5 skipped in 507 s head (root/calib-strip-pad-4889) 2 failed, 253 passed, 5 skipped in 509 sThe two failures are identical on both sides and are the known pre-existing
test_calibration_data_device.pynodes(
test_stage_capture_balanced_mode_empty_filter_fallback,test_balanced_mode_fallback_when_input_device_not_in_forward_devices). Theonly delta is the 5 new tests.
ruff checkpasses ongptqmodel/utils/calibration.pyand every changed testfile.
What the new module pins:
test_padded_rows_are_stripped_before_sorting_and_batchingpadding=Trueand unpadded input give the same total batch width, andsort="desc"orders by real lengthtest_padding_max_length_is_stripped_toopadding="max_length"rows are stripped below the requested widthtest_padded_and_unpadded_keep_the_same_real_tokens_and_nsamplestest_interior_mask_zeros_are_preserved[1, 1, 0, 0, 1]mask is not rewrittentest_batches_are_padded_to_their_own_widest_rowbatch_size=2collates to each batch's own widest row ([11, 7]), not the dataset width ([11, 11])Measurements
Token count — 128 wikitext docs, SmolLM2-135M tokenizer,
max_length=2048,batch_size=1,sort="desc":padding=Truepadding=Truequantize()time — SmolLM2-135M fp16, GPTQ 4-bit g128, CPU, 8 docs,max_length=128,batch_size=1. The shared host was at load average ~250during the runs, so the wall-clock delta is smaller than the token delta and
noisy; the two runs per side were back-to-back:
padding=Truepadding=TrueModel Support / Module Plan
Not applicable: no
MODEL_MAPentry, alias, or definition change.AWQ onboarding safety
Not applicable: no model onboarding, grouping, replay, or fold change.
Logic Flow Documentation
The per-row flow gains one branch. Diagram (also added to
docs/calibration-row-trimming.md, linked fromREADME.mdline 153):flowchart TD A["row_ids, row_mask"] --> B{"padded width > max_positions"} B -- "yes" --> C["strip truthy positions, then truncate by truncation_side"] B -- "no" --> D{"row_mask has exactly one<br/>contiguous run of truthy positions"} D -- "no, interior zeros" --> E["keep the row as given"] D -- "yes" --> F["row_ids[first_real : last_real + 1]<br/>row_mask[first_real : last_real + 1]"] C --> G["all-pad / min-length filters"] E --> G F --> G G --> H["sort by real length, then collate_data re-pads per batch"] style E fill:#e6f4ff,stroke:#0958d9 style F fill:#e6f4ff,stroke:#0958d9calibration.py:983, the newelse)rows_changednot set (bit-identical)concat_sizesetsupport_batch_quantize=False, item left ragged by strippingpadding_sidehonouredReview Requirements
concat_sizeoutput is unchanged.Kernel Accuracy Requirements
Not applicable: no kernel change.
Notes
tests/test_docs_contract_refs.pyis not green on this base (4 failures indocs/awq-scale-folding.md,docs/calibration-replay.mdx2,docs/fp8-scale-semantics.mdpointing atgptqmodel/models/base.pylines that#4909 shifted). Same 4 failures on a clean
origin/mainworktree; this PRtouches neither
base.pynor those pages.Rebased onto current
main, which now also carries #4916'sconcat_sizepackingorder (#4892), #4930's
position_idscapture change and #4927's messages/toolsrendering. The strip sits in the
elseof #4916'sif not concat_mode and max_positions is not None and row_len > max_positions:,so both changes compose: the concat path keeps its pack-then-filter order, and
its windows are unchanged because the packer already stripped pads.