forked from NVIDIA/cuda-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_find_bitcode_lib.py
More file actions
163 lines (127 loc) · 5.78 KB
/
test_find_bitcode_lib.py
File metadata and controls
163 lines (127 loc) · 5.78 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
import os
from pathlib import Path
import pytest
import cuda.pathfinder._static_libs.find_bitcode_lib as find_bitcode_lib_module
from cuda.pathfinder._static_libs.find_bitcode_lib import (
SUPPORTED_BITCODE_LIBS,
BitcodeLibNotFoundError,
find_bitcode_lib,
locate_bitcode_lib,
)
from cuda.pathfinder._utils.env_vars import get_cuda_path_or_home
STRICTNESS = os.environ.get("CUDA_PATHFINDER_TEST_FIND_NVIDIA_BITCODE_LIB_STRICTNESS", "see_what_works")
assert STRICTNESS in ("see_what_works", "all_must_work")
BCL_FILENAME = find_bitcode_lib_module._SUPPORTED_BITCODE_LIBS_INFO["device"]["filename"]
@pytest.fixture
def clear_find_bitcode_lib_cache():
find_bitcode_lib_module.find_bitcode_lib.cache_clear()
get_cuda_path_or_home.cache_clear()
yield
find_bitcode_lib_module.find_bitcode_lib.cache_clear()
get_cuda_path_or_home.cache_clear()
def _make_bitcode_lib_file(dir_path: Path) -> str:
dir_path.mkdir(parents=True, exist_ok=True)
file_path = dir_path / BCL_FILENAME
file_path.touch()
return str(file_path)
def _bitcode_lib_dir_under(anchor_dir: Path) -> Path:
return anchor_dir / "nvvm" / "libdevice"
def _conda_anchor(conda_prefix: Path) -> Path:
if find_bitcode_lib_module.IS_WINDOWS:
return conda_prefix / "Library"
return conda_prefix
def _located_bitcode_lib_asserts(located_bitcode_lib):
"""Common assertions for a located bitcode library."""
assert located_bitcode_lib is not None
assert isinstance(located_bitcode_lib.name, str)
assert isinstance(located_bitcode_lib.abs_path, str)
assert isinstance(located_bitcode_lib.filename, str)
assert isinstance(located_bitcode_lib.found_via, str)
assert located_bitcode_lib.found_via in ("site-packages", "conda", "CUDA_PATH")
assert os.path.isfile(located_bitcode_lib.abs_path)
@pytest.mark.usefixtures("clear_find_bitcode_lib_cache")
@pytest.mark.parametrize("libname", SUPPORTED_BITCODE_LIBS)
def test_locate_bitcode_lib(info_log, libname):
try:
located_lib = locate_bitcode_lib(libname)
lib_path = find_bitcode_lib(libname)
except BitcodeLibNotFoundError:
if STRICTNESS == "all_must_work":
raise
info_log.info(f"{libname}: not found")
return
info_log.info(f"{lib_path=!r}")
_located_bitcode_lib_asserts(located_lib)
assert os.path.isfile(lib_path)
assert lib_path == located_lib.abs_path
expected_filename = located_lib.filename
assert os.path.basename(lib_path) == expected_filename
@pytest.mark.usefixtures("clear_find_bitcode_lib_cache")
def test_locate_bitcode_lib_search_order(monkeypatch, tmp_path):
site_packages_lib_dir = tmp_path / "site-packages" / "nvidia" / "cu13" / "nvvm" / "libdevice"
site_packages_path = _make_bitcode_lib_file(site_packages_lib_dir)
conda_prefix = tmp_path / "conda-prefix"
conda_path = _make_bitcode_lib_file(_bitcode_lib_dir_under(_conda_anchor(conda_prefix)))
cuda_home = tmp_path / "cuda-home"
cuda_home_path = _make_bitcode_lib_file(_bitcode_lib_dir_under(cuda_home))
monkeypatch.setattr(
find_bitcode_lib_module,
"find_sub_dirs_all_sitepackages",
lambda _sub_dir: [str(site_packages_lib_dir)],
)
monkeypatch.setenv("CONDA_PREFIX", str(conda_prefix))
monkeypatch.setenv("CUDA_HOME", str(cuda_home))
monkeypatch.delenv("CUDA_PATH", raising=False)
located_lib = locate_bitcode_lib("device")
assert located_lib.abs_path == site_packages_path
assert located_lib.found_via == "site-packages"
os.remove(site_packages_path)
located_lib = locate_bitcode_lib("device")
assert located_lib.abs_path == conda_path
assert located_lib.found_via == "conda"
os.remove(conda_path)
located_lib = locate_bitcode_lib("device")
assert located_lib.abs_path == cuda_home_path
assert located_lib.found_via == "CUDA_PATH"
@pytest.mark.usefixtures("clear_find_bitcode_lib_cache")
def test_find_bitcode_lib_not_found_error_includes_cuda_home_directory_listing(monkeypatch, tmp_path):
cuda_home = tmp_path / "cuda-home"
lib_dir = _bitcode_lib_dir_under(cuda_home)
lib_dir.mkdir(parents=True, exist_ok=True)
extra_file = lib_dir / "README.txt"
extra_file.write_text("placeholder", encoding="utf-8")
monkeypatch.setattr(
find_bitcode_lib_module,
"find_sub_dirs_all_sitepackages",
lambda _sub_dir: [],
)
monkeypatch.delenv("CONDA_PREFIX", raising=False)
monkeypatch.setenv("CUDA_HOME", str(cuda_home))
monkeypatch.delenv("CUDA_PATH", raising=False)
with pytest.raises(BitcodeLibNotFoundError, match=r'Failure finding "libdevice\.10\.bc"') as exc_info:
find_bitcode_lib("device")
message = str(exc_info.value)
expected_missing_file = os.path.join(str(lib_dir), BCL_FILENAME)
assert f"No such file: {expected_missing_file}" in message
assert f'listdir("{lib_dir}"):' in message
assert "README.txt" in message
@pytest.mark.usefixtures("clear_find_bitcode_lib_cache")
def test_find_bitcode_lib_not_found_error_without_cuda_home(monkeypatch):
monkeypatch.setattr(
find_bitcode_lib_module,
"find_sub_dirs_all_sitepackages",
lambda _sub_dir: [],
)
monkeypatch.delenv("CONDA_PREFIX", raising=False)
monkeypatch.delenv("CUDA_HOME", raising=False)
monkeypatch.delenv("CUDA_PATH", raising=False)
with pytest.raises(
BitcodeLibNotFoundError,
match=r'Failure finding "libdevice\.10\.bc": CUDA_HOME/CUDA_PATH not set',
):
find_bitcode_lib("device")
def test_find_bitcode_lib_invalid_name():
with pytest.raises(ValueError, match="Unknown bitcode library"):
find_bitcode_lib_module.locate_bitcode_lib("invalid")