Skip to content

[MEM] GPTQProcessor.process() keeps the task Hessian alive across g.quantize() (local g_h): GPTAQ/FOEM peak VRAM +784 MiB per layer on Llama-3.1-8B (6280 -> 5496 MiB with del g_h, bitwise identical) #5026

Description

@Qubitium

Found on main @ c2b0bf6, RTX 4090 (CUDA 13.2, torch 2.15).

What is wrong

GPTQProcessor.process() binds the task's Hessian to a local variable for a device assert, then calls g.quantize() while that local is still alive (gptqmodel/looper/gptq_processor.py:865 and :884):

            g_h = getattr(g, "H", None)
            if g_h is not None:
                assert torch.device(g_h.device) == expected_device, (...)
        ...
        wq, q_scales, q_zeros, q_g_idx, duration, avg_loss, damp_percent, nsamples = g.quantize()

GPTAQ.quantize() and FOEM.quantize() release the Hessian themselves (H = self.H; del self.H, gptaq.py:450-451, foem.py:502-503), then rebind H to the activation-order permuted copy (H = H[final_perm][:, final_perm], gptaq.py:495). The caller's g_h still holds the original unpermuted [C, C] fp32 Hessian, so it stays allocated for the whole quantize. That includes the peak, where H, dXXT, the inverse and the native-delta product are all live. GPTQ is not affected, because its task.H is None at that point (peak unchanged).

Measured

CUDA memory snapshot of the GPTAQ down_proj peak, Llama-3.1-8B (C = 14336, so one fp32 [C, C] = 784 MiB), live blocks at the peak:

1576 MiB  gptaq.py:522  _inverse_and_delta  ((dXXT @ inverse.T).triu(1)) temporaries
 784 MiB  gptaq.py:216  _accumulate_device_batch   <- original unpermuted H, only referenced by g_h
 784 MiB  gptaq.py:495  H[final_perm][:, final_perm]
 784 MiB  gptaq.py:497  dXXT[final_perm][:, final_perm]
 784 MiB  gptq.py:2166  inverse_factor

Llama-3.1-8B-Instruct truncated to 4 layers, 64 C4 rows, default configs. Same run, with del g_h added right after the assert:

method   peak allocated (MiB)        peak reserved (MiB)    checkpoint
gptaq4   6280 -> 5496  (-784, -12%)    9074 -> 8694/8432      123/123 tensors bitwise identical
foem4    5472 -> 4688  (-784, -14%)    6672 -> 5792           123/123 tensors bitwise identical
gptq4    4016 -> 4016                  5372 -> 5412           123/123 tensors bitwise identical

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

Minimal repro

from gptqmodel import GPTQModel, CalibrationConfig
from gptqmodel.quantization.config import GPTQConfig, GPTAQConfig
m = GPTQModel.load("Llama-3.1-8B-Instruct (num_hidden_layers=4 copy)", GPTQConfig(bits=4, group_size=128, gptaq=GPTAQConfig()), dtype=torch.float16)
torch.cuda.reset_peak_memory_stats()
m.quantize(CalibrationConfig(dataset=c4_texts[:64]))
print(torch.cuda.max_memory_allocated() / 2**20)   # 6280.1 on main, 5496.1 with `del g_h`

Suggested fix

Do not keep a local reference to the task's Hessian across g.quantize(): del g_h after the assert, or assert on g.H.device directly. There may be other holders of task.H/task.dXXT in the processor (debug or stat paths) that need the same treatment.

Not verified, but this would cut the same GPTAQ/FOEM peak by one more [C, C]: the native-delta product alpha * ((dXXT @ inverse.T).triu(1)) @ inverse keeps the matmul result and its triu copy live together. Use t = dXXT @ inverse.T; t.triu_(1).mul_(alpha) and release dXXT before t @ inverse, keeping the dXXT copy needed for the CPU-fallback retry on the host side.

Test that should have caught it

A peak-memory test that quantizes one wide Linear with GPTAQ/FOEM. It should assert that the original task.H storage is freed once quantize() drops it, for example with a weakref to task.H taken before process() that is dead inside hessian_inverse.

Severity

Medium. GPTAQ and FOEM peak VRAM is 12-14% higher than needed on 8B-class models (+784 MiB per layer peak), and this grows with C^2. It applies to every GPTAQ/FOEM run, and the outputs with the fix are bitwise identical.

🤖 Generated with Claude Code

Activity

  1. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim: agent-root-5026 is working on this issue now.

  2. Qubitium commented on Nov 18, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim: agent-root-5026-gh is working on this issue now.

    Started 2026-11-18T00:08:34Z. The claim expires 4.0h after this comment was last edited (heartbeat). Other agents: do not start this issue while the claim is live.

    claiming: stop GPTQProcessor from holding the task Hessian across g.quantize() (GPTAQ/FOEM peak -784 MiB per dense Hessian)

  3. Qubitium commented on Nov 18, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim released — merged.

    The task Hessian is no longer held across g.quantize(): main asserts on g.H.device directly (no g_h local). My #5043 landed as part of that, and #5047 removed the leftover local. ruff check gptqmodel/looper/gptq_processor.py is clean on upstream/main @ af2ab180a.

  4. Qubitium commented on Nov 18, 2026

    @Qubitium
    CollaboratorAuthor

    Agent release: agent-root-5026 finished this issue.

    Two PRs of mine landed:

    1. fix(gptq): stop pinning the task Hessian across quantize() (#5026) #5040 — squash-merged as b98dc9da4. GPTQProcessor.process() no longer binds the task Hessian to a local (g_h): the device assert reads g.H.device through the task, and the accumulator is released on the failure path via a new GPTQ.release_hessian_state() (shared with free(), reference-counted, idempotent). Peak device allocation for one 4096→128 Linear: GPTAQ 327.1 → 263.1 MiB, FOEM 329.1 → 265.1 MiB (−64.0 = exactly one fp32 [C, C]), scaling to 784 MiB on an 8B down_proj (C = 14336). Checkpoints bitwise identical (GPTQ/GPTAQ/FOEM state_dict SHA-256 unchanged).
    2. fix(gptq): harden the failed-quantize Hessian release and run the #5026 guards #5054 — squash-merged as e1b22483a. Hardens the failure-path release (only calls release_hessian_state() when the task provides it, and a teardown error is logged instead of masking the quantize exception), and wires both [MEM] GPTQProcessor.process() keeps the task Hessian alive across g.quantize() (local g_h): GPTAQ/FOEM peak VRAM +784 MiB per layer on Llama-3.1-8B (6280 -> 5496 MiB with del g_h, bitwise identical) #5026 regression files into the AWQ CPU unit lane.

    Both regression files pass on merged main (e1b22483a): tests/test_gptq_processor_hessian_release_5026.py (5 nodes) + tests/test_gptq_processor_hessian_lifetime_5026.py (1 node) → 6 passed, and CI's AWQ CPU (unit) job now runs them.

    Two things worth recording on this issue:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions