Repository navigation
fix(gptq): permute W once when the Hessian permutation OOMs (#4168) - #4230
Merged
Merged
Conversation
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.
Collaborator
Author
|
Already fixed on main: merged as e9f7926 ( |
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 #4168
Root cause
GPTQ._quantize_implpermuted the weight before the Hessian in both ordering branches (gptqmodel/quantization/gptq.py:2295desc_act,gptqmodel/quantization/gptq.py:2324act_group_aware on main):The H gather allocates two
cols x colstemporaries and is the step that OOMs;Wis onlyrows x cols. When it raised,Wwas already permuted, so theexceptbranch ranW = W.to(cpu)[:, perm]— permutingWa second time while permutingHonce. The block loop then ran with mismatched column orders and only a WARN was logged.Fix
Gather
Hfirst, thenW, into locals, and commit both only in theelseblock, so theexceptpath always starts from an unpermutedWandH. Applied to both branches:His gathered first so the extra liveWreference never overlaps thecols x colsH temporaries. Measured peak (max_memory_allocatedup to the Hessian inverse, desc_act):The first shape is the wide
down_projcase that motivates the bug, and its peak is unchanged. The second addsW - Hin 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-permutedW(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 permutedW, so the quantizer state matches the tensor order on both paths. No change was needed;cpu_fallback_usedstill movesQback to the module device at the end.Proxy loss
tr(dW·H·dW^T)with an independent fp64 oracleHref = 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:AMD R9700, ROCm 10.1 (
HIP_VISIBLE_DEVICES=1), after the fix:Every "OOM fallback" run above is a real CUDA OOM from the issue's 40 MiB
set_per_process_memory_fractiontrick; each loggedCUDA 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) injectsRuntimeError("CUDA out of memory")from the first 1-D index ofself.Hinstead of waiting for a real OOM, then asserts that theHseen by the Hessian inverse equalsH_original[perm][:, perm]and that theWpassed tofind_paramsequalsW_original[:, perm]for the sameperm. 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 skippedpytest tests/test_gptq_cuda.py -q(ROCm venv, R9700) ->152 passedpytest tests/test_gptq_cuda.py::test_hessian_permutation_oom_fallback_permutes_w_once -qon the parent commit ->2 failedruff check gptqmodel docs/eora tests setup.py->All checks passed!Residual risk
RuntimeErrorwhose message contains "out of memory" on a CUDA-typed Hessian is handled, unchanged from before; other failures still propagate.