Repository navigation
fix(calibration): keep all-zero extended attention masks (#5134) - #5155
Merged
Merged
Conversation
normalize_seq_mask inferred additive vs 0/1 from the presence of a negative value. PanGu-alpha's layer input mask (1.0 - mask) * -10000.0 is all zeros for an unpadded row, so the 0/1 branch dropped every token and every GPTQ module accumulated a zero Hessian and fell back to RTN. Treat an all-zero floating-point or rank>=3 mask as the unpadded additive form (keep = m >= 0). 2-D integer 0/1 masks keep their all-padding meaning; bool, partially padded additive, all-negative and causal masks are unchanged. Remove the duplicate special case from calibration.py so layer replay, user calibration and AWQ share one classifier. Add unit coverage for the mask forms and a PanGu test asserting every GPTQ module records non-zero Hessian samples. Document the flow and ambiguity contracts in docs/attention-mask-keep-classification.md. Fixes #5134
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 #5134.
GPTQ on PanGu-alpha never saw a calibration token: every quantized module of every layer fell back to RTN with
no calibration samples reached this module; forcing rtn fallback. The layer-input keep mask was built bynormalize_seq_mask, which inferred "additive" vs "0/1" from the presence of a negative value. PanGu's remote block code passes the old HF extended additive mask(1.0 - mask[:, None, None, :]) * -10000.0; for an unpadded row that tensor is all zeros, so the classifier took the 0/1 branch (m > 0) and returned all-False.#4946strips edge padding, so every PanGu calibration mask reaching a layer is all zeros and every Hessian was empty.The same helper feeds the GPTQ Hessian, user calibration masks and the AWQ token-row replay. The fix centralises the all-zero additive rule there and removes the duplicate special case in
calibration.py, so the paths cannot drift again.What Changed
gptqmodel/utils/attn_mask.py: an all-zero floating-point or rank >= 3 numeric mask is now the unpadded additive form (keep = m >= 0, allTrue). 2-D integer 0/1 masks keep their all-padding meaning; bool masks, partially padded additive masks, all-negative masks and 4-D causal masks are unchanged. The CUDA/MPS path builds the decision as a device tensor so it does not add a host sync.gptqmodel/utils/calibration.py: removed the duplicate all-zero special case;normalize_seq_maskowns the rule for both layer replay and user calibration.tests/test_attn_mask.py: unit coverage for all-zero[B, 1, 1, S], the exact PanGu form with a padded control, partially padded additive, all-negative, 2-D integer all-zero, 2-D float all-zero, all-zero rank-3 integer and bool masks.tests/test_pangu_alpha_replay.py: new unpadded PanGu GPTQ node that asserts every quantized module accumulated a non-zero Hessian sample count; the quality node now prints its errors.docs/attention-mask-keep-classification.md: mechanism page with before/after flows,file:lineevidence and ambiguity contracts; linked fromREADME.md.Tests
Environment:
CUDA_VISIBLE_DEVICES=0,PYTHONPATH=/root/work/issue-5134,HF_HUB_OFFLINE=1, torch 2.15.0+cu132, transformers 5.22.1, GPU 0 only.Red evidence (source file temporarily restored to
HEAD, tests kept):Also red before the fix:
tests/test_pangu_alpha_replay.py::test_pangu_last_layer_gptq_beats_rtn-> GPTQ error0.0942== RTN0.0942.Green after the fix:
Skip, not a pass:
tests/test_hessian.py::test_hessian_merge_multi_gpu_with_attention_maskwas collected but skipped because this run is restricted to GPU 0 (requires 2 CUDA devices for the multi-GPU merge). The single-GPU mask paths are covered by the suites above.Model Support / Module Plan
Not applicable: no new model, alias, definition, module plan or fusion change.
AWQ onboarding safety
Not applicable: no model onboarding or AWQ grouping/search/fold change. The AWQ replay consumer of the changed helper is exercised by
tests/test_awq_hash_moe_kept_rows_5015.py(passed).Logic Flow Documentation
file:lineevidence.docs/attention-mask-keep-classification.mdrepeats the diagrams and is linked fromREADME.md.Before (
gptqmodel/utils/attn_mask.py:28-34ate2f9676c6; consumergptqmodel/looper/forward_executor.py:435,gptqmodel/utils/looper_helpers.py:785):flowchart TD A["layer attention_mask<br/>PanGu: (1.0 - mask) * -10000.0"] --> B{"dtype is bool?"} B -- yes --> C["keep = mask"] B -- no --> D{"any value negative?"} D -- yes --> E["additive: keep = mask at least 0"] D -- no --> F["0/1: keep = mask greater than 0"] F --> G["all-zero unpadded row<br/>keep = all False"] G --> H["apply_keep_mask_bt drops every token"] H --> I["GPTQ nsamples = 0"] I --> J["RTN fallback<br/>zero Hessian"] style G fill:#ffccc7,stroke:#cf1322 style J fill:#ffccc7,stroke:#cf1322After (
gptqmodel/utils/attn_mask.py:30-46):flowchart TD A["attention_mask"] --> B{"dtype is bool?"} B -- yes --> C["keep = mask"] B -- no --> D{"any value negative?"} D -- yes --> E["additive: keep = mask at least 0"] D -- no --> F{"all values zero AND<br/>floating point OR rank at least 3?"} F -- yes --> G["unpadded additive:<br/>keep = mask at least 0 = all True"] F -- no --> H["0/1: keep = mask greater than 0"] C --> I["collapse broadcast/causal dims to B,S"] E --> I G --> I H --> I I --> J["Hessian / AWQ consumers"] style G fill:#d9f7be,stroke:#389e0d style J fill:#d9f7be,stroke:#389e0dAmbiguity contract: an all-zero rank >= 3 integer grid can mean an unpadded additive mask or a 0/1 causal mask whose every position is padding. The fix follows the extended/additive convention (all keep), because an all-masked calibration row has no signal and only produces the RTN fallback this change removes.
tests/test_attn_mask.pypins that choice.Review Requirements
GPTQ.quantizesample counts.Kernel Accuracy Requirements
Not applicable: no kernel, packer or dispatch change.
Notes
calibration.pypreviously normalised all-zero float/rank >= 3 masks to ones before calling the helper. Removing that duplicate is behaviour-preserving for user calibration and makes the layer-replay and AWQ paths share the rule.rg -n "normalize_seq_mask" gptqmodel tests.