Skip to content

fix(gptq): cap sink-row Hessian contributions when desc_act disables act_group_aware (#4742) - #4909

Merged
Qubitium merged 1 commit into
mainfrom
root/descact-sink-4742
Nov 17, 2026
Merged

Qubitium merged 1 commit into
mainfrom
root/descact-sink-4742

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

GPTQConfig(bits=4, group_size=128, desc_act=True) on Llama-3.1-8B is worse
than RTN (PPL 11.62 vs 10.22) and 10/160 wikitext windows blow up to PPL
800-12000 whenever the first token after BOS is a delimiter. desc_act=True
turns act_group_aware off, so the BOS/delimiter sink rows dominate the
Hessian, the sink columns are ordered first with a tiny Hinv[i, i], and
their error is amplified into every correlated column
(layers.1.mlp.down_proj rel err ~5).

This caps each calibration row's norm before X^T X when desc_act has
disabled act_group_aware, per the issue's suggested fix. The cap keeps the
sink direction but bounds its weight in the Hessian. It is off for the default
act_group_aware path, which stays bit-for-bit identical.

What Changed

  • HessianConfig.sink_row_cap_median_multiple (default 4.0, 0/None
    disables): cap each calibration row's norm at min(p99.9, N x median)
    before X^T X.
  • GPTQ.resolve_sink_row_cap_scale() computes the per-row clamp scale
    chunk-by-chunk in fp32 (whole-batch threshold, so independent of
    HessianConfig.chunk_size); compute_hessian_xtx() applies it only when
    desc_act=True and act_group_aware=False. The uncapped fast path is
    untouched.
  • The new setting is written to meta.hessian (so a reload does not silently
    restore the default) and included in the shared-input Hessian capture
    signature.
  • Expanded the desc_act=True automatically disables act_group_aware warning
    to name the Llama-3 sink risk, exclude_attention_sinks, and the new knob.
  • Documented the mechanism, default, opt-out and the exclude_attention_sinks
    alternative in docs/quantization-guide.md; re-anchored the contract refs
    shifted by the new code.
  • New tests/test_descact_sink_4742.py (7 CPU-only tests, synthetic Hessian).

API/behavior: new optional HessianConfig field, default on for
desc_act=True. Default GPTQ is unchanged. Not addressed here: enabling
desc_act ordering together with group-aware scales (the issue's optional
third suggestion) and the 8B end-to-end PPL rerun (the checkpoint is not
cached in this environment).

Tests

New regression test, red on main (6ec93e632-era base) and green on the
branch:

# base worktree (origin/main + the new test copied in)
PYTHONPATH=/root/worktrees/root-4742-base python /tmp/root-rt.py tests/test_descact_sink_4742.py -q
# 7 failed, 2 warnings in 5.98s
#   assert 440.44 <= 2.64            (uncapped sink Hessian diagonal)
#   assert 0.7828 < 0.5              (worst column rel err, uncapped)
#   AttributeError: 'GPTQ' object has no attribute 'sink_row_cap_median_multiple'

# branch
PYTHONPATH=/root/worktrees/root-4742 python /tmp/root-rt.py tests/test_descact_sink_4742.py -q
# 7 passed, 2 warnings in 7.77s

Synthetic Hessian (256 cols, 2 sink rows at 700x, 64 rows) worst column rel
err: uncapped 0.783, capped (default 4x) 0.388; dropping the sink rows
entirely is ~0.31, so the cap recovers most of the protection without
removing calibration data.

Real-model check on cached HuggingFaceTB/SmolLM2-135M (1577 calibration
tokens, first token after BOS forced to " and "), per-column rel err of
three mlp.down_proj layers (worst column; rows clamped by the cap):

layer desc_act, cap off desc_act, cap default default (AGA) rows clamped
10 0.3567 0.3476 0.3484 15/1577
20 0.2682 0.2595 0.3533 1/1577
29 0.4419 0.4094 0.3923 8/1577

Mean column error moves <1% and output-relative error is unchanged or lower.
SmolLM2's sink is mild, so this only shows the cap is a no-op-ish, monotone
improvement there; the 8B checkpoint is not cached, so the PPL table above is
from the issue.

Regression runs (CUDA device 5):

python /tmp/root-rt.py tests/test_quantize_config_offload_tempdir.py tests/test_quantize_in_session_4381.py \
  tests/test_quantize_run_summary.py tests/test_quantized_embeddings.py tests/test_gptq.py \
  tests/test_gptq_fallback_offset.py tests/test_gptq_hessian_accumulation.py \
  tests/test_gptq_hessian_alloc_fallback_4350.py tests/test_gptq_hessian_memory.py \
  tests/test_gptq_hessian_pipeline.py tests/test_gptq_keep_mask_device.py \
  tests/test_gptq_mock_group_order.py tests/test_gptq_post_inverse_fallback_4400.py \
  tests/test_gptq_shared_hessian.py tests/test_gptq_static_group_order.py \
  tests/test_gptq_workspace_release.py tests/test_hessian.py \
  tests/test_calibration_attention_sinks.py tests/test_descact_sink_4742.py -q
# 444 passed, 6 skipped, 1 failed

python /tmp/root-rt.py tests/test_gptq_cuda.py -q
# 152 passed

python /tmp/root-rt.py tests/test_docs_contract_refs.py tests/test_descact_sink_4742.py -q
# 27 passed

python /tmp/root-rt.py tests/test_serialization.py tests/qcfg/ tests/test_shared_input_hessian_dedup.py \
  tests/test_gptq_hessian_pipeline.py -q
# 219 passed, 2 failed

The failures are pre-existing and unrelated:

Lint: ruff check gptqmodel/quantization/gptq.py gptqmodel/quantization/config.py gptqmodel/looper/gptq_processor.py tests/test_descact_sink_4742.py — all checks passed.

  • Unit tests have been executed and passed, or the reason they are not applicable is documented.
  • I added a new simple/fast unit test for this change, or documented why that is not applicable.

Model Support / Module Plan

Not applicable: no model definitions, MODEL_MAP entries, or module plans
change.

Logic Flow Documentation

The change adds a bounded preconditioning step to an existing flow
(compute_hessian_xtx) rather than replacing a decision mechanism or
tensor lifecycle. No new failure/ambiguity contract is introduced: a
non-finite norm, a zero threshold, or an all-ones scale falls back to the
existing uncapped path, and the default configuration keeps the existing
fast path. docs/quantization-guide.md documents the new knob and when it
applies; no new reference page is warranted.

Review Requirements

  • Reviewed this PR.
  • Checked that the code matches existing project structure, APIs, and conventions.
  • Avoided unnecessary monkeypatching and used the project's normal extension points where possible.
  • Minimized and compacted the impacted code surface.
  • Considered and eliminated potential regressions (default path bit-identical, covered by test).

Kernel Accuracy Requirements

Not applicable: no kernel, fusion, or packed-storage change. The Hessian math
stays in the existing fp32 quant_blas_backend GEMM; only row scaling is
added, and the new test checks the capped Hessian against a float64
X^T X of the row-clamped activations.

Notes

The cap is a mitigation, not a guarantee; the docs still say to validate a
checkpoint per model, and CalibrationConfig(exclude_attention_sinks=True)
remains available. The 8B PPL rerun from the issue could not be repeated here
because that checkpoint is not in the local HF cache (16 GB download).

…act_group_aware (#4742)

`desc_act=True` switches `act_group_aware` off, and with it the per-group
scale protection that keeps a handful of massive-activation ("attention
sink") calibration rows from dominating `X^T X`. On Llama-3.1-8B the BOS and
leading-delimiter rows then own the largest Hessian diagonal, `desc_act`
quantizes those columns first with a tiny `Hinv[i, i]`, and the error
saturates every column correlated with the sink: `layers.1.mlp.down_proj`
columns come out at rel err ~5 and 10/160 wikitext windows reach PPL
800-12000 (PPL 11.62 vs RTN 10.22).

Cap each calibration row's norm at `min(p99.9, N x median)` before `X^T X`,
with `N = HessianConfig.sink_row_cap_median_multiple` (default 4.0, 0/None
disables). The cap keeps the sink direction but bounds its weight, so the
columns correlated with it are no longer ordered first against a degenerate
`Hinv[i, i]`. It applies only when `desc_act` has disabled
`act_group_aware`; the default GPTQ path keeps the existing fast path and is
bit-for-bit unchanged. The setting is part of the serialized Hessian meta
and of the shared-input Hessian capture signature.

Also document the mechanism and the cap in the quantization guide, expand
the auto-disable warning to name the sink risk, `exclude_attention_sinks`
and the new knob, and re-anchor the contract refs shifted by the new code.

Tests: tests/test_descact_sink_4742.py (7 new CPU-only tests) fail 7/7 on
main and pass 7/7 here.
@Qubitium
Qubitium merged commit 7edbca8 into main Nov 17, 2026
9 checks passed
@Qubitium
Qubitium deleted the root/descact-sink-4742 branch November 17, 2026 18:18
sLaffey pushed a commit to sLaffey/GPTQModel that referenced this pull request Nov 18, 2026
ModelCloud#4976)

`resolve_sink_row_cap_scale` ran on every `add_batch` of every module when
`desc_act=True`, and it read the host four times per call:
`bool(isfinite(norms).all())`, `bool(isfinite(threshold))`,
`float(threshold)` and `bool((scale >= 1).all())`. `add_batch` runs inside the
layer forward hooks, so each read drained the CUDA queue mid-calibration.
When a cap applied it also left the single fp32 `X^T X` GEMM for the leased
chunk-workspace path (`chunk_size = rows`).

Compute the whole cap decision with tensor ops and fold the per-row scale into
the existing fp32 fast path:

* `resolve_sink_row_cap_threshold` derives `min(p99.9, N x median)` from one
  device sort of the row norms and turns a non-finite/non-positive threshold
  (where the old code returned `None`) into `+inf` through `torch.where`;
* `_sink_row_cap_scale_from_threshold` builds the per-row clamp on device;
* `compute_hessian_xtx` multiplies the fp32 staging copy by the scale instead
  of switching to the chunk lease, so a capped batch keeps the single-GEMM
  path. The multiply is out of place when the input is already fp32, because
  `matrix.to(float32)` then returns the caller's (possibly shared) tensor.

The cap is still decided per batch, so the Hessian is bitwise identical to the
pre-ModelCloud#4950 formula for one batch and for a batch sequence. Caching the threshold
on the first batch was tried and rejected: it changed the quantized Qwen2.5-0.5B
checkpoint (wikitext PPL 20.78 vs 20.32).

`test_descact_cap_sync_4950.py` pins the absence of per-`add_batch` host reads,
the fast-path choice, the first-batch cap and the bitwise H. It fails on
`main` (16 host reads for 4 batches, no device-only threshold resolver, one
chunk lease per capped batch) and passes here.

Qwen2.5-0.5B-Instruct, 4-bit/gs128/desc_act, 128 C4 rows, fp16, shared PG506
(two runs each, the box is 80-92 GiB of 98 GiB busy): region "Forward" over
21504 calls 12.2-23.1 s -> 8.5-8.7 s, `quantize()` 79.9-174.8 s -> 45.7-60.2 s,
wikitext-2 PPL unchanged at 20.319 (pre-ModelCloud#4909: Forward 2.53 s, PPL 20.278).
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