Skip to content

fix(grpo): port Gym log dir fix (#3956), warn on overridden output dirs - #4712

Merged
yfw merged 10 commits into
mainfrom
pjin/gym-log-dir-rebase-20261012
Oct 27, 2026
Merged

yfw merged 10 commits into
mainfrom
pjin/gym-log-dir-rebase-20261012

Conversation

@pjin-nvidia

Copy link
Copy Markdown
Contributor

Port of 6401e0f (#3956, super-v3.5-posttraining) onto main.

When NeMo Gym is enabled, setup() sets env.nemo_gym.nemo_gym_log_dir to
<logger.log_dir>/nemo_gym so that Gym server logs land in the run's log
directory. Gym writes server log files only when this key is set, and
the Super and Super-Omni launchers on main never set it.

The override replaces any configured value. Report replaced output
paths with a soft warning instead of changing them silently:

  • setup(): a configured nemo_gym_log_dir that differs from the run's
    Gym log directory, e.g. the logs/nemo_gym hardcoded in the Ultra and
    Lightning recipes, or a launcher's own directory.
  • run_grpo_nemo_gym.py: logger.log_dir, checkpointing.checkpoint_dir,
    or nemo_gym_log_dir from the config file changed by command-line
    overrides, and logger.log_dir replaced by the per-run exp_NNN
    subdirectory.

Conflicts: main added _validate_seq_logprob_error_in_loss() next to the
moved should_use_nemo_gym() call, and the dynamo setup test now mocks
build_nemo_gym_actors (a shard set) instead of spinup_nemo_gym_actor.

Port of 6401e0f (#3956, super-v3.5-posttraining) onto main.

When NeMo Gym is enabled, setup() sets env.nemo_gym.nemo_gym_log_dir to
<logger.log_dir>/nemo_gym so that Gym server logs land in the run's log
directory. Gym writes server log files only when this key is set, and
the Super and Super-Omni launchers on main never set it.

The override replaces any configured value. Report replaced output
paths with a soft warning instead of changing them silently:

- setup(): a configured nemo_gym_log_dir that differs from the run's
  Gym log directory, e.g. the logs/nemo_gym hardcoded in the Ultra and
  Lightning recipes, or a launcher's own directory.
- run_grpo_nemo_gym.py: logger.log_dir, checkpointing.checkpoint_dir,
  or nemo_gym_log_dir from the config file changed by command-line
  overrides, and logger.log_dir replaced by the per-run exp_NNN
  subdirectory.

Conflicts: main added _validate_seq_logprob_error_in_loss() next to the
moved should_use_nemo_gym() call, and the dynamo setup test now mocks
build_nemo_gym_actors (a shard set) instead of spinup_nemo_gym_actor.

Signed-off-by: Peter Jin <[email protected]>
…mo_gym.py

Launch scripts and test scripts pass logger.log_dir and the checkpoint
dir on the command line on purpose, and every run gets a fresh exp_NNN
log directory, so these warnings fired on nearly every run without
telling anyone anything. The file is back to main's version.

The one override that does deserve a warning, a configured Gym log dir
that setup() replaces, is reported where the replacement happens.

Signed-off-by: Peter Jin <[email protected]>
…ion setup too

GRPO setup() put the Gym server logs under the run's log directory, but
the single-controller setup and distillation setup built their Gym
actors without setting nemo_gym_log_dir, so those runs wrote Gym server
logs only when the config set it by hand. All three now call one helper,
set_nemo_gym_log_dir(), before the Gym actors are built (and, in GRPO
and distillation, before the config is logged, so the logged config
shows the real location).

The helper makes the path absolute on the driver. Gym resolves a
relative path against the Gym actor's working directory, which is the
raylet's, not the driver's, when the driver attaches to a cluster
started elsewhere. A configured value is still replaced, with a warning.

Signed-off-by: Peter Jin <[email protected]>
With nemo_gym_log_dir set, which setup() now always does, Gym launches
each server as `set -o pipefail; (<cmd>) 2>&1 | tee -a <log>` under
bash, and RunHelper.shutdown() signals only that bash process. The
server never gets the signal and outlives the Gym actor, reparented to
init, along with any placement group it holds as a Ray driver, such as
a local vLLM judge's GPUs. Ray kills only an actor's direct children,
so nothing stops these servers on a cluster without process-group
cleanup.

NemoGym.shutdown() now records the process trees of the servers Gym
launched before calling RunHelper.shutdown(), then interrupts whatever
is left and kills it if it ignores that. Only Gym's own processes are
signalled; signalling every descendant of the actor process would also
hit unrelated children, such as a local Ray cluster in a test session.
tee is left to exit at EOF so the log keeps the servers' shutdown
output.

Gym has no public accessor for the processes it launched, so this reads
RunHelper._processes; a nemo_gym-marked test pins that contract against
the Gym in the venv.

Signed-off-by: Peter Jin <[email protected]>
…ightning recipes

setup() now always puts the Gym server logs under the run's log
directory, replacing any configured nemo_gym_log_dir with a warning, so
these settings no longer had any effect and would only add that warning
to every run. Removed from the Ultra and Lightning 3.5 recipe YAMLs and
their launch scripts, and from the k8s Ultra pipeclean examples, whose
Gym logs now land under ${CHECKPOINT_DIR}/logs/exp_NNN/nemo_gym with the
rest of the run's logs.

Signed-off-by: Peter Jin <[email protected]>
@copy-pr-bot

copy-pr-bot Bot commented Oct 13, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@pjin-nvidia
pjin-nvidia marked this pull request as ready for review October 13, 2026 19:45
@yfw yfw added the CI:Lfast Runs a fast test suite and re-use nightly `main` container (but sync dependencies to PRs version) label Oct 15, 2026
Comment thread nemo_rl/environments/nemo_gym.py Resolved
…eardown fits its budget

At the end of a run shutdown_environments() gives each Gym actor
DEFAULT_ENV_SHUTDOWN_TIMEOUT_SECONDS (10 s) and then kills it. Behind
Gym's tee wrapper, which setup() now turns on for every run, a server
never sees the SIGINT RunHelper.shutdown() sends, so Gym waits out its
one-second grace period on each server in turn. With ten or more
servers (13 in grpo_nanov3.yaml, about 53 in the Super recipe) the
actor was killed before the cleanup that followed Gym's shutdown could
run. Ray's process-group cleanup, on by default since Ray 2.57, then
kills the servers without a graceful shutdown; where it is off they
keep running.

The servers are now interrupted before Gym's shutdown: every process
below a command Gym launched, except tee. Each wrapper then exits as
soon as its server does. The commands themselves are left for Gym to
signal: without a log dir a command is the server, and a second SIGINT
makes uvicorn skip the application's shutdown. Whatever is still
running afterwards gets until NEMO_GYM_LEFTOVER_SERVER_TIMEOUT_S, now
half of that 10 s budget, and is then killed.

Also:
- Skip commands that have already exited. Gym's poll(), which the
  single-controller watchdog runs through NemoGym.health_check(), reaps
  a command that died, and its pid may since belong to an unrelated
  process.
- Read the process table once rather than once per server. With 40
  servers on a node with ~2,700 processes, the per-server scans took
  2.2 s of the budget.
- Tolerate EINVAL from psutil 7.2.2's Process.wait()
  (giampaolo/psutil#2715, fixed upstream but not yet released).

Against Gym's own launch and shutdown code at the pinned commit, 12
wrapped servers now stop in 0.2 s instead of 12.6 s, and 53 in 0.4 s,
each after a single SIGINT.

Signed-off-by: Peter Jin <[email protected]>
@yfw

yfw commented Oct 16, 2026

Copy link
Copy Markdown
Contributor

/ok to test 991c794

@nemo-automation-bot
nemo-automation-bot Bot requested a review from a team October 16, 2026 08:06
yfw
yfw previously approved these changes Oct 16, 2026
@yfw

yfw commented Oct 16, 2026

Copy link
Copy Markdown
Contributor

/ok to test ea78422

@yfw
yfw enabled auto-merge (squash) October 16, 2026 08:10
@yfw

yfw commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

/ok to test f7227ee

@yfw
yfw merged commit 09f1594 into main Oct 27, 2026
92 checks passed
@yfw
yfw deleted the pjin/gym-log-dir-rebase-20261012 branch October 27, 2026 07:26
jthomson04 added a commit to jthomson04/RL that referenced this pull request Oct 27, 2026
Resolve tests/unit/algorithms/test_grpo.py: main (NVIDIA-NeMo#4712) added Gym
log-dir assertions to test_setup_initializes_noncolocated_dynamo_with_nemo_gym,
which this branch deletes with backend: dynamo. The log-dir fix is not
Dynamo-specific, so its assertions move to the surviving Gym setup test,
test_setup_starts_nemo_gym_for_trtllm, with the same parametrization.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: jthomson04 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI:Lfast Runs a fast test suite and re-use nightly `main` container (but sync dependencies to PRs version)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants