Skip to content

Commit 516ec3f

Browse files
committed
fix(loader): improve Windows DLL search path handling and diagnostics
- Remove duplicated Windows DLL directory registration logic - Add optional CUDA, HIP, and Vulkan runtime DLL search paths - Keep bundled library paths with correct priority order for loading - Add comments explaining DLL search path ordering behavior - Add load source diagnostics for system and bundled libraries - Improve visibility when debugging shared library loading issues Signed-off-by: JamePeng <jame_peng@sina.com>
1 parent 6c954f4 commit 516ec3f

1 file changed

Lines changed: 29 additions & 12 deletions

File tree

llama_cpp/_ctypes_extensions.py

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,8 @@ def load_shared_library(lib_base_name: str, base_paths: Union[pathlib.Path, list
9090

9191
# Add the library directory to the DLL search path on Windows (if needed)
9292
if sys.platform == "win32":
93-
for base_path in base_paths:
94-
p = pathlib.Path(base_path)
95-
if p.exists() and p.is_dir():
96-
os.add_dll_directory(str(p))
97-
os.environ["PATH"] = str(p) + os.pathsep + os.environ["PATH"]
9893

99-
if sys.platform == "win32" and sys.version_info >= (3, 9):
100-
for base_path in base_paths:
101-
p = pathlib.Path(base_path)
102-
if p.exists() and p.is_dir():
103-
os.add_dll_directory(str(p))
94+
# Add CUDA runtime DLL directories if CUDA is available.
10495
if "CUDA_PATH" in os.environ:
10596
cuda_path = os.environ["CUDA_PATH"]
10697
sub_dirs_to_add = [
@@ -114,14 +105,36 @@ def load_shared_library(lib_base_name: str, base_paths: Union[pathlib.Path, list
114105
if os.path.exists(full_path):
115106
os.add_dll_directory(full_path)
116107

108+
# Add HIP runtime DLL directories when HIP backend is available.
117109
if "HIP_PATH" in os.environ:
118110
os.add_dll_directory(os.path.join(os.environ["HIP_PATH"], "bin"))
119111
os.add_dll_directory(os.path.join(os.environ["HIP_PATH"], "lib"))
120112

113+
# Add Vulkan SDK DLL directories when Vulkan backend is enabled.
121114
if "VULKAN_SDK" in os.environ:
122115
os.add_dll_directory(os.path.join(os.environ["VULKAN_SDK"], "Bin"))
123116
os.add_dll_directory(os.path.join(os.environ["VULKAN_SDK"], "Lib"))
124117

118+
# Add package-provided library directories.
119+
#
120+
# The paths are added in reverse order intentionally.
121+
# This ensures that the first entry in base_paths gets prepended
122+
# to PATH last, making it the highest priority search location.
123+
#
124+
# Example:
125+
# base_paths = [
126+
# package/lib,
127+
# package/bin,
128+
# ]
129+
#
130+
# After reversed iteration:
131+
# PATH = package/lib;package/bin;...
132+
for base_path in reversed(base_paths):
133+
p = pathlib.Path(base_path)
134+
if p.exists() and p.is_dir():
135+
os.add_dll_directory(str(p))
136+
os.environ["PATH"] = str(p) + os.pathsep + os.environ["PATH"]
137+
125138
cdll_args["winmode"] = ctypes.RTLD_GLOBAL
126139

127140
errors = []
@@ -130,7 +143,9 @@ def load_shared_library(lib_base_name: str, base_paths: Union[pathlib.Path, list
130143
lib_path = find_library(lib_base_name)
131144
if lib_path:
132145
try:
133-
return ctypes.CDLL(lib_path, **cdll_args)
146+
lib = ctypes.CDLL(lib_path, **cdll_args)
147+
print(f"[llama-cpp-python].find_library: loaded library from {lib_path}")
148+
return lib
134149
except Exception as e:
135150
errors.append(f"{lib_path}: {e}")
136151

@@ -141,7 +156,9 @@ def load_shared_library(lib_base_name: str, base_paths: Union[pathlib.Path, list
141156

142157
if lib_path.exists():
143158
try:
144-
return ctypes.CDLL(str(lib_path), **cdll_args)
159+
lib = ctypes.CDLL(str(lib_path), **cdll_args)
160+
print(f"[llama-cpp-python].provided_path: loaded library from {lib_path}")
161+
return lib
145162
except Exception as e:
146163
errors.append(f"{lib_path}: {e}")
147164

0 commit comments

Comments
 (0)