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

Update logic to parse trainer settings from env vars #18876

Closed
wants to merge 10 commits into from

Conversation

awaelchli
Copy link
Contributor

@awaelchli awaelchli commented Oct 26, 2023

What does this PR do?

Fixes #18874

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

📚 Documentation preview 📚: https://pytorch-lightning--18876.org.readthedocs.build/en/18876/

cc @carmocca @mauvilsa @justusschock @awaelchli @Borda

@github-actions github-actions bot added the pl Generic label for PyTorch Lightning package label Oct 26, 2023
@awaelchli awaelchli added bug Something isn't working trainer strategy: ddp DistributedDataParallel labels Oct 26, 2023
@awaelchli awaelchli added this to the 2.1.x milestone Oct 26, 2023
@awaelchli awaelchli force-pushed the bugfix/cli-env-parse branch from c63f5d1 to c3b3a78 Compare October 26, 2023 19:46
@awaelchli awaelchli force-pushed the bugfix/cli-env-parse branch from 8baf30e to 09b6707 Compare October 26, 2023 20:00
@awaelchli awaelchli force-pushed the bugfix/cli-env-parse branch from 8e296b0 to 363ed83 Compare October 26, 2023 20:03
@@ -64,9 +64,9 @@ def insert_env_defaults(self: Any, *args: Any, **kwargs: Any) -> Any:
kwargs.update(dict(zip(cls_arg_names, args)))
env_variables = vars(_parse_env_variables(cls))
# update the kwargs by env variables
kwargs = dict(list(env_variables.items()) + list(kwargs.items()))
kwargs = dict(list(kwargs.items()) + list(env_variables.items()))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the fix. The env vars now take precendence over the arguments passed by the user.
This functionality is undocumented and so I am ok with changing that. I am confident that this was always the intention.

@@ -288,6 +290,21 @@ def test_lightning_env_parse(cleandir):
assert cli.config.fit.trainer.logger is False


def test_lightning_cli_and_trainer_kwargs_override(cleandir):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Test doesn't pass on master, because the CLI passes all arguments to the Trainer, including the defaults, and so env variables would never take precedence.

@awaelchli awaelchli marked this pull request as ready for review October 26, 2023 20:05
@awaelchli awaelchli added the lightningcli pl.cli.LightningCLI label Oct 26, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Oct 26, 2023

⛈️ Required checks status: Has failure 🔴

Warning
This job will need to be re-run to merge your PR. If you do not have write access to the repository, you can ask Lightning-AI/lai-frameworks to re-run it. If you push a new commit, all of CI will re-trigger.

Groups summary

🔴 pytorch_lightning: Tests workflow
Check ID Status
pl-cpu (macOS-11, lightning, 3.8, 1.12, oldest) success
pl-cpu (macOS-11, lightning, 3.9, 1.12) success
pl-cpu (macOS-11, lightning, 3.10, 1.13) success
pl-cpu (macOS-11, lightning, 3.10, 2.0) success
pl-cpu (macOS-11, lightning, 3.10, 2.1) success
pl-cpu (ubuntu-20.04, lightning, 3.8, 1.12, oldest) success
pl-cpu (ubuntu-20.04, lightning, 3.9, 1.12) success
pl-cpu (ubuntu-20.04, lightning, 3.10, 1.13) success
pl-cpu (ubuntu-20.04, lightning, 3.10, 2.0) success
pl-cpu (ubuntu-20.04, lightning, 3.10, 2.1) success
pl-cpu (windows-2022, lightning, 3.8, 1.12, oldest) success
pl-cpu (windows-2022, lightning, 3.9, 1.12) success
pl-cpu (windows-2022, lightning, 3.10, 1.13) success
pl-cpu (windows-2022, lightning, 3.10, 2.0) success
pl-cpu (windows-2022, lightning, 3.10, 2.1) success
pl-cpu (macOS-11, pytorch, 3.8, 1.13) success
pl-cpu (ubuntu-20.04, pytorch, 3.8, 1.13) failure
pl-cpu (windows-2022, pytorch, 3.8, 1.13) success
pl-cpu (macOS-12, pytorch, 3.11, 2.0) failure
pl-cpu (macOS-12, pytorch, 3.11, 2.1) success
pl-cpu (ubuntu-22.04, pytorch, 3.11, 2.0) success
pl-cpu (ubuntu-22.04, pytorch, 3.11, 2.1) success
pl-cpu (windows-2022, pytorch, 3.11, 2.0) success
pl-cpu (windows-2022, pytorch, 3.11, 2.1) success

These checks are required after the changes to src/lightning/pytorch/trainer/trainer.py, src/lightning/pytorch/utilities/argparse.py, tests/tests_pytorch/test_cli.py, tests/tests_pytorch/trainer/flags/test_env_vars.py.

🔴 pytorch_lightning: Azure GPU
Check ID Status
pytorch-lightning (GPUs) (testing Lightning | latest) success
pytorch-lightning (GPUs) (testing PyTorch | latest) failure

These checks are required after the changes to src/lightning/pytorch/trainer/trainer.py, src/lightning/pytorch/utilities/argparse.py, tests/tests_pytorch/test_cli.py, tests/tests_pytorch/trainer/flags/test_env_vars.py.

🟢 pytorch_lightning: Benchmarks
Check ID Status
lightning.Benchmarks success

These checks are required after the changes to src/lightning/pytorch/trainer/trainer.py, src/lightning/pytorch/utilities/argparse.py.

