Can the folder rename happen before the TQ, dataloader or critic saves finish?

NVIDIA-NeMo/RL #4859, flag async_rl.defer_async_checkpoint_finalize: true. All permalinks pin the current head 8841e8e4.

Before
Same order as now. Only the policy write ran in the background; a thread waited for it, then renamed the folder.
This PR
Changes who waits for the policy write (a check at each step boundary instead of that thread). The order of the other saves does not change.
Status
No race today. The rename needs a mark that is set only after every other part is on disk. Nothing states or checks that rule โ€” review comment C9 proposes a guard.

Three pages on this PR, one question each โ€” this one: can the rename pull the folder out from under a slow save.

Background. A SingleController checkpoint is one folder with several parts: policy weights and optimizer, the PPO critic, the dataloader position, the replay-buffer files, and a snapshot of TQ, the data plane. Every part is written into tmp_step_N/. Renaming it to step_N/ publishes the checkpoint. Dotted words have a short explanation: hover or tap them.

Only the policy save runs in the background: with Megatron async_save it returns after copying to host memory, and a writer process finishes the disk write. Every other part is awaited inside _save_checkpoint_impl. The last thing that function does is mark the folder as pending. The rename reads that mark, and runs only once the writer also reports the policy write done.

One save, in the order the code runs it

the save call: training waits training goes on controller Inside the data-plane barrier: rollouts cannot change TQ or the loader meanwhile. 1 ยท TQ save awaited, checked 2 ยท critic save PPO only, blocking 3 ยท policy save returns after copy 4 ยท loader, replay files 5 ยท mark pending step 3 check โ†’ no step 4 check โ†’ yes L5317 L5340 L5361 L5375 L5400 L3032 policy writer disk write, in the background done folder tmp_step_2/ โ€” on disk, but resume ignores it step_2 rename at the step-4 check: needs the mark (step 5) and the writer's "done" awaited: on disk before the next step runs in the background written, not published published Flag off: step 5 starts a thread instead, which waits for the writer and renames as soon as it is done. Either way the rename comes after steps 1โ€“4, because nothing starts it before step 5.

Your two cases, and one more

Every caller of the rename runs in the train loop that also runs the save (step check, next save, loop exit), or in teardown after that loop has stopped. It also takes the same checkpoint lock as the save. SFT v2 follows the same order: its loader file is written (sft_v2.py L468) before it marks the save pending.

So what. The flag only moves the wait on the policy's background write. The TQ, critic, dataloader and replay-buffer saves still pause training for their full length, flag on or off, so on runs with save_data_plane or PPO they may be most of the save time. The safety comes from one rule: publishing is the last step of the save. Nothing in the code says so or checks it, so the review asks for one publish helper, called last, and a test that fails if any file is written into the folder after it. Moving the publish one line up already breaks that test with the flag off: the rename removes tmp_step_2/ while the dataloader file is still being written.

๐ŸŸก review comment C9 โ€” single_controller.py:5398-5412