Skip to content

Commit afd997a

Browse files
authored
llama.swiftui : fix infinite loop, ouput timings, buff UI (#4674)
* fix infinite loop * slight UI simplification, clearer UX * clearer UI text, add timings to completion log
1 parent c8255f8 commit afd997a

File tree

4 files changed

+29
-37
lines changed

4 files changed

+29
-37
lines changed

examples/llama.swiftui/llama.cpp.swift/LibLlama.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import Foundation
22

3+
// To use this in your own project, add llama.cpp as a swift package dependency
4+
// and uncomment this import line.
35
// import llama
46

57
enum LlamaError: Error {

examples/llama.swiftui/llama.swiftui/Models/LlamaState.swift

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Foundation
44
class LlamaState: ObservableObject {
55
@Published var messageLog = ""
66
@Published var cacheCleared = false
7+
let NS_PER_S = 1_000_000_000.0
78

89
private var llamaContext: LlamaContext?
910
private var defaultModelUrl: URL? {
@@ -20,12 +21,12 @@ class LlamaState: ObservableObject {
2021
}
2122

2223
func loadModel(modelUrl: URL?) throws {
23-
messageLog += "Loading model...\n"
2424
if let modelUrl {
25+
messageLog += "Loading model...\n"
2526
llamaContext = try LlamaContext.create_context(path: modelUrl.path())
2627
messageLog += "Loaded model \(modelUrl.lastPathComponent)\n"
2728
} else {
28-
messageLog += "Could not locate model\n"
29+
messageLog += "Load a model from the list below\n"
2930
}
3031
}
3132

@@ -34,15 +35,29 @@ class LlamaState: ObservableObject {
3435
return
3536
}
3637

38+
let t_start = DispatchTime.now().uptimeNanoseconds
3739
await llamaContext.completion_init(text: text)
40+
let t_heat_end = DispatchTime.now().uptimeNanoseconds
41+
let t_heat = Double(t_heat_end - t_start) / NS_PER_S
42+
3843
messageLog += "\(text)"
3944

40-
while await llamaContext.n_cur <= llamaContext.n_len {
45+
while await llamaContext.n_cur < llamaContext.n_len {
4146
let result = await llamaContext.completion_loop()
4247
messageLog += "\(result)"
4348
}
49+
50+
let t_end = DispatchTime.now().uptimeNanoseconds
51+
let t_generation = Double(t_end - t_heat_end) / NS_PER_S
52+
let tokens_per_second = Double(await llamaContext.n_len) / t_generation
53+
4454
await llamaContext.clear()
45-
messageLog += "\n\ndone\n"
55+
messageLog += """
56+
\n
57+
Done
58+
Heat up took \(t_heat)s
59+
Generated \(tokens_per_second) t/s\n
60+
"""
4661
}
4762

4863
func bench() async {
@@ -56,10 +71,10 @@ class LlamaState: ObservableObject {
5671
messageLog += await llamaContext.model_info() + "\n"
5772

5873
let t_start = DispatchTime.now().uptimeNanoseconds
59-
await llamaContext.bench(pp: 8, tg: 4, pl: 1) // heat up
74+
let _ = await llamaContext.bench(pp: 8, tg: 4, pl: 1) // heat up
6075
let t_end = DispatchTime.now().uptimeNanoseconds
6176

62-
let t_heat = Double(t_end - t_start) / 1_000_000_000.0
77+
let t_heat = Double(t_end - t_start) / NS_PER_S
6378
messageLog += "Heat up time: \(t_heat) seconds, please wait...\n"
6479

6580
// if more than 5 seconds, then we're probably running on a slow device

examples/llama.swiftui/llama.swiftui/UI/ContentView.swift

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -42,96 +42,71 @@ struct ContentView: View {
4242
Button("Send") {
4343
sendText()
4444
}
45-
.padding(8)
46-
.background(Color.blue)
47-
.foregroundColor(.white)
48-
.cornerRadius(8)
4945

5046
Button("Bench") {
5147
bench()
5248
}
53-
.padding(8)
54-
.background(Color.blue)
55-
.foregroundColor(.white)
56-
.cornerRadius(8)
5749

5850
Button("Clear") {
5951
clear()
6052
}
61-
.padding(8)
62-
.background(Color.blue)
63-
.foregroundColor(.white)
64-
.cornerRadius(8)
6553

6654
Button("Copy") {
6755
UIPasteboard.general.string = llamaState.messageLog
6856
}
69-
.padding(8)
70-
.background(Color.blue)
71-
.foregroundColor(.white)
72-
.cornerRadius(8)
73-
}
57+
}.buttonStyle(.bordered)
7458

75-
VStack {
59+
VStack(alignment: .leading) {
7660
DownloadButton(
7761
llamaState: llamaState,
7862
modelName: "TinyLlama-1.1B (Q4_0, 0.6 GiB)",
7963
modelUrl: "https://huggingface.co/TheBloke/TinyLlama-1.1B-1T-OpenOrca-GGUF/resolve/main/tinyllama-1.1b-1t-openorca.Q4_0.gguf?download=true",
8064
filename: "tinyllama-1.1b-1t-openorca.Q4_0.gguf"
8165
)
82-
.font(.system(size: 12))
83-
.padding(.top, 4)
84-
.frame(maxWidth: .infinity, alignment: .leading)
8566

8667
DownloadButton(
8768
llamaState: llamaState,
8869
modelName: "TinyLlama-1.1B (Q8_0, 1.1 GiB)",
8970
modelUrl: "https://huggingface.co/TheBloke/TinyLlama-1.1B-1T-OpenOrca-GGUF/resolve/main/tinyllama-1.1b-1t-openorca.Q8_0.gguf?download=true",
9071
filename: "tinyllama-1.1b-1t-openorca.Q8_0.gguf"
9172
)
92-
.font(.system(size: 12))
9373

9474
DownloadButton(
9575
llamaState: llamaState,
9676
modelName: "TinyLlama-1.1B (F16, 2.2 GiB)",
9777
modelUrl: "https://huggingface.co/ggml-org/models/resolve/main/tinyllama-1.1b/ggml-model-f16.gguf?download=true",
9878
filename: "tinyllama-1.1b-f16.gguf"
9979
)
100-
.font(.system(size: 12))
101-
.frame(maxWidth: .infinity, alignment: .leading)
10280

10381
DownloadButton(
10482
llamaState: llamaState,
10583
modelName: "Phi-2.7B (Q4_0, 1.6 GiB)",
10684
modelUrl: "https://huggingface.co/ggml-org/models/resolve/main/phi-2/ggml-model-q4_0.gguf?download=true",
10785
filename: "phi-2-q4_0.gguf"
10886
)
109-
.font(.system(size: 12))
11087

11188
DownloadButton(
11289
llamaState: llamaState,
11390
modelName: "Phi-2.7B (Q8_0, 2.8 GiB)",
11491
modelUrl: "https://huggingface.co/ggml-org/models/resolve/main/phi-2/ggml-model-q8_0.gguf?download=true",
11592
filename: "phi-2-q8_0.gguf"
11693
)
117-
.font(.system(size: 12))
118-
.frame(maxWidth: .infinity, alignment: .leading)
11994

12095
DownloadButton(
12196
llamaState: llamaState,
12297
modelName: "Mistral-7B-v0.1 (Q4_0, 3.8 GiB)",
12398
modelUrl: "https://huggingface.co/TheBloke/Mistral-7B-v0.1-GGUF/resolve/main/mistral-7b-v0.1.Q4_0.gguf?download=true",
12499
filename: "mistral-7b-v0.1.Q4_0.gguf"
125100
)
126-
.font(.system(size: 12))
127101

128102
Button("Clear downloaded models") {
129103
ContentView.cleanupModelCaches()
130104
llamaState.cacheCleared = true
131105
}
132-
.padding(8)
133-
.font(.system(size: 12))
134106
}
107+
.padding(.top, 4)
108+
.font(.system(size: 12))
109+
.frame(maxWidth: .infinity, alignment: .leading)
135110
}
136111
.padding()
137112
}

examples/llama.swiftui/llama.swiftui/UI/DownloadButton.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ struct DownloadButton: View {
9393
print("Error: \(err.localizedDescription)")
9494
}
9595
}) {
96-
Text("\(modelName) (Downloaded)")
96+
Text("Load \(modelName)")
9797
}
9898
} else {
9999
Text("Unknown status")

0 commit comments

Comments
 (0)