-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use tensorboard.compat.gfile to support remote writing
- Loading branch information
Showing
9 changed files
with
552 additions
and
321 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 |
---|---|---|
@@ -1,11 +1,56 @@ | ||
import torch | ||
|
||
import sys | ||
import os | ||
from typing import Union | ||
from pathlib import Path | ||
from urllib.parse import urlparse | ||
import torch | ||
|
||
import tensorboard | ||
from packaging import version | ||
from pytorch_lightning import _logger as log | ||
|
||
# we want this for tf.io.gfile, which if tf is installed gives full tf, | ||
# otherwise gives a pruned down version which works for some file backends but | ||
# not all | ||
from tensorboard.compat import tf | ||
|
||
gfile = tf.io.gfile | ||
|
||
pathlike = Union[Path, str] | ||
|
||
# older version of tensorboard had buggy gfile compatibility layers | ||
# only support remote cloud paths if newer | ||
modern_gfile = version.parse(tensorboard.version.VERSION) >= version.parse('2.0') | ||
|
||
|
||
def load(path_or_url: str, map_location=None): | ||
if urlparse(path_or_url).scheme == '' or Path(path_or_url).drive: # no scheme or with a drive letter | ||
return torch.load(path_or_url, map_location=map_location) | ||
else: | ||
return torch.hub.load_state_dict_from_url(path_or_url, map_location=map_location) | ||
return torch.hub.load_state_dict_from_url(path_or_url, map_location=map_location) | ||
|
||
|
||
def cloud_open(path: pathlike, mode: str, newline:str = None): | ||
if not modern_gfile or sys.platform == "win32": | ||
log.debug( | ||
"tenosrboard.compat gfile does not work on older versions " | ||
"of tensorboard normal local file open." | ||
) | ||
return open(path, mode, newline=newline) | ||
if sys.platform == "win32": | ||
log.debug( | ||
"gfile does not handle newlines correctly on windows so remote files are not" | ||
"supported falling back to normal local file open." | ||
) | ||
return open(path, mode, newline=newline) | ||
try: | ||
return gfile.GFile(path, mode) | ||
except NotImplementedError as e: | ||
# minimal dependencies are installed and only local files will work | ||
return open(path, mode) | ||
|
||
|
||
def makedirs(path: pathlike): | ||
if modern_gfile and hasattr(gfile, "makedirs"): | ||
return gfile.makedirs(str(path)) | ||
# otherwise minimal dependencies are installed and only local files will work | ||
return os.makedirs(path, exist_ok=True) |
This file contains 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 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 |
---|---|---|
|
@@ -12,4 +12,7 @@ black==19.10b0 | |
pre-commit>=1.0 | ||
|
||
cloudpickle>=1.2 | ||
|
||
boto3 | ||
moto>=1.3.14 | ||
nltk>=3.3 |
Oops, something went wrong.