Skip to content

Commit 40bc2d2

Browse files
committed
feat: Display Time To First Token in MLXChatExample
1 parent b1d5708 commit 40bc2d2

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

Applications/MLXChatExample/ViewModels/ChatViewModel.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ class ChatViewModel {
4949
generateCompletionInfo?.tokensPerSecond ?? 0
5050
}
5151

52+
/// Time to generate the first token in seconds
53+
var timeToFirstToken: Double {
54+
generateCompletionInfo?.promptTime ?? 0
55+
}
56+
5257
/// Progress of the current model download, if any
5358
var modelDownloadProgress: Progress? {
5459
mlxService.modelDownloadProgress

Applications/MLXChatExample/Views/Toolbar/ChatToolbarView.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ struct ChatToolbarView: View {
2929
vm.clear([.chat, .meta])
3030
} label: {
3131
GenerationInfoView(
32-
tokensPerSecond: vm.tokensPerSecond
32+
tokensPerSecond: vm.tokensPerSecond,
33+
timeToFirstToken: vm.timeToFirstToken
3334
)
3435
}
3536

Applications/MLXChatExample/Views/Toolbar/GenerationInfoView.swift

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,23 @@ import SwiftUI
99

1010
struct GenerationInfoView: View {
1111
let tokensPerSecond: Double
12+
let timeToFirstToken: Double
1213

1314
var body: some View {
14-
Text("\(tokensPerSecond, format: .number.precision(.fractionLength(2))) tokens/s")
15+
HStack {
16+
if timeToFirstToken > 0 {
17+
Text(String(format: "TTFT: %.2f s", timeToFirstToken))
18+
}
19+
if tokensPerSecond > 0 {
20+
Text(String(format: "TPS: %.2f", tokensPerSecond))
21+
}
22+
}
23+
.lineLimit(1)
24+
.frame(minWidth: 150, alignment: .leading)
1525
}
1626
}
1727

1828
#Preview {
19-
GenerationInfoView(tokensPerSecond: 58.5834)
29+
GenerationInfoView(tokensPerSecond: 58.5834, timeToFirstToken: 1.234)
30+
.padding()
2031
}

0 commit comments

Comments
 (0)