Repository navigation
perf(megatron): add manual_gc to align Python GC across policy ranks - #4857
Conversation
An automatic full Python GC lands on whichever rank crosses its threshold, and every other rank then waits for it in the next collective. On a 32-node NM4 Nano SFT run, ~1 s gen-2 collections hit up to 90 of 128 ranks in a single step, stretching 66 s steps to 75-85 s. policy.megatron_cfg.manual_gc mirrors Megatron's manual_gc: the Megatron policy workers disable automatic GC and every rank collects together at the end of every manual_gc_interval-th finish_train_step (0 = never collect). Forked children re-enable GC. Off by default. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: sniverty <[email protected]>
|
/ok to test cdd9b68 |
There was a problem hiding this comment.
Overall: the diagnosis is solid and the mechanism is faithful to upstream on the path where it fires — I verified the collect position, the disable-site semantics, the interval >= 0 validation, and the modulo cadence all match Megatron-LM at the pinned SHA, and the register_at_fork guard improves on it. The one substantive issue is that the collection hook sits on finish_train_step, which the v1 entrypoints never reach, so the feature cannot fire on sft.yaml or rm.yaml as shipped.
Also checked and clean, so you don't have to re-litigate them: gc.disable() does not leak into the vLLM workers (separate Ray actors, separate processes); the fork handler cannot stack (no max_restarts, no __setstate__); rank counters cannot desync on the SC path (run_all_workers_single_data fans out to every rank); config coverage across all 22 megatron_cfg exemplars and 7 reference configs is complete; and the repo's pre-commit suite (ruff, ruff-format, pyrefly) passes on this diff.
Generated by Claude Code
…erval, enable GC late Addresses review feedback on manual_gc: - The monolithic train() (v1 SFT, RM, DPO) now calls _maybe_run_manual_gc() after each optimizer step, so manual_gc no longer disables GC without ever collecting there. Both paths share one counter: the interval counts optimizer steps on train() and finish_train_step alike. - manual_gc_interval is read with .get() like manual_gc, and manual_gc: true now requires a positive integer interval (missing, 0, negative, bool or str raise a ValueError at worker setup). The exemplar and reference configs ship 10 instead of 0, so enabling manual_gc alone works. - The config is validated early in __init__, but gc.collect() + gc.disable() now run at the end of __init__, so model, optimizer and checkpoint setup run with automatic GC and their garbage is collected before it is disabled. - Tests: the off case covers manual_gc: false and the shipped defaults and checks no instance state is written; invalid intervals are parametrized; the finish_train_step error path asserts the MTP-scale reset and hook restore; structural tests pin the __init__ and train() call sites; a GPU test runs train() with manual_gc and checks GC is off and every optimizer step is counted on every worker. - docs/design-docs/training-backends.md: new "Manual garbage collection" subsection with interval guidance and the host-memory trade-off. Signed-off-by: sniverty <[email protected]>
saumishr
left a comment
There was a problem hiding this comment.
Thanks for addressing the comments!
terrykong
left a comment
There was a problem hiding this comment.
Thanks for this, @snivertynv, and for the quick fixes in b4c6733. Two places the flag still doesn't land right, neither covered in the earlier review:
- (C2) OPD/MOPD teacher workers copy the student's config, turn GC off, and never collect.
- (C4) The PPO Megatron value worker has the same stall but ignores the flag; please wire it in here too.
Visual explainer: https://terrykong.github.io/gh-pages-poc/terryk/pr-4857-manual-gc.html
Visual explainer: https://terrykong.github.io/gh-pages-poc/terryk/pr-4857-v1-never-collects.html
Generated by Claude Code
…alue worker Addresses review feedback (C2, C4): - OPD/MOPD teacher workers copy the student's policy config, so a student's manual_gc: true disabled GC on teachers that never train and so never collect. TeacherWorkerGroup now resets the inherited manual_gc before applying the teacher's megatron_cfg_overrides, and raises if a teacher override turns it back on. - The PPO Megatron value worker trains across data-parallel ranks like the policy and had the same stall, but ignored the flag. The manual GC methods move into ManualGCMixin (nemo_rl/models/megatron/manual_gc.py), inherited by both workers; the value worker validates early in __init__, enables at the end of __init__, and collects after each optimizer step in train(). ppo_math_1B.yaml and its reference config gain the keys under value.megatron_cfg. - Tests: the manual_gc unit and structural tests run for both workers; two teacher tests cover the reset and the rejected override. Docs updated. Signed-off-by: sniverty <[email protected]>
|
/ok to test 87ed093 |
…whitelist Signed-off-by: sniverty <[email protected]>
|
/ok to test cc291fd |
run_all_workers_single_data rejects positional args (issue #582), so the GPU test called __ray_call__ through it and failed before checking anything. Call each worker actor's __ray_call__ directly, with the reader defined inside the helper so Ray pickles it by value. Signed-off-by: sniverty <[email protected]>
|
/ok to test dfc1617 |
|
/ok to test 42db556 |
In a distributed training job, an automatic full python garbage collection lands on whichever rank crosses its threshold which can leave every other rank waiting for it in the next collective. On a 32-node SFT run, ~1s GC collections hit up to 90 of 128 ranks in a single step at different points, stretching 66 s steps to 75-85 s.
policy.megatron_cfg.manual_gcmirrors Megatron'smanual_gc: the Megatron policy workers disable automatic GC and every rank collects together at the end of every manual_gc_interval-thfinish_train_step(0 = never collect).This ensures that all ranks collect at the same time, removing the scenario where staggered GCs slow down everyone else.
The screenshot below shows how in a 4-rank setup, when the last rank at the bottom hits a GC event in the middle of a training step that lasts about 1 second (no GPU activity), the other 3 ranks above it are stuck on a NCCL synchronization call.
Perf measurements - results below for text-only SFT through SFT v2 (examples/run_sft_v2.py), Megatron policy, 32 nodes × 4 GPUs (128 ranks), 100 steps, a save every 10 steps. Manual GC ran with manual_gc_interval: 10 (an env-flag prototype with the same logic as this PR).
With manual GC, the ten collection steps together cost 11.5 s over a normal step, equal to each rank's own total GC pause time (median 11.5 s): aligned collections cost about one rank's collection time. With automatic GC, the same 90 steps took ~190 s longer, against at least ~26 s of GC work per rank, so most of the automatic-GC cost was ranks waiting on another rank's collection. Peak RSS was unchanged at this interval. Only interval 10 and only the SFT v2 entrypoint were measured; v1 run_sft.py was not.
Usage
Before your PR is "Ready for review"
Pre checks: