-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
Lazy import tensorboard #15762
Lazy import tensorboard #15762
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,12 +19,10 @@ | |
import logging | ||
import os | ||
from argparse import Namespace | ||
from typing import Any, Dict, Mapping, Optional, Union | ||
from typing import Any, Dict, Mapping, Optional, TYPE_CHECKING, Union | ||
|
||
import numpy as np | ||
from lightning_utilities.core.imports import RequirementCache | ||
from tensorboardX import SummaryWriter | ||
from tensorboardX.summary import hparams | ||
from torch import Tensor | ||
|
||
import pytorch_lightning as pl | ||
|
@@ -40,6 +38,13 @@ | |
log = logging.getLogger(__name__) | ||
|
||
_TENSORBOARD_AVAILABLE = RequirementCache("tensorboard") | ||
_TENSORBOARDX_AVAILABLE = RequirementCache("tensorboardX") | ||
if TYPE_CHECKING: | ||
# assumes at least one will be installed when type checking | ||
if _TENSORBOARD_AVAILABLE: | ||
from torch.utils.tensorboard import SummaryWriter | ||
else: | ||
from tensorboardX import SummaryWriter # type: ignore[no-redef] | ||
|
||
if _OMEGACONF_AVAILABLE: | ||
from omegaconf import Container, OmegaConf | ||
|
@@ -109,6 +114,10 @@ def __init__( | |
sub_dir: Optional[_PATH] = None, | ||
**kwargs: Any, | ||
): | ||
if not _TENSORBOARD_AVAILABLE and not _TENSORBOARDX_AVAILABLE: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this won't happen as TBX is mandatory depenency |
||
raise ModuleNotFoundError( | ||
"Neither `tensorboard` nor `tensorboardX` is available. Try `pip install`ing either." | ||
) | ||
super().__init__() | ||
save_dir = os.fspath(save_dir) | ||
self._save_dir = save_dir | ||
|
@@ -172,7 +181,7 @@ def sub_dir(self) -> Optional[str]: | |
|
||
@property | ||
@rank_zero_experiment | ||
def experiment(self) -> SummaryWriter: | ||
def experiment(self) -> "SummaryWriter": | ||
r""" | ||
Actual tensorboard object. To use TensorBoard features in your | ||
:class:`~pytorch_lightning.core.module.LightningModule` do the following. | ||
|
@@ -188,6 +197,12 @@ def experiment(self) -> SummaryWriter: | |
assert rank_zero_only.rank == 0, "tried to init log dirs in non global_rank=0" | ||
if self.root_dir: | ||
self._fs.makedirs(self.root_dir, exist_ok=True) | ||
|
||
if _TENSORBOARD_AVAILABLE: | ||
from torch.utils.tensorboard import SummaryWriter | ||
else: | ||
from tensorboardX import SummaryWriter # type: ignore[no-redef] | ||
carmocca marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
self._experiment = SummaryWriter(log_dir=self.log_dir, **self._kwargs) | ||
return self._experiment | ||
|
||
|
@@ -224,6 +239,12 @@ def log_hyperparams( | |
|
||
if metrics: | ||
self.log_metrics(metrics, 0) | ||
|
||
if _TENSORBOARD_AVAILABLE: | ||
from torch.utils.tensorboard.summary import hparams | ||
else: | ||
from tensorboardX.summary import hparams # type: ignore[no-redef] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how many times is this called per epoch? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was answered in #15762 (comment) |
||
|
||
exp, ssi, sei = hparams(params, metrics) | ||
writer = self.experiment._get_file_writer() | ||
writer.add_summary(exp) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is opposite what @lantiga said in #15728 (comment) 🦦