Skip to content

Commit b54ec83

Browse files
committed
Add _CTK_MAJOR_MINOR_PATCH version guards and DRY up explanation tests (#1712)
Rename explanation dicts to _EXPLANATIONS / _FALLBACK_EXPLANATIONS, add _CTK_MAJOR_MINOR_PATCH to each module, and enforce that the cuda_core fallback copy is as new as (and in-sync with) cuda_bindings. Parametrize the smoke and version-check tests to cover both driver and runtime without duplication. Made-with: Cursor
1 parent d6b3e78 commit b54ec83

6 files changed

Lines changed: 55 additions & 21 deletions

File tree

cuda_bindings/cuda/bindings/_utils/driver_cu_result_explanations.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
# Replace the dictionary below with the output.
77
# Also update the CUDA Toolkit version number below.
88

9-
# CUDA Toolkit v13.2.0
10-
DRIVER_CU_RESULT_EXPLANATIONS = {
9+
_CTK_MAJOR_MINOR_PATCH = (13, 2, 0)
10+
11+
_EXPLANATIONS = {
1112
0: (
1213
"The API call returned with no errors. In the case of query calls, this"
1314
" also means that the operation being queried is complete (see"

cuda_bindings/cuda/bindings/_utils/runtime_cuda_error_explanations.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
# Replace the dictionary below with the output.
77
# Also update the CUDA Toolkit version number below.
88

9-
# CUDA Toolkit v13.2.0
10-
RUNTIME_CUDA_ERROR_EXPLANATIONS = {
9+
_CTK_MAJOR_MINOR_PATCH = (13, 2, 0)
10+
11+
_EXPLANATIONS = {
1112
0: (
1213
"The API call returned with no errors. In the case of query calls, this"
1314
" also means that the operation being queried is complete (see"

cuda_bindings/tests/test_enum_explanations.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
import importlib.metadata
55

66
from cuda.bindings import driver, runtime
7-
from cuda.bindings._utils.driver_cu_result_explanations import DRIVER_CU_RESULT_EXPLANATIONS
8-
from cuda.bindings._utils.runtime_cuda_error_explanations import RUNTIME_CUDA_ERROR_EXPLANATIONS
7+
from cuda.bindings._utils import driver_cu_result_explanations, runtime_cuda_error_explanations
98

109

1110
def _get_binding_version():
@@ -17,7 +16,7 @@ def _get_binding_version():
1716

1817

1918
def test_driver_cu_result_explanations_health():
20-
expl_dict = DRIVER_CU_RESULT_EXPLANATIONS
19+
expl_dict = driver_cu_result_explanations._EXPLANATIONS
2120

2221
known_codes = set()
2322
for error in driver.CUresult:
@@ -31,7 +30,7 @@ def test_driver_cu_result_explanations_health():
3130

3231

3332
def test_runtime_cuda_error_explanations_health():
34-
expl_dict = RUNTIME_CUDA_ERROR_EXPLANATIONS
33+
expl_dict = runtime_cuda_error_explanations._EXPLANATIONS
3534

3635
known_codes = set()
3736
for error in runtime.cudaError_t:

cuda_core/cuda/core/_utils/driver_cu_result_explanations.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
# SPDX-License-Identifier: LicenseRef-NVIDIA-SOFTWARE-LICENSE
33

44
# Fallback copy -- overridden from cuda.bindings below when available.
5-
# CUDA Toolkit v13.2.0
6-
DRIVER_CU_RESULT_EXPLANATIONS = {
5+
_CTK_MAJOR_MINOR_PATCH = (13, 2, 0)
6+
7+
_FALLBACK_EXPLANATIONS = {
78
0: (
89
"The API call returned with no errors. In the case of query calls, this"
910
" also means that the operation being queried is complete (see"
@@ -353,10 +354,12 @@
353354
999: "This indicates that an unknown internal error has occurred.",
354355
}
355356

357+
DRIVER_CU_RESULT_EXPLANATIONS = _FALLBACK_EXPLANATIONS
358+
356359
# Prefer the authoritative copy from cuda.bindings when available.
357360
try:
358361
import cuda.bindings._utils.driver_cu_result_explanations as _authoritative
359362
except ModuleNotFoundError:
360363
pass
361364
else:
362-
DRIVER_CU_RESULT_EXPLANATIONS = _authoritative.DRIVER_CU_RESULT_EXPLANATIONS
365+
DRIVER_CU_RESULT_EXPLANATIONS = _authoritative._EXPLANATIONS

cuda_core/cuda/core/_utils/runtime_cuda_error_explanations.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
# SPDX-License-Identifier: LicenseRef-NVIDIA-SOFTWARE-LICENSE
33

44
# Fallback copy -- overridden from cuda.bindings below when available.
5-
# CUDA Toolkit v13.2.0
6-
RUNTIME_CUDA_ERROR_EXPLANATIONS = {
5+
_CTK_MAJOR_MINOR_PATCH = (13, 2, 0)
6+
7+
_FALLBACK_EXPLANATIONS = {
78
0: (
89
"The API call returned with no errors. In the case of query calls, this"
910
" also means that the operation being queried is complete (see"
@@ -546,10 +547,12 @@
546547
),
547548
}
548549

550+
RUNTIME_CUDA_ERROR_EXPLANATIONS = _FALLBACK_EXPLANATIONS
551+
549552
# Prefer the authoritative copy from cuda.bindings when available.
550553
try:
551554
import cuda.bindings._utils.runtime_cuda_error_explanations as _authoritative
552555
except ModuleNotFoundError:
553556
pass
554557
else:
555-
RUNTIME_CUDA_ERROR_EXPLANATIONS = _authoritative.RUNTIME_CUDA_ERROR_EXPLANATIONS
558+
RUNTIME_CUDA_ERROR_EXPLANATIONS = _authoritative._EXPLANATIONS

cuda_core/tests/test_cuda_utils.py

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,53 @@
33
# SPDX-License-Identifier: LicenseRef-NVIDIA-SOFTWARE-LICENSE
44

55
import dataclasses
6+
import importlib
67

78
import pytest
89

910
from cuda.bindings import driver, runtime
1011
from cuda.core._utils import cuda_utils
1112
from cuda.core._utils.clear_error_support import assert_type_str_or_bytes_like, raise_code_path_meant_to_be_unreachable
1213

14+
_EXPLANATION_MODULES = [
15+
("driver_cu_result_explanations", "DRIVER_CU_RESULT_EXPLANATIONS"),
16+
("runtime_cuda_error_explanations", "RUNTIME_CUDA_ERROR_EXPLANATIONS"),
17+
]
1318

14-
def test_driver_cu_result_explanations_smoke():
15-
expl = cuda_utils.DRIVER_CU_RESULT_EXPLANATIONS
19+
20+
@pytest.mark.parametrize("module_name,public_name", _EXPLANATION_MODULES)
21+
def test_explanations_smoke(module_name, public_name):
22+
expl = getattr(cuda_utils, public_name)
1623
for code in (0, 1, 2):
1724
assert code in expl
1825
assert isinstance(expl[code], str)
1926

2027

21-
def test_runtime_cuda_error_explanations_smoke():
22-
expl = cuda_utils.RUNTIME_CUDA_ERROR_EXPLANATIONS
23-
for code in (0, 1, 2):
24-
assert code in expl
25-
assert isinstance(expl[code], str)
28+
@pytest.mark.parametrize("module_name,public_name", _EXPLANATION_MODULES)
29+
def test_explanations_ctk_version(module_name, public_name):
30+
del public_name # unused
31+
core_mod = importlib.import_module(f"cuda.core._utils.{module_name}")
32+
try:
33+
bindings_mod = importlib.import_module(f"cuda.bindings._utils.{module_name}")
34+
except ModuleNotFoundError:
35+
pytest.skip("cuda.bindings._utils not available")
36+
bindings_path = f"cuda_bindings/cuda/bindings/_utils/{module_name}.py"
37+
core_path = f"cuda_core/cuda/core/_utils/{module_name}.py"
38+
if core_mod._CTK_MAJOR_MINOR_PATCH < bindings_mod._CTK_MAJOR_MINOR_PATCH:
39+
raise RuntimeError(
40+
f"cuda_core copy is older ({core_mod._CTK_MAJOR_MINOR_PATCH})"
41+
f" than cuda_bindings ({bindings_mod._CTK_MAJOR_MINOR_PATCH})."
42+
f" Please copy the _EXPLANATIONS dict from {bindings_path} to {core_path}"
43+
)
44+
if (
45+
core_mod._CTK_MAJOR_MINOR_PATCH == bindings_mod._CTK_MAJOR_MINOR_PATCH
46+
and core_mod._FALLBACK_EXPLANATIONS != bindings_mod._EXPLANATIONS
47+
):
48+
raise RuntimeError(
49+
f"The cuda_core copy of the cuda_bindings _EXPLANATIONS dict is out of sync"
50+
f" (both at CTK {core_mod._CTK_MAJOR_MINOR_PATCH})."
51+
f" Please copy the _EXPLANATIONS dict from {bindings_path} to {core_path}"
52+
)
2653

2754

2855
def test_check_driver_error():

0 commit comments

Comments
 (0)