-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPlatformSupport.cs
More file actions
139 lines (115 loc) · 4.87 KB
/
Copy pathPlatformSupport.cs
File metadata and controls
139 lines (115 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using System.IO;
using System.Runtime.InteropServices;
namespace PrimeDictate;
internal static class PlatformSupport
{
private static readonly Lazy<bool> WhisperNetOpenVinoSupport = new(() =>
OperatingSystem.IsWindows() &&
RuntimeInformation.ProcessArchitecture == Architecture.X64 &&
HasWhisperNetRuntimeAsset("openvino"));
private static readonly Lazy<bool> WhisperNetCudaSupport = new(() =>
OperatingSystem.IsWindows() &&
RuntimeInformation.ProcessArchitecture == Architecture.X64 &&
File.Exists(Path.Combine(Environment.SystemDirectory, "nvcuda.dll")) &&
HasWhisperNetRuntimeAsset("cuda"));
private static readonly Lazy<bool> WhisperNetVulkanSupport = new(() =>
OperatingSystem.IsWindows() &&
RuntimeInformation.ProcessArchitecture == Architecture.X64 &&
File.Exists(Path.Combine(Environment.SystemDirectory, "vulkan-1.dll")) &&
HasWhisperNetRuntimeAsset("vulkan"));
public static Architecture ProcessArchitecture => RuntimeInformation.ProcessArchitecture;
public static bool IsWindowsArm64 =>
OperatingSystem.IsWindows() &&
RuntimeInformation.ProcessArchitecture == Architecture.Arm64;
public static bool SupportsWhisperNetOpenVino =>
WhisperNetOpenVinoSupport.Value;
public static bool SupportsWhisperNetCuda =>
WhisperNetCudaSupport.Value;
public static bool SupportsWhisperNetVulkan =>
WhisperNetVulkanSupport.Value;
public static bool SupportsWhisperNetGpu =>
SupportsWhisperNetCuda ||
SupportsWhisperNetVulkan;
public static bool SupportsQualcommQnnHtp => QnnRuntimeSupport.GetAvailability().SupportsQnnHtp;
public static bool ShouldOfferQualcommQnnBackend => SupportsQualcommQnnHtp;
public static string WhisperNetRuntimeSummary => SupportsWhisperNetOpenVino
? SupportsWhisperNetGpu
? $"Whisper.net GGML can use CPU, {WhisperNetGpuLabel}, and OpenVINO NPU on this machine."
: "Whisper.net GGML can use CPU and OpenVINO NPU on this machine."
: SupportsWhisperNetGpu
? $"Whisper.net GGML can use CPU and {WhisperNetGpuLabel} on this machine."
: IsWindowsArm64
? "Whisper.net GGML runs natively on ARM64 with CPU. GPU/NPU acceleration currently requires an x64 build."
: $"Whisper.net GGML can use CPU, but no supported GPU/NPU runtime was detected for {RuntimeInformation.ProcessArchitecture}.";
public static string QualcommQnnRuntimeSummary =>
$"{QnnRuntimeSupport.GetAvailability().Summary} Qualcomm AI Hub Whisper packages use precompiled ONNX Runtime EPContext wrappers around QNN context binaries.";
public static string WhisperNetGpuLabel
{
get
{
var runtimeLabel = WhisperNetGpuRuntimeLabel;
return string.Equals(runtimeLabel, "GPU", StringComparison.Ordinal)
? "GPU"
: $"{runtimeLabel} GPU";
}
}
public static string WhisperNetGpuRuntimeLabel
{
get
{
if (SupportsWhisperNetCuda && SupportsWhisperNetVulkan)
{
return "CUDA/Vulkan";
}
if (SupportsWhisperNetCuda)
{
return "CUDA";
}
if (SupportsWhisperNetVulkan)
{
return "Vulkan";
}
return "GPU";
}
}
private static bool HasWhisperNetRuntimeAsset(string runtimeName)
{
var nativeFileName = OperatingSystem.IsWindows() ? "whisper.dll" : "libwhisper.so";
foreach (var candidateDirectory in EnumerateWhisperNetRuntimeDirectories(runtimeName))
{
var candidatePath = Path.Combine(candidateDirectory, nativeFileName);
if (File.Exists(candidatePath) &&
TryLoadNativeLibrary(candidatePath))
{
return true;
}
}
return false;
}
private static bool TryLoadNativeLibrary(string path)
{
try
{
var handle = NativeLibrary.Load(path);
NativeLibrary.Free(handle);
return true;
}
catch
{
return false;
}
}
private static IEnumerable<string> EnumerateWhisperNetRuntimeDirectories(string runtimeName)
{
if (OperatingSystem.IsWindows())
{
yield return Path.Combine(AppContext.BaseDirectory, "runtimes", runtimeName, "win-x64");
var assemblyDirectory = Path.GetDirectoryName(typeof(PlatformSupport).Assembly.Location);
if (!string.IsNullOrWhiteSpace(assemblyDirectory))
{
yield return Path.Combine(assemblyDirectory, "runtimes", runtimeName, "win-x64");
}
yield return Path.Combine(Directory.GetCurrentDirectory(), "runtimes", runtimeName, "win-x64");
}
}
}