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

Added SaveConfigCallback.save_config #17475

Merged
merged 7 commits into from
May 4, 2023
Merged

Added SaveConfigCallback.save_config #17475

merged 7 commits into from
May 4, 2023

Conversation

mauvilsa
Copy link
Contributor

@mauvilsa mauvilsa commented Apr 26, 2023

What does this PR do?

Makes it easy to subclass SaveConfigCallback to additionally save the config in other places, such as a logger. The main purpose of this feature is to provide a way to implement a custom save config without having to worry about ranks or race conditions.

Motivated by #15340 (comment)

Might fix #14628, unless we consider that to be an out-of-the-box saving as artifact in logger.

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

@github-actions github-actions bot added the pl Generic label for PyTorch Lightning package label Apr 26, 2023
@mauvilsa mauvilsa marked this pull request as ready for review April 26, 2023 04:36
@awaelchli awaelchli added feature Is an improvement or enhancement lightningcli pl.cli.LightningCLI labels Apr 26, 2023
src/lightning/pytorch/cli.py Outdated Show resolved Hide resolved
docs/source-pytorch/cli/lightning_cli_advanced.rst Outdated Show resolved Hide resolved
@Borda Borda changed the title Added SaveConfigCallback.extra_save_config Added SaveConfigCallback.extra_save_config Apr 27, 2023
@mauvilsa mauvilsa changed the title Added SaveConfigCallback.extra_save_config Added SaveConfigCallback.save_config Apr 28, 2023
@mergify mergify bot removed the has conflicts label Apr 28, 2023
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.

This looks better than before but still doesn't seem ideal to me. Left comments!

docs/source-pytorch/cli/lightning_cli_advanced.rst Outdated Show resolved Hide resolved
src/lightning/pytorch/cli.py Show resolved Hide resolved
src/lightning/pytorch/cli.py Outdated Show resolved Hide resolved
@mergify mergify bot added the has conflicts label May 3, 2023
@mergify mergify bot removed the has conflicts label May 4, 2023
Comment on lines +68 to +72
class LoggerSaveConfigCallback(SaveConfigCallback):
def save_config(self, trainer: Trainer, pl_module: LightningModule, stage: str) -> None:
if isinstance(trainer.logger, Logger):
config = self.parser.dump(self.config, skip_none=False) # Required for proper reproducibility
trainer.logger.log_hyperparams({"config": config})
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@tommycwh I answer your comment since it is about this pull request.

However, I would want some more instructions on how to implement my save_config() function. For example, in my example above, there are some hyperparameters (like those numerical values) that can be saved by the "default" config saving mechanism and I would need to implement my own mechanism for the others that cannot be saved automaically (like the init_args).

I am not sure if I truly understand what you mean or what exactly is it that you want to achieve. This pull request adds save_config such that it can be used however people want to use it. In principle the config is intended for reproducibility and I would save it in a logger as an artifact or a single yaml string which includes any init_args. This is just three lines of code like shown in the example. Doing something different is perfectly fine, but it depends on what you want to do.

@carmocca carmocca enabled auto-merge (squash) May 4, 2023 17:46
@carmocca carmocca merged commit fa509af into Lightning-AI:master May 4, 2023
@mergify mergify bot added the ready PRs ready to be merged label May 4, 2023
@carmocca carmocca added this to the 2.1 milestone May 4, 2023
@mauvilsa mauvilsa deleted the cli-extra-save-config branch May 4, 2023 18:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Is an improvement or enhancement lightningcli pl.cli.LightningCLI 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.

Upload pytorch lightning config.yaml as artifact for WandB logger
4 participants