-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcuda_paths.py
328 lines (265 loc) · 9.79 KB
/
cuda_paths.py
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import sys
import re
import os
from collections import namedtuple
import platform
from numba.core.config import IS_WIN32
from numba.misc.findlib import find_lib, find_file
from numba import config
_env_path_tuple = namedtuple('_env_path_tuple', ['by', 'info'])
def _find_valid_path(options):
"""Find valid path from *options*, which is a list of 2-tuple of
(name, path). Return first pair where *path* is not None.
If no valid path is found, return ('<unknown>', None)
"""
for by, data in options:
if data is not None:
return by, data
else:
return '<unknown>', None
def _get_libdevice_path_decision():
options = [
('Conda environment', get_conda_ctk()),
('Conda environment (NVIDIA package)', get_nvidia_libdevice_ctk()),
('CUDA_HOME', get_cuda_home('nvvm', 'libdevice')),
('System', get_system_ctk('nvvm', 'libdevice')),
('Debian package', get_debian_pkg_libdevice()),
]
by, libdir = _find_valid_path(options)
return by, libdir
def _nvvm_lib_dir():
if IS_WIN32:
return 'nvvm', 'bin'
else:
return 'nvvm', 'lib64'
def _get_nvvm_path_decision():
options = [
('Conda environment', get_conda_ctk()),
('Conda environment (NVIDIA package)', get_nvidia_nvvm_ctk()),
('CUDA_HOME', get_cuda_home(*_nvvm_lib_dir())),
('System', get_system_ctk(*_nvvm_lib_dir())),
]
by, path = _find_valid_path(options)
return by, path
def _get_libdevice_paths():
by, libdir = _get_libdevice_path_decision()
# Search for pattern
pat = r'libdevice(\.\d+)*\.bc$'
candidates = find_file(re.compile(pat), libdir)
# Keep only the max (most recent version) of the bitcode files.
out = max(candidates, default=None)
return _env_path_tuple(by, out)
def _cudalib_path():
if IS_WIN32:
return 'bin'
else:
return 'lib64'
def _cuda_home_static_cudalib_path():
if IS_WIN32:
return ('lib', 'x64')
else:
return ('lib64',)
def _get_cudalib_dir_path_decision():
options = [
('Conda environment', get_conda_ctk()),
('Conda environment (NVIDIA package)', get_nvidia_cudalib_ctk()),
('CUDA_HOME', get_cuda_home(_cudalib_path())),
('System', get_system_ctk(_cudalib_path())),
]
by, libdir = _find_valid_path(options)
return by, libdir
def _get_static_cudalib_dir_path_decision():
options = [
('Conda environment', get_conda_ctk()),
('Conda environment (NVIDIA package)', get_nvidia_static_cudalib_ctk()),
('CUDA_HOME', get_cuda_home(*_cuda_home_static_cudalib_path())),
('System', get_system_ctk(_cudalib_path())),
]
by, libdir = _find_valid_path(options)
return by, libdir
def _get_cudalib_dir():
by, libdir = _get_cudalib_dir_path_decision()
return _env_path_tuple(by, libdir)
def _get_static_cudalib_dir():
by, libdir = _get_static_cudalib_dir_path_decision()
return _env_path_tuple(by, libdir)
def get_system_ctk(*subdirs):
"""Return path to system-wide cudatoolkit; or, None if it doesn't exist.
"""
# Linux?
if sys.platform.startswith('linux'):
# Is cuda alias to /usr/local/cuda?
# We are intentionally not getting versioned cuda installation.
base = '/usr/local/cuda'
if os.path.exists(base):
return os.path.join(base, *subdirs)
def get_conda_ctk():
"""Return path to directory containing the shared libraries of cudatoolkit.
"""
is_conda_env = os.path.exists(os.path.join(sys.prefix, 'conda-meta'))
if not is_conda_env:
return
# Assume the existence of NVVM to imply cudatoolkit installed
paths = find_lib('nvvm')
if not paths:
return
# Use the directory name of the max path
return os.path.dirname(max(paths))
def get_nvidia_nvvm_ctk():
"""Return path to directory containing the NVVM shared library.
"""
is_conda_env = os.path.exists(os.path.join(sys.prefix, 'conda-meta'))
if not is_conda_env:
return
# Assume the existence of NVVM in the conda env implies that a CUDA toolkit
# conda package is installed.
# First, try the location used on Linux and the Windows 11.x packages
libdir = os.path.join(sys.prefix, 'nvvm', _cudalib_path())
if not os.path.exists(libdir) or not os.path.isdir(libdir):
# If that fails, try the location used for Windows 12.x packages
libdir = os.path.join(sys.prefix, 'Library', 'nvvm', _cudalib_path())
if not os.path.exists(libdir) or not os.path.isdir(libdir):
# If that doesn't exist either, assume we don't have the NVIDIA
# conda package
return
paths = find_lib('nvvm', libdir=libdir)
if not paths:
return
# Use the directory name of the max path
return os.path.dirname(max(paths))
def get_nvidia_libdevice_ctk():
"""Return path to directory containing the libdevice library.
"""
nvvm_ctk = get_nvidia_nvvm_ctk()
if not nvvm_ctk:
return
nvvm_dir = os.path.dirname(nvvm_ctk)
return os.path.join(nvvm_dir, 'libdevice')
def get_nvidia_cudalib_ctk():
"""Return path to directory containing the shared libraries of cudatoolkit.
"""
nvvm_ctk = get_nvidia_nvvm_ctk()
if not nvvm_ctk:
return
env_dir = os.path.dirname(os.path.dirname(nvvm_ctk))
subdir = 'bin' if IS_WIN32 else 'lib'
return os.path.join(env_dir, subdir)
def get_nvidia_static_cudalib_ctk():
"""Return path to directory containing the static libraries of cudatoolkit.
"""
nvvm_ctk = get_nvidia_nvvm_ctk()
if not nvvm_ctk:
return
if IS_WIN32 and ("Library" not in nvvm_ctk):
# Location specific to CUDA 11.x packages on Windows
dirs = ('Lib', 'x64')
else:
# Linux, or Windows with CUDA 12.x packages
dirs = ('lib',)
env_dir = os.path.dirname(os.path.dirname(nvvm_ctk))
return os.path.join(env_dir, *dirs)
def get_cuda_home(*subdirs):
"""Get paths of CUDA_HOME.
If *subdirs* are the subdirectory name to be appended in the resulting
path.
"""
cuda_home = os.environ.get('CUDA_HOME')
if cuda_home is None:
# Try Windows CUDA installation without Anaconda
cuda_home = os.environ.get('CUDA_PATH')
if cuda_home is not None:
return os.path.join(cuda_home, *subdirs)
def _get_nvvm_path():
by, path = _get_nvvm_path_decision()
candidates = find_lib('nvvm', path)
path = max(candidates) if candidates else None
return _env_path_tuple(by, path)
def get_cuda_paths():
"""Returns a dictionary mapping component names to a 2-tuple
of (source_variable, info).
The returned dictionary will have the following keys and infos:
- "nvvm": file_path
- "libdevice": List[Tuple[arch, file_path]]
- "cudalib_dir": directory_path
Note: The result of the function is cached.
"""
# Check cache
if hasattr(get_cuda_paths, '_cached_result'):
return get_cuda_paths._cached_result
else:
# Not in cache
d = {
'nvvm': _get_nvvm_path(),
'libdevice': _get_libdevice_paths(),
'cudalib_dir': _get_cudalib_dir(),
'static_cudalib_dir': _get_static_cudalib_dir(),
'include_dir': _get_include_dir(),
}
# Cache result
get_cuda_paths._cached_result = d
return d
def get_debian_pkg_libdevice():
"""
Return the Debian NVIDIA Maintainers-packaged libdevice location, if it
exists.
"""
pkg_libdevice_location = '/usr/lib/nvidia-cuda-toolkit/libdevice'
if not os.path.exists(pkg_libdevice_location):
return None
return pkg_libdevice_location
def get_current_cuda_target_name():
"""Determine conda's CTK target folder based on system and machine arch.
CTK's conda package delivers headers based on its architecture type. For example,
`x86_64` machine places header under `$CONDA_PREFIX/targets/x86_64-linux`, and
`aarch64` places under `$CONDA_PREFIX/targets/sbsa-linux`. Read more about the
nuances at cudart's conda feedstock:
https://github.com/conda-forge/cuda-cudart-feedstock/blob/main/recipe/meta.yaml#L8-L11 # noqa: E501
"""
system = platform.system()
machine = platform.machine()
if system == "Linux":
arch_to_targets = {
'x86_64': 'x86_64-linux',
'aarch64': 'sbsa-linux'
}
elif system == "Windows":
arch_to_targets = {
'AMD64': 'x64',
}
else:
arch_to_targets = {}
return arch_to_targets.get(machine, None)
def get_conda_include_dir():
"""
Return the include directory in the current conda environment, if one
is active and it exists.
"""
is_conda_env = os.path.exists(os.path.join(sys.prefix, 'conda-meta'))
if not is_conda_env:
return
if platform.system() == "Windows":
include_dir = os.path.join(
sys.prefix, 'Library', 'include'
)
elif target_name := get_current_cuda_target_name():
include_dir = os.path.join(
sys.prefix, 'targets', target_name, 'include'
)
else:
# A fallback when target cannot determined
# though usually it shouldn't.
include_dir = os.path.join(sys.prefix, 'include')
if (os.path.exists(include_dir) and os.path.isdir(include_dir)
and os.path.exists(os.path.join(include_dir,
'cuda_device_runtime_api.h'))):
return include_dir
return
def _get_include_dir():
"""Find the root include directory."""
options = [
('Conda environment (NVIDIA package)', get_conda_include_dir()),
('CUDA_INCLUDE_PATH Config Entry', config.CUDA_INCLUDE_PATH),
# TODO: add others
]
by, include_dir = _find_valid_path(options)
return _env_path_tuple(by, include_dir)