What a run keeps when it stops at the checkpoint deadline

NVIDIA-NeMo/RL #5056 (part 4 of the rollout-checkpointing stack, on top of #5049). Permalinks pin the PR head dea7ae5a.

Two pages on this PR, one question each. This one: what the PR changes at the deadline.

Before
At the deadline the run saved a trainer checkpoint and stopped at once. The background snapshot task was cancelled, so the environment's unfinished turns were never saved and a restart redid them.
This PR
After that trainer checkpoint, the run takes one forced environment snapshot, then stops. A new key lets a run save environment state only at the deadline.
Status
New feature, works as described. The review asks for a smaller version (about 70 fewer lines), one doc fix, and for the two deadline saves to overlap; no correctness bug blocks it.
Background. With turn-level recovery, part of a run's state lives in the environment (the tool or game server a model talks to over many turns). A trainer checkpoint saves the model and optimizer, but not that state. Only a rollout snapshot saves it. The checkpoint deadline makes a run save and exit before the job's time limit. Dotted words have a short explanation: hover or tap them.

When the deadline is due, the train loop saves a trainer checkpoint at the end of that step. Publishing it calls trainer_checkpoint_published(), which wakes a background task to snapshot the environment. Before this PR, the loop then hit break, and teardown cancelled that task before it could save. This PR has the train loop take the snapshot itself, in save_at_deadline(), before it breaks.

The last seconds of a run, before and after

Example: the deadline is due during train step 7, with 4 conversations part-way through. Before train step 7 save step_7 ✓ break, exit snapshot task cancelled ✗ 4 unfinished turns not saved → redone after restart single_controller.py L3258 (base) After train step 7 save step_7 ✓ forced snapshot ✓ (4 turns) break, exit restart resumes the 4 conversations at their turn single_controller.py L3258-L3265 snapshots.py save_at_deadline() saved lost at exit no state saved Same order in both lanes up to the save; the only new step is the snapshot before the exit.

The PR also adds a key that decides whether the environment is snapshotted after every trainer checkpoint, or only at the deadline. Turn-level recovery no longer needs a timer (rollout_snapshots_enabled() turns snapshots on for it):

Environment state is savedsnapshot_on_trainer_checkpointsnapshot_attempt_interval_s
After every trainer checkpoint, and at the deadline (default)truenull
Only at the deadline — no pause of the environment during trainingfalsenull
Also on a timer (here every 300 s)true300

The change, and how it could be smaller

Where it lands: RL only. The deadline branch of the train loop in single_controller.py, the new save_at_deadline(), and setup checks in setup.py.

@@ single_controller.py L3258-L3265 · _train_pump (deadline branch, as in the PR)
             if should_save_by_timeout:
+                if self._master_config.checkpointing["enabled"]:
+                    # The trainer checkpoint holds no environment state, and no
+                    # later step will wake the pump to snapshot it.
+                    await self._finish_pending_save(blocking=True)
+                    await self._rollout_snapshots.save_at_deadline()
                 print("Timeout has been reached, stopping training early", flush=True)
                 break

_finish_pending_save publishes step_7 first when the save is still finishing in the background, because a snapshot must point at a published trainer checkpoint. The review asks for these cuts (none are in the PR yet):

Review comments: C2 · snapshots.py:960C1 · timer.py:466C5 · single-controller.md:235C6 · test_checkpointing.py:1679C7 · test_checkpointing.py:2858C8 · test_setup.py:1377C4 · snapshots.py:949 — GPU runC9 · single_controller.py:3272 — overlap

So what. Turn-level runs now keep their unfinished turns across a walltime stop. One catch: the PR moves the deadline earlier by the length of the last snapshot (snapshots.py L715-L718), but in the deadline-only mode no snapshot runs before the deadline, so that amount is always 0. Leave room in checkpoint_must_save_by for both saves. The review asks to delete that reserve rather than document it.

review comment — timer.py:466 (C1)