Why the rollout-recovery save got faster, and why the saved file did not change

NVIDIA-NeMo/RL #5185 ยท perf(checkpoint) ยท permalinks pin head d3e2af6c (new code) and base defdaef4 (old code). Dotted words have a short explanation: hover or tap them.

Before
Every save deep-copied the saved state of every prompt group, including groups it was about to throw away.
This PR
Skips those groups before copying, and copies plain dicts and lists with a small copier that reuses strings and numbers instead of copying them.
Status
Same output, about 2.6ร— faster (4.8ร— when half the groups are skipped). The saved state matched the old code in every case tested. Review: no bugs, two small cleanups (C1, C2).

Why it should be faster. Python's copy.deepcopy makes a new object for every dict and list, checks every string and number, and records each one in its memo. The new copier only rebuilds plain dict and list objects and hands strings, numbers, bool and None back as they are (they cannot change, so sharing them is safe). Separately, groups that the replay buffer already saves are now skipped before the copy instead of after it.

Measured: 500 groups, 64 receipt entries each, CPU, Python 3.13.14oldnewwhere the saving comes from
no groups skipped112 ms43 msfaster receipt copy โ‰ˆ 67 ms (98%); hand-written reward fields โ‰ˆ 3โ€“4 ms
half the groups skipped110 ms23 msnot copying skipped groups โ‰ˆ 54 ms; faster copy of the rest โ‰ˆ 37 ms

What is not faster: the copy is still about 95% of the remaining time, and writing the file (torch.save, on a thread) is not touched. When it is no faster: receipts made mostly of tuples or dict/list subclasses go to deepcopy anyway, plus one cheap type check. These numbers are a CPU benchmark of the build step alone, not a full training run.

At each save, the controller builds the recovery state in build_rollout_recovery_state on its event loop, so every millisecond here blocks the controller. For each prompt group it records the group's receipt and reward details. It must copy them, so later changes to the live ledger cannot leak into the saved file.

One save, before and after

โ‘  check every group โ‘ก copy receipts โ‘ข saved state Before base A B C D E A B C D E C D E state_dict L1682 copy.deepcopy ร— 5 drop A, B after copy A and B copied, then thrown away finalizer rewards: convert all 5 groups with dataclasses.asdict, then keep only A, B After this PR A B C D E A B C D E C D E same checks L1757 skip A, B fast copy ร— 3 same saved state โœ“ finalizer rewards: convert only A, B, with three small hand-written functions one receipt: {"capture_key", "manifest": [{"model_call_id", "metadata": {"sizes": [4, 7, 9]}}, โ€ฆ ร—64]} deepcopy: new dict, list, dict, dict, list โ€” and every string and number looked up in the memo fast copy: the same new dicts and lists โ€” strings and numbers reused as they are copied and saved copied, then thrown away checked, not copied A, B = already saved by replay buffer

Both versions run the safety checks on all five groups, including A and B. Only the copy work changes. The team checked the output three ways: the base and head code gave the same saved state with 0, 1 and 2 groups skipped, 5,000 random receipts copied the same with both copiers, and the PR's own tests fail if a field is added to RewardChecks, RewardLogContext or FinalizedReward without updating the hand-written functions.

The code, as pseudocode

Build the recovery state, before

# controller event loop, once per save
def build(ledger, replay_owned):
    groups = []
    for g in ledger:                           # A B C D E
        check(g)                               # safety + prompt identity
        groups.append(deepcopy(g.receipt))
    groups = [g for g in groups
              if g.id not in replay_owned]     # drops A, B
    rewards = {id: asdict(r) for id, r in finalizer_rewards}
    rewards = keep_only(rewards, replay_owned)  # keeps A, B

Build the recovery state, after

# controller event loop, once per save
def build(ledger, replay_owned):
    groups = []
    for g in ledger:                           # A B C D E
        check(g)                               # safety + prompt identity
        if g.id in replay_owned: continue      # skip A, B before copying
        groups.append(fast_copy(g.receipt))
    groups = [g for g in groups if g.id not in replay_owned]
    rewards = {id: to_dict(r) for id, r in finalizer_rewards
               if id in replay_owned}          # convert only A, B
    rewards = keep_only(rewards, replay_owned)  # C1: now does nothing

The new copier

# called once per receipt; memo is shared down the whole receipt
def fast_copy(x, memo={}):
    if type(x) in {str, bytes, int, float, bool, complex, None}:
        return x                               # cannot change: share it
    if id(x) in memo: return memo[id(x)]       # same object twice, or a cycle
    if type(x) is dict: return new dict of fast_copy(k): fast_copy(v)
    if type(x) is list: return new list of fast_copy(v)
    return deepcopy(x, memo)                  # tuples, subclasses, anything else

Real code: _copy_checkpoint_value, RolloutRecoveryLedger.state_dict, build_rollout_recovery_state. type(x) is dict checks the exact type, so a dict subclass keeps its own copy behavior.

What the review asks

C1 ยท cleanup. The rewards are now filtered twice: once in the new comprehension and again in the old loop. The output is right, but the rule "keep only replay-owned groups" now lives in two places. The ask: let the old loop filter only the metrics.

๐ŸŸก review comment C1 โ€” rollout_recovery.py:2339

C2 ยท test gap. The end-to-end test builds its "before" snapshot with the same copier it is testing. If the copier ever shared a list with the ledger, the snapshot would share it too, and the "ledger was not changed" check would still pass. The ask: take that snapshot with copy.deepcopy. With that one line, a broken copier fails the test.

๐ŸŸก review comment C2 โ€” test_rollout_recovery_capture_copy.py:228

Nearly all of the gain is the receipt copier plus skipping groups before copying them. The three hand-written reward functions save only 3โ€“4 ms per 500 groups on Python 3.13; their tests keep them in step with the dataclasses. The saved state is the same as before, so this change is safe to land once C1 and C2 are done.