-
Notifications
You must be signed in to change notification settings - Fork 239
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add TensorBoard logging with loss and wps
ghstack-source-id: cdfe4c2c496feae23399019ec2a63b443fb3b6a9 Pull Request resolved: #57
- Loading branch information
Showing
6 changed files
with
119 additions
and
2 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 |
---|---|---|
|
@@ -2,3 +2,4 @@ torch >= 2.2.0.dev | |
sentencepiece | ||
datasets | ||
tomli >= 1.1.0 ; python_version < "3.11" | ||
tensorboard |
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
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,49 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# This software may be used and distributed according to the terms of the Llama 2 Community License Agreement. | ||
|
||
import os | ||
from datetime import datetime | ||
from typing import Any, Dict, Optional | ||
|
||
import torch | ||
from torch.utils.tensorboard import SummaryWriter | ||
|
||
from torchtrain.logging_utils import rank0_log | ||
from torchtrain.profiling import get_config_from_toml | ||
|
||
|
||
class MetricLogger: | ||
def __init__(self, log_dir, tag, enable_tb): | ||
self.tag = tag | ||
self.writer: Optional[SummaryWriter] = None | ||
if enable_tb: | ||
self.writer = SummaryWriter(log_dir, max_queue=1000) | ||
|
||
def log(self, metrics: Dict[str, Any], step: int): | ||
if self.writer is not None: | ||
for k, v in metrics.items(): | ||
tag = k if self.tag is None else f"{self.tag}/{k}" | ||
self.writer.add_scalar(tag, v, step) | ||
|
||
def close(self): | ||
if self.writer is not None: | ||
self.writer.close() | ||
|
||
|
||
def build_metric_logger(tag: Optional[str] = None): | ||
config = get_config_from_toml() | ||
|
||
dump_dir = config["global"]["dump_folder"] | ||
save_tb_folder = config["metrics"]["save_tb_folder"] | ||
# since we don't have run id yet, use current minute as identifier | ||
datetime_str = datetime.now().strftime("%Y%m%d-%H%M") | ||
log_dir = os.path.join(dump_dir, save_tb_folder, datetime_str) | ||
|
||
enable_tb = config["metrics"].get("enable_tensorboard", False) | ||
if enable_tb: | ||
rank0_log( | ||
f"Metrics logging active. Tensorboard logs will be saved at {log_dir}." | ||
) | ||
|
||
rank_str = f"rank_{torch.distributed.get_rank()}" | ||
return MetricLogger(os.path.join(log_dir, rank_str), tag, enable_tb) |
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
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,19 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# This software may be used and distributed according to the terms of the Llama 2 Community License Agreement. | ||
|
||
from typing import Union | ||
|
||
import torch | ||
import torch.distributed._functional_collectives as funcol | ||
import torch.distributed.distributed_c10d as c10d | ||
from torch.distributed.device_mesh import DeviceMesh | ||
|
||
|
||
def dist_max(x: Union[int, float], mesh: DeviceMesh) -> float: | ||
tensor = torch.tensor(x).cuda() | ||
return funcol.all_reduce(tensor, reduceOp=c10d.ReduceOp.MAX.name, group=mesh) | ||
|
||
|
||
def dist_mean(x: Union[int, float], mesh: DeviceMesh) -> float: | ||
tensor = torch.tensor(x).cuda() | ||
return funcol.all_reduce(tensor, reduceOp=c10d.ReduceOp.AVG.name, group=mesh) |
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