Skip to content

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

Closed
lonexreb wants to merge 14 commits into
NVIDIA-NeMo:mainfrom
lonexreb:refactor/config-v2-checkpointing
Closed

lonexreb wants to merge 14 commits into
NVIDIA-NeMo:mainfrom
lonexreb:refactor/config-v2-checkpointing

Conversation

@lonexreb

@lonexreb lonexreb commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do ?

Second PR in the utils-layer config v1 → v2 series (after #3916 / ClusterConfig; pattern from #2518, running alongside #3197 policy-config and #3289 generation-config series). Migrates CheckpointingConfig (nemo_rl/utils/checkpoint.py) from TypedDict to BaseModel(extra="allow"). PretrainedCheckpointConfig remains a TypedDict.

Schema decisions:

  • Hidden call-site defaults are centralized on the class, including save_optimizer=True and save_data_plane=False.
  • load_replay_buffer is a boolean defaulting to True; False opts out of restoring replay-buffer state.
  • save_period defaults to 10.
  • Configs that omit save_optimizer / keep_top_k previously hit a latent KeyError (config["save_optimizer"] on a NotRequired key); they now get the documented defaults.

Call sites: ~85 across all algorithms, SC utils, and lm_policy move to attribute access. PretrainedCheckpointConfig is forwarded directly to policy_config["pretrained_checkpoint"] as a dictionary, matching the existing PolicyConfig and Megatron consumers.

Tests: checkpointing fixtures use keyword-based CheckpointingConfig(...) construction when required fields are present, and CheckpointingConfig.model_construct(...) for partial fixtures.

Reference configs are updated to reflect the resolved checkpoint defaults. Exemplar YAMLs and tests/unit/test_config_v2.py are unchanged.

Issues

Part of the TypedDict → BaseModel migration tracked by tests/unit/test_config_v2.py (see also #1675 / #2102).

Usage

No user-facing YAML change. Unknown/legacy keys under checkpointing: continue to load; missing required keys (enabled, checkpoint_dir, metric_name, higher_is_better) now fail at config validation time with a clear pydantic error.

Before your PR is "Ready for review"

  • 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 (repo-pinned 0.9.9); py_compile on all touched files; zero 3rdparty/ changes (gitlinks verified against main)
  • CI unit tests (will trigger)

@lonexreb
lonexreb requested review from a team as code owners September 16, 2026 07:00
@copy-pr-bot

copy-pr-bot Bot commented Sep 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.

@lonexreb

Copy link
Copy Markdown
Contributor Author

/ok to test d99873c

@svcnvidia-nemo-ci svcnvidia-nemo-ci added the waiting-on-maintainers Waiting on maintainers to respond label Sep 18, 2026
@lonexreb
lonexreb force-pushed the refactor/config-v2-checkpointing branch from d99873c to 743d58d Compare September 27, 2026 01:14
@lonexreb

Copy link
Copy Markdown
Contributor Author

/ok to test 743d58d

@lonexreb

Copy link
Copy Markdown
Contributor Author

Rebased onto current main, which meanwhile removed the automodel-only fields from CheckpointingConfig (moved under policy.dtensor_cfg) and dropped the checkpointing_cfg worker hand-off — so this PR is now smaller: the schema carries only the algorithm-level fields, the model_save_format/worker-boundary reasoning from the original description no longer applies, and the new _AUTOMODEL_ONLY_CHECKPOINT_FIELDS rejection guard is kept, adapted to read config.model_extra (legacy keys now arrive via extra="allow" instead of the field set).

Also pre-emptively applied both failure classes @yuki-97 found on #3916:

  • Reference configs: Optional[...] = None fields are always emitted by model_dump() where NotRequired keys were omitted — added the always-emitted checkpointing keys with their default values to all six reference blocks, and verified each block now covers exactly the keys the model emits.
  • Residual dict-style sites (incl. research/ and the example run scripts, which model_validate their MasterConfig): converted subscripts/.get to attribute access, and wrapped dict-over-field test assignments in model_construct so missed call sites can't hide behind a dict that still answers .get.

@lonexreb lonexreb changed the title refactor(utils): migrate CheckpointingConfig and PretrainedCheckpointConfig to BaseModel refactor(utils): migrate checkpointing configs to BaseModel Sep 27, 2026
@svcnvidia-nemo-ci svcnvidia-nemo-ci added waiting-on-maintainers Waiting on maintainers to respond and removed waiting-on-maintainers Waiting on maintainers to respond labels Sep 27, 2026
yuki-97 pushed a commit to lonexreb/RL that referenced this pull request Sep 29, 2026
Third PR in the utils-layer config v1 -> v2 series (NVIDIA-NeMo#3916 ClusterConfig,
NVIDIA-NeMo#4152 checkpointing configs; pattern from NVIDIA-NeMo#2518).

Schema: WandbConfig, SwanlabConfig, TensorboardConfig, MLflowConfig,
GPUMonitoringConfig, and LoggerConfig move from TypedDict to
BaseModel(extra="allow"); NotRequired fields become
Optional[...] = None. GpuMetricSnapshot stays TypedDict (internal
data container, not YAML-loaded config).

Behavior-preserving details:
- WandbLogger/SwanlabLogger passed dict(cfg) / **cfg straight into
  wandb.init / swanlab.init; they now pass
  model_dump(exclude_none=True) so unset Optional fields do not reach
  the backends as explicit Nones, while extra= passthrough keys
  survive.
- Logger.__init__ raises a named ValueError when a backend is enabled
  but its sub-config block is absent (previously a KeyError).
- MLflowLogger keeps its cfg-or-env fallbacks, now via attributes.

Call sites (~22 across algorithms, sync_rollout_actor,
trajectory_collector, SC setup, all example run scripts, flow_grpo)
move to attribute access.

Reference configs: always-emitted Optional keys are added with their
default values at both the logger level and inside the nested
wandb/tensorboard/mlflow blocks (deep-verified: each reference logger
block now matches LoggerConfig.model_dump exactly). Test fixtures wrap
logger dict literals in LoggerConfig.model_construct and move
subscripts to attribute access.

Signed-off-by: Shubhankar Tripathy <[email protected]>
lonexreb added a commit to lonexreb/RL that referenced this pull request Oct 1, 2026
Third PR in the utils-layer config v1 -> v2 series (NVIDIA-NeMo#3916 ClusterConfig,
NVIDIA-NeMo#4152 checkpointing configs; pattern from NVIDIA-NeMo#2518).

Schema: WandbConfig, SwanlabConfig, TensorboardConfig, MLflowConfig,
GPUMonitoringConfig, and LoggerConfig move from TypedDict to
BaseModel(extra="allow"); NotRequired fields become
Optional[...] = None. GpuMetricSnapshot stays TypedDict (internal
data container, not YAML-loaded config).

Behavior-preserving details:
- WandbLogger/SwanlabLogger passed dict(cfg) / **cfg straight into
  wandb.init / swanlab.init; they now pass
  model_dump(exclude_none=True) so unset Optional fields do not reach
  the backends as explicit Nones, while extra= passthrough keys
  survive.
- Logger.__init__ raises a named ValueError when a backend is enabled
  but its sub-config block is absent (previously a KeyError).
- MLflowLogger keeps its cfg-or-env fallbacks, now via attributes.

Call sites (~22 across algorithms, sync_rollout_actor,
trajectory_collector, SC setup, all example run scripts, flow_grpo)
move to attribute access.

Reference configs: always-emitted Optional keys are added with their
default values at both the logger level and inside the nested
wandb/tensorboard/mlflow blocks (deep-verified: each reference logger
block now matches LoggerConfig.model_dump exactly). Test fixtures wrap
logger dict literals in LoggerConfig.model_construct and move
subscripts to attribute access.

Signed-off-by: Shubhankar Tripathy <[email protected]>
@lonexreb

lonexreb commented Oct 1, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test 93242d3

@lonexreb

lonexreb commented Oct 1, 2026

Copy link
Copy Markdown
Contributor Author

Local deep-test pass (same hand-built-venv setup as on #4278): tests/unit/utils/test_checkpoint.py 73/73, tests/unit/test_config_v2.py + test_config_validation.py 1172 passed / 0 failed on this branch. One fix from that run is in the new commit: test_checkpoint_manager_rejects_top_level_automodel_fields still fed a raw OmegaConf dict into CheckpointManager, which now takes the validated model — the rejection guard reads model_extra, so the test now routes legacy keys through CheckpointingConfig.model_validate, matching the production path. Single-controller suites are running locally as well; will follow up if anything surfaces.

@svcnvidia-nemo-ci svcnvidia-nemo-ci added waiting-on-maintainers Waiting on maintainers to respond and removed waiting-on-maintainers Waiting on maintainers to respond labels Oct 1, 2026
@lonexreb

lonexreb commented Oct 5, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test f181f5c

@lonexreb
lonexreb force-pushed the refactor/config-v2-checkpointing branch from 93242d3 to f181f5c Compare October 5, 2026 05:49
@lonexreb

lonexreb commented Oct 5, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto main after #4278 and #3916 landed — the conflicts were the expected overlap in shared test fixtures (logger/cluster wrappings from the merged PRs vs this PR's checkpointing wrappings; resolution keeps both). Re-verified locally on the rebased branch: tests/unit/utils/test_checkpoint.py + test_config_v2.py + test_config_validation.py — 1174 passed, 0 failed. This is the last PR of the utils-layer trio; same recipe as the two you've merged.

@svcnvidia-nemo-ci svcnvidia-nemo-ci added waiting-on-maintainers Waiting on maintainers to respond and removed waiting-on-maintainers Waiting on maintainers to respond labels Oct 5, 2026
@lonexreb

Copy link
Copy Markdown
Contributor Author

/ok to test 7c17278

@lonexreb
lonexreb force-pushed the refactor/config-v2-checkpointing branch from f181f5c to 7c17278 Compare October 10, 2026 04:29
@lonexreb
lonexreb force-pushed the refactor/config-v2-checkpointing branch from fa3466f to e33acf6 Compare October 31, 2026 23:06
@svcnvidia-nemo-ci svcnvidia-nemo-ci removed the waiting-on-maintainers Waiting on maintainers to respond label Nov 1, 2026
@RayenTian

Copy link
Copy Markdown
Contributor

/ok to test 3e41070

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]>

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

Merge blockers — please rebase on main; the branch now conflicts with #5009 in three tests/unit/single_controller/ files, and the "Check if PR branch is up to date" job allows at most 10 commits behind.

Thanks for moving CheckpointingConfig and PretrainedCheckpointConfig onto BaseModel; the migration keeps the Automodel-only legacy keys failing at startup with a clear error through model_extra, so stale recipes are still caught.

Scope: the CheckpointingConfig / PretrainedCheckpointConfig migration with its callers and tests, at 5202d4d

Checked:

  • Schema defaults vs exemplar YAMLs — save_optimizer, save_data_plane and checkpoint_must_save_by match; load_replay_buffer does not (inline); the remaining None defaults keep the old absent-key behaviour.
  • Dict-style readers — none left on the migrated config in nemo_rl/; the two remaining .get reads operate on a dumped dict and on the raw YAML.
  • Reference configs — the new Optional keys are dumped, so test_config_v2_same_as_v1 stays in sync.

Remaining comments are on:

  • load_replay_buffer declared as a tri-state that no reader distinguishes from True
  • defaulting save_period to 10, with a note that higher_is_better is only read when metric_name is set
  • keeping PretrainedCheckpointConfig a TypedDict until PolicyConfig migrates, which removes the model_dump() hand-offs

nit

  • unify test config construction to keyword arguments
  • indent the CheckpointingConfig docstring entries under Attributes: and add the three missing ones

Comment thread nemo_rl/utils/checkpoint.py Resolved
Comment thread tests/unit/utils/test_checkpoint.py Resolved
Comment thread nemo_rl/utils/checkpoint.py Resolved
Comment thread nemo_rl/utils/checkpoint.py Resolved
Comment thread nemo_rl/utils/checkpoint.py Resolved
@svcnvidia-nemo-ci svcnvidia-nemo-ci added waiting-on-maintainers Waiting on maintainers to respond and removed waiting-on-maintainers Waiting on maintainers to respond labels Nov 4, 2026
Merge origin/main (49 commits) to clear the conflicts with NVIDIA-NeMo#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
  (NVIDIA-NeMo#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 (NVIDIA-NeMo#5009 testing rules).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Shubhankar Tripathy <[email protected]>
@lonexreb

lonexreb commented Nov 7, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @yuki-97, all addressed in 47d151c.

Merge blockers: merged main (49 commits; 0 behind now). The #5009 conflicts were taken from main, and main's new tests plus the code it moved into rollout_checkpointing/ (restore.py, snapshots.py) now use attribute access. Main's mc.checkpointing.update(...) calls in test_setup.py became attribute assignments.

Inline comments:

  • load_replay_buffer: now bool = True; tri-state comment and "Defaults to None" removed, grpo.py parameter typed bool, both readers test not load_replay_buffer, the five reference configs dump true, and test_grpo.py drops the None case and passes True.
  • save_period: int = 10, with the higher_is_better note (also in the docstring).
  • PretrainedCheckpointConfig is back to the TypedDict; the seven setup sites hand it over as-is, PolicyConfig.pretrained_checkpoint is NotRequired[PretrainedCheckpointConfig] again (nemo_rl/models/policy/__init__.py now matches main), and the forwarding test compares the hand-off directly.
  • Docstring: entries indented under Attributes:, save_period / checkpoint_must_save_by / pretrained_checkpoint added.
  • Test configs: CheckpointingConfig(k=v) when the required fields are given, .model_construct(k=v) otherwise; the checkpoint_config fixtures return the model. The one exception is the legacy-keys test, which still passes the stale keys as kwargs because that is what it checks.

I also dropped test_checkpoint_manager_defaults_when_optional_keys_omitted, which only checked pydantic defaults (#5009 rule).

Local: ruff and pyrefly clean on the PR files; 431/431 in test_setup.py, test_sft_v2.py, test_checkpoint.py, plus the forwarding test. In the wider utils/algorithms/single_controller/data_plane run, the 139 remaining failures also fail on a clean main checkout here (missing transfer_queue/mooncake/CUDA, gated HF models).

@svcnvidia-nemo-ci svcnvidia-nemo-ci added waiting-on-maintainers Waiting on maintainers to respond and removed waiting-on-maintainers Waiting on maintainers to respond labels Nov 7, 2026
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 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 addressing the previous round's comments.

Scope: fix commits 47d151c through cf10c7a, checked against the previous review's open threads, plus a scan of the current PR changes.

Checked:

  • Replay restoration now uses a boolean contract consistently across the schema, both readers, reference configs and tests.
  • The pretrained checkpoint remains a TypedDict and all seven setup hand-offs match the downstream dictionary consumers.
  • The requested constructor conversions, field documentation and save-period default are implemented.
  • Validation was static inspection plus an extracted-constructor reproduction; pytest/GPU tests were not run, and current-head checks contain no unit-test results.

Remaining comments are on:

  • Two remaining dictionary fixtures need migration to the model-only CheckpointManager interface.

nit

  • Refresh the PR description and two stale or inaccurate documentation statements.

Generated by Codex

Comment thread nemo_rl/utils/checkpoint.py Resolved
Comment thread nemo_rl/utils/checkpoint.py Resolved
Comment thread nemo_rl/utils/checkpoint.py Resolved
@yuki-97

yuki-97 commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

/ok to test f83d7a1

@yuki-97
yuki-97 enabled auto-merge (squash) November 12, 2026 16:15
@svcnvidia-nemo-ci svcnvidia-nemo-ci removed the waiting-on-maintainers Waiting on maintainers to respond label Nov 12, 2026
@yuki-97

yuki-97 commented Nov 14, 2026

Copy link
Copy Markdown
Contributor

/ok to test f55d456

@yuki-97

yuki-97 commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

hi @lonexreb , thanks for helping on so much things!
currently it looks our CI has some issue, do you mind me submitting a PR for your changes to go through CI and merge it? will make sure adding you as co-author.

@lonexreb

Copy link
Copy Markdown
Contributor Author

Hi @yuki-97, yes please, go ahead and open your own PR with these changes. Thanks for the reviews and for adding me as co-author. I'll leave this branch alone so it doesn't drift from yours, and you can close this PR once yours lands. If CI turns up anything in the checkpointing changes, ping me here and I'll take a look.

@yuki-97

yuki-97 commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

closes with #5239, thanks @lonexreb !

@yuki-97 yuki-97 closed this Nov 16, 2026
auto-merge was automatically disabled November 16, 2026 15:39

Pull request was closed

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 community-request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants