Skip to content

[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) #4187

Closed

Description

@Qubitium

Summary

AWQProcessor._token_row_source_structure normalizes a captured additive attention mask with
(m >= 0) if (m < 0).any() else (m > 0). An additive mask that contains no negative entries — the normal
case for an unpadded batch, where nothing is masked and causality is handled inside the attention
implementation — therefore takes the (m > 0) branch, which marks every position as masked. The diagonal
keep mask comes back all-False, the flat_keep.sum() != row_count check fails, and the function returns
None. The replay then falls back to a single causal sequence spanning the whole packed batch, which is
exactly the cross-document contamination #4125 item 4 set out to remove.

Additive masks are summed into the logits, so 0 is neutral (attended) and only negatives suppress. (m > 0)
is never the right normalization for an additive mask.

Code

gptqmodel/looper/awq_processor.py:1530 (main @ 9dad2497):

if m.dtype == torch.bool:
    allowed_grid = m if form == AWQProcessor._MASK_FORM_BOOL_KEEP else ~m
else:
    allowed_grid = (m >= 0) if bool((m < 0).any()) else (m > 0)
keep = allowed_grid.diagonal(dim1=-2, dim2=-1)

The failure surfaces at :1545:

if int(flat_keep.sum().item()) != row_count:
    return None

Trigger

AWQ quantization of a model whose captured attention_mask is additive and entirely non-negative, with
batch_size > 1. That is an unpadded batch: all documents the same length, no padding keys to suppress, so
every entry of the [B, 1, S, S] additive mask is 0.

Expected vs actual

  • Expected: an all-zero additive mask means "everything attended", the diagonal is all True,
    flat_keep.sum() == row_count, and the per-document block structure is recovered (as it already is for
    bool keep masks and for additive masks that contain -inf).
  • Actual: (m > 0) yields an all-False grid, the structure check fails, None is returned, and
    _rebuild takes its documented fallback — "an explicit causal mask is the equivalent replay" — which is
    one causal sequence across all B*S packed rows.

Impact

Silent, and the same class as #4125 item 4: with bs > 1 and no padding, the calibration replay lets later
documents attend to earlier documents' tokens, so GPTQ Hessians and AWQ scale search are built from attention
the deployed model never computes. No error, no warning. bs == 1 is unaffected in practice (one document),
which is why the common single-sequence smoke tests do not show it.

Evidence (CPU, 9dad24974, direct calls into the processor)

z = torch.zeros(1, 1, 5, 5)                      # additive, nothing masked
AWQProcessor._token_row_source_structure(z, 5)   # -> None  (fallback)
all-zero additive, row_count=B*S=5 -> None (fallback)
padded additive (-inf keys), row_count=6 -> RECOVERED
bool keep mask -> RECOVERED
all-zero: has_negatives=False  branch=(m>0)  diagonal_keep=[False, False, False, False, False]
all-zero: flat_keep.sum()=0 vs row_count=5

So the additive path is only broken for the non-negative case; the padded additive case and the bool cases
recover correctly.

Suggested fix

Normalize additive masks unconditionally, since 0 is attended and negatives are suppressed:

else:
    allowed_grid = m >= 0

Then add a regression test that calls _token_row_source_structure with an all-zero [B, 1, S, S] additive
mask and row_count == B*S, asserts a non-None result, and asserts the rebuilt mask is block-diagonal
rather than one causal block. A second case with -inf on padding keys should keep working. Testing the
private helper directly is appropriate here — the existing end-to-end AWQ tests use masks that contain -inf,
which is why this slipped through.

Validation limits

Observed directly on CPU with synthetic masks; the perplexity/accuracy cost on a real checkpoint with an
unpadded bs > 1 calibration set was not measured. Found while auditing #4131/#4125 item 4 during the #4150
work (reported as a cross-reference rather than fixed there, to avoid two workers editing awq_processor.py).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions