Skip to content

perf(checkpoint): reduce recovery metadata capture overhead - #5185

Merged
terrykong merged 5 commits into
mainfrom
amahishi/recovery-metadata-copy-main
Nov 26, 2026
Merged

terrykong merged 5 commits into
mainfrom
amahishi/recovery-metadata-copy-main

Conversation

@macandro96

@macandro96 macandro96 commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor
  • I, the PR author, have personally reviewed every line of this PR.

What and why

Reduce controller-side recovery metadata capture cost without changing checkpoint contents or the checkpoint schema:

  • Copy scalar-heavy receipt dictionaries/lists with a specialized copier that shares exact immutable scalar leaves, while retaining memoization for aliases/cycles and falling back to deepcopy for custom types and subclasses.
  • Project reward-check, reward-log-context, and finalized-reward dataclasses directly into checkpoint dictionaries, retaining the generic asdict fallback for dataclass subclasses. The reward-check projection includes the length_penalty input main added in feat(single_controller): support group length penalties #5206.
  • Exclude groups already represented by canonical replay rows before copying their nested attempt metadata, and filter discarded finalizer rewards before projection. Global checkpoint-safety and prompt-identity checks still run for excluded groups.

This is a focused port of the recovery-copy optimizations from the integrated checkpoint branch (c795e0219 and the recovery-ledger hunks of a37a013ec), adapted to main. Logical selection, attribution, short-batch state, and the existing restore validation remain intact.

Scope / compatibility

Targets main directly, rebased onto d93ba7ddb4, which already includes #5137 and #5139; this reduces their capture overhead.

Only one runtime module and one regression-test module change. No new flags, dependencies, Gym changes, checkpoint-format migration, writer actors, or asynchronous serialization/persistence behavior. Deferring recovery serialization/hash until after Gym resume is a separate follow-up.

Issues

No separate issue; scoped performance follow-up to the rollout-checkpoint work, independently useful on main.

Convergence / perf / functionality proof

Checkpoint contents and schema are unchanged, so no convergence run is needed. CPU timing and unit-test evidence below.

CPU comparison

Compared against the actual main implementation at the base commit using 500 prompt groups, 64 manifest entries per group's sealed receipt, and three counterbalanced trials per method/scenario.

Groups omitted from the recovery ledger because canonical replay owns them Main median Optimized median Speedup
0% 0.396s 0.155s 2.55x
50% 0.403s 0.081s 5.00x

All 12 trials matched baseline checkpoint contents and passed weights-only load plus native ledger restore. Timings measure synthetic CPU recovery-state construction only; serialization, restore, disk I/O, and full rollout-pause timing are excluded. This is not an end-to-end snapshot speedup claim. No GPU smoke or full pre-commit suite was run.

Validation

846 CPU tests passed at the original base defdaef48:

PYTHONPATH="$PWD:<existing-checkout>/3rdparty/Gym-workspace/Gym" \
RAY_ENABLE_UV_RUN_RUNTIME_ENV=0 NRL_IGNORE_VERSION_MISMATCH=1 \
uv run --no-project --python <cpu-test-venv>/bin/python \
  -m pytest --noconftest -q -o addopts= --disable-warnings \
  tests/unit/experience/test_rollout_recovery_capture_copy.py \
  tests/unit/experience/test_rollout_recovery.py \
  tests/unit/experience/test_reward_penalties.py \
  tests/unit/experience/test_capture_reward_penalties.py \
  tests/unit/single_controller/test_rollout_checkpoint.py

Used that base's Gym pin 5911d628c744d0bf43fe1d9ddf3bda72d59b0f9a, Python 3.13.14, and a pre-existing CPU test environment. No Ray actors or GPU job were launched for this change.

  • Scoped Ruff 0.9.9 lint and formatting checks passed.
  • Scoped Pyrefly 0.24.2 on nemo_rl/experience/rollout_recovery.py: zero errors, zero ignored.
  • git diff --check passed.
  • Regression coverage includes snapshot/live mutation isolation, shared containers and cycles, mixed custom-container fallback, custom scalar copying, copy errors, subclass schema preservation, current-main logical selection, early filtering, safety guards, and weights-only serialization/native ledger restore.

