Skip to content

Commit 703cb03

Browse files
kunalb-metafacebook-github-bot
authored andcommitted
Lazy initialize tensorboard
Summary: Every time a summary writer object is created it'll end up creating a file in the passed in logdir with just metadata of 40 bytes. This will cause a lot of spam with every rank initializing the file aggressively (I've done this myself when adding support for Adhoc logging to PyPer to a lot of angry complaints). Report at https://fb.workplace.com/groups/723537759122220/posts/745046770304652/ Differential Revision: D43863071 fbshipit-source-id: b5b8cab7dcbf09e50e35cc3e05f9ef91f9105a7e
1 parent 5fcee61 commit 703cb03

2 files changed

Lines changed: 31 additions & 3 deletions

File tree

detectron2/utils/events.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import time
77
from collections import defaultdict
88
from contextlib import contextmanager
9+
from functools import cached_property
910
from typing import Optional
1011
import torch
1112
from fvcore.common.history_buffer import HistoryBuffer
@@ -142,10 +143,14 @@ def __init__(self, log_dir: str, window_size: int = 20, **kwargs):
142143
kwargs: other arguments passed to `torch.utils.tensorboard.SummaryWriter(...)`
143144
"""
144145
self._window_size = window_size
146+
self._writer_args = {"log_dir": log_dir, **kwargs}
147+
self._last_write = -1
148+
149+
@cached_property
150+
def _writer(self):
145151
from torch.utils.tensorboard import SummaryWriter
146152

147-
self._writer = SummaryWriter(log_dir, **kwargs)
148-
self._last_write = -1
153+
return SummaryWriter(**self._writer_args)
149154

150155
def write(self):
151156
storage = get_event_storage()
@@ -174,7 +179,7 @@ def write(self):
174179
storage.clear_histograms()
175180

176181
def close(self):
177-
if hasattr(self, "_writer"): # doesn't exist when the code fails at import
182+
if "_writer" in self.__dict__:
178183
self._writer.close()
179184

180185

tests/utils/test_tensorboardx.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import os
2+
import tempfile
3+
import unittest
4+
5+
from detectron2.utils.events import TensorboardXWriter
6+
7+
8+
# TODO Fix up capitalization
9+
class TestTensorboardXWriter(unittest.TestCase):
10+
def test_no_files_created(self) -> None:
11+
with tempfile.TemporaryDirectory() as dir:
12+
writer = TensorboardXWriter(dir)
13+
writer.close()
14+
15+
self.assertFalse(os.listdir(dir))
16+
17+
def test_single_write(self) -> None:
18+
with tempfile.TemporaryDirectory() as dir:
19+
writer = TensorboardXWriter(dir)
20+
writer._writer.add_scalar("testing", 1, 1)
21+
writer.close()
22+
23+
self.assertTrue(os.listdir(dir))

0 commit comments

Comments
 (0)