Repository navigation
Description
Describe the bug
Device::operator< (mlx/device.h) and Stream::operator< (mlx/stream.h) are not strict weak orderings:
bool operator<(const Device& rhs) const {
return type < rhs.type || index < rhs.index;
}
bool operator<(const Stream& rhs) const {
return device < rhs.device || index < rhs.index;
}The second clause is evaluated even when the first field differs, so both a < b and b < a can be true. It needs to be a lexicographic compare (the second field should only be consulted when the first is equal).
This matters because eval_impl uses the ordering: mlx/transforms.cpp keys std::set<Stream> open_streams on the whole Stream (it was std::unordered_set<int> on stream.index before 0.31.2), so every eval() runs the comparator. A std::set with an invalid comparator can hold duplicate streams and fail to find ones it holds, and the standard places these calls outside the domain of the algorithm.
To Reproduce
C++ rather than Python, since these are C++ operators:
#include "mlx/device.h"
#include "mlx/stream.h"
#include <cstdio>
using namespace mlx::core;
int main() {
Device cpu1(Device::cpu, 1);
Device gpu0(Device::gpu, 0);
printf("Device{cpu,1} < Device{gpu,0} : %d\n", cpu1 < gpu0);
printf("Device{gpu,0} < Device{cpu,1} : %d\n", gpu0 < cpu1);
Stream s_cpu1(1, Device(Device::cpu, 0));
Stream s_gpu0(0, Device(Device::gpu, 0));
printf("Stream{idx1,cpu} < Stream{idx0,gpu} : %d\n", s_cpu1 < s_gpu0);
printf("Stream{idx0,gpu} < Stream{idx1,cpu} : %d\n", s_gpu0 < s_cpu1);
}$ c++ -std=c++20 -I <mlx> repro.cpp -o repro && ./repro
Device{cpu,1} < Device{gpu,0} : 1
Device{gpu,0} < Device{cpu,1} : 1
Stream{idx1,cpu} < Stream{idx0,gpu} : 1
Stream{idx0,gpu} < Stream{idx1,cpu} : 1
Both directions are true in both cases. Generally: any pair where one field compares less and the other compares greater.
It also aborts at runtime. libc++ validates comparators under _LIBCPP_HARDENING_MODE_DEBUG, which the Xcode 27 SDK enables, so any eval() with two such streams open dies:
__utility/lazy_synth_three_way_comparator.h:47: libc++ Hardening assertion
__result ? !static_cast<bool>(__comp_(__rhs_, __lhs_)) : true failed:
Comparator does not induce a strict weak ordering
frame #3: std::__1::__tree<mlx::core::Stream, std::__1::less<mlx::core::Stream>, ...>::__emplace_unique<...>
frame #7: std::__1::set<mlx::core::Stream, ...>::insert
frame #8: mlx::core::eval_impl(...) at transforms.cpp:227
frame #9: mlx::core::eval(...) at transforms.cpp:344
In mlx-swift this reproduces by running an op on an explicit cpu stream while the default device is gpu (two streams, Stream{0, gpu} and Stream{1, cpu}) and calling eval.
Expected behavior
a < b and b < a are never both true, and std::set<Stream> / std::set<Device> behave correctly. A lexicographic compare:
bool operator<(const Device& rhs) const {
if (type != rhs.type) {
return type < rhs.type;
}
return index < rhs.index;
}
bool operator<(const Stream& rhs) const {
if (!(device == rhs.device)) {
return device < rhs.device;
}
return index < rhs.index;
}Happy to send a PR if useful.
Desktop (please complete the following information):
- OS Version: macOS 27.0 (Apple Silicon), Xcode 27.0 SDK
- Version: introduced in 0.31.2 (both operators are new in that release), still present in 0.32.0 and on
main(8056817)
Additional context
The // TODO: Use default three-way comparison when it gets supported in XCode. comment above both operators suggests these were hand-written stand-ins for <=>; a defaulted operator<=> would have produced the lexicographic order.
Activity
zcbenz commentedon Aug 9, 2026
Thanks for noticing this, a PR would be appreciated.
fix: make Device and Stream operator< a strict weak ordering
Add regression tests for Device/Stream strict weak ordering (fix ml-e…
align mlx.patch with the upstream ordering fix
align mlx.patch with the upstream ordering fix