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
Your two cases, and one more
Policy write fast, TQ slow. This cannot happen in this order. The TQ save (step 1) is awaited, and the
adapter checks that the snapshot is complete
(storage_saved must be true)
before the policy save (step 3) even starts. A slow TQ save just makes the whole save call longer; training waits for it,
with the flag on or off.
TQ fast, policy write slow. Steps 1โ5 finish, and the folder stays tmp_step_2/ while the
writer works. The check at each step boundary says "no" until the write is done. Nothing else writes into the folder
by then, so the late rename publishes a complete checkpoint.
Training stops in the middle of a save. The mark in step 5 is never set, so neither the step-boundary check
nor the teardown publish
(L949)
can rename a half-written folder. It stays tmp_step_2/, and resume uses the previous checkpoint.
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.