Repository navigation
fix(gptq): stop pinning the task Hessian across quantize() (#5026) - #5040
Merged
Merged
Conversation
`GPTQProcessor.process()` bound the task's Hessian to a local (`g_h`) for a device assert and then called `g.quantize()` with that local still alive. GPTAQ/FOEM hand their `[C, C]` accumulator to the quantizer (`H = self.H; del self.H`) and rebind `H` to the activation-order permuted copy, so the caller's local was the last reference to the original unpermuted fp32 Hessian and kept it allocated for the whole quantize - including the peak, where `H`, `dXXT`, the inverse and the native-delta product are all live. The device check now reads `g.H.device` through the task instead of holding a reference, and the same accumulator is released when `quantize()` fails: `process()` calls the new `GPTQ.release_hessian_state()` before re-raising, so a failed or cancelled module does not pin `[C, C]` until the processor is torn down. `free()` now uses the same helper, so the success and failure paths cannot diverge, and the release stays reference-counted for a shared capture. Measured on one 4096 -> 128 Linear (fp16, 4 x 128 calibration tokens; the fp32 `[4096, 4096]` Hessian is 64 MiB), peak device allocation for `quantize()`: GPTAQ 327.1 -> 263.1 MiB (-64.0) FOEM 329.1 -> 265.1 MiB (-64.0) That is exactly one `4 * C^2` accumulator per module: 784 MiB on an 8B `down_proj` (C = 14336), 1.25 GiB at C = 18432. Checkpoints are bitwise identical: GPTQ, GPTAQ and FOEM `state_dict` SHA-256 match before and after. Docs: `docs/quantization-memory.md` gains section 6 with the rule and the measurement.
This was referenced Nov 18, 2026
Qubitium
added a commit
that referenced
this pull request
Nov 18, 2026
#5040 made `process()` call `task.release_hessian_state()` when `quantize()` raises; #5043's probe double predates that helper, so the probe fails with AttributeError instead of measuring the reference count. That leaves `tests/test_gptq_processor_hessian_lifetime_5026.py` red on main after #5047 removed the stale `del g_h`. Give the double a no-op hook.
Qubitium
added a commit
that referenced
this pull request
Nov 18, 2026
) #5040 made `process()` call `task.release_hessian_state()` when `quantize()` raises; #5043's probe double predates that helper, so the probe fails with AttributeError instead of measuring the reference count. That leaves `tests/test_gptq_processor_hessian_lifetime_5026.py` red on main after #5047 removed the stale `del g_h`. Give the double a no-op hook.
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.
Summary
Fixes #5026.
GPTQProcessor.process()bound the task's Hessian to a local(
g_h) for a device assert and then calledg.quantize()with that local stillalive.
GPTAQ.quantize()/FOEM.quantize()hand their[C, C]accumulator tothe quantizer (
H = self.H; del self.H) and rebindHto the activation-orderpermuted copy, so the caller's local was the last reference to the original
unpermuted fp32 Hessian. It stayed allocated for the whole quantize, including
the peak where
H,dXXT, the inverse and the native-delta product are alllive. GPTQ was unaffected because its
task.His still the live buffer at thatpoint (the fix leaves that path alone).
What Changed
gptqmodel/looper/gptq_processor.pyg.H.devicethrough the task instead of bindingthe tensor to a local, so no caller reference survives into
quantize();g.quantize()now callsg.release_hessian_state()beforere-raising, so a failed/cancelled module does not pin
[C, C]until theprocessor is torn down. Only the Hessian state is released; the module and
quantizer are left intact for the existing failed-run cleanup.
gptqmodel/quantization/gptq.py: the release body offree()is factoredinto
release_hessian_state()(idempotent, still reference-counted for ashared capture), and
free()calls it. The success path and the failure paththerefore cannot diverge.
docs/quantization-memory.md: new section 6 with the rule and themeasurement.
tests/test_gptq_processor_hessian_release_5026.py(new, 5 nodes).Tests
The test takes a
weakreftotask.Hbeforeprocess()and, on entry tohessian_inverse, scans the frames above the quantizer's ownquantize()frame for a local that still holds that tensor. A caller local is exactly the
defect; the quantizer's own handle (which it releases) is not counted.
Red on unmodified
origin/main(65a3099f8, test copied into a separateworktree): 5 failed, 0 passed
On this branch: 5 passed.
Neighbouring suites, identical on both trees (no delta):
origin/maintest_gptq*.py+test_gptaq_foem_{oom_fallback_4604,multigpu}.py(11 files)test_gptq_hessian_{memory,accumulation},test_gptq_shared_hessian,test_gptq_workspace_release,test_gptaq_foem_batch,test_gptaq_shuffle_pairing_4869,test_fallback_hessian_releasetest_failed_run_model_state_5009/test_failed_run_fence_4595/ docs contractThe single failure in both trees is the pre-existing
test_fallback_hessian_release.py::test_shared_input_fallback_releases_adopted_source_before_clone(
IndexError: list index out of range), unrelated to this change.ruff checkclean.Measured
One 4096 -> 128
nn.Linearon an NVIDIA PG506 (fp16, 4 x 128 calibrationtokens; one fp32
[4096, 4096]Hessian is 64 MiB), peak device allocation forthe
quantize()call,hold= the oldg_hlocal vs reading through the task:Exactly one
4 * C^2accumulator per module, so it scales with width: 784 MiBon an 8B
down_proj(C = 14336) and 1.25 GiB at C = 18432, matching the issue's6280 -> 5496(GPTAQ) and5472 -> 4688(FOEM) on 4-layer Llama-3.1-8B.Checkpoints are unchanged: the
state_dictSHA-256 of each method is identicalbefore and after the change on the same tiny model (GPTQ
6fc05e0e…, GPTAQ3ca48220…, FOEMb2862b52…).Notes
keeps the module/quantizer, because [BUG] #4515 (fix of #4470) incomplete: stopping quantize() at the forward_end subset stage (StopMainLoop, exception or Ctrl-C) leaves capture hooks with forward_hook_last=True; every later forward raises StopForward #5009/[BUG] #4572 repair skipped on the failure path: after a cancelled/failed quantize() the in-session model keeps inference-mode norms/buffers; a plain (grad-mode) forward raises 'Inference tensors cannot be saved for backward' #5012's failed-run cleanup needs to
rehome the module and
free()deletes both.release_hessian_state()is idempotent (_hessian_state_released), soprocess()'s failure call followed by the processor's normal teardown cannotrelease a shared capture twice.
triu_on the native-deltaproduct) was not taken: it touches the numerics of the GPTAQ/FOEM epilogue for
a further
[C, C], and this PR is scoped to the reference that the callerowns. It is a candidate follow-up.