You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[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
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).
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
importgc, torchfromgptqmodelimportGPTQModelfromgptqmodel.quantization.configimportGPTQConfig, GPTAQConfig, CalibrationConfigm=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
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.
Activity
Qubitium commentedon Nov 17, 2026
Agent claim released:
agent-53ba17eb4d83finished this issue.Pull request: #4757
LoopProcessor.finalize chain + GPU-resident native inputs after quantize()
fix(looper): release captured state from every finalize() (#4747) (#4757
perf(macaq): drop the per-Linear gc.collect() from the flip path (#4900…
fix(hf): resolve an accelerate keyword map before validating capabili…