Skip to content

Commit b73c4db

Browse files
committed
fix #10: clean up unquantized models
1 parent 1a181de commit b73c4db

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

Diff for: LlamaChat/LlamaChatApp.swift

+8
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,16 @@ enum WindowIdentifier: String {
1313
case modelContext
1414
}
1515

16+
class LlamaChatAppDelegate: NSObject, NSApplicationDelegate {
17+
func applicationDidFinishLaunching(_ notification: Notification) {
18+
ModelFileManager.shared.cleanUpUnquantizedModelFiles()
19+
}
20+
}
21+
1622
@main
1723
struct LlamaChatApp: App {
24+
@NSApplicationDelegateAdaptor var appDelegate: LlamaChatAppDelegate
25+
1826
@StateObject var chatSources: ChatSources
1927
@StateObject var chatModels: ChatModels
2028
@StateObject var messagesModel: MessagesModel

Diff for: LlamaChat/model/models/ModelFileManager.swift

+25
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ class ModelDirectory {
3333
}
3434

3535
class ModelFileManager {
36+
static let shared = ModelFileManager()
37+
38+
private init() {}
39+
3640
private var modelsDirectoryURL: URL? {
3741
return applicationSupportDirectoryURL()?.appendingPathComponent("models")
3842
}
@@ -54,4 +58,25 @@ class ModelFileManager {
5458
guard let modelsDirectory = modelsDirectoryURL else { return nil }
5559
return modelsDirectory.appendingPathComponent(id, isDirectory: true)
5660
}
61+
62+
// Fixes any issues caused by https://github.com/alexrozanski/LlamaChat/issues/10
63+
func cleanUpUnquantizedModelFiles() {
64+
guard let modelsDirectoryURL else { return }
65+
66+
let enumerator = FileManager.default.enumerator(at: modelsDirectoryURL, includingPropertiesForKeys: nil, options: [])
67+
enumerator?.forEach { itemURL in
68+
guard let itemURL = itemURL as? URL else { return }
69+
70+
// This is hardcoded by the conversion script
71+
let unquantizedModelName = "ggml-model-f16.bin"
72+
73+
if itemURL.lastPathComponent == unquantizedModelName {
74+
do {
75+
try FileManager.default.removeItem(at: itemURL)
76+
} catch {
77+
print("WARNING: Couldn't clean up unquantized model at", itemURL)
78+
}
79+
}
80+
}
81+
}
5782
}

Diff for: LlamaChat/model/sources/ChatSources.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ class ChatSources: ObservableObject {
162162

163163
func remove(source: ChatSource) {
164164
_ = sources.firstIndex(where: { $0 === source }).map { sources.remove(at: $0) }
165-
if let modelDirectoryId = source.modelDirectoryId, let modelDirectory = ModelFileManager().modelDirectory(with: modelDirectoryId) {
165+
if let modelDirectoryId = source.modelDirectoryId, let modelDirectory = ModelFileManager.shared.modelDirectory(with: modelDirectoryId) {
166166
modelDirectory.cleanUp()
167167
}
168168
}

Diff for: LlamaChat/viewmodel/sources/convert/ConvertSourceViewModel.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ class ConvertSourceViewModel: ObservableObject {
137137
state = .converting
138138

139139
do {
140-
let modelDirectory = try ModelFileManager().makeNewModelDirectory()
140+
let modelDirectory = try ModelFileManager.shared.makeNewModelDirectory()
141141
self.modelDirectory = modelDirectory
142142

143143
Task.init {

0 commit comments

Comments
 (0)