Skip to content

fix(macaq): keep the compiled flip sweep graph-break free (#4801) - #4854

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

Qubitium merged 1 commit into
mainfrom
fix/issue-4801

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4801.

Root cause

_flip_sweep's data-dependent if not improving.any(): return is a Dynamo graph break. The resume frames form an unreachable reference cycle holding the Hessian operands and masks, so compile_flip=True pinned them until a cyclic GC pass.

Change

gptqmodel/quantization/macaq.py: removed the early return (a converged iteration runs one update whose empty masks make every index_put a no-op) and compiled with fullgraph=True, so a future break fails loudly instead of silently pinning the Hessian. compile_flip=True remains the default; eager path unchanged.

Measured (Qwen2.5-0.5B, 3 layers, 16 rows, n_flip_iters=32, GB10)

  • parent compiled: 62.8 MiB resident after quantize (0 after gc)
  • fixed compiled: 0.0 MiB; peak 903.8 vs 972.8 MiB; time 29.2 vs 32.9 s
  • issue repro: 116/376 MiB -> 0/0
  • quantized-output digest identical; 240/240 sweep cases bit-identical

Tests

  • new tests/test_macaq_compile_flip_residency_4801.py: 2 passed (both fail on parent)
  • tests/test_macaq*.py + related: 173 passed, 1 skipped
  • ruff check gptqmodel tests: clean

Residual risks

  • fullgraph=True turns any future graph break into a hard error (covered by the CPU test).
  • Converged iterations do one wasted no-op update; end-to-end time improved.

`flip_refine_levels(compile_flip=True)` compiled `_flip_sweep`, whose
`if not improving.any(): return ...` is a data-dependent branch. Dynamo
emitted resume frames for the break, and those frames (with
`signed_index`, the Hessian operands and the masks) ended up in an
unreachable reference cycle that only Python's cyclic GC reclaimed. On
Llama-3.1-8B that left 0.4-1.4 GiB on the GPU after `quantize()` and
raised the quantize peak by 1.25 GiB.

Drop the early return so the sweep traces as one graph, and compile it
with `fullgraph=True` so a future break fails loudly instead of silently
pinning the Hessian. A converged iteration now runs one update whose
empty masks make every index_put a no-op, so the refined codes are
bit-identical (verified over 240 seeded shape/fast/iteration cases, and
the full-model quantized tensors hash identically before and after).

Qwen2.5-0.5B-Instruct, 3 layers, 16 wikitext rows, concat 256,
n_flip_iters=32, bf16, GB10:

  variant                     after quantize   after gc   peak      time
  parent, compile_flip=True   62.8 MiB         0.0 MiB    972.8 MiB 32.9 s
  fixed,  compile_flip=True    0.0 MiB         0.0 MiB    903.8 MiB 29.2 s
  fixed,  compile_flip=False   0.0 MiB         0.0 MiB   1273.1 MiB 29.5 s

The issue's minimal repro drops from 116/376 MiB to 0.0 MiB per call.
@Qubitium
Qubitium merged commit e739eef into main Nov 17, 2026
9 checks passed
@Qubitium
Qubitium deleted the fix/issue-4801 branch November 17, 2026 17:29
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