Skip to content

fix(checkpoint): break metric ties by recency in get_best_checkpoint_path - #3071

Merged
yuki-97 merged 8 commits into
NVIDIA-NeMo:mainfrom
tianyi-zhang-02:fix/get-best-checkpoint-tie-break
Jul 8, 2026
Merged

yuki-97 merged 8 commits into
NVIDIA-NeMo:mainfrom
tianyi-zhang-02:fix/get-best-checkpoint-tie-break

Conversation

@tianyi-zhang-02

@tianyi-zhang-02 tianyi-zhang-02 commented Jul 6, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do ?

CheckpointManager.get_best_checkpoint_path() sorted candidate checkpoints on the metric value alone. Because _load_checkpoint_history builds its list from an unordered glob.glob(...) and Python's sort is stable, when two or more checkpoints tie for the best metric value the returned one was whichever glob happened to list first — often an older checkpoint.

This PR sorts by (metric, step) so the most recent checkpoint wins on ties, matching the tie-breaking already used by remove_old_checkpoints() (pinned by test_remove_old_checkpoints_topk_bias_recent_if_equal). It also adds a regression test.

This is a user-facing, documented selection API (no internal caller; six algorithm configs point users to it for final checkpoint export/eval), so the fix affects which checkpoint a user gets when exporting the "best" model on a metric tie.

Issues

Closes #3067.

Usage

from nemo_rl.utils.checkpoint import CheckpointManager

# Two checkpoints tie for the best (lowest) loss=0.2; step 30 is more recent.
mgr = CheckpointManager({
    "enabled": True, "checkpoint_dir": d, "metric_name": "loss",
    "higher_is_better": False, "keep_top_k": None, "save_optimizer": True,
})
for step, loss in [(10, 0.2), (20, 0.9), (30, 0.2)]:
    mgr.finalize_checkpoint(mgr.init_tmp_checkpoint(step, {"loss": loss}))

mgr.get_best_checkpoint_path()  # before: step_10 (older tied-best); after: step_30

Before your PR is "Ready for review"

Pre checks:

  • Make sure you read and followed Contributor guidelines
  • Did you write any new necessary tests? — added test_get_best_checkpoint_path_bias_recent_if_equal, which fails on the previous behavior and passes with this fix.
  • Did you run the unit tests and functional tests locally? — validated tests/unit/utils/test_checkpoint.py logic locally; the added test forces the adversarial (older-first) history order via monkeypatch so it is deterministic regardless of filesystem glob ordering.
  • Did you add or update any necessary documentation? — N/A (bug fix; no doc changes required).

Additional Information

  • Behavior is unchanged when there is no tie (distinct metric values still select the true best).
  • The fix applies the recency tie-break for both higher_is_better=True and higher_is_better=False.

…path

get_best_checkpoint_path sorted only on the metric value, so when
multiple checkpoints tied for the best value the returned checkpoint
depended on unordered glob() ordering and could be an older one. Sort
by (metric, step) preferring the most recent, matching the tie-breaking
already used by remove_old_checkpoints. Add a regression test that fails
on the previous behavior.

Fixes NVIDIA-NeMo#3067.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Signed-off-by: Tianyi Zhang <[email protected]>
@tianyi-zhang-02
tianyi-zhang-02 requested review from a team as code owners July 6, 2026 06:07
@copy-pr-bot

copy-pr-bot Bot commented Jul 6, 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.

@tianyi-zhang-02 tianyi-zhang-02 changed the title fix(checkpoint): break metric ties by recency in get_best_checkpoint_… fix(checkpoint): break metric ties by recency in get_best_checkpoint_path Jul 6, 2026
yuki-97
yuki-97 previously approved these changes Jul 6, 2026

@yuki-97 yuki-97 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM, thanks for catching this and the fix!

@yuki-97 yuki-97 added the CI:Lfast Runs a fast test suite and re-use nightly `main` container (but sync dependencies to PRs version) label Jul 6, 2026
@yuki-97

yuki-97 commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

/ok to test 0ab3647

@yuki-97
yuki-97 enabled auto-merge (squash) July 6, 2026 09:04
@terrykong

Copy link
Copy Markdown
Collaborator

/ok to test e8d9e95

@terrykong

Copy link
Copy Markdown
Collaborator

thanks @tianyi-zhang-02 . retriggeed the CI since the merge in from main invalidates the CI

@yuki-97

yuki-97 commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

/ok to test f6137ab

The PR branch pinned 3rdparty/Gym-workspace/Gym at an older commit than
main, failing the submodule fast-forward check. Point it at main's Gym
commit (d67ad66) so the gitlink is a fast-forward.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Signed-off-by: Tianyi Zhang <[email protected]>
auto-merge was automatically disabled July 7, 2026 09:04

Head branch was pushed to by a user without write access

@tianyi-zhang-02

tianyi-zhang-02 commented Jul 7, 2026 •

Copy link
Copy Markdown
Contributor Author

Update — current tip is fff014d, synced with latest main. All submodule pointers (Gym d67ad66, Megatron-Bridge, Automodel) now match main, so Check submodule fast-forward passes. The earlier failure was just the Gym gitlink being behind — I'd been merging a stale main, which kept re-pinning the old commit; it now points at main's Gym commit directly.

Heads up on merge state: the pushes dismissed the prior approval and disabled auto-merge (author push without base-repo write access). @yuki-97 could you re-approve and re-enable auto-merge, and /ok to test fff014d? Thanks for your patience with the CI churn.

@yuki-97

yuki-97 commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

/ok to test fff014d

@yuki-97
yuki-97 enabled auto-merge (squash) July 7, 2026 09:17
@yuki-97

yuki-97 commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

thanks @tianyi-zhang-02 , re-approved and enqueued, also retriggeed the CI.

@tianyi-zhang-02

Copy link
Copy Markdown
Contributor Author

Sorry — accidentally hit "Update branch" again, so the tip is now 886f5e1 (approval + auto-merge are still intact this time). Could you /ok to test 886f5e1 when you get a chance? Promise I'll keep my hands off the branch now 😅 thanks @yuki-97!

@yuki-97

yuki-97 commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

/ok to test 886f5e1

@yuki-97

yuki-97 commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

hi @tianyi-zhang-02 , no worry and retriggered.

@yuki-97
yuki-97 merged commit ca6cda8 into NVIDIA-NeMo:main Jul 8, 2026
86 checks passed
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) community-request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

get_best_checkpoint_path returns an older checkpoint than intended when metric values tie

3 participants