Rank zero logging (#124)
* add rank zero logger * prevent multiple printing of config in multi-gpu setup * fix formatting
This commit is contained in:
1 parent
e2c8ed4bf8
commit
296c5bf5ed
2 files changed
+23
-23
No files matched your search
+1
-2
@@ -1,4 +1,3 @@
|
||||
import logging
|
||||
from typing import List, Optional
|
||||
|
||||
from pytorch_lightning import LightningModule, LightningDataModule, Callback, Trainer
|
||||
@@ -10,7 +9,7 @@ from omegaconf import DictConfig
|
||||
|
||||
from src.utils import template_utils
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
log = template_utils.get_logger(__name__)
|
||||
|
||||
|
||||
def train(config: DictConfig) -> Optional[float]:
|
||||
|
||||
+22
-21
@@ -3,14 +3,30 @@ import warnings
|
||||
from typing import List, Sequence
|
||||
|
||||
import pytorch_lightning as pl
|
||||
import rich
|
||||
import wandb
|
||||
from omegaconf import DictConfig, OmegaConf
|
||||
from pytorch_lightning.loggers.wandb import WandbLogger
|
||||
from rich import print
|
||||
from pytorch_lightning.utilities import rank_zero_only
|
||||
from rich.syntax import Syntax
|
||||
from rich.tree import Tree
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
def get_logger(name=__name__, level=logging.INFO):
|
||||
"""Initializes python logger."""
|
||||
|
||||
logger = logging.getLogger(name)
|
||||
logger.setLevel(level)
|
||||
|
||||
# this ensures all logging levels get marked with the rank zero decorator
|
||||
# otherwise logs would get multiplied for each GPU process in multi-GPU setup
|
||||
for level in ("debug", "info", "warning", "error", "exception", "fatal", "critical"):
|
||||
setattr(logger, level, rank_zero_only(getattr(logger, level)))
|
||||
|
||||
return logger
|
||||
|
||||
|
||||
log = get_logger()
|
||||
|
||||
|
||||
def extras(config: DictConfig) -> None:
|
||||
@@ -58,6 +74,7 @@ def extras(config: DictConfig) -> None:
|
||||
OmegaConf.set_struct(config, True)
|
||||
|
||||
|
||||
@rank_zero_only
|
||||
def print_config(
|
||||
config: DictConfig,
|
||||
fields: Sequence[str] = (
|
||||
@@ -92,13 +109,14 @@ def print_config(
|
||||
|
||||
branch.add(Syntax(branch_content, "yaml"))
|
||||
|
||||
print(tree)
|
||||
rich.print(tree)
|
||||
|
||||
|
||||
def empty(*args, **kwargs):
|
||||
pass
|
||||
|
||||
|
||||
@rank_zero_only
|
||||
def log_hyperparameters(
|
||||
config: DictConfig,
|
||||
model: pl.LightningModule,
|
||||
@@ -112,14 +130,6 @@ def log_hyperparameters(
|
||||
Additionaly saves:
|
||||
- sizes of train, val, test dataset
|
||||
- number of trainable model parameters
|
||||
|
||||
Args:
|
||||
config (DictConfig): [description]
|
||||
model (pl.LightningModule): [description]
|
||||
datamodule (pl.LightningDataModule): [description]
|
||||
trainer (pl.Trainer): [description]
|
||||
callbacks (List[pl.Callback]): [description]
|
||||
logger (List[pl.loggers.LightningLoggerBase]): [description]
|
||||
"""
|
||||
|
||||
hparams = {}
|
||||
@@ -168,16 +178,7 @@ def finish(
|
||||
callbacks: List[pl.Callback],
|
||||
logger: List[pl.loggers.LightningLoggerBase],
|
||||
) -> None:
|
||||
"""Makes sure everything closed properly.
|
||||
|
||||
Args:
|
||||
config (DictConfig): [description]
|
||||
model (pl.LightningModule): [description]
|
||||
datamodule (pl.LightningDataModule): [description]
|
||||
trainer (pl.Trainer): [description]
|
||||
callbacks (List[pl.Callback]): [description]
|
||||
logger (List[pl.loggers.LightningLoggerBase]): [description]
|
||||
"""
|
||||
"""Makes sure everything closed properly."""
|
||||
|
||||
# without this sweeps with wandb logger might crash!
|
||||
for lg in logger:
|
||||
|
||||
Reference in new issue
Block a user