|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +"""Machine-state capture for the first CUDA OOM of a test session (issue #2381). |
| 5 | +
|
| 6 | +A failing ``cuda_core`` run reports ~190 ``CUDA_ERROR_OUT_OF_MEMORY`` failures |
| 7 | +that all descend from a single earlier event, so only the first is worth |
| 8 | +capturing; running ``nvidia-smi`` on every one would add minutes and bury the |
| 9 | +log. Hence the latch in :class:`OomDiagnosticsRecorder`. |
| 10 | +""" |
| 11 | + |
| 12 | +import os |
| 13 | +import pathlib |
| 14 | +import subprocess |
| 15 | +import sys |
| 16 | +import threading |
| 17 | + |
| 18 | +from cuda.bindings import driver |
| 19 | + |
| 20 | +OOM_MARKER = "CUDA_ERROR_OUT_OF_MEMORY" |
| 21 | +DEFAULT_FILENAME = "cuda_core_oom_diagnostics.txt" |
| 22 | + |
| 23 | +_BANNER = "=" * 78 |
| 24 | + |
| 25 | + |
| 26 | +def format_probe(label, fn, *args): |
| 27 | + """Call a driver API and render its raw result.""" |
| 28 | + try: |
| 29 | + return f"{label} -> {fn(*args)!r}" |
| 30 | + except Exception as exc: # diagnostics must never mask the original test failure |
| 31 | + return f"{label} -> <raised {exc!r}>" |
| 32 | + |
| 33 | + |
| 34 | +def probe_driver_state(): |
| 35 | + """Query the driver directly, bypassing cuda.core's error reporting. |
| 36 | +
|
| 37 | + cuda.core surfaces handle-creation failures through a thread-local "last |
| 38 | + error" slot (see cuda/core/_cpp/DESIGN.md). Reading the driver directly |
| 39 | + shows whether the device is genuinely out of memory and whether the default |
| 40 | + mempool is actually unavailable, which is what distinguishes real |
| 41 | + exhaustion from a mempool setup failure. |
| 42 | + """ |
| 43 | + lines = ["--- direct driver probe (bypasses cuda.core error reporting) ---"] |
| 44 | + lines.append(format_probe("cuCtxGetCurrent()", driver.cuCtxGetCurrent)) |
| 45 | + lines.append(format_probe("cuMemGetInfo()", driver.cuMemGetInfo)) |
| 46 | + |
| 47 | + try: |
| 48 | + err, count = driver.cuDeviceGetCount() |
| 49 | + except Exception as exc: # see format_probe |
| 50 | + lines.append(f"cuDeviceGetCount() -> <raised {exc!r}>") |
| 51 | + return "\n".join(lines) |
| 52 | + |
| 53 | + lines.append(f"cuDeviceGetCount() -> ({err!r}, {count})") |
| 54 | + if err != driver.CUresult.CUDA_SUCCESS: |
| 55 | + return "\n".join(lines) |
| 56 | + |
| 57 | + for ordinal in range(count): |
| 58 | + try: |
| 59 | + err, dev = driver.cuDeviceGet(ordinal) |
| 60 | + except Exception as exc: # see format_probe |
| 61 | + lines.append(f"cuDeviceGet({ordinal}) -> <raised {exc!r}>") |
| 62 | + continue |
| 63 | + if err != driver.CUresult.CUDA_SUCCESS: |
| 64 | + lines.append(f"cuDeviceGet({ordinal}) -> {err!r}") |
| 65 | + continue |
| 66 | + lines.append(format_probe(f"cuDeviceGetMemPool(dev {ordinal})", driver.cuDeviceGetMemPool, dev)) |
| 67 | + lines.append(format_probe(f"cuDeviceGetDefaultMemPool(dev {ordinal})", driver.cuDeviceGetDefaultMemPool, dev)) |
| 68 | + |
| 69 | + return "\n".join(lines) |
| 70 | + |
| 71 | + |
| 72 | +def run_nvidia_smi(*args): |
| 73 | + cmd = ["nvidia-smi", *args] |
| 74 | + printable = " ".join(cmd) |
| 75 | + try: |
| 76 | + proc = subprocess.run(cmd, capture_output=True, text=True, timeout=60, check=False) # noqa: S603 |
| 77 | + except (OSError, subprocess.SubprocessError) as exc: |
| 78 | + return f"$ {printable}\n<could not run: {exc!r}>" |
| 79 | + output = proc.stdout.strip() or proc.stderr.strip() or "<no output>" |
| 80 | + return f"$ {printable}\n(exit {proc.returncode})\n{output}" |
| 81 | + |
| 82 | + |
| 83 | +class OomDiagnosticsRecorder: |
| 84 | + """Captures machine state the first time a CUDA OOM is seen, and only then.""" |
| 85 | + |
| 86 | + def __init__(self, filename=DEFAULT_FILENAME): |
| 87 | + self._filename = filename |
| 88 | + self._lock = threading.Lock() |
| 89 | + self._captured = False |
| 90 | + self._nodeid = None |
| 91 | + self._artifact_path = None |
| 92 | + self._artifact_written = False |
| 93 | + |
| 94 | + @property |
| 95 | + def captured(self): |
| 96 | + return self._captured |
| 97 | + |
| 98 | + @property |
| 99 | + def nodeid(self): |
| 100 | + """Node id of the test that triggered capture, or None.""" |
| 101 | + return self._nodeid |
| 102 | + |
| 103 | + @property |
| 104 | + def artifact_path(self): |
| 105 | + """Where the report was written, or None if nothing was captured.""" |
| 106 | + return self._artifact_path |
| 107 | + |
| 108 | + @property |
| 109 | + def artifact_written(self): |
| 110 | + return self._artifact_written |
| 111 | + |
| 112 | + @staticmethod |
| 113 | + def matches(exc_text): |
| 114 | + return OOM_MARKER in exc_text |
| 115 | + |
| 116 | + def build_report(self, nodeid, phase, exc_text): |
| 117 | + return "\n".join( |
| 118 | + [ |
| 119 | + _BANNER, |
| 120 | + "cuda_core diagnostics: first CUDA_ERROR_OUT_OF_MEMORY of this session", |
| 121 | + _BANNER, |
| 122 | + f"test: {nodeid}", |
| 123 | + f"phase: {phase}", |
| 124 | + f"pid: {os.getpid()}", |
| 125 | + f"platform: {sys.platform}", |
| 126 | + f"exception: {exc_text}", |
| 127 | + "", |
| 128 | + probe_driver_state(), |
| 129 | + "", |
| 130 | + run_nvidia_smi("-q"), |
| 131 | + "", |
| 132 | + run_nvidia_smi("--query-compute-apps=timestamp,pid,process_name,used_memory", "--format=csv"), |
| 133 | + _BANNER, |
| 134 | + ] |
| 135 | + ) |
| 136 | + |
| 137 | + def capture(self, nodeid, phase, exc_text, directory): |
| 138 | + """Build and persist the report. Returns None if already captured.""" |
| 139 | + with self._lock: |
| 140 | + if self._captured: |
| 141 | + return None |
| 142 | + self._captured = True |
| 143 | + |
| 144 | + report = self.build_report(nodeid, phase, exc_text) |
| 145 | + destination = pathlib.Path(directory) / self._filename |
| 146 | + self._nodeid = nodeid |
| 147 | + self._artifact_path = destination |
| 148 | + try: |
| 149 | + destination.write_text(report, encoding="utf-8") |
| 150 | + self._artifact_written = True |
| 151 | + return f"{report}\n(diagnostics also written to {destination})" |
| 152 | + except OSError as exc: |
| 153 | + return f"{report}\n(could not write {destination}: {exc!r})" |
| 154 | + |
| 155 | + |
| 156 | +_default_recorder = OomDiagnosticsRecorder() |
| 157 | + |
| 158 | + |
| 159 | +def record_if_oom(item, call, report, recorder=None): |
| 160 | + """Capture diagnostics when ``report`` is the session's first CUDA OOM. |
| 161 | +
|
| 162 | + ``recorder`` defaults to a module-level singleton so the conftest hook does |
| 163 | + not have to hold session state; tests pass their own to stay isolated. |
| 164 | +
|
| 165 | + Returns the emitted text, or None when nothing was captured. |
| 166 | + """ |
| 167 | + if recorder is None: |
| 168 | + recorder = _default_recorder |
| 169 | + |
| 170 | + if recorder.captured or not report.failed or call.excinfo is None: |
| 171 | + return None |
| 172 | + |
| 173 | + exc_text = str(call.excinfo.value) |
| 174 | + if not recorder.matches(exc_text): |
| 175 | + return None |
| 176 | + |
| 177 | + text = recorder.capture(item.nodeid, call.when, exc_text, item.config.rootpath) |
| 178 | + if text is None: |
| 179 | + return None |
| 180 | + |
| 181 | + # terminalreporter writes outside pytest's stdout capture, so this survives |
| 182 | + # into a redirected log; a bare print() would not. |
| 183 | + terminal_reporter = item.config.pluginmanager.get_plugin("terminalreporter") |
| 184 | + if terminal_reporter is not None: |
| 185 | + terminal_reporter.write_line("") |
| 186 | + terminal_reporter.write_line(text) |
| 187 | + return text |
| 188 | + |
| 189 | + |
| 190 | +def report_terminal_summary(terminalreporter, recorder=None): |
| 191 | + """Point at the diagnostics artifact from pytest's end-of-run summary. |
| 192 | +
|
| 193 | + The report itself is emitted beside the failing test, which in a real |
| 194 | + failing run is thousands of lines above the summary people actually read. |
| 195 | +
|
| 196 | + Returns the emitted line, or None when nothing was captured. |
| 197 | + """ |
| 198 | + if recorder is None: |
| 199 | + recorder = _default_recorder |
| 200 | + |
| 201 | + if not recorder.captured: |
| 202 | + return None |
| 203 | + |
| 204 | + verb = "written to" if recorder.artifact_written else "could NOT be written to" |
| 205 | + line = f"first CUDA OOM at {recorder.nodeid}; diagnostics {verb} {recorder.artifact_path}" |
| 206 | + terminalreporter.write_sep("=", "cuda_core OOM diagnostics", red=True) |
| 207 | + terminalreporter.write_line(line) |
| 208 | + return line |
0 commit comments