Prefer Python CUDA libs over implicit system paths#3182
Open
sbhavani wants to merge 2 commits into
Open
Conversation
Signed-off-by: Santosh Bhavani <santosh.bhavani@live.com>
0e4b923 to
dd365a5
Compare
Contributor
timmoon10
reviewed
Jul 10, 2026
|
|
||
| @functools.lru_cache(maxsize=None) | ||
| def _load_cuda_library_from_system(lib_name: str): | ||
| def _load_cuda_library_from_system( |
Member
There was a problem hiding this comment.
This design is kind of convoluted and overtuned to the use-case in _load_cuda_library. Now that we've separated the envvar search from the /usr/local/cuda+ dynamic linker path, there's no reason for them to live in the same function. I'd suggest splitting the function up into fine-grained helper functions that _load_cuda_library can call in any order:
def _try_load_library_from_paths(file_name: str, paths: Iterable[str]) -> Optional[ctypes.CDLL]:
for path in paths:
libs = glob.glob(f"{path}/**/{file_name}*", recursive=True)
libs = [lib for lib in libs if "stub" not in lib]
libs.sort(reverse=True, key=os.path.basename)
if libs:
return ctypes.CDLL(libs[0], mode=ctypes.RTLD_GLOBAL)
return None
@functools.lru_cache(maxsize=None)
def _try_load_cuda_library_from_envvars(lib_name: str) -> Optional[ctypes.CDLL]:
paths = [
os.environ.get(f"{lib_name.upper()}_HOME"),
os.environ.get(f"{lib_name.upper()}_PATH"),
os.environ.get("CUDA_HOME"),
os.environ.get("CUDA_PATH"),
]
paths = [p for p in paths if p is not None]
return _try_load_library_from_paths(
f"lib{lib_name}{_get_sys_extension()}",
paths,
)
@functools.lru_cache(maxsize=None)
def _try_load_cuda_library_from_default_path(lib_name: str) -> Optional[ctypes.CDLL]:
return _try_load_library_from_paths(
f"lib{lib_name}{_get_sys_extension()}",
["/usr/local/cuda"],
)
def _try_load_library_from_dynamic_linker(file_name: str)-> Optional[ctypes.CDLL]:
try:
return ctypes.CDLL(file_name, mode=ctypes.RTLD_GLOBAL)
except OSError:
return None
Collaborator
Author
There was a problem hiding this comment.
thanks for the suggestion! Just refactored the code into helpers
Signed-off-by: Santosh Bhavani <santosh.bhavani@live.com>
7f324a8 to
46dbba4
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Prefer CUDA runtime libraries from the active Python environment before falling back to implicit system locations such as
/usr/local/cudaor dynamic linker lookup.Explicit user configuration via
CUDNN_HOME,CUDNN_PATH,CUDA_HOME, orCUDA_PATHstill takes highest priority.Fixes #2147.
Type of change
Changes
CUDNN_HOMEover Python packageChecklist: