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.

DefaultNativeLibrarySelectingPolicy.cs 2.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. /// <inheritdoc/>
  8. public class DefaultNativeLibrarySelectingPolicy: INativeLibrarySelectingPolicy
  9. {
  10. /// <inheritdoc/>
  11. public IEnumerable<INativeLibrary> Apply(NativeLibraryConfig.Description description, SystemInfo systemInfo, NativeLogConfig.LLamaLogCallback? logCallback)
  12. {
  13. List<INativeLibrary> results = new();
  14. // Show the configuration we're working with
  15. Log(description.ToString(), LLamaLogLevel.Info, logCallback);
  16. // If a specific path is requested, only use it, no fall back.
  17. if (!string.IsNullOrEmpty(description.Path))
  18. {
  19. yield return new NativeLibraryFromPath(description.Path);
  20. }
  21. else
  22. {
  23. if (description.UseCuda)
  24. {
  25. yield return new NativeLibraryWithCuda(systemInfo.CudaMajorVersion, description.Library, description.SkipCheck);
  26. }
  27. if(!description.UseCuda || description.AllowFallback)
  28. {
  29. if (description.AllowFallback)
  30. {
  31. // Try all of the AVX levels we can support.
  32. if (description.AvxLevel >= AvxLevel.Avx512)
  33. yield return new NativeLibraryWithAvx(description.Library, AvxLevel.Avx512, description.SkipCheck);
  34. if (description.AvxLevel >= AvxLevel.Avx2)
  35. yield return new NativeLibraryWithAvx(description.Library, AvxLevel.Avx2, description.SkipCheck);
  36. if (description.AvxLevel >= AvxLevel.Avx)
  37. yield return new NativeLibraryWithAvx(description.Library, AvxLevel.Avx, description.SkipCheck);
  38. yield return new NativeLibraryWithAvx(description.Library, AvxLevel.None, description.SkipCheck);
  39. }
  40. else
  41. {
  42. yield return new NativeLibraryWithAvx(description.Library, description.AvxLevel, description.SkipCheck);
  43. }
  44. }
  45. if(systemInfo.OSPlatform == OSPlatform.OSX || description.AllowFallback)
  46. {
  47. yield return new NativeLibraryWithMacOrFallback(description.Library, description.SkipCheck);
  48. }
  49. }
  50. }
  51. private void Log(string message, LLamaLogLevel level, NativeLogConfig.LLamaLogCallback? logCallback)
  52. {
  53. if (!message.EndsWith("\n"))
  54. message += "\n";
  55. logCallback?.Invoke(level, message);
  56. }
  57. }
  58. #endif
  59. }