-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Logger support in Lite #16121
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
Merged
awaelchli
merged 21 commits into
Lightning-AI:master
from
lightningforever:lite/debug-logger
Jan 9, 2023
Merged
Logger support in Lite #16121
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
28c4445
rebased changes
lightningforever a9bedf8
fix
lightningforever 055a546
fix
lightningforever b81ee71
using inheritance to share code it's so wrong but we do it anyway not…
lightningforever 5df141c
more
lightningforever 52aeaa7
more nonsense
lightningforever 0715b5d
docs
lightningforever 364e840
typing
lightningforever 4a22511
fix log_graph
lightningforever e2f079b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 789a28a
reviewer comments
lightningforever fee648b
pseudo code
lightningforever 8a743d3
Fix logger typing by using fabric's base logger
carmocca b844809
resolve rebase conflicts
lightningforever 5b28859
simplify Union[Logger]
lightningforever 50a4031
trigger CI
lightningforever 08f22a0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 0d0c864
Bad change
carmocca 97d2740
Missed this too
carmocca 605a5e2
Unnecessary quotes
carmocca c88b945
Avoid jsonargparse breaking change
carmocca File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -4,3 +4,4 @@ pytest==7.2.0 | |
pytest-cov==4.0.0 | ||
pre-commit==2.20.0 | ||
click==8.1.3 | ||
tensorboard>=2.9.1, <2.12.0 |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# 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. | ||
from lightning_fabric.loggers.logger import Logger # noqa: F401 | ||
from lightning_fabric.loggers.tensorboard import TensorBoardLogger # noqa: F401 |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,136 @@ | ||
# 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. | ||
"""Abstract base class used to build new loggers.""" | ||
|
||
from abc import ABC, abstractmethod | ||
from argparse import Namespace | ||
from functools import wraps | ||
from typing import Any, Callable, Dict, Optional, Union | ||
|
||
from torch import Tensor | ||
from torch.nn import Module | ||
|
||
from lightning_fabric.utilities.rank_zero import rank_zero_only | ||
|
||
|
||
class Logger(ABC): | ||
lightningforever marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""Base class for experiment loggers.""" | ||
|
||
@property | ||
@abstractmethod | ||
def name(self) -> Optional[str]: | ||
"""Return the experiment name.""" | ||
|
||
@property | ||
@abstractmethod | ||
def version(self) -> Optional[Union[int, str]]: | ||
"""Return the experiment version.""" | ||
|
||
@property | ||
def root_dir(self) -> Optional[str]: | ||
"""Return the root directory where all versions of an experiment get saved, or `None` if the logger does | ||
not save data locally.""" | ||
return None | ||
|
||
@property | ||
def log_dir(self) -> Optional[str]: | ||
"""Return directory the current version of the experiment gets saved, or `None` if the logger does not save | ||
data locally.""" | ||
return None | ||
|
||
@property | ||
def group_separator(self) -> str: | ||
"""Return the default separator used by the logger to group the data into subfolders.""" | ||
return "/" | ||
|
||
@abstractmethod | ||
def log_metrics(self, metrics: Dict[str, float], step: Optional[int] = None) -> None: | ||
"""Records metrics. This method logs metrics as soon as it received them. | ||
|
||
Args: | ||
metrics: Dictionary with metric names as keys and measured quantities as values | ||
step: Step number at which the metrics should be recorded | ||
""" | ||
pass | ||
lightningforever marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
@abstractmethod | ||
def log_hyperparams(self, params: Union[Dict[str, Any], Namespace], *args: Any, **kwargs: Any) -> None: | ||
"""Record hyperparameters. | ||
|
||
Args: | ||
params: :class:`~argparse.Namespace` or `Dict` containing the hyperparameters | ||
args: Optional positional arguments, depends on the specific logger being used | ||
kwargs: Optional keyword arguments, depends on the specific logger being used | ||
""" | ||
|
||
def log_graph(self, model: Module, input_array: Optional[Tensor] = None) -> None: | ||
"""Record model graph. | ||
|
||
Args: | ||
model: the model with an implementation of ``forward``. | ||
input_array: input passes to `model.forward` | ||
""" | ||
pass | ||
|
||
def save(self) -> None: | ||
"""Save log data.""" | ||
|
||
def finalize(self, status: str) -> None: | ||
"""Do any processing that is necessary to finalize an experiment. | ||
|
||
Args: | ||
status: Status that the experiment finished with (e.g. success, failed, aborted) | ||
""" | ||
self.save() | ||
|
||
|
||
def rank_zero_experiment(fn: Callable) -> Callable: | ||
"""Returns the real experiment on rank 0 and otherwise the _DummyExperiment.""" | ||
|
||
@wraps(fn) | ||
def experiment(self) -> Union[Any, _DummyExperiment]: # type: ignore[no-untyped-def] | ||
""" | ||
Note: | ||
``self`` is a custom logger instance. The loggers typically wrap an ``experiment`` method | ||
with a ``@rank_zero_experiment`` decorator. | ||
|
||
``Union[Any, _DummyExperiment]`` is used because the wrapped hooks have several return | ||
types that are specific to the custom logger. The return type here can be considered as | ||
``Union[return type of logger.experiment, _DummyExperiment]``. | ||
""" | ||
|
||
@rank_zero_only | ||
def get_experiment() -> Callable: | ||
return fn(self) | ||
|
||
return get_experiment() or _DummyExperiment() | ||
|
||
return experiment | ||
|
||
|
||
class _DummyExperiment: | ||
"""Dummy experiment.""" | ||
|
||
def nop(self, *args: Any, **kw: Any) -> None: | ||
pass | ||
|
||
def __getattr__(self, _: Any) -> Callable: | ||
return self.nop | ||
|
||
def __getitem__(self, idx: int) -> "_DummyExperiment": | ||
# enables self.logger.experiment[0].add_image(...) | ||
return self | ||
|
||
def __setitem__(self, *args: Any, **kwargs: Any) -> None: | ||
pass |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.