Skip to content

torchx/runner: log events to torch.monitor #379

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .pyre_configuration
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
".*/IPython/core/tests/nonascii.*"
],
"source_directories": [
"typestubs",
"."
],
"strict": true,
Expand Down
9 changes: 9 additions & 0 deletions torchx/runner/events/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ def _get_or_create_logger(destination: str = "null") -> logging.Logger:
def record(event: TorchxEvent, destination: str = "null") -> None:
_get_or_create_logger(destination).info(event.serialize())

if destination != "console":

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it make sense to have a console event handler, so we don't need this check?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not actually sure we use console anywhere, this is mostly just to be safe. Easy enough to remove it if people need it.

# if using torch>1.11 log the event to torch.monitor
try:
from torch import monitor

monitor.log_event(event.to_monitor_event())
except ImportError:
pass


class log_event:
"""
Expand Down
15 changes: 14 additions & 1 deletion torchx/runner/events/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@

import json
from dataclasses import asdict, dataclass
from datetime import datetime
from enum import Enum
from typing import Optional, Union
from typing import Optional, Union, TYPE_CHECKING

if TYPE_CHECKING:
from torch import monitor


class SourceType(str, Enum):
Expand Down Expand Up @@ -60,3 +64,12 @@ def deserialize(data: Union[str, "TorchxEvent"]) -> "TorchxEvent":

def serialize(self) -> str:
return json.dumps(asdict(self))

def to_monitor_event(self) -> "monitor.Event":
from torch import monitor

return monitor.Event(
name="torch.runner.Event",
timestamp=datetime.now(),
data={k: v for k, v in self.__dict__.items() if v is not None},
)
54 changes: 54 additions & 0 deletions torchx/runner/events/test/lib_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,24 @@
import json
import logging
import unittest
from typing import List
from unittest.mock import patch, MagicMock

from torchx.runner.events import (
_get_or_create_logger,
SourceType,
TorchxEvent,
log_event,
record,
)

try:
from torch import monitor

SKIP_MONITOR: bool = False
except ImportError:
SKIP_MONITOR: bool = True


class TorchxEventLibTest(unittest.TestCase):
def assert_event(
Expand Down Expand Up @@ -57,6 +66,51 @@ def test_event_deser(self) -> None:
deser_event = TorchxEvent.deserialize(json_event)
self.assert_event(event, deser_event)

@unittest.skipIf(SKIP_MONITOR, "no torch.monitor available")
def test_monitor(self) -> None:
event = TorchxEvent(
session="test_session",
scheduler="test_scheduler",
api="test_api",
source=SourceType.EXTERNAL,
)
monitor_event = event.to_monitor_event()
self.assertEqual(
monitor_event.data,
{
"session": "test_session",
"scheduler": "test_scheduler",
"api": "test_api",
"source": "EXTERNAL",
},
)
self.assertEqual(monitor_event.name, "torch.runner.Event")

@unittest.skipIf(SKIP_MONITOR, "no torch.monitor available")
@patch("torchx.runner.events._get_or_create_logger")
def test_monitor_record(self, get_logging_handler: MagicMock) -> None:
event = TorchxEvent(
session="test_session",
scheduler="test_scheduler",
api="test_api",
source=SourceType.EXTERNAL,
)
events: List[monitor.Event] = []

def handler(e: monitor.Event) -> None:
events.append(e)

handle = monitor.register_event_handler(handler)

try:
record(event)
finally:
monitor.unregister_event_handler(handle)

self.assertEqual(get_logging_handler.call_count, 1)
self.assertEqual(len(events), 1)
self.assertEqual(events[0].data["session"], "test_session")


@patch("torchx.runner.events.record")
class LogEventTest(unittest.TestCase):
Expand Down
41 changes: 41 additions & 0 deletions typestubs/torch/monitor.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Defined in torch/csrc/monitor/python_init.cpp

from typing import List, Dict, Callable, Union
from enum import Enum
import datetime

class Aggregation(Enum):
VALUE = "value"
MEAN = "mean"
COUNT = "count"
SUM = "sum"
MAX = "max"
MIN = "min"

class Stat:
name: str
count: int
def __init__(
self, name: str, aggregations: List[Aggregation], window_size: int,
max_samples: int = -1,
) -> None: ...
def add(self, v: float) -> None: ...
def get(self) -> Dict[Aggregation, float]: ...

class Event:
name: str
timestamp: datetime.datetime
data: Dict[str, Union[int, float, bool, str]]
def __init__(
self,
name: str,
timestamp: datetime.datetime,
data: Dict[str, Union[int, float, bool, str]],
) -> None: ...

def log_event(e: Event) -> None: ...

class EventHandlerHandle: ...

def register_event_handler(handler: Callable[[Event], None]) -> EventHandlerHandle: ...
def unregister_event_handler(handle: EventHandlerHandle) -> None: ...