Skip to content
Merged
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
16 changes: 16 additions & 0 deletions python/src/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,22 @@ void init_array(nb::module_& m) {
[](mx::array& a) {
return nb::cast<std::complex<double>>(to_scalar(a));
})
.def(
"__index__",
[](mx::array& a) {
if (!mx::issubdtype(a.dtype(), mx::integer) || a.ndim() != 0) {
throw nb::type_error(
"Only 0-dimensional integer arrays can be converted to an index.");
Comment thread
zcbenz marked this conversation as resolved.
Resolved
}
return nb::int_(to_scalar(a));
})
.def(
"__bytes__",
[](mx::array& a) {
a.eval();
return nb::bytes(

@simeetnayan81 simeetnayan81 Sep 1, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was going through the code when I realized this:
We probably need to call a.contiguous here as well.
eval() only makes the buffer readable

If a refers to transpose/slice/view of some array b. byte(a) might be wrong silently.
I am still not 100% sure, but that's what I think.

Example:

x = mx.arange(10, int32)     # 0 1 2 3 4 5 6 7 8 9
y = x[::2]                # 0 2 4 6 8
bytes(y) #This would generate a false dump since y is not packed still. It'd take 0 1 2 3 4

I'll file a new issue with more detail

reinterpret_cast<const char*>(a.data<void>()), a.nbytes());
})
.def(
"__format__",
[](mx::array& a, nb::object format_spec) {
Expand Down
4 changes: 4 additions & 0 deletions python/src/indexing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <nanobind/ndarray.h>

#include "mlx/dtype.h"
#include "mlx/ops.h"
#include "python/src/convert.h"
#include "python/src/indexing.h"
Expand All @@ -20,6 +21,9 @@ bool is_index_scalar(const nb::object& obj) {
if (nb::isinstance<nb::bool_>(obj)) {
return false;
}
if (nb::isinstance<mx::array>(obj)) {
return false;
}
if (!PyIndex_Check(obj.ptr())) {
return false;
}
Expand Down
24 changes: 24 additions & 0 deletions python/tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,30 @@ def test_scalar_conversion(self):
self.assertListEqual(list(z.shape), list(x.shape))
self.assertListEqual(list(z.shape), list(y.shape))

def test_index_conversion(self):
for dtype in [
mx.uint8,
mx.uint16,
mx.uint32,
mx.uint64,
mx.int8,
mx.int16,
mx.int32,
mx.int64,
]:
with self.subTest(dtype=dtype):
self.assertEqual(operator.index(mx.array(2, dtype)), 2)
self.assertEqual(list(range(mx.array(3, dtype))), [0, 1, 2])

def test_index_conversion_invalid(self):
for dtype in [mx.float16, mx.float32, mx.bfloat16, mx.complex64, mx.bool_]:
with self.subTest(dtype=dtype):
with self.assertRaises(TypeError):
operator.index(mx.array(2, dtype))

with self.assertRaises(TypeError):
list(range(mx.array(3, dtype)))

def test_finfo(self):
with self.assertRaises(ValueError):
mx.finfo(mx.int32)
Expand Down
Loading