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.
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.14 | old | new | where the saving comes from |
|---|---|---|---|
| no groups skipped | 112 ms | 43 ms | faster receipt copy โ 67 ms (98%); hand-written reward fields โ 3โ4 ms |
| half the groups skipped | 110 ms | 23 ms | not 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.
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.
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.
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