Repository navigation
fix(models): compile the unbound forward so optimize() leaves no cycle (#4937) - #4986
Merged
Merged
Conversation
#4937) #4912 made `BaseQModel.optimize()` compile `model.forward` in place so `generate()` enters the compiled path (#4669). Compiling the *bound* method stored that bound method inside the compiled wrapper, and installing the wrapper as an instance attribute closed a `model -> forward -> model` cycle: the model and its VRAM survived the last user reference until a full cyclic `gc.collect()`. Compile the unbound class function instead and reach the instance through a weak reference, the same pattern `TorchLinear.optimize()` uses for `dequantize_weight` (#4871). `_CompiledModelForward` exposes `_torchdynamo_orig_callable` on demand (a stored bound method would recreate the cycle), so unwrapping callers still see the eager callable, and a repeated `optimize()` recompiles the same unbound function instead of stacking proxies. When compilation is unavailable the instance attribute is removed, so the class method is used again.
Conflict in `gptqmodel/models/base.py`: main added `import types` for the #4947 `_old_forward` rebinding while this branch adds `import weakref` for the cycle-free compiled forward. Both are kept.
This was referenced Nov 17, 2026
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #4937 — the model-level leftover from my #4871.
Root cause
#4912 made
BaseQModel.optimize()compilemodel.forwardin place sogenerate()enters the compiled path (#4669). Compiling the bound method stored that bound method inside the compiled wrapper, and installing the wrapper as an instance attribute closed amodel -> forward -> modelcycle: the model and its VRAM survived the last user reference until a full cyclicgc.collect().Fix
gptqmodel/models/base.py:optimize()now compiles the unbound class function (type(model).forward) and installs a_CompiledModelForwardproxy that resolves the instance through aweakref— the same patternTorchLinear.optimize()uses fordequantize_weight(fix(mem): drop the reference cycles a single-device GPU load leaves behind (#4847) #4871)._torchdynamo_orig_callableon demand (a stored bound method would recreate the cycle), so unwrapping callers still see the eager callable.optimize()recompiles the same unbound function, so proxies do not stack; when compilation is unavailable the instance attribute is removed so the class method is used again (no cycle, no stale proxy).generate()still enters the compiled path — the [PERF] generate(cache_implementation="static") is 2-4x SLOWER than eager on every default CUDA kernel (HF auto-compile graph-breaks on each quantized linear: no fake impls, data_ptr/id() guards); model.optimize() compiles nothing that generate() runs #4669 contract is unchanged and its test still passes.Evidence
New
tests/test_optimize_no_reference_cycle_4937.py(3 tests,gc.disable()so only a cyclic collection could mask the leak):test_optimize_does_not_leave_a_model_cycle— the model must die by refcount afteroptimize()test_repeated_optimize_does_not_stack_or_cycle— twooptimize()calls still leave no cyclegenerate()afteroptimize()compiles >=1 frame and matches eagerRed on parent (
git show origin/main:gptqmodel/models/base.pyswapped in): 2 failed (the two cycle tests), 1 passed. Green here: 3 passed.test_optimize_no_reference_cycle_4937+test_optimize_generate_4669+test_single_device_dispatch_4847+test_quant_linear_release_4847+test_generation_compile_policy_4669+test_torch_compile_fallback+test_cancelled_quantize_kernel_rehome_4947: 60 passed, exit 0ruff check gptqmodel tests: cleanResidual risk
RuntimeErrorifforwardis called after the model was released (it cannot happen through normal use — the caller holds the model to call it).torch.compile's own cache now keys on the unbound function, so two instances of the same class share a compiled graph (correct, and cheaper) — the graph still takesselfas its first argument.