Skip to content

fix(models): compile the unbound forward so optimize() leaves no cycle (#4937) - #4986

Merged
Qubitium merged 2 commits into
mainfrom
fix/issue-4937
Nov 17, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/issue-4937

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4937 — the model-level leftover from my #4871.

Root cause

#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().

Fix

gptqmodel/models/base.py:

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 after optimize()
  • test_repeated_optimize_does_not_stack_or_cycle — two optimize() calls still leave no cycle
  • a guard that generate() after optimize() compiles >=1 frame and matches eager

Red on parent (git show origin/main:gptqmodel/models/base.py swapped 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 0
  • ruff check gptqmodel tests: clean

Residual risk

  • The proxy raises RuntimeError if forward is 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 takes self as its first argument.
  • Measured on CPU (refcount + weakref); no VRAM number was taken here because the cycle is fully explained by the object graph.

#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.
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