manual_gc still doesn't collect, or doesn't applyNVIDIA-NeMo/RL #4857
ยท permalinks pin the PR head b4c6733.
manual_gc flag. Python's automatic GC runs in every worker.train() (v1) and finish_train_step (v2). The v1 half landed in b4c6733.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.
manual_gc_interval: 2 over 5 stepsNothing 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)