nemo_rl.models.megatron.manual_gc#

Megatron-style manual garbage collection for Megatron training workers.

An automatic full Python GC lands on whichever rank crosses its threshold, and every other rank then waits for it in the next collective. With megatron_cfg.manual_gc set, a worker disables automatic GC and every rank collects together every megatron_cfg.manual_gc_interval optimizer steps.

Module Contents#

Classes#

ManualGCMixin

Manual GC for Megatron workers that train across data-parallel ranks.

API#

class nemo_rl.models.megatron.manual_gc.ManualGCMixin[source]#

Manual GC for Megatron workers that train across data-parallel ranks.

The host class must set self.cfg (with a "megatron_cfg" entry) and make three calls:

  • self._manual_gc_interval_from_cfg() early in __init__, to fail on a bad config before the expensive setup;

  • self._maybe_enable_manual_gc() as the last statement of __init__, so model, optimizer and checkpoint setup run with automatic GC on;

  • self._maybe_run_manual_gc() once after every optimizer step.

cfg: Any#

None

_manual_gc_interval: Optional[int]#

None

_manual_gc_steps: int#

0

_manual_gc_interval_from_cfg() → Optional[int][source]#

Return megatron_cfg.manual_gc_interval if manual GC is on, else None.

Raises:

ValueError – if manual_gc is True and manual_gc_interval is missing or not a positive number of optimizer steps. NeMo-RL has no equivalent of Megatron’s manual_gc_eval collections, so a non-positive interval would disable automatic GC with no collection ever running.

_maybe_enable_manual_gc() → None[source]#

Disable automatic Python GC when megatron_cfg.manual_gc is set.

Mirrors Megatron’s manual_gc: collections then run on every rank at the same point (see _maybe_run_manual_gc) instead of stalling the other ranks one at a time. Called at the end of __init__, so setup runs with automatic GC on and the collection here frees its garbage. Forked children re-enable GC so subprocesses such as data workers keep the default behavior.

_maybe_run_manual_gc() → None[source]#

Collect every manual_gc_interval optimizer steps; every rank runs it in lockstep.