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 14 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. using LLama.Exceptions;
  2. using Microsoft.Extensions.Logging;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.IO;
  7. using System.Runtime.InteropServices;
  8. using System.Text.Json;
  9. namespace LLama.Native
  10. {
  11. public static partial class NativeApi
  12. {
  13. static NativeApi()
  14. {
  15. // Try to load a preferred library, based on CPU feature detection
  16. TryLoadLibrary();
  17. try
  18. {
  19. llama_empty_call();
  20. }
  21. catch (DllNotFoundException)
  22. {
  23. throw new RuntimeError("The native library cannot be correctly loaded. It could be one of the following reasons: \n" +
  24. "1. No LLamaSharp backend was installed. Please search LLamaSharp.Backend and install one of them. \n" +
  25. "2. You are using a device with only CPU but installed cuda backend. Please install cpu backend instead. \n" +
  26. "3. One of the dependency of the native library is missed. Please use `ldd` on linux, `dumpbin` on windows and `otool`" +
  27. "to check if all the dependency of the native library is satisfied. Generally you could find the libraries under your output folder.\n" +
  28. "4. Try to compile llama.cpp yourself to generate a libllama library, then use `LLama.Native.NativeLibraryConfig.WithLibrary` " +
  29. "to specify it at the very beginning of your code. For more informations about compilation, please refer to LLamaSharp repo on github.\n");
  30. }
  31. llama_backend_init();
  32. }
  33. private static void Log(string message, LogLevel level)
  34. {
  35. if (!enableLogging)
  36. return;
  37. if ((int)level < (int)logLevel)
  38. return;
  39. ConsoleColor color;
  40. string levelPrefix;
  41. if (level == LogLevel.Information)
  42. {
  43. color = ConsoleColor.Green;
  44. levelPrefix = "[Info]";
  45. }
  46. else if (level == LogLevel.Error)
  47. {
  48. color = ConsoleColor.Red;
  49. levelPrefix = "[Error]";
  50. }
  51. else
  52. {
  53. color = ConsoleColor.Yellow;
  54. levelPrefix = "[Error]";
  55. }
  56. Console.ForegroundColor = color;
  57. Console.WriteLine($"{loggingPrefix} {levelPrefix} {message}");
  58. Console.ResetColor();
  59. }
  60. private static int GetCudaMajorVersion()
  61. {
  62. string? cudaPath;
  63. string version = "";
  64. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  65. {
  66. cudaPath = Environment.GetEnvironmentVariable("CUDA_PATH");
  67. if (cudaPath is null)
  68. {
  69. return -1;
  70. }
  71. version = GetCudaVersionFromPath(cudaPath);
  72. }
  73. else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
  74. {
  75. // Try the default first
  76. cudaPath = "/usr/local/bin/cuda";
  77. version = GetCudaVersionFromPath(cudaPath);
  78. if (string.IsNullOrEmpty(version))
  79. {
  80. cudaPath = Environment.GetEnvironmentVariable("LD_LIBRARY_PATH");
  81. if (cudaPath is null)
  82. {
  83. return -1;
  84. }
  85. foreach (var path in cudaPath.Split(':'))
  86. {
  87. version = GetCudaVersionFromPath(Path.Combine(path, ".."));
  88. if (string.IsNullOrEmpty(version))
  89. {
  90. break;
  91. }
  92. }
  93. }
  94. }
  95. if (string.IsNullOrEmpty(version))
  96. return -1;
  97. version = version.Split('.')[0];
  98. if (int.TryParse(version, out var majorVersion))
  99. return majorVersion;
  100. return -1;
  101. }
  102. private static string GetCudaVersionFromPath(string cudaPath)
  103. {
  104. try
  105. {
  106. string json = File.ReadAllText(Path.Combine(cudaPath, cudaVersionFile));
  107. using (JsonDocument document = JsonDocument.Parse(json))
  108. {
  109. JsonElement root = document.RootElement;
  110. JsonElement cublasNode = root.GetProperty("libcublas");
  111. JsonElement versionNode = cublasNode.GetProperty("version");
  112. if (versionNode.ValueKind == JsonValueKind.Undefined)
  113. {
  114. return string.Empty;
  115. }
  116. return versionNode.GetString() ?? "";
  117. }
  118. }
  119. catch (Exception)
  120. {
  121. return string.Empty;
  122. }
  123. }
  124. #if NET6_0_OR_GREATER
  125. private static string GetAvxLibraryPath(NativeLibraryConfig.AvxLevel avxLevel, string prefix, string suffix, string libraryNamePrefix)
  126. {
  127. var avxStr = NativeLibraryConfig.AvxLevelToString(avxLevel);
  128. if (!string.IsNullOrEmpty(avxStr))
  129. {
  130. avxStr += "/";
  131. }
  132. return $"{prefix}{avxStr}{libraryNamePrefix}{libraryName}{suffix}";
  133. }
  134. private static List<string> GetLibraryTryOrder(NativeLibraryConfig.Description configuration)
  135. {
  136. OSPlatform platform;
  137. string prefix, suffix, libraryNamePrefix;
  138. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  139. {
  140. platform = OSPlatform.Windows;
  141. prefix = "runtimes/win-x64/native/";
  142. suffix = ".dll";
  143. libraryNamePrefix = "";
  144. }
  145. else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
  146. {
  147. platform = OSPlatform.Linux;
  148. prefix = "runtimes/linux-x64/native/";
  149. suffix = ".so";
  150. libraryNamePrefix = "lib";
  151. }
  152. else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
  153. {
  154. platform = OSPlatform.OSX;
  155. suffix = ".dylib";
  156. prefix = System.Runtime.Intrinsics.Arm.ArmBase.Arm64.IsSupported
  157. ? "runtimes/osx-arm64/native/"
  158. : "runtimes/osx-x64/native/";
  159. libraryNamePrefix = "lib";
  160. }
  161. else
  162. {
  163. throw new RuntimeError("Your system plarform is not supported, please open an issue in LLamaSharp.");
  164. }
  165. Log($"Detected OS Platform: {platform}", LogLevel.Information);
  166. List<string> result = new();
  167. if (configuration.UseCuda && (platform == OSPlatform.Windows || platform == OSPlatform.Linux)) // no cuda on macos
  168. {
  169. int cudaVersion = GetCudaMajorVersion();
  170. // TODO: load cuda library with avx
  171. if (cudaVersion == -1 && !configuration.AllowFallback)
  172. {
  173. // if check skipped, we just try to load cuda libraries one by one.
  174. if (configuration.SkipCheck)
  175. {
  176. result.Add($"{prefix}cuda12/{libraryNamePrefix}{libraryName}{suffix}");
  177. result.Add($"{prefix}cuda11/{libraryNamePrefix}{libraryName}{suffix}");
  178. }
  179. else
  180. {
  181. throw new RuntimeError("Configured to load a cuda library but no cuda detected on your device.");
  182. }
  183. }
  184. else if (cudaVersion == 11)
  185. {
  186. Log($"Detected cuda major version {cudaVersion}.", LogLevel.Information);
  187. result.Add($"{prefix}cuda11/{libraryNamePrefix}{libraryName}{suffix}");
  188. }
  189. else if (cudaVersion == 12)
  190. {
  191. Log($"Detected cuda major version {cudaVersion}.", LogLevel.Information);
  192. result.Add($"{prefix}cuda12/{libraryNamePrefix}{libraryName}{suffix}");
  193. }
  194. else if (cudaVersion > 0)
  195. {
  196. throw new RuntimeError($"Cuda version {cudaVersion} hasn't been supported by LLamaSharp, please open an issue for it.");
  197. }
  198. // otherwise no cuda detected but allow fallback
  199. }
  200. // use cpu (or mac possibly with metal)
  201. if (!configuration.AllowFallback && platform != OSPlatform.OSX)
  202. {
  203. result.Add(GetAvxLibraryPath(configuration.AvxLevel, prefix, suffix, libraryNamePrefix));
  204. }
  205. else if (platform != OSPlatform.OSX) // in macos there's absolutely no avx
  206. {
  207. if (configuration.AvxLevel >= NativeLibraryConfig.AvxLevel.Avx512)
  208. result.Add(GetAvxLibraryPath(NativeLibraryConfig.AvxLevel.Avx512, prefix, suffix, libraryNamePrefix));
  209. if (configuration.AvxLevel >= NativeLibraryConfig.AvxLevel.Avx2)
  210. result.Add(GetAvxLibraryPath(NativeLibraryConfig.AvxLevel.Avx2, prefix, suffix, libraryNamePrefix));
  211. if (configuration.AvxLevel >= NativeLibraryConfig.AvxLevel.Avx)
  212. result.Add(GetAvxLibraryPath(NativeLibraryConfig.AvxLevel.Avx, prefix, suffix, libraryNamePrefix));
  213. result.Add(GetAvxLibraryPath(NativeLibraryConfig.AvxLevel.None, prefix, suffix, libraryNamePrefix));
  214. }
  215. if (platform == OSPlatform.OSX)
  216. {
  217. result.Add($"{prefix}{libraryNamePrefix}{libraryName}{suffix}");
  218. result.Add($"{prefix}{libraryNamePrefix}{llavaLibraryName}{suffix}");
  219. }
  220. return result;
  221. }
  222. #endif
  223. /// <summary>
  224. /// Try to load libllama, using CPU feature detection to try and load a more specialised DLL if possible
  225. /// </summary>
  226. /// <returns>The library handle to unload later, or IntPtr.Zero if no library was loaded</returns>
  227. private static IntPtr TryLoadLibrary()
  228. {
  229. #if NET6_0_OR_GREATER
  230. var configuration = NativeLibraryConfig.CheckAndGatherDescription();
  231. enableLogging = configuration.Logging;
  232. logLevel = configuration.LogLevel;
  233. // We move the flag to avoid loading library when the variable is called else where.
  234. NativeLibraryConfig.LibraryHasLoaded = true;
  235. Log(configuration.ToString(), LogLevel.Information);
  236. if (!string.IsNullOrEmpty(configuration.Path))
  237. {
  238. // When loading the user specified library, there's no fallback.
  239. var success = NativeLibrary.TryLoad(configuration.Path, out var result);
  240. if (!success)
  241. {
  242. throw new RuntimeError($"Failed to load the native library [{configuration.Path}] you specified.");
  243. }
  244. Log($"Successfully loaded the library [{configuration.Path}] specified by user", LogLevel.Information);
  245. return result;
  246. }
  247. var libraryTryLoadOrder = GetLibraryTryOrder(configuration);
  248. var preferredPaths = configuration.SearchDirectories;
  249. var possiblePathPrefix = new[] {
  250. AppDomain.CurrentDomain.BaseDirectory,
  251. Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) ?? ""
  252. };
  253. string TryFindPath(string filename)
  254. {
  255. foreach (var path in preferredPaths)
  256. {
  257. if (File.Exists(Path.Combine(path, filename)))
  258. {
  259. return Path.Combine(path, filename);
  260. }
  261. }
  262. foreach (var path in possiblePathPrefix)
  263. {
  264. if (File.Exists(Path.Combine(path, filename)))
  265. {
  266. return Path.Combine(path, filename);
  267. }
  268. }
  269. return filename;
  270. }
  271. foreach (var libraryPath in libraryTryLoadOrder)
  272. {
  273. var fullPath = TryFindPath(libraryPath);
  274. var result = TryLoad(fullPath, true);
  275. if (result is not null && result != IntPtr.Zero)
  276. {
  277. Log($"{fullPath} is selected and loaded successfully.", LogLevel.Information);
  278. // One we have clear the detection and that llama loads successfully we load LLaVa if exist on the
  279. // same path.
  280. TryLoad( libraryPath.Replace("llama", "llava_shared"), true);
  281. return (IntPtr)result;
  282. }
  283. Log($"Tried to load {fullPath} but failed.", LogLevel.Information);
  284. }
  285. if (!configuration.AllowFallback)
  286. {
  287. throw new RuntimeError("Failed to load the library that match your rule, please" +
  288. " 1) check your rule." +
  289. " 2) try to allow fallback." +
  290. " 3) or open an issue if it's expected to be successful.");
  291. }
  292. #endif
  293. Log($"No library was loaded before calling native apis. " +
  294. $"This is not an error under netstandard2.0 but needs attention with net6 or higher.", LogLevel.Warning);
  295. return IntPtr.Zero;
  296. #if NET6_0_OR_GREATER
  297. // Try to load a DLL from the path if supported. Returns null if nothing is loaded.
  298. static IntPtr? TryLoad(string path, bool supported = true)
  299. {
  300. if (!supported)
  301. return null;
  302. if (NativeLibrary.TryLoad(path, out var handle))
  303. return handle;
  304. return null;
  305. }
  306. #endif
  307. }
  308. internal const string libraryName = "llama";
  309. internal const string llavaLibraryName = "llava_shared";
  310. private const string cudaVersionFile = "version.json";
  311. private const string loggingPrefix = "[LLamaSharp Native]";
  312. private static bool enableLogging = false;
  313. private static LLamaLogLevel logLevel = LLamaLogLevel.Info;
  314. }
  315. }