Repository navigation
fix(critic): break autograd hook reference cycle leaking VRAM in _live_captures - #585
Merged
Merged
Conversation
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.
Summary
Fixes a C++/Python reference-cycle VRAM leak in ParticleGAN's critic
_live_capturesforward hooks. Previously, capturingentryinsideunscaled.register_hook(lambda _g, _e=entry: _e.__setitem__(5, False))formed a cycle (unscaled -> AutogradMeta hook -> entry -> unscaled+x_act) that leaked 2 input batches (and their autograd graphs) per step even after_live_captures.clear().Fixed sites (all 6 cycle sites across
particlegan/)particlegan/_unified_v11/critic.pyparticlegan/_unified_v4/critic.pyparticlegan/_unified/critic.pyparticlegan/_t2_critic/critic.py(added shared_clear_live_capture_entries/_trim_live_captureshelpers)particlegan/_nash_v3/record.py(also addedrecord_step()cleanup on_NashV3ObserverStepRecord)particlegan/_minimal/dynamics.py(inline standalone fix keeping minimal core< 900LOC)Mechanism
alive = [True]) and hookraw_target/targetwithlambda _g, _a=alive: _a.clear()sobool(alive)flipsTrue -> Falseidempotently across all 4 existing consumers (_unified/penalty.py,_t2_critic/penalty.py,_nash_v3/penalty.py,_minimal/geometry.py) without modifying any loss/penalty/optimizer formulas, and properly invalidatesaliveon BF16 separate-backward passes.entry[3] = entry[4] = Noneand clearentry[5]inclear_transient_captures(), ring-buffer eviction, and checkpoint restore (load_state_dict/_restore_*_packet).Testing
Added
tests/test_unified_critic_vram_leak.pycovering weakref GC (fp32 & bf16), ring-buffer eviction,load_state_dictrelease, backward-alive semantic controls across all 4 consumers, CPU multi-stepGANTrainerzero-retained-batch checks, and CUDA 40-step CIFAR-10-sized flat-VRAM checks.python3 scripts/check_lint.py: PASS (294 existing findings; no additions)test_unified_critic_vram_leak.py,test_unified_v11.py,test_unified_v4.py,test_unified_recipe.py,test_t2_secant_critic.py,test_nash_v3.py,test_minimal.py,test_training.py,test_recipe_defaults.py): 177 passed, 0 failedCUDA_VISIBLE_DEVICES=0 python3 -m pytest -q -m cuda tests/test_unified_critic_vram_leak.py: 3 passed, 0 failed (0 leaked batch weakrefs, flat VRAM over 40 steps onunified_v11,unified_v4,unified)python3 -m experiments.forge validate && python3 -m experiments.forge compile --check: PASS (valid,CURRENT)