Skip to content

Commit cbd8f66

Browse files
authored
Merge branch 'main' into fix-safetensor
2 parents 94e3f59 + 846d176 commit cbd8f66

2 files changed

Lines changed: 18 additions & 20 deletions

File tree

mlx/ops.cpp

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2794,17 +2794,9 @@ array sort(const array& a, StreamOrDevice s /* = {} */) {
27942794

27952795
/** Returns a sorted copy of the array along a given axis. */
27962796
array sort(const array& a, int axis, StreamOrDevice s /* = {} */) {
2797-
// Check for valid axis
2798-
if (axis + static_cast<int>(a.ndim()) < 0 ||
2799-
axis >= static_cast<int>(a.ndim())) {
2800-
std::ostringstream msg;
2801-
msg << "[sort] Received invalid axis " << axis << " for array with "
2802-
<< a.ndim() << " dimensions.";
2803-
throw std::invalid_argument(msg.str());
2804-
}
2805-
2797+
auto ax = normalize_axis_index(axis, a.ndim(), "[sort] ");
28062798
return array(
2807-
a.shape(), a.dtype(), std::make_shared<Sort>(to_stream(s), axis), {a});
2799+
a.shape(), a.dtype(), std::make_shared<Sort>(to_stream(s), ax), {a});
28082800
}
28092801

28102802
/** Returns indices that sort the flattened array. */
@@ -2815,17 +2807,9 @@ array argsort(const array& a, StreamOrDevice s /* = {} */) {
28152807

28162808
/** Returns indices that sort the array along a given axis. */
28172809
array argsort(const array& a, int axis, StreamOrDevice s /* = {} */) {
2818-
// Check for valid axis
2819-
if (axis + static_cast<int>(a.ndim()) < 0 ||
2820-
axis >= static_cast<int>(a.ndim())) {
2821-
std::ostringstream msg;
2822-
msg << "[argsort] Received invalid axis " << axis << " for array with "
2823-
<< a.ndim() << " dimensions.";
2824-
throw std::invalid_argument(msg.str());
2825-
}
2826-
2810+
auto ax = normalize_axis_index(axis, a.ndim(), "[argsort] ");
28272811
return array(
2828-
a.shape(), uint32, std::make_shared<ArgSort>(to_stream(s), axis), {a});
2812+
a.shape(), uint32, std::make_shared<ArgSort>(to_stream(s), ax), {a});
28292813
}
28302814

28312815
/**

python/tests/test_vmap.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,6 +1023,20 @@ def fn(x, y):
10231023
self.assertTrue(mx.array_equal(expected, out))
10241024
self.assertEqual(6, counter[0])
10251025

1026+
def test_vmap_sort(self):
1027+
a = mx.random.uniform(shape=(3, 5))
1028+
expected = mx.stack([mx.sort(a[:, i]) for i in range(a.shape[1])], axis=1)
1029+
for axis in (0, -1):
1030+
out = mx.vmap(lambda x: mx.sort(x, axis=axis), in_axes=1, out_axes=1)(a)
1031+
self.assertTrue(mx.array_equal(out, expected))
1032+
1033+
def test_vmap_argsort(self):
1034+
a = mx.random.uniform(shape=(3, 5))
1035+
expected = mx.stack([mx.argsort(a[:, i]) for i in range(a.shape[1])], axis=1)
1036+
for axis in (0, -1):
1037+
out = mx.vmap(lambda x: mx.argsort(x, axis=axis), in_axes=1, out_axes=1)(a)
1038+
self.assertTrue(mx.array_equal(out, expected))
1039+
10261040

10271041
if __name__ == "__main__":
10281042
mlx_tests.MLXTestRunner()

0 commit comments

Comments
 (0)