-
Notifications
You must be signed in to change notification settings - Fork 618
/
ipu.py
179 lines (146 loc) 路 5.21 KB
/
ipu.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import multiprocessing as mp
from collections import deque
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Set, Tuple, Union
try:
import gcipuinfo # type: ignore
except ImportError:
gcipuinfo = None
import wandb
from .aggregators import aggregate_mean
from .asset_registry import asset_registry
from .interfaces import Interface, Metric, MetricsMonitor
if TYPE_CHECKING:
from typing import Deque
from wandb.sdk.internal.settings_static import SettingsStatic
class IPUStats:
"""
Stats for Graphcore IPU devices
"""
name = "ipu.{}.{}"
samples: "Deque[dict]"
# The metrics that change over time.
# Only these are returned on each invocation
# to avoid sending a load of unnecessary data.
variable_metric_keys = {
"average board temp",
"average die temp",
"clock",
"ipu power",
"ipu utilisation",
"ipu utilisation (session)",
}
def __init__(self, pid: int, gc_ipu_info: Any = None) -> None:
self.samples: "Deque[dict]" = deque()
if gc_ipu_info is None:
if not gcipuinfo:
raise ImportError(
"Monitoring IPU stats requires gcipuinfo to be installed"
)
self._gc_ipu_info = gcipuinfo.gcipuinfo()
else:
self._gc_ipu_info = gc_ipu_info
self._gc_ipu_info.setUpdateMode(True)
self._pid = pid
self._devices_called: Set[str] = set()
@staticmethod
def parse_metric(key: str, value: str) -> Optional[Tuple[str, Union[int, float]]]:
metric_suffixes = {
"temp": "C",
"clock": "MHz",
"power": "W",
"utilisation": "%",
"utilisation (session)": "%",
"speed": "GT/s",
}
for metric, suffix in metric_suffixes.items():
if key.endswith(metric) and value.endswith(suffix):
value = value[: -len(suffix)]
key = f"{key} ({suffix})"
try:
float_value = float(value)
num_value = int(float_value) if float_value.is_integer() else float_value
except ValueError:
return None
return key, num_value
def sample(self) -> None:
try:
stats = {}
devices = self._gc_ipu_info.getDevices()
for device in devices:
device_metrics: Dict[str, str] = dict(device)
pid = device_metrics.get("user process id")
if pid is None or int(pid) != self._pid:
continue
device_id = device_metrics.get("id")
initial_call = device_id not in self._devices_called
if device_id is not None:
self._devices_called.add(device_id)
for key, value in device_metrics.items():
log_metric = initial_call or key in self.variable_metric_keys
if not log_metric:
continue
parsed = self.parse_metric(key, value)
if parsed is None:
continue
parsed_key, parsed_value = parsed
stats[self.name.format(device_id, parsed_key)] = parsed_value
self.samples.append(stats)
except Exception as e:
wandb.termwarn(f"IPU stats error {e}", repeat=False)
def clear(self) -> None:
self.samples.clear()
def aggregate(self) -> dict:
if not self.samples:
return {}
stats = {}
for key in self.samples[0].keys():
samples = [s[key] for s in self.samples if key in s]
aggregate = aggregate_mean(samples)
stats[key] = aggregate
return stats
@asset_registry.register
class IPU:
def __init__(
self,
interface: "Interface",
settings: "SettingsStatic",
shutdown_event: mp.synchronize.Event,
) -> None:
self.name = self.__class__.__name__.lower()
self.metrics: List[Metric] = [
IPUStats(settings._stats_pid),
]
self.metrics_monitor = MetricsMonitor(
self.name,
self.metrics,
interface,
settings,
shutdown_event,
)
@classmethod
def is_available(cls) -> bool:
return gcipuinfo is not None
def start(self) -> None:
self.metrics_monitor.start()
def finish(self) -> None:
self.metrics_monitor.finish()
def probe(self) -> dict:
device_data = self.metrics[0]._gc_ipu_info.getDevices() # type: ignore
device_count = len(device_data)
devices = []
for i, device in enumerate(device_data):
device_metrics: Dict[str, str] = dict(device)
devices.append(
{
"id": device_metrics.get("id") or i,
"board ipu index": device_metrics.get("board ipu index"),
"board type": device_metrics.get("board type") or "unknown",
}
)
return {
self.name: {
"device_count": device_count,
"devices": devices,
"vendor": "Graphcore",
}
}