Repository navigation
fix(gptq): share RTN fallback and preserve act-order retries (#4179) - #4248
Merged
Merged
Conversation
Collaborator
Author
|
CI note: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #4179
TL;DR
GPTAQ and FOEM now share GPTQ's data-independent RTN fallback instead of dereferencing a
Noneinverse, a zero-sample module can no longer ship an all-zero weight, the NaN-loss retry keeps activation order, GPTAQ/FOEM honouract_group_awareand 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_inversereturns(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:AttributeError: 'NoneType' object has no attribute 'T'atgptqmodel/quantization/gptaq.py:242on the parent commit.TypeError: 'NoneType' object is not subscriptableatgptqmodel/quantization/foem.py:350on the parent commit.A zero-sample module with fallback disabled reached
_neutralize_dead_columnswithH = 0. Every column looked calibration-dead, so the whole weight became zero; the only signal wasavg_loss = 999999999.The NaN retry captured
retry_hessian_diagafterdesc_act/act_group_awarehad permutedH(oldgptqmodel/quantization/gptq.py:2397). The retry rebuildsH = diag(permuted_diag)but clonesWin natural order, soargsortof the already-sorted diagonal was the identity andg_idxwas not act-ordered.GPTAQ/FOEM ignored
act_group_aware=Truewhile the checkpoint meta recorded it, did not truncate_tp_pad_cols, and FOEM counted forward sequences innsamples. RTN'sSmoothMSEpath used the1e-8default instead ofSCALE_STORAGE_FLOOR, and itsSmoothRowColpath 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:#0f5132flowchart 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:#0f5132hessian_inversereturns(None, 1.0)gptaq.py:226/foem.py:271dereferencedNoneand aborted the runGPTQ._rtn_fallback_after_hessian_failure, which delegates to_fallback_quantize(RTN); GPTQ keeps its inline no-Hessian loopnsamples == 0, fallback disabledavg_loss = 999999999desc_actg_idxnot act-orderedg_idxas the primary pathact_group_aware=Trueon GPTAQ/FOEMTrueFOEM.nsamplesSmoothMSE/SmoothRowCol1e-8scale floor; restored scale could underflow FP16SCALE_STORAGE_FLOOR;scale_min = floor / scale_factor3. Per-item changes
gptqmodel/quantization/gptq.py:1757addsGPTQ._rtn_fallback_after_hessian_failure, which calls the existing_fallback_quantize(FallbackStrategy.RTN, ...).gptaq.py:270andfoem.py:331call it whenHinv is None.gptq.py:2236forcesFallbackStrategy.RTNwhenevernsamples == 0, before the threshold decision, and logs a WARN naming the module. This is deliberately unconditional:fallback=Nonedisables the threshold policy, not the guard against an all-zero weight.docs/quantization_protocol.mdrecords that contract.gptq.py:2311capturesretry_hessian_diagfrom the natural-order diagonal before neutralization/permutation. The retry restores that diagonal and recomputes the same permutation as the primary path.act_group_awarepermutation, static-group mapping, scale/zero reordering, and_tp_pad_colstruncation (gptaq.py:241,363,379,401;foem.py:300,343,367,478,492,514).foem.py:210countsinp.shape[1]tokens fornsamplesand the Hessian recurrence. The MLX FOEM path is skipped foract_group_aware/TP-padded modules so the Torch path handles them.rtn.py:217passeseps=SCALE_STORAGE_FLOORtomse_optimal_quant;rtn.py:228passesscale_min = SCALE_STORAGE_FLOOR / scale_factortofind_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
08f1b2b73with the new test file copied in:Zero-sample reproduction (
fallback=None,nsamples=0):Retry ordering (
desc_act=True, natural-order reference):The new suite has 13 cases. All 13 fail on parent
08f1b2b73; all 13 pass after the fix. The updated existing testtests/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):ROCm venv
/opt/venvs/rocm101t/bin/pythonwithHIP_VISIBLE_DEVICES=0only:Lint:
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, andtests/test_weight_only.py::test_gguf_forward_requests_dequantized_weight_in_input_dtypefail on08f1b2b73as well.6. Residual risk / limits
_fallback_quantize(RTN)helper. Both are RTN, but the static-group inline path and_fallback_quantizeare not bitwise identical. Keeping GPTQ's inline path avoids changing static-group behaviour.fallback=None/threshold=False. That is the documented safety contract; an all-zero weight is worse than a data-independent RTN grid.act_group_aware/TP-padded FOEM through the Torch path, but that path was not run on Metal here.