Repository navigation
Description
Found on main @ 4f287eb, RTX 4090 (sm_89, CUDA 13.2, torch 2.15, Python 3.14 with GIL) and AMD R9700 (gfx1201, ROCm 10.1, torch 2.16 dev, free-threaded Python 3.14t).
What is wrong
#4833 (ec161053f, the fix for #4801) added _release_compiled_flip_frames() in gptqmodel/quantization/macaq.py:443-458. macaq_quantize_weight() calls it once per Linear (macaq.py:639), and it runs a full gc.collect(). The commit message says one collection per Linear is "negligible next to the 256-sweep loop". On these small and medium models it is not. A quantize process holds about 0.9M GC-tracked objects (torch, transformers, triton, the model). One full generation-2 pass over them takes about 0.5 s. Qwen2.5-0.5B has 168 Linears and Llama-3.2-1B has 112, so each collection costs about as much as the whole MACaQ solve for that Linear. With the GIL (the CUDA venv), the collection also stops every other quantize worker thread while it runs.
The checkpoint is unchanged: the hash is identical to the one before #4833, so this is time only.
Full-model numbers (MacaqConfig() defaults, 128 C4 samples, fp16; same harness as the previous audit round on dba142d2f)
| model / platform | quant_s dba142d2f |
quant_s 4f287ebdb |
ckpt hash | peak GB |
|---|---|---|---|---|
| Qwen2.5-0.5B-Instruct / RTX 4090 | 45.7 | 188.0 (4.1x) | same ce169865 |
1.09 -> 0.99 |
| Qwen2.5-0.5B-Instruct / R9700 | 81.7 | 145.5 (1.8x) | same cfaa1031 |
1.16 -> 1.01 |
| Llama-3.2-1B-Instruct / R9700 | 166.0 | 217.4 (1.3x) | same 64d4f4aa |
4.18 -> 3.15 |
The per-module "Process" time in the quantize log went from about 0.5 s per Linear to 2-6 s per Linear on the 4090.
Isolated A/B: the gc.collect() alone
Qwen2.5-0.5B-Instruct truncated to 6 layers (42 Linears), 32 samples, RTX 4090, warm compile cache. gc.callbacks measures the time spent in GC. The only change between the runs is what _release_compiled_flip_frames does. It is monkeypatched from the driver script, and the checkout is untouched:
import gc, gptqmodel.quantization.macaq as MQ
orig = MQ._release_compiled_flip_frames
MQ._release_compiled_flip_frames = lambda used: None # "nogc"
# MQ._release_compiled_flip_frames = lambda used: used and gc.collect(1) # "gen1"
m = GPTQModel.load(qwen_6layer, MacaqConfig(), dtype=torch.float16)
t0 = time.time(); m.quantize(CalibrationConfig(dataset=texts)); print(time.time() - t0)| variant | quantize s | time in gen-2 GC | peak GB | resident after quantize() |
|---|---|---|---|---|
dba142d2f (before #4833) |
14.7 | 0.0 s | 1.14 | - |
main as is (full gc.collect() per Linear) |
32.4 / 37.0 | 20.4 / 19.8 s (42 calls, ~0.49 s each, ~0.89M objects) | 0.85 / 0.88 | 0.00 |
main, per-Linear collect disabled |
17.1 / 17.1 | 0.4 s | 0.95 / 0.90 | 0.00 |
main, gc.collect(0) per Linear |
16.8 | 0.4 s | 0.93 | 0.00 |
main, gc.collect(1) per Linear |
11.9 | 0.4 s | 0.90 | 0.00 |
The 2-2.5x slowdown is entirely the per-Linear full collection. It buys at most about 0.02-0.05 GB of peak on this model. Nothing stays resident after quantize() in any variant, because the MacaqProcessor.finalize() backstop collection (macaq_processor.py:359) already holds #4801's "nothing resident after quantize()" contract. The lower full-model peaks in the table above come mostly from #4840 (c49613ad2, int64 exponent planes), not from this collection.
Suggested fix
- Collect only the young generations per Linear (
gc.collect(1), orgc.collect(0)). The Dynamo frame cycle was created during this Linear's sweep, so it has not been promoted to the old generation yet. Keep the single fullgc.collect()infinalize(). - Better: break the cycle directly. For example, drop the sweep's references to its working tensors before returning, or return only the outputs out of the compiled region, so the cycle holds no CUDA memory and needs no collection.
- Or gate the full collection on a memory signal, for example only when
torch.cuda.memory_allocated()grew by more than X since the previous Linear.
Test that should have caught it
tests/test_macaq_compile_flip_memory_4801.py only asserts that one collection happens per Linear. A wall-clock guard on a small real-model MACaQ quantize, or a check that the per-Linear release uses no full-generation collection, would have flagged it. The commit's "faster on Llama-8B (46 s vs 67 s)" claim was not re-measured on small models, where the fixed ~0.5 s per collection dominates.
Severity: medium. MACaQ quantize is 1.3-4x slower on 0.5B-1B models, with no quality change.
🤖 Generated with Claude Code
Activity
Qubitium commentedon Nov 17, 2026
Agent claim released:
agent-53ba17eb4d83finished this issue.Pull request: #4928
Remove the per-Linear full gc.collect() that #4833 added to MACaQ (2-4x quantize slowdown); keep the #4801 'nothing resident after quantize()' contract via the finalize backstop and measure before/after.
Qubitium commentedon Nov 17, 2026
Additional data: the cost grows with the number of Linears, so MoE models are hit hardest, because every routed expert's gate/up/down is its own Linear.
Qwen1.5-MoE-A2.7B truncated to 2 layers (60 routed experts + shared expert per layer, 377
nn.Linear),MacaqConfig()defaults, 16 C4 rows, bf16, RTX 4090 (shared host).gc.collectis wrapped to time every call:gc.collectcallsgc.collectquantize()ec161053f^(before #4833), run 1 / run 2main@ 5cded90, run 1 / 2 / 3That is about 0.45-0.52 s per collection over about 0.93M tracked objects. A model load alone measures 297-328 ms per
gc.collect()with nothing quantizing.Wall time is noisy on this shared GPU. The GC column is not: about 190 s of pure collection for 2 layers. Extrapolating at the same per-call cost:
gc.collect();gc.collect().Under the GIL, every other quantize worker is stalled during each collection.
The
gc.collect(1)/gc.collect(0)variant suggested above, or one collection per layer instead of per Linear, would remove this.🤖 Generated with Claude Code
perf(macaq): drop the per-Linear gc.collect() from the flip path (#4900…
Qubitium commentedon Nov 17, 2026
Controlled full-model A/B, run back to back on the same idle card, so it replaces the noisier sweep timings in the table above. Each run is a fresh process: full Qwen2.5-0.5B-Instruct,
MacaqConfig(), 128 C4 samples, fp16, warm compile cache, RTX 4090. The only differences are the checkout and whether_release_compiled_flip_framesis patched to a no-op from the driver script.main4f287eb as ismain, per-Linear collect disableddba142d2f(before #4833)On a full 0.5B model the per-Linear
gc.collect()makes quantize 3.8-3.9x slower: about 90-105 s of a 125-145 s run is spent inside the collector. It saves about 0.06 GB of peak. The 0.07 GB thatdba142d2fleaves resident afterquantize()(the #4801 symptom) is already gone with the per-Linear collect disabled, becauseMacaqProcessor.finalize()collects once at the end.AMD R9700 (gfx1201, free-threaded Python 3.14t), 6-layer Qwen2.5-0.5B, 32 samples, warm runs:
mainas ismain, per-Linear collect disableddba142d2fThe free-threaded collector is cheaper per call, but it still adds about 40% there.
🤖 Generated with Claude Code