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 thread safety and Swift 6 warnings. #123 #125

Closed

Conversation

johnmai-dev
Copy link
Contributor

Fix thread safety and Swift 6 warnings. #123

@@ -145,7 +145,7 @@ public class CohereModelInner: Module {
}
}

public class CohereModel: Module, LLMModel, KVCacheDimensionProvider {
public class CohereModel: Module, LLMModel, KVCacheDimensionProvider,@unchecked Sendable {
Copy link
Collaborator

Choose a reason for hiding this comment

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

The models can't be @unchecked Sendable as they are entirely not thread safe (the MLXArrays themselves cannot be used in a multithreaded manner)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If @unchecked Sendable is not used, there will be a Task warning in createModelSynchronous.

Value of non-Sendable type '@isolated(any) @async @callee_guaranteed @substituted <τ_0_0> () -> @out τ_0_0 for <()>' accessed after being transferred; later accesses could race; this is an error in the Swift 6 language mode
Xcode 2024-09-10 23 21 40

semaphore.signal()
}

semaphore.wait()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Using semaphores like this is an anti-pattern:

func setReplacement(_ replacement: String, for tokenizer: String) {
replacementTokenizers[tokenizer] = replacement
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

This part is fine but as you noted it makes async leak into parts we don't want. I think this could be encapsulated like this but maybe use an NSLock to guard the critical section, then it stays synchronous

@davidkoski
Copy link
Collaborator

  • none of the models can be marked as Sendable -- it just isn't possible to make them multithreaded, this is why we have the actor to wrap them
  • to solve the async issue I think we should use NSLock to guard the mutable state

davidkoski added a commit that referenced this pull request Sep 12, 2024
- replaces #125 with simpler mechanism (NSLock)

Co-authored-by: John Mai <maiqingqiang@gmail.com>
@davidkoski
Copy link
Collaborator

Check out #126 -- I used what you did but made it NSLock instead. This kept everything synchronous but let's use also make it thread safe.

@johnmai-dev
Copy link
Contributor Author

Check out #126 -- I used what you did but made it NSLock instead. This kept everything synchronous but let's use also make it thread safe.

Nice! I've been a bit busy lately, so I'll close the current PR first.

davidkoski added a commit that referenced this pull request Sep 30, 2024
- replaces #125 with simpler mechanism (NSLock)

Co-authored-by: John Mai <maiqingqiang@gmail.com>
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