Skip to content

fix(gptq): add opt-in attention-sink masking for the calibration Hessian (#4393) - #4440

Merged
Qubitium merged 2 commits into
mainfrom
fix/gptq-bos-quality-4393
Nov 16, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/gptq-bos-quality-4393

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

Calibration rows that begin with a BOS token make position 0 an attention sink: on Llama-3.2-1B the leading activation norm at layers.1.mlp.down_proj is ~337-680x the median row norm, so that single row contributes 99.0-99.97% of the input Hessian Frobenius norm. GPTQ then optimizes almost entirely for the sink row and gets worse than RTN on ordinary tokens (issue #4393).

This adds an explicit, opt-in GPTQConfig.exclude_attention_sinks that drops the first token of every 3-D calibration sequence before accumulating H, so H and nsamples describe the same rows. Flattened routed-expert batches have no sequence axis and are left untouched. Default is unchanged.

Addresses #4393.

What Changed

  • config.py: new exclude_attention_sinks: bool = False (help text, bool validation, meta serialization + factory reload). QQQConfig rejects it (QQQ has its own capture path), and GPTQConfig.validate() rejects it together with gptaq/foem because those methods override process_batch and would silently ignore it.
  • gptq.py: _attention_sink_row_keep() builds a (batch, seq) keep-mask for nn.Linear/Conv1D 3-D inputs; process_batch() applies it before the Hessian GEMM.
  • docs/quantization-guide.md: documents the field, the default, and the measured effect.
  • tests/test_gptq.py: masked H equals a float64 X^T X oracle over retained rows; the default and flattened-input paths keep the historical accumulation; the GPTAQ/FOEM and QQQ combinations are rejected.

Measured evidence

Per-token Hessian at layers.1.mlp.down_proj (32 wikitext-2 train articles, 512 tokens, one BOS each):

model H_bos share BOS norm mean rest norm median ratio
Llama-3.2-1B-Instruct 99.01% 1159.19 1.71 679.6x
Llama-3.2-1B base 99.97% 623.71 1.85 336.6x

Wikitext-2 ppl (40 test articles, one BOS each, 512-token rows, 17,566 predicted tokens):

model fp16 RTN GPTQ off GPTQ on
Llama-3.2-1B-Instruct 18.21 22.06 23.21 19.51
Llama-3.2-1B base 12.39 15.75 13.39 14.32
SmolLM2-135M 20.22 32.05 27.60 28.22

Control on Instruct: enabling the flag but dropping the last token instead stays at 23.22, so the 16% gain is specific to the leading sink, not to removing an arbitrary row. With the flag on, GPTQ <= RTN on all three models.

Default stays off: base Llama and SmolLM2 regress slightly (both still beat RTN), so default-on is not strictly better and the historical Hessian is preserved.

Tests

PYTHONPATH=/root/work-4393 /root/venv/bin/python -m pytest tests/test_gptq.py -q
72 passed, 3 skipped
PYTHONPATH=/root/work-4393 /root/venv/bin/python -m pytest tests/test_hessian_fallback_contracts.py -q
13 passed
PYTHONPATH=/root/work-4393 /root/venv/bin/python -m pytest \
  tests/test_config_serialization_roundtrip_4201.py tests/test_qqq_config.py -q
32 passed
/root/venv/bin/python -m ruff check gptqmodel/quantization/config.py gptqmodel/quantization/gptq.py tests/test_gptq.py
All checks passed!

Not executed: the issue's exact 63.09-vs-15.90 gap did not reproduce on current main (base GPTQ off measured 13.39, already better than RTN 15.75), though the 99.97% BOS Hessian dominance does. Gated meta-llama/* checkpoints were not downloadable (no HF token); unsloth/Llama-3.2-1B{,-Instruct} and cached SmolLM2-135M were used. Reload used backend="gptq_torch" because Marlin JIT needs CUDA_HOME (unset here).

Logic Flow Documentation

The new flag is a calibration-only opt-in with a single decision point (_attention_sink_row_keep) and an unchanged default path; the mechanism, the rejected combinations and the measured effect are documented in docs/quantization-guide.md. No existing decision mechanism or selection order changed.

Review Requirements

  • Reviewed; config/API only, no env-var switch.
  • Default path pinned bit-for-bit against the historical Hessian.

AI Agent and others added 2 commits November 16, 2026 15:30
…4393)

Tokenizers that prepend BOS make the leading position an attention sink:
on Llama-3.2-1B its activation norm at layers.1.mlp.down_proj is ~337-680x
the median row norm, so that single row contributes 99.0-99.97% of the
input Hessian Frobenius norm. GPTQ then optimizes almost entirely for it.

Add GPTQConfig.exclude_attention_sinks (default False). When enabled,
process_batch drops the first token of every 3-D linear/Conv1D calibration
sequence before accumulating H, and the sample count is reduced to match.
Flattened routed-expert batches have no sequence axis and are untouched.
The flag is serialized in checkpoint meta and round-trips through
from_quant_config. QQQConfig rejects it because QQQ has its own capture
path.

Measured on wikitext-2 (40 articles, one BOS each, 512-token rows):
Llama-3.2-1B-Instruct GPTQ off 23.21 -> on 19.51 (RTN 22.06, fp16 18.21).
Dropping the last token instead (control) stays at 23.22, so the gain is
specific to the attention sink. Default is unchanged because base
Llama-3.2-1B (13.39 -> 14.32) and SmolLM2-135M (27.60 -> 28.22) regress
slightly; both remain better than RTN.

Unit tests assert the masked Hessian equals a float64 X^T X oracle over
the retained rows, and that the default and flattened-input paths keep the
historical accumulation.
GPTAQ and FOEM override process_batch with their own Hessian/dXXT
accumulation, so exclude_attention_sinks would be silently ignored. Reject
the combination in GPTQConfig.validate() and cover it with a test.
@Qubitium
Qubitium merged commit 8d6ebb0 into main Nov 16, 2026
9 checks passed
@Qubitium
Qubitium deleted the fix/gptq-bos-quality-4393 branch November 16, 2026 15:37
Qubitium added a commit that referenced this pull request Nov 16, 2026
…4393) (#4444)

Llama-3.2-1B-Instruct's BOS row is an attention sink that holds ~99% of the
calibration Hessian (#4393). With the opt-in from #4440 enabled, the slow
model test improves on both metrics and passes against its own baselines
instead of failing or relying on the stale-baseline native fallback:

  slow mode, GPTQ 4-bit g128     native bf16  sinks kept  sinks excluded
  gsm8k_platinum_cot acc,num     0.4797       0.4078      0.4127
  arc_challenge acc              0.3183       0.3038      0.3063
  arc_challenge acc_norm         0.3456       0.3456      0.3456

The previous slow baselines (0.3987 / 0.3234 / 0.3643) no longer matched this
eval setup: even the native model scores 0.3183 / 0.3456 on ARC. With sinks
kept, arc acc was 93.9% of the stored value (fail) and 95.4% of native.

- model_test: add EXCLUDE_ATTENTION_SINKS (default False); forwarded to the
  quantize config only when True, so other methods never see the field.
- test_llama3_2: enable it and set the slow baselines to the measured
  sink-excluded quantized scores.

Co-authored-by: Claude Opus 5.5 <[email protected]>
Qubitium added a commit that referenced this pull request Nov 17, 2026
…mask (#4393)

Move the attention-sink opt-in from `GPTQConfig.exclude_attention_sinks` to
the calibration config, `quantize(..., calibration_exclude_attention_sinks=True)`
(`CalibrationConfig.exclude_attention_sinks`), and apply it once, in the
shared calibration keep-mask, so every calibration method drops the same rows.

Rule: every attended BOS token is excluded (the leading one, after left or
right padding, and each document-boundary BOS of concatenated calibration).
Nothing else is: a tokenizer without a BOS token (Qwen2/2.5/3) keeps every
row and its checkpoint is bitwise identical to the default run (#4459).

- `LoopProcessor.calibration_keep_mask` edits the padding mask per batch;
  `_mask_tls.padding_value` keeps the padding-only mask for forward-visible
  consumers (MoE padding zeroing, NVFP4 amax, EXL3/DeepSeek-V4 router
  evidence). Token totals and the zero-sample threshold use the retained rows.
- Consumers: GPTQ/GPTAQ/FOEM/native capture, QQQ, MACaQ, EXL3 capture, AWQ
  features and token-row structure, calibrated FP8 GSQ. Natively routed
  experts and VLM batches whose sequence cannot be placed keep their rows.
- VPTQ rejects the option (PPL 4.5-4.7x worse on Llama-3.2-1B/-Instruct);
  ParoQuant and staged GSQ reject it; `quantize()` rejects a
  `dynamic[...].exclude_attention_sinks` rule.
- Remove the GPTQConfig field and its per-method plumbing from #4440,
  #4525, #4526 and #4610 (`attention_sink_mask_supported`, factory/dynamic/load
  sanitizers, `GPTQ._attention_sink_row_keep`, `attention_sink_bos_flags`).
  `GPTQConfig(exclude_attention_sinks=...)` now raises `TypeError`.
- Supersede the GPTQ-only #4568 fix (#4610): the BOS positions are resolved
  from the prepared `input_ids` and `tokenizer.bos_token_id` when the
  processor is built, before `release_calibration_dataset()`, for every
  calibration method; #4610's per-batch decision cache in `GPTQProcessor`,
  its dynamic gating and its Hessian-share signature field are removed.
- Provenance: `meta.calibration_exclude_attention_sinks: true` only when on.
- test_llama3_2: forward the switch through the new kwarg and re-baseline the
  slow metrics for the new rule.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Qubitium added a commit that referenced this pull request Nov 17, 2026
…mask (#4393) (#4663)

* feat(calibration): exclude attention sinks in the shared calibration mask (#4393)

Move the attention-sink opt-in from `GPTQConfig.exclude_attention_sinks` to
the calibration config, `quantize(..., calibration_exclude_attention_sinks=True)`
(`CalibrationConfig.exclude_attention_sinks`), and apply it once, in the
shared calibration keep-mask, so every calibration method drops the same rows.

Rule: every attended BOS token is excluded (the leading one, after left or
right padding, and each document-boundary BOS of concatenated calibration).
Nothing else is: a tokenizer without a BOS token (Qwen2/2.5/3) keeps every
row and its checkpoint is bitwise identical to the default run (#4459).

- `LoopProcessor.calibration_keep_mask` edits the padding mask per batch;
  `_mask_tls.padding_value` keeps the padding-only mask for forward-visible
  consumers (MoE padding zeroing, NVFP4 amax, EXL3/DeepSeek-V4 router
  evidence). Token totals and the zero-sample threshold use the retained rows.
- Consumers: GPTQ/GPTAQ/FOEM/native capture, QQQ, MACaQ, EXL3 capture, AWQ
  features and token-row structure, calibrated FP8 GSQ. Natively routed
  experts and VLM batches whose sequence cannot be placed keep their rows.
- VPTQ rejects the option (PPL 4.5-4.7x worse on Llama-3.2-1B/-Instruct);
  ParoQuant and staged GSQ reject it; `quantize()` rejects a
  `dynamic[...].exclude_attention_sinks` rule.
- Remove the GPTQConfig field and its per-method plumbing from #4440,
  #4525, #4526 and #4610 (`attention_sink_mask_supported`, factory/dynamic/load
  sanitizers, `GPTQ._attention_sink_row_keep`, `attention_sink_bos_flags`).
  `GPTQConfig(exclude_attention_sinks=...)` now raises `TypeError`.
- Supersede the GPTQ-only #4568 fix (#4610): the BOS positions are resolved
  from the prepared `input_ids` and `tokenizer.bos_token_id` when the
  processor is built, before `release_calibration_dataset()`, for every
  calibration method; #4610's per-batch decision cache in `GPTQProcessor`,
  its dynamic gating and its Hessian-share signature field are removed.
- Provenance: `meta.calibration_exclude_attention_sinks: true` only when on.
- test_llama3_2: forward the switch through the new kwarg and re-baseline the
  slow metrics for the new rule.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(gptq): keep process_batch signature compatible with its overrides

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
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.

1 participant