-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTranscriptionRuntimeSupport.cs
More file actions
247 lines (221 loc) · 9.62 KB
/
Copy pathTranscriptionRuntimeSupport.cs
File metadata and controls
247 lines (221 loc) · 9.62 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
using System.IO;
namespace PrimeDictate;
internal sealed record TranscriptionComputeChoice(
TranscriptionComputeInterface Kind,
string Label,
string Description)
{
public override string ToString() => this.Label;
}
internal readonly record struct TranscriptionConfigurationSelection(
TranscriptionBackendKind Backend,
TranscriptionComputeInterface ComputeInterface,
string? SelectedModelId,
string? ModelPath);
internal static class TranscriptionRuntimeSupport
{
public static IReadOnlyList<TranscriptionComputeChoice> GetComputeChoices(
TranscriptionBackendKind backend,
string? selectedModelId,
string? configuredModelPath)
{
var choices = new List<TranscriptionComputeChoice>
{
new(
TranscriptionComputeInterface.Cpu,
"CPU (most compatible)",
"Runs transcription on the CPU. This is the fallback for every backend.")
};
if (backend == TranscriptionBackendKind.WhisperNet)
{
if (PlatformSupport.SupportsWhisperNetGpu)
{
choices.Add(new TranscriptionComputeChoice(
TranscriptionComputeInterface.Gpu,
$"GPU ({PlatformSupport.WhisperNetGpuRuntimeLabel})",
"Runs Whisper.net through the fastest detected GPU runtime before falling back to CPU."));
}
if (CanUseWhisperNetNpu(selectedModelId, configuredModelPath))
{
choices.Add(new TranscriptionComputeChoice(
TranscriptionComputeInterface.Npu,
"NPU (OpenVINO)",
"Runs the Whisper encoder through OpenVINO NPU sidecars for supported GGML models."));
}
}
else if (backend == TranscriptionBackendKind.QualcommQnn &&
PlatformSupport.SupportsQualcommQnnHtp)
{
choices.Add(new TranscriptionComputeChoice(
TranscriptionComputeInterface.Npu,
"NPU (Qualcomm QNN HTP)",
"Runs Qualcomm AI Hub Whisper precompiled ONNX wrappers through ONNX Runtime QNN HTP with CPU fallback disabled."));
}
return choices;
}
public static bool IsBackendSupportedOnCurrentMachine(TranscriptionBackendKind backend) =>
backend != TranscriptionBackendKind.QualcommQnn ||
PlatformSupport.ShouldOfferQualcommQnnBackend;
public static bool IsComputeSupported(
TranscriptionBackendKind backend,
TranscriptionComputeInterface computeInterface,
string? selectedModelId,
string? configuredModelPath) =>
GetComputeChoices(backend, selectedModelId, configuredModelPath)
.Any(choice => choice.Kind == computeInterface);
public static TranscriptionComputeInterface GetBestComputeInterface(
TranscriptionBackendKind backend,
string? selectedModelId,
string? configuredModelPath)
{
var choices = GetComputeChoices(backend, selectedModelId, configuredModelPath);
return choices.FirstOrDefault(choice => choice.Kind == TranscriptionComputeInterface.Gpu)?.Kind
?? choices.FirstOrDefault(choice => choice.Kind == TranscriptionComputeInterface.Npu)?.Kind
?? TranscriptionComputeInterface.Cpu;
}
public static bool NormalizeSettingsForCurrentMachine(AppSettings settings)
{
var changed = false;
var requestedComputeInterface = settings.TranscriptionComputeInterface;
if (!IsBackendSupportedOnCurrentMachine(settings.TranscriptionBackend))
{
if (requestedComputeInterface != TranscriptionComputeInterface.Cpu &&
TryFindBestInstalledHardwareConfiguration(out var backendHardwareSelection))
{
settings.TranscriptionBackend = backendHardwareSelection.Backend;
settings.TranscriptionComputeInterface = backendHardwareSelection.ComputeInterface;
settings.SelectedModelId = backendHardwareSelection.SelectedModelId;
settings.ModelPath = backendHardwareSelection.ModelPath;
return true;
}
if (settings.TranscriptionBackend == TranscriptionBackendKind.QualcommQnn)
{
if (QualcommAihubWhisperModelCatalog.TryGetById(settings.SelectedModelId, out _) ||
QualcommAihubWhisperModelCatalog.TryResolveDirectory(settings.ModelPath, out _) ||
QualcommAihubWhisperModelCatalog.IsRawContextOnlyDirectory(settings.ModelPath))
{
settings.TranscriptionBackend = TranscriptionBackendKind.Whisper;
settings.SelectedModelId = null;
settings.ModelPath = null;
}
else
{
settings.TranscriptionBackend = TranscriptionBackendKind.Moonshine;
}
settings.TranscriptionComputeInterface = TranscriptionComputeInterface.Cpu;
changed = true;
}
else
{
settings.TranscriptionBackend = TranscriptionBackendKind.Whisper;
settings.TranscriptionComputeInterface = TranscriptionComputeInterface.Cpu;
settings.SelectedModelId = null;
settings.ModelPath = null;
changed = true;
}
}
if (IsComputeSupported(
settings.TranscriptionBackend,
settings.TranscriptionComputeInterface,
settings.SelectedModelId,
settings.ModelPath))
{
return changed;
}
if (settings.TranscriptionComputeInterface != TranscriptionComputeInterface.Cpu &&
TryFindBestInstalledHardwareConfiguration(out var hardwareSelection))
{
settings.TranscriptionBackend = hardwareSelection.Backend;
settings.TranscriptionComputeInterface = hardwareSelection.ComputeInterface;
settings.SelectedModelId = hardwareSelection.SelectedModelId;
settings.ModelPath = hardwareSelection.ModelPath;
return true;
}
settings.TranscriptionComputeInterface = GetBestComputeInterface(
settings.TranscriptionBackend,
settings.SelectedModelId,
settings.ModelPath);
return true;
}
public static bool TryFindBestInstalledHardwareConfiguration(
out TranscriptionConfigurationSelection selection)
{
if (PlatformSupport.SupportsWhisperNetGpu)
{
foreach (var option in WhisperNetModelCatalog.Options)
{
if (WhisperNetModelCatalog.TryResolveInstalledPath(option, out var installedPath))
{
selection = new TranscriptionConfigurationSelection(
TranscriptionBackendKind.WhisperNet,
TranscriptionComputeInterface.Gpu,
option.Id,
installedPath);
return true;
}
}
}
if (PlatformSupport.SupportsWhisperNetOpenVino)
{
foreach (var option in WhisperNetModelCatalog.Options)
{
if (WhisperNetModelCatalog.TryResolveInstalledArtifacts(option, out var artifacts) &&
artifacts.Value.HasOpenVinoSidecars)
{
selection = new TranscriptionConfigurationSelection(
TranscriptionBackendKind.WhisperNet,
TranscriptionComputeInterface.Npu,
option.Id,
artifacts.Value.ModelPath);
return true;
}
}
}
if (PlatformSupport.SupportsQualcommQnnHtp)
{
foreach (var option in QualcommAihubWhisperModelCatalog.Options)
{
if (QualcommAihubWhisperModelCatalog.TryResolveInstalledPath(option, out var installedPath))
{
selection = new TranscriptionConfigurationSelection(
TranscriptionBackendKind.QualcommQnn,
TranscriptionComputeInterface.Npu,
option.Id,
installedPath);
return true;
}
}
}
selection = default;
return false;
}
private static bool CanUseWhisperNetNpu(string? selectedModelId, string? configuredModelPath)
{
if (!PlatformSupport.SupportsWhisperNetOpenVino)
{
return false;
}
if (!string.IsNullOrWhiteSpace(configuredModelPath) &&
File.Exists(configuredModelPath))
{
var artifacts = CreateWhisperNetArtifacts(configuredModelPath);
return artifacts.HasOpenVinoSidecars;
}
if (WhisperNetModelCatalog.TryGetById(selectedModelId, out var selectedOption))
{
return selectedOption.SupportsOpenVinoBundle;
}
return WhisperNetModelCatalog.Options.Any(option =>
option.Recommended &&
option.SupportsOpenVinoBundle);
}
private static WhisperNetModelArtifacts CreateWhisperNetArtifacts(string modelPath)
{
var directory = Path.GetDirectoryName(modelPath) ?? string.Empty;
var stem = Path.GetFileNameWithoutExtension(modelPath);
return new WhisperNetModelArtifacts(
Path.GetFullPath(modelPath),
Path.GetFullPath(Path.Combine(directory, $"{stem}-encoder-openvino.xml")),
Path.GetFullPath(Path.Combine(directory, $"{stem}-encoder-openvino.bin")));
}
}