Skip to content

First version of cuda.bindings.path_finder #447

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 79 commits into from
Apr 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
79 commits
Select commit Hold shift + click to select a range
2480389
Unmodified copies of:
rwgk Feb 12, 2025
a0b8d3c
Add Forked from URLs.
rwgk Feb 12, 2025
628f9e6
Merge branch 'main' into find_libnvvm
rwgk Feb 12, 2025
8439abc
Strip down cuda_paths.py to minimum required for `_get_nvvm_path()`
rwgk Feb 12, 2025
586d209
ruff auto-fixes (NO manual changes)
rwgk Feb 12, 2025
91b6b99
Make `get_nvvm_path()` a pubic API (i.e. remove leading underscore).
rwgk Feb 12, 2025
322f252
Merge branch 'main' into find_libnvvm
rwgk Mar 11, 2025
2d268b5
Merge branch 'main' into find_libnvvm
rwgk Mar 19, 2025
519dc11
Merge branch 'main' into find_libnvvm
rwgk Mar 19, 2025
d31920c
Fetch numba-cuda/numba_cuda/numba/cuda/cuda_paths.py from https://git…
rwgk Mar 19, 2025
ed0ebb3
ruff format NO MANUAL CHANGES
rwgk Mar 19, 2025
0c5aca5
Minimal changes to adapt numba-cuda/numba_cuda/numba/cuda/cuda_paths.…
rwgk Mar 19, 2025
ac8921d
Rename ecosystem/cuda_paths.py -> path_finder.py
rwgk Mar 20, 2025
1c217db
Plug cuda.bindings.path_finder into cuda/bindings/_internal/nvvm_linu…
rwgk Mar 21, 2025
3655d70
Plug cuda.bindings.path_finder into cuda/bindings/_internal/nvjitlink…
rwgk Mar 22, 2025
4133132
Fix `os.path.exists(None)` issue:
rwgk Mar 22, 2025
746cee0
Fix another `os.path.exists(None)` issue:
rwgk Mar 22, 2025
08c3041
Change "/lib64/" → "/lib/" in nvjitlink_linux.pyx
rwgk Mar 25, 2025
02694c7
nvjitlink_linux.pyx load_library() enhancements, mainly to avoid os.p…
rwgk Mar 25, 2025
29def61
Merge branch 'main' into find_libnvvm
rwgk Mar 25, 2025
4e99722
Add missing f-string f
rwgk Mar 25, 2025
76aa826
Add back get_nvjitlink_dso_version_suffix() call.
rwgk Mar 25, 2025
f8b3dd5
pytest -ra -s -v
rwgk Mar 25, 2025
0ea73b1
Rewrite nvjitlink_linux.pyx load_library() to produce detailed error …
rwgk Mar 25, 2025
4e0ec81
Attach listdir output to "Unable to load" exception message.
rwgk Mar 25, 2025
e421b48
Guard os.listdir() call with os.path.isdir()
rwgk Mar 26, 2025
1239be9
Fix logic error in nvjitlink_linux.pyx load_library()
rwgk Mar 26, 2025
20bbd4e
Merge branch 'main' into find_libnvvm
rwgk Mar 26, 2025
e82c878
Move path_finder.py to _path_finder_utils/cuda_paths.py, import only …
rwgk Mar 26, 2025
5bd23ec
Add find_nvidia_dynamic_library() and use from nvjitlink_linux.pyx, n…
rwgk Mar 26, 2025
ec02838
Fix oversight in _find_using_lib_dir()
rwgk Mar 27, 2025
5d8b58b
Also look for versioned library in _find_using_nvidia_lib_dirs()
rwgk Mar 27, 2025
7fd7cba
glob.glob() Python 3.9 compatibility
rwgk Mar 27, 2025
269b7f8
Reduce build-and-test.yml to Windows-only, Python 3.12 only.
rwgk Mar 31, 2025
b0db24f
Comment out `if: ${{ github.repository_owner == nvidia }}`
rwgk Mar 31, 2025
2a7780c
Revert "Comment out `if: ${{ github.repository_owner == nvidia }}`"
rwgk Mar 31, 2025
c2136ea
Add back `linux-64` `host-platform`
rwgk Mar 31, 2025
1bb7151
Rewrite load_library() in nvjitlink_windows.pyx to use path_finder.fi…
rwgk Mar 31, 2025
00466f8
Revert "Rewrite load_library() in nvjitlink_windows.pyx to use path_f…
rwgk Mar 31, 2025
389cd5a
Add _inspect_environment() in find_nvidia_dynamic_library.py, call fr…
rwgk Mar 31, 2025
1112fce
Add & use _find_dll_using_nvidia_bin_dirs(), _find_dll_using_cudalib_…
rwgk Apr 1, 2025
eb0fe30
Fix silly oversight: forgot to undo experimental change.
rwgk Apr 1, 2025
8fd33bf
Also reduce test test-linux matrix.
rwgk Apr 1, 2025
667d3ed
Reimplement load_library() functions in nvjitlink_windows.pyx, nvvm_w…
rwgk Apr 2, 2025
e4a4849
Factor out load_nvidia_dynamic_library() from _internal/nvjitlink_lin…
rwgk Apr 2, 2025
a140f5b
Generalize load_nvidia_dynamic_library.py to also work under Windows.
rwgk Apr 2, 2025
da417da
Add `void*` return type to load_library() implementations in _interna…
rwgk Apr 2, 2025
feed101
Resolve cython error: object handle vs `void*` handle
rwgk Apr 2, 2025
870f2e9
Resolve another cython error: `void*` handle vs `intptr_t` handle
rwgk Apr 2, 2025
f14d76b
Resolve signed/unsigned runtime error. Use uintptr_t consistently.
rwgk Apr 2, 2025
19c50f8
Change <void*><uintptr_t>win32api.GetProcAddress` back to `intptr_t`.…
rwgk Apr 2, 2025
93a66b0
Use win32api.LoadLibrary() instead of ctypes.windll.kernel32.LoadLibr…
rwgk Apr 2, 2025
1944e4c
Remove debug print statements.
rwgk Apr 2, 2025
28cd334
Remove some cruft.
rwgk Apr 2, 2025
491cda1
Trivial renaming of variables. No functional changes.
rwgk Apr 3, 2025
cc6113c
Revert debug changes under .github/workflows
rwgk Apr 3, 2025
eaeb836
Rename _path_finder_utils → _path_finder
rwgk Apr 3, 2025
50b9b7e
Merge branch 'main' into find_libnvvm
rwgk Apr 4, 2025
1b1139c
Remove LD_LIBRARY_PATH in fetch_ctk/action.yml
rwgk Apr 4, 2025
a603ef8
Linux: First try using the platform-specific dynamic loader search me…
rwgk Apr 4, 2025
b5dcd67
Merge branch 'main' into find_libnvvm
rwgk Apr 4, 2025
daae8e5
Add _windows_load_with_dll_basename()
rwgk Apr 5, 2025
aaa6aff
Revert "Revert debug changes under .github/workflows"
rwgk Apr 5, 2025
6262afc
Merge branch 'main' into find_libnvvm
rwgk Apr 6, 2025
69967c7
Add debug prints in load_nvidia_dynamic_library()
rwgk Apr 6, 2025
db571f4
Report dlopen error for libnvrtc.so.12
rwgk Apr 6, 2025
2244556
print("\nLOOOK dlfcn.dlopen('libnvrtc.so.12', dlfcn.RTLD_NOW)", flush…
rwgk Apr 6, 2025
fdcd195
Revert "Remove LD_LIBRARY_PATH in fetch_ctk/action.yml"
rwgk Apr 6, 2025
d12cbf5
Only remove ${CUDA_PATH}/nvvm/lib64 from LD_LIBRARY_PATH
rwgk Apr 6, 2025
14c72cc
Use path_finder.load_nvidia_dynamic_library("nvrtc") from cuda/bindin…
rwgk Apr 7, 2025
43abec8
Somewhat ad hoc heuristics for nvidia_cuda_nvrtc wheels.
rwgk Apr 7, 2025
bff8cf0
Remove LD_LIBRARY_PATH entirely from .github/actions/fetch_ctk/action…
rwgk Apr 7, 2025
dcc802a
Remove CUDA_PATH\nvvm\bin in .github/workflows/test-wheel-windows.yml
rwgk Apr 7, 2025
74d3cfb
Revert "Remove LD_LIBRARY_PATH entirely from .github/actions/fetch_ct…
rwgk Apr 8, 2025
5d6339d
Revert "Somewhat ad hoc heuristics for nvidia_cuda_nvrtc wheels."
rwgk Apr 8, 2025
ba093f5
Restore cuda/bindings/_bindings/cynvrtc.pyx.in as-is on main
rwgk Apr 8, 2025
332e15c
Remove debug print from load_nvidia_dynamic_library.py
rwgk Apr 8, 2025
8f69f83
Reapply "Revert debug changes under .github/workflows"
rwgk Apr 8, 2025
81eb97c
Merge branch 'main' into find_libnvvm
rwgk Apr 9, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/actions/fetch_ctk/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,4 @@ runs:
echo "CUDA_PATH=${CUDA_PATH}" >> $GITHUB_ENV
echo "CUDA_HOME=${CUDA_PATH}" >> $GITHUB_ENV
echo "${CUDA_PATH}/bin" >> $GITHUB_PATH
echo "LD_LIBRARY_PATH=${LD_LIBRARY_PATH:-}:${CUDA_PATH}/lib:${CUDA_PATH}/nvvm/lib64" >> $GITHUB_ENV
echo "LD_LIBRARY_PATH=${LD_LIBRARY_PATH:-}:${CUDA_PATH}/lib" >> $GITHUB_ENV
7 changes: 0 additions & 7 deletions .github/workflows/test-wheel-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,6 @@ jobs:
method: 'network'
sub-packages: ${{ env.MINI_CTK_DEPS }}

- name: Update PATH
if: ${{ inputs.local-ctk == '1' }}
run: |
# mimics actual CTK installation
echo $PATH
echo "$env:CUDA_PATH\nvvm\bin" >> $env:GITHUB_PATH

- name: Run cuda.bindings tests
if: ${{ env.SKIP_CUDA_BINDINGS_TEST == '0' }}
run: |
Expand Down
20 changes: 6 additions & 14 deletions cuda_bindings/cuda/bindings/_internal/nvjitlink_linux.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
#
# This code was automatically generated across versions from 12.0.1 to 12.8.0. Do not modify it directly.

from libc.stdint cimport intptr_t

from .utils cimport get_nvjitlink_dso_version_suffix
from libc.stdint cimport intptr_t, uintptr_t

from .utils import FunctionNotFoundError, NotSupportedError

from cuda.bindings import path_finder

###############################################################################
# Extern
###############################################################################
Expand Down Expand Up @@ -52,17 +52,9 @@ cdef void* __nvJitLinkGetInfoLog = NULL
cdef void* __nvJitLinkVersion = NULL


cdef void* load_library(const int driver_ver) except* with gil:
cdef void* handle
for suffix in get_nvjitlink_dso_version_suffix(driver_ver):
so_name = "libnvJitLink.so" + (f".{suffix}" if suffix else suffix)
handle = dlopen(so_name.encode(), RTLD_NOW | RTLD_GLOBAL)
if handle != NULL:
break
else:
err_msg = dlerror()
raise RuntimeError(f'Failed to dlopen libnvJitLink ({err_msg.decode()})')
return handle
cdef void* load_library(int driver_ver) except* with gil:
cdef uintptr_t handle = path_finder.load_nvidia_dynamic_library("nvJitLink")
return <void*>handle


cdef int _check_or_init_nvjitlink() except -1 nogil:
Expand Down
63 changes: 8 additions & 55 deletions cuda_bindings/cuda/bindings/_internal/nvjitlink_windows.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@

from libc.stdint cimport intptr_t

from .utils cimport get_nvjitlink_dso_version_suffix

from .utils import FunctionNotFoundError, NotSupportedError

import os
import site
from cuda.bindings import path_finder

import win32api

Expand Down Expand Up @@ -42,54 +39,9 @@ cdef void* __nvJitLinkGetInfoLog = NULL
cdef void* __nvJitLinkVersion = NULL


cdef inline list get_site_packages():
return [site.getusersitepackages()] + site.getsitepackages()


cdef load_library(const int driver_ver):
handle = 0

for suffix in get_nvjitlink_dso_version_suffix(driver_ver):
if len(suffix) == 0:
continue
dll_name = f"nvJitLink_{suffix}0_0.dll"

# First check if the DLL has been loaded by 3rd parties
try:
handle = win32api.GetModuleHandle(dll_name)
except:
pass
else:
break

# Next, check if DLLs are installed via pip
for sp in get_site_packages():
mod_path = os.path.join(sp, "nvidia", "nvJitLink", "bin")
if not os.path.isdir(mod_path):
continue
os.add_dll_directory(mod_path)
try:
handle = win32api.LoadLibraryEx(
# Note: LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR needs an abs path...
os.path.join(mod_path, dll_name),
0, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS | LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR)
except:
pass
else:
break

# Finally, try default search
try:
handle = win32api.LoadLibrary(dll_name)
except:
pass
else:
break
else:
raise RuntimeError('Failed to load nvJitLink')

assert handle != 0
return handle
cdef void* load_library(int driver_ver) except* with gil:
cdef intptr_t handle = path_finder.load_nvidia_dynamic_library("nvJitLink")
return <void*>handle


cdef int _check_or_init_nvjitlink() except -1 nogil:
Expand All @@ -98,23 +50,24 @@ cdef int _check_or_init_nvjitlink() except -1 nogil:
return 0

cdef int err, driver_ver
cdef intptr_t handle
with gil:
# Load driver to check version
try:
handle = win32api.LoadLibraryEx("nvcuda.dll", 0, LOAD_LIBRARY_SEARCH_SYSTEM32)
nvcuda_handle = win32api.LoadLibraryEx("nvcuda.dll", 0, LOAD_LIBRARY_SEARCH_SYSTEM32)
except Exception as e:
raise NotSupportedError(f'CUDA driver is not found ({e})')
global __cuDriverGetVersion
if __cuDriverGetVersion == NULL:
__cuDriverGetVersion = <void*><intptr_t>win32api.GetProcAddress(handle, 'cuDriverGetVersion')
__cuDriverGetVersion = <void*><intptr_t>win32api.GetProcAddress(nvcuda_handle, 'cuDriverGetVersion')
if __cuDriverGetVersion == NULL:
raise RuntimeError('something went wrong')
err = (<int (*)(int*) noexcept nogil>__cuDriverGetVersion)(&driver_ver)
if err != 0:
raise RuntimeError('something went wrong')

# Load library
handle = load_library(driver_ver)
handle = <intptr_t>load_library(driver_ver)

# Load function
global __nvJitLinkCreate
Expand Down
18 changes: 5 additions & 13 deletions cuda_bindings/cuda/bindings/_internal/nvvm_linux.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
#
# This code was automatically generated across versions from 11.0.3 to 12.8.0. Do not modify it directly.

from libc.stdint cimport intptr_t

from .utils cimport get_nvvm_dso_version_suffix
from libc.stdint cimport intptr_t, uintptr_t

from .utils import FunctionNotFoundError, NotSupportedError

from cuda.bindings import path_finder

###############################################################################
# Extern
###############################################################################
Expand Down Expand Up @@ -51,16 +51,8 @@ cdef void* __nvvmGetProgramLog = NULL


cdef void* load_library(const int driver_ver) except* with gil:
cdef void* handle
for suffix in get_nvvm_dso_version_suffix(driver_ver):
so_name = "libnvvm.so" + (f".{suffix}" if suffix else suffix)
handle = dlopen(so_name.encode(), RTLD_NOW | RTLD_GLOBAL)
if handle != NULL:
break
else:
err_msg = dlerror()
raise RuntimeError(f'Failed to dlopen libnvvm ({err_msg.decode()})')
return handle
cdef uintptr_t handle = path_finder.load_nvidia_dynamic_library("nvvm")
return <void*>handle


cdef int _check_or_init_nvvm() except -1 nogil:
Expand Down
63 changes: 8 additions & 55 deletions cuda_bindings/cuda/bindings/_internal/nvvm_windows.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@

from libc.stdint cimport intptr_t

from .utils cimport get_nvvm_dso_version_suffix

from .utils import FunctionNotFoundError, NotSupportedError

import os
import site
from cuda.bindings import path_finder

import win32api

Expand Down Expand Up @@ -40,54 +37,9 @@ cdef void* __nvvmGetProgramLogSize = NULL
cdef void* __nvvmGetProgramLog = NULL


cdef inline list get_site_packages():
return [site.getusersitepackages()] + site.getsitepackages()


cdef load_library(const int driver_ver):
handle = 0

for suffix in get_nvvm_dso_version_suffix(driver_ver):
if len(suffix) == 0:
continue
dll_name = "nvvm64_40_0"

# First check if the DLL has been loaded by 3rd parties
try:
handle = win32api.GetModuleHandle(dll_name)
except:
pass
else:
break

# Next, check if DLLs are installed via pip
for sp in get_site_packages():
mod_path = os.path.join(sp, "nvidia", "cuda_nvcc", "nvvm", "bin")
if not os.path.isdir(mod_path):
continue
os.add_dll_directory(mod_path)
try:
handle = win32api.LoadLibraryEx(
# Note: LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR needs an abs path...
os.path.join(mod_path, dll_name),
0, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS | LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR)
except:
pass
else:
break

# Finally, try default search
try:
handle = win32api.LoadLibrary(dll_name)
except:
pass
else:
break
else:
raise RuntimeError('Failed to load nvvm')

assert handle != 0
return handle
cdef void* load_library(int driver_ver) except* with gil:
cdef intptr_t handle = path_finder.load_nvidia_dynamic_library("nvvm")
return <void*>handle


cdef int _check_or_init_nvvm() except -1 nogil:
Expand All @@ -96,23 +48,24 @@ cdef int _check_or_init_nvvm() except -1 nogil:
return 0

cdef int err, driver_ver
cdef intptr_t handle
with gil:
# Load driver to check version
try:
handle = win32api.LoadLibraryEx("nvcuda.dll", 0, LOAD_LIBRARY_SEARCH_SYSTEM32)
nvcuda_handle = win32api.LoadLibraryEx("nvcuda.dll", 0, LOAD_LIBRARY_SEARCH_SYSTEM32)
except Exception as e:
raise NotSupportedError(f'CUDA driver is not found ({e})')
global __cuDriverGetVersion
if __cuDriverGetVersion == NULL:
__cuDriverGetVersion = <void*><intptr_t>win32api.GetProcAddress(handle, 'cuDriverGetVersion')
__cuDriverGetVersion = <void*><intptr_t>win32api.GetProcAddress(nvcuda_handle, 'cuDriverGetVersion')
if __cuDriverGetVersion == NULL:
raise RuntimeError('something went wrong')
err = (<int (*)(int*) noexcept nogil>__cuDriverGetVersion)(&driver_ver)
if err != 0:
raise RuntimeError('something went wrong')

# Load library
handle = load_library(driver_ver)
handle = <intptr_t>load_library(driver_ver)

# Load function
global __nvvmVersion
Expand Down
3 changes: 0 additions & 3 deletions cuda_bindings/cuda/bindings/_internal/utils.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,3 @@ cdef int get_nested_resource_ptr(nested_resource[ResT] &in_out_ptr, object obj,

cdef bint is_nested_sequence(data)
cdef void* get_buffer_pointer(buf, Py_ssize_t size, readonly=*) except*

cdef tuple get_nvjitlink_dso_version_suffix(int driver_ver)
cdef tuple get_nvvm_dso_version_suffix(int driver_ver)
14 changes: 0 additions & 14 deletions cuda_bindings/cuda/bindings/_internal/utils.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -127,17 +127,3 @@ cdef int get_nested_resource_ptr(nested_resource[ResT] &in_out_ptr, object obj,
class FunctionNotFoundError(RuntimeError): pass

class NotSupportedError(RuntimeError): pass


cdef tuple get_nvjitlink_dso_version_suffix(int driver_ver):
if 12000 <= driver_ver < 13000:
return ('12', '')
raise NotSupportedError(f'CUDA driver version {driver_ver} is not supported')


cdef tuple get_nvvm_dso_version_suffix(int driver_ver):
if 11000 <= driver_ver < 11020:
return ('3', '')
if 11020 <= driver_ver < 13000:
return ('4', '')
raise NotSupportedError(f'CUDA driver version {driver_ver} is not supported')
Loading