forked from NVIDIA/cuda-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cuda_utils.py
More file actions
78 lines (64 loc) · 2.77 KB
/
test_cuda_utils.py
File metadata and controls
78 lines (64 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. ALL RIGHTS RESERVED.
#
# SPDX-License-Identifier: LicenseRef-NVIDIA-SOFTWARE-LICENSE
import pytest
from cuda.bindings import driver, runtime
from cuda.core.experimental._utils import cuda_utils
def test_driver_cu_result_explanations_health():
expl_dict = cuda_utils.DRIVER_CU_RESULT_EXPLANATIONS
# Ensure all CUresult enums are in expl_dict
known_codes = set()
for error in driver.CUresult:
code = int(error)
assert code in expl_dict
known_codes.add(code)
if cuda_utils.get_binding_version() >= (12, 0):
# Ensure expl_dict has no codes not known as a CUresult enum
extra_expl = sorted(set(expl_dict.keys()) - known_codes)
assert not extra_expl
def test_runtime_cuda_error_explanations_health():
expl_dict = cuda_utils.RUNTIME_CUDA_ERROR_EXPLANATIONS
# Ensure all cudaError_t enums are in expl_dict
known_codes = set()
for error in runtime.cudaError_t:
code = int(error)
assert code in expl_dict
known_codes.add(code)
if cuda_utils.get_binding_version() >= (12, 0):
# Ensure expl_dict has no codes not known as a cudaError_t enum
extra_expl = sorted(set(expl_dict.keys()) - known_codes)
assert not extra_expl
def test_check_driver_error():
num_unexpected = 0
for error in driver.CUresult:
if error == driver.CUresult.CUDA_SUCCESS:
assert cuda_utils._check_driver_error(error) is None
else:
with pytest.raises(cuda_utils.CUDAError) as e:
cuda_utils._check_driver_error(error)
msg = str(e)
if "UNEXPECTED ERROR CODE" in msg:
num_unexpected += 1
else:
# Example repr(error): <CUresult.CUDA_ERROR_UNKNOWN: 999>
enum_name = repr(error).split(".", 1)[1].split(":", 1)[0]
assert enum_name in msg
# Smoke test: We don't want most to be unexpected.
assert num_unexpected < len(driver.CUresult) * 0.5
def test_check_runtime_error():
num_unexpected = 0
for error in runtime.cudaError_t:
if error == runtime.cudaError_t.cudaSuccess:
assert cuda_utils._check_runtime_error(error) is None
else:
with pytest.raises(cuda_utils.CUDAError) as e:
cuda_utils._check_runtime_error(error)
msg = str(e)
if "UNEXPECTED ERROR CODE" in msg:
num_unexpected += 1
else:
# Example repr(error): <cudaError_t.cudaErrorUnknown: 999>
enum_name = repr(error).split(".", 1)[1].split(":", 1)[0]
assert enum_name in msg
# Smoke test: We don't want most to be unexpected.
assert num_unexpected < len(driver.CUresult) * 0.5