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

Add Trainer max_time argument + Callback #6823

Merged
merged 50 commits into from
Apr 16, 2021
Merged

Add Trainer max_time argument + Callback #6823

merged 50 commits into from
Apr 16, 2021

Conversation

awaelchli
Copy link
Contributor

@awaelchli awaelchli commented Apr 4, 2021

What does this PR do?

Partial #6795

RFC about naming suggestions and API (class, arguments, methods).

Proposed solution:

# set max_time in Trainer (common use case):
trainer Trainer(max_time="HH:MM:SS")

# or via callback with customizable options
from pytorch_lightning.callbacks import Timer
timer = Timer(duration="HH:MM:SS", interval=("step" | "epoch"), verbose=(True | False))
trainer = Trainer(callbacks=[timer])

# user can access time elapsed for different stages
trainer.fit(model)
timer.elapsed("train")

trainer.validate()
timer.elapsed("validate")

trainer.test()
timer.elapsed("test")

TODO:

  • Finalize API (request for suggestions)
  • Add docs, example
  • Add more tests

Comment on the PR with preferences/opinion about the following questions:

  • Should max_time be exposed in the Trainer?
  • Should the Timer be a callback or be built into the Training loop?
  • Should there be Timers for val/test?

Before submitting

  • Was this discussed/approved 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 update the CHANGELOG? (not for typos, docs, test updates, or internal minor changes/refactorings)

PR review

Anyone in the community is free to review the PR once the tests have passed.
Before you start reviewing make sure you have read Review guidelines. In short, see the following bullet-list:

  • 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

Did you have fun?

Make sure you had fun coding 🙃

@pep8speaks
Copy link

pep8speaks commented Apr 4, 2021

Hello @awaelchli! Thanks for updating this PR.

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

Comment last updated at 2021-04-16 11:00:49 UTC

@awaelchli awaelchli added callback feature Is an improvement or enhancement labels Apr 4, 2021
@awaelchli awaelchli added this to the 1.3 milestone Apr 4, 2021
@codecov
Copy link

codecov bot commented Apr 4, 2021

Codecov Report

Merging #6823 (3989fc7) into master (f645df5) will decrease coverage by 2%.
The diff coverage is 100%.

@@           Coverage Diff           @@
##           master   #6823    +/-   ##
=======================================
- Coverage      92%     90%    -2%     
=======================================
  Files         194     195     +1     
  Lines       12386   12904   +518     
=======================================
+ Hits        11414   11611   +197     
- Misses        972    1293   +321     

@jlperla
Copy link

jlperla commented Apr 5, 2021

I actually am worried this is not the right design. @edenafek had sugggested this should be built into the core of the max_time and we definietley don't want to have a second early stopping callback. These things are too fragile.

@jlperla
Copy link

jlperla commented Apr 5, 2021

Ah @awaelchli Sorry I missed the stuff at the top about We can also make a max_time Trainer argument and inject the Callback internally.

If that is what is planned, and this is not a user-enabled argument, then the internal design is none of my business of course :-)

@awaelchli awaelchli changed the title Callback for setting max training duration Add Trainer max_time argument + Callback Apr 7, 2021
@awaelchli awaelchli marked this pull request as ready for review April 7, 2021 10:29
Comment on lines 107 to 118
def on_train_start(self, trainer, *args, **kwargs) -> None:
self._start_time = datetime.now()

def on_train_batch_end(self, trainer, *args, **kwargs) -> None:
if self._interval != Interval.step:
return
self._check_time_remaining(trainer)

def on_train_epoch_end(self, trainer, *args, **kwargs) -> None:
if self._interval != Interval.epoch:
return
self._check_time_remaining(trainer)
Copy link
Contributor

Choose a reason for hiding this comment

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

in case people extend this callback, *args/**kwargs makes these callbacks harder to provide typehints for

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure I understand, because I am a typehint noob.
The arguments are unused, therefore they can be of type Any.
If I did specify all the args, linter will complain they are unused.

Copy link
Contributor

@carmocca carmocca left a comment

Choose a reason for hiding this comment

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

Pushed a few commits with improvements.

LGTM

Comment on lines 126 to 142
def on_train_start(self, *args, **kwargs) -> None:
self._start_time[RunningStage.TRAINING] = datetime.now()

def on_train_end(self, *args, **kwargs) -> None:
self._end_time[RunningStage.TRAINING] = datetime.now()

def on_validation_start(self, *args, **kwargs) -> None:
self._start_time[RunningStage.VALIDATING] = datetime.now()

def on_validation_end(self, *args, **kwargs) -> None:
self._end_time[RunningStage.VALIDATING] = datetime.now()

def on_test_start(self, *args, **kwargs) -> None:
self._start_time[RunningStage.TESTING] = datetime.now()

def on_test_end(self, *args, **kwargs) -> None:
self._end_time[RunningStage.TESTING] = datetime.now()
Copy link
Contributor

Choose a reason for hiding this comment

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

n00b q: would this fail with daylight savings time? should we use time.monotonic() in case system clocks are reset or rewound?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I switched everything to monotonic, but note now time_elapsed() etc returns seconds.
Hope that's fine.

@awaelchli awaelchli added the ready PRs ready to be merged label Apr 15, 2021
Co-authored-by: Akihiro Nitta <nitta@akihironitta.com>
@awaelchli awaelchli merged commit 67d2160 into master Apr 16, 2021
@awaelchli awaelchli deleted the feature/timer branch April 16, 2021 11:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
callback 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.

8 participants