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

Fix support for symlink save_dir in TensorBoardLogger #6730

Merged
merged 6 commits into from
Apr 6, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Fixed a bug where gradients were disabled after calling `Trainer.predict` ([#6657](https://github.com/PyTorchLightning/pytorch-lightning/pull/6657))


- Fixed a bug where `TensorBoardLogger` would give a warning and not log correctly to a symbolic link `save_dir` ([#6730](https://github.com/PyTorchLightning/pytorch-lightning/pull/6730))


## [1.2.4] - 2021-03-16

### Changed
Expand Down
16 changes: 15 additions & 1 deletion pytorch_lightning/utilities/cloud_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,29 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import io
from distutils.version import LooseVersion
from pathlib import Path
from typing import IO, Union

import fsspec
from fsspec.implementations.local import LocalFileSystem

import torch


class _LightningLocalFileSystem(LocalFileSystem):
"""Extension of ``fsspec.implementations.local.LocalFileSystem`` where ``LightningLocalFileSystem.isdir`` behaves
the same as ``os.isdir``.

To be removed when https://github.com/intake/filesystem_spec/issues/591 is fixed.
"""
ethanwharris marked this conversation as resolved.
Show resolved Hide resolved

def isdir(self, path: str) -> bool:
carmocca marked this conversation as resolved.
Show resolved Hide resolved
return os.path.isdir(path) # follows symlinks

ethanwharris marked this conversation as resolved.
Show resolved Hide resolved

def load(path_or_url: Union[str, IO, Path], map_location=None):
if not isinstance(path_or_url, (str, Path)):
# any sort of BytesIO or similiar
Expand All @@ -39,7 +53,7 @@ def get_filesystem(path: Union[str, Path]):
return fsspec.filesystem(path.split(":", 1)[0])
else:
# use local filesystem
return fsspec.filesystem("file")
return _LightningLocalFileSystem()


def atomic_save(checkpoint, filepath: str):
Expand Down
19 changes: 19 additions & 0 deletions tests/loggers/test_tensorboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,22 @@ def test_tensorboard_save_hparams_to_yaml_once(tmpdir):
hparams_file = "hparams.yaml"
assert os.path.isfile(os.path.join(trainer.log_dir, hparams_file))
assert not os.path.isfile(os.path.join(tmpdir, hparams_file))


@mock.patch('pytorch_lightning.loggers.tensorboard.log')
def test_tensorboard_with_symlink(log, tmpdir):
"""
Tests a specific failure case when tensorboard logger is used with empty name, symbolic link ``save_dir``, and
relative paths.
"""
awaelchli marked this conversation as resolved.
Show resolved Hide resolved
os.chdir(tmpdir) # need to use relative paths
source = os.path.join('.', 'lightning_logs')
dest = os.path.join('.', 'sym_lightning_logs')

os.makedirs(source, exist_ok=True)
os.symlink(source, dest)

logger = TensorBoardLogger(save_dir=dest, name='')
_ = logger.version

log.warning.assert_not_called()