diff --git a/python/src/convert.cpp b/python/src/convert.cpp index a6dd7fd1..edc40ff8 100644 --- a/python/src/convert.cpp +++ b/python/src/convert.cpp @@ -491,7 +491,7 @@ mx::array create_array(nb::object v, std::optional t) { std::optional nb_dtype; // Nanobind does not recognize bfloat16 numpy array: // https://github.com/wjakob/nanobind/discussions/560 - if (v.attr("dtype").equal(nb::str("bfloat16"))) { + if (nb::hasattr(v, "dtype") && v.attr("dtype").equal(nb::str("bfloat16"))) { nd = nb::cast(v.attr("view")("uint16")); nb_dtype = nb::dtype(); } else { diff --git a/python/tests/test_array.py b/python/tests/test_array.py index 86328c2a..0ae62eb2 100644 --- a/python/tests/test_array.py +++ b/python/tests/test_array.py @@ -1782,6 +1782,15 @@ class TestArray(mlx_tests.MLXTestCase): a_np = None self.assertIsNone(wr()) + def test_create_from_buffer(self): + x = mx.array(b"Hello") + self.assertEqual(x.dtype, mx.uint8) + self.assertEqual(x.tolist(), [72, 101, 108, 108, 111]) + + x = mx.array(bytearray([1, 2, 3])) + self.assertEqual(x.dtype, mx.uint8) + self.assertEqual(x.tolist(), [1, 2, 3]) + @unittest.skipIf(not has_tf, "requires TensorFlow") def test_buffer_protocol_tf(self): dtypes_list = [