After rebasing onto d93ba7ddb4 (main's Gym pin 9e2e5d0ad0d1723acef1b63beb28292da16f7c5b):

  • All 32 tests in test_rollout_recovery_capture_copy.py pass, plus the length-penalty and reward-check checkpoint round-trip tests in test_rollout_recovery.py.
  • With the length_penalty projection removed, 12 of those tests fail.
  • Pyrefly 1.4.0 on nemo_rl/experience/rollout_recovery.py: zero errors.

Needs upstream changes

None.

PR commands and CI
  • CI label, exactly one:
    • CI:Lfast: the default for most PRs. Runs a fast test subset.
    • CI:L1: dependency or container changes (pyproject.toml, uv.lock, Dockerfile) or broad core changes. Builds a new image and runs unit and functional tests.
    • CI:docs: docs-only changes. Runs doc tests only.
  • /ok to test <full-commit-sha>: start CI for that commit. Post again after each push.
  • /internal-ci test-case=<job>: run NVIDIA's internal nemo-ci pipeline for the PR head; eligible teams only.
  • /review: automated review comments. Add mode=strict for a stricter review, reset=true for a fresh full review.
  • /resolve-conflict: Autobot merges main into this PR and resolves the conflicts. PRs from forks must allow edits by maintainers.
  • Locally: /review-pr-team <pr-number> for a team review before you ask for human review.
  • Contributor guidelines

@macandro96 macandro96 added the CI:Lfast Runs a fast test suite and re-use nightly `main` container (but sync dependencies to PRs version) label Nov 12, 2026
@copy-pr-bot

copy-pr-bot Bot commented Nov 12, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@macandro96

Copy link
Copy Markdown
Contributor Author

/ok to test d3e2af6

@terrykong terrykong left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the clear write-up and the thorough tests. A team review found no bugs. The checkpoint output matches the old code. We ran a script against both the base and this head, and both gave the same saved state in every case: no exclusions, an empty exclusion set, and one or two excluded groups. We also fuzz-tested _copy_checkpoint_value against copy.deepcopy on 5,000 random receipts and found no differences. If someone adds a field to RewardChecks, RewardLogContext or FinalizedReward, the new tests fail, so the hand-written copies cannot quietly fall out of date.

Two small asks, both inline:

  1. (C1) Finalizer rewards are filtered twice. Filter only the metrics in the old loop.
  2. (C2) The end-to-end capture test makes its baseline with the code under test. Use copy.deepcopy instead.

Not blocking: the timings come from a synthetic CPU benchmark. build_rollout_recovery_state runs on the controller's event loop (snapshots.py#L400-L413). Numbers from one real snapshot would show how much this matters in practice: the group count, and the build time before and after, next to the save time.

Visual explainer: https://terrykong.github.io/gh-pages-poc/terryk/pr-5185-faster-recovery-copy.html

nemo-rl-review: kind=expert harness=claude-code/2.1.332 model=claude-opus-5-5 effort=medium review-pr-team=v3 reviewed=d3e2af6

Comment thread nemo_rl/experience/rollout_recovery.py Resolved
macandro96 and others added 3 commits November 25, 2026 08:32
Copy scalar-heavy receipt containers without generic deepcopy bookkeeping, project reward fields directly, and exclude canonically owned groups before copying their attempt metadata. Preserve the checkpoint schema, detached mutable state, custom copy behavior and current-main recovery fields.

Validation: 846 CPU recovery, reward and checkpoint tests passed; scoped Ruff and Pyrefly passed. A 500-group, 64-entry-per-receipt CPU comparison measured 2.55x faster construction without exclusions and 5.00x with half the groups excluded; all twelve trials matched baseline contents and passed weights-only/native restore checks. Timings exclude serialization and disk I/O.
Signed-off-by: Anish Mahishi <[email protected]>
… test baseline

The finalizer-rewards comprehension already keeps only canonical groups,
so the later loop filtered those rewards a second time. Filter only the
finalizer metrics there, so the ownership rule for rewards lives in one
place.

test_capture_matches_legacy_projection built its "ledger unchanged"
baseline with ledger.state_dict(), the copy code under test. A copy that
shared lists with the ledger would have aliased the baseline too and
passed. Take the baseline with copy.deepcopy; with lists left uncopied
the test now fails in all three cases.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Anish Mahishi <[email protected]>
…ecks

main's #5206 added RewardChecks.length_penalty after this branch was cut.
_reward_checks_state listed the three boolean fields by hand, so a
checkpoint written with it dropped the length-penalty input and a resumed
run restored that attempt without its length penalty. Copy the field the
way dataclasses.asdict does.

Cover a real LengthPenaltyInput in the projection, capture and restore
tests, and expect the multi_harness_routes key main's #5158 added to the
recovery state.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Anish Mahishi <[email protected]>
@macandro96
macandro96 force-pushed the amahishi/recovery-metadata-copy-main branch from d3e2af6 to 16453a5 Compare November 25, 2026 16:38
@macandro96

Copy link
Copy Markdown
Contributor Author

/ok to test 16453a5

@macandro96

Copy link
Copy Markdown
Contributor Author

/ok to test 16453a5

terrykong
terrykong previously approved these changes Nov 25, 2026
@terrykong
terrykong enabled auto-merge (squash) November 25, 2026 17:14
_populated_ledger seals each attempt with a Forest selection, and sealing
checks call ownership through Gym's rebuild rule, which imports nemo_gym.
The default unit lane has no nemo_gym, so mark the three tests that build
that ledger, as the ledger tests in test_rollout_recovery.py are.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Anish Mahishi <[email protected]>
terrykong
terrykong previously approved these changes Nov 25, 2026
@macandro96

Copy link
Copy Markdown
Contributor Author

/ok to test 7827d39

…r fake

build_rollout_recovery_state now calls ledger.state_dict(exclude_group_ids=...).
The _PendingLedger stand-in in test_checkpoint_dispatch_races.py still took no
arguments, so six tests failed with a TypeError. Give it the same keyword and
drop the excluded groups, as RolloutRecoveryLedger.state_dict does.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Anish Mahishi <[email protected]>
@macandro96

Copy link
Copy Markdown
Contributor Author

/ok to test 5737e4a

@terrykong
terrykong disabled auto-merge November 25, 2026 23:09
@terrykong
terrykong enabled auto-merge (squash) November 25, 2026 23:43
@terrykong
terrykong merged commit 7a5c913 into main Nov 26, 2026
98 of 99 checks passed
@terrykong
terrykong deleted the amahishi/recovery-metadata-copy-main branch November 26, 2026 03:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI:Lfast Runs a fast test suite and re-use nightly `main` container (but sync dependencies to PRs version)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants