Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions mlx/backend/cpu/simd/base_simd.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ Simd<T, 1> maximum(Simd<T, 1> a_, Simd<T, 1> b_) {
T a = a_.value;
T b = b_.value;
if constexpr (!std::is_integral_v<T>) {
if (std::isnan(a)) {
if (mlx::core::isnan(a)) {
return a;
}
}
Expand All @@ -256,7 +256,7 @@ Simd<T, 1> minimum(Simd<T, 1> a_, Simd<T, 1> b_) {
T a = a_.value;
T b = b_.value;
if constexpr (!std::is_integral_v<T>) {
if (std::isnan(a)) {
if (mlx::core::isnan(a)) {
return a;
}
}
Expand Down
17 changes: 8 additions & 9 deletions mlx/backend/cpu/sort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include <algorithm>
#include <cassert>
#include <cmath>
#include <numeric>

#include "mlx/backend/common/utils.h"
Expand All @@ -19,9 +18,9 @@ namespace {
template <typename T>
bool nan_aware_less(T a, T b) {
if constexpr (is_floating_point_v<T> || std::is_same_v<T, complex64_t>) {
if (std::isnan(a))
if (mlx::core::isnan(a))
return false;
if (std::isnan(b))
if (mlx::core::isnan(b))
return true;
}
return a < b;
Expand Down Expand Up @@ -206,10 +205,10 @@ void argsort(const array& in, array& out, int axis) {
auto v2 = data_ptr[b * in_stride];

// Handle NaNs (place them at the end)
if constexpr (is_floating_point_v<T>) {
if (std::isnan(v1))
if constexpr (is_floating_point_v<T> || std::is_same_v<T, complex64_t>) {
if (mlx::core::isnan(v1))
return false;
if (std::isnan(v2))
if (mlx::core::isnan(v2))
return true;
}

Expand Down Expand Up @@ -315,10 +314,10 @@ void argpartition(const array& in, array& out, int axis, int kth) {
auto v2 = data_ptr[b * in_stride];

// Handle NaNs (place them at the end)
if constexpr (is_floating_point_v<T>) {
if (std::isnan(v1))
if constexpr (is_floating_point_v<T> || std::is_same_v<T, complex64_t>) {
if (mlx::core::isnan(v1))
return false;
if (std::isnan(v2))
if (mlx::core::isnan(v2))
return true;
}

Expand Down
22 changes: 22 additions & 0 deletions python/tests/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,11 @@ def test_minimum(self):
self.assertTrue(math.isnan(mx.minimum(a, b).item()))
self.assertTrue(math.isnan(mx.minimum(b, a).item()))

# A NaN in either part of the first argument propagates.
a = mx.array([complex(3, float("nan"))])
b = mx.array([2 + 0j])
self.assertTrue(mx.array_equal(mx.minimum(a, b), a, equal_nan=True))

def test_maximum(self):
x = mx.array([0.0, -5, 10.0])
y = mx.array([1.0, -7.0, 3.0])
Expand All @@ -605,6 +610,11 @@ def test_maximum(self):
self.assertTrue(math.isnan(mx.maximum(a, b).item()))
self.assertTrue(math.isnan(mx.maximum(b, a).item()))

# A NaN in either part of the first argument propagates.
a = mx.array([complex(1, float("nan"))])
b = mx.array([2 + 0j])
self.assertTrue(mx.array_equal(mx.maximum(a, b), a, equal_nan=True))

def test_floor(self):
x = mx.array([-22.03, 19.98, -27, 9, 0.0, -np.inf, np.inf])
expected = [-23, 19, -27, 9, 0, -np.inf, np.inf]
Expand Down Expand Up @@ -4274,6 +4284,11 @@ def test_sort_nan(self):
expected = mx.array([0.0 + 1j, 2.0 + 1j, 3.0 + 1j, mx.nan + 2j])
self.assertTrue(mx.array_equal(mx.sort(x), expected, equal_nan=True))

# A NaN in the imaginary part also sorts last.
x = mx.array([3.0 + 1j, complex(1.0, mx.nan), 2.0 + 1j, 0.0 + 1j])
expected = mx.array([0.0 + 1j, 2.0 + 1j, 3.0 + 1j, complex(1.0, mx.nan)])
self.assertTrue(mx.array_equal(mx.sort(x), expected, equal_nan=True))

def test_argsort_nan(self):
for dtype in [mx.float32, mx.float16, mx.bfloat16]:
with self.subTest(dtype=dtype):
Expand All @@ -4283,6 +4298,13 @@ def test_argsort_nan(self):
sorted_x = mx.take(x, indices)
self.assertTrue(mx.array_equal(sorted_x, expected, equal_nan=True))

# Also test complex values
for nan_val in [mx.nan + 2j, complex(1.0, mx.nan)]:
x = mx.array([3.0 + 1j, nan_val, 2.0 + 1j, 0.0 + 1j])
expected = mx.array([0.0 + 1j, 2.0 + 1j, 3.0 + 1j, nan_val])
sorted_x = mx.take(x, mx.argsort(x))
self.assertTrue(mx.array_equal(sorted_x, expected, equal_nan=True))

def test_to_from_fp8(self):
vals = mx.array(
[448, 256, 192, 128, 96, 64, 48, 32, 24, 16, 12, 8, 6, 4, 3, 2, 0.015625]
Expand Down