🟢 pytorch_lightning: Docs
Check ID Status
docs-make (pytorch, doctest) success
docs-make (pytorch, html) success

These checks are required after the changes to src/lightning/pytorch/trainer/trainer.py, src/lightning/pytorch/utilities/argparse.py.

🟢 mypy
Check ID Status
mypy success

These checks are required after the changes to src/lightning/pytorch/trainer/trainer.py, src/lightning/pytorch/utilities/argparse.py.

🟢 install
Check ID Status
install-pkg (ubuntu-22.04, app, 3.8) success
install-pkg (ubuntu-22.04, app, 3.11) success
install-pkg (ubuntu-22.04, fabric, 3.8) success
install-pkg (ubuntu-22.04, fabric, 3.11) success
install-pkg (ubuntu-22.04, pytorch, 3.8) success
install-pkg (ubuntu-22.04, pytorch, 3.11) success
install-pkg (ubuntu-22.04, lightning, 3.8) success
install-pkg (ubuntu-22.04, lightning, 3.11) success
install-pkg (ubuntu-22.04, notset, 3.8) success
install-pkg (ubuntu-22.04, notset, 3.11) success
install-pkg (macOS-12, app, 3.8) success
install-pkg (macOS-12, app, 3.11) success
install-pkg (macOS-12, fabric, 3.8) success
install-pkg (macOS-12, fabric, 3.11) success
install-pkg (macOS-12, pytorch, 3.8) success
install-pkg (macOS-12, pytorch, 3.11) success
install-pkg (macOS-12, lightning, 3.8) success
install-pkg (macOS-12, lightning, 3.11) success
install-pkg (macOS-12, notset, 3.8) success
install-pkg (macOS-12, notset, 3.11) success
install-pkg (windows-2022, app, 3.8) success
install-pkg (windows-2022, app, 3.11) success
install-pkg (windows-2022, fabric, 3.8) success
install-pkg (windows-2022, fabric, 3.11) success
install-pkg (windows-2022, pytorch, 3.8) success
install-pkg (windows-2022, pytorch, 3.11) success
install-pkg (windows-2022, lightning, 3.8) success
install-pkg (windows-2022, lightning, 3.11) success
install-pkg (windows-2022, notset, 3.8) success
install-pkg (windows-2022, notset, 3.11) success

These checks are required after the changes to src/lightning/pytorch/trainer/trainer.py, src/lightning/pytorch/utilities/argparse.py.


Thank you for your contribution! 💜

Note
This comment is automatically generated and updates for 60 minutes every 180 seconds. If you have any other questions, contact carmocca for help.

@codecov
Copy link

codecov bot commented Oct 26, 2023

Codecov Report

Merging #18876 (6b22a0f) into master (182c30b) will decrease coverage by 27%.
The diff coverage is 100%.

Additional details and impacted files
@@            Coverage Diff            @@
##           master   #18876     +/-   ##
=========================================
- Coverage      75%      48%    -27%     
=========================================
  Files         443      435      -8     
  Lines       35377    35224    -153     
=========================================
- Hits        26641    16926   -9715     
- Misses       8736    18298   +9562     

Copy link
Contributor

@mauvilsa mauvilsa left a comment

Choose a reason for hiding this comment

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

This change worries me a bit. If a unit test now fails, it means it is a breaking change. Though, most worrying is that I am not sure if the change breaks reproducibility, which is one of the main features of LightningCLI. I mean, if an environment variable is set, would this value be in the automatically saved config?

Note that if environment variables should always be enabled, then LightningArgumentParsercan be changed such that default_env is True by default. People could change it to False if they want to. Also, even if default_env=False, environment variable parsing can be enabled without changing the code by setting environment variable JSONARGPARSE_DEFAULT_ENV=true.

@carmocca
Copy link
Contributor

carmocca commented Oct 27, 2023

I agree with Mauricio's comment. If this is only meant for the CLI, we can either change the default or use that special environment variable.

I am hesitant about changing the trainer env vars logic like this. It is definitely a breaking change. The original feature, added in 1.0 (#4022) overwrote the Trainer values. But it was changed to the current behaviour in 1.3 (#6510). There must have been a reason to change it in 1.3 like this, so if you still want to change that (unrelated to the CLI decision), it should be explained why that reason would not matter anymore.

@awaelchli
Copy link
Contributor Author

awaelchli commented Oct 27, 2023

I'm sorry, I didn't know that I could set JSONARGPARSE_DEFAULT_ENV=true. I can work around #18874 with this for now. The reproducibility aspect is a valid counter argument and I can understand that. Thank you for pointing it out.

But I definitely strongly object to the accusations that this is a breaking change. The feature for setting Trainer arguments through environment variables is completely undocumented. If it is documented somewhere I'm missing it, I request that you provide proof.

@awaelchli awaelchli closed this Oct 27, 2023
@carmocca
Copy link
Contributor

carmocca commented Oct 27, 2023

Across the history of the project, many changed features that were undocumented have been considered "breaking changes". I guess each person has their own slightly different sense of what is a "breaking change" and if "is it in the docs?" is yours, then I would understand that you don't see it as a breaking change.

With my comment, I did not mean that it was documented, but that users have been able to use these environment variables the way it is now since 1.3.

@carmocca carmocca deleted the bugfix/cli-env-parse branch October 27, 2023 17:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working has conflicts lightningcli pl.cli.LightningCLI pl Generic label for PyTorch Lightning package strategy: ddp DistributedDataParallel trainer
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Can't override Trainer defaults via env variables for LightningCLI
4 participants