Skip to content

Commit a0bd84a

Browse files
committed
whisper.swiftui : model list & bench methods
1 parent f78aea7 commit a0bd84a

File tree

6 files changed

+356
-15
lines changed

6 files changed

+356
-15
lines changed

examples/whisper.swiftui/whisper.cpp.swift/LibWhisper.swift

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,85 @@ actor WhisperContext {
5555
return transcription
5656
}
5757

58+
func system_info() -> String {
59+
var info = ""
60+
if (ggml_cpu_has_neon() != 0) { info += "NEON " }
61+
if (ggml_cpu_has_metal() != 0) { info += "METAL " }
62+
if (ggml_cpu_has_blas() != 0) { info += "BLAS " }
63+
return String(info.dropLast())
64+
}
65+
66+
func bench_full(modelName: String) async -> String {
67+
let n_threads = Int32(min(4, cpuCount())) // Default in whisper.cpp
68+
69+
let n_mels = whisper_model_n_mels(context)
70+
if (whisper_set_mel(context, nil, 0, n_mels) != 0) {
71+
return "error: failed to set mel"
72+
}
73+
74+
// heat encoder
75+
if (whisper_encode(context, 0, n_threads) != 0) {
76+
return "error: failed to encode"
77+
}
78+
79+
var tokens = [whisper_token](repeating: 0, count: 512)
80+
81+
// prompt heat
82+
if (whisper_decode(context, &tokens, 256, 0, n_threads) != 0) {
83+
return "error: failed to decode"
84+
}
85+
86+
// text-generation heat
87+
if (whisper_decode(context, &tokens, 1, 256, n_threads) != 0) {
88+
return "error: failed to decode"
89+
}
90+
91+
whisper_reset_timings(context)
92+
93+
// actual run
94+
if (whisper_encode(context, 0, n_threads) != 0) {
95+
return "error: failed to encode"
96+
}
97+
98+
// text-generation
99+
for i in 0..<256 {
100+
if (whisper_decode(context, &tokens, 1, Int32(i), n_threads) != 0) {
101+
return "error: failed to decode"
102+
}
103+
}
104+
105+
// batched decoding
106+
for i in 0..<64 {
107+
if (whisper_decode(context, &tokens, 5, 0, n_threads) != 0) {
108+
return "error: failed to decode"
109+
}
110+
}
111+
112+
// prompt processing
113+
for i in 0..<16 {
114+
if (whisper_decode(context, &tokens, 256, 0, n_threads) != 0) {
115+
return "error: failed to decode"
116+
}
117+
}
118+
119+
whisper_print_timings(context)
120+
121+
let system_info = self.system_info()
122+
let timings: whisper_timings = whisper_get_timings(context)
123+
let encode_ms = String(format: "%.2f", timings.encode_ms)
124+
let decode_ms = String(format: "%.2f", timings.decode_ms)
125+
let batchd_ms = String(format: "%.2f", timings.batchd_ms)
126+
let prompt_ms = String(format: "%.2f", timings.prompt_ms)
127+
return "| <todo> | iOS | \(system_info) | \(modelName) | \(n_threads) | 1 | \(encode_ms) | \(decode_ms) | \(batchd_ms) | \(prompt_ms) | <todo> |"
128+
}
129+
58130
static func createContext(path: String) throws -> WhisperContext {
59131
var params = whisper_context_default_params()
60132
#if targetEnvironment(simulator)
61133
params.use_gpu = false
62134
print("Running on the simulator, using CPU")
135+
#else
136+
params.flash_attn = true // Enabled by default for Metal
63137
#endif
64138
let context = whisper_init_from_file_with_params(path, params)
65139
if let context {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import Foundation
2+
3+
struct Model: Identifiable {
4+
var id = UUID()
5+
var name: String
6+
var info: String
7+
var url: String
8+
9+
var filename: String
10+
var fileURL: URL {
11+
FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent(filename)
12+
}
13+
14+
func fileExists() -> Bool {
15+
FileManager.default.fileExists(atPath: fileURL.path)
16+
}
17+
}

examples/whisper.swiftui/whisper.swiftui.demo/Models/WhisperState.swift

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Foundation
22
import SwiftUI
33
import AVFoundation
44

5+
56
@MainActor
67
class WhisperState: NSObject, ObservableObject, AVAudioRecorderDelegate {
78
@Published var isModelLoaded = false
@@ -14,7 +15,7 @@ class WhisperState: NSObject, ObservableObject, AVAudioRecorderDelegate {
1415
private var recordedFile: URL? = nil
1516
private var audioPlayer: AVAudioPlayer?
1617

17-
private var modelUrl: URL? {
18+
private var builtInModelUrl: URL? {
1819
Bundle.main.url(forResource: "ggml-base.en", withExtension: "bin", subdirectory: "models")
1920
}
2021

@@ -28,23 +29,51 @@ class WhisperState: NSObject, ObservableObject, AVAudioRecorderDelegate {
2829

2930
override init() {
3031
super.init()
32+
loadModel()
33+
}
34+
35+
func loadModel(path: URL? = nil, log: Bool = true) {
3136
do {
32-
try loadModel()
37+
whisperContext = nil
38+
if (log) { messageLog += "Loading model...\n" }
39+
let modelUrl = path ?? builtInModelUrl
40+
if let modelUrl {
41+
whisperContext = try WhisperContext.createContext(path: modelUrl.path())
42+
if (log) { messageLog += "Loaded model \(modelUrl.lastPathComponent)\n" }
43+
} else {
44+
if (log) { messageLog += "Could not locate model\n" }
45+
}
3346
canTranscribe = true
3447
} catch {
3548
print(error.localizedDescription)
36-
messageLog += "\(error.localizedDescription)\n"
49+
if (log) { messageLog += "\(error.localizedDescription)\n" }
3750
}
3851
}
39-
40-
private func loadModel() throws {
41-
messageLog += "Loading model...\n"
42-
if let modelUrl {
43-
whisperContext = try WhisperContext.createContext(path: modelUrl.path())
44-
messageLog += "Loaded model \(modelUrl.lastPathComponent)\n"
45-
} else {
46-
messageLog += "Could not locate model\n"
52+
53+
func benchCurrentModel() async {
54+
if whisperContext == nil {
55+
messageLog += "Cannot bench without loaded model\n"
56+
return
57+
}
58+
messageLog += "Benchmarking current model\n"
59+
let result = await whisperContext?.bench_full(modelName: "<current>")
60+
if (result != nil) { messageLog += result! + "\n" }
61+
}
62+
63+
func bench(models: [Model]) async {
64+
messageLog += "Benchmarking models\n"
65+
messageLog += "| CPU | OS | Config | Model | Th | FA | Enc. | Dec. | Bch5 | PP | Commit |\n"
66+
messageLog += "| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |\n"
67+
for model in models {
68+
loadModel(path: model.fileURL, log: false)
69+
if whisperContext == nil {
70+
messageLog += "Cannot bench without loaded model\n"
71+
break
72+
}
73+
let result = await whisperContext?.bench_full(modelName: model.name)
74+
if (result != nil) { messageLog += result! + "\n" }
4775
}
76+
messageLog += "Benchmarking completed\n"
4877
}
4978

5079
func transcribeSample() async {

examples/whisper.swiftui/whisper.swiftui.demo/UI/ContentView.swift

Lines changed: 115 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import SwiftUI
22
import AVFoundation
3+
import Foundation
34

45
struct ContentView: View {
56
@StateObject var whisperState = WhisperState()
@@ -29,15 +30,125 @@ struct ContentView: View {
2930
Text(verbatim: whisperState.messageLog)
3031
.frame(maxWidth: .infinity, alignment: .leading)
3132
}
33+
.font(.footnote)
34+
.padding()
35+
.background(Color.gray.opacity(0.1))
36+
.cornerRadius(10)
37+
38+
HStack {
39+
Button("Clear Logs", action: {
40+
whisperState.messageLog = ""
41+
})
42+
.font(.footnote)
43+
.buttonStyle(.bordered)
44+
45+
Button("Copy Logs", action: {
46+
UIPasteboard.general.string = whisperState.messageLog
47+
})
48+
.font(.footnote)
49+
.buttonStyle(.bordered)
50+
51+
Button("Bench", action: {
52+
Task {
53+
await whisperState.benchCurrentModel()
54+
}
55+
})
56+
.font(.footnote)
57+
.buttonStyle(.bordered)
58+
.disabled(!whisperState.canTranscribe)
59+
60+
Button("Bench All", action: {
61+
Task {
62+
await whisperState.bench(models: ModelSettingsView.getDownloadedModels())
63+
}
64+
})
65+
.font(.footnote)
66+
.buttonStyle(.bordered)
67+
.disabled(!whisperState.canTranscribe)
68+
}
69+
70+
NavigationLink(destination: ModelSettingsView(whisperState: whisperState)) {
71+
Text("View Models")
72+
}
73+
.font(.footnote)
74+
.padding()
3275
}
3376
.navigationTitle("Whisper SwiftUI Demo")
3477
.padding()
3578
}
3679
}
37-
}
3880

39-
struct ContentView_Previews: PreviewProvider {
40-
static var previews: some View {
41-
ContentView()
81+
struct ModelSettingsView: View {
82+
@ObservedObject var whisperState: WhisperState
83+
@Environment(\.dismiss) var dismiss
84+
85+
private static let models: [Model] = [
86+
Model(name: "tiny", info: "(F16, 75 MiB)", url: "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-tiny.bin", filename: "tiny.bin"),
87+
Model(name: "tiny-q5_1", info: "(31 MiB)", url: "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-tiny-q5_1.bin", filename: "tiny-q5_1.bin"),
88+
Model(name: "tiny-q8_0", info: "(42 MiB)", url: "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-tiny-q8_0.bin", filename: "tiny-q8_0.bin"),
89+
Model(name: "tiny.en", info: "(F16, 75 MiB)", url: "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-tiny.en.bin", filename: "tiny.en.bin"),
90+
Model(name: "tiny.en-q5_1", info: "(31 MiB)", url: "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-tiny.en-q5_1.bin", filename: "tiny.en-q5_1.bin"),
91+
Model(name: "tiny.en-q8_0", info: "(42 MiB)", url: "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-tiny.en-q8_0.bin", filename: "tiny.en-q8_0.bin"),
92+
Model(name: "base", info: "(F16, 142 MiB)", url: "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-base.bin", filename: "base.bin"),
93+
Model(name: "base-q5_1", info: "(57 MiB)", url: "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-base-q5_1.bin", filename: "base-q5_1.bin"),
94+
Model(name: "base-q8_0", info: "(78 MiB)", url: "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-base-q8_0.bin", filename: "base-q8_0.bin"),
95+
Model(name: "base.en", info: "(F16, 142 MiB)", url: "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-base.en.bin", filename: "base.en.bin"),
96+
Model(name: "base.en-q5_1", info: "(57 MiB)", url: "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-base.en-q5_1.bin", filename: "base.en-q5_1.bin"),
97+
Model(name: "base.en-q8_0", info: "(78 MiB)", url: "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-base.en-q8_0.bin", filename: "base.en-q8_0.bin"),
98+
Model(name: "small", info: "(F16, 466 MiB)", url: "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-small.bin", filename: "small.bin"),
99+
Model(name: "small-q5_1", info: "(181 MiB)", url: "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-small-q5_1.bin", filename: "small-q5_1.bin"),
100+
Model(name: "small-q8_0", info: "(252 MiB)", url: "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-small-q8_0.bin", filename: "small-q8_0.bin"),
101+
Model(name: "small.en", info: "(F16, 466 MiB)", url: "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-small.en.bin", filename: "small.en.bin"),
102+
Model(name: "small.en-q5_1", info: "(181 MiB)", url: "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-small.en-q5_1.bin", filename: "small.en-q5_1.bin"),
103+
Model(name: "small.en-q8_0", info: "(252 MiB)", url: "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-small.en-q8_0.bin", filename: "small.en-q8_0.bin"),
104+
Model(name: "medium", info: "(F16, 1.5 GiB)", url: "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-medium.bin", filename: "medium.bin"),
105+
Model(name: "medium-q5_0", info: "(514 MiB)", url: "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-medium-q5_0.bin", filename: "medium-q5_0.bin"),
106+
Model(name: "medium-q8_0", info: "(785 MiB)", url: "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-medium-q8_0.bin", filename: "medium-q8_0.bin"),
107+
Model(name: "medium.en", info: "(F16, 1.5 GiB)", url: "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-medium.en.bin", filename: "medium.en.bin"),
108+
Model(name: "medium.en-q5_0", info: "(514 MiB)", url: "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-medium.en-q5_0.bin", filename: "medium.en-q5_0.bin"),
109+
Model(name: "medium.en-q8_0", info: "(785 MiB)", url: "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-medium.en-q8_0.bin", filename: "medium.en-q8_0.bin"),
110+
Model(name: "large-v1", info: "(F16, 2.9 GiB)", url: "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-large.bin", filename: "large.bin"),
111+
Model(name: "large-v2", info: "(F16, 2.9 GiB)", url: "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-large-v2.bin", filename: "large-v2.bin"),
112+
Model(name: "large-v2-q5_0", info: "(1.1 GiB)", url: "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-large-v2-q5_0.bin", filename: "large-v2-q5_0.bin"),
113+
Model(name: "large-v2-q8_0", info: "(1.5 GiB)", url: "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-large-v2-q8_0.bin", filename: "large-v2-q8_0.bin"),
114+
Model(name: "large-v3", info: "(F16, 2.9 GiB)", url: "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-large-v3.bin", filename: "large-v3.bin"),
115+
Model(name: "large-v3-q5_0", info: "(1.1 GiB)", url: "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-large-v3-q5_0.bin", filename: "large-v3-q5_0.bin"),
116+
Model(name: "large-v3-turbo", info: "(F16, 1.5 GiB)", url: "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-large-v3-turbo.bin", filename: "large-v3-turbo.bin"),
117+
Model(name: "large-v3-turbo-q5_0", info: "(547 MiB)", url: "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-large-v3-turbo-q5_0.bin", filename: "large-v3-turbo-q5_0.bin"),
118+
Model(name: "large-v3-turbo-q8_0", info: "(834 MiB)", url: "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-large-v3-turbo-q8_0.bin", filename: "large-v3-turbo-q8_0.bin"),
119+
]
120+
121+
static func getDownloadedModels() -> [Model] {
122+
// Filter models that have been downloaded
123+
return models.filter {
124+
FileManager.default.fileExists(atPath: $0.fileURL.path())
125+
}
126+
}
127+
128+
func loadModel(model: Model) {
129+
Task {
130+
dismiss()
131+
whisperState.loadModel(path: model.fileURL)
132+
}
133+
}
134+
135+
var body: some View {
136+
List {
137+
Section(header: Text("Models")) {
138+
ForEach(ModelSettingsView.models) { model in
139+
DownloadButton(model: model)
140+
.onLoad(perform: loadModel)
141+
}
142+
}
143+
}
144+
.listStyle(GroupedListStyle())
145+
.navigationBarTitle("Model Settings", displayMode: .inline).toolbar {}
146+
}
42147
}
43148
}
149+
150+
//struct ContentView_Previews: PreviewProvider {
151+
// static var previews: some View {
152+
// ContentView()
153+
// }
154+
//}

0 commit comments

Comments
 (0)