Skip to content

fix(gptq): stop pinning the task Hessian across quantize() (#5026) - #5040

Merged
Qubitium merged 1 commit into
mainfrom
root/gptqprocessor-hessian-5026
Nov 18, 2026
Merged

Qubitium merged 1 commit into
mainfrom
root/gptqprocessor-hessian-5026

Conversation

@Qubitium

@Qubitium Qubitium commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Fixes #5026. 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.quantize() / FOEM.quantize() 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. It stayed allocated for the whole quantize, including
the peak where H, dXXT, the inverse and the native-delta product are all
live. GPTQ was unaffected because its task.H is still the live buffer at that
point (the fix leaves that path alone).

What Changed

  • gptqmodel/looper/gptq_processor.py
    • the device assertion reads g.H.device through the task instead of binding
      the tensor to a local, so no caller reference survives into quantize();
    • a failing g.quantize() now calls g.release_hessian_state() before
      re-raising, so a failed/cancelled module does not pin [C, C] until the
      processor 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 of free() is factored
    into release_hessian_state() (idempotent, still reference-counted for a
    shared capture), and free() calls it. The success path and the failure path
    therefore cannot diverge.
  • docs/quantization-memory.md: new section 6 with the rule and the
    measurement.
  • tests/test_gptq_processor_hessian_release_5026.py (new, 5 nodes).

Tests

The test takes a weakref to task.H before process() and, on entry to
hessian_inverse, scans the frames above the quantizer's own quantize()
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 separate
worktree): 5 failed, 0 passed

gptaq: a caller still held the task Hessian while quantize() ran
  {'self_attn.q_proj': ['process():g_h'], 'mlp.down_proj': ['process():g_h'], ...}
foem:  same, every module
gptq:  same (the local was an extra reference even where it did not change peak)
test_failed_quantize_releases_the_hessian[gptaq|foem]:
  "a failed quantize left the task Hessian attached"

On this branch: 5 passed.

Neighbouring suites, identical on both trees (no delta):

Run origin/main this branch
test_gptq*.py + test_gptaq_foem_{oom_fallback_4604,multigpu}.py (11 files) 150 passed, 217 skipped 150 passed, 217 skipped
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_release 51 passed, 10 skipped, 1 failed 56 passed, 10 skipped, 1 failed (+5 = the new file)
post-rebase, incl. test_failed_run_model_state_5009 / test_failed_run_fence_4595 / docs contract — 65 passed, 6 skipped

The 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 check clean.

Measured

One 4096 -> 128 nn.Linear on an NVIDIA PG506 (fp16, 4 x 128 calibration
tokens; one fp32 [4096, 4096] Hessian is 64 MiB), peak device allocation for
the quantize() call, hold = the old g_h local vs reading through the task:

Method before after
GPTAQ 327.1 MiB 263.1 MiB (-64.0)
FOEM 329.1 MiB 265.1 MiB (-64.0)

Exactly one 4 * C^2 accumulator per module, so it scales with width: 784 MiB
on an 8B down_proj (C = 14336) and 1.25 GiB at C = 18432, matching the issue's
6280 -> 5496 (GPTAQ) and 5472 -> 4688 (FOEM) on 4-layer Llama-3.1-8B.

Checkpoints are unchanged: the state_dict SHA-256 of each method is identical
before and after the change on the same tiny model (GPTQ 6fc05e0e…, GPTAQ
3ca48220…, FOEM b2862b52…).

Notes

`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.
@Qubitium
Qubitium merged commit b98dc9d into main Nov 18, 2026
8 checks passed
@Qubitium
Qubitium deleted the root/gptqprocessor-hessian-5026 branch November 18, 2026 00:08
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