"""The agent's startup report of which runtime landed on the GPU (#1451). Both runtimes fall back to the CPU without raising, so the report is the only thing that says so. These pin the distinction it exists for: onnxruntime listing the CUDA provider is not the same as the provider being able to load its libraries. """ from __future__ import annotations import types from agent.fc_agent import accel def _ort(providers, preload=None): mod = types.SimpleNamespace( __version__="1.30.0", __file__="/site/onnxruntime/__init__.py", get_available_providers=lambda: providers, ) if preload is not None: mod.preload_dlls = preload return mod def _imp(mod): return lambda name: mod def test_onnx_on_gpu_when_the_cuda_provider_loads(): calls = [] s = accel.onnx_status( _imp(_ort(["CUDAExecutionProvider", "CPUExecutionProvider"], lambda: calls.append("preload"))), load=lambda path, mode=0: calls.append(path), ) assert s["device"] == "cuda" assert calls[0] == "preload" assert calls[-1].endswith("capi/libonnxruntime_providers_cuda.so") def test_onnx_listed_but_unloadable_reports_cpu_with_the_reason(): def load(path, mode=0): if path.endswith("providers_cuda.so"): raise OSError("libcudart.so.13: cannot open shared object file") s = accel.onnx_status(_imp(_ort(["CUDAExecutionProvider", "CPUExecutionProvider"])), load=load) assert s["device"] == "cpu" assert "libcudart.so.13" in s["error"] def test_onnx_cpu_build_never_tries_the_cuda_library(): def load(path, mode=0): raise AssertionError("a CPU build has no CUDA provider to load") s = accel.onnx_status(_imp(_ort(["CPUExecutionProvider"])), load=load) assert s["device"] == "cpu" def test_torch_reports_cpu_when_cuda_is_unavailable(): torch = types.SimpleNamespace( __version__="2.14.0+cu130", version=types.SimpleNamespace(cuda="13.0"), cuda=types.SimpleNamespace(is_available=lambda: False), ) s = accel.torch_status(_imp(torch)) assert s == {"version": "2.14.0+cu130", "cuda_build": "13.0", "device": "cpu"} def test_a_missing_runtime_is_reported_not_raised(): def imp(name): raise ImportError(f"No module named {name!r}") assert accel.torch_status(imp)["device"] == "unavailable" assert accel.onnx_status(imp)["device"] == "unavailable"