You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

NativeApi.Load.cs 19 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. using LLama.Exceptions;
  2. using System;
  3. using System.IO;
  4. using System.Runtime.InteropServices;
  5. using System.Text.Json;
  6. using System.Collections.Generic;
  7. namespace LLama.Native
  8. {
  9. public static partial class NativeApi
  10. {
  11. static NativeApi()
  12. {
  13. // Overwrite the Dll import resolver for this assembly. The resolver gets
  14. // called by the runtime every time that a call into a DLL is required. The
  15. // resolver returns the loaded DLL handle. This allows us to take control of
  16. // which llama.dll is used.
  17. SetDllImportResolver();
  18. // Immediately make a call which requires loading the llama DLL. This method call
  19. // can't fail unless the DLL hasn't been loaded.
  20. try
  21. {
  22. llama_empty_call();
  23. }
  24. catch (DllNotFoundException)
  25. {
  26. throw new RuntimeError("The native library cannot be correctly loaded. It could be one of the following reasons: \n" +
  27. "1. No LLamaSharp backend was installed. Please search LLamaSharp.Backend and install one of them. \n" +
  28. "2. You are using a device with only CPU but installed cuda backend. Please install cpu backend instead. \n" +
  29. "3. One of the dependency of the native library is missed. Please use `ldd` on linux, `dumpbin` on windows and `otool`" +
  30. "to check if all the dependency of the native library is satisfied. Generally you could find the libraries under your output folder.\n" +
  31. "4. Try to compile llama.cpp yourself to generate a libllama library, then use `LLama.Native.NativeLibraryConfig.WithLibrary` " +
  32. "to specify it at the very beginning of your code. For more informations about compilation, please refer to LLamaSharp repo on github.\n");
  33. }
  34. // Init llama.cpp backend
  35. llama_backend_init();
  36. }
  37. #if NET5_0_OR_GREATER
  38. private static IntPtr _loadedLlamaHandle;
  39. private static IntPtr _loadedLlavaSharedHandle;
  40. #endif
  41. private static void SetDllImportResolver()
  42. {
  43. // NativeLibrary is not available on older runtimes. We'll have to depend on
  44. // the normal runtime dll resolution there.
  45. #if NET5_0_OR_GREATER
  46. NativeLibrary.SetDllImportResolver(typeof(NativeApi).Assembly, (name, _, _) =>
  47. {
  48. if (name == "llama")
  49. {
  50. // If we've already loaded llama return the handle that was loaded last time.
  51. if (_loadedLlamaHandle != IntPtr.Zero)
  52. return _loadedLlamaHandle;
  53. // Try to load a preferred library, based on CPU feature detection
  54. _loadedLlamaHandle = TryLoadLibraries(LibraryName.Llama);
  55. return _loadedLlamaHandle;
  56. }
  57. if (name == "llava_shared")
  58. {
  59. // If we've already loaded llava return the handle that was loaded last time.
  60. if (_loadedLlavaSharedHandle != IntPtr.Zero)
  61. return _loadedLlavaSharedHandle;
  62. // Try to load a preferred library, based on CPU feature detection
  63. _loadedLlavaSharedHandle = TryLoadLibraries(LibraryName.LlavaShared);
  64. return _loadedLlavaSharedHandle;
  65. }
  66. // Return null pointer to indicate that nothing was loaded.
  67. return IntPtr.Zero;
  68. });
  69. #endif
  70. }
  71. private static void Log(string message, LLamaLogLevel level)
  72. {
  73. if (!enableLogging)
  74. return;
  75. if ((int)level > (int)logLevel)
  76. return;
  77. var fg = Console.ForegroundColor;
  78. var bg = Console.BackgroundColor;
  79. try
  80. {
  81. ConsoleColor color;
  82. string levelPrefix;
  83. if (level == LLamaLogLevel.Debug)
  84. {
  85. color = ConsoleColor.Cyan;
  86. levelPrefix = "[Debug]";
  87. }
  88. else if (level == LLamaLogLevel.Info)
  89. {
  90. color = ConsoleColor.Green;
  91. levelPrefix = "[Info]";
  92. }
  93. else if (level == LLamaLogLevel.Error)
  94. {
  95. color = ConsoleColor.Red;
  96. levelPrefix = "[Error]";
  97. }
  98. else
  99. {
  100. color = ConsoleColor.Yellow;
  101. levelPrefix = "[UNK]";
  102. }
  103. Console.ForegroundColor = color;
  104. Console.WriteLine($"{loggingPrefix} {levelPrefix} {message}");
  105. }
  106. finally
  107. {
  108. Console.ForegroundColor = fg;
  109. Console.BackgroundColor = bg;
  110. }
  111. }
  112. #region CUDA version
  113. private static int GetCudaMajorVersion()
  114. {
  115. string? cudaPath;
  116. string version = "";
  117. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  118. {
  119. cudaPath = Environment.GetEnvironmentVariable("CUDA_PATH");
  120. if (cudaPath is null)
  121. {
  122. return -1;
  123. }
  124. //Ensuring cuda bin path is reachable. Especially for MAUI environment.
  125. string cudaBinPath = Path.Combine(cudaPath, "bin");
  126. if (Directory.Exists(cudaBinPath))
  127. {
  128. AddDllDirectory(cudaBinPath);
  129. }
  130. version = GetCudaVersionFromPath(cudaPath);
  131. }
  132. else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
  133. {
  134. // Try the default first
  135. cudaPath = "/usr/local/bin/cuda";
  136. version = GetCudaVersionFromPath(cudaPath);
  137. if (string.IsNullOrEmpty(version))
  138. {
  139. cudaPath = Environment.GetEnvironmentVariable("LD_LIBRARY_PATH");
  140. if (cudaPath is null)
  141. {
  142. return -1;
  143. }
  144. foreach (var path in cudaPath.Split(':'))
  145. {
  146. version = GetCudaVersionFromPath(Path.Combine(path, ".."));
  147. if (string.IsNullOrEmpty(version))
  148. {
  149. break;
  150. }
  151. }
  152. }
  153. }
  154. if (string.IsNullOrEmpty(version))
  155. return -1;
  156. version = version.Split('.')[0];
  157. if (int.TryParse(version, out var majorVersion))
  158. return majorVersion;
  159. return -1;
  160. }
  161. private static string GetCudaVersionFromPath(string cudaPath)
  162. {
  163. try
  164. {
  165. string json = File.ReadAllText(Path.Combine(cudaPath, cudaVersionFile));
  166. using (JsonDocument document = JsonDocument.Parse(json))
  167. {
  168. JsonElement root = document.RootElement;
  169. JsonElement cublasNode = root.GetProperty("libcublas");
  170. JsonElement versionNode = cublasNode.GetProperty("version");
  171. if (versionNode.ValueKind == JsonValueKind.Undefined)
  172. {
  173. return string.Empty;
  174. }
  175. return versionNode.GetString() ?? "";
  176. }
  177. }
  178. catch (Exception)
  179. {
  180. return string.Empty;
  181. }
  182. }
  183. #endregion
  184. #if NET6_0_OR_GREATER
  185. private static IEnumerable<string> GetLibraryTryOrder(NativeLibraryConfig.Description configuration)
  186. {
  187. var loadingName = configuration.Library.GetLibraryName();
  188. Log($"Loading library: '{loadingName}'", LLamaLogLevel.Debug);
  189. // Get platform specific parts of the path (e.g. .so/.dll/.dylib, libName prefix or not)
  190. GetPlatformPathParts(out var platform, out var os, out var ext, out var libPrefix);
  191. Log($"Detected OS Platform: '{platform}'", LLamaLogLevel.Info);
  192. Log($"Detected OS string: '{os}'", LLamaLogLevel.Debug);
  193. Log($"Detected extension string: '{ext}'", LLamaLogLevel.Debug);
  194. Log($"Detected prefix string: '{libPrefix}'", LLamaLogLevel.Debug);
  195. if (configuration.UseCuda && (platform == OSPlatform.Windows || platform == OSPlatform.Linux))
  196. {
  197. var cudaVersion = GetCudaMajorVersion();
  198. Log($"Detected cuda major version {cudaVersion}.", LLamaLogLevel.Info);
  199. if (cudaVersion == -1 && !configuration.AllowFallback)
  200. {
  201. // if check skipped, we just try to load cuda libraries one by one.
  202. if (configuration.SkipCheck)
  203. {
  204. yield return GetCudaLibraryPath(loadingName, "cuda12");
  205. yield return GetCudaLibraryPath(loadingName, "cuda11");
  206. }
  207. else
  208. {
  209. throw new RuntimeError("Configured to load a cuda library but no cuda detected on your device.");
  210. }
  211. }
  212. else if (cudaVersion == 11)
  213. {
  214. yield return GetCudaLibraryPath(loadingName, "cuda11");
  215. }
  216. else if (cudaVersion == 12)
  217. {
  218. yield return GetCudaLibraryPath(loadingName, "cuda12");
  219. }
  220. else if (cudaVersion > 0)
  221. {
  222. throw new RuntimeError($"Cuda version {cudaVersion} hasn't been supported by LLamaSharp, please open an issue for it.");
  223. }
  224. // otherwise no cuda detected but allow fallback
  225. }
  226. // Add the CPU/Metal libraries
  227. if (platform == OSPlatform.OSX)
  228. {
  229. // On Mac it's very simple, there's no AVX to consider.
  230. yield return GetMacLibraryPath(loadingName);
  231. }
  232. else
  233. {
  234. if (configuration.AllowFallback)
  235. {
  236. // Try all of the AVX levels we can support.
  237. if (configuration.AvxLevel >= NativeLibraryConfig.AvxLevel.Avx512)
  238. yield return GetAvxLibraryPath(loadingName, NativeLibraryConfig.AvxLevel.Avx512);
  239. if (configuration.AvxLevel >= NativeLibraryConfig.AvxLevel.Avx2)
  240. yield return GetAvxLibraryPath(loadingName, NativeLibraryConfig.AvxLevel.Avx2);
  241. if (configuration.AvxLevel >= NativeLibraryConfig.AvxLevel.Avx)
  242. yield return GetAvxLibraryPath(loadingName, NativeLibraryConfig.AvxLevel.Avx);
  243. yield return GetAvxLibraryPath(loadingName, NativeLibraryConfig.AvxLevel.None);
  244. }
  245. else
  246. {
  247. // Fallback is not allowed - use the exact specified AVX level
  248. yield return GetAvxLibraryPath(loadingName, configuration.AvxLevel);
  249. }
  250. }
  251. }
  252. private static string GetMacLibraryPath(string libraryName)
  253. {
  254. GetPlatformPathParts(out _, out var os, out var fileExtension, out var libPrefix);
  255. return $"runtimes/{os}/native/{libPrefix}{libraryName}{fileExtension}";
  256. }
  257. /// <summary>
  258. /// Given a CUDA version and some path parts, create a complete path to the library file
  259. /// </summary>
  260. /// <param name="libraryName">Library being loaded (e.g. "llama")</param>
  261. /// <param name="cuda">CUDA version (e.g. "cuda11")</param>
  262. /// <returns></returns>
  263. private static string GetCudaLibraryPath(string libraryName, string cuda)
  264. {
  265. GetPlatformPathParts(out _, out var os, out var fileExtension, out var libPrefix);
  266. return $"runtimes/{os}/native/{cuda}/{libPrefix}{libraryName}{fileExtension}";
  267. }
  268. /// <summary>
  269. /// Given an AVX level and some path parts, create a complete path to the library file
  270. /// </summary>
  271. /// <param name="libraryName">Library being loaded (e.g. "llama")</param>
  272. /// <param name="avx"></param>
  273. /// <returns></returns>
  274. private static string GetAvxLibraryPath(string libraryName, NativeLibraryConfig.AvxLevel avx)
  275. {
  276. GetPlatformPathParts(out _, out var os, out var fileExtension, out var libPrefix);
  277. var avxStr = NativeLibraryConfig.AvxLevelToString(avx);
  278. if (!string.IsNullOrEmpty(avxStr))
  279. avxStr += "/";
  280. return $"runtimes/{os}/native/{avxStr}{libPrefix}{libraryName}{fileExtension}";
  281. }
  282. private static void GetPlatformPathParts(out OSPlatform platform, out string os, out string fileExtension, out string libPrefix)
  283. {
  284. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  285. {
  286. platform = OSPlatform.Windows;
  287. os = "win-x64";
  288. fileExtension = ".dll";
  289. libPrefix = "";
  290. return;
  291. }
  292. if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
  293. {
  294. platform = OSPlatform.Linux;
  295. os = "linux-x64";
  296. fileExtension = ".so";
  297. libPrefix = "lib";
  298. return;
  299. }
  300. if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
  301. {
  302. platform = OSPlatform.OSX;
  303. fileExtension = ".dylib";
  304. os = System.Runtime.Intrinsics.Arm.ArmBase.Arm64.IsSupported
  305. ? "osx-arm64"
  306. : "osx-x64";
  307. libPrefix = "lib";
  308. }
  309. else
  310. {
  311. throw new RuntimeError("Your operating system is not supported, please open an issue in LLamaSharp.");
  312. }
  313. }
  314. #endif
  315. /// <summary>
  316. /// Try to load libllama/llava_shared, using CPU feature detection to try and load a more specialised DLL if possible
  317. /// </summary>
  318. /// <returns>The library handle to unload later, or IntPtr.Zero if no library was loaded</returns>
  319. private static IntPtr TryLoadLibraries(LibraryName lib)
  320. {
  321. #if NET6_0_OR_GREATER
  322. var configuration = NativeLibraryConfig.CheckAndGatherDescription(lib);
  323. enableLogging = configuration.Logging;
  324. logLevel = configuration.LogLevel;
  325. // Set the flag to ensure the NativeLibraryConfig can no longer be modified
  326. NativeLibraryConfig.LibraryHasLoaded = true;
  327. // Show the configuration we're working with
  328. Log(configuration.ToString(), LLamaLogLevel.Info);
  329. // If a specific path is requested, load that or immediately fail
  330. if (!string.IsNullOrEmpty(configuration.Path))
  331. {
  332. if (!NativeLibrary.TryLoad(configuration.Path, out var handle))
  333. throw new RuntimeError($"Failed to load the native library [{configuration.Path}] you specified.");
  334. Log($"Successfully loaded the library [{configuration.Path}] specified by user", LLamaLogLevel.Info);
  335. return handle;
  336. }
  337. // Get a list of locations to try loading (in order of preference)
  338. var libraryTryLoadOrder = GetLibraryTryOrder(configuration);
  339. foreach (var libraryPath in libraryTryLoadOrder)
  340. {
  341. var fullPath = TryFindPath(libraryPath);
  342. Log($"Trying '{fullPath}'", LLamaLogLevel.Debug);
  343. var result = TryLoad(fullPath);
  344. if (result != IntPtr.Zero)
  345. {
  346. Log($"Loaded '{fullPath}'", LLamaLogLevel.Info);
  347. return result;
  348. }
  349. Log($"Failed Loading '{fullPath}'", LLamaLogLevel.Info);
  350. }
  351. if (!configuration.AllowFallback)
  352. {
  353. throw new RuntimeError("Failed to load the library that match your rule, please" +
  354. " 1) check your rule." +
  355. " 2) try to allow fallback." +
  356. " 3) or open an issue if it's expected to be successful.");
  357. }
  358. #endif
  359. Log($"No library was loaded before calling native apis. " +
  360. $"This is not an error under netstandard2.0 but needs attention with net6 or higher.", LLamaLogLevel.Warning);
  361. return IntPtr.Zero;
  362. #if NET6_0_OR_GREATER
  363. // Try to load a DLL from the path.
  364. // Returns null if nothing is loaded.
  365. static IntPtr TryLoad(string path)
  366. {
  367. if (NativeLibrary.TryLoad(path, out var handle))
  368. return handle;
  369. return IntPtr.Zero;
  370. }
  371. // Try to find the given file in any of the possible search paths
  372. string TryFindPath(string filename)
  373. {
  374. // Try the configured search directories in the configuration
  375. foreach (var path in configuration.SearchDirectories)
  376. {
  377. var candidate = Path.Combine(path, filename);
  378. if (File.Exists(candidate))
  379. return candidate;
  380. }
  381. // Try a few other possible paths
  382. var possiblePathPrefix = new[] {
  383. AppDomain.CurrentDomain.BaseDirectory,
  384. Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) ?? ""
  385. };
  386. foreach (var path in possiblePathPrefix)
  387. {
  388. var candidate = Path.Combine(path, filename);
  389. if (File.Exists(candidate))
  390. return candidate;
  391. }
  392. return filename;
  393. }
  394. #endif
  395. }
  396. internal const string libraryName = "llama";
  397. internal const string llavaLibraryName = "llava_shared";
  398. private const string cudaVersionFile = "version.json";
  399. private const string loggingPrefix = "[LLamaSharp Native]";
  400. private static bool enableLogging = false;
  401. private static LLamaLogLevel logLevel = LLamaLogLevel.Info;
  402. }
  403. }