Repository navigation
[rl] Controller GC: freeze the setup heap, fold generator metrics per rollout and group - #5290
Conversation
… rollout and group A full collection walks every object the GC tracks in the controller. Two kinds dominated: - ~1M objects alive after setup (imports, configs, the dataset), which never die; - 18 generator Metric records per generation, which the Verifiers GenerationServer kept for the whole rollout and compute_rollout_metrics copied into the group's metrics. Controller.run() now calls gc.collect() + gc.freeze() before its loops. A new m.fold merges the Mean/Max/Min/Sum records that share a key into one record per type; MetricsProcessor reduces the result to the same numbers. GenerationServer and compute_rollout_metrics fold generator metrics.
| # Setup objects (modules, configs, dataset, actor handles) live until exit. Freeze them | ||
| # so full GCs skip them: ~1M objects, ~0.5 s per full GC (Python 3.12, x86). | ||
| gc.collect() | ||
| gc.freeze() |
There was a problem hiding this comment.
instead, to unify things a bit, could we call / add to this API?
There was a problem hiding this comment.
reply from claude: Done. Added GarbageCollector.freeze(reason) next to collect: a full collect, then gc.freeze(), with the time logged like collect. The controller calls it now. I didn't give the controller a scheduled GarbageCollector like the trainer has, because its __init__ calls gc.disable() and the controller makes garbage on every request. Happy to switch if that's what you meant.
|
|
||
|
|
||
| def fold(metrics: Iterable[Metric]) -> list[Metric]: | ||
| """Merge the `Mean`, `Max`, `Min` and `Sum` records that share a key into one record per type. |
There was a problem hiding this comment.
document the motivation here
There was a problem hiding this comment.
reply from claude: Added a short "Why" to the docstring. Each generate call returns 16 records. Without folding, a multi-turn rollout keeps every turn's records until the step is logged, and every full GC walks them: ~180K of the ~220K objects added after setup, at 12 turns and ~320 rollouts in flight.
# Conflicts: # torchtitan/rl/controller.py
- GarbageCollector.freeze(reason): full collection, then gc.freeze(); the controller calls it instead of gc directly. - fold's docstring says why it exists: unfolded, multi-turn rollouts hold every turn's generator records until the step is logged.
TLDR: Every full garbage collection in the RL controller walks every object the collector tracks. After setup that's ~1M objects that never die, plus 16 generator-metric records per generation that a Verifiers rollout keeps until it ends. This PR freezes the setup objects out of the collector and folds each rollout's generator metrics into one
Meanand oneMaxper key. In a local multi-turn run, the longest GC pause drops from 0.98 s to 0.06 s, and GC time from 1.63 to 0.57 s per minute.Problem
Meanand aMaxrecord, so 48 tracked objects. The VerifiersGenerationServerkeeps all of them until the rollout ends.compute_rollout_metricsputs every turn's records into the group's metrics, which live until the step is logged.Solution
Controller.run()calls the newGarbageCollector.freeze(reason)before starting its loops: a full collection, thengc.freeze(). The setup objects move to a generation the collector never scans.metrics.fold(metrics)merges theMean,Max,MinandSumrecords that share a key into one record per type.MetricsProcessorreduces the result to the same numbers.GenerationServerfolds each generation's metrics into the rollout's.compute_rollout_metricsfolds the turns' generator metrics.Results
The real loop on 2 H100s: Qwen3-0.6B, trainer on one GPU and one generator on the other. A Verifiers env runs 12-turn episodes of fake tool output, so no sandboxes are needed. 64 rollouts per step, ~320 in flight, 30 steps per arm. The census thread logs per-generation GC time and object counts every 30 s.
You may ask:
run()and not intrain.py? Launchers that build theControllerthemselves callsetup_async()andrun()without going throughtrain.main().Metriccheaper (__slots__)? Slots would save one object of the three per record. Folding makes a rollout's record count stop growing with its turns.NOTE: not run at the ~2,000-rollouts-in-flight scale of the agentic run above, and that run's heap was never inventoried. How much of its 4.7-8.6 s these two changes remove is an estimate.
NOTE: a Verifiers rollout whose episode raises before
pop_generation_metadatastill leaves its metadata inGenerationServer. After folding that's ~20 objects per failed rollout.Test plan