Skip to content

fix(dtensor-v2): keep the KL reference anchored to base weights across resumes - #3722

Merged
yuki-97 merged 9 commits into
NVIDIA-NeMo:mainfrom
yuekaizhang:fix/kl-ref-anchor-across-resumes
Aug 25, 2026
Merged

yuki-97 merged 9 commits into
NVIDIA-NeMo:mainfrom
yuekaizhang:fix/kl-ref-anchor-across-resumes

Conversation

@yuekaizhang

@yuekaizhang yuekaizhang commented Aug 20, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

Cherry-pick of f186c3b from #3643 (which was merged into the muse-glimmer-support branch, not main) so the fix lands on main.

In dtensor_policy_worker_v2, setup_model_and_optimizer restores weights_path internally, so capturing the reference model from its output re-anchors the KL reference to the resumed step on every resume instead of the original base policy. Each resume then grants the policy a fresh KL drift budget, defeating the anchor over multi-window runs.

Fix: when a reference model is needed on a resumed run, defer the NeMo RL checkpoint load — build the model from the pristine base weights first, capture the reference from that, then load the checkpoint. This matches the ordering already used by the v1 dtensor_policy_worker (reference captured before load_checkpoint).

Issues

Regression introduced in the v2 worker refactor; v1 worker is unaffected.

…s resumes

setup_model_and_optimizer restores weights_path internally, so capturing the
reference model from its output re-anchors the KL reference to the resumed
step on every resume instead of the original base policy. Each resume then
grants a fresh KL drift budget, defeating the anchor over multi-window runs.

When a reference model is needed on a resumed run, defer the checkpoint load:
build the model from the pristine base weights first, capture the reference
from that, then load the checkpoint.

(cherry picked from commit f186c3b)
Signed-off-by: Yuekai Zhang <[email protected]>
@yuekaizhang
yuekaizhang requested review from a team as code owners August 20, 2026 01:48
@copy-pr-bot

copy-pr-bot Bot commented Aug 20, 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.

@yuekaizhang

Copy link
Copy Markdown
Contributor Author

/ok to test 79c519e

