What #4507's test proves: a run killed in the middle of a reply, then restored from the tokens it already had, trains on exactly the same data as a run that was never killed

NVIDIA-NeMo/RL #4507, part 6 of 11 in the partial-checkpointing stack. It tests the feature added by #4506. All permalinks pin the PR head 7f7a7a5.

Before
The turn-recovery test covered a crash between two model calls. Nothing tested a crash in the middle of a reply, and no test compared a restored run with a run that never crashed.
This PR
Adds that test for #4506's prefix recovery. Test code only; no product code changes.
Status
Not merged; review open. It passes on the author's machine (470 s). It has never run in CI.

Five pages on this PR. Start here. Pages 2 and 3 are the current review; pages 4 and 5 are the first review, on an older head.

Background. During GRPO training, NeMo RL runs an agent in Gym, and vLLM writes the model's replies. Every finished episode becomes a training row. A rollout snapshot saves the episodes still in progress. #4506 adds prefix recovery: if a snapshot lands while vLLM is in the middle of a reply, it keeps the tokens written so far (the prefix). After a crash, the restored run continues from those tokens and only writes the rest (the tail). Dotted words have a short explanation: hover or tap them.

The test launches the same small job three times (Qwen3-1.7B, 4 prompts Ɨ 2 tries, 1 training step): once without a crash, once killed mid-reply, and once restored from that kill (the script's three runs). Each prompt asks the agent to create one calendar event, then list the numbers 1 to N in its closing reply. That reply is long, so the kill lands inside it. Sampling uses top_p=1e-6, which makes generation almost greedy, so two runs pick the same tokens and can be compared token by token. Each run saves the exact rows the trainer trained on, and the test compares the restored run's rows with the reference rows.

Three runs of one job, and the one comparison that matters

MODEL CALL 1 TOOL RUNS MODEL CALL 2: THE CLOSING REPLY (N = 250) TRAINER saved in a snapshot, restored later written in this run process killed never happened Reference no crash calendar_create_event event written "1\n2\n3\n…\n250" all written here train_data_step1rows it trained on Phase 1 killed mid-reply calendar_create_event event written "1\n2\n…139–156 tokens kill -9 rest never written never trained Phase 2 restore, finish from the snapshot from the snapshot "1\n2\n…restored as is …\n250"only the tail is written train_data_step1rows it trained on Compare row by row: all 8 rows (4 prompts Ɨ 2 tries) token ids, masks, reward and sample mask must be identical which snapshot: one that cut a reply in all 4 prompts (phase 1 snapshots every 0.05 s until one qualifies) kill: the whole process group, then wait for Ray to stop Every run gets the same 4 prompts: list 1 to N for N = 150, 250, 350, 450, with 2 tries each. Shown: the N = 250 prompt. 139–156 tokens: the 8 saved prefixes in the author's run on this head. All 8 rows then matched the reference.

What the test checks after the restore

CheckWhy it mattersCode
The snapshot it restores from cut a reply in all 4 prompts, and the save reports no failed or skipped cutsOtherwise a reply could quietly start over and still passsnapshot.py L83-120
Each restored episode goes back to the same Gym instance, as its next attemptGym holds the calendar the episode already wrote tosnapshot.py L261-331
Each reply is restored once and finished once, writes at least one new token, and its first tokens hash to what the restore loggedShows phase 2 really continued from the saved tokensparity.py L285-366
Every trained row matches the reference: token ids, masks, input length, reward, sample maskThe main claim: a crash changes nothing the trainer seesparity.py L198-282
The saved prefix data is still there at phase 2's first training step, and the next snapshot deletes itThe data neither disappears early nor piles upparity.py L369-401

One exception. vLLM's math is not exactly the same from run to run, so phase 1 can pick a different token at a near-tie before the kill. Phase 2 must then continue phase 1's tokens, not the reference's. Such a row is set aside. Today up to 7 of the 8 restored rows can be set aside and the run still passes; review comment C16 asks to cap that at one. Page 4 shows the looser first version, which the author has since narrowed.

How it differs from the existing turn-recovery test

turn-recovery test (already on the base)prefix-recovery test (this PR)
Where the crash landsBetween model calls: a test proxy holds the call after the tool resultInside a reply: snapshots every 0.05 s until one cuts a reply in all 4 prompts
What the restore reusesFinished turns; the held call starts overFinished turns and the tokens of the reply that was cut
Run with no crash to compare againstNone. It checks Gym identity and metrics.Yes. Every trained row must match it.
Runs, training steps2 runs, 3 steps3 runs, 1 step (phase 2 trains 2)
Time on the author's machine260 s470 s

Both tests use the same test hook, the same policy-call proxy and the same snapshot readers (_single_controller_turn_recovery_hook.py, _gym_recovery_snapshot_helpers.py).

So what. If this test passes, a crash in the middle of a reply costs only the tail of that reply, and the trainer sees exactly the rows a crash-free run would give it. It does not test learning: with top_p=1e-6 the policy-gradient term is 0. Before merge it still needs its first CI run. Up to 7 of 8 rows can be set aside today without failing (C16 asks for a cap of one). Page 2 covers how much of its code the test needs.

C16 — parity.py:463