Skip to content

[BUG] Device::operator< and Stream::operator< are not strict weak orderings #4083

Closed

Description

@CharlieTLe

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

  1. zcbenz commented on Aug 9, 2026

    @zcbenz
    Member

    Thanks for noticing this, a PR would be appreciated.

  2. added 2 commits that reference this issue on Aug 9, 2026
    e9c33ab
    641f998
  3. added 2 commits that reference this issue on Aug 9, 2026
    a0fff05
    d876219
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions