Skip to content

Commit 85b11c2

Browse files
shoumikhinfacebook-github-bot
authored andcommitted
Ease main thread by updating UI on every Nth token only. (#2760)
Summary: . Reviewed By: kirklandsign Differential Revision: D55532081
1 parent f0c6666 commit 85b11c2

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

examples/demo-apps/apple_ios/LLaMA/LLaMA/Application/ContentView.swift

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -204,15 +204,21 @@ struct ContentView: View {
204204
return
205205
}
206206
do {
207+
var tokens: [String] = []
207208
try runnerHolder.runner?.generate(text, sequenceLength: seq_len) { token in
208-
209-
DispatchQueue.main.async {
210-
withAnimation {
211-
var message = messages.removeLast()
212-
message.text += token
213-
message.tokenCount += 1
214-
message.dateUpdated = Date()
215-
messages.append(message)
209+
tokens.append(token)
210+
if tokens.count > 2 {
211+
let text = tokens.joined()
212+
let count = text.count
213+
tokens = []
214+
DispatchQueue.main.async {
215+
withAnimation {
216+
var message = messages.removeLast()
217+
message.text += text
218+
message.tokenCount += count
219+
message.dateUpdated = Date()
220+
messages.append(message)
221+
}
216222
}
217223
}
218224
if shouldStopGenerating {

examples/demo-apps/apple_ios/LLaMA/LLaMA/Application/ResourceMonitor.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ final class ResourceMonitor: ObservableObject {
1212
}
1313

1414
public func start() {
15-
memoryUpdateTimer = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true) { [weak self] _ in
15+
memoryUpdateTimer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { [weak self] _ in
1616
self?.updateMemoryUsage()
1717
}
1818
}

0 commit comments

Comments
 (0)