Skip to content

fix(qqq): honour damp_auto_increment in Hessian recovery - #5932

Merged
Qubitium merged 2 commits into
mainfrom
fix/5858-qqq-damp-recovery
Nov 20, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/5858-qqq-damp-recovery

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

QQQConfig accepts and serializes damp_auto_increment, but the QQQ solve
made exactly one damping attempt: a rank-deficient Hessian raised
torch.linalg.cholesky instead of retrying at the next candidate. Starting the
same fixture at damp_percent=0.01 succeeded, which is exactly the candidate
the configured increment describes.

Extract GPTQ's damping-recovery policy (configured damp probe, additive
damp_auto_increment grid, diagonal floor) into a module-level
recover_damped_hessian_factor helper and call it from QQQ. QQQ keeps its own
inverse-factor convention (lower Cholesky factor, then the inverse's upper
factor), so only the damping decision is shared. A zero increment remains the
explicit no-recovery control.

Fixes #5858

What Changed

  • gptqmodel/quantization/gptq.py: move the existing hessian_inverse
    damping body into recover_damped_hessian_factor(H, *, damp_percent, damp_auto_increment, name, npu_block_size) -> (factor | None, used_damp).
    GPTQ behaviour is unchanged; the helper restores the Hessian diagonal
    between candidates and only mutates it on the final floor attempt.
  • gptqmodel/quantization/qqq.py: call the shared helper with percdamp and
    self.qcfg.damp_auto_increment. The CUDA OOM retry is preserved (restore the
    original diagonal, move H/W to the host, re-run recovery there). A
    numerical failure that exhausts recovery uses the existing sample-count
    fallback when configured and otherwise raises torch._C._LinAlgError
    without moving to CPU. The reported damp is the one actually applied.
  • docs/quantizer-moe.md#41-damping-recovery-inside-the-hessian-solve:
    document the shared recovery flow and the QQQ/GPTQ split.
  • Tests: new tests/test_qqq_damp_recovery_5858.py; the existing [MEM] MoE/OOM: shared-input Hessian adoption keeps 3 C^2 per pair (+0.96 GiB/20% peak on Qwen1.5-MoE), quantized MoE reload materializes bf16 experts on host (12.2 vs 4.2 GiB RSS), OOM CPU fallback missing in QQQ/EXL3 and after GPTQ inverse #4400
    numerical-failure test now drives cholesky_ex to info != 0 on every CUDA
    probe so recovery is genuinely exercised before the expected failure.

Out of scope: no change to GPTQ's selection order or to the sample-count
fallback policy itself.

Tests

env GPTQ_BYPASS_SAFETY_CHECKS=1 PYTHONPATH=. \
  /root/venv-tf523/bin/python -m pytest \
  tests/test_qqq_damp_recovery_5858.py \
  tests/test_qqq_exl3_oom_fallback_4400.py \
  tests/test_gptq_damp_grid_budget_5790.py \
  tests/test_gptq_post_inverse_fallback_4400.py \
  tests/test_gptq_hessian_alloc_fallback_4350.py \
  tests/test_hessian.py -q
# 61 passed, 2 skipped (the two skips need multiple GPUs)

bash format/check.sh
# All checks passed!

git diff --check origin/main...HEAD
# clean

The new coverage fails on the parent commit (LinAlgError where recovery is
expected), and the #4400 rewrite fails on this branch if the CPU fallback
message appears for a numerical failure.

Measured Impact

Conditions: aarch64, torch 2.11.0+cu130, CPU FP16, torch.set_num_threads(1).
The before/after runs use the issue's exact script: tiny Llama, 128 hidden
size, one layer, 32 calibration tokens, QQQConfig(bits=4, group_size=-1, fallback=None, device="cpu", offload_to_disk=False, damp_percent=1e-10, damp_auto_increment=0.01) and the model.layers.0.self_attn.q_proj target.
The control run uses an explicit damp_percent=0.01 with the same weights.

Metric (unit) Before After Change (after - before)
Issue fixture QQQ solve torch._C._LinAlgError (leading minor 34) completes recovery engaged
Applied damp reported by QQQ n/a (aborted) 0.01000 n/a
Recovered state dict vs explicit damp_percent=0.01 control n/a bitwise identical (max abs diff 0.0) exact
Healthy-layer damp probes 1 1 0 (no recovery log)
Indefinite fixture with damp_auto_increment=0 LinAlgError LinAlgError unchanged control

Logic Flow Documentation

The shared flow is documented with shipped symbol names in
docs/quantizer-moe.md#41-damping-recovery-inside-the-hessian-solve,
which repeats this diagram:

flowchart TD
    A["QQQ._quantize_impl / GPTQ.hessian_inverse"] --> B["recover_damped_hessian_factor(H)"]
    B --> C{"try_factor at damp_percent"}
    C -- success --> S["return factor, damp_percent"]
    C -- failure --> D{"damp_auto_increment == 0"}
    D -- yes --> E["try diagonal floor"]
    D -- no --> F["probe damp_percent + increment"]
    F -- success --> S2["return factor, candidate damp"]
    F -- failure --> G["walk additive grid, endpoint first"]
    G -- success --> S2
    G -- failure --> E
    E -- success --> S3["return factor, floor candidate"]
    E -- failure --> N["return None, 1.0"]
    N --> R["QQQ: configured fallback or LinAlgError;<br/>GPTQ: existing no-Hessian fallback"]
    style S fill:#d1e7dd,stroke:#0f5132,color:#0f5132
    style S2 fill:#d1e7dd,stroke:#0f5132,color:#0f5132
    style S3 fill:#d1e7dd,stroke:#0f5132,color:#0f5132
    style N fill:#f9d5d5,stroke:#af3b3b,color:#222
    style R fill:#f9d5d5,stroke:#af3b3b,color:#222
Loading
  • The new-flow diagram uses the shipped symbols (recover_damped_hessian_factor,
    try_factor, damp_auto_increment, QQQ._quantize_impl,
    GPTQ.hessian_inverse).
  • The failure path (recovery exhausted) is visually distinct from success.
  • The reference page under docs/ repeats the diagram and is already
    linked from README.md via the quantizer calibration flows link.

Review Requirements

I or an AI agent (such as Codex or Claude) have:

  • 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.

Notes

  • GPTQ's hessian_inverse now resolves npu_block_size only when the Hessian
    is on an NPU, matching the old lazy lookup.
  • The helper restores the Hessian diagonal after every probe, so QQQ no longer
    leaves the applied damping in H; H is released before the block pass
    either way.
  • The new test_qqq_numerical_failure_does_not_retry_on_cpu replaces a
    one-shot torch.linalg.cholesky failure with an always-indefinite
    cholesky_ex: with recovery enabled, a one-shot failure is expected to be
    recovered, so it no longer proves the no-CPU-fallback contract.

QQQConfig accepted and serialized damp_auto_increment, but the QQQ
solve made exactly one damping attempt: a rank-deficient Hessian raised
from torch.linalg.cholesky instead of retrying at the next candidate.

Extract GPTQ's damping-recovery policy (configured damp probe, additive
grid, diagonal floor) into a module-level helper and call it from QQQ,
which keeps its own inverse-factor convention. QQQ now reports the damp
it actually applied, and a zero increment remains the explicit
no-recovery control.

The CUDA OOM retry still restores the original diagonal, moves H/W to
the host, and re-runs recovery there. A numerical failure that exhausts
recovery raises torch._C._LinAlgError without falling back to CPU.

Tests: rank-deficient recovery matches an explicit damp control bit for
bit; healthy layers do not start recovery; applied damp is reported;
zero increment fails; a large-enough grid recovers an indefinite matrix.
The existing #4400 numerical-failure test now uses cholesky_ex info != 0
so the grid and floor are exercised before the expected failure.

Fixes #5858
@Qubitium
Qubitium marked this pull request as ready for review November 20, 2026 15:36
@Qubitium
Qubitium merged commit e869623 into main Nov 20, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/5858-qqq-damp-recovery branch November 20, 2026 15:36
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.

[Audit][QQQ] Configured damping recovery is ignored after a Cholesky failure

1 participant