Repository navigation
Conversation
MLX ml-explore/mlx#3348 (Make CommandEncoder thread local) makes each thread maintain its own Metal command encoder. The module-level generation_stream created at import time belongs to the main thread's command encoder. When generation runs in a thread pool (e.g., via vllm-mlx BatchedEngine), mx.eval and mx.synchronize on that stream fail with: RuntimeError: There is no Stream(gpu, 0) in current thread. Fix: replace the module-level mx.Stream with a thread-local factory function. Each thread gets its own stream on first use. The public name `generation_stream` becomes a function returning the current thread's stream, keeping the call-site diff minimal. Backward compatible: on MLX versions without thread-local command encoders, per-thread streams work identically to a shared stream.
Member
|
Closing in favor of #1090 which does a more comprehensive fix. |
4 tasks done
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
Make
generation_streamthread-local to work with MLX's thread-local CommandEncoder (ml-explore/mlx#3348).Problem
generation_stream = mx.new_stream(mx.default_device())is created at module import on the main thread. When generation runs in a worker thread (e.g., vllm-mlx BatchedEngine usesasyncio.to_thread), MLX's thread-local CommandEncoder cannot find the stream:This breaks any server that dispatches generation to a thread pool, which is the standard pattern for async inference servers.
Fix
Replace the module-level
mx.Streamwith athreading.local()-backed factory function. Each thread creates its own stream on first use.Call sites change from
generation_streamtogeneration_stream(). 7 call sites updated.Backward compatible: on MLX versions without thread-local command encoders, per-thread streams work identically.
Test plan