-
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
Closed
Closed
Changes from 15 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
ec07d11
Added default parser for trainer and class method to construct traine…
78b3552
Removed print statement
77a1fc3
Added test for constructing Trainer from command line args
a68baeb
Removed extra line
541e2ec
Merge branch 'master' into trainerArgparser
mtnwni 679c9a7
Removed redundant imports, removed whitespace from empty lines
a017312
Fixed typo
1c7fccc
Updated default parser creation to get class attributes automatically
c3942cd
Updated default parser creation to get class attributes automatically
b31295f
Merge branch 'trainerArgparser' of https://github.com/skepticleo/pyto…
fc66c0c
Added method to get default args for trainer
7eb4149
Trimmed trainer get default args method
7cf6a7c
Updated from argparse method to not return trainer with static arguments
0ee7128
Update trainer get default args to classmethod
b417be7
adjustment
Borda e456cbc
fix
Borda df0a487
Fixed variable name
9a8d05a
Update trainer.py
williamFalcon baf5219
Update test_trainer.py
williamFalcon 1a7c208
Update test_trainer.py
williamFalcon 60d4ca5
Update trainer.py
williamFalcon cd25d79
Update test_trainer.py
williamFalcon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
import sys | ||
import warnings | ||
import logging as log | ||
from argparse import ArgumentParser | ||
from typing import Union, Optional, List, Dict, Tuple, Iterable | ||
|
||
import torch | ||
|
@@ -116,6 +117,7 @@ def __init__( | |
profiler: Optional[BaseProfiler] = None, | ||
benchmark: bool = False, | ||
reload_dataloaders_every_epoch: bool = False, | ||
**kwargs | ||
): | ||
r""" | ||
|
||
|
@@ -626,6 +628,7 @@ def on_train_end(self): | |
|
||
# Transfer params | ||
# Backward compatibility | ||
self.num_nodes = num_nodes | ||
if nb_gpu_nodes is not None: | ||
warnings.warn("`nb_gpu_nodes` has renamed to `num_nodes` since v0.5.0" | ||
" and this method will be removed in v0.8.0", DeprecationWarning) | ||
|
@@ -746,10 +749,12 @@ def on_train_end(self): | |
self.weights_save_path = weights_save_path | ||
|
||
# accumulated grads | ||
self.accumulate_grad_batches = accumulate_grad_batches | ||
self.configure_accumulated_gradients(accumulate_grad_batches) | ||
|
||
# allow int, string and gpu list | ||
self.data_parallel_device_ids = parse_gpu_ids(gpus) | ||
self.gpus = gpus | ||
self.data_parallel_device_ids = parse_gpu_ids(self.gpus) | ||
self.root_gpu = determine_root_gpu_device(self.data_parallel_device_ids) | ||
|
||
# tpu state flags | ||
|
@@ -796,6 +801,7 @@ def on_train_end(self): | |
self.row_log_interval = row_log_interval | ||
|
||
# how much of the data to use | ||
self.overfit_pct = overfit_pct | ||
self.determine_data_use_amount(train_percent_check, val_percent_check, | ||
test_percent_check, overfit_pct) | ||
|
||
|
@@ -818,6 +824,29 @@ def slurm_job_id(self) -> int: | |
job_id = None | ||
return job_id | ||
|
||
@property | ||
@classmethod | ||
def default_attributes(cls): | ||
return vars(cls()) | ||
|
||
@classmethod | ||
def add_argparse_args(cls, parent_parser: ArgumentParser) -> ArgumentParser: | ||
|
||
parser = ArgumentParser(parents=[parent_parser]) | ||
|
||
trainer_default_params = Trainer.default_attributes | ||
|
||
for arg in trainer_default_args: | ||
parser.add_argument('--{0}'.format(arg), default=trainer_default_params[arg], dest=arg) | ||
|
||
return parser | ||
|
||
@classmethod | ||
def from_argparse_args(cls, args) -> Trainer: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Throwing a |
||
|
||
params = vars(args) | ||
return cls(**params) | ||
|
||
def __parse_gpu_ids(self, gpus): | ||
"""Parse GPUs id. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@Borda
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