|
| 1 | +# Copyright 2024-2025 NVIDIA Corporation. All rights reserved. |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: LicenseRef-NVIDIA-SOFTWARE-LICENSE |
| 4 | + |
| 5 | +import functools |
| 6 | +import os |
| 7 | + |
| 8 | +from .cuda_paths import get_cuda_paths |
| 9 | +from .find_nvidia_lib_dirs import find_nvidia_lib_dirs |
| 10 | + |
| 11 | + |
| 12 | +def _find_using_nvidia_lib_dirs(so_basename, error_messages, attachments): |
| 13 | + for lib_dir in find_nvidia_lib_dirs(): |
| 14 | + so_name = os.path.join(lib_dir, so_basename) |
| 15 | + if os.path.isfile(so_name): |
| 16 | + return so_name |
| 17 | + error_messages.append(f"No such file: {so_name}") |
| 18 | + for lib_dir in find_nvidia_lib_dirs(): |
| 19 | + attachments.append(f" listdir({repr(lib_dir)}):") |
| 20 | + for node in sorted(os.listdir(lib_dir)): |
| 21 | + attachments.append(f" {node}") |
| 22 | + return None |
| 23 | + |
| 24 | + |
| 25 | +def _get_cuda_paths_info(key, error_messages): |
| 26 | + env_path_tuple = get_cuda_paths()[key] |
| 27 | + if not env_path_tuple: |
| 28 | + error_messages.append(f'Failure obtaining get_cuda_paths()["{key}"]') |
| 29 | + return None |
| 30 | + if not env_path_tuple.info: |
| 31 | + error_messages.append(f'Failure obtaining get_cuda_paths()["{key}"].info') |
| 32 | + return None |
| 33 | + return env_path_tuple.info |
| 34 | + |
| 35 | + |
| 36 | +def _find_using_lib_dir(so_basename, error_messages, attachments): |
| 37 | + lib_dir = _get_cuda_paths_info("cudalib_dir", error_messages) |
| 38 | + primary_so_dir = lib_dir + "/" |
| 39 | + candidate_so_dirs = [primary_so_dir] |
| 40 | + libs = ["/lib/", "/lib64/"] |
| 41 | + for _ in range(2): |
| 42 | + alt_dir = libs[0].join(primary_so_dir.rsplit(libs[1], 1)) |
| 43 | + if alt_dir not in candidate_so_dirs: |
| 44 | + candidate_so_dirs.append(alt_dir) |
| 45 | + libs.reverse() |
| 46 | + candidate_so_names = [so_dirname + so_basename for so_dirname in candidate_so_dirs] |
| 47 | + error_messages = [] |
| 48 | + for so_name in candidate_so_names: |
| 49 | + if os.path.isfile(so_name): |
| 50 | + return so_name |
| 51 | + error_messages.append(f"No such file: {so_name}") |
| 52 | + for so_dirname in candidate_so_dirs: |
| 53 | + attachments.append(f" listdir({repr(so_dirname)}):") |
| 54 | + if not os.path.isdir(so_dirname): |
| 55 | + attachments.append(" DIRECTORY DOES NOT EXIST") |
| 56 | + else: |
| 57 | + for node in sorted(os.listdir(so_dirname)): |
| 58 | + attachments.append(f" {node}") |
| 59 | + return None |
| 60 | + |
| 61 | + |
| 62 | +@functools.cache |
| 63 | +def find_nvidia_dynamic_library(libbasename): |
| 64 | + so_basename = f"lib{libbasename}.so" |
| 65 | + error_messages = [] |
| 66 | + attachments = [] |
| 67 | + so_name = _find_using_nvidia_lib_dirs(so_basename, error_messages, attachments) |
| 68 | + if so_name is None: |
| 69 | + if libbasename == "nvvm": |
| 70 | + so_name = _get_cuda_paths_info("nvvm", error_messages) |
| 71 | + else: |
| 72 | + so_name = _find_using_lib_dir(so_basename, error_messages, attachments) |
| 73 | + if so_name is None: |
| 74 | + attachments = "\n".join(attachments) |
| 75 | + raise RuntimeError(f"Unable to load {so_basename} from: {', '.join(error_messages)}\n{attachments}") |
| 76 | + return so_name |
0 commit comments