2020
2121
2222# Load the library
23- def load_shared_library (lib_base_name : str , base_path : pathlib .Path ):
24- """Platform independent shared library loader"""
25- # Searching for the library in the current directory under the name "libllama" (default name
26- # for llamacpp) and "llama" (default name for this repo)
27- lib_paths : List [ pathlib . Path ] = []
28- # Determine the file extension based on the platform
23+ def load_shared_library (lib_base_name : str , base_paths : Union [ pathlib .Path , list [ pathlib . Path ]] ):
24+ if isinstance ( base_paths , pathlib . Path ):
25+ base_paths = [ base_paths ]
26+
27+ lib_names = []
28+
2929 if sys .platform .startswith ("linux" ) or sys .platform .startswith ("freebsd" ):
30- lib_paths += [
31- base_path / f"lib{ lib_base_name } .so" ,
32- ]
30+ lib_names = [f"lib{ lib_base_name } .so" ]
3331 elif sys .platform == "darwin" :
34- lib_paths + = [
35- base_path / f"lib{ lib_base_name } .so " ,
36- base_path / f"lib{ lib_base_name } .dylib " ,
32+ lib_names = [
33+ f"lib{ lib_base_name } .dylib " ,
34+ f"lib{ lib_base_name } .so " ,
3735 ]
3836 elif sys .platform == "win32" :
39- lib_paths + = [
40- base_path / f"{ lib_base_name } .dll" ,
41- base_path / f"lib{ lib_base_name } .dll" ,
37+ lib_names = [
38+ f"{ lib_base_name } .dll" ,
39+ f"lib{ lib_base_name } .dll" ,
4240 ]
4341 else :
4442 raise RuntimeError ("Unsupported platform" )
@@ -47,11 +45,13 @@ def load_shared_library(lib_base_name: str, base_path: pathlib.Path):
4745
4846 # Add the library directory to the DLL search path on Windows (if needed)
4947 if sys .platform == "win32" :
50- os .add_dll_directory (str (base_path ))
48+ for base_path in base_paths :
49+ os .add_dll_directory (str (base_path ))
5150 os .environ ["PATH" ] = str (base_path ) + os .pathsep + os .environ ["PATH" ]
5251
53- if sys .platform == "win32" and sys .version_info >= (3 , 8 ):
54- os .add_dll_directory (str (base_path ))
52+ if sys .platform == "win32" and sys .version_info >= (3 , 9 ):
53+ for base_path in base_paths :
54+ os .add_dll_directory (str (base_path ))
5555 if "CUDA_PATH" in os .environ :
5656 cuda_path = os .environ ["CUDA_PATH" ]
5757 sub_dirs_to_add = [
@@ -75,16 +75,21 @@ def load_shared_library(lib_base_name: str, base_path: pathlib.Path):
7575
7676 cdll_args ["winmode" ] = ctypes .RTLD_GLOBAL
7777
78+ errors = []
79+
7880 # Try to load the shared library, handling potential errors
79- for lib_path in lib_paths :
80- if lib_path .exists ():
81- try :
82- return ctypes .CDLL (str (lib_path ), ** cdll_args ) # type: ignore
83- except Exception as e :
84- raise RuntimeError (f"Failed to load shared library '{ lib_path } ': { e } " )
85-
86- raise FileNotFoundError (
87- f"Shared library with base name '{ lib_base_name } ' not found"
81+ for base_path in base_paths :
82+ for lib_name in lib_names :
83+ lib_path = base_path / lib_name
84+ if lib_path .exists ():
85+ try :
86+ return ctypes .CDLL (str (lib_path ), ** cdll_args )
87+ except Exception as e :
88+ errors .append (f"{ lib_path } : { e } " )
89+
90+ raise RuntimeError (
91+ f"Failed to load '{ lib_base_name } ' from { base_paths } \n "
92+ + "\n " .join (errors )
8893 )
8994
9095
0 commit comments