Repository navigation
Expand file tree
/
Copy pathspeed_meter.py
More file actions
210 lines (179 loc) · 7.26 KB
/
Copy pathspeed_meter.py
File metadata and controls
210 lines (179 loc) · 7.26 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
"""
Lightweight speed-measurement helper for paper-publication benchmarking.
Records per-call wall-clock time of YOLO ``model.predict()`` invocations, then
partitions the recorded samples into K contiguous batches. The mean and
standard deviation are computed across those K batch samples (one batch = one
trial), which is what the NemaSeg paper reports for inference timing.
Designed to be opt-in: when ``enabled=False`` the helper short-circuits in
~one branch per call, so leaving it disabled has negligible overhead and does
not change any existing stdout/stderr output.
"""
from __future__ import annotations
import csv
import json
import os
import statistics
import sys
import time
from typing import Any
def _cuda_available() -> bool:
try:
import torch # noqa: WPS433 (local import keeps cold-path light)
return torch.cuda.is_available()
except Exception:
return False
def _cuda_sync() -> None:
import torch
torch.cuda.synchronize()
def _gpu_name() -> str | None:
try:
import torch
if torch.cuda.is_available():
return torch.cuda.get_device_name(0)
except Exception:
pass
return None
def resolve_device(device_arg: str) -> str:
"""Map ``"auto"`` / ``""`` to ``"cuda"`` or ``"cpu"`` based on availability."""
if device_arg in ("auto", "", None):
return "cuda" if _cuda_available() else "cpu"
if device_arg == "cuda":
return "cuda" if _cuda_available() else "cpu"
return device_arg # "cpu", "0", "0,1", etc.
def device_label(device_arg: str) -> str:
"""Short label suitable for filename suffix: ``cpu`` or ``gpu``."""
resolved = resolve_device(device_arg)
return "gpu" if resolved.startswith("cuda") or resolved.isdigit() else "cpu"
class SpeedMeter:
"""Records per-call timings and writes batched mean/SD summaries."""
def __init__(self, name: str, enabled: bool, device: str = "auto") -> None:
self.name = name
self.enabled = enabled
self.device = resolve_device(device)
self._is_cuda = self.device.startswith("cuda") or self.device.isdigit()
self._samples_sec: list[float] = []
self._warmup_sec: float = 0.0
self._warmup_items: int = 0
self._t0: float | None = None
self._tick_t0: float | None = None
# -- warmup --------------------------------------------------------
def warmup_start(self) -> None:
if not self.enabled:
return
if self._is_cuda:
_cuda_sync()
self._t0 = time.perf_counter()
def warmup_end(self, n_items: int) -> None:
if not self.enabled or self._t0 is None:
return
if self._is_cuda:
_cuda_sync()
self._warmup_sec += time.perf_counter() - self._t0
self._warmup_items += n_items
self._t0 = None
# -- per-call timing ----------------------------------------------
def tick_start(self) -> None:
if not self.enabled:
return
if self._is_cuda:
_cuda_sync()
self._tick_t0 = time.perf_counter()
def tick_end(self) -> None:
if not self.enabled or self._tick_t0 is None:
return
if self._is_cuda:
_cuda_sync()
self._samples_sec.append(time.perf_counter() - self._tick_t0)
self._tick_t0 = None
# -- summary -------------------------------------------------------
def finalize(
self,
output_dir: str,
num_batches: int,
extra_metadata: dict[str, Any] | None = None,
) -> dict[str, Any] | None:
if not self.enabled:
return None
os.makedirs(output_dir, exist_ok=True)
n = len(self._samples_sec)
if n == 0:
sys.stderr.write(
f"[speed] {self.name}: no samples recorded; nothing to summarize.\n"
)
summary = {
"name": self.name,
"n_samples": 0,
"n_batches": 0,
"device": self.device,
"gpu_name": _gpu_name(),
"warmup_sec": self._warmup_sec,
"warmup_items": self._warmup_items,
}
if extra_metadata:
summary.update(extra_metadata)
with open(os.path.join(output_dir, f"speed_{self.name}_summary.json"), "w") as f:
json.dump(summary, f, indent=2)
return summary
# Partition samples into K contiguous batches.
k = max(1, min(num_batches, n))
if k != num_batches:
sys.stderr.write(
f"[speed] {self.name}: only {n} samples; reducing batches "
f"from {num_batches} to {k}.\n"
)
# Distribute samples evenly: the first (n % k) batches get one extra
# item so batch sizes differ by at most 1. This avoids inflating the
# last batch when n is not divisible by k (which would otherwise bias
# mean/SD upward for that final trial).
base, rem = divmod(n, k)
batch_seconds: list[float] = []
batch_counts: list[int] = []
lo = 0
for i in range(k):
size = base + (1 if i < rem else 0)
hi = lo + size
batch_seconds.append(sum(self._samples_sec[lo:hi]))
batch_counts.append(size)
lo = hi
# Write raw per-batch CSV.
raw_path = os.path.join(output_dir, f"speed_{self.name}_raw.csv")
with open(raw_path, "w", newline="") as f:
w = csv.writer(f)
w.writerow(["batch_idx", "n_items", "seconds"])
for i, (sec, cnt) in enumerate(zip(batch_seconds, batch_counts)):
w.writerow([i, cnt, f"{sec:.6f}"])
mean_sec = statistics.fmean(batch_seconds)
std_sec = statistics.stdev(batch_seconds) if len(batch_seconds) > 1 else 0.0
total_sec = sum(batch_seconds)
mean_items = statistics.fmean(batch_counts)
summary: dict[str, Any] = {
"name": self.name,
"device": self.device,
"gpu_name": _gpu_name(),
"n_samples": n,
"n_batches": k,
"batch_size_mean_items": mean_items,
"batch_seconds_mean": mean_sec,
"batch_seconds_std": std_sec,
"batch_seconds_min": min(batch_seconds),
"batch_seconds_max": max(batch_seconds),
"batch_seconds_median": statistics.median(batch_seconds),
"total_seconds": total_sec,
"throughput_items_per_sec": n / total_sec if total_sec > 0 else None,
"per_item_seconds_mean": total_sec / n,
"warmup_sec": self._warmup_sec,
"warmup_items": self._warmup_items,
}
if extra_metadata:
summary.update(extra_metadata)
json_path = os.path.join(output_dir, f"speed_{self.name}_summary.json")
with open(json_path, "w") as f:
json.dump(summary, f, indent=2)
sys.stderr.write(
f"[speed] {self.name}: {k} batches × ~{mean_items:.1f} items, "
f"mean = {mean_sec*1000:.2f} ms, std = {std_sec*1000:.2f} ms, "
f"per-item = {summary['per_item_seconds_mean']*1000:.2f} ms "
f"(device={self.device})\n"
)
sys.stderr.write(f"[speed] {self.name}: wrote {raw_path} and {json_path}\n")
return summary