Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

llama.swiftui : add bench functionality #4483

Merged
merged 12 commits into from
Dec 17, 2023
Prev Previous commit
Next Next commit
llama.swiftui : avoid data copy via "downloadTask"
  • Loading branch information
ggerganov committed Dec 17, 2023
commit 4ed98b90bc89cddd2834518cbd9db1aa98ef8ecd
23 changes: 13 additions & 10 deletions examples/llama.swiftui/llama.swiftui/UI/DownloadButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ struct DownloadButton: View {

@State private var status: String

@State private var downloadTask: URLSessionDataTask?
@State private var downloadTask: URLSessionDownloadTask?
@State private var progress = 0.0
@State private var observation: NSKeyValueObservation?

Expand All @@ -32,7 +32,10 @@ struct DownloadButton: View {
private func download() {
status = "downloading"
print("Downloading model \(modelName) from \(modelUrl)")
downloadTask = URLSession.shared.dataTask(with: URL(string: modelUrl)!) { data, response, error in
guard let url = URL(string: modelUrl) else { return }
let fileURL = DownloadButton.getFileURL(filename: filename)

downloadTask = URLSession.shared.downloadTask(with: url) { temporaryURL, response, error in
if let error = error {
print("Error: \(error.localizedDescription)")
return
Expand All @@ -43,24 +46,24 @@ struct DownloadButton: View {
return
}

if let data = data {
do {
print("Writing to \(filename)")
let fileURL = DownloadButton.getFileURL(filename: filename)
try data.write(to: fileURL)
do {
if let temporaryURL = temporaryURL {
try FileManager.default.copyItem(at: temporaryURL, to: fileURL)
print("Writing to \(filename) completed")

llamaState.cacheCleared = false

status = "downloaded"
try llamaState.loadModel(modelUrl: fileURL)
} catch let err {
print("Error: \(err.localizedDescription)")
}
} catch let err {
print("Error: \(err.localizedDescription)")
}
}

observation = downloadTask?.progress.observe(\.fractionCompleted) { progress, _ in
self.progress = progress.fractionCompleted
}

downloadTask?.resume()
}

Expand Down
Loading