-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- v0.4.0
- v0.3.28
- v0.3.27
- v0.3.26
- v0.3.26-alpha.7
- v0.3.25
- v0.3.23
- v0.3.22
- v0.3.21
- v0.3.21-alpha
- v0.3.20
- v0.3.20-alpha.1
- v0.3.20-alpha
- v0.3.19
- v0.3.19-alpha.3
- v0.3.19-alpha.2
- v0.3.19-alpha.1
- v0.3.19-alpha
- v0.3.18
- v0.3.17
- v0.3.16
- v0.3.15-beta.1
- v0.3.15-beta
- v0.3.15-alpha.4
- v0.3.15-alpha.3
- v0.3.15-alpha.2
- v0.3.15-alpha.1
Showing
1 changed file
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,88 @@ | ||
from torchtune.utils._distributed import get_world_size_and_rank | ||
from torchtune.utils.metric_logging import MetricLoggerInterface | ||
from typing import Mapping, Optional, Union | ||
|
||
from numpy import ndarray | ||
from omegaconf import DictConfig, OmegaConf | ||
from torch import Tensor | ||
|
||
Scalar = Union[Tensor, ndarray, int, float] | ||
|
||
|
||
class SwanLabLogger(MetricLoggerInterface): | ||
""" | ||
Example: | ||
In torchtune config: | ||
>>> # Logging | ||
>>> metric_logger: | ||
>>> _component_: swanlab.integration.huggingface.SwanLabLogger | ||
>>> project: "gemma-lora-finetune" | ||
>>> experiment_name: "gemma-2b" | ||
>>> log_dir: ${output_dir} | ||
Raises: | ||
ImportError: If ``swanlab`` package is not installed. | ||
Note: | ||
This logger requires the swanlab package to be installed. | ||
You can install it with `pip install swanlab`. | ||
In order to use the logger, you need to login to your SwanLab account. | ||
You can do this by running `swanlab login` in your terminal. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
project: str = "torchtune", | ||
workspace: Optional[str] = None, | ||
experiment_name: Optional[str] = None, | ||
description: Optional[str] = None, | ||
mode: Optional[str] = None, | ||
log_dir: Optional[str] = None, | ||
**kwargs, | ||
): | ||
try: | ||
import swanlab | ||
except ImportError as e: | ||
raise ImportError( | ||
"``swanlab`` package not found. Please install swanlab using `pip install swanlab` to use SwanLabLogger." | ||
) from e | ||
self._swanlab = swanlab | ||
|
||
# Use dir if specified, otherwise use log_dir. | ||
self.log_dir = kwargs.pop("dir", log_dir) | ||
|
||
_, self.rank = get_world_size_and_rank() | ||
|
||
if self._swanlab.get_run() is None and self.rank == 0: | ||
run = self._swanlab.init( | ||
project=project, | ||
workspace=workspace, | ||
name=experiment_name, | ||
description=description, | ||
mode=mode, | ||
logdir=self.log_dir, | ||
**kwargs, | ||
) | ||
|
||
self.config_allow_val_change = kwargs.get("allow_val_change", False) | ||
|
||
def log_config(self, config: DictConfig) -> None: | ||
if self._swanlab.get_run(): | ||
resolved = OmegaConf.to_container(config, resolve=True) | ||
self._swanlab.config.update(resolved, allow_val_change=self.config_allow_val_change) | ||
|
||
def log(self, name: str, data: Scalar, step: int) -> None: | ||
if self._swanlab.get_run(): | ||
self._swanlab.log({name: data}, step=step) | ||
|
||
def log_dict(self, payload: Mapping[str, Scalar], step: int) -> None: | ||
if self._swanlab.get_run(): | ||
self._swanlab.log({**payload}, step=step) | ||
|
||
def __del__(self) -> None: | ||
if self._swanlab.get_run(): | ||
self._swanlab.finish() | ||
|
||
def close(self) -> None: | ||
if self._swanlab.get_run(): | ||
self._swanlab.finish() |