mirror of
https://github.com/ml-explore/mlx.git
synced 2025-06-25 01:41:17 +08:00
expose itemsize and nbytes as for numpy arrays (#284)
see: * https://numpy.org/doc/stable/reference/generated/numpy.ndarray.nbytes.html * https://numpy.org/doc/stable/reference/generated/numpy.ndarray.itemsize.html relates to https://github.com/ml-explore/mlx-examples/pull/174
This commit is contained in:
parent
a123c3c7d2
commit
d58ac083f3
@ -510,6 +510,14 @@ void init_array(py::module_& m) {
|
||||
"size", &array::size, R"pbdoc(Number of elments in the array.)pbdoc")
|
||||
.def_property_readonly(
|
||||
"ndim", &array::ndim, R"pbdoc(The array's dimension.)pbdoc")
|
||||
.def_property_readonly(
|
||||
"itemsize",
|
||||
&array::itemsize,
|
||||
R"pbdoc(The size of the array's datatype in bytes.)pbdoc")
|
||||
.def_property_readonly(
|
||||
"nbytes",
|
||||
&array::nbytes,
|
||||
R"pbdoc(The number of bytes in the array.)pbdoc")
|
||||
// TODO, this makes a deep copy of the shape
|
||||
// implement alternatives to use reference
|
||||
// https://pybind11.readthedocs.io/en/stable/advanced/cast/stl.html
|
||||
|
@ -84,6 +84,8 @@ class TestArray(mlx_tests.MLXTestCase):
|
||||
x = mx.array(1)
|
||||
self.assertEqual(x.size, 1)
|
||||
self.assertEqual(x.ndim, 0)
|
||||
self.assertEqual(x.itemsize, 4)
|
||||
self.assertEqual(x.nbytes, 4)
|
||||
self.assertEqual(x.shape, [])
|
||||
self.assertEqual(x.dtype, mx.int32)
|
||||
self.assertEqual(x.item(), 1)
|
||||
|
Loading…
Reference in New Issue
Block a user