@yuekaizhang yuekaizhang left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Self-review via the team-review skill (5 agents: rl-expert, bug-finder, test-agent, design-reviewer, devil's advocate), per the new self-review-before-human-review process.

Summary: the fix itself was verified correct and necessary — deferred load is kwarg-identical to the internal one, setup_reference_model_state copies to pinned CPU memory (no aliasing), ordering matches the v1 worker's existing semantics, and the megatron/value workers are unaffected. Devil's advocate: 2 findings confirmed, 3 downgraded, 0 disputed. Linters (pre-commit on the changed file): pass. All actionable findings are inline below.

Generated by Claude Code

- Log an explicit note when the checkpoint load is deferred for KL
  reference capture, since setup_model_and_optimizer otherwise prints a
  misleading 'No weights path provided ... default policy init' line on
  every KL-anchored resume; also use bool(weights_path) to match the
  truthiness guard inside setup_model_and_optimizer.
- Add mock-based unit tests pinning the deferred-load contract: base
  weights used for setup, reference captured before load_checkpoint,
  checkpoint loaded with the original paths, and pass-through/fresh-run
  negative cases.

Signed-off-by: Yuekai Zhang <[email protected]>
@yuekaizhang
yuekaizhang requested a review from a team as a code owner August 20, 2026 04:03
@yuekaizhang yuekaizhang added the CI:L1 Run doctests, unit tests, and functional tests label Aug 20, 2026
@yuekaizhang

Copy link
Copy Markdown
Contributor Author

/ok to test 358a19d

@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.

thanks for adding this, feels this is a feature rather than a fix, left some comments.

…est mocks

- Deferred path now delegates to the worker's own load_checkpoint()
  instead of duplicating the five checkpoint-manager kwargs, matching
  how the v1 worker loads on resume.
- Test mocks construct RuntimeConfig and ModelAndOptimizerState by
  keyword instead of bare positional tuples, so a field addition or
  reorder fails here with a named error rather than a silent swap.

Signed-off-by: Yuekai Zhang <[email protected]>
@yuekaizhang

Copy link
Copy Markdown
Contributor Author

/ok to test 198064f

…_base_weights

Per review, the deferred checkpoint load is now opt-in:
- New optional policy config bool ref_model_from_base_weights (default
  false). False keeps the existing behavior — on resume the reference is
  captured from the restored checkpoint weights. True anchors the KL
  reference to the pristine base (model_name) weights across resumes.
- Documented in PolicyConfig, grpo_math_1B exemplar, and the matching
  reference config for the v1->v2 migration check.
- Tests updated: the deferred-load test opts in; a new test pins the
  default checkpoint-anchored behavior.

Signed-off-by: Yuekai Zhang <[email protected]>
@yuekaizhang
yuekaizhang requested a review from a team as a code owner August 24, 2026 08:13
@yuekaizhang

Copy link
Copy Markdown
Contributor Author

/ok to test e18d99a

…oss resume

Three runs share one TRAIN_CMD (Qwen3-0.6B, 2 GPUs, 3 GRPO steps):
an uninterrupted baseline that checkpoints step_2, a default resume,
and a flag-on resume, each resuming from its own copy of step_2.
Asserts the deferred-load log line only appears with the flag on, and
on step-3 train/kl_penalty: the default resume collapses to the noise
floor (reference re-anchored to the checkpoint) while the flag-on
resume stays continuous with the baseline (reference still on base
weights). Verified locally on 2xH100: baseline 9.4e-2, default resume
3.7e-3, flag-on resume 1.1e-1. Registered in L1 AutoModel.

Signed-off-by: Yuekai Zhang <[email protected]>
@yuekaizhang

Copy link
Copy Markdown
Contributor Author

/ok to test 0eaa949

@yuekaizhang

Copy link
Copy Markdown
Contributor Author

/ok to test 25bfb18

…config flag

Per review discussion this is a bug fix, not an opt-in feature: on a
resumed run that needs a reference model, the v2 worker now always
defers the NeMo RL checkpoint load, captures the KL reference from the
pristine base (model_name) weights, then loads the checkpoint — the
ordering the v1 worker already had. Removes the
ref_model_from_base_weights config knob from PolicyConfig and the
exemplar/reference YAMLs, and simplifies the functional test to a
baseline-vs-resume KL continuity check (verified locally: baseline
9.9e-2, resume 1.1e-1 at the resume-boundary step).

Signed-off-by: Yuekai Zhang <[email protected]>
@yuekaizhang
yuekaizhang requested a review from a team as a code owner August 25, 2026 08:00
@yuekaizhang

Copy link
Copy Markdown
Contributor Author

/ok to test 58b7886

@github-actions

Copy link
Copy Markdown

❌ Submodule Fast-Forward Check Failed

Check based on commit: 58b7886 (PR #3722 from fix/kl-ref-anchor-across-resumes)

❌ Submodules that need attention:

Gym: ❌ PR branch is BEHIND main branch
TARGET (main branch): https://github.com/NVIDIA-NeMo/Gym/commits/c3bac96314a59f28b896f597eb9845d175bb0252/
CURRENT (PR #3722 from fix/kl-ref-anchor-across-resumes): https://github.com/NVIDIA-NeMo/Gym/commits/bb9b26de3dac1294faccccb5ed70f3386c16de10/

Please ensure all submodule commits are fast-forwards of the main branch before merging.

@github-actions

Copy link
Copy Markdown

❌ Submodule Fast-Forward Check Failed

Check based on commit: a2bbe61 (PR #3722 from fix/kl-ref-anchor-across-resumes)

❌ Submodules that need attention:

Gym: ❌ PR branch is BEHIND main branch
TARGET (main branch): https://github.com/NVIDIA-NeMo/Gym/commits/c3bac96314a59f28b896f597eb9845d175bb0252/
CURRENT (PR #3722 from fix/kl-ref-anchor-across-resumes): https://github.com/NVIDIA-NeMo/Gym/commits/bb9b26de3dac1294faccccb5ed70f3386c16de10/

Please ensure all submodule commits are fast-forwards of the main branch before merging.

The Gym gitlink was bumped to bb9b26de by accident in 58b7886 (a dirty
local submodule checkout swept up by git add -u); this PR does not touch
Gym. Restore the pointer to c3bac963 as recorded on main.

Signed-off-by: Yuekai Zhang <[email protected]>
@yuekaizhang

Copy link
Copy Markdown
Contributor Author

/ok to test 5faea40

@yuki-97
yuki-97 enabled auto-merge (squash) August 25, 2026 10:12
@yuki-97
yuki-97 merged commit 646e6e4 into NVIDIA-NeMo:main Aug 25, 2026
100 checks passed
@yuekaizhang
yuekaizhang deleted the fix/kl-ref-anchor-across-resumes branch September 9, 2026 10:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI:L1 Run doctests, unit tests, and functional tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants