Skip to content

fix(gptq): stop holding the task Hessian across g.quantize() (#5026) - #5043

Merged
Qubitium merged 1 commit into
ModelCloud:mainfrom
Qubitium:fix/5026-hessian-lifetime
Nov 18, 2026
Merged

Qubitium merged 1 commit into
ModelCloud:mainfrom
Qubitium:fix/5026-hessian-lifetime

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

GPTQProcessor.process() bound the task's Hessian to a local g_h for a device assert and then called g.quantize() while that local was still alive:

g_h = getattr(g, "H", None)
if g_h is not None:
    assert torch.device(g_h.device) == expected_device, (...)
...
wq, q_scales, ... = g.quantize()

GPTAQ/FOEM release the Hessian themselves (H = self.H; del self.H, gptaq.py:450-451, foem.py:502-503) and then rebind H to the activation-order permuted copy. The surviving local kept the original unpermuted [C, C] fp32 tensor alive for the whole quantize — including the peak, where H, dXXT and the inverse are all live.

Closes #5026.

Fix

Drop the local right after the assert. One line plus the comment explaining why; no behavior change.

Measured by the issue (4-layer Llama-3.1-8B, 64 C4 rows):

method peak allocated (MiB) checkpoint
GPTAQ 6280 -> 5496 (-784, -12%) 123/123 tensors bitwise identical
FOEM 5472 -> 4688 (-784, -14%) 123/123 bitwise identical
GPTQ 4016 -> 4016 unchanged

The saving is 4*C^2 bytes per module with a dense Hessian: 784 MiB on an 8B down_proj, 1.25 GiB at C = 18432.

Tests

New tests/test_gptq_processor_hessian_lifetime_5026.py runs the real process() preamble and stops inside quantize(), where it records the strong-reference count of the task Hessian. The task itself is one reference; the probe allows exactly that (2 including getrefcount's own argument).

python -m pytest tests/test_gptq_processor_hessian_lifetime_5026.py -q
# 1 passed
# parent commit: assert 3 == 2

Related GPTQ/GPTAQ coverage:

python -m pytest tests/test_gptaq_foem_batch.py tests/test_gptq_keep_mask_device.py -q
# 12 passed, 3 skipped

ruff check on the processor and the new test: All checks passed.

Logic Flow Documentation

Not applicable: a local's lifetime changed; the flow is unchanged.

Model Support / Module Plan

Not applicable: no definition or plan change.

…oud#5026)

`GPTQProcessor.process()` bound the task's Hessian to a local `g_h` for a device
assert and then called `g.quantize()` while that local was still alive.

GPTAQ/FOEM release the Hessian themselves (`H = self.H; del self.H`) and rebind
`H` to the activation-order permuted copy, so the surviving local kept the
original unpermuted `[C, C]` fp32 tensor alive for the whole quantize --
including the peak where `H`, `dXXT` and the inverse are all live. That is
784 MiB per dense Hessian on an 8B down_proj (C = 14336), 1.25 GiB at C = 18432.

Drop the local right after the assert. Measured by the issue on a 4-layer
Llama-3.1-8B: GPTAQ peak 6280 -> 5496 MiB, FOEM 5472 -> 4688 MiB, GPTQ
unchanged, and all 123 checkpoint tensors bitwise identical.

Regression test: `tests/test_gptq_processor_hessian_lifetime_5026.py` runs the
real `process()` preamble and stops inside `quantize()`, where it records the
strong-reference count of the task Hessian. The task itself is one reference;
the fix leaves exactly that (2 with `getrefcount`'s argument), the parent commit
leaves 3.
@Qubitium
Qubitium merged commit 297a5c4 into ModelCloud:main Nov 18, 2026
2 of 4 checks passed
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant