@@ -645,7 +645,7 @@ public void Dispose() {}
645645 private List < IntPtr > dependencyHandles = new List < IntPtr > ( ) ;
646646 private static int debugLevelGlobal = 0 ;
647647 private static CharArrayCallback loggingCallbackGlobal = null ;
648- private string [ ] availableLibraries = null ;
648+ private List < Tuple < string , bool > > availableLibraries = null ;
649649 private int currentLibraryIndex = 0 ;
650650
651651 // Runtime lib
@@ -778,12 +778,20 @@ private string[] GetAvailableArchitectures(bool gpu)
778778
779779 private void LoadLibraries ( bool gpu )
780780 {
781- availableLibraries = GetAvailableArchitectures ( gpu ) ;
781+ availableLibraries = new List < Tuple < string , bool > > ( ) ;
782+ bool [ ] arch_options = gpu ? new bool [ ] { true , false } : new bool [ ] { false } ;
783+ foreach ( bool arch_gpu in arch_options )
784+ {
785+ string [ ] archs = GetAvailableArchitectures ( arch_gpu ) ;
786+ foreach ( string arch in archs ) availableLibraries . Add ( new Tuple < string , bool > ( arch , arch_gpu ) ) ;
787+ }
782788 currentLibraryIndex = - 1 ;
783789
784790 if ( ! TryNextLibrary ( ) )
785791 {
786- throw new InvalidOperationException ( $ "Failed to load any library. Available libraries: { string . Join ( ", " , availableLibraries ) } ") ;
792+ string libs = "" ;
793+ foreach ( Tuple < string , bool > arch in availableLibraries ) libs += arch . Item1 + ", " ;
794+ throw new InvalidOperationException ( $ "Failed to load any library. Available libraries: { libs . TrimEnd ( ',' , ' ' ) } ") ;
787795 }
788796 }
789797
@@ -824,9 +832,9 @@ public bool TryNextLibrary()
824832 libraryHandle = IntPtr . Zero ;
825833 }
826834
827- while ( ++ currentLibraryIndex < availableLibraries . Length )
835+ while ( ++ currentLibraryIndex < availableLibraries . Count )
828836 {
829- string library = availableLibraries [ currentLibraryIndex ] ;
837+ var ( library , is_gpu_library ) = availableLibraries [ currentLibraryIndex ] ;
830838 try
831839 {
832840 string libraryPath = FindLibrary ( library . Trim ( ) ) ;
@@ -839,6 +847,8 @@ public bool TryNextLibrary()
839847 libraryHandle = LibraryLoader . LoadLibrary ( libraryPath ) ;
840848
841849 LoadFunctionPointers ( ) ;
850+ if ( is_gpu_library && ! LLMService_Supports_GPU ( ) ) continue ;
851+
842852 architecture = library . Trim ( ) ;
843853 if ( debugLevelGlobal > 0 ) Console . WriteLine ( "Successfully loaded: " + libraryPath ) ;
844854 return true ;
@@ -885,6 +895,7 @@ private void LoadFunctionPointers()
885895 LLM_Debug = LibraryLoader . GetSymbolDelegate < LLM_Debug_Delegate > ( libraryHandle , "LLM_Debug" ) ;
886896 LLM_Logging_Callback = LibraryLoader . GetSymbolDelegate < LLM_Logging_Callback_Delegate > ( libraryHandle , "LLM_Logging_Callback" ) ;
887897 LLM_Logging_Stop = LibraryLoader . GetSymbolDelegate < LLM_Logging_Stop_Delegate > ( libraryHandle , "LLM_Logging_Stop" ) ;
898+ LLMService_Supports_GPU = LibraryLoader . GetSymbolDelegate < LLMService_Supports_GPU_Delegate > ( libraryHandle , "LLMService_Supports_GPU" ) ;
888899
889900 LLM_Enable_Reasoning_Internal = LibraryLoader . GetSymbolDelegate < LLM_Enable_Reasoning_Delegate > ( libraryHandle , "LLM_Enable_Reasoning" ) ;
890901 LLM_Apply_Template_Internal = LibraryLoader . GetSymbolDelegate < LLM_Apply_Template_Delegate > ( libraryHandle , "LLM_Apply_Template" ) ;
@@ -950,9 +961,13 @@ private void LoadFunctionPointers()
950961 [ UnmanagedFunctionPointer ( CallingConvention . Cdecl ) ]
951962 public delegate void LLM_Logging_Stop_Delegate ( ) ;
952963
964+ [ UnmanagedFunctionPointer ( CallingConvention . Cdecl ) ]
965+ public delegate bool LLMService_Supports_GPU_Delegate ( ) ;
966+
953967 public LLM_Debug_Delegate LLM_Debug ;
954968 public LLM_Logging_Callback_Delegate LLM_Logging_Callback ;
955969 public LLM_Logging_Stop_Delegate LLM_Logging_Stop ;
970+ public LLMService_Supports_GPU_Delegate LLMService_Supports_GPU ;
956971
957972 public static void Debug ( int debugLevel )
958973 {
0 commit comments