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

fix swift 6 warnings - thread safe tokenizer and model config #126

Merged
merged 1 commit into from
Sep 30, 2024

Conversation

davidkoski
Copy link
Collaborator

@davidkoski davidkoski commented Sep 12, 2024

- 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] = [
Copy link
Collaborator Author

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
}
Copy link
Collaborator Author

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()
Copy link
Collaborator Author

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? {
Copy link
Collaborator Author

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)
Copy link
Collaborator Author

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.

Copy link
Member

@awni awni left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙏 thanks!

@davidkoski davidkoski merged commit caa5caf into main Sep 30, 2024
1 check passed
@davidkoski davidkoski deleted the llm-factories branch September 30, 2024 21:22
DePasqualeOrg pushed a commit to DePasqualeOrg/mlx-swift-examples that referenced this pull request Oct 1, 2024
…lore#126)

- replaces ml-explore#125 with simpler mechanism (NSLock)

Co-authored-by: John Mai <maiqingqiang@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants