How manual_gc makes every rank collect garbage at the same moment

NVIDIA-NeMo/RL #4857 · permalinks pin the PR head b4c6733.

Before
Python garbage collection runs on its own schedule in each Megatron training process. A ~1 s collection on one rank makes all the other ranks wait for it at the next sync.
This PR
Adds an opt-in flag, 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.
Status
Partly fixed. v1 and v2 trainers collect as of 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.

Background. Python frees most objects right away. Objects that point at each other in a loop are only freed by the garbage collector, which runs when a process has created enough new objects. In a training job every rank is its own process, so each one decides on its own. Dotted words have a short explanation: hover or tap them.

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.

Same 4 ranks, 3 steps: automatic GC vs. manual_gc_interval: 2

Automatic GC (before, and the default) step 1step 2step 3 rank 0rank 1 rank 2rank 3 GC GC GC GC One rank's pause becomes everyone's pause. Step 3 pays twice: rank 0's GC, then rank 2's. manual_gc: true, manual_gc_interval: 2 (this PR, v2 trainers) step 1step 2step 3 rank 0rank 1 rank 2 GCGCGC (rank 3 is the same) finish_train_step → _maybe_run_manual_gc() collect after step 2 only: training GC on one rank idle, waiting at a collective GC on all ranks together

How the PR does it

Three 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.

So what. On v1 and v2 policy training, the flag turns many scattered, stacked pauses into one shared pause every N steps. The trade-off is memory: garbage builds up between collections, so a large interval needs a check of host memory. The PR has no run with the flag on yet; an earlier review asks for step time and host memory, flag off vs on. OPD/MOPD teachers and the PPO value worker don't get it right yet: page 2.