Skip to content

fix: StreamOrDevice.stream(_:) ignored its argument - #15

Closed
owizdom wants to merge 1 commit into
Layr-Labs:mainfrom
owizdom:fix/streamordevice-honors-stream
Closed

owizdom wants to merge 1 commit into
Layr-Labs:mainfrom
owizdom:fix/streamordevice-honors-stream

Conversation

@owizdom

@owizdom owizdom commented Aug 23, 2026

Copy link
Copy Markdown

Proposed changes

StreamOrDevice.stream(_:) discarded the stream it was given:

public static func stream(_ stream: Stream) -> StreamOrDevice {
    StreamOrDevice(Device.defaultStream())   // parameter unused
}

Compare device(_:) two lines above, which uses its parameter. The consequence is that a
non-default stream is unreachable from Swift — every .stream(x) silently resolves to the
default 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 calls newCommandQueue() and labels it by stream index), so with
every op collapsing onto one stream, all GPU work funnels through a single queue.

Reproduction

Two freshly created GPU streams, printed before the fix:

fresh stream a            Stream(Device(gpu, 0), 0)
fresh stream b            Stream(Device(gpu, 0), 1)
default                   Stream(Device(gpu, 0), 2)
StreamOrDevice.stream(a)  Stream(Device(gpu, 0), 2)   <- should be 0
StreamOrDevice.stream(b)  Stream(Device(gpu, 0), 2)   <- should be 1

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):

rows one stream two streams
64 3.12 ms 8.60 ms
256 10.53 ms 12.61 ms
1024 37.14 ms 39.21 ms

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

  • I have read the CONTRIBUTING document
  • pre-commit run --all-files — not run; no pre-commit installed here. The change is one
    expression plus a test and matches surrounding style.
  • I have added a test that fails without the fix
  • I have updated the necessary documentation (if needed)

Tests

StreamTests.testStreamOrDeviceHonorsTheGivenStream asserts two fresh streams are distinct and
that .stream(a)/.stream(b) resolve to them rather than collapsing onto the default. It fails
on main and passes with the change.

I could not execute the XCTest target locally: it needs XCTest.framework, which ships only with
full 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.

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.
@owizdom

owizdom commented Aug 23, 2026

Copy link
Copy Markdown
Author

Closing to keep my open PRs down to one. The bug is real if anyone wants it later:

StreamOrDevice.stream(_:) ignored its argument and returned Device.defaultStream(), so passing an explicit stream silently ran the work on the default one. One-line fix, verified at runtime before and after.

Happy to reopen.

@owizdom owizdom closed this Aug 23, 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>
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.

1 participant