1
- import sys
2
- import re
3
1
import os
4
- from collections import namedtuple
5
2
import platform
3
+ import re
6
4
import site
5
+ import sys
6
+ from collections import namedtuple
7
7
from pathlib import Path
8
- from numba .core .config import IS_WIN32
9
- from numba .misc .findlib import find_lib , find_file
10
- from numba import config
11
8
9
+ from numba import config
10
+ from numba .core .config import IS_WIN32
11
+ from numba .misc .findlib import find_file , find_lib
12
12
13
- _env_path_tuple = namedtuple (' _env_path_tuple' , ['by' , ' info' ])
13
+ _env_path_tuple = namedtuple (" _env_path_tuple" , ["by" , " info" ])
14
14
15
15
16
16
def _find_valid_path (options ):
@@ -22,52 +22,50 @@ def _find_valid_path(options):
22
22
if data is not None :
23
23
return by , data
24
24
else :
25
- return ' <unknown>' , None
25
+ return " <unknown>" , None
26
26
27
27
28
28
def _get_libdevice_path_decision ():
29
29
options = [
30
- (' Conda environment' , get_conda_ctk ()),
31
- (' Conda environment (NVIDIA package)' , get_nvidia_libdevice_ctk ()),
32
- (' CUDA_HOME' , get_cuda_home (' nvvm' , ' libdevice' )),
33
- (' Debian package' , get_debian_pkg_libdevice ()),
34
- (' NVIDIA NVCC Wheel' , get_libdevice_wheel ()),
30
+ (" Conda environment" , get_conda_ctk ()),
31
+ (" Conda environment (NVIDIA package)" , get_nvidia_libdevice_ctk ()),
32
+ (" CUDA_HOME" , get_cuda_home (" nvvm" , " libdevice" )),
33
+ (" Debian package" , get_debian_pkg_libdevice ()),
34
+ (" NVIDIA NVCC Wheel" , get_libdevice_wheel ()),
35
35
]
36
- libdevice_ctk_dir = get_system_ctk (' nvvm' , ' libdevice' )
36
+ libdevice_ctk_dir = get_system_ctk (" nvvm" , " libdevice" )
37
37
if os .path .exists (libdevice_ctk_dir ):
38
- options .append ((' System' , libdevice_ctk_dir ))
38
+ options .append ((" System" , libdevice_ctk_dir ))
39
39
40
40
by , libdir = _find_valid_path (options )
41
41
return by , libdir
42
42
43
43
44
44
def _nvvm_lib_dir ():
45
45
if IS_WIN32 :
46
- return ' nvvm' , ' bin'
46
+ return " nvvm" , " bin"
47
47
else :
48
- return ' nvvm' , ' lib64'
48
+ return " nvvm" , " lib64"
49
49
50
50
51
51
def _get_nvvm_path_decision ():
52
52
options = [
53
- (' Conda environment' , get_conda_ctk ()),
54
- (' Conda environment (NVIDIA package)' , get_nvidia_nvvm_ctk ()),
55
- (' CUDA_HOME' , get_cuda_home (* _nvvm_lib_dir ())),
56
- (' NVIDIA NVCC Wheel' , _get_nvvm_wheel ()),
53
+ (" Conda environment" , get_conda_ctk ()),
54
+ (" Conda environment (NVIDIA package)" , get_nvidia_nvvm_ctk ()),
55
+ (" CUDA_HOME" , get_cuda_home (* _nvvm_lib_dir ())),
56
+ (" NVIDIA NVCC Wheel" , _get_nvvm_wheel ()),
57
57
]
58
58
# need to ensure nvvm dir actually exists
59
59
nvvm_ctk_dir = get_system_ctk (* _nvvm_lib_dir ())
60
60
if os .path .exists (nvvm_ctk_dir ):
61
- options .append ((' System' , nvvm_ctk_dir ))
61
+ options .append ((" System" , nvvm_ctk_dir ))
62
62
63
63
by , path = _find_valid_path (options )
64
64
return by , path
65
65
66
66
67
67
def _get_nvvm_wheel ():
68
- site_paths = [
69
- site .getusersitepackages ()
70
- ] + site .getsitepackages () + ["conda" , None ]
68
+ site_paths = [site .getusersitepackages ()] + site .getsitepackages () + ["conda" , None ]
71
69
for sp in site_paths :
72
70
# The SONAME is taken based on public CTK 12.x releases
73
71
if sys .platform .startswith ("linux" ):
@@ -82,13 +80,7 @@ def _get_nvvm_wheel():
82
80
raise AssertionError ()
83
81
84
82
if sp is not None :
85
- dso_dir = os .path .join (
86
- sp ,
87
- "nvidia" ,
88
- "cuda_nvcc" ,
89
- "nvvm" ,
90
- dso_dir
91
- )
83
+ dso_dir = os .path .join (sp , "nvidia" , "cuda_nvcc" , "nvvm" , dso_dir )
92
84
dso_path = os .path .join (dso_dir , dso_path )
93
85
if os .path .exists (dso_path ):
94
86
return str (Path (dso_path ).parent )
@@ -101,7 +93,7 @@ def _get_libdevice_paths():
101
93
out = os .path .join (libdir , "libdevice.10.bc" )
102
94
else :
103
95
# Search for pattern
104
- pat = r' libdevice(\.\d+)*\.bc$'
96
+ pat = r" libdevice(\.\d+)*\.bc$"
105
97
candidates = find_file (re .compile (pat ), libdir )
106
98
# Keep only the max (most recent version) of the bitcode files.
107
99
out = max (candidates , default = None )
@@ -110,35 +102,35 @@ def _get_libdevice_paths():
110
102
111
103
def _cudalib_path ():
112
104
if IS_WIN32 :
113
- return ' bin'
105
+ return " bin"
114
106
else :
115
- return ' lib64'
107
+ return " lib64"
116
108
117
109
118
110
def _cuda_home_static_cudalib_path ():
119
111
if IS_WIN32 :
120
- return (' lib' , ' x64' )
112
+ return (" lib" , " x64" )
121
113
else :
122
- return (' lib64' ,)
114
+ return (" lib64" ,)
123
115
124
116
125
117
def _get_cudalib_dir_path_decision ():
126
118
options = [
127
- (' Conda environment' , get_conda_ctk ()),
128
- (' Conda environment (NVIDIA package)' , get_nvidia_cudalib_ctk ()),
129
- (' CUDA_HOME' , get_cuda_home (_cudalib_path ())),
130
- (' System' , get_system_ctk (_cudalib_path ())),
119
+ (" Conda environment" , get_conda_ctk ()),
120
+ (" Conda environment (NVIDIA package)" , get_nvidia_cudalib_ctk ()),
121
+ (" CUDA_HOME" , get_cuda_home (_cudalib_path ())),
122
+ (" System" , get_system_ctk (_cudalib_path ())),
131
123
]
132
124
by , libdir = _find_valid_path (options )
133
125
return by , libdir
134
126
135
127
136
128
def _get_static_cudalib_dir_path_decision ():
137
129
options = [
138
- (' Conda environment' , get_conda_ctk ()),
139
- (' Conda environment (NVIDIA package)' , get_nvidia_static_cudalib_ctk ()),
140
- (' CUDA_HOME' , get_cuda_home (* _cuda_home_static_cudalib_path ())),
141
- (' System' , get_system_ctk (_cudalib_path ())),
130
+ (" Conda environment" , get_conda_ctk ()),
131
+ (" Conda environment (NVIDIA package)" , get_nvidia_static_cudalib_ctk ()),
132
+ (" CUDA_HOME" , get_cuda_home (* _cuda_home_static_cudalib_path ())),
133
+ (" System" , get_system_ctk (_cudalib_path ())),
142
134
]
143
135
by , libdir = _find_valid_path (options )
144
136
return by , libdir
@@ -155,92 +147,86 @@ def _get_static_cudalib_dir():
155
147
156
148
157
149
def get_system_ctk (* subdirs ):
158
- """Return path to system-wide cudatoolkit; or, None if it doesn't exist.
159
- """
150
+ """Return path to system-wide cudatoolkit; or, None if it doesn't exist."""
160
151
# Linux?
161
- if sys .platform .startswith (' linux' ):
152
+ if sys .platform .startswith (" linux" ):
162
153
# Is cuda alias to /usr/local/cuda?
163
154
# We are intentionally not getting versioned cuda installation.
164
- base = ' /usr/local/cuda'
155
+ base = " /usr/local/cuda"
165
156
if os .path .exists (base ):
166
157
return os .path .join (base , * subdirs )
167
158
168
159
169
160
def get_conda_ctk ():
170
- """Return path to directory containing the shared libraries of cudatoolkit.
171
- """
172
- is_conda_env = os .path .exists (os .path .join (sys .prefix , 'conda-meta' ))
161
+ """Return path to directory containing the shared libraries of cudatoolkit."""
162
+ is_conda_env = os .path .exists (os .path .join (sys .prefix , "conda-meta" ))
173
163
if not is_conda_env :
174
164
return
175
165
# Assume the existence of NVVM to imply cudatoolkit installed
176
- paths = find_lib (' nvvm' )
166
+ paths = find_lib (" nvvm" )
177
167
if not paths :
178
168
return
179
169
# Use the directory name of the max path
180
170
return os .path .dirname (max (paths ))
181
171
182
172
183
173
def get_nvidia_nvvm_ctk ():
184
- """Return path to directory containing the NVVM shared library.
185
- """
186
- is_conda_env = os .path .exists (os .path .join (sys .prefix , 'conda-meta' ))
174
+ """Return path to directory containing the NVVM shared library."""
175
+ is_conda_env = os .path .exists (os .path .join (sys .prefix , "conda-meta" ))
187
176
if not is_conda_env :
188
177
return
189
178
190
179
# Assume the existence of NVVM in the conda env implies that a CUDA toolkit
191
180
# conda package is installed.
192
181
193
182
# First, try the location used on Linux and the Windows 11.x packages
194
- libdir = os .path .join (sys .prefix , ' nvvm' , _cudalib_path ())
183
+ libdir = os .path .join (sys .prefix , " nvvm" , _cudalib_path ())
195
184
if not os .path .exists (libdir ) or not os .path .isdir (libdir ):
196
185
# If that fails, try the location used for Windows 12.x packages
197
- libdir = os .path .join (sys .prefix , ' Library' , ' nvvm' , _cudalib_path ())
186
+ libdir = os .path .join (sys .prefix , " Library" , " nvvm" , _cudalib_path ())
198
187
if not os .path .exists (libdir ) or not os .path .isdir (libdir ):
199
188
# If that doesn't exist either, assume we don't have the NVIDIA
200
189
# conda package
201
190
return
202
191
203
- paths = find_lib (' nvvm' , libdir = libdir )
192
+ paths = find_lib (" nvvm" , libdir = libdir )
204
193
if not paths :
205
194
return
206
195
# Use the directory name of the max path
207
196
return os .path .dirname (max (paths ))
208
197
209
198
210
199
def get_nvidia_libdevice_ctk ():
211
- """Return path to directory containing the libdevice library.
212
- """
200
+ """Return path to directory containing the libdevice library."""
213
201
nvvm_ctk = get_nvidia_nvvm_ctk ()
214
202
if not nvvm_ctk :
215
203
return
216
204
nvvm_dir = os .path .dirname (nvvm_ctk )
217
- return os .path .join (nvvm_dir , ' libdevice' )
205
+ return os .path .join (nvvm_dir , " libdevice" )
218
206
219
207
220
208
def get_nvidia_cudalib_ctk ():
221
- """Return path to directory containing the shared libraries of cudatoolkit.
222
- """
209
+ """Return path to directory containing the shared libraries of cudatoolkit."""
223
210
nvvm_ctk = get_nvidia_nvvm_ctk ()
224
211
if not nvvm_ctk :
225
212
return
226
213
env_dir = os .path .dirname (os .path .dirname (nvvm_ctk ))
227
- subdir = ' bin' if IS_WIN32 else ' lib'
214
+ subdir = " bin" if IS_WIN32 else " lib"
228
215
return os .path .join (env_dir , subdir )
229
216
230
217
231
218
def get_nvidia_static_cudalib_ctk ():
232
- """Return path to directory containing the static libraries of cudatoolkit.
233
- """
219
+ """Return path to directory containing the static libraries of cudatoolkit."""
234
220
nvvm_ctk = get_nvidia_nvvm_ctk ()
235
221
if not nvvm_ctk :
236
222
return
237
223
238
224
if IS_WIN32 and ("Library" not in nvvm_ctk ):
239
225
# Location specific to CUDA 11.x packages on Windows
240
- dirs = (' Lib' , ' x64' )
226
+ dirs = (" Lib" , " x64" )
241
227
else :
242
228
# Linux, or Windows with CUDA 12.x packages
243
- dirs = (' lib' ,)
229
+ dirs = (" lib" ,)
244
230
245
231
env_dir = os .path .dirname (os .path .dirname (nvvm_ctk ))
246
232
return os .path .join (env_dir , * dirs )
@@ -251,10 +237,10 @@ def get_cuda_home(*subdirs):
251
237
If *subdirs* are the subdirectory name to be appended in the resulting
252
238
path.
253
239
"""
254
- cuda_home = os .environ .get (' CUDA_HOME' )
240
+ cuda_home = os .environ .get (" CUDA_HOME" )
255
241
if cuda_home is None :
256
242
# Try Windows CUDA installation without Anaconda
257
- cuda_home = os .environ .get (' CUDA_PATH' )
243
+ cuda_home = os .environ .get (" CUDA_PATH" )
258
244
if cuda_home is not None :
259
245
return os .path .join (cuda_home , * subdirs )
260
246
@@ -265,7 +251,7 @@ def _get_nvvm_path():
265
251
# The NVVM path is a directory, not a file
266
252
path = os .path .join (path , "libnvvm.so" )
267
253
else :
268
- candidates = find_lib (' nvvm' , path )
254
+ candidates = find_lib (" nvvm" , path )
269
255
path = max (candidates ) if candidates else None
270
256
return _env_path_tuple (by , path )
271
257
@@ -282,16 +268,16 @@ def get_cuda_paths():
282
268
Note: The result of the function is cached.
283
269
"""
284
270
# Check cache
285
- if hasattr (get_cuda_paths , ' _cached_result' ):
271
+ if hasattr (get_cuda_paths , " _cached_result" ):
286
272
return get_cuda_paths ._cached_result
287
273
else :
288
274
# Not in cache
289
275
d = {
290
- ' nvvm' : _get_nvvm_path (),
291
- ' libdevice' : _get_libdevice_paths (),
292
- ' cudalib_dir' : _get_cudalib_dir (),
293
- ' static_cudalib_dir' : _get_static_cudalib_dir (),
294
- ' include_dir' : _get_include_dir (),
276
+ " nvvm" : _get_nvvm_path (),
277
+ " libdevice" : _get_libdevice_paths (),
278
+ " cudalib_dir" : _get_cudalib_dir (),
279
+ " static_cudalib_dir" : _get_static_cudalib_dir (),
280
+ " include_dir" : _get_include_dir (),
295
281
}
296
282
# Cache result
297
283
get_cuda_paths ._cached_result = d
@@ -303,7 +289,7 @@ def get_debian_pkg_libdevice():
303
289
Return the Debian NVIDIA Maintainers-packaged libdevice location, if it
304
290
exists.
305
291
"""
306
- pkg_libdevice_location = ' /usr/lib/nvidia-cuda-toolkit/libdevice'
292
+ pkg_libdevice_location = " /usr/lib/nvidia-cuda-toolkit/libdevice"
307
293
if not os .path .exists (pkg_libdevice_location ):
308
294
return None
309
295
return pkg_libdevice_location
@@ -332,13 +318,10 @@ def get_current_cuda_target_name():
332
318
machine = platform .machine ()
333
319
334
320
if system == "Linux" :
335
- arch_to_targets = {
336
- 'x86_64' : 'x86_64-linux' ,
337
- 'aarch64' : 'sbsa-linux'
338
- }
321
+ arch_to_targets = {"x86_64" : "x86_64-linux" , "aarch64" : "sbsa-linux" }
339
322
elif system == "Windows" :
340
323
arch_to_targets = {
341
- ' AMD64' : ' x64' ,
324
+ " AMD64" : " x64" ,
342
325
}
343
326
else :
344
327
arch_to_targets = {}
@@ -351,35 +334,33 @@ def get_conda_include_dir():
351
334
Return the include directory in the current conda environment, if one
352
335
is active and it exists.
353
336
"""
354
- is_conda_env = os .path .exists (os .path .join (sys .prefix , ' conda-meta' ))
337
+ is_conda_env = os .path .exists (os .path .join (sys .prefix , " conda-meta" ))
355
338
if not is_conda_env :
356
339
return
357
340
358
341
if platform .system () == "Windows" :
359
- include_dir = os .path .join (
360
- sys .prefix , 'Library' , 'include'
361
- )
342
+ include_dir = os .path .join (sys .prefix , "Library" , "include" )
362
343
elif target_name := get_current_cuda_target_name ():
363
- include_dir = os .path .join (
364
- sys .prefix , 'targets' , target_name , 'include'
365
- )
344
+ include_dir = os .path .join (sys .prefix , "targets" , target_name , "include" )
366
345
else :
367
346
# A fallback when target cannot determined
368
347
# though usually it shouldn't.
369
- include_dir = os .path .join (sys .prefix , ' include' )
348
+ include_dir = os .path .join (sys .prefix , " include" )
370
349
371
- if (os .path .exists (include_dir ) and os .path .isdir (include_dir )
372
- and os .path .exists (os .path .join (include_dir ,
373
- 'cuda_device_runtime_api.h' ))):
350
+ if (
351
+ os .path .exists (include_dir )
352
+ and os .path .isdir (include_dir )
353
+ and os .path .exists (os .path .join (include_dir , "cuda_device_runtime_api.h" ))
354
+ ):
374
355
return include_dir
375
356
return
376
357
377
358
378
359
def _get_include_dir ():
379
360
"""Find the root include directory."""
380
361
options = [
381
- (' Conda environment (NVIDIA package)' , get_conda_include_dir ()),
382
- (' CUDA_INCLUDE_PATH Config Entry' , config .CUDA_INCLUDE_PATH ),
362
+ (" Conda environment (NVIDIA package)" , get_conda_include_dir ()),
363
+ (" CUDA_INCLUDE_PATH Config Entry" , config .CUDA_INCLUDE_PATH ),
383
364
# TODO: add others
384
365
]
385
366
by , include_dir = _find_valid_path (options )
0 commit comments