-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
[WIP] Use store_true for bool args #1646
Conversation
@nateraw I'll try to clarify this point. We want to automatically assign an appropriate type for the argument ( |
@@ -613,6 +613,16 @@ def add_argparse_args(cls, parent_parser: ArgumentParser) -> ArgumentParser: | |||
def allowed_type(x): | |||
return bool(parsing.strtobool(x)) | |||
|
|||
# Bool args with default of True parsed as flags not key value pair |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But don't we add the argument twice to the parser in such a case? One time here, and another one there:
https://github.com/PyTorchLightning/pytorch-lightning/blob/8614074dc3d97ec5b9b3296b297010f60e36e2cb/pytorch_lightning/trainer/trainer.py#L630-L635
I think it could be handled like so:
(the code is non-working sketch)
if arg_types == (bool,) and arg_default is False:
action = 'store_true'
default = None
arg_type = None
else:
action = None
default = arg_default
arg_type = allowed_type
...
parser.add_argument(
f'--{arg}',
default=default,
type=arg_type,
dest=arg,
action=action,
type = type
help='autogenerated by pl.Trainer'
)
...
So, I think, it could be generalized a little
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, this is the way I did it originally but ended up just goin with the one I pushed. I'll change it back to something like this. Thank you!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i removed parsing.strtobool(x)
on purpose... this fails on some python versions.
If we're going to use strtobool please keep the implementation i added (which is copy paste from strtobool but that works on any python version
Finishing in #1822 |
Before submitting
What does this PR do?
Overview
Fixes #1168
This PR edits
pl.Trainer.add_argparse_args
to allow users to pass flags instead of key value pairs forbool
type arguments with a default ofFalse
.For example, instead of:
python trainer_main.py --fast_dev_run True
You can now do:
python trainer_main.py --fast_dev_run
Notes
parser.add_argument
call, but I felt it started to look messy when I did that.allowed_type(x)
within the loop that gathers the flags. (CC: @alexeykarnachev from git blame)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 🙃