Skip to content

refactor(utils): migrate checkpointing configs to BaseModel - #5239

Merged
yuki-97 merged 16 commits into
mainfrom
yukih/mirror-4152
Nov 16, 2026
Merged

yuki-97 merged 16 commits into
mainfrom
yukih/mirror-4152

Conversation

@yuki-97

@yuki-97 yuki-97 commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Mirrors #4152 so the changes can go through CI and be merged.

Thanks @lonexreb for the original contribution!

lonexreb and others added 14 commits October 31, 2026 15:58
…Config to BaseModel

Second PR in the utils-layer config v1 -> v2 series (#3916 migrated
ClusterConfig; pattern from #2518, alongside #3197/#3289).

Schema:
- Both classes become BaseModel(extra="allow"); NotRequired fields
  become Optional[...] = None.
- Hidden call-site defaults are centralized on the class exactly as
  they behave today: save_optimizer=True, save_data_plane=False,
  save_consolidated=False, model_cache_dir="", model_repo_id="",
  is_peft=False, is_async=False.
- model_save_format stays Optional[str] = None on purpose: the DTensor
  v1 worker rejects any non-None value, and the automodel layers apply
  their own "safetensors" fallback (CheckpointManager keeps that
  resolution, now explicit instead of a .get default).
- load_replay_buffer stays tri-state (Optional[bool] = None): the
  async-GRPO restore path only opts out on an explicit False.

Call sites (~85 across all algorithms, SC utils, lm_policy) move from
key access / .get() to attribute access. Two deliberate dict
boundaries are preserved with model_dump():
- policy_config["pretrained_checkpoint"] hand-off (PolicyConfig is
  still TypedDict-shaped; Megatron setup consumes dict keys), and
- checkpointing_cfg forwarded to save_checkpoint workers (the
  automodel checkpoint layer consumes .items()/.get()).

Tests: fixtures built via model_construct bypass validation, so
checkpointing dict literals are wrapped in
CheckpointingConfig.model_construct(**{...}) (missing required fields
now raise AttributeError where they raised KeyError before);
CheckpointManager unit tests validate their full config dicts via
model_validate.

No exemplar YAML or reference-config changes; class defaults mirror
the documented/exemplar values, so tests/unit/test_config_v2.py is
unaffected.

Verified: py_compile on all touched files; standalone pydantic
semantics checks (exemplar blocks round-trip, sft.yaml block keeps
model_save_format=None, nested pretrained_checkpoint coerces and dumps
back, extra keys preserved, model_construct default-fill); ruff format
+ check clean.

Signed-off-by: Shubhankar Tripathy <[email protected]>
…e configs and residual call sites

Rebase over main's removal of the automodel-only fields from
CheckpointingConfig (moved under policy.dtensor_cfg) — the schema now
carries only the algorithm-level fields, and the new
_AUTOMODEL_ONLY_CHECKPOINT_FIELDS rejection guard is kept, adapted to
read config.model_extra since legacy keys now arrive via
extra="allow" rather than the field set. The model_dump() worker
boundary in lm_policy/lm_value is dropped along with the
checkpointing_cfg parameter main removed.

Also applies the two failure classes from the #3916 review
pre-emptively:

- Reference configs: Optional fields are always emitted by
  model_dump() where NotRequired keys were omitted, so the always-
  emitted checkpointing keys are added with their default values to
  all six reference blocks; verified each block now covers exactly
  the keys the model emits.
- Residual dict-style sites: converted subscripts/get on the validated
  checkpointing field in the example run scripts, single_controller,
  SC setup, flow_grpo, and test files; wrapped dict-over-field
  assignments (test_ppo, test_sft_v2) in
  CheckpointingConfig.model_construct so missed call sites cannot hide
  behind a dict that still answers .get.

Signed-off-by: Shubhankar Tripathy <[email protected]>
…ed model

Found by running tests/unit/utils/test_checkpoint.py locally (73 tests):
test_checkpoint_manager_rejects_top_level_automodel_fields still passed
a raw OmegaConf dict into CheckpointManager, which now takes the
validated CheckpointingConfig — the guard reads config.model_extra, so
a plain dict raised ConfigAttributeError instead of the expected
ValueError. Legacy keys now arrive through extra="allow" on the
validated model, which is exactly the production path.

Also verified on this branch: tests/unit/test_config_v2.py +
tests/unit/test_config_validation.py — 1172 passed, 0 failed.

Signed-off-by: Shubhankar Tripathy <[email protected]>
Signed-off-by: Shubhankar Tripathy <[email protected]>
… and tests

- single_controller.py: replace the remaining dict-style .get() on
  master_config.checkpointing (a BaseModel since this PR) with attribute
  access; BaseModel has no .get(), so replay-buffer resume would raise.
- tests: convert dict-style .update()/dict fixtures on checkpointing to
  CheckpointingConfig.model_construct and attribute assignment.
- tests: sort imports per the ruff pre-commit hook.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Signed-off-by: Shubhankar Tripathy <[email protected]>
pyrefly (pre-commit lint) failed on single_controller_utils/setup.py:1258:
dict[str, Any] is not assignable to TypedDict key pretrained_checkpoint
with type PretrainedCheckpointConfig.

PretrainedCheckpointConfig became a BaseModel in this PR, but
PolicyConfig is still a TypedDict that crosses the Ray boundary, and every
Megatron consumer reads policy.pretrained_checkpoint with key access. Each
algorithm's setup therefore hands off checkpointing.pretrained_checkpoint
via model_dump(). Annotate the policy-side key as that dict instead of the
BaseModel, which also stops pydantic from coercing a YAML-set
policy.pretrained_checkpoint into a model that ["path"] cannot read.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Shubhankar Tripathy <[email protected]>
…warding test

test_setup_forwards_pretrained_checkpoint landed on main after the last
rebase and assigns a plain dict over checkpointing.pretrained_checkpoint.
That field is a BaseModel in this PR, so setup's model_dump() hand-off
raised AttributeError. Build the model and compare the policy side against
its model_dump(), since PolicyConfig still receives a dict.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Shubhankar Tripathy <[email protected]>
- Sync the load_replay_buffer docstring with its Optional[bool] = None
  field: None behaves like True and only an explicit False opts out.
- Pin the omitted-key behavior with
  test_checkpoint_manager_defaults_when_optional_keys_omitted.
- Convert the dict-style checkpointing access that landed on main in
  test_grpo.py and the forest tests in test_setup.py to attributes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Shubhankar Tripathy <[email protected]>
test_generic_launchers_reject_nemo_gym_configs came in from main with
checkpointing={"enabled": False}. This branch makes CheckpointingConfig a
BaseModel, so run_grpo.py reads config.checkpointing.enabled and the
native case failed with AttributeError in L0_Unit_Tests_Other.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Shubhankar Tripathy <[email protected]>
Merge origin/main (49 commits) to clear the conflicts with #5009 and the
"Check if PR branch is up to date" gate. Code main moved into
rollout_checkpointing/ and main's new tests get the same attribute access
as the rest of this PR.

Review fixes:
- load_replay_buffer is a plain bool defaulting to True; readers test
  `not load_replay_buffer`, and the reference configs dump `true`.
- save_period defaults to 10; note that higher_is_better is only read
  when metric_name is set.
- PretrainedCheckpointConfig stays a TypedDict until PolicyConfig migrates
  (#3197), so setup hands it over unchanged and
  PolicyConfig.pretrained_checkpoint keeps its TypedDict annotation.
- Indent the CheckpointingConfig docstring under Attributes: and document
  save_period, checkpoint_must_save_by and pretrained_checkpoint.
- Tests build CheckpointingConfig with keyword arguments.
- Drop the defaults-only CheckpointManager test (#5009 testing rules).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Shubhankar Tripathy <[email protected]>
Main's new reads of checkpointing (snapshot gating in setup.py, the
pump epilogue, and three tests) still used dict access; switch them to
attributes.

Signed-off-by: Shubhankar Tripathy <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
@yuki-97 yuki-97 added the CI:L1 Run doctests, unit tests, and functional tests label Nov 16, 2026
@yuki-97

yuki-97 commented Nov 16, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test f55d456

@copy-pr-bot

copy-pr-bot Bot commented Nov 16, 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.

@yuki-97
yuki-97 requested a review from RayenTian November 16, 2026 02:48

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

Overall LGTM. The migration is mechanical and consistent. I loaded every shipped/exemplar checkpointing: block (383 after defaults inheritance) through the new CheckpointingConfig and all validate. The nine Automodel-only legacy keys are still rejected through model_extra.

One thing to fix, and it is what is failing L0_Unit_Tests_Other (the only failure in that job: 1 failed, 3529 passed):

tests/unit/single_controller/test_setup.py:1509 still uses dict-style update on the config:

FAILED unit/single_controller/test_setup.py::TestSetup::test_prefix_recovery_accepts_multimodal_capture
AttributeError: 'CheckpointingConfig' object has no attribute 'update'

This test came in with #4583, which the merge of main brought in, so it was probably missed by the conversion. CheckpointingConfig is a BaseModel now, so please switch to attribute assignment:

mc.checkpointing.checkpoint_dir = str(tmp_path / "checkpoints")
mc.checkpointing.enabled = True
mc.checkpointing.save_data_plane = True
mc.checkpointing.save_period = 1

I grepped tests/, research/, examples/ and nemo_rl/ for other dict-style uses of the checkpointing config ([...], .get, .update, .pop) and this is the only remaining one. research/flow_grpo/tests/unit/algorithms/test_flow_grpo.py:236 indexes a plain dict that is passed to model_validate afterwards, so it is fine.

Since this needs a new commit, CI will need a fresh /ok to test <new-sha> after the push. The earlier test_ssd_tier.py fd-count failure in the first run is unrelated to this PR.

@yuki-97

yuki-97 commented Nov 16, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test 76a4a97

@yuki-97
yuki-97 requested a review from RayenTian November 16, 2026 09:33
@yuki-97
yuki-97 enabled auto-merge (squash) November 16, 2026 09:33
@yuki-97
yuki-97 merged commit 80c0564 into main Nov 16, 2026
97 checks passed
@yuki-97
yuki-97 deleted the yukih/mirror-4152 branch November 16, 2026 14:47
seonjinn added a commit to seonjinn/RL that referenced this pull request Nov 17, 2026
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.

3 participants