Repository navigation
fix(awq): normalize additive attention masks as non-negative (#4187) - #4190
Merged
Merged
Conversation
An additive attention mask is summed into the logits, so 0 is attended and only negatives suppress. The old (m > 0) branch read an all-zero unpadded bs>1 grid as fully masked, failed the row_count check, and silently replayed the packed batch as one causal sequence. Normalize float additive grids as m >= 0. Keep the 0/1 multiplicative keep grid used by LLaDA2 as its own classified form so its positive = attended convention is not conflated with an additive mask.
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 #4187
TL;DR
AWQProcessor._token_row_source_structureno longer reads an all-zero additive mask as "everything masked". Float additive grids normalize withm >= 0. The integer 0/1 multiplicative keep grid used by LLaDA2 is classified as its ownpositive_keepform, so itspositive = attendedconvention is preserved instead of being conflated with an additive mask. Net diff: 4 files, +270/-15.1. The problem
At
9dad24974,gptqmodel/looper/awq_processor.py:1530normalized every non-bool[B, S, S]grid with:An additive mask is summed into the logits, so
0is attended and only negatives suppress. An unpaddedbs > 1calibration batch captures an all-zero[B, 1, S, S]additive grid, which took the(m > 0)branch. The diagonal keep mask became all-False, theflat_keep.sum() != row_countcheck at:1545failed, the function returnedNone, and_token_row_replay_kwargsfell back to one causal sequence across allB*Spacked rows. Silent;bs == 1is unaffected.Was
(m > 0)load-bearing?Yes, for a different representation. LLaDA2 expands its 2D tokenizer mask into an integer
[B, 1, S, S]multiplicative keep grid (gptqmodel/models/definitions/llada2.py:22-31):That grid uses
1 = attended,0 = masked._captured_mask_formclassified every non-bool mask asadditive, so the value-dependent(m > 0)branch was the only thing keeping LLaDA2's padded captures recoverable. Replacing it withm >= 0unconditionally would count padding queries as real and returnNone. A float 0/1 grid is not a PyTorch additive convention, so the correct split is by representation (dtype), not by whether the values happen to be negative.2. The new flow
flowchart TD A["AWQProcessor._captured_mask_form"] --> B{"mask.dtype"} B -- bool --> C{"square grid diagonal has True"} C -- yes --> D["bool_keep"] C -- no --> E["bool_masked"] B -- float --> F["additive"] B -- integer --> G["positive_keep"] F --> H["AWQProcessor._token_row_source_structure<br/>allowed = m >= 0"] G --> I["allowed = m > 0"] D --> J["allowed = m"] E --> K["allowed = ~m"] H --> L["keep = diagonal(allowed)"] I --> L J --> L K --> L L --> M{"flat_keep.sum() == row_count"} M -- no --> N["None: per-batch contiguous fallback"] M -- yes --> O["documents + positions + allowed"] O --> P["AWQProcessor._token_row_replay_kwargs<br/>allowed & same_document"] P --> Q{"captured form"} Q -- additive --> R["float block grid, finfo.min masked"] Q -- positive_keep --> S["integer 0/1 block grid, 1 attended"] Q -- bool_keep --> T["bool block grid, True attended"] Q -- bool_masked --> U["bool block grid, True masked"] style N fill:#ffd6d6,stroke:#b00020Before/after of the decision mechanism:
9dad24974)awq_processor.py:1530value-dependent(m >= 0) if negatives else (m > 0)awq_processor.py:1536-1545float additivem >= 0; integerpositive_keepusesm > 0awq_processor.py:1477-1494_captured_mask_formreturnedadditivefor every non-bool dtype:1470adds_MASK_FORM_POSITIVE_KEEP; float ->additive, integer ->positive_keepawq_processor.py:1684-1692int 0/1 captures were rebuilt as an additive float mask withfinfo.min:1687-1691rebuilds the captured integer 0/1 grid in its original dtypeNonefallback onrow_countmismatch3. Fix
allowed_grid = m >= 0(0 attended, negatives masked). This fixes the all-zero unpadded case._MASK_FORM_POSITIVE_KEEP, normalized asm > 0and rebuilt as an integer 0/1 block grid in the captured dtype. This keeps LLaDA2's convention and fixes a latent form-preservation bug where its integer grid was rebuilt as a floatfinfo.minmask.docs/awq-calibration-aggregation.mdgains the form, polarity rules, diagram branch, and failure-table row;README.mdlinks the page.4. Before / after evidence
Direct call on the parent (
9dad24974), unchanged snippet from the issue:On the branch, both venvs:
End-to-end replay,
B=2,S=3, all-zero additive capture,max_tokens=6:position_ids = [[0,1,2,3,4,5]]:position_ids = [[0,1,2,0,1,2]]:The captured all-zero grid contains no causal suppression, so the rebuild preserves its full within-document relation and adds only cross-document separation; causality stays inside the attention implementation for that captured form, matching the issue description.
5. Tests
New tests in
tests/test_awq_weight_mean.py:9dad24974test_awq_token_row_source_structure_recovers_an_all_zero_additive_maskassert None is not Nonetest_awq_token_rows_replay_unpadded_additive_mask_is_block_diagonaltest_awq_token_row_source_structure_keeps_padded_additive_and_bool_formstest_awq_token_row_source_structure_recovers_a_0_1_keep_grid_MASK_FORM_POSITIVE_KEEPabsent)test_awq_token_rows_replay_preserves_a_0_1_keep_gridExact commands and results (branch
c2c4faa26, CPU only):Parent check: reverting only
gptqmodel/looper/awq_processor.pyto9dad24974and rerunning the same 5-test selection gives 4 failed, 1 passed (the guard test passes before and after).The CUDA venv cannot collect this test file (
ModuleNotFoundError: parameterized, pre-existing); it ran the issue snippet directly and recovered every form above.tests/test_awq_gptq_lifecycle.pygives 3 passed, 1 failed (test_awq_capture_is_pre_forward_and_gptq_does_not_receive_the_hook); that failure reproduces identically on9dad24974and is ingptq_processor.py, unrelated to this change.6. Reviewer notes / residual risk
row_countmismatch path is still a silent per-batch contiguous fallback; this PR only removes the false mismatch.