Where manual_gc still doesn't collect, or doesn't apply

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

Before
No manual_gc flag. Python's automatic GC runs in every worker.
This PR
Turns GC off in every Megatron policy worker at the end of setup, and collects every N optimizer steps in both train() (v1) and finish_train_step (v2). The v1 half landed in b4c6733.
Status
Partly fixed. v1 and v2 trainers now collect. OPD/MOPD teachers still never collect (C2), and the PPO value worker ignores the flag (C4). Both are in the review.

Two pages on this PR. This one: which workers get the collection.

The flag has two halves. The end of setup turns automatic GC off for every Megatron policy worker (__init__ L1161). The periodic collection runs after an optimizer step, in train() L1571 and finish_train_step L2273. A worker that turns GC off but never trains never collects. A worker that isn't a policy worker never reads the flag at all.

Which workers collect, with manual_gc_interval: 2 over 5 steps

trainer / workerworker methodcollections in steps 1 โ€ฆ 5 12345 run_sft_v2.pysingle-controller GRPO / PPO finish_train_step() counts steps, collects every 2nd 2 โœ“ run_sft.py, run_rm.pyDPO, v1 GRPO โ€” fixed in b4c6733 train() collects after optimizer.step() 2 โœ“ OPD / MOPD teacherscopy the student's config โ€” C2 deepcopy(policy_config) get_logprobs only, never trains โ€” โ€” โ€” โ€” โ€” 0 โœ— PPO value workernever reads the flag โ€” C4 value train() automatic GC, rank by rank auto manual_gc collect, all ranks together automatic GC: one rank at a time โ€”no collect: garbage piles up Red: GC is off and never runs, so objects that point at each other (and any GPU tensors they hold) stay in memory for the whole run. Gray: no leak, but the other ranks still wait on each pause.

The fix โ€” proposed, not in the PR

Nothing below is implemented.

Where the fix lands: RL only. teacher_worker_group.py for C2, and megatron_value_worker.py plus a shared mixin for C4. No Megatron change.

@@ teacher_worker_group.py L159-L165 ยท teacher config builder
         cfg["megatron_cfg"]["expert_model_parallel_size"] = (
             teacher_cfg.expert_model_parallel_size
         )
+        # The student's manual_gc is for its training steps; a teacher never trains.
+        cfg["megatron_cfg"]["manual_gc"] = False

         # Apply any additional megatron config overrides from teacher config.
         for key, value in teacher_cfg.megatron_cfg_overrides.items():
             cfg["megatron_cfg"][key] = value
+        if cfg["megatron_cfg"]["manual_gc"]:
+            raise ValueError(
+                f"Teacher '{self.alias}': manual_gc is not supported on teachers. "
+                "They never run a training step, so GC would be disabled with "
+                "nothing ever collecting."
+            )
@@ megatron_value_worker.py L231 ยท class line (mixin holds the three policy-worker helpers)
-class MegatronValueWorkerImpl(TQWorkerMixin, AbstractPolicyWorker):
+class MegatronValueWorkerImpl(ManualGCMixin, TQWorkerMixin, AbstractPolicyWorker):
@@ megatron_value_worker.py L490 ยท end of __init__
         print(f"MegatronValueWorker initialized on rank {self.rank}")
+        # Setup ran with automatic GC; switch to lockstep collection now.
+        self._maybe_enable_manual_gc()
@@ megatron_value_worker.py L609-L620 ยท train(), once per optimizer step
                 if not eval_mode:
                     update_successful, grad_norm, num_zeros_in_grad = (
                         self.optimizer.step()
                     )
                     ...
                     grad_norm = reduce_max_stat_across_model_parallel_group(
                         grad_norm, mp_group=pg_collection.mp
                     )
+                    self._maybe_run_manual_gc()

The teacher hunk handles two cases. A manual_gc: true copied from the student is reset, so the frozen teacher keeps normal GC. A manual_gc: true set on purpose for a teacher (through default_teacher_cfg or teacher_overrides) raises at setup instead of being silently dropped, because a teacher has no training step to collect after. The value-worker hunks copy what the policy worker does: enable at the end of setup, then collect once per optimizer step, so every value rank pauses at the same point. The value worker also needs the early config check at the top of __init__, and ppo_math_1B.yaml needs the two keys under value.megatron_cfg. ManualGCMixin is a proposed name.

๐ŸŸ  C2 review comment โ€” teacher configs (anchored at megatron_policy_worker.py:2311)

๐ŸŸก C4 review comment โ€” value worker (anchored at policy/__init__.py:392)

So what. With the flag on, MOPD runs every teacher with no garbage collection, so teacher host memory grows for the whole job. PPO gets the lockstep collect on the policy ranks only, and the value ranks keep stalling each other. Until C2 lands, don't turn the flag on for OPD/MOPD runs.