Skip to content

Commit ebd5ced

Browse files
committed
whisper.swiftui : get device name / os from UIDevice
1 parent 0ed4702 commit ebd5ced

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

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

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Foundation
2+
import UIKit
23
import whisper
34

45
enum WhisperError: Error {
@@ -64,67 +65,69 @@ actor WhisperContext {
6465
}
6566

6667
func benchFull(modelName: String) async -> String {
67-
let n_threads = Int32(min(4, cpuCount())) // Default in whisper.cpp
68+
let nThreads = Int32(min(4, cpuCount())) // Default in whisper.cpp
6869

69-
let n_mels = whisper_model_n_mels(context)
70-
if (whisper_set_mel(context, nil, 0, n_mels) != 0) {
70+
let nMels = whisper_model_n_mels(context)
71+
if (whisper_set_mel(context, nil, 0, nMels) != 0) {
7172
return "error: failed to set mel"
7273
}
7374

7475
// heat encoder
75-
if (whisper_encode(context, 0, n_threads) != 0) {
76+
if (whisper_encode(context, 0, nThreads) != 0) {
7677
return "error: failed to encode"
7778
}
7879

7980
var tokens = [whisper_token](repeating: 0, count: 512)
8081

8182
// prompt heat
82-
if (whisper_decode(context, &tokens, 256, 0, n_threads) != 0) {
83+
if (whisper_decode(context, &tokens, 256, 0, nThreads) != 0) {
8384
return "error: failed to decode"
8485
}
8586

8687
// text-generation heat
87-
if (whisper_decode(context, &tokens, 1, 256, n_threads) != 0) {
88+
if (whisper_decode(context, &tokens, 1, 256, nThreads) != 0) {
8889
return "error: failed to decode"
8990
}
9091

9192
whisper_reset_timings(context)
9293

9394
// actual run
94-
if (whisper_encode(context, 0, n_threads) != 0) {
95+
if (whisper_encode(context, 0, nThreads) != 0) {
9596
return "error: failed to encode"
9697
}
9798

9899
// text-generation
99100
for i in 0..<256 {
100-
if (whisper_decode(context, &tokens, 1, Int32(i), n_threads) != 0) {
101+
if (whisper_decode(context, &tokens, 1, Int32(i), nThreads) != 0) {
101102
return "error: failed to decode"
102103
}
103104
}
104105

105106
// batched decoding
106107
for _ in 0..<64 {
107-
if (whisper_decode(context, &tokens, 5, 0, n_threads) != 0) {
108+
if (whisper_decode(context, &tokens, 5, 0, nThreads) != 0) {
108109
return "error: failed to decode"
109110
}
110111
}
111112

112113
// prompt processing
113114
for _ in 0..<16 {
114-
if (whisper_decode(context, &tokens, 256, 0, n_threads) != 0) {
115+
if (whisper_decode(context, &tokens, 256, 0, nThreads) != 0) {
115116
return "error: failed to decode"
116117
}
117118
}
118119

119120
whisper_print_timings(context)
120121

121-
let system_info = self.systemInfo()
122+
let deviceModel = UIDevice.current.model
123+
let systemName = UIDevice.current.systemName
124+
let systemInfo = self.systemInfo()
122125
let timings: whisper_timings = whisper_get_timings(context).pointee
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> |"
126+
let encodeMs = String(format: "%.2f", timings.encode_ms)
127+
let decodeMs = String(format: "%.2f", timings.decode_ms)
128+
let batchdMs = String(format: "%.2f", timings.batchd_ms)
129+
let promptMs = String(format: "%.2f", timings.prompt_ms)
130+
return "| \(deviceModel) | \(systemName) | \(systemInfo) | \(modelName) | \(nThreads) | 1 | \(encodeMs) | \(decodeMs) | \(batchdMs) | \(promptMs) | <todo> |"
128131
}
129132

130133
static func createContext(path: String) throws -> WhisperContext {

0 commit comments

Comments
 (0)