Skip to content

fix(calibration): normalize causal masks and validate rows per token - #4274

Merged
Qubitium merged 3 commits into
mainfrom
fix/issue-4194-calib-normalisation
Nov 16, 2026
Merged

Qubitium merged 3 commits into
mainfrom
fix/issue-4194-calib-normalisation

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4194

Summary

Three calibration input-normalisation gaps:

  1. [B, T, T] causal masks collapsed to one token. _normalize_attention_mask handed 3-D square masks to normalize_seq_mask, whose fallback flattened them to reshape(B, -1)[..., :T] — the first query row. [B, T, T] masks now collapse the query axis with any, matching the existing [B, 1, T, T] path.
  2. calibration_data_min_length checked only row 0 and counted padding. The filter now runs per row on attention_mask.sum(), so a long first row can no longer hide later rows that are mostly padding. Items whose rows all fail are dropped; the unbatched path still re-pads rows left ragged by filtering.
  3. Validation gaps. Unknown calibration_dataset_sort now raises instead of silently falling back to native order. Empty samples (torch.empty(0, N)), empty rows, mismatched row counts/lengths, and non-integer token ids raise clear ValueErrors. Integer-like token lists (NumPy scalars, torch scalars, nested [[ids]]) are accepted instead of rejected.

The #4271 padding-stripping and all-pad-row dropping logic is preserved; this change composes with it.

Tests

tests/test_calibration_normalisation.py (new, 14 tests) covers each defect:

Defect Tests
[B,T,T] mask test_normalize_seq_mask_handles_3d_causal_square, test_3d_causal_attention_mask_keeps_every_real_token, test_3d_causal_attention_mask_preserves_padding_positions
Per-row min length test_min_length_filters_each_row_by_real_token_count, test_min_length_does_not_count_padding_as_real_tokens, test_min_length_drops_an_item_when_no_row_survives, test_min_length_filter_re_pads_ragged_unbatched_items
Validation test_unknown_calibration_sort_is_rejected, test_numpy_integer_token_list_is_accepted, test_torch_scalar_token_list_is_accepted, test_nested_token_id_list_is_accepted, test_empty_token_tensor_raises_a_clear_error[*], test_float_token_ids_are_rejected_instead_of_truncated

Fail-before / pass-after

Against origin/main (c45c1b018), the same test file:

12 failed, 2 passed, 1 warning in 3.11s
FAILED ...::test_normalize_seq_mask_handles_3d_causal_square
FAILED ...::test_3d_causal_attention_mask_keeps_every_real_token
FAILED ...::test_3d_causal_attention_mask_preserves_padding_positions
FAILED ...::test_min_length_filters_each_row_by_real_token_count
FAILED ...::test_min_length_does_not_count_padding_as_real_tokens
FAILED ...::test_unknown_calibration_sort_is_rejected
FAILED ...::test_numpy_integer_token_list_is_accepted
FAILED ...::test_torch_scalar_token_list_is_accepted
FAILED ...::test_nested_token_id_list_is_accepted
FAILED ...::test_empty_token_tensor_raises_a_clear_error[sample0]
FAILED ...::test_empty_token_tensor_raises_a_clear_error[sample1]
FAILED ...::test_float_token_ids_are_rejected_instead_of_truncated

Issue repro before (main):

causal (1, 16, 16) mask_sum= 1
causal (1, 1, 16, 16) mask_sum= 16
min_length padded row -> 1 real_tokens= 3
numpy -> ValueError Quantize: list-based calibration example at index 0 must contain only integers.
torch_scalars -> ValueError Quantize: list-based calibration example at index 0 must contain only integers.
nested -> ValueError Quantize: list-based calibration example at index 0 must contain only integers.
empty -> IndexError list index out of range
bad_sort -> no error (native order)

After (this branch):

causal (1, 16, 16) mask_sum= 16
causal (1, 1, 16, 16) mask_sum= 16
min_length padded row -> 0
numpy -> [[1, 2, 3]]
torch_scalars -> [[1, 2, 3]]
nested -> [[1, 2, 3]]
empty -> ValueError Quantize: calibration item 0 has empty `input_ids` with shape (0, 8); every item needs at least one token.
bad_sort -> ValueError Quantize: unsupported `calibration_dataset_sort` 'descending'. Expected `asc`, `desc`, `shuffle`, or None.

pytest -q tests/test_calibration_normalisation.py -> 14 passed.

Related calibration suite (new file + test_prepare_dataset.py, test_calibration_truncation.py, test_calibration_padding_mask.py, test_calibration_mapping_input.py, test_attn_mask.py, test_calibration_windows.py) -> 76 passed, 1 warning.

Validation

ruff check gptqmodel docs/eora tests setup.py
All checks passed!

Residual risk

  • calibration_dataset_sort="shuffle" still uses the unseeded global random.shuffle; adding a seed argument would touch the public quantize() plumbing, so it is left as a follow-up.
  • Sorting still uses padded width (len(input_ids[0])); the issue only called out the min-length check.
  • 3-D causal masks collapse the query axis with any (the existing documented policy for [B, 1, T, T]), not the diagonal. This matches standard HF padding masks where padding keys are masked for every query.
  • Non-integer token ids (including bool) are now rejected rather than silently truncated.
  • Two GPU integration tests in tests/test_calibration_data_device.py fail on this host because its device map spans mixed compute capabilities (cuda:4=12.0); they are unrelated to this change.

…4194)

A [B, T, T] causal attention_mask was flattened to its first query row, so
calibration rows kept only one real token. Collapse the query axis for 3-D
square masks the same way the [B, 1, T, T] path already does.

The minimum-length filter looked only at row 0 and measured padded width, so a
long first row hid later rows that were mostly padding. Filter every row on
the number of real tokens in its attention mask, and keep the unbatched re-pad
path for items that become ragged.

Reject empty samples, malformed row shapes, non-integer token ids, and unknown
calibration_sort values with clear errors instead of coercing or falling back.
Accept integer-like token lists (NumPy scalars, torch scalars, nested rows).

Fixes #4194
@Qubitium
Qubitium merged commit 9b05b76 into main Nov 16, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/issue-4194-calib-normalisation branch November 16, 2026 07:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Calibration input normalisation gaps: [B,T,T] masks collapse to one token; min-length checks only row 0 and counts padding; validation gaps

1 participant