Skip to content

Commit b0ab0bf

Browse files
ethanwharriscarmocca
authored andcommitted
Fix support for symlink save_dir in TensorBoardLogger (#6730)
* Add test for symlink support and initial fix * Respond to comment and add docstring * Update CHANGELOG.md * Simplify * Update pytorch_lightning/utilities/cloud_io.py Co-authored-by: Carlos Mocholí <[email protected]> * Make `LightningLocalFileSystem` protected Co-authored-by: Carlos Mocholí <[email protected]>
1 parent c7422d4 commit b0ab0bf

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
4040

4141
- Fixed resolve a bug with omegaconf and xm.save ([#6741](https://github.com/PyTorchLightning/pytorch-lightning/pull/6741))
4242

43+
- 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))
44+
45+
4346
## [1.2.4] - 2021-03-16
4447

4548
### Changed

pytorch_lightning/utilities/cloud_io.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,29 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import os
1516
import io
1617
from distutils.version import LooseVersion
1718
from pathlib import Path
1819
from typing import IO, Union
1920

2021
import fsspec
22+
from fsspec.implementations.local import LocalFileSystem
23+
2124
import torch
2225

2326

27+
class _LightningLocalFileSystem(LocalFileSystem):
28+
"""Extension of ``fsspec.implementations.local.LocalFileSystem`` where ``LightningLocalFileSystem.isdir`` behaves
29+
the same as ``os.isdir``.
30+
31+
To be removed when https://github.com/intake/filesystem_spec/issues/591 is fixed.
32+
"""
33+
34+
def isdir(self, path: str) -> bool:
35+
return os.path.isdir(path) # follows symlinks
36+
37+
2438
def load(path_or_url: Union[str, IO, Path], map_location=None):
2539
if not isinstance(path_or_url, (str, Path)):
2640
# any sort of BytesIO or similiar
@@ -39,7 +53,7 @@ def get_filesystem(path: Union[str, Path]):
3953
return fsspec.filesystem(path.split(":", 1)[0])
4054
else:
4155
# use local filesystem
42-
return fsspec.filesystem("file")
56+
return _LightningLocalFileSystem()
4357

4458

4559
def atomic_save(checkpoint, filepath: str):

tests/loggers/test_tensorboard.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,3 +303,22 @@ def test_tensorboard_save_hparams_to_yaml_once(tmpdir):
303303
hparams_file = "hparams.yaml"
304304
assert os.path.isfile(os.path.join(trainer.log_dir, hparams_file))
305305
assert not os.path.isfile(os.path.join(tmpdir, hparams_file))
306+
307+
308+
@mock.patch('pytorch_lightning.loggers.tensorboard.log')
309+
def test_tensorboard_with_symlink(log, tmpdir):
310+
"""
311+
Tests a specific failure case when tensorboard logger is used with empty name, symbolic link ``save_dir``, and
312+
relative paths.
313+
"""
314+
os.chdir(tmpdir) # need to use relative paths
315+
source = os.path.join('.', 'lightning_logs')
316+
dest = os.path.join('.', 'sym_lightning_logs')
317+
318+
os.makedirs(source, exist_ok=True)
319+
os.symlink(source, dest)
320+
321+
logger = TensorBoardLogger(save_dir=dest, name='')
322+
_ = logger.version
323+
324+
log.warning.assert_not_called()

0 commit comments

Comments
 (0)