Coverage for cuda / bindings / _test_helpers / arch_check.py: 84.38%
32 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-29 01:27 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-29 01:27 +0000
1# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2# SPDX-License-Identifier: LicenseRef-NVIDIA-SOFTWARE-LICENSE
5from contextlib import contextmanager
6from functools import cache
8import pytest
10from cuda.bindings import nvml
11from cuda.bindings._internal.utils import FunctionNotFoundError as NvmlSymbolNotFoundError
14@cache
15def hardware_supports_nvml():
16 """
17 Tries to call the simplest NVML API possible to see if just the basics
18 works. If not we are probably on one of the platforms where NVML is not
19 supported at all (e.g. Jetson Orin).
20 """
21 nvml.init_v2()
22 try:
23 nvml.system_get_driver_branch()
24 except (nvml.NotSupportedError, nvml.UnknownError):
25 return False
26 else:
27 return True
28 finally:
29 nvml.shutdown()
32@contextmanager
33def unsupported_before(device: int, expected_device_arch: nvml.DeviceArch | str | None):
34 device_arch = nvml.device_get_architecture(device) 1wxycdzeVfghABijWXYZCklmnopDEFGH0qrIbJsK1LMNO2PQRtuSTvU
36 if isinstance(expected_device_arch, nvml.DeviceArch): 1wxycdzeVfghABijWXYZCklmnopDEFGH0qrIbJsK1LMNO2PQRtuSTvU
37 expected_device_arch_int = int(expected_device_arch) 1VWXYZ0b12
38 elif expected_device_arch == "FERMI": 1wxycdzefghABijCklmnopDEFGHqrIbJsKLMNOPQRtuSTvU
39 expected_device_arch_int = 1 1GbJ
40 else:
41 expected_device_arch_int = 0 1wxycdzefghABijCklmnopDEFHqrIbsKLMNOPQRtuSTvU
43 if expected_device_arch is None or expected_device_arch == "HAS_INFOROM" or device_arch == nvml.DeviceArch.UNKNOWN: 1wxycdzeVfghABijWXYZCklmnopDEFGH0qrIbJsK1LMNO2PQRtuSTvU
44 # In this case, we don't /know/ if it will fail, but we are ok if it
45 # does or does not.
47 # TODO: There are APIs that are documented as supported only if the
48 # device has an InfoROM, but I couldn't find a way to detect that. For
49 # now, they are just handled as "possibly failing".
51 try: 1wxycdzefghABijCklmnopDEFHqrIbsKLMNOPQRtuSTvU
52 yield 1wxycdzefghABijCklmnopDEFHqrIbsKLMNOPQRtuSTvU
53 except (nvml.NotSupportedError, nvml.FunctionNotFoundError, NvmlSymbolNotFoundError): 1cdefghijklmnopqrbstuv
54 # The API call raised NotSupportedError, NVML status FunctionNotFoundError,
55 # or NvmlSymbolNotFoundError (symbol absent from the loaded NVML DLL), so we
56 # skip the test but don't fail it
57 pytest.skip( 1cdefghijklmnopqrbstuv
58 f"Unsupported call for device architecture {nvml.DeviceArch(device_arch).name} "
59 f"on device '{nvml.device_get_name(device)}'"
60 )
61 # If the API call worked, just continue
62 elif int(device_arch) < expected_device_arch_int: 1VWXYZG0bJ12
63 # In this case, we /know/ if will fail, and we want to assert that it does.
64 with pytest.raises(nvml.NotSupportedError):
65 yield
66 # The above call was unsupported, so the rest of the test is skipped
67 pytest.skip(f"Unsupported before {expected_device_arch.name}, got {nvml.device_get_name(device)}")
68 else:
69 # In this case, we /know/ it should work, and if it fails, the test should fail.
70 yield 1VWXYZG0bJ12