-
-
Notifications
You must be signed in to change notification settings - Fork 657
added TimeLimit handler with its test and doc #1611
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
2f5d2d7
added TimeLimit handler with its test and doc
ahmedo42 1831709
fixed documentation
ahmedo42 03ab1e0
fixed docstring and formatting
ahmedo42 308b3a2
flake8 fix trailing whitespace :)
ahmedo42 0cd44f2
modified class logger , default value and tests
ahmedo42 58d4441
changed rounding to nearest integer
ahmedo42 902065e
tests refactored , docs modified
ahmedo42 159622b
fixed default value , removed global logger
ahmedo42 e589e85
fixing formatting
ahmedo42 f84f837
Merge branch 'master' into TimeLimit-handler
vfdev-5 000f7c3
Added versionadded
vfdev-5 add50c7
added test for engine termination
ahmedo42 aec7ea2
Merge branch 'TimeLimit-handler' of https://github.com/ahmedo42/ignit…
ahmedo42 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import logging | ||
import time | ||
from typing import Optional | ||
|
||
from ignite.engine import Engine | ||
|
||
__all__ = ["TimeLimit"] | ||
|
||
|
||
class TimeLimit: | ||
"""TimeLimit handler can be used to control training time for computing environments where session time is limited. | ||
Timer starts when handler is created and not training started. | ||
This handler gracefully terminates the training if time passed in the training exceeds a limit. | ||
|
||
Args: | ||
limit_sec (int, optional): Maximum time before training terminates (in seconds). Defaults to 28800. | ||
|
||
Examples: | ||
|
||
.. code-block:: python | ||
|
||
from ignite.engine import Events | ||
from ignite.handlers import TimeLimit | ||
|
||
handler = TimeLimit() # 8 hours of training | ||
trainer.add_event_handler(Events.ITERATION_COMPLETED, handler) | ||
|
||
.. versionadded:: 0.4.3 | ||
""" | ||
|
||
def __init__(self, limit_sec: Optional[int] = 28800): | ||
|
||
if not isinstance(limit_sec, int): | ||
raise TypeError("Argument limit_sec should be an integer.") | ||
if limit_sec <= 0: | ||
raise ValueError("Argument limit_sec should be a positive integer.") | ||
|
||
self.limit_sec = limit_sec | ||
self.start_time = time.time() | ||
self.logger = logging.getLogger(__name__ + "." + self.__class__.__name__) | ||
vfdev-5 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def __call__(self, engine: Engine) -> None: | ||
elapsed_time = time.time() - self.start_time | ||
if elapsed_time > self.limit_sec: | ||
self.logger.info("Reached the time limit: {} sec. Stop training".format(self.limit_sec)) | ||
engine.terminate() |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import time | ||
|
||
import pytest | ||
|
||
from ignite.engine import Engine, Events | ||
from ignite.handlers import TimeLimit | ||
|
||
|
||
def test_arg_validation(): | ||
|
||
with pytest.raises(ValueError, match=r"Argument limit_sec should be a positive integer."): | ||
TimeLimit(limit_sec=-5) | ||
|
||
with pytest.raises(TypeError, match=r"Argument limit_sec should be an integer."): | ||
TimeLimit(limit_sec="abc") | ||
|
||
|
||
def test_terminate_on_time_limit(): | ||
ahmedo42 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def _train_func(engine, batch): | ||
time.sleep(1) | ||
|
||
def _test(n_iters, limit): | ||
vfdev-5 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
started = time.time() | ||
trainer = Engine(_train_func) | ||
|
||
@trainer.on(Events.TERMINATE) | ||
def _(): | ||
trainer.state.is_terminated = True | ||
|
||
trainer.add_event_handler(Events.ITERATION_COMPLETED, TimeLimit(limit)) | ||
trainer.state.is_terminated = False | ||
|
||
trainer.run(range(n_iters)) | ||
elapsed = round(time.time() - started) | ||
assert elapsed <= limit + 1 | ||
vfdev-5 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert trainer.state.is_terminated == (n_iters > limit) | ||
|
||
_test(20, 10) | ||
_test(5, 10) |
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.
Uh oh!
There was an error while loading. Please reload this page.