2020-08-20 02:03:22 +00:00
|
|
|
# Copyright The PyTorch Lightning team.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2020-08-13 11:26:33 +00:00
|
|
|
import io
|
|
|
|
from distutils.version import LooseVersion
|
2020-08-09 10:08:44 +00:00
|
|
|
from typing import Union
|
2020-06-18 21:53:51 +00:00
|
|
|
from pathlib import Path
|
2020-06-11 21:12:48 +00:00
|
|
|
from urllib.parse import urlparse
|
2020-08-07 22:33:51 +00:00
|
|
|
import torch
|
2020-09-03 12:19:20 +00:00
|
|
|
import fsspec
|
2020-08-07 22:33:51 +00:00
|
|
|
|
2020-08-09 10:08:44 +00:00
|
|
|
|
|
|
|
pathlike = Union[Path, str]
|
|
|
|
|
2020-06-11 21:12:48 +00:00
|
|
|
|
|
|
|
def load(path_or_url: str, map_location=None):
|
2020-09-03 12:19:20 +00:00
|
|
|
if urlparse(path_or_url).scheme == "" or Path(path_or_url).drive: # no scheme or with a drive letter
|
2020-06-11 21:12:48 +00:00
|
|
|
return torch.load(path_or_url, map_location=map_location)
|
2020-08-09 10:08:44 +00:00
|
|
|
return torch.hub.load_state_dict_from_url(path_or_url, map_location=map_location)
|
|
|
|
|
|
|
|
|
2020-09-03 12:19:20 +00:00
|
|
|
def get_filesystem(path: pathlike):
|
|
|
|
path = str(path)
|
|
|
|
if "://" in path:
|
|
|
|
# use the fileystem from the protocol specified
|
|
|
|
return fsspec.filesystem(path.split(":", 1)[0])
|
|
|
|
else:
|
|
|
|
# use local filesystem
|
|
|
|
return fsspec.filesystem("file")
|
2020-08-13 11:26:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
def atomic_save(checkpoint, filepath: str):
|
|
|
|
"""Saves a checkpoint atomically, avoiding the creation of incomplete checkpoints.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
checkpoint: The object to save.
|
|
|
|
Built to be used with the ``dump_checkpoint`` method, but can deal with anything which ``torch.save``
|
|
|
|
accepts.
|
|
|
|
filepath: The path to which the checkpoint will be saved.
|
|
|
|
This points to the file that the checkpoint will be stored in.
|
|
|
|
"""
|
|
|
|
bytesbuffer = io.BytesIO()
|
|
|
|
# Can't use the new zipfile serialization for 1.6.0 because there's a bug in
|
|
|
|
# torch.hub.load_state_dict_from_url() that prevents it from loading the new files.
|
|
|
|
# More details can be found here: https://github.com/pytorch/pytorch/issues/42239
|
|
|
|
if LooseVersion(torch.__version__).version[:3] == [1, 6, 0]:
|
|
|
|
torch.save(checkpoint, bytesbuffer, _use_new_zipfile_serialization=False)
|
|
|
|
else:
|
|
|
|
torch.save(checkpoint, bytesbuffer)
|
2020-09-03 12:19:20 +00:00
|
|
|
with fsspec.open(filepath, "wb") as f:
|
2020-08-13 11:26:33 +00:00
|
|
|
f.write(bytesbuffer.getvalue())
|