mirror of
https://github.com/wassname/pytorch-lightning.git
synced 2026-09-21 13:20:08 +08:00
Added atomic checkpoint creation (#689)
* Added atomic checkpoint creation * Added documentation for _atomic_checkpoint
This commit is contained in:
committed by
William Falcon
parent
06242c200a
commit
9aad69d856
@@ -257,17 +257,34 @@ class TrainerIOMixin(ABC):
|
||||
# --------------------
|
||||
# MODEL SAVE CHECKPOINT
|
||||
# --------------------
|
||||
def _atomic_save(self, checkpoint, filepath):
|
||||
"""Saves a checkpoint atomically, avoiding the creation of incomplete checkpoints.
|
||||
|
||||
This will create a temporary checkpoint with a suffix of ``.part``, then copy it to the final location once
|
||||
saving is finished.
|
||||
|
||||
Args:
|
||||
checkpoint (object): The object to save.
|
||||
Built to be used with the ``dump_checkpoint`` method, but can deal with anything which ``torch.save``
|
||||
accepts.
|
||||
filepath (str|pathlib.Path): The path to which the checkpoint will be saved.
|
||||
This points to the file that the checkpoint will be stored in.
|
||||
"""
|
||||
tmp_path = str(filepath) + ".part"
|
||||
torch.save(checkpoint, tmp_path)
|
||||
os.replace(tmp_path, filepath)
|
||||
|
||||
def save_checkpoint(self, filepath):
|
||||
checkpoint = self.dump_checkpoint()
|
||||
|
||||
# do the actual save
|
||||
try:
|
||||
torch.save(checkpoint, filepath)
|
||||
self._atomic_save(checkpoint, filepath)
|
||||
except AttributeError:
|
||||
if 'hparams' in checkpoint:
|
||||
del checkpoint['hparams']
|
||||
|
||||
torch.save(checkpoint, filepath)
|
||||
self._atomic_save(checkpoint, filepath)
|
||||
|
||||
def restore(self, checkpoint_path, on_gpu):
|
||||
|
||||
@@ -415,12 +432,12 @@ class TrainerIOMixin(ABC):
|
||||
# do the actual save
|
||||
# TODO: fix for anything with multiprocess DP, DDP, DDP2
|
||||
try:
|
||||
torch.save(checkpoint, filepath)
|
||||
self._atomic_save(checkpoint, filepath)
|
||||
except AttributeError:
|
||||
if 'hparams' in checkpoint:
|
||||
del checkpoint['hparams']
|
||||
|
||||
torch.save(checkpoint, filepath)
|
||||
self._atomic_save(checkpoint, filepath)
|
||||
|
||||
return filepath
|
||||
|
||||
|
||||
Reference in New Issue
Block a user