Skip to content

fix(gptq): permute W once when the Hessian permutation OOMs (#4168) - #4230

Merged
Qubitium merged 2 commits into
mainfrom
fix/issue-4168-permute-once
Nov 16, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/issue-4168-permute-once

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4168

Root cause

GPTQ._quantize_impl permuted the weight before the Hessian in both ordering branches (gptqmodel/quantization/gptq.py:2295 desc_act, gptqmodel/quantization/gptq.py:2324 act_group_aware on main):

W = W[:, perm]
self.H = self.H[perm][:, perm]

The H gather allocates two cols x cols temporaries and is the step that OOMs; W is only rows x cols. When it raised, W was already permuted, so the except branch ran W = W.to(cpu)[:, perm] — permuting W a second time while permuting H once. The block loop then ran with mismatched column orders and only a WARN was logged.

Fix

Gather H first, then W, into locals, and commit both only in the else block, so the except path always starts from an unpermuted W and H. Applied to both branches:

try:
    H_permuted = self.H[perm][:, perm]
    W_permuted = W[:, perm]
except RuntimeError as exc:
    ...                          # W and self.H are still unpermuted here
else:
    W = W_permuted
    self.H = H_permuted

H is gathered first so the extra live W reference never overlaps the cols x cols H temporaries. Measured peak (max_memory_allocated up to the Hessian inverse, desc_act):

shape before after
cols=14336, rows=4096 (W 224 MB, H 784 MB) 2800 MB 2800 MB
cols=8192, rows=16384 (W 512 MB, H 256 MB) 1792 MB 2048 MB (+256 MB)

The first shape is the wide down_proj case that motivates the bug, and its peak is unchanged. The second adds W - H in the window after the H gather has already succeeded; a failure there still enters the CPU fallback with both tensors unpermuted, so it degrades to a slower correct path, not to corruption.

Quantizer consistency on the fallback path

Both branches already call self.quantizer.find_params(W, weight=True) after the fallback move, and it now receives the once-permuted W (asserted by the new test). find_params(weight=True) fits per-output-row ranges, which are invariant to a column permutation, and the block loop refits per group on the permuted W, so the quantizer state matches the tensor order on both paths. No change was needed; cpu_fallback_used still moves Q back to the module device at the end.

Proxy loss

tr(dW·H·dW^T) with an independent fp64 oracle Href = 2 * X.double().T @ X.double() / X.shape[0]. The issue snippet is unmodified except for thread caps (OMP/MKL/OPENBLAS/NUMEXPR_NUM_THREADS=4, torch.set_num_threads(4), torch.set_num_interop_threads(1)); thread count does not change GPU memory. RTX 4090, torch 2.16.0.dev+cu130:

Mode normal before OOM fallback before normal after OOM fallback after
desc_act 0.05526260304122839 21.65917248630882 0.05526260304122839 0.05526342977244515
act_group_aware 0.0758728365598003 21.703019188522543 0.0758728365598003 0.07587098126378558

AMD R9700, ROCm 10.1 (HIP_VISIBLE_DEVICES=1), after the fix:

Mode normal OOM fallback
desc_act 0.055268084063580074 0.055267929783744105
act_group_aware 0.07587283874309142 0.07587283767032228

Every "OOM fallback" run above is a real CUDA OOM from the issue's 40 MiB set_per_process_memory_fraction trick; each logged CUDA OOM during ... Hessian permutation ... falling back to CPU. The pre-fix numbers reproduce the issue's reported 21.66 / 21.70.

Regression test

tests/test_gptq_cuda.py::test_hessian_permutation_oom_fallback_permutes_w_once (desc_act and act_group_aware) injects RuntimeError("CUDA out of memory") from the first 1-D index of self.H instead of waiting for a real OOM, then asserts that the H seen by the Hessian inverse equals H_original[perm][:, perm] and that the W passed to find_params equals W_original[:, perm] for the same perm. On the parent commit (63e07a758) it fails with 98.4% / 100% mismatched elements; with the fix it passes.

Tests run

  • pytest tests/test_gptq_cuda.py tests/test_gptq.py tests/test_gptq_mock_group_order.py tests/test_gptq_hessian_pipeline.py -q (CUDA venv, RTX 4090) -> 242 passed, 2 skipped
  • pytest tests/test_gptq_cuda.py -q (ROCm venv, R9700) -> 152 passed
  • pytest tests/test_gptq_cuda.py::test_hessian_permutation_oom_fallback_permutes_w_once -q on the parent commit -> 2 failed
  • ruff check gptqmodel docs/eora tests setup.py -> All checks passed!

Residual risk

  • Only RuntimeError whose message contains "out of memory" on a CUDA-typed Hessian is handled, unchanged from before; other failures still propagate.
  • The fallback still finishes that module's quantization on CPU, which is slower than the GPU path.

The desc_act and act_group_aware blocks assigned `W = W[:, perm]` before
gathering the Hessian. A CUDA OOM in the H gather (two cols x cols
temporaries) therefore left W permuted, and the CPU fallback applied the
permutation a second time while permuting H once. The block loop then ran
with mismatched column orders and only a WARN was logged.

Gather H first, then W, into locals and commit both only after both
succeed, so the fallback always starts from an unpermuted W and H.
@Qubitium
Qubitium merged commit e9f7926 into main Nov 16, 2026
9 checks passed
@Qubitium
Qubitium deleted the fix/issue-4168-permute-once branch November 16, 2026 04:51
@Qubitium Qubitium self-assigned this Nov 16, 2026
@Qubitium

Copy link
Copy Markdown
Collaborator Author

Already fixed on main: merged as e9f7926 (fix(gptq): permute W only once when the Hessian permutation OOMs (#4168)). Verified at HEAD: the H-then-W gather with commit-on-success is in both ordering branches of quantization/gptq.py, and tests/test_gptq_cuda.py::test_hessian_permutation_oom_fallback_permutes_w_once is present. Closing as resolved.

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] CUDA OOM during desc_act / act_group_aware Hessian permutation permutes W twice → scrambled weights

1 participant