Skip to content

fix(calibration): keep all-zero extended attention masks (#5134) - #5155

Merged
Qubitium merged 2 commits into
mainfrom
fix/5134-hessian-keep-mask
Nov 18, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/5134-hessian-keep-mask

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

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 by normalize_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. #4946 strips 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, all True). 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_mask owns 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:line evidence and ambiguity contracts; linked from README.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):

CUDA_VISIBLE_DEVICES=0 PYTHONPATH=/root/work/issue-5134 HF_HUB_OFFLINE=1 \
  /root/venv/bin/python -m pytest \
  tests/test_attn_mask.py::test_normalize_seq_mask_all_zero_extended_mask_keeps_every_token \
  tests/test_pangu_alpha_replay.py::test_pangu_unpadded_calibration_reaches_every_gptq_module -q -rs -s
# 2 failed
# unit: normalize_seq_mask(zeros(1,1,1,5)) == [[False] * 5]
# PanGu: all 24 modules nsamples=0

Also red before the fix: tests/test_pangu_alpha_replay.py::test_pangu_last_layer_gptq_beats_rtn -> GPTQ error 0.0942 == RTN 0.0942.

Green after the fix:

CUDA_VISIBLE_DEVICES=0 PYTHONPATH=/root/work/issue-5134 HF_HUB_OFFLINE=1 \
  /root/venv/bin/python -m pytest tests/test_attn_mask.py -q -rs
# 17 passed

CUDA_VISIBLE_DEVICES=0 PYTHONPATH=/root/work/issue-5134 HF_HUB_OFFLINE=1 \
  /root/venv/bin/python -m pytest tests/test_pangu_alpha_replay.py -q -rs -s
# 5 passed
# [pangu-quality] {'RTN': 0.09416788071393967, 'GPTQ': 0.05688156560063362}
# [pangu-hessian] modules=24, every module nsamples=128 (4 rows x 32 tokens)

CUDA_VISIBLE_DEVICES=0 PYTHONPATH=/root/work/issue-5134 HF_HUB_OFFLINE=1 \
  /root/venv/bin/python -m pytest tests/test_attn_mask.py tests/test_calibration_normalisation.py -q -rs
# 28 passed

CUDA_VISIBLE_DEVICES=0 PYTHONPATH=/root/work/issue-5134 HF_HUB_OFFLINE=1 \
  /root/venv/bin/python -m pytest \
  tests/test_calibration_attention_sinks.py \
  tests/test_positional_mask_capture_4660.py \
  tests/test_awq_hash_moe_kept_rows_5015.py -q -rs
# 39 passed

CUDA_VISIBLE_DEVICES=0 PYTHONPATH=/root/work/issue-5134 HF_HUB_OFFLINE=1 \
  /root/venv/bin/python -m pytest tests/test_generate_attention_mask.py -q -rs
# 3 passed

/root/venv/bin/ruff check gptqmodel tests setup.py
# All checks passed!

Skip, not a pass: tests/test_hessian.py::test_hessian_merge_multi_gpu_with_attention_mask was 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

  • The required diagrams and docs page are included.
  • New-flow Mermaid diagram describes the shipped code using real symbol names.
  • The removed/replaced mechanism is shown with file:line evidence.
  • Failure and ambiguity paths are marked and visually distinct from success.
  • docs/attention-mask-keep-classification.md repeats the diagrams and is linked from README.md.

Before (gptqmodel/utils/attn_mask.py:28-34 at e2f9676c6; consumer gptqmodel/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:#cf1322
Loading

After (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:#389e0d
Loading

Ambiguity 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.py pins that choice.

Review Requirements

  • Reviewed this PR.
  • Checked that the code matches existing project structure, APIs, and conventions.
  • Avoided unnecessary monkeypatching; the test monkeypatch only observes GPTQ.quantize sample counts.
  • Minimized and compacted the impacted code surface.
  • Considered and eliminated potential regressions (2-D integer masks, bool masks, real negative additive masks, causal collapse).

Kernel Accuracy Requirements

Not applicable: no kernel, packer or dispatch change.

Notes

  • calibration.py previously 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.
  • Unverified: the multi-GPU Hessian merge mask test skipped on this GPU-0-only run; no other call sites were found by rg -n "normalize_seq_mask" gptqmodel tests.

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant