Skip to content

fix(gptq): share RTN fallback and preserve act-order retries (#4179) - #4248

Merged
Qubitium merged 2 commits into
mainfrom
fix/issue-4179-hessian-fallbacks
Nov 16, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/issue-4179-hessian-fallbacks

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4179

TL;DR

GPTAQ and FOEM now share GPTQ's data-independent RTN fallback instead of dereferencing a None inverse, a zero-sample module can no longer ship an all-zero weight, the NaN-loss retry keeps activation order, GPTAQ/FOEM honour act_group_aware and tensor-parallel padding, FOEM counts tokens, and RTN keeps scales inside the FP16 storage range. Net diff: 11 files, +715/-25.

1. The problem

hessian_inverse returns (None, 1.0) when every damp and diagonal-floor attempt fails. GPTQ handles that with its no-Hessian RTN loop; GPTAQ and FOEM dereferenced the missing inverse:

  • GPTAQ: AttributeError: 'NoneType' object has no attribute 'T' at gptqmodel/quantization/gptaq.py:242 on the parent commit.
  • FOEM: TypeError: 'NoneType' object is not subscriptable at gptqmodel/quantization/foem.py:350 on the parent commit.

A zero-sample module with fallback disabled reached _neutralize_dead_columns with H = 0. Every column looked calibration-dead, so the whole weight became zero; the only signal was avg_loss = 999999999.

The NaN retry captured retry_hessian_diag after desc_act / act_group_aware had permuted H (old gptqmodel/quantization/gptq.py:2397). The retry rebuilds H = diag(permuted_diag) but clones W in natural order, so argsort of the already-sorted diagonal was the identity and g_idx was not act-ordered.

GPTAQ/FOEM ignored act_group_aware=True while the checkpoint meta recorded it, did not truncate _tp_pad_cols, and FOEM counted forward sequences in nsamples. RTN's SmoothMSE path used the 1e-8 default instead of SCALE_STORAGE_FLOOR, and its SmoothRowCol path did not divide the storage floor by the restoring factor.

2. The new fallback and retry flow

flowchart TD
    A["GPTQ._quantize_impl"] --> Z{"nsamples == 0"}
    Z -- yes --> R["_fallback_quantize(FallbackStrategy.RTN)<br/>WARN names the module"]
    Z -- no --> T{"fallback threshold reached"}
    T -- yes --> F["configured fallback strategy"]
    T -- no --> H["finalize_hessian; neutralize dead columns"]
    H --> P["desc_act / act_group_aware permutation"]
    P --> I["hessian_inverse"]
    I -- "None in GPTQ" --> L["inline data-independent RTN loop"]
    I -- "None in GPTAQ/FOEM" --> G["_rtn_fallback_after_hessian_failure -><br/>_fallback_quantize(RTN)"]
    I -- "factor present" --> Q["blockwise GPTQ update"]
    Q --> N{"avg_loss is NaN and fallback configured"}
    N -- yes --> M["restore natural-order retry diagonal;<br/>mock_quantization retry"]
    N -- no --> O["return Q, scale, zero, g_idx"]
    R --> O
    F --> O
    L --> O
    G --> O
    M --> O
    style R fill:#d1e7dd,stroke:#0f5132,color:#0f5132
    style G fill:#d1e7dd,stroke:#0f5132,color:#0f5132
    style M fill:#d1e7dd,stroke:#0f5132,color:#0f5132
Loading
flowchart LR
    A["natural-order H diagonal"] --> B["capture retry_hessian_diag"]
    B --> C["permute H and W"]
    C --> D["primary quantization"]
    D --> E{"NaN loss?"}
    E -- yes --> F["H = diag(captured natural diagonal)"]
    F --> G["retry re-runs neutralize + permutation"]
    G --> H["same g_idx / column order"]
    style H fill:#d1e7dd,stroke:#0f5132,color:#0f5132
Loading
Situation Before After
hessian_inverse returns (None, 1.0) gptaq.py:226 / foem.py:271 dereferenced None and aborted the run GPTAQ/FOEM call GPTQ._rtn_fallback_after_hessian_failure, which delegates to _fallback_quantize(RTN); GPTQ keeps its inline no-Hessian loop
nsamples == 0, fallback disabled zero Hessian zeroed the whole weight; avg_loss = 999999999 zero-sample guard forces RTN and logs a WARN naming the module
NaN retry with desc_act retry diagonal captured after permutation; g_idx not act-ordered retry diagonal captured before permutation; same g_idx as the primary path
act_group_aware=True on GPTAQ/FOEM ignored; checkpoint meta still said True same GAR permutation as GPTQ
Tensor-parallel padding on GPTAQ/FOEM reshape failed on padded columns padding truncated before reshape and metadata export
FOEM.nsamples forward sequences calibration tokens
RTN SmoothMSE / SmoothRowCol 1e-8 scale floor; restored scale could underflow FP16 SCALE_STORAGE_FLOOR; scale_min = floor / scale_factor

3. Per-item changes

  1. Shared RTN fallback — gptqmodel/quantization/gptq.py:1757 adds GPTQ._rtn_fallback_after_hessian_failure, which calls the existing _fallback_quantize(FallbackStrategy.RTN, ...). gptaq.py:270 and foem.py:331 call it when Hinv is None.
  2. Zero-sample guard — gptq.py:2236 forces FallbackStrategy.RTN whenever nsamples == 0, before the threshold decision, and logs a WARN naming the module. This is deliberately unconditional: fallback=None disables the threshold policy, not the guard against an all-zero weight. docs/quantization_protocol.md records that contract.
  3. Retry ordering — gptq.py:2311 captures retry_hessian_diag from the natural-order diagonal before neutralization/permutation. The retry restores that diagonal and recomputes the same permutation as the primary path.
  4. GPTAQ/FOEM ordering and TP padding — both now mirror GPTQ's act_group_aware permutation, static-group mapping, scale/zero reordering, and _tp_pad_cols truncation (gptaq.py:241,363,379,401; foem.py:300,343,367,478,492,514). foem.py:210 counts inp.shape[1] tokens for nsamples and the Hessian recurrence. The MLX FOEM path is skipped for act_group_aware/TP-padded modules so the Torch path handles them.
  5. RTN scale floor — rtn.py:217 passes eps=SCALE_STORAGE_FLOOR to mse_optimal_quant; rtn.py:228 passes scale_min = SCALE_STORAGE_FLOOR / scale_factor to find_params.

Docs: docs/quantizer-moe.md, docs/gptq-mock-group-flow.md, docs/quantization-scale-storage.md, docs/quantization_protocol.md, and a README link.

4. Before / after evidence

Parent commit 08f1b2b73 with the new test file copied in:

tests/test_hessian_fallback_contracts.py::test_non_invertible_hessian_uses_the_shared_rtn_fallback[gptaq]
E   AttributeError: 'NoneType' object has no attribute 'T'
tests/test_hessian_fallback_contracts.py::test_non_invertible_hessian_uses_the_shared_rtn_fallback[foem]
E   TypeError: 'NoneType' object is not subscriptable

Zero-sample reproduction (fallback=None, nsamples=0):

before: item2 Q_max=0.000000 nonzero=0 avg_loss=999999999
after:  item2 Q_max=29.866669 nonzero=32 avg_loss=fallback(rtn): 0.6958331

Retry ordering (desc_act=True, natural-order reference):

before: item3 reference_g=[1, 1, 1, 1, 0, 0, 0, 0] retry_g=[0, 0, 0, 0, 1, 1, 1, 1] equal=False
after:  item3 reference_g=[1, 1, 1, 1, 0, 0, 0, 0] retry_g=[1, 1, 1, 1, 0, 0, 0, 0] equal=True

The new suite has 13 cases. All 13 fail on parent 08f1b2b73; all 13 pass after the fix. The updated existing test tests/test_gptq_full_peak_memory.py::test_nan_retry_uses_original_hessian_diagonal[desc_act-cpu] now uses the natural-order reference and fails on parent, passes after.

5. Tests run

All commands use OMP_NUM_THREADS=4 MKL_NUM_THREADS=4 OPENBLAS_NUM_THREADS=4 NUMEXPR_NUM_THREADS=4.

CUDA venv /opt/venvs/cu130t/bin/python, CPU (no GPU kernel path changed):

PYTHONPATH=$PWD python -m pytest -q tests/test_hessian_fallback_contracts.py tests/test_gptq_mock_group_order.py tests/test_gptq_full_peak_memory.py tests/test_gptq_static_group_order.py tests/test_gptaq_foem_batch.py tests/test_quant_scale_overflow.py tests/test_smooth_mse_affine_range.py
-> 89 passed, 7 skipped

PYTHONPATH=$PWD python -m pytest -q tests/test_gptq.py -k "act_group_aware or neutralize_dead_columns or gptaq or embedding or replace_zero_scale"
-> 24 passed, 1 skipped, 48 deselected

ROCm venv /opt/venvs/rocm101t/bin/python with HIP_VISIBLE_DEVICES=0 only:

PYTHONPATH=$PWD python -m pytest -q tests/test_hessian_fallback_contracts.py
-> 13 passed

Lint:

ruff check gptqmodel docs/eora tests setup.py
-> All checks passed!

Parent-only pre-existing failures observed and not caused by this change: tests/test_fallback.py::test_fallback_near_dead_scale_survives_smoothing_and_packing[True-*-cpu], tests/test_weight_only.py::test_baseqmodel_quantize_uses_weight_only_rtn_pipeline, and tests/test_weight_only.py::test_gguf_forward_requests_dequantized_weight_in_input_dtype fail on 08f1b2b73 as well.

6. Residual risk / limits

  • GPTQ's own no-Hessian path remains its inline data-independent RTN loop; GPTAQ/FOEM use the shared _fallback_quantize(RTN) helper. Both are RTN, but the static-group inline path and _fallback_quantize are not bitwise identical. Keeping GPTQ's inline path avoids changing static-group behaviour.
  • The zero-sample guard intentionally overrides fallback=None/threshold=False. That is the documented safety contract; an all-zero weight is worse than a data-independent RTN grid.
  • MLX FOEM is macOS-only and is not exercised on this Linux host. The new guard routes act_group_aware/TP-padded FOEM through the Torch path, but that path was not run on Metal here.
  • The tests are CPU tensor tests. They exercise the quantizer logic, not CUDA/ROCm kernel arithmetic; no inference kernel was changed.

@Qubitium

Copy link
Copy Markdown
Collaborator Author

CI note: tests/test_awq_scaling_groups.py::test_scaling_groups_preserve_decoder_layer_output[nemotron_h] fails in the AWQ CPU (unit) job. I reproduced the same failure in a clean origin/main worktree at 5e8a24744; it is pre-existing and unrelated to this PR's GPTQ/GPTAQ/FOEM/RTN changes.

@Qubitium
Qubitium merged commit 60b420d into main Nov 16, 2026
8 of 9 checks passed
@Qubitium
Qubitium deleted the fix/issue-4179-hessian-fallbacks branch November 16, 2026 05:54
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.

[BUG] GPTAQ/FOEM crash on a non-invertible Hessian; unrouted MoE experts are zeroed with fallback disabled; NaN retry loses act-order

1 participant