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

Custom argparser extension with Trainer arguments (argument types added) #1147

Merged

Conversation

alexeykarnachev
Copy link
Contributor

@alexeykarnachev alexeykarnachev commented Mar 14, 2020

Before submitting

  • Was this discussed/approved via a Github issue? (no need for typos and docs improvements)
  • Did you read the contributor guideline, Pull Request section?
  • Did you make sure to update the docs?
  • Did you write any new necessary tests?
  • If you made a notable change (that affects users), did you update the CHANGELOG?

What does this PR do?

Fixes #1139

PR review

Anyone in the community is free to review the PR once the tests have passed.
If we didn't discuss your PR in Github issues there's a high chance it will not be merged.

Did you have fun?

Make sure you had fun coding 🙃

@pep8speaks
Copy link

pep8speaks commented Mar 14, 2020

Hello @alexeykarnachev! Thanks for updating this PR.

There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻

Comment last updated at 2020-03-24 18:54:51 UTC

@alexeykarnachev alexeykarnachev marked this pull request as ready for review March 14, 2020 15:45
@alexeykarnachev alexeykarnachev changed the title Custom argparser extension with Trainer arguments (argument types added) WIP Custom argparser extension with Trainer arguments (argument types added) Mar 14, 2020
@alexeykarnachev alexeykarnachev changed the title WIP Custom argparser extension with Trainer arguments (argument types added) Custom argparser extension with Trainer arguments (argument types added) Mar 14, 2020
@Borda Borda added the feature Is an improvement or enhancement label Mar 15, 2020
@Borda Borda added this to the 0.7.2 milestone Mar 15, 2020
Copy link
Member

@Borda Borda left a comment

Choose a reason for hiding this comment

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

LGTM 🚀

pytorch_lightning/trainer/trainer.py Outdated Show resolved Hide resolved
pytorch_lightning/trainer/trainer.py Outdated Show resolved Hide resolved
def add_argparse_args(cls, parent_parser: ArgumentParser) -> ArgumentParser:
"""Extend existing argparse by default `Trainer` attributes."""
parser = ArgumentParser(parents=[parent_parser], add_help=False)
def _get_argparse_args_and_types(cls) -> Generator[Tuple[str, Any, Any], None, None]:
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
def _get_argparse_args_and_types(cls) -> Generator[Tuple[str, Any, Any], None, None]:
def _get_init_arguments_and_types(cls) -> Generator[Tuple[str, Any, Any], None, None]:

sounds a bit better as it can be used elsewhere not only for argparser

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe in this case it’s worth to make this method public?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

And also move the allowed_types to the method's input arguments?

Copy link
Member

Choose a reason for hiding this comment

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

yes, both are good suggestions, lest doing it...

Copy link
Contributor Author

@alexeykarnachev alexeykarnachev Mar 16, 2020

Choose a reason for hiding this comment

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

I decided to make this method more general. Let's a client decides what arguments he needs. For instance, add_argparse_args is a "client" of this method, and it restricts the allowed types by itself.
What are your thoughts on this?

Copy link
Member

Choose a reason for hiding this comment

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

that would be nice, just thinking if is better to defien wanted args/types or ignored ones

tests/trainer/test_trainer.py Outdated Show resolved Hide resolved
@codecov
Copy link

codecov bot commented Mar 16, 2020

Codecov Report

Merging #1147 into master will not change coverage by %.
The diff coverage is n/a.

@@          Coverage Diff           @@
##           master   #1147   +/-   ##
======================================
  Coverage      91%     91%           
======================================
  Files          61      61           
  Lines        3151    3151           
======================================
  Hits         2868    2868           
  Misses        283     283           

@Borda
Copy link
Member

Borda commented Mar 17, 2020

@alexeykarnachev could you pls update from master? we have fixed the failure in #1163

@joeddav
Copy link

joeddav commented Mar 20, 2020

This PR works great for me with the exception of boolean casting. I.e. if I run with --checkpoint_callback 0 or false or anything but an empty value, then args.checkpoint_callback -> True, since bool('false') -> True.

This can be resolved with distutils:

bool('false')
# True
bool(distutils.util.strtobool('false'))
# False

Would it be possible to add a line to check for bool types and cast them using the above? Otherwise the behavior is quite counterintuitive.

@Borda
Copy link
Member

Borda commented Mar 20, 2020

good point, @joeddav could you do it in review mode as a suggestion (so it is clear and @alexeykarnachev can simply accept it) or follow-up PR?

Copy link

@joeddav joeddav left a comment

Choose a reason for hiding this comment

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

Add more robust boolean type casting

pytorch_lightning/trainer/trainer.py Show resolved Hide resolved
pytorch_lightning/trainer/trainer.py Show resolved Hide resolved
@mergify
Copy link
Contributor

mergify bot commented Mar 24, 2020

This pull request is now in conflict... :(

@williamFalcon williamFalcon merged commit ced662f into Lightning-AI:master Mar 24, 2020
@Borda Borda modified the milestones: v0.7., v0.7.x Apr 18, 2021
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 ready PRs ready to be merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Can't cast Trainer automatically generated args to their required types
5 participants