Repository navigation
fix(calibration): pack before the per-row policies when concat_size is set (#4892) - #4916
Merged
Merged
Conversation
…s set (#4892) The per-row min_length filter and the per-row position-budget trim both ran before concatenation, so they discarded exactly the data packing exists to combine: - min_length (default 10) dropped every document of <= 10 tokens before packing. On 400 sentences of 3-9 words that left 0 windows and 0 of 2423 real tokens. - The position trim cut a document to max_position_embeddings before it was windowed, so a 40000-token document under concat_size=2048 lost 7232 tokens even though every resulting window was far below the budget. - concat_size > max_position_embeddings was accepted, and the padded tail window was kept regardless of how few real tokens it carried. concat_mode now skips both per-row policies and applies min_length to the packed windows, where only a near-empty tail window can fall below it. The width is validated against the position budget up front, because a window is exactly concat_size wide. The non-concat path is unchanged. An empty result now raises instead of returning an empty batch list, since concat_size <= min_length would otherwise look like an empty dataset. Docs: docs/calibration-row-trimming.md gains the concat flow, the validation flow and a before/after table; README links it. Tests: tests/test_calibration_concat_policies_4892.py (5 of 7 fail before this change). test_calibration_input_gaps_4311's concat test moves to min_length=0 because its 1-token tail window is now below a positive filter.
5 tasks done
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.
Fixes #4892.
TL;DR
When
concat_sizeis set,prepare_calibration_datasetnow packs every realtoken first and applies the per-row policies to the packed windows. The per-row
min_lengthfilter and position-budget trim no longer run on that path, andconcat_sizeis validated against the model's position budget up front.+55/−9 in
utils/calibration.py, one new test module, one docs section.1. The problem
Both per-row policies ran before concatenation. Each one throws away data that
packing exists to use.
flowchart TD A["row_ids, row_mask"] --> B{"row longer than max_positions"} B -- "yes" --> C["trim to max_positions<br/>lost: everything past the budget"] B -- "no" --> D{"real tokens <= min_length"} C --> D D -- "yes" --> E["drop row<br/>lost: every short document"] D -- "no" --> F["keep row"] E --> G["packer never sees it"] F --> H["_iter_calibration_rows packs to concat_size"] H --> I["padded tail window kept<br/>even with 1 real token"] style C fill:#fff1f0,stroke:#d4380d style E fill:#fff1f0,stroke:#d4380d style I fill:#fff1f0,stroke:#d4380d style B fill:#e6f4ff,stroke:#0958d9 style D fill:#e6f4ff,stroke:#0958d9concat_sizemin_lengthfilterutils/calibration.py:899-903(before this PR)min_length=10utils/calibration.py:867-871(before this PR)max_position_embeddingsis lostconcat_size/budget validationutils/calibration.py:956(before this PR)concat_size > max_position_embeddingsproduced over-budget windowsutils/calibration.py:1080-1095(before this PR)Measured on the Qwen2.5-0.5B-Instruct tokenizer (
max_position_embeddings=32768),400 sentences of 3-9 words, 2423 tokens total, longest row 11 tokens:
concat_size=256, defaultmin_length=10concat_size=256,min_length=0concat_size=2048concat_size=32769ValueError2. The new flow
flowchart TD A["row_ids, row_mask"] --> B{"concat_mode =<br/>bool concat_size"} B -- "no" --> C["per-row position trim<br/>then per-row min_length"] C --> D["new_calibration_dataset"] B -- "yes" --> E["keep every real token<br/>no per-row trim, no per-row min_length"] E --> F["_iter_calibration_rows packs into<br/>windows of exactly concat_size"] F --> G{"window real tokens > min_length"} G -- "yes" --> H["emit window; only the tail window is padded"] G -- "no" --> I["drop window<br/>count in too_short_window_count"] H --> J["collate_data / non-batched tensors"] I --> J D --> J style I fill:#fff1f0,stroke:#d4380d style B fill:#e6f4ff,stroke:#0958d9 style G fill:#e6f4ff,stroke:#0958d9The two per-row guards read
concat_modedirectly:The window filter lives in
flush_buffer()and in the final tail block. Padpositions are already stripped by the packer, so
sum(attention_mask_buff)isthe window's real token count.
3. Up-front validation
A packed window is exactly
concat_sizewide, so it can only fit the budgetwhen the width does. Validating the width is cheaper and more honest than
trimming rows to a budget the window will then exceed.
flowchart TD A["concat_size, max_positions"] --> B{"concat_size set"} B -- "no" --> Z["per-row path unchanged"] B -- "yes" --> C{"max_positions resolved"} C -- "no" --> Y["no validation: model declares no budget"] C -- "yes" --> D{"concat_size > max_positions"} D -- "yes" --> E["ValueError naming both settings"] D -- "no" --> F["pack"] style E fill:#fff1f0,stroke:#d4380d style D fill:#e6f4ff,stroke:#0958d94. Failure and ambiguity contract
concat_sizesetmin_lengthconcat_sizesetconcat_size > max_positionsValueErrorbefore any row is readmin_lengthtoo_short_window_count, WARNconcat_size <= min_length)[]ValueErrornaming both settingsmax_positionsunresolvedconcat_size5. Change inventory
gptqmodel/utils/calibration.pyconcat_mode, up-front width validation, window-levelmin_length, empty-result guarddocs/calibration-row-trimming.mdREADME.mdtests/test_calibration_concat_policies_4892.pytests/test_calibration_input_gaps_4311.pymin_length=0; see below6. Tests
tests/test_calibration_concat_policies_4892.py:test_concat_keeps_documents_shorter_than_min_lengthmin_length=10yield 30 real tokens, not 0test_concat_does_not_trim_a_document_before_packingtest_concat_drops_a_tail_window_below_min_lengthtest_concat_keeps_a_tail_window_above_min_lengthtest_concat_size_above_the_position_budget_is_refusedValueErrornaming the budgettest_concat_size_equal_to_the_position_budget_is_acceptedtest_non_concat_path_keeps_the_per_row_policiesOne existing expectation changed.
test_concat_path_strips_pad_tokens_before_packingused
min_length=1and asserted that a tail window with exactly 1 real tokensurvives. Under the issue's rule (
min_lengthapplies to packed windows) thatwindow is at the filter and is dropped, so the case now runs with
min_length=0. Its actual subject — pad ids must not leak into a packed window— is unchanged, and the tail-drop rule now has its own test above.
7. Validation
Red,
origin/main@d8758c4, library unchanged:Green, same host, with the fix:
test_docs_contract_refs.pyis not green on this base, and the failure is notfrom this PR: 4 references in
docs/awq-scale-folding.md,docs/calibration-replay.md(2) anddocs/fp8-scale-semantics.mdpoint atgptqmodel/models/base.pylines that the just-merged #4909 shifted. A cleanorigin/mainworktree at7edbca8gives the same4 failed, 16 passedwith nopatch applied. This PR touches neither
base.pynor those pages.ruff checkpasses on every changed Python file. Mermaid blocks are balanced:3 in this body (12 fences) and 3 in
docs/calibration-row-trimming.md(8 fences), all
flowchart TD.Claim:
agent-11788dff99fd, workloade51a73d81f31.