Repository navigation
fix(runner): freeze GC survivors so forced gc.collect() stays constant - #1680
Open
ZhengXinwei-F wants to merge 5 commits into
Open
ZhengXinwei-F wants to merge 5 commits into
ZhengXinwei-F wants to merge 5 commits into
Conversation
maybe_empty_cache(collect_garbage=True / force=True) runs a full gc.collect(), which walks every tracked object. MiniMax-H3 forces one per request in _offload_transformer. With torch 2.9.0/2.9.1 and use_compile/vae_use_compile, every compiled call leaks two small lists (pytorch/pytorch#174468), ~13.7k per 768p/120-frame request, so the forced GC grows by ~1.6-3.6 ms per request and adds seconds per request after a day of uptime. maybe_empty_cache now calls _collect_garbage(): a full collection followed by gc.freeze(), so later collections only walk objects created since. Every gc_full_collect_every calls (default 200, 0 disables) it unfreezes once to reclaim cyclic garbage among frozen objects. gc_freeze_survivors=false restores the plain gc.collect(). lazy_load and unload_modules keep the plain collection, as _maybe_freeze_gc() does. Refs ModelTC#1678 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A runner holds itself in reference cycles (self.run_input_encoder is one of its own bound methods). Once gc.freeze() has moved it to the permanent generation, dropping it is not enough: gc.collect() skips frozen objects, so its models stay alive and __del__ never runs. BaseRunner's one-shot _maybe_freeze_gc() already had this property; the per-runner counter in _collect_garbage() could not help either, since it was dropped together with the runner. - The periodic full-pass counter is now process-wide, so the next periodic pass of any runner releases a dropped runner. - base_runner.release_frozen_gc() unfreezes and collects at once, for code that drops or replaces a runner in a long-lived process. gc_freeze_survivors stays on by default. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Nov 9, 2026
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.
Refs #1678 (the GC side; the torch version constraint discussed there is separate).
Problem
maybe_empty_cache(force=True, collect_garbage=True)runs a fullgc.collect(), which walks every tracked object. MiniMax-H3 forces one per request in_offload_transformer. With torch 2.9.0/2.9.1 anduse_compile/vae_use_compile, each compiled call leaks two small lists that a collection cannot free (pytorch/pytorch#174468), about 13.7k per 768p / 120-frame request. Each forced GC re-walks everything leaked so far, so it grows by ~1.6–3.6 ms per request: about +2.9 s per request per day of uptime.BaseRunner._maybe_freeze_gc()already freezes the steady-state graph once after the first run, but objects that accumulate after that still land in the normal generations and are re-walked on every forced collection.Also: a frozen runner cannot be released (latent since #1188)
Since #1188,
BaseRunner._maybe_freeze_gc()freezes the process once after a runner's firstrun_pipelineand never unfreezes. A runner holds itself in reference cycles (self.run_input_encoderis one of its own bound methods), so a runner dropped or replaced in the same process is never freed:gc.collect()skips frozen objects,__del__never runs, and its models stay in memory. LightX2V's own entry points do not replace runners (the server worker builds one per process,create_generatorrefuses a second), so this only affects external code that swaps runners in a long-lived process. Details and a repro are in #1678. Disablinggc_freeze_survivorsalone would not fix it, because the one-shot freeze from #1188 would still pin the runner.Change
maybe_empty_cachenow calls a newDefaultRunner._collect_garbage():gc.collect(), thengc.freeze(), so the next collection only walks objects created since;gc_full_collect_everycalls (default 200,0disables) it runsgc.unfreeze()first, so cyclic garbage among frozen objects is still reclaimed. Reference counting frees frozen objects as usual;gc_freeze_survivors: falserestores the plaingc.collect();base_runner.release_frozen_gc()(unfreeze + collect) is available for code that drops or replaces a runner in a long-lived process. A runner holds itself in reference cycles (self.run_input_encoderis one of its own bound methods), so once frozen, by this change or by the existing one-shot_maybe_freeze_gc(), dropping it alone does not free it; the next periodic pass orrelease_frozen_gc()does;lazy_load/unload_moduleskeep the plaingc.collect(), same as_maybe_freeze_gc(), because they rebuild the model per request and a frozen model in a reference cycle would otherwise wait for the next full pass.Changes are in
default_runner.pyandbase_runner.py. Freezing is on by default and applies to everyDefaultRunnersubclass whosemaybe_empty_cachecall collects garbage (Wan, LTX, LingBot, ...), not only MiniMax-H3; setgc_freeze_survivors: falseto restore the previous behaviour. It only reduces how many objects each collection walks; the trade-off is that objects frozen while alive and later turned into cyclic garbage are released up togc_full_collect_everycalls later. Paths that drop models inside a request and then force a collection (clean_lazy_load_warmupin Wan / QwenImage) run only underlazy_load, which skips freezing. A/B validation was done on MiniMax-H3 only.Testing
MiniMax-H3 fl2av (768p, 120 frames,
use_compile+vae_use_compile,cpu_offload) on torch 2.9.1, one worker, same inputs:Per-request pipeline time stayed flat after the change.
gc.get_freeze_count()grows by ~13.8k per request, matching the leaked lists. Upgrading torch to ≥ 2.10 removes the leak itself; this change keeps the forced GC constant regardless of what accumulates.🤖 Generated with Claude Code