Repository navigation
Conversation
The factory discarded the stream it was handed and returned Device.defaultStream(), so every attempt to target a non-default stream silently ran on the default one. Compare device(_:) two lines above, which uses its parameter. The effect is that a second stream is unreachable from Swift. Each MLX stream owns its own Metal command queue (CommandEncoder's constructor calls newCommandQueue and labels it by stream index), so with every op collapsing onto one stream all GPU work funnels through a single queue. Observed before the fix, with two freshly created GPU streams: Stream(Device(gpu, 0), 0) a Stream(Device(gpu, 0), 1) b Stream(Device(gpu, 0), 2) default Stream(Device(gpu, 0), 2) StreamOrDevice.stream(a) Stream(Device(gpu, 0), 2) StreamOrDevice.stream(b) and after it, .stream(a) resolves to index 0 and .stream(b) to index 1. This is a correctness fix, not a performance one. Splitting independent matmuls across two streams measured slower on an M1 Pro once the streams were actually reachable (3.12 -> 8.60 ms for 8 kernels at 64 rows), because cross-stream joins synchronise and each stream carries its own command buffer. The value here is that stream selection does what it says.
Author
|
Closing to keep my open PRs down to one. The bug is real if anyone wants it later:
Happy to reopen. |
This was referenced Oct 26, 2026
davidtai
added a commit
that referenced
this pull request
Oct 27, 2026
Source/Cmlx/mlx moves from ea318ee0 to e637499f, the head of Layr-Labs/mlx#41: the fast fence is updated before the event fence, fenced inputs stay in device memory and take their waits first, a sealed frame over INT_MAX is refused, and the JACCL mesh shares its failure flag across wires and does all_gather offsets in 64 bits. The range changes .cpp files that the package already compiles and headers that it does not mirror (distributed/sealed.h, sealed_stream.h, jaccl/lib). No Metal kernel, JIT source, CPU preamble input or CUDA device file changes, so mlx-generated does not change. The Metal.hpp commit stamp is written only by a cmake run and is left as it is. Source/Cmlx/mlx-c moves from 73720efb to 2ac897e3, the head of Layr-Labs/mlx-c#15. Only comments in mlx/c/distributed_group.h change; both mirrored copies are refreshed as tools/update-mlx.sh and tools/update-mlx-xcodeproj.sh copy them. The other 28 headers are unchanged byte for byte. fork.yaml: the Submodule pins prose names the new pins and fixes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Proposed changes
StreamOrDevice.stream(_:)discarded the stream it was given:Compare
device(_:)two lines above, which uses its parameter. The consequence is that anon-default stream is unreachable from Swift — every
.stream(x)silently resolves to thedefault stream, so an op you meant to place on a second stream runs on the first.
That matters beyond the API contract because each MLX stream owns its own Metal command queue
(
CommandEncoder's constructor callsnewCommandQueue()and labels it by stream index), so withevery op collapsing onto one stream, all GPU work funnels through a single queue.
Reproduction
Two freshly created GPU streams, printed before the fix:
After the fix
.stream(a)resolves to index 0 and.stream(b)to index 1.This is a correctness fix, not a speedup
Worth stating plainly so nobody reads a throughput win into it. I went looking for exactly that
and did not find one. With streams actually reachable, splitting 8 independent quantized matmuls
across two streams measured slower on an M1 Pro (16-core GPU):
Cross-stream joins synchronise and each stream carries its own command buffer, so naive
op-level splitting costs more than it saves. The value of this change is that stream selection
does what its signature says.
Checklist
pre-commit run --all-files— not run; no pre-commit installed here. The change is oneexpression plus a test and matches surrounding style.
Tests
StreamTests.testStreamOrDeviceHonorsTheGivenStreamasserts two fresh streams are distinct andthat
.stream(a)/.stream(b)resolve to them rather than collapsing onto the default. It failson
mainand passes with the change.I could not execute the XCTest target locally: it needs
XCTest.framework, which ships only withfull Xcode, and this machine has Command Line Tools. The before/after behaviour above was verified
by running the same identity checks from a plain executable linked against MLX, and the change
compiles clean.