Skip to content

perf(sc): Load the training dataset while generation, Gym and the trainer start - #4596

Merged
yfw merged 6 commits into
mainfrom
sniverty/sc-startup-overlap
Oct 16, 2026
Merged

yfw merged 6 commits into
mainfrom
sniverty/sc-startup-overlap

Conversation

@snivertynv

Copy link
Copy Markdown
Contributor

What does this PR do ?

Takes the training-dataset load off the SingleController startup critical path on the NeMo-Gym + vLLM + non-colocated path: generation, Gym and the trainer start first, and the dataset loads on the driver while they build.

Before: setup_single_controller loaded the dataset and built the dataloader before starting any worker, although no worker build needs them. Generation and Gym need only URLs and the tokenizer, and the trainer needs only Megatron's train_iters. For a large data blend that load is minutes of single-threaded driver work ahead of every worker (111 s for a 22 GB blend).

After, on the NeMo-Gym + vLLM + non-colocated path:

  • The worker builds (generation, Gym, trainer) are submitted first, and _load_training_data() runs on the driver while they build.
  • Speculative train_iters: the trainer is built with Megatron train_iters from the unclamped max_num_steps. Once the dataset is in, the clamped value (max_num_epochs * len(dataloader)) is compared. If the clamp bound (the run asked for more steps than its data holds), a warning is printed and the trainer (and PPO critic) is shut down and rebuilt with the right value. In the normal case they match and nothing is rebuilt.
  • Thread safety: a new pure helper, _megatron_train_iters, computes the values without writing the config the trainer build is reading on another thread. _maybe_inject_megatron_train_iters now uses it. It returns None for a model that doesn't train with Megatron, so a non-Megatron trainer is never rebuilt.
  • Every other path (colocated, Megatron/SGLang generation, native environments) keeps the original order.

Issues

Usage

No new configuration; the change applies automatically on the NeMo-Gym + vLLM + non-colocated path. When max_num_steps exceeds what the data holds, startup logs:

⚠ max_num_steps clamped to <N> by the dataset; Megatron train_iters (<old>, <value>) -> (<N>, <value>) (policy, value). Rebuilding the trainer. Set max_num_steps <= max_num_epochs * steps per epoch to avoid this.

Before your PR is "Ready for review"

Pre checks:

  • Make sure you read and followed Contributor guidelines
  • Did you write any new necessary tests?
  • Did you run the unit tests and functional tests locally? Visit our Testing Guide for how to run tests
  • Did you add or update any necessary documentation? Visit our Document Development Guide for how to write, build and test the docs.

Additional Information

New tests (tests/unit/single_controller/test_setup.py):

  • test_megatron_train_iters_helper_reads_without_writing: the helper doesn't write the config, and returns None when Megatron is off.
  • test_gym_vllm_loads_dataset_while_trainer_builds: the mocked dataset load waits for the trainer build to start, so the old order (load first) would fail.
  • test_native_path_loads_dataset_before_building: other paths keep the original order.
  • test_gym_vllm_keeps_speculative_trainer_when_clamp_does_not_bind: one trainer build, with the speculative train_iters.
  • test_gym_vllm_rebuilds_trainer_when_clamp_binds: train_iters 1000 → 8, the stale trainer shut down, the warning printed.
  • test_gym_vllm_dataset_load_failure_shuts_down_gym: a load failure on the new path re-raises and shuts Gym down.

The existing test_nemo_gym_wires_env_handle also runs the changed path and checks that validate_dataset_agent_coverage still receives both datasets.

Measured with the same change on an earlier NeMo-RL base (Nano 3.5 RLVR, 86 nodes, NeMo-Gym with external judge pools):

  • about 90 s less serial driver work before the workers start;
  • Megatron ready 83 s earlier (415 s → 332 s after allocation start).

…iner start

On the NeMo-Gym + vLLM + non-colocated path, setup_single_controller loaded
the training dataset and built the dataloader before starting any worker,
although no worker build needs them: generation and Gym need only URLs and
the tokenizer, and the trainer needs only Megatron's train_iters. For a
large data blend that load is minutes of single-threaded driver work on the
startup critical path (111 s for a 22 GB blend).

Start generation, Gym and the trainer first and load the dataset while they
build. The trainer is built with train_iters from the unclamped
max_num_steps. Once the dataset is in, the clamped value is compared; if the
clamp bound (a run asking for more steps than max_num_epochs *
len(dataloader) holds), the trainer is shut down and rebuilt with the right
value. A new pure helper, _megatron_train_iters, computes the values without
writing the config the trainer build is reading, and returns None for a
model that does not train with Megatron, so a non-Megatron trainer is never
rebuilt. Every other path keeps the original order.

On an 86-node Nano 3.5 RLVR run (on an earlier base) this removed ~90 s of
serial driver work ahead of the workers and had Megatron ready 83 s earlier.

Signed-off-by: sniverty <[email protected]>
@snivertynv snivertynv self-assigned this Oct 8, 2026
@snivertynv snivertynv added the CI:L1 Run doctests, unit tests, and functional tests label Oct 8, 2026
@copy-pr-bot

copy-pr-bot Bot commented Oct 8, 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.

@snivertynv

Copy link
Copy Markdown
Contributor Author

/ok to test 5647e21

@snivertynv
snivertynv requested a review from yfw October 8, 2026 15:36
@snivertynv

Copy link
Copy Markdown
Contributor Author

/ok to test 9c938ec

snivertynv and others added 2 commits October 16, 2026 05:56
On the overlapped path the trainer is first built with train_iters from the unclamped max_num_steps. A resume restores Megatron's LR scheduler from the checkpoint, which asserts that the saved horizon (computed from the clamped value) equals train_iters, so the first build failed before the rebuild could correct it whenever the dataset caps max_num_steps. Resumes now load the dataset first, as on main; the critic's resume paths come from the same checkpoint, so this covers PPO too.

Test: a resumed Gym + vLLM run builds the trainer once, with the clamped train_iters.
Signed-off-by: sniverty <[email protected]>
@snivertynv

Copy link
Copy Markdown
Contributor Author

/ok to test f3c00ba

@yfw
yfw enabled auto-merge (squash) October 16, 2026 21:30
@yfw
yfw merged commit bc1ddb7 into main Oct 16, 2026
210 of 212 checks passed
@yfw
yfw deleted the sniverty/sc-startup-overlap branch October 16, 2026 21:55
alexandery-nvidia added a commit that referenced this pull request Oct 16, 2026
…failures-and-redispatch

main gained #4745, #4542 and #4596 since the previous merge, which put this head
eleven commits behind it; the merge keeps the head within main's up-to-date rule
for CI. No conflicts.

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

Labels

CI:L1 Run doctests, unit tests, and functional tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants