Repository navigation
fix(calibration): normalize causal masks and validate rows per token - #4274
Merged
Merged
Conversation
…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
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 #4194
Summary
Three calibration input-normalisation gaps:
[B, T, T]causal masks collapsed to one token._normalize_attention_maskhanded 3-D square masks tonormalize_seq_mask, whose fallback flattened them toreshape(B, -1)[..., :T]— the first query row.[B, T, T]masks now collapse the query axis withany, matching the existing[B, 1, T, T]path.calibration_data_min_lengthchecked only row 0 and counted padding. The filter now runs per row onattention_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.calibration_dataset_sortnow 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 clearValueErrors. 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:[B,T,T]masktest_normalize_seq_mask_handles_3d_causal_square,test_3d_causal_attention_mask_keeps_every_real_token,test_3d_causal_attention_mask_preserves_padding_positionstest_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_itemstest_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_truncatedFail-before / pass-after
Against
origin/main(c45c1b018), the same test file:Issue repro before (main):
After (this branch):
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
Residual risk
calibration_dataset_sort="shuffle"still uses the unseeded globalrandom.shuffle; adding a seed argument would touch the publicquantize()plumbing, so it is left as a follow-up.len(input_ids[0])); the issue only called out the min-length check.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.tests/test_calibration_data_device.pyfail on this host because its device map spans mixed compute capabilities (cuda:4=12.0); they are unrelated to this change.