-
Notifications
You must be signed in to change notification settings - Fork 110
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
fix swift 6 warnings - thread safe tokenizer and model config #126
Conversation
davidkoski
commented
Sep 12, 2024
•
edited
Loading
edited
- based on Fix thread safety and Swift 6 warnings. #123 #125 with simpler mechanism (NSLock)
- replaces #125 with simpler mechanism (NSLock) Co-authored-by: John Mai <maiqingqiang@gmail.com>
private static func createLlamaModel(url: URL) throws -> LLMModel { | ||
let configuration = try JSONDecoder().decode( | ||
LlamaConfiguration.self, from: Data(contentsOf: url)) | ||
return LlamaModel(configuration) | ||
} | ||
|
||
private static var creators: [String: (URL) throws -> LLMModel] = [ | ||
private var creators: [String: @Sendable (URL) throws -> LLMModel] = [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the key -- we want to mark it as storing thread safe (Sendable) closures.
creators[type] = creator | ||
lock.withLock { | ||
creators[type] = creator | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
by guarding the mutable state with the lock we can mark the type as Sendable (unchecked)
|
||
} | ||
|
||
private let modelTypeRegistry = ModelTypeRegistry() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made it a private global rather than a static property of ModelType. ModelType is the only access to it however
"CohereTokenizer": "PreTrainedTokenizer", | ||
] | ||
|
||
public subscript(key: String) -> String? { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This ends up being source compatible with replacementTokenizers: [String:String]
but uses an NSLock for thread safety.
@@ -7,7 +7,7 @@ import MLXNN | |||
import MLXOptimizers | |||
import MLXRandom | |||
|
|||
#if swift(>=6.0) | |||
#if swift(>=5.10) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixing some other warnings -- we don't necessarily need swift 6, but not all swift 5 understands this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🙏 thanks!
…lore#126) - replaces ml-explore#125 with simpler mechanism (NSLock) Co-authored-by: John Mai <maiqingqiang@gmail.com>