manual_gc makes every rank collect garbage at the same momentNVIDIA-NeMo/RL #4857
· permalinks pin the PR head b4c6733.
policy.megatron_cfg.manual_gc. It turns automatic GC off, and every rank collects together once every manual_gc_interval optimizer steps, on both v1 (train()) and v2 (finish_train_step) trainers.b4c6733. OPD/MOPD teachers turn GC off and never collect, and the PPO value worker ignores the flag. See page 2.Two pages on this PR. This one: what the flag does and how it compares to automatic GC.
A training step has many sync points: every rank must reach the same
collective
before any of them can go on. If rank 3 starts a 1 s collection mid-step, ranks 0–2 sit idle at the next collective for that second. The PR's trace on 32 nodes saw this on up to 90 of 128 ranks in one step, stretching 66 s steps to 75–85 s.
The flag copies Megatron-LM's own
manual_gc:
turn GC off at startup, then collect on every rank at the same step boundary.
manual_gc_interval: 2Three small methods on the Megatron policy worker. The start of setup checks the config, the end of setup turns GC off, and each optimizer step counts toward the interval. Forked child processes (data workers) get normal GC back.
@@ megatron_policy_worker.py L891 · start of __init__ self.timer = Timer(context={"worker": "megatron_policy", "rank": self.rank}) + self._manual_gc_interval_from_cfg() # bad interval fails before slow setup @@ megatron_policy_worker.py L1161 · end of __init__ log_gpu_memory_diagnostics(...) + self._maybe_enable_manual_gc() @@ megatron_policy_worker.py L1571 · v1 train(), after optimizer.step() + self._maybe_run_manual_gc() @@ megatron_policy_worker.py L2273 · v2 finish_train_step, after a successful step + self._maybe_run_manual_gc() return result @@ megatron_policy_worker.py L2276-L2325 · new methods + gc.collect() # free setup garbage first + gc.disable() + os.register_at_fork(after_in_child=gc.enable) # forked children keep normal GC + self._manual_gc_interval = interval + ... + self._manual_gc_steps += 1 + if self._manual_gc_steps % self._manual_gc_interval == 0: + gc.collect()
Code:
_manual_gc_interval_from_cfg / _maybe_enable_manual_gc / _maybe_run_manual_gc,
config fields in
MegatronConfig.
Same rule as Megatron-Bridge's
training loop. Unlike Megatron, the PR rejects an interval of 0:
NeMo-RL has no eval-time collect to fall back on, so 0 would mean GC off with nothing ever collecting.