mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-07 12:21:27 +01:00
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/71658
This adds the beginnings of a TensorboardEventHandler which will log stats to Tensorboard.
Test Plan: buck test //caffe2/test:monitor
Reviewed By: edward-io
Differential Revision: D33719954
fbshipit-source-id: e9847c1319255ce0d9cf2d85d8b54b7a3c681bd2
(cherry picked from commit 5c8520a6ba)
34 lines
1.0 KiB
Python
34 lines
1.0 KiB
Python
from torch._C._monitor import * # noqa: F403
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
if TYPE_CHECKING:
|
|
from torch.utils.tensorboard import SummaryWriter
|
|
|
|
|
|
STAT_EVENT = "torch.monitor.Stat"
|
|
|
|
class TensorboardEventHandler:
|
|
"""
|
|
TensorboardEventHandler is an event handler that will write known events to
|
|
the provided SummaryWriter.
|
|
|
|
This currently only supports ``torch.monitor.Stat`` events which are logged
|
|
as scalars.
|
|
|
|
>>> from torch.utils.tensorboard import SummaryWriter
|
|
>>> from torch.monitor import TensorboardEventHandler, register_event_handler
|
|
>>> writer = SummaryWriter("log_dir")
|
|
>>> register_event_handler(TensorboardEventHandler(writer))
|
|
"""
|
|
def __init__(self, writer: "SummaryWriter") -> None:
|
|
"""
|
|
Constructs the ``TensorboardEventHandler``.
|
|
"""
|
|
self._writer = writer
|
|
|
|
def __call__(self, event: Event) -> None:
|
|
if event.name == STAT_EVENT:
|
|
for k, v in event.data.items():
|
|
self._writer.add_scalar(k, v, walltime=event.timestamp.timestamp())
|