Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to change "=" symbol in ModelCheckpoint filenames #17999

Conversation

giorgioskij
Copy link
Contributor

@giorgioskij giorgioskij commented Jul 6, 2023

What does this PR do?

Part of #17825

This PR adds a feature, as discussed in issue #17825, to change the default "=" symbol between the name of a field and its value in the filenames generated by the ModelCheckpoint class. There was already an option to change the separator symbol in the filenames, so this follows the same exact implementation style.

Example:

ModelCheckpoint.CHECKPOINT_JOIN_CHAR = "-"  # this was already present
ModelCheckpoint.CHECKPOINT_EQUALS_CHAR = ":"  # this is my addition
ckp = ModelCheckpoint(dirpath=".", monitor="valid_loss")
trainer = L.Trainer(callbacks=[ckp])

now this trainer will save its checkpoints as: epoch:1-step:1234.ckpt instead of epoch=1-step=1234.ckpt

While adding the new attribute to the docstring of the class ModelCheckpoint, we noticed a discrepancy between code and docstring. The docs say:

For example, you can change the default last checkpoint name by doing
``checkpoint_callback.CHECKPOINT_NAME_LAST = "{epoch}-last"``

However, the correct way to change that attributes at the moment is by using the class name, and not the instance name, so:
ModelCheckpoint.CHECKPOINT_NAME_LAST = {epoch}-last

In issue #17825 we decided to keep this as is, and make a new pr to change the code to behave how it is currently documented.

I also updated CHANGELOG.md to mention this new attribute, including the link to this PR.

Thanks a lot for your time reviewing this!

Before submitting
  • Was this discussed/agreed via a GitHub issue? (not for typos and docs)
  • Did you read the contributor guideline, Pull Request section?
  • Did you make sure your PR does only one thing, instead of bundling different changes together?
  • Did you make sure to update the documentation with your changes? (if necessary)
  • Did you write any new necessary tests? (not for typos and docs)
  • Did you verify new and existing tests pass locally with your changes?
  • Did you list all the breaking changes introduced by this pull request?
  • Did you update the CHANGELOG? (not for typos, docs, test updates, or minor internal changes/refactors)

PR review

Anyone in the community is welcome to review the PR.
Before you start reviewing, make sure you have read the review guidelines. In short, see the following bullet-list:

Reviewer checklist
  • Is this pull request ready for review? (if not, please submit in draft mode)
  • Check that all items from Before submitting are resolved
  • Make sure the title is self-explanatory and the description concisely explains the PR
  • Add labels and milestones (and optionally projects) to the PR so it can be classified

Add attribute CHECKPOINT_EQUALS_CHAR to class ModelCheckpoint, which
determines what character to use between the <key, value> pairs in
the filename of the checkpoints. Before it was hard-coded to "=".
Now it defaults to "=" but can be changed.
Also fix a mistake in the documentation of that same class.
@github-actions github-actions bot added the pl Generic label for PyTorch Lightning package label Jul 6, 2023
@giorgioskij giorgioskij closed this Jul 6, 2023
@giorgioskij giorgioskij reopened this Jul 6, 2023
@awaelchli awaelchli changed the title Solve issue 17825: add option to change "=" symbol in ModelCheckpoint filenames Add option to change "=" symbol in ModelCheckpoint filenames Jul 7, 2023
@awaelchli awaelchli added feature Is an improvement or enhancement callback: model checkpoint labels Jul 7, 2023
@awaelchli awaelchli added this to the 2.1 milestone Jul 7, 2023
@awaelchli awaelchli added the community This PR is from the community label Jul 7, 2023
@giorgioskij giorgioskij marked this pull request as ready for review July 7, 2023 17:31
Copy link
Contributor

@awaelchli awaelchli left a comment

Choose a reason for hiding this comment

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

Nice addition!
We would need a test too. I suggest to extend the existing test_model_checkpoint_format_checkpoint_name in test_model_checkpoint.py.

src/lightning/pytorch/CHANGELOG.md Outdated Show resolved Hide resolved
Copy link
Contributor

@carmocca carmocca left a comment

Choose a reason for hiding this comment

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

LGTM!

@mergify mergify bot added the ready PRs ready to be merged label Jul 9, 2023
@awaelchli awaelchli enabled auto-merge (squash) July 9, 2023 18:30
@awaelchli awaelchli merged commit f952750 into Lightning-AI:master Jul 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
callback: model checkpoint community This PR is from the community feature Is an improvement or enhancement pl Generic label for PyTorch Lightning package ready PRs ready to be merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants