feat: adds gpu usages stat in the toolbar (#36)
* feat: adds gpu usages stat in the toolbar
This commit is contained in:
@@ -12,6 +12,7 @@ struct ContentView: View {
|
||||
|
||||
@State var prompt = "compare python and swift"
|
||||
@State var llm = LLMEvaluator()
|
||||
@Environment(DeviceStat.self) private var deviceStat
|
||||
|
||||
enum displayStyle: String, CaseIterable, Identifiable {
|
||||
case plain, markdown
|
||||
@@ -82,6 +83,23 @@ struct ContentView: View {
|
||||
}
|
||||
.padding()
|
||||
.toolbar {
|
||||
ToolbarItem {
|
||||
Label(
|
||||
"GPU Usage: \(deviceStat.gpuUsage.activeMemory.formatted(.byteCount(style: .memory)))",
|
||||
systemImage: "info.circle.fill"
|
||||
)
|
||||
.labelStyle(.titleAndIcon)
|
||||
.padding(.horizontal)
|
||||
.help(
|
||||
Text(
|
||||
"""
|
||||
Active Memory: \(deviceStat.gpuUsage.activeMemory.formatted(.byteCount(style: .memory)))/\(GPU.memoryLimit.formatted(.byteCount(style: .memory)))
|
||||
Cache Memory: \(deviceStat.gpuUsage.cacheMemory.formatted(.byteCount(style: .memory)))/\(GPU.cacheLimit.formatted(.byteCount(style: .memory)))
|
||||
Peak Memory: \(deviceStat.gpuUsage.peakMemory.formatted(.byteCount(style: .memory)))
|
||||
"""
|
||||
)
|
||||
)
|
||||
}
|
||||
ToolbarItem(placement: .primaryAction) {
|
||||
Button {
|
||||
Task {
|
||||
@@ -216,7 +234,7 @@ class LLMEvaluator {
|
||||
|
||||
await MainActor.run {
|
||||
running = false
|
||||
self.stat += " Token/second: \(String(format: "%.3f", tokensPerSecond))"
|
||||
self.stat += " Tokens/second: \(String(format: "%.3f", tokensPerSecond))"
|
||||
}
|
||||
|
||||
} catch {
|
||||
|
||||
@@ -7,6 +7,7 @@ struct LLMEvalApp: App {
|
||||
var body: some Scene {
|
||||
WindowGroup {
|
||||
ContentView()
|
||||
.environment(DeviceStat())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
42
Applications/LLMEval/ViewModels/DeviceStat.swift
Normal file
42
Applications/LLMEval/ViewModels/DeviceStat.swift
Normal file
@@ -0,0 +1,42 @@
|
||||
import Foundation
|
||||
import LLM
|
||||
import MLX
|
||||
|
||||
@Observable
|
||||
class DeviceStat {
|
||||
var gpuUsage = GPU.snapshot()
|
||||
private var initialGPUSnapshot = GPU.snapshot()
|
||||
private var timer: Timer?
|
||||
|
||||
init() {
|
||||
startTimer()
|
||||
}
|
||||
|
||||
deinit {
|
||||
stopTimer()
|
||||
}
|
||||
|
||||
private func startTimer() {
|
||||
timer?.invalidate()
|
||||
timer = Timer.scheduledTimer(withTimeInterval: 2.0, repeats: true) { [weak self] _ in
|
||||
self?.updateStats()
|
||||
}
|
||||
}
|
||||
|
||||
private func stopTimer() {
|
||||
timer?.invalidate()
|
||||
timer = nil
|
||||
}
|
||||
|
||||
private func updateStats() {
|
||||
updateGPUUsages()
|
||||
}
|
||||
|
||||
private func updateGPUUsages() {
|
||||
let gpuSnapshotDelta = initialGPUSnapshot.delta(GPU.snapshot())
|
||||
DispatchQueue.main.async { [weak self] in
|
||||
self?.gpuUsage = gpuSnapshotDelta
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user