Repository navigation
perf(macaq): drop the per-Linear gc.collect() from the flip path (#4900) - #4928
Merged
Merged
Conversation
#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.
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 #4900 — a regression from #4833.
Root cause
Two fixes stacked: #4833 kept #4801's "nothing resident after
quantize()" contract by running a fullgc.collect()after every compiled flip sweep; #4854 then removed the Dynamo graph break itself (branch-free sweep,fullgraph=True), so the frame cycle that collection chased no longer exists. The per-Linear generation-2 pass over ~0.9M tracked objects became pure overhead — one collection per Linear, serialized under the GIL.tests/test_macaq_compile_flip_residency_4801.pyalready proves the sweep's operands die with no collection, so the hot-path pass was redundant.Fix
_release_compiled_flip_frames()and its call site inmacaq_quantize_weight()(gptqmodel/quantization/macaq.py); dropped the now-unusedimport gc.gc.collect()inMacaqProcessor.finalize()as the [MEM] NativeProcessor.finalize() never calls LoopProcessor.finalize(): GPTAQ/FOEM keep the last layer's native inputs on GPU after quantize() until cyclic GC (474 MiB on Llama-3.1-8B @128 samples; AWQ leaks last down_proj wq similarly) #4747/fix(looper): release captured state from every finalize() (#4747) #4757 residency backstop, with an updated comment.tests/test_macaq_compile_flip_memory_4801.py: the hot path must record zerogc.collect(...)calls, and a newtest_finalize_keeps_the_collection_backstopfails if the finalize collection is removed. The strong residency test is untouched and stays green.Measured (Qwen2.5-0.5B-Instruct, 6 layers, 32 wikitext samples, fp16, GB10,
MacaqConfig()defaults; A/B by monkeypatching the helper)2 layers / 8 samples: 34.0 s -> 33.0 s, GC 2.18 s -> 0.26 s.
gc.collect(1)was not cheaper here (274.7 s, 7.06 s GC), so "no collection" is the right choice. Checkpoint hash equality: parent and fixed both produce digestd04e2623d2b7fc6d— time only.Tests
tests/test_macaq*.py+ both [MEM] MACaQ default compile_flip leaves each Linear's flip working set in a frame cycle: 0.4-1.4 GiB still on GPU after quantize() until cyclic GC, +1.25 GiB peak on Llama-8B (sibling of #4747 that #4757 does not cover) #4801 tests +test_processor_finalize_4747+test_checkpoint_resume_nf12_macaq_4792: 166 passed, 1 skipped, exit 0.test_compiled_flip_does_not_collect_per_weightfails ([()] != []).gc.collect()removed):test_finalize_keeps_the_collection_backstopfails.ruff check gptqmodel tests: clean.Residual risk