-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
Default argparser for Trainer #952
Conversation
@staticmethod | ||
def add_default_args(parent_parser): | ||
|
||
parser = ArgumentParser(parents=[parent_parser]) |
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.
these should be auto-generated given a trainer class. we don't want to start making sure this list maintains parity.
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.
Also, could we add it to another mixin? This file is already very long.
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.
we can regenerate it from class attributes, we may also get types (not sure hot the typing would cooperate)
but not sure how to pass help strings and single-letter shortcuts in an argument...
@PyTorchLightning/core-contributors any idea about passing help string
otherwise I would go for the automatic generated, good idea
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've drafted a solution to this that automatically parses intit's doc string for the help fields and the functions signature to populate the argument parser
pytorch_lightning/trainer/trainer.py
Outdated
@@ -781,6 +782,135 @@ def slurm_job_id(self) -> int: | |||
job_id = None | |||
return job_id | |||
|
|||
@staticmethod | |||
def add_default_args(parent_parser): |
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.
maybe rename to add_trainer_args? and ideally it can be called by:
from pytorch_lightning import Trainer
from argparse import ArgumentParser
parser = ArgumentParser()
parser = Trainer.add_argparse_args(parser)
Then we can use the same kind of language to enable any lightning module to do the same:
from pytorch_lightning import Trainer
from argparse import ArgumentParser
from project import MyPLModule
parser = ArgumentParser()
parser = Trainer.add_argparse_args(parser)
parser = MyPLModule.add_argparse_args(parser)
Hello @skepticleo! Thanks for updating this PR. There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻 Comment last updated at 2020-03-03 14:19:55 UTC |
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.
How about a class method cosntruct:
class Trainer(...):
@classmethod
def create_from_args(cls):
args = ArgParser(...)
# generate the arguments
# parse the arguments as hparams
return cls(hparams)
so later you just call trainer = Trainer.create_from_args()
and done lol
@staticmethod | ||
def add_default_args(parent_parser): | ||
|
||
parser = ArgumentParser(parents=[parent_parser]) |
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.
we can regenerate it from class attributes, we may also get types (not sure hot the typing would cooperate)
but not sure how to pass help strings and single-letter shortcuts in an argument...
@PyTorchLightning/core-contributors any idea about passing help string
otherwise I would go for the automatic generated, good idea
let's keep this to another PR :) |
in this release or another? |
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.
pls check the discussion about auto-generated items
https://github.com/PyTorchLightning/pytorch-lightning/pull/952/files#r384611463
…rch-lightning into trainerArgparser # Conflicts: # pytorch_lightning/trainer/trainer.py
pytorch_lightning/trainer/trainer.py
Outdated
trainer_default_args = vars(Trainer()) | ||
|
||
for arg in trainer_default_args: | ||
parser.add_argument('--{0}'.format(arg), default=trainer_args[arg], dest=arg) |
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.
is there a way how to get also help texts?
@skepticleo mind rebasing so we can merge? |
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.
LGTM 🚀
tests/test_trainer.py
Outdated
@@ -856,6 +857,26 @@ def test_end(self, outputs): | |||
Trainer().test(model) | |||
|
|||
|
|||
@mock.patch('argparse.ArgumentParser.parse_args', | |||
return_value=argparse.Namespace(**(Trainer.default_attributes))) |
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.
return_value=argparse.Namespace(**(Trainer.default_attributes))) | |
return_value=argparse.Namespace(**Trainer.default_attributes)) |
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.
@Borda this doesn't seem to work, I see a TypeError: type object argument after ** must be a mapping, not property
while running the unit tests
pytorch_lightning/trainer/trainer.py
Outdated
|
||
return parser | ||
|
||
@classmethod | ||
def from_argparse_args(cls, args): | ||
def from_argparse_args(cls, args) -> Trainer: |
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.
Throwing a NameError: name 'Trainer' is not defined
while running the tests
merged in #1023 for time-sensitive rebase |
@Borda is attributing author for that commit |
Before submitting
What does this PR do?
Adds a default argument parser for Trainer(#916)
Closes #916
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 🙃