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 resuming from specific checkpoint #516

Merged
merged 7 commits into from
Nov 30, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion pytorch_lightning/trainer/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ def __init__(self,
weights_save_path=None,
amp_level='O1',
nb_sanity_val_steps=5,
truncated_bptt_steps=None):
truncated_bptt_steps=None,
resume_from_checkpoint=None):
"""

:param logger: Logger for experiment tracking
Expand Down Expand Up @@ -119,6 +120,7 @@ def __init__(self,
:param amp_level: str. Check nvidia docs for level
:param nb_sanity_val_steps: int. How many val steps before a full train loop.
:param truncated_bptt_steps: int. Enables multiple backward passes for each batch.
:param resume_from_checkpoint: str or os.PathLike object. Resume from specific checkpoint.
"""
# Transfer params
self.nb_gpu_nodes = nb_gpu_nodes
Expand All @@ -139,6 +141,7 @@ def __init__(self,
self.nb_sanity_val_steps = nb_sanity_val_steps
self.print_nan_grads = print_nan_grads
self.truncated_bptt_steps = truncated_bptt_steps
self.resume_from_checkpoint = resume_from_checkpoint
self.shown_warnings = set()

self.fast_dev_run = fast_dev_run
Expand Down
17 changes: 16 additions & 1 deletion pytorch_lightning/trainer/trainer_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import re
import signal
import warnings
from pathlib import Path
from subprocess import call
import logging

Expand Down Expand Up @@ -46,7 +47,9 @@ def restore_weights(self, model):

if not did_restore_hpc_weights:
# restore weights if same exp version
self.restore_state_if_checkpoint_exists(model)
did_restore_last_checkpoint = self.restore_state_if_checkpoint_exists(model)
if not did_restore_last_checkpoint and self.resume_from_checkpoint is not None:
self.restore_state_from_checkpoint(self.resume_from_checkpoint)

# wait for all models to restore weights
if self.use_ddp or self.use_ddp2:
Expand Down Expand Up @@ -93,6 +96,18 @@ def restore_state_if_checkpoint_exists(self, model):

return did_restore

def restore_state_from_checkpoint(self, checkpoint_path):
did_restore = False
Copy link
Member

Choose a reason for hiding this comment

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

add doc what data type the checkpoint_path is

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 removed this function. Please review the updated code :)


checkpoint_path = Path(checkpoint_path)
if not checkpoint_path.exists():
Copy link
Member

Choose a reason for hiding this comment

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

does not work for str as it is defined :param resume_from_checkpoint: str or os.PathLike object.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

checkpoint_path is what torch.load expects as input. It can be file-like object or str containing a file name.
https://pytorch.org/docs/stable/torch.html?highlight=torch%20load#torch.load

return did_restore
Copy link
Member

Choose a reason for hiding this comment

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

You can simply return True/False no need for extra variable

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay. I'll change the code accordingly.

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 simplified the code and this function no longer exists


self.restore(checkpoint_path, self.on_gpu)
did_restore = True

return did_restore

# --------------------
# HPC SIGNAL HANDLING
# --------------------
Expand Down