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.

NativeLibraryWithCuda.cs 3.0 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using LLama.Abstractions;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4. namespace LLama.Native
  5. {
  6. #if NET6_0_OR_GREATER
  7. /// <summary>
  8. /// A native library compiled with cublas/cuda.
  9. /// </summary>
  10. public class NativeLibraryWithCuda : INativeLibrary
  11. {
  12. private int _majorCudaVersion;
  13. private NativeLibraryName _libraryName;
  14. private AvxLevel _avxLevel;
  15. private bool _skipCheck;
  16. /// <inheritdoc/>
  17. public NativeLibraryMetadata? Metadata
  18. {
  19. get
  20. {
  21. return new NativeLibraryMetadata(_libraryName, true, _avxLevel);
  22. }
  23. }
  24. /// <summary>
  25. ///
  26. /// </summary>
  27. /// <param name="majorCudaVersion"></param>
  28. /// <param name="libraryName"></param>
  29. /// <param name="skipCheck"></param>
  30. public NativeLibraryWithCuda(int majorCudaVersion, NativeLibraryName libraryName, bool skipCheck)
  31. {
  32. _majorCudaVersion = majorCudaVersion;
  33. _libraryName = libraryName;
  34. _skipCheck = skipCheck;
  35. }
  36. /// <inheritdoc/>
  37. public IEnumerable<string> Prepare(SystemInfo systemInfo, NativeLogConfig.LLamaLogCallback? logCallback)
  38. {
  39. // TODO: Avx level is ignored now, needs to be implemented in the future.
  40. if (systemInfo.OSPlatform == OSPlatform.Windows || systemInfo.OSPlatform == OSPlatform.Linux || _skipCheck)
  41. {
  42. if (_majorCudaVersion == -1 && _skipCheck)
  43. {
  44. // Currently only 11 and 12 are supported.
  45. var cuda12LibraryPath = GetCudaPath(systemInfo, 12, logCallback);
  46. if (cuda12LibraryPath is not null)
  47. {
  48. yield return cuda12LibraryPath;
  49. }
  50. var cuda11LibraryPath = GetCudaPath(systemInfo, 11, logCallback);
  51. if (cuda11LibraryPath is not null)
  52. {
  53. yield return cuda11LibraryPath;
  54. }
  55. }
  56. else if (_majorCudaVersion != -1)
  57. {
  58. var cudaLibraryPath = GetCudaPath(systemInfo, _majorCudaVersion, logCallback);
  59. if (cudaLibraryPath is not null)
  60. {
  61. yield return cudaLibraryPath;
  62. }
  63. }
  64. }
  65. }
  66. private string? GetCudaPath(SystemInfo systemInfo, int cudaVersion, NativeLogConfig.LLamaLogCallback? logCallback)
  67. {
  68. NativeLibraryUtils.GetPlatformPathParts(systemInfo.OSPlatform, out var os, out var fileExtension, out var libPrefix);
  69. var relativePath = $"runtimes/{os}/native/cuda{cudaVersion}/{libPrefix}{_libraryName.GetLibraryName()}{fileExtension}";
  70. return relativePath;
  71. }
  72. }
  73. #endif
  74. }