Add register_stream for cross-thread GPU stream usage#3398
Open
TheTom wants to merge 1 commit intoml-explore:mainfrom
Open
Add register_stream for cross-thread GPU stream usage#3398TheTom wants to merge 1 commit intoml-explore:mainfrom
TheTom wants to merge 1 commit intoml-explore:mainfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
After #3348 made
CommandEncoderthread-local, evaluating arrays on a thread that did not create the stream fails because the encoder does not exist on the new thread.ThreadLocalStream(#3355) solves this for Python by creating per-thread streams, but there is no way to share a single stream across threads.register_stream(Stream)registers an existing stream's GPU command encoder on the calling thread, allowingeval()to succeed. It is a no-op if already registered, and is safe to call from any thread including the creating thread.This is needed for Swift concurrency where the runtime may hop tasks between threads, and for Python
threading.Threadworkflows that share a stream.Related: #3078
Changes
mlx/stream.h/mlx/stream.cpp: addregister_stream(Stream)python/src/stream.cpp: nanobind bindingpython/tests/test_threads.py: cross-thread eval test, idempotency testTest plan
python/tests/test_threads.pypasses (3/3)