Skip to content

fix(calibration): pack before the per-row policies when concat_size is set (#4892) - #4916

Merged
Qubitium merged 1 commit into
mainfrom
fix/calibration-concat-policies
Nov 17, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/calibration-concat-policies

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4892.

TL;DR

When concat_size is set, prepare_calibration_dataset now packs every real
token first and applies the per-row policies to the packed windows. The per-row
min_length filter and position-budget trim no longer run on that path, and
concat_size is 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:#0958d9
Loading
Mechanism Where Why it is wrong with concat_size
Per-row min_length filter utils/calibration.py:899-903 (before this PR) Packing combines short samples; filtering first drops them. Default min_length=10
Per-row position trim utils/calibration.py:867-871 (before this PR) Trims a long document to the budget, then windows it far below the budget. Everything past max_position_embeddings is lost
No concat_size/budget validation utils/calibration.py:956 (before this PR) concat_size > max_position_embeddings produced over-budget windows
Tail window unchecked utils/calibration.py:1080-1095 (before this PR) A window with 1 real token reached the Hessian

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:

Configuration Before After
concat_size=256, default min_length=10 0 windows, 0 real tokens 10 windows, 2423 real tokens
concat_size=256, min_length=0 10 windows, 2423 tokens 10 windows, 2423 tokens
one 40000-token document, concat_size=2048 16 windows, 32768 tokens (7232 lost) 20 windows, 40000 tokens (0 lost)
concat_size=32769 accepted ValueError

2. 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:#0958d9
Loading

The two per-row guards read concat_mode directly:

if not concat_mode and max_positions is not None and row_len > max_positions:
    ...
if not concat_mode and sum(1 for keep in row_mask if keep) <= calibration_data_min_length:
    ...

The window filter lives in flush_buffer() and in the final tail block. Pad
positions are already stripped by the packer, so sum(attention_mask_buff) is
the window's real token count.

3. Up-front validation

A packed window is exactly concat_size wide, so it can only fit the budget
when 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:#0958d9
Loading

4. Failure and ambiguity contract

Situation Before After
Short document with concat_size set Dropped by per-row min_length Packed with its neighbours
Long document with concat_size set Truncated to the budget before windowing Packed in full; no token lost
concat_size > max_positions Accepted, over-budget windows ValueError before any row is read
Tail window at or below min_length Kept Dropped, counted in too_short_window_count, WARN
Every window dropped (concat_size <= min_length) Returned [] ValueError naming both settings
max_positions unresolved No trim No validation either; unchanged
No concat_size Per-row trim + filter Unchanged

5. Change inventory

Area File ± Purpose
calibration gptqmodel/utils/calibration.py +55/−9 concat_mode, up-front width validation, window-level min_length, empty-result guard
docs docs/calibration-row-trimming.md +120 New concat section: flow, validation flow, before/after, measured table
docs README.md +6 Bullet linking the concat section
tests tests/test_calibration_concat_policies_4892.py +150 7 new cases
tests tests/test_calibration_input_gaps_4311.py +6/−1 Its concat case moves to min_length=0; see below

6. Tests

tests/test_calibration_concat_policies_4892.py:

Test Invariant
test_concat_keeps_documents_shorter_than_min_length Ten 3-token docs under min_length=10 yield 30 real tokens, not 0
test_concat_does_not_trim_a_document_before_packing A 200-token doc under a 64-token budget keeps all 200 tokens
test_concat_drops_a_tail_window_below_min_length The 4-token tail of a 16-token window set is dropped
test_concat_keeps_a_tail_window_above_min_length A 14-token tail is kept
test_concat_size_above_the_position_budget_is_refused ValueError naming the budget
test_concat_size_equal_to_the_position_budget_is_accepted Boundary accepted
test_non_concat_path_keeps_the_per_row_policies The non-concat path still trims and filters

One existing expectation changed. test_concat_path_strips_pad_tokens_before_packing
used min_length=1 and asserted that a tail window with exactly 1 real token
survives. Under the issue's rule (min_length applies to packed windows) that
window 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:

$ python repro_4892.py
rows 400 raw tokens 2423
  concat_size=256 min_length=10: windows=0 real tokens=0
  concat_size=256 min_length=0: windows=10 real tokens=2423
  one 40000-token doc, concat_size=2048: windows=16 real tokens=32768 lost=7232
  concat_size > max_position_embeddings: ACCEPTED (bug)

$ pytest test_calibration_concat_policies_4892.py test_calibration_input_gaps_4311.py test_prepare_dataset.py -q
5 failed, 28 passed

Green, same host, with the fix:

$ python repro_4892.py
  concat_size=256 min_length=10: windows=10 real tokens=2423
  concat_size=256 min_length=0: windows=10 real tokens=2423
  one 40000-token doc, concat_size=2048: windows=20 real tokens=40000 lost=0
  concat_size > max_position_embeddings: ValueError: ... exceeds max_position_embeddings=32768 ...

$ pytest <18 calibration + VLM modules> -q
402 passed, 1 warning in 41.04s

test_docs_contract_refs.py is not green on this base, and the failure is not
from this PR: 4 references in docs/awq-scale-folding.md,
docs/calibration-replay.md (2) and docs/fp8-scale-semantics.md point at
gptqmodel/models/base.py lines that the just-merged #4909 shifted. A clean
origin/main worktree at 7edbca8 gives the same 4 failed, 16 passed with no
patch applied. This PR touches neither base.py nor those pages.

ruff check passes 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, workload e51a73d81f31.

…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant