-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcuda_diagnostics.py
More file actions
324 lines (255 loc) · 9.47 KB
/
Copy pathcuda_diagnostics.py
File metadata and controls
324 lines (255 loc) · 9.47 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
"""CUDA diagnostics and CPU-vs-GPU benchmark for BlitzID.
Reports:
- OpenCV build configuration (CUDA flags)
- Available CUDA devices (via OpenCV and optionally TensorFlow)
- Face detection speed comparison: CPU baseline vs CUDA backend
Run::
python scripts/cuda_diagnostics.py
python scripts/cuda_diagnostics.py \
--image images/bub_der_personalausweis_kopie.jpg --rounds 20
python scripts/cuda_diagnostics.py --check-tensorflow
"""
from __future__ import annotations
import argparse
import statistics
import time
from pathlib import Path
import cv2
import numpy as np
# ---------------------------------------------------------------------------
# Diagnostics
# ---------------------------------------------------------------------------
def _section(title: str) -> None:
print(f"\n{'=' * 70}")
print(f" {title}")
print("=" * 70)
def report_opencv_build() -> dict[str, str]:
"""Parse and display CUDA-related lines from OpenCV build info."""
_section("OpenCV Build Info")
print(f" Version: {cv2.__version__}")
info = cv2.getBuildInformation()
cuda_lines: dict[str, str] = {}
keywords = ("NVIDIA CUDA", "CUDA", "cuDNN", "CUFFT", "CUBLAS", "NVCUVID")
for line in info.splitlines():
stripped = line.strip()
for kw in keywords:
if stripped.startswith(kw):
key, _, val = stripped.partition(":")
cuda_lines[key.strip()] = val.strip()
if cuda_lines:
for k, v in cuda_lines.items():
print(f" {k}: {v}")
else:
print(" No CUDA-related build flags found — OpenCV was built without CUDA.")
return cuda_lines
def report_cuda_devices() -> int:
"""Report CUDA devices visible to OpenCV."""
_section("OpenCV CUDA Devices")
try:
count = cv2.cuda.getCudaEnabledDeviceCount()
except Exception as e:
print(f" cv2.cuda module unavailable: {e}")
return 0
print(f" Enabled device count: {count}")
for i in range(count):
try:
cv2.cuda.setDevice(i)
dev = cv2.cuda.getDevice()
print(f"\n Device {i} (active={dev == i}):")
try:
info = cv2.cuda.DeviceInfo(i) # type: ignore[attr-defined]
for attr in ("name", "totalMemory", "majorVersion", "minorVersion"):
if hasattr(info, attr):
val = getattr(info, attr)
val = val() if callable(val) else val
if attr == "totalMemory":
val = f"{val / (1024**2):.0f} MB"
print(f" {attr}: {val}")
except (AttributeError, Exception):
try:
cv2.cuda.printCudaDeviceInfo(i) # type: ignore[attr-defined]
except (AttributeError, Exception):
print(" (device info not available in this build)")
except cv2.error as e:
print(f" Error querying device {i}: {e}")
return count
def report_tensorflow_gpu() -> None:
"""Optionally report TensorFlow GPU visibility (for DeepFace users)."""
_section("TensorFlow GPU (optional)")
try:
import tensorflow as tf # type: ignore[import-untyped]
except ImportError:
print(" TensorFlow not installed — skipping.")
return
print(f" TensorFlow version: {tf.__version__}")
gpus = tf.config.list_physical_devices("GPU")
print(f" Visible GPU devices: {len(gpus)}")
for g in gpus:
print(f" {g}")
if not gpus:
print(" No GPU visible to TensorFlow.")
# ---------------------------------------------------------------------------
# Benchmark
# ---------------------------------------------------------------------------
def _load_or_generate_image(image_path: Path | None) -> np.ndarray:
"""Load a real image or generate a synthetic 640x480 test image."""
if image_path and image_path.exists():
img = cv2.imread(str(image_path))
if img is not None:
return img
print(f" Warning: could not read {image_path}, using synthetic image")
# Synthetic image with some structure (gradient + noise) so the DNN
# doesn't just short-circuit on a blank frame.
rng = np.random.default_rng(42)
img = rng.integers(0, 256, (480, 640, 3), dtype=np.uint8)
return img
def run_benchmark(
image: np.ndarray,
rounds: int,
model_dir: Path | None,
) -> None:
"""Run CPU baseline and (if available) CUDA benchmark."""
from blitzid._models import ModelManager, default_model_dir
_section("Benchmark: CPU vs CUDA")
mdir = model_dir or default_model_dir()
import logging
logger = logging.getLogger("cuda_diag")
logger.setLevel(logging.WARNING)
mgr = ModelManager(mdir, logger)
# --- CPU run ---
print("\n [CPU] Loading network...")
try:
net_cpu, _ = mgr.load_network(use_cuda=False)
except Exception as e:
print(f" CPU load failed: {e}")
return
times_cpu = _bench_forward(net_cpu, image, rounds)
_print_stats("CPU", times_cpu)
# --- CUDA run ---
cuda_available = ModelManager._opencv_has_cuda_support()
if not cuda_available:
print("\n [CUDA] Skipped — OpenCV built without CUDA support.")
return
try:
cuda_count = cv2.cuda.getCudaEnabledDeviceCount()
except Exception:
cuda_count = 0
if cuda_count == 0:
print("\n [CUDA] Skipped — no CUDA devices visible.")
return
print(f"\n [CUDA] Loading network (devices={cuda_count})...")
try:
net_cuda, backend = mgr.load_network(use_cuda=True, require_cuda=True)
except Exception as e:
print(f" CUDA load failed: {e}")
return
print(f" [CUDA] Backend: {backend}")
# Warm-up pass (first CUDA inference is slow due to kernel compilation).
_forward_pass(net_cuda, image)
times_cuda = _bench_forward(net_cuda, image, rounds)
_print_stats("CUDA", times_cuda)
# --- Comparison ---
if times_cpu and times_cuda:
mean_cpu = statistics.mean(times_cpu)
mean_cuda = statistics.mean(times_cuda)
speedup = mean_cpu / mean_cuda if mean_cuda > 0 else float("inf")
print(f"\n Speedup (mean): {speedup:.2f}x")
def _forward_pass(net: cv2.dnn.Net, image: np.ndarray) -> None:
"""Single DNN forward pass."""
blob = cv2.dnn.blobFromImage(
image=image,
scalefactor=1.0,
size=(300, 300),
mean=(104.0, 177.0, 123.0),
swapRB=False,
crop=False,
)
net.setInput(blob)
net.forward()
def _bench_forward(
net: cv2.dnn.Net,
image: np.ndarray,
rounds: int,
) -> list[float]:
"""Time *rounds* forward passes, return per-round durations in ms."""
times: list[float] = []
for _ in range(rounds):
start = time.perf_counter()
_forward_pass(net, image)
elapsed_ms = (time.perf_counter() - start) * 1000.0
times.append(elapsed_ms)
return times
def _print_stats(label: str, times: list[float]) -> None:
if not times:
return
print(f"\n [{label}] {len(times)} rounds:")
print(f" mean: {statistics.mean(times):8.2f} ms")
print(f" median: {statistics.median(times):8.2f} ms")
print(f" min: {min(times):8.2f} ms")
print(f" max: {max(times):8.2f} ms")
if len(times) > 1:
print(f" stdev: {statistics.stdev(times):8.2f} ms")
# ---------------------------------------------------------------------------
# CLI
# ---------------------------------------------------------------------------
def main() -> None:
parser = argparse.ArgumentParser(
description="CUDA diagnostics and CPU-vs-GPU benchmark for BlitzID"
)
parser.add_argument(
"--image",
type=Path,
default=None,
help="Image to benchmark (default: synthetic 640x480)",
)
parser.add_argument(
"--rounds",
type=int,
default=10,
help="Number of forward-pass rounds (default: 10)",
)
parser.add_argument(
"--model-dir",
type=Path,
default=None,
help="Model directory override",
)
parser.add_argument(
"--check-tensorflow",
action="store_true",
help="Also check TensorFlow GPU visibility",
)
parser.add_argument(
"--skip-benchmark",
action="store_true",
help="Only run diagnostics, skip benchmark",
)
args = parser.parse_args()
# --- Diagnostics ---
cuda_lines = report_opencv_build()
device_count = report_cuda_devices()
if args.check_tensorflow:
report_tensorflow_gpu()
# --- Summary ---
_section("Summary")
has_cuda_build = any("YES" in v.upper() for v in cuda_lines.values())
print(f" OpenCV CUDA build: {'Yes' if has_cuda_build else 'No'}")
print(f" CUDA devices: {device_count}")
if not has_cuda_build:
print("\n ⚠ OpenCV was built without CUDA. To use GPU acceleration,")
print(
" rebuild OpenCV with -DWITH_CUDA=ON "
"or install opencv-contrib-python-cuda."
)
# --- Benchmark ---
if args.skip_benchmark:
print("\n Benchmark skipped (--skip-benchmark).")
return
image = _load_or_generate_image(args.image)
print(
f"\n Benchmark image: {image.shape[1]}x{image.shape[0]} "
f"({'file' if args.image else 'synthetic'})"
)
run_benchmark(image, rounds=args.rounds, model_dir=args.model_dir)
if __name__ == "__main__":
main()