mirror of
https://github.com/pytorch/pytorch.git
synced 2025-11-11 22:34:53 +08:00
torch/monitor: add pybind (#69567)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/69567 This exposes torch.monitor events and stats via pybind11 to the underlying C++ implementation. * The registration interface is a tad different since it takes a lambda function in Python where as in C++ it's a full class. * This has a small amount of changes to the counter interfaces since there's no way to create an initializer list at runtime so they now also take a vector. * Only double based stats are provided in Python since it's intended more for high level stats where float imprecision shouldn't be an issue. This can be changed down the line if need arises. ``` events = [] def handler(event): events.append(event) handle = register_event_handler(handler) log_event(Event(type="torch.monitor.TestEvent", timestamp=datetime.now(), metadata={"foo": 1.0})) ``` D32969391 is now included in this diff. This cleans up the naming for events. type is now name, message is gone, and metadata is renamed data. Test Plan: buck test //caffe2/test:monitor //caffe2/test/cpp/monitor:monitor Reviewed By: kiukchung Differential Revision: D32924141 fbshipit-source-id: 563304c2e3261a4754e40cca39fc64c5a04b43e8
This commit is contained in:
committed by
Facebook GitHub Bot
parent
90ef54f8ea
commit
bfe1abd3b5
92
test/test_monitor.py
Normal file
92
test/test_monitor.py
Normal file
@ -0,0 +1,92 @@
|
||||
from torch.testing._internal.common_utils import (
|
||||
TestCase, run_tests,
|
||||
)
|
||||
|
||||
from datetime import timedelta, datetime
|
||||
import time
|
||||
|
||||
from torch.monitor import (
|
||||
Aggregation,
|
||||
FixedCountStat,
|
||||
IntervalStat,
|
||||
Event,
|
||||
log_event,
|
||||
register_event_handler,
|
||||
unregister_event_handler,
|
||||
)
|
||||
|
||||
class TestMonitor(TestCase):
|
||||
def test_interval_stat(self) -> None:
|
||||
events = []
|
||||
|
||||
def handler(event):
|
||||
events.append(event)
|
||||
|
||||
handle = register_event_handler(handler)
|
||||
s = IntervalStat(
|
||||
"asdf",
|
||||
(Aggregation.SUM, Aggregation.COUNT),
|
||||
timedelta(milliseconds=1),
|
||||
)
|
||||
s.add(2)
|
||||
time.sleep(0.002)
|
||||
s.add(3)
|
||||
self.assertEqual(s.name, "asdf")
|
||||
self.assertGreaterEqual(len(events), 1)
|
||||
unregister_event_handler(handle)
|
||||
|
||||
def test_fixed_count_stat(self) -> None:
|
||||
s = FixedCountStat(
|
||||
"asdf",
|
||||
(Aggregation.SUM, Aggregation.COUNT),
|
||||
3,
|
||||
)
|
||||
s.add(1)
|
||||
s.add(2)
|
||||
name = s.name
|
||||
self.assertEqual(name, "asdf")
|
||||
self.assertEqual(s.count, 2)
|
||||
s.add(3)
|
||||
self.assertEqual(s.count, 0)
|
||||
self.assertEqual(s.get(), {Aggregation.SUM: 6.0, Aggregation.COUNT: 3})
|
||||
|
||||
def test_log_event(self) -> None:
|
||||
e = Event(
|
||||
name="torch.monitor.TestEvent",
|
||||
timestamp=datetime.now(),
|
||||
data={
|
||||
"str": "a string",
|
||||
"float": 1234.0,
|
||||
"int": 1234,
|
||||
},
|
||||
)
|
||||
self.assertEqual(e.name, "torch.monitor.TestEvent")
|
||||
self.assertIsNotNone(e.timestamp)
|
||||
self.assertIsNotNone(e.data)
|
||||
log_event(e)
|
||||
|
||||
def test_event_handler(self) -> None:
|
||||
events = []
|
||||
|
||||
def handler(event: Event) -> None:
|
||||
events.append(event)
|
||||
|
||||
handle = register_event_handler(handler)
|
||||
e = Event(
|
||||
name="torch.monitor.TestEvent",
|
||||
timestamp=datetime.now(),
|
||||
data={},
|
||||
)
|
||||
log_event(e)
|
||||
self.assertEqual(len(events), 1)
|
||||
self.assertEqual(events[0], e)
|
||||
log_event(e)
|
||||
self.assertEqual(len(events), 2)
|
||||
|
||||
unregister_event_handler(handle)
|
||||
log_event(e)
|
||||
self.assertEqual(len(events), 2)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
run_tests()
|
||||
Reference in New Issue
Block a user