Skip to content

Commit 2b597cf

Browse files
committed
whisper.swiftui : add memcpy and ggml_mul_mat (commented)
1 parent cc692b0 commit 2b597cf

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ actor WhisperContext {
5656
return transcription
5757
}
5858

59+
static func benchMemcpy(nThreads: Int32) async -> String {
60+
return String.init(cString: whisper_bench_memcpy_str(nThreads))
61+
}
62+
63+
static func benchGgmlMulMat(nThreads: Int32) async -> String {
64+
return String.init(cString: whisper_bench_ggml_mul_mat_str(nThreads))
65+
}
66+
5967
private func systemInfo() -> String {
6068
var info = ""
6169
if (ggml_cpu_has_neon() != 0) { info += "NEON " }
@@ -64,9 +72,7 @@ actor WhisperContext {
6472
return String(info.dropLast())
6573
}
6674

67-
func benchFull(modelName: String) async -> String {
68-
let nThreads = Int32(min(4, cpuCount())) // Default in whisper.cpp
69-
75+
func benchFull(modelName: String, nThreads: Int32) async -> String {
7076
let nMels = whisper_model_n_mels(context)
7177
if (whisper_set_mel(context, nil, 0, nMels) != 0) {
7278
return "error: failed to set mel"

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,21 @@ class WhisperState: NSObject, ObservableObject, AVAudioRecorderDelegate {
5454
messageLog += "Cannot bench without loaded model\n"
5555
return
5656
}
57-
messageLog += "Benchmarking current model\n"
58-
let result = await whisperContext?.benchFull(modelName: "<current>")
57+
messageLog += "Running benchmark for loaded model\n"
58+
let result = await whisperContext?.benchFull(modelName: "<current>", nThreads: Int32(min(4, cpuCount())))
5959
if (result != nil) { messageLog += result! + "\n" }
6060
}
6161

6262
func bench(models: [Model]) async {
63-
messageLog += "Benchmarking models\n"
63+
let nThreads = Int32(min(4, cpuCount()))
64+
65+
// messageLog += "Running memcpy benchmark\n"
66+
// messageLog += await WhisperContext.benchMemcpy(nThreads: nThreads) + "\n"
67+
//
68+
// messageLog += "Running ggml_mul_mat benchmark with \(nThreads) threads\n"
69+
// messageLog += await WhisperContext.benchGgmlMulMat(nThreads: nThreads) + "\n"
70+
71+
messageLog += "Running benchmark for all downloaded models\n"
6472
messageLog += "| CPU | OS | Config | Model | Th | FA | Enc. | Dec. | Bch5 | PP | Commit |\n"
6573
messageLog += "| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |\n"
6674
for model in models {
@@ -69,7 +77,7 @@ class WhisperState: NSObject, ObservableObject, AVAudioRecorderDelegate {
6977
messageLog += "Cannot bench without loaded model\n"
7078
break
7179
}
72-
let result = await whisperContext?.benchFull(modelName: model.name)
80+
let result = await whisperContext?.benchFull(modelName: model.name, nThreads: nThreads)
7381
if (result != nil) { messageLog += result! + "\n" }
7482
}
7583
messageLog += "Benchmarking completed\n"
@@ -188,3 +196,8 @@ class WhisperState: NSObject, ObservableObject, AVAudioRecorderDelegate {
188196
isRecording = false
189197
}
190198
}
199+
200+
201+
fileprivate func cpuCount() -> Int {
202+
ProcessInfo.processInfo.processorCount
203+
}

0 commit comments

Comments
 (0)