import os import sys import pytest from unlimited_ocr_rdna4.errors import RuntimeEnvironmentError from unlimited_ocr_rdna4.runtime import DeviceInfo, RuntimeInfo, select_device def test_runtime_info_serializes() -> None: info = RuntimeInfo( torch_version="2.9.1+rocm7.2.1", hip_version="7.2", cuda_available=True, bf16_supported=True, visible_devices=1, devices=(DeviceInfo(0, "AMD Radeon RX 9070 XT", "gfx1201", "uuid"),), package_versions={"torch": "different"}, accepted_architecture=True, hardware_verified=True, software_verified=False, warnings=("different stack",), ) payload = info.to_dict() assert payload["devices"][0]["architecture"] == "gfx1201" assert payload["hardware_verified"] is True assert payload["software_verified"] is False def test_device_selection_rejects_conflicting_visibility(monkeypatch) -> None: monkeypatch.delitem(sys.modules, "torch", raising=False) monkeypatch.setenv("HIP_VISIBLE_DEVICES", "2") with pytest.raises(RuntimeEnvironmentError, match="HIP_VISIBLE_DEVICES"): select_device("GPU-example") def test_device_selection_preserves_matching_rocr_value(monkeypatch) -> None: monkeypatch.delitem(sys.modules, "torch", raising=False) monkeypatch.delenv("HIP_VISIBLE_DEVICES", raising=False) monkeypatch.delenv("CUDA_VISIBLE_DEVICES", raising=False) monkeypatch.setenv("ROCR_VISIBLE_DEVICES", "GPU-example") select_device("GPU-example") assert os.environ["ROCR_VISIBLE_DEVICES"] == "GPU-example"