Skip to content

fix(looper): release captured state from every finalize() (#4747) - #4757

Merged
Qubitium merged 1 commit into
mainfrom
fix/issue-4747
Nov 17, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/issue-4747

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Closes #4747.

NativeProcessor.finalize() was the only finalize() override in the looper that did not chain to LoopProcessor.finalize(). The last layer's captured native inputs therefore stayed reachable through the ModuleLooper -> processors reference cycle and quantize() returned with them still allocated, only released by a later cyclic GC.

AWQ's pack_module() had the same shape: the dense fake-quantized wq was popped only on the branch that used it, so the last mlp.down_proj kept its dense weight in module.state.

Changes

  • NativeProcessor.finalize() drops native_inp_caches and then calls super().finalize(...).
  • AWQProcessor.pack_module() pops wq unconditionally after packing.
  • New tests/test_processor_finalize_4747.py: an AST probe that every looper finalize override chains to the base (red on parent: 4 failures), plus runtime checks that the native caches are empty after quantize() and that no packed module keeps wq.

Verification

  • pytest tests/test_processor_finalize_4747.py -> 4 passed.
  • ruff check gptqmodel tests -> clean.
  • Same file on unpatched parent -> 4 failed (red check).

NativeProcessor.finalize() was the only override that did not chain to
LoopProcessor.finalize(), so the last layer's captured native inputs
stayed reachable through the ModuleLooper -> processors reference cycle
and quantize() returned with them allocated. AWQ's pack_module() also
kept the last mlp.down_proj dense fake-quantized wq in module.state.

Chain the base cleanup from the native finalizer, drop native_inp_caches
explicitly, and pop wq unconditionally after packing.
@Qubitium
Qubitium merged commit 162e345 into main Nov 17, 2026
9 checks passed
@Qubitium
Qubitium deleted the fix/issue-4747 branch November 17, 2026 14:02
Qubitium pushed a commit that referenced this pull request Nov 17, 2026
`macaq_quantize_weight(..., compile_flip=True)` runs the flip sweep through
`torch.compile`. Each compiled call leaves a cycle of dead frames (the Dynamo
resume frames, the wrapper, `_flip_sweep`) that still holds the sweep's
intermediate tensors, so nothing releases them deterministically: a run keeps
0.4-1.4 GiB resident per Linear after `quantize()` and peaks higher until
Python's cyclic GC happens to run. #4757 fixed this for the other processors;
MACaQ's default `compile_flip=True` path was the remaining one.

The cycle can only be collected once the sweep's frames have returned, so the
collection runs at the call site in `macaq_quantize_weight` (after
`flip_refine_levels`), not inside `flip_refine_levels` itself. One collection
per Linear is negligible next to the 256-sweep loop; the issue measured it as faster on Llama-8B (46 s vs 67 s) because the allocator stops growing.

`MacaqProcessor.finalize()` collects once more as a backstop, so the
"quantize() returns with nothing resident" contract holds even if something
else in the processor still holds a frame.

Tests: new `tests/test_macaq_compile_flip_memory_4801.py` (CPU-only) drives
`macaq_quantize_weight` with the compiled sweep stubbed and asserts one
collection per Linear, and that neither the eager sweep nor an unsupported
device collects.

Red on unmodified main (9d2ea51): `test_compiled_flip_collects_once_per_weight`
fails (nothing collects). Green here: 3 passed; `test_macaq`,
`test_processor_finalize_4747` and `test_macaq_triton_forward_dtype_4315` give
73 passed with only the four pre-existing CUDA-kernel failures in
`test_macaq.py` (identical on main on this host).

Note: the GPU residency regression itself does not reproduce on this host
(torch 2.11.0+cu130: 0 MiB residue on main for 1024x1024 and 2048x2048 with
cyclic GC disabled), so the test pins the collection rather than the MiB
number.
Qubitium added a commit that referenced this pull request Nov 17, 2026
… (#4833)

`macaq_quantize_weight(..., compile_flip=True)` runs the flip sweep through
`torch.compile`. Each compiled call leaves a cycle of dead frames (the Dynamo
resume frames, the wrapper, `_flip_sweep`) that still holds the sweep's
intermediate tensors, so nothing releases them deterministically: a run keeps
0.4-1.4 GiB resident per Linear after `quantize()` and peaks higher until
Python's cyclic GC happens to run. #4757 fixed this for the other processors;
MACaQ's default `compile_flip=True` path was the remaining one.

The cycle can only be collected once the sweep's frames have returned, so the
collection runs at the call site in `macaq_quantize_weight` (after
`flip_refine_levels`), not inside `flip_refine_levels` itself. One collection
per Linear is negligible next to the 256-sweep loop; the issue measured it as faster on Llama-8B (46 s vs 67 s) because the allocator stops growing.

`MacaqProcessor.finalize()` collects once more as a backstop, so the
"quantize() returns with nothing resident" contract holds even if something
else in the processor still holds a frame.

Tests: new `tests/test_macaq_compile_flip_memory_4801.py` (CPU-only) drives
`macaq_quantize_weight` with the compiled sweep stubbed and asserts one
collection per Linear, and that neither the eager sweep nor an unsupported
device collects.

Red on unmodified main (9d2ea51): `test_compiled_flip_collects_once_per_weight`
fails (nothing collects). Green here: 3 passed; `test_macaq`,
`test_processor_finalize_4747` and `test_macaq_triton_forward_dtype_4315` give
73 passed with only the four pre-existing CUDA-kernel failures in
`test_macaq.py` (identical on main on this host).

Note: the GPU residency regression itself does not reproduce on this host
(torch 2.11.0+cu130: 0 MiB residue on main for 1024x1024 and 2048x2048 with
cyclic GC disabled), so the test pins the collection rather than the MiB
number.

Co-authored-by: Qubitium <[email protected]>
Qubitium added a commit that referenced this pull request Nov 17, 2026
…) (#4928)

#4833 fixed #4801 by running a full `gc.collect()` after every compiled flip
sweep. #4854 then removed the Dynamo graph break itself (the sweep is
`fullgraph=True` and branch-free), so the frame cycle that collection chased no
longer exists: `test_macaq_compile_flip_residency_4801.py` proves the sweep's
operands die with no collection. The per-Linear pass only cost time.

Remove `_release_compiled_flip_frames` and its call site. `MacaqProcessor.
finalize()` keeps its single full collection as the #4747/#4757 backstop, and
the memory test now pins both halves: no hot-path collection, and a finalize
collection that fails if removed.

Qwen2.5-0.5B-Instruct truncated to 6 layers, 32 wikitext samples, fp16, GB10,
`MacaqConfig()` defaults, warm compile cache (A/B by monkeypatching the
helper, checkout untouched):

  variant                       quantize   gen-2 GC   calls  peak     resident
  main (per-Linear collect)     275.5 s    6.75 s     44     0.85 GiB  ~0
  main, per-Linear collect off  261.5 s    0.27 s      2     0.84 GiB  ~0

2 layers / 8 samples: 34.0 s -> 33.0 s, GC 2.18 s -> 0.26 s. The quantized
checkpoint digest is identical (`d04e2623d2b7fc6d`), so this is time only.
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