Skip to content

fix(awq): normalize additive attention masks as non-negative (#4187) - #4190

Merged
Qubitium merged 3 commits into
mainfrom
fix/issue-4187-additive-mask
Nov 16, 2026
Merged

Qubitium merged 3 commits into
mainfrom
fix/issue-4187-additive-mask

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4187

TL;DR

AWQProcessor._token_row_source_structure no longer reads an all-zero additive mask as "everything masked". Float additive grids normalize with m >= 0. The integer 0/1 multiplicative keep grid used by LLaDA2 is classified as its own positive_keep form, so its positive = attended convention 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:1530 normalized every non-bool [B, S, S] grid with:

allowed_grid = (m >= 0) if bool((m < 0).any()) else (m > 0)

An additive mask is summed into the logits, so 0 is attended and only negatives suppress. An unpadded bs > 1 calibration batch captures an all-zero [B, 1, S, S] additive grid, which took the (m > 0) branch. The diagonal keep mask became all-False, the flat_keep.sum() != row_count check at :1545 failed, the function returned None, and _token_row_replay_kwargs fell back to one causal sequence across all B*S packed rows. Silent; bs == 1 is 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):

causal_mask = torch.tril(torch.ones((seq_len, seq_len), dtype=dtype, device=device))
return causal_mask * padding_mask

That grid uses 1 = attended, 0 = masked. _captured_mask_form classified every non-bool mask as additive, so the value-dependent (m > 0) branch was the only thing keeping LLaDA2's padded captures recoverable. Replacing it with m >= 0 unconditionally would count padding queries as real and return None. 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:#b00020
Loading

Before/after of the decision mechanism:

Mechanism (at 9dad24974) Replacement
awq_processor.py:1530 value-dependent (m >= 0) if negatives else (m > 0) awq_processor.py:1536-1545 float additive m >= 0; integer positive_keep uses m > 0
awq_processor.py:1477-1494 _captured_mask_form returned additive for every non-bool dtype :1470 adds _MASK_FORM_POSITIVE_KEEP; float -> additive, integer -> positive_keep
awq_processor.py:1684-1692 int 0/1 captures were rebuilt as an additive float mask with finfo.min :1687-1691 rebuilds the captured integer 0/1 grid in its original dtype
silent None fallback on row_count mismatch unchanged (explicitly marked failure path above)

3. Fix

  • Float additive grids: allowed_grid = m >= 0 (0 attended, negatives masked). This fixes the all-zero unpadded case.
  • Integer grids: new _MASK_FORM_POSITIVE_KEEP, normalized as m > 0 and 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 float finfo.min mask.
  • Bool keep/masked and planar forms are unchanged.
  • Docs: docs/awq-calibration-aggregation.md gains the form, polarity rules, diagram branch, and failure-table row; README.md links the page.

4. Before / after evidence

Direct call on the parent (9dad24974), unchanged snippet from the issue:

z = torch.zeros(1, 1, 5, 5)
AWQProcessor._token_row_source_structure(z, 5)   # -> None
all-zero additive, row_count=5 -> None (fallback)
all-zero: has_negatives=False branch=(m>0) diagonal_keep=[False x5]
all-zero: flat_keep.sum()=0 vs row_count=5

On the branch, both venvs:

all-zero additive bs1 -> RECOVERED form=additive keep_rows=5 allowed_all=True
all-zero additive bs2 -> RECOVERED form=additive keep_rows=6
padded additive -> RECOVERED form=additive
bool keep -> RECOVERED form=bool_keep
bool masked -> RECOVERED form=bool_masked
0/1 int keep grid -> RECOVERED form=positive_keep

End-to-end replay, B=2, S=3, all-zero additive capture, max_tokens=6:

  • Parent: one causal block, position_ids = [[0,1,2,3,4,5]]:
[[1,0,0,0,0,0],
 [1,1,0,0,0,0],
 [1,1,1,0,0,0],
 [1,1,1,1,0,0],
 [1,1,1,1,1,0],
 [1,1,1,1,1,1]]
  • Branch: block-diagonal per document, position_ids = [[0,1,2,0,1,2]]:
[[1,1,1,0,0,0],
 [1,1,1,0,0,0],
 [1,1,1,0,0,0],
 [0,0,0,1,1,1],
 [0,0,0,1,1,1],
 [0,0,0,1,1,1]]

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:

Test Parent 9dad24974 Branch
test_awq_token_row_source_structure_recovers_an_all_zero_additive_mask FAIL assert None is not None PASS
test_awq_token_rows_replay_unpadded_additive_mask_is_block_diagonal FAIL (one causal block, synthetic positions) PASS
test_awq_token_row_source_structure_keeps_padded_additive_and_bool_forms PASS PASS
test_awq_token_row_source_structure_recovers_a_0_1_keep_grid FAIL (_MASK_FORM_POSITIVE_KEEP absent) PASS
test_awq_token_rows_replay_preserves_a_0_1_keep_grid FAIL (rebuilt as float) PASS

Exact commands and results (branch c2c4faa26, CPU only):

PYTHONPATH=$PWD /opt/venvs/rocm101t/bin/python -m pytest tests/test_awq_weight_mean.py \
  -k "all_zero_additive or unpadded_additive or keeps_padded_additive or 0_1_keep_grid" -q
# 5 passed, 81 deselected

PYTHONPATH=$PWD /opt/venvs/rocm101t/bin/python -m pytest tests/test_awq_weight_mean.py \
  -k "token_row and not cuda" -q
# 22 passed, 64 deselected

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

Parent check: reverting only gptqmodel/looper/awq_processor.py to 9dad24974 and 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.py gives 3 passed, 1 failed (test_awq_capture_is_pre_forward_and_gptq_does_not_receive_the_hook); that failure reproduces identically on 9dad24974 and is in gptq_processor.py, unrelated to this change.

6. Reviewer notes / residual risk

  • Classification uses dtype: a float square grid is additive, an integer square grid is a 0/1 keep grid. A float 0/1 multiplicative keep grid would still be read as additive; no caller in the repository produces one (LLaDA2 inherits the int64 tokenizer mask dtype). If one appears, it needs an explicit form rather than a value-dependent branch.
  • The row_count mismatch path is still a silent per-batch contiguous fallback; this PR only removes the false mismatch.
  • Validation is CPU synthetic masks; no real-checkpoint perplexity/accuracy measurement, same limitation as the issue.
  • Hardware: CPU only, no GPU taken.

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.
@Qubitium
Qubitium merged commit a91d129 into main Nov 16, 2026
9 checks passed
@Qubitium
Qubitium deleted the fix/issue-4187-additive-mask branch November 16, 2026 03:53
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] AWQ token-row replay misreads an all-zero additive mask as fully masked, so unpadded bs>1 batches fall back to one causal sequence (#4131)

1 participant