Add configurable checkpoint filenames and state-dict key remapping
Summary
- Add optional
filename_formattosave_checkpointso model checkpoint basenames can use custom layouts (e.g. zero-padded epochs) while preserving legacy naming when unset. - Add
Module._backward_compat_state_dict_mapperand apply it infrom_checkpointbeforeload_state_dict, mirroring the existing constructor-arg backward-compat hook for refactored parameter names.
Closes #1175 Closes #1173
Test plan
- [x]
pytest test/utils/test_checkpoint.py::test_save_checkpoint_filename_format - [x]
pytest test/utils/test_checkpoint.py::test_save_checkpoint_filename_format_invalid_placeholder - [x]
pytest test/models/test_from_checkpoint.py::test_from_checkpoint_state_dict_mapper - [x] pre-commit on changed files
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.
Greptile Summary
This PR adds two independent utilities: a filename_format parameter to save_checkpoint for custom model-checkpoint filenames (e.g., zero-padded epochs), and a _backward_compat_state_dict_mapper hook on Module for version-aware state-dict key remapping in from_checkpoint.
- The state-dict mapper feature (
module.py) is well-structured and correctly integrated into both the zip and tarfrom_checkpointcode paths using the existingmdlus_file_versionmetadata key. - The
filename_formatfeature (checkpoint.py) works correctly whenepochis explicitly specified, but_resolve_checkpoint_indexuses a legacy regex that cannot discover custom-formatted filenames; auto-indexing (epoch=Nonewith a custom format) will always resolve to index0and silently overwrite the first checkpoint on every subsequent save. _resolve_checkpoint_indexis invoked unconditionally but its result is ignored on the legacy (filename_format=None) code path, triggering a redundant filesystem scan on every call.
Important Files Changed
| Filename | Overview |
|---|---|
| physicsnemo/utils/checkpoint.py | Introduces _resolve_checkpoint_index helper and filename_format parameter; auto-indexing is broken when filename_format is used without an explicit epoch because the helper's regex only matches legacy filenames, and the helper result is unused on the legacy code path causing a double scan. |
| physicsnemo/core/module.py | Adds _backward_compat_state_dict_mapper classmethod (no-op base) and _apply_backward_compat_state_dict static method; correctly wired into both zip and tar from_checkpoint paths using the existing mdlus_file_version metadata key. |
| test/utils/test_checkpoint.py | Adds two tests for filename_format: happy-path with zero-padded epoch and invalid-placeholder error. Tests always pass an explicit epoch, so the broken auto-index path is not exercised. |
| test/models/test_from_checkpoint.py | Adds end-to-end test for state-dict key remapping across model versions; covers key rename and verifies weight values are preserved. |
| CHANGELOG.md | Adds changelog entries for both new features under the Unreleased section. |
Reviews (1): Last reviewed commit: "Add checkpoint filename_format and state..." | Re-trigger Greptile
Greptile triage for 8e60df5:
- P1 auto-indexing with custom
filename_format: fixed._filename_format_index_patternparses formatted basenames, andtest_save_checkpoint_filename_format_auto_indexcovers theepoch=Nonepath. - P2 no-op ternary: fixed. plain
return 0now. - P2 redundant legacy scan: fixed. legacy naming reuses
resolved_indexdirectly.
@CharlelieLrt @coreyjadams @ktangsali gentle ping when you have a moment. Greptile feedback is addressed in 8e60df5. I will rebase onto main shortly to clear the merge conflict. Closes #1173 and #1175. Thanks!