Skip to content

[MEM] NativeProcessor.finalize() never calls LoopProcessor.finalize(): GPTAQ/FOEM keep the last layer's native inputs on GPU after quantize() until cyclic GC (474 MiB on Llama-3.1-8B @128 samples; AWQ leaks last down_proj wq similarly) #4747

Closed

Description

@Qubitium

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

What is wrong

NativeProcessor.finalize() (gptqmodel/looper/native_processor.py:285-289) overrides LoopProcessor.finalize() without calling super().finalize(). It is the only processor finalize override that skips it. The other ten processors (gptq, qqq, awq, macaq, exl3, vptq, paroquant, eora, fp8_gsq, weight_only) all call super().finalize(...).

So for every GPTAQ/FOEM run (the plans that add ProcessorStep.NATIVE), the base cleanup at loop_processor.py:1597-1613 never runs:

  • del self.inputs_cache: the last layer's captured native inputs, one CUDA tensor per calibration batch;
  • del self._results;
  • _close_device_smi_handles();
  • release_workspace_cache();
  • remove_processor_log(...).

ModuleLooper -> processors -> NativeProcessor is part of a reference cycle, so the cache is not freed when quantize() returns. It waits for Python's cyclic GC. The NativeProcessor is no longer reachable, but the tensors stay allocated on the GPU, and torch.cuda.empty_cache() cannot release them. docs/quantization-memory.md states the contract that replay activations are released "without waiting for Python's cyclic garbage collector".

Referrer chain of a leftover tensor ([1, seq, 4096] bf16, Llama-3.1-8B):
tensor <- list(len 1) <- list(len 32, one per batch) <- InputCache <- _ThreadSafeInputCache <- NativeProcessor.__dict__ <- ModuleLooper.processors (unreachable cycle).

Measured

run memory_allocated() right after quantize() after gc.collect()
Llama-3.1-8B-Instruct GPTAQ 4-bit, 128 C4 docs (62.8k tokens) 474 MiB (the in-session model then shows 5.81 GiB on GPU instead of 5.34) 0
same, plain GPTQ 0 0
4-layer Llama-3.1-8B GPTAQ, 32 docs 117.8 MiB 0
4-layer GPTAQ with NativeProcessor.finalize also calling LoopProcessor.finalize 0.0 MiB 0

The retained size is (calibration tokens) x hidden x 2 bytes. A 70B-class model (hidden 8192) calibrated on 512 x 2048 tokens would keep about 17 GiB on the GPU after quantize() returns, until a gen-2 collection happens.

Repro

import gc, torch
from gptqmodel import GPTQModel
from gptqmodel.quantization.config import GPTQConfig, GPTAQConfig, CalibrationConfig
m = GPTQModel.load("meta-llama/Llama-3.1-8B-Instruct",
                   GPTQConfig(bits=4, group_size=128, gptaq=GPTAQConfig()), dtype=torch.bfloat16)
m.quantize(CalibrationConfig(dataset=c4_texts[:128]))
print(torch.cuda.memory_allocated() / 2**20)   # 474 MiB  (expected ~0: the in-session model is on CPU)
gc.collect()
print(torch.cuda.memory_allocated() / 2**20)   # 0.0

Suggested fix

# gptqmodel/looper/native_processor.py
def finalize(self, model, **kwargs):
    self._release_native_share_cache()
    del self.native_inp_caches
    super().finalize(model=model, **kwargs)

Also consider breaking the ModuleLooper <-> processor cycle at the end of loop(), for example by clearing self.processors. Then any future override that forgets super() still cannot pin GPU tensors.

There is a smaller case of the same pattern in AWQ. pack_module pops state["wq"] only when calculate_w_wq_diff is set (awq_processor.py:5004-5005). Otherwise the dense fake-quantized weight of the last mlp.down_proj (4096x14336 bf16, 112 MiB on Llama-3.1-8B) stays in NamedModule.state until cyclic GC: 0.109 GiB is still allocated after a full AWQ quantize(). module.state.pop("wq", None) should be unconditional.

Test that should have caught it

A lifecycle test that runs a tiny GPTAQ (and FOEM) quantize on CUDA and asserts torch.cuda.memory_allocated() is about 0 right after quantize(), without calling gc.collect(). Or a structural test asserting that every LoopProcessor.finalize override reaches LoopProcessor.finalize.

Severity

Low-medium: GPU memory and processor resources (device-smi handles, processor log file, workspace) held after every GPTAQ/FOEM run until cyclic GC. The amount scales with calibration size.

🤖 Generated with Claude Code

Activity

  1. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim released: agent-53ba17eb4d83 finished this issue.

    Pull request: #4757

    LoopProcessor.finalize chain + GPU-resident native inputs after quantize()

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