forked from ml-explore/mlx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray.cpp
1481 lines (1415 loc) · 44.9 KB
/
array.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright © 2023 Apple Inc.
#include <cstdint>
#include <cstring>
#include <sstream>
#include <pybind11/numpy.h>
#include "python/src/indexing.h"
#include "python/src/utils.h"
#include "mlx/ops.h"
#include "mlx/transforms.h"
#include "mlx/utils.h"
namespace py = pybind11;
using namespace py::literals;
enum PyScalarT {
pybool = 0,
pyint = 1,
pyfloat = 2,
pycomplex = 3,
};
template <typename T>
py::list to_list(array& a, size_t index, int dim) {
py::list pl;
auto stride = a.strides()[dim];
for (int i = 0; i < a.shape(dim); ++i) {
if (dim == a.ndim() - 1) {
pl.append((a.data<T>()[index]));
} else {
pl.append(to_list<T>(a, index, dim + 1));
}
index += stride;
}
return pl;
}
auto to_scalar(array& a) {
switch (a.dtype()) {
case bool_:
return py::cast(a.item<bool>());
case uint8:
return py::cast(a.item<uint8_t>());
case uint16:
return py::cast(a.item<uint16_t>());
case uint32:
return py::cast(a.item<uint32_t>());
case uint64:
return py::cast(a.item<uint64_t>());
case int8:
return py::cast(a.item<int8_t>());
case int16:
return py::cast(a.item<int16_t>());
case int32:
return py::cast(a.item<int32_t>());
case int64:
return py::cast(a.item<int64_t>());
case float16:
return py::cast(static_cast<float>(a.item<float16_t>()));
case float32:
return py::cast(a.item<float>());
case bfloat16:
return py::cast(static_cast<float>(a.item<bfloat16_t>()));
case complex64:
return py::cast(a.item<std::complex<float>>());
}
}
py::object tolist(array& a) {
if (a.ndim() == 0) {
return to_scalar(a);
}
a.eval();
py::object pl;
switch (a.dtype()) {
case bool_:
return to_list<bool>(a, 0, 0);
case uint8:
return to_list<uint8_t>(a, 0, 0);
case uint16:
return to_list<uint16_t>(a, 0, 0);
case uint32:
return to_list<uint32_t>(a, 0, 0);
case uint64:
return to_list<uint64_t>(a, 0, 0);
case int8:
return to_list<int8_t>(a, 0, 0);
case int16:
return to_list<int16_t>(a, 0, 0);
case int32:
return to_list<int32_t>(a, 0, 0);
case int64:
return to_list<int64_t>(a, 0, 0);
case float16:
return to_list<float16_t>(a, 0, 0);
case float32:
return to_list<float>(a, 0, 0);
case bfloat16:
return to_list<float16_t>(a, 0, 0);
case complex64:
return to_list<std::complex<float>>(a, 0, 0);
}
}
template <typename T, typename U>
void fill_vector(T list, std::vector<U>& vals) {
for (auto l : list) {
if (py::isinstance<py::list>(l)) {
fill_vector(l.template cast<py::list>(), vals);
} else if (py::isinstance<py::tuple>(*list.begin())) {
fill_vector(l.template cast<py::tuple>(), vals);
} else {
vals.push_back(l.template cast<U>());
}
}
}
template <typename T>
PyScalarT validate_shape(
T list,
const std::vector<int>& shape,
int idx,
bool& all_python_primitive_elements) {
if (idx >= shape.size()) {
throw std::invalid_argument("Initialization encountered extra dimension.");
}
auto s = shape[idx];
if (py::len(list) != s) {
throw std::invalid_argument(
"Initialization encountered non-uniform length.");
}
if (s == 0) {
return pyfloat;
}
PyScalarT type = pybool;
for (auto l : list) {
PyScalarT t;
if (py::isinstance<py::list>(l)) {
t = validate_shape(
l.template cast<py::list>(),
shape,
idx + 1,
all_python_primitive_elements);
} else if (py::isinstance<py::tuple>(*list.begin())) {
t = validate_shape(
l.template cast<py::tuple>(),
shape,
idx + 1,
all_python_primitive_elements);
} else if (py::isinstance<py::bool_>(l)) {
t = pybool;
} else if (py::isinstance<py::int_>(l)) {
t = pyint;
} else if (py::isinstance<py::float_>(l)) {
t = pyfloat;
} else if (PyComplex_Check(l.ptr())) {
t = pycomplex;
} else if (py::isinstance<array>(l)) {
all_python_primitive_elements = false;
auto arr = py::cast<array>(l);
if (arr.ndim() + idx + 1 == shape.size() &&
std::equal(
arr.shape().cbegin(),
arr.shape().cend(),
shape.cbegin() + idx + 1)) {
t = pybool;
} else {
throw std::invalid_argument(
"Initialization encountered non-uniform length.");
}
} else {
std::ostringstream msg;
msg << "Invalid type in array initialization" << l.get_type() << ".";
throw std::invalid_argument(msg.str());
}
type = std::max(type, t);
}
return type;
}
template <typename T>
void get_shape(T list, std::vector<int>& shape) {
shape.push_back(py::len(list));
if (shape.back() > 0) {
auto& l = *list.begin();
if (py::isinstance<py::list>(l)) {
return get_shape(l.template cast<py::list>(), shape);
} else if (py::isinstance<py::tuple>(l)) {
return get_shape(l.template cast<py::tuple>(), shape);
} else if (py::isinstance<array>(l)) {
auto arr = py::cast<array>(l);
shape.insert(shape.end(), arr.shape().begin(), arr.shape().end());
return;
}
}
}
using array_init_type = std::variant<
py::bool_,
py::int_,
py::float_,
std::complex<float>,
py::list,
py::tuple,
array,
py::array,
py::buffer,
py::object>;
// Forward declaration
array create_array(array_init_type v, std::optional<Dtype> t);
template <typename T>
array array_from_list(
T pl,
const PyScalarT& inferred_type,
std::optional<Dtype> specified_type,
const std::vector<int>& shape) {
// Make the array
switch (inferred_type) {
case pybool: {
std::vector<bool> vals;
fill_vector(pl, vals);
return array(vals.begin(), shape, specified_type.value_or(bool_));
}
case pyint: {
auto dtype = specified_type.value_or(int32);
if (dtype == int64) {
std::vector<int64_t> vals;
fill_vector(pl, vals);
return array(vals.begin(), shape, dtype);
} else if (dtype == uint64) {
std::vector<uint64_t> vals;
fill_vector(pl, vals);
return array(vals.begin(), shape, dtype);
} else if (dtype == uint32) {
std::vector<uint32_t> vals;
fill_vector(pl, vals);
return array(vals.begin(), shape, dtype);
} else if (is_floating_point(dtype)) {
std::vector<float> vals;
fill_vector(pl, vals);
return array(vals.begin(), shape, dtype);
} else {
std::vector<int> vals;
fill_vector(pl, vals);
return array(vals.begin(), shape, dtype);
}
}
case pyfloat: {
std::vector<float> vals;
fill_vector(pl, vals);
return array(vals.begin(), shape, specified_type.value_or(float32));
}
case pycomplex: {
std::vector<std::complex<float>> vals;
fill_vector(pl, vals);
return array(
reinterpret_cast<complex64_t*>(vals.data()),
shape,
specified_type.value_or(complex64));
}
default: {
std::ostringstream msg;
msg << "Should not happen, inferred: " << inferred_type
<< " on subarray made of only python primitive types.";
throw std::runtime_error(msg.str());
}
}
}
template <typename T>
array array_from_list(T pl, std::optional<Dtype> dtype) {
// Compute the shape
std::vector<int> shape;
get_shape(pl, shape);
// Validate the shape and type
bool all_python_primitive_elements = true;
auto type = validate_shape(pl, shape, 0, all_python_primitive_elements);
if (all_python_primitive_elements) {
// `pl` does not contain mlx arrays
return array_from_list(pl, type, dtype, shape);
}
// `pl` contains mlx arrays
std::vector<array> arrays;
for (auto l : pl) {
arrays.push_back(create_array(py::cast<array_init_type>(l), dtype));
}
return stack(arrays);
}
///////////////////////////////////////////////////////////////////////////////
// Numpy -> MLX
///////////////////////////////////////////////////////////////////////////////
template <typename T>
array np_array_to_mlx_contiguous(
py::array_t<T, py::array::c_style | py::array::forcecast> np_array,
const std::vector<int>& shape,
Dtype dtype) {
// Make a copy of the numpy buffer
// Get buffer ptr pass to array constructor
py::buffer_info buf = np_array.request();
const T* data_ptr = static_cast<T*>(buf.ptr);
return array(data_ptr, shape, dtype);
// Note: Leaving the following memoryless copy from np to mx commented
// out for the time being since it is unsafe given that the incoming
// numpy array may change the underlying data
// // Share underlying numpy buffer
// // Copy to increase ref count
// auto deleter = [np_array](void*) {};
// void* data_ptr = np_array.mutable_data();
// // Use buffer from numpy
// return array(data_ptr, deleter, shape, dtype);
}
template <>
array np_array_to_mlx_contiguous(
py::array_t<std::complex<float>, py::array::c_style | py::array::forcecast>
np_array,
const std::vector<int>& shape,
Dtype dtype) {
// Get buffer ptr pass to array constructor
py::buffer_info buf = np_array.request();
auto data_ptr = static_cast<std::complex<float>*>(buf.ptr);
return array(reinterpret_cast<complex64_t*>(data_ptr), shape, dtype);
}
array np_array_to_mlx(py::array np_array, std::optional<Dtype> dtype) {
// Compute the shape and size
std::vector<int> shape;
for (int i = 0; i < np_array.ndim(); i++) {
shape.push_back(np_array.shape(i));
}
// Get dtype
auto type = np_array.dtype();
// Copy data and make array
if (type.is(py::dtype::of<int>())) {
return np_array_to_mlx_contiguous<int32_t>(
np_array, shape, dtype.value_or(int32));
} else if (type.is(py::dtype::of<uint32_t>())) {
return np_array_to_mlx_contiguous<uint32_t>(
np_array, shape, dtype.value_or(uint32));
} else if (type.is(py::dtype::of<bool>())) {
return np_array_to_mlx_contiguous<bool>(
np_array, shape, dtype.value_or(bool_));
} else if (type.is(py::dtype::of<double>())) {
return np_array_to_mlx_contiguous<double>(
np_array, shape, dtype.value_or(float32));
} else if (type.is(py::dtype::of<float>())) {
return np_array_to_mlx_contiguous<float>(
np_array, shape, dtype.value_or(float32));
} else if (type.is(py::dtype("float16"))) {
return np_array_to_mlx_contiguous<float>(
np_array, shape, dtype.value_or(float16));
} else if (type.is(py::dtype::of<uint8_t>())) {
return np_array_to_mlx_contiguous<uint8_t>(
np_array, shape, dtype.value_or(uint8));
} else if (type.is(py::dtype::of<uint16_t>())) {
return np_array_to_mlx_contiguous<uint16_t>(
np_array, shape, dtype.value_or(uint16));
} else if (type.is(py::dtype::of<uint64_t>())) {
return np_array_to_mlx_contiguous<uint64_t>(
np_array, shape, dtype.value_or(uint64));
} else if (type.is(py::dtype::of<int8_t>())) {
return np_array_to_mlx_contiguous<int8_t>(
np_array, shape, dtype.value_or(int8));
} else if (type.is(py::dtype::of<int16_t>())) {
return np_array_to_mlx_contiguous<int16_t>(
np_array, shape, dtype.value_or(int16));
} else if (type.is(py::dtype::of<int64_t>())) {
return np_array_to_mlx_contiguous<int64_t>(
np_array, shape, dtype.value_or(int64));
} else if (type.is(py::dtype::of<std::complex<float>>())) {
return np_array_to_mlx_contiguous<std::complex<float>>(
np_array, shape, dtype.value_or(complex64));
} else if (type.is(py::dtype::of<std::complex<double>>())) {
return np_array_to_mlx_contiguous<std::complex<float>>(
np_array, shape, dtype.value_or(complex64));
} else {
std::ostringstream msg;
msg << "Cannot convert numpy array of type " << type << " to mlx array.";
throw std::invalid_argument(msg.str());
}
}
///////////////////////////////////////////////////////////////////////////////
// Python Buffer Protocol (MLX -> Numpy)
///////////////////////////////////////////////////////////////////////////////
std::optional<std::string> buffer_format(const array& a) {
// https://docs.python.org/3.10/library/struct.html#format-characters
switch (a.dtype()) {
case bool_:
return pybind11::format_descriptor<bool>::format();
case uint8:
return pybind11::format_descriptor<uint8_t>::format();
case uint16:
return pybind11::format_descriptor<uint16_t>::format();
case uint32:
return pybind11::format_descriptor<uint32_t>::format();
case uint64:
return pybind11::format_descriptor<uint64_t>::format();
case int8:
return pybind11::format_descriptor<int8_t>::format();
case int16:
return pybind11::format_descriptor<int16_t>::format();
case int32:
return pybind11::format_descriptor<int32_t>::format();
case int64:
return pybind11::format_descriptor<int64_t>::format();
case float16:
// https://github.com/pybind/pybind11/issues/4998
return "e";
case float32: {
return pybind11::format_descriptor<float>::format();
}
case bfloat16:
// not supported by python buffer protocol or numpy.
// must be null according to
// https://docs.python.org/3.10/c-api/buffer.html#c.PyBUF_FORMAT
// which implies 'B'.
return {};
case complex64:
return pybind11::format_descriptor<std::complex<float>>::format();
default: {
std::ostringstream os;
os << "bad dtype: " << a.dtype();
throw std::runtime_error(os.str());
}
}
}
std::vector<size_t> buffer_strides(const array& a) {
std::vector<size_t> py_strides;
py_strides.reserve(a.strides().size());
for (const size_t stride : a.strides()) {
py_strides.push_back(stride * a.itemsize());
}
return py_strides;
}
///////////////////////////////////////////////////////////////////////////////
// Module
///////////////////////////////////////////////////////////////////////////////
array create_array(array_init_type v, std::optional<Dtype> t) {
if (auto pv = std::get_if<py::bool_>(&v); pv) {
return array(py::cast<bool>(*pv), t.value_or(bool_));
} else if (auto pv = std::get_if<py::int_>(&v); pv) {
return array(py::cast<int>(*pv), t.value_or(int32));
} else if (auto pv = std::get_if<py::float_>(&v); pv) {
return array(py::cast<float>(*pv), t.value_or(float32));
} else if (auto pv = std::get_if<std::complex<float>>(&v); pv) {
return array(static_cast<complex64_t>(*pv), t.value_or(complex64));
} else if (auto pv = std::get_if<py::list>(&v); pv) {
return array_from_list(*pv, t);
} else if (auto pv = std::get_if<py::tuple>(&v); pv) {
return array_from_list(*pv, t);
} else if (auto pv = std::get_if<array>(&v); pv) {
return astype(*pv, t.value_or((*pv).dtype()));
} else if (auto pv = std::get_if<py::array>(&v); pv) {
return np_array_to_mlx(*pv, t);
} else if (auto pv = std::get_if<py::buffer>(&v); pv) {
return np_array_to_mlx(*pv, t);
} else {
auto arr = to_array_with_accessor(std::get<py::object>(v));
return astype(arr, t.value_or(arr.dtype()));
}
}
class ArrayAt {
public:
ArrayAt(array x) : x_(std::move(x)) {}
ArrayAt& set_indices(py::object indices) {
indices_ = indices;
return *this;
}
array add(const ScalarOrArray& v) {
return mlx_add_item(x_, indices_, v);
}
array subtract(const ScalarOrArray& v) {
return mlx_subtract_item(x_, indices_, v);
}
array multiply(const ScalarOrArray& v) {
return mlx_multiply_item(x_, indices_, v);
}
array divide(const ScalarOrArray& v) {
return mlx_divide_item(x_, indices_, v);
}
array maximum(const ScalarOrArray& v) {
return mlx_maximum_item(x_, indices_, v);
}
array minimum(const ScalarOrArray& v) {
return mlx_minimum_item(x_, indices_, v);
}
private:
array x_;
py::object indices_;
};
class ArrayPythonIterator {
public:
ArrayPythonIterator(array x) : idx_(0), x_(std::move(x)) {
if (x_.shape(0) > 0 && x_.shape(0) < 10) {
splits_ = split(x_, x_.shape(0));
}
}
array next() {
if (idx_ >= x_.shape(0)) {
throw py::stop_iteration();
}
if (idx_ >= 0 && idx_ < splits_.size()) {
return squeeze(splits_[idx_++], 0);
}
return *(x_.begin() + idx_++);
}
private:
int idx_;
array x_;
std::vector<array> splits_;
};
void init_array(py::module_& m) {
// Set Python print formatting options
mlx::core::global_formatter.capitalize_bool = true;
// Types
py::class_<Dtype>(
m,
"Dtype",
R"pbdoc(
An object to hold the type of a :class:`array`.
See the :ref:`list of types <data_types>` for more details
on available data types.
)pbdoc")
.def_readonly(
"size", &Dtype::size, R"pbdoc(Size of the type in bytes.)pbdoc")
.def(
"__repr__",
[](const Dtype& t) {
std::ostringstream os;
os << "mlx.core.";
os << t;
return os.str();
})
.def("__eq__", [](const Dtype& t1, const Dtype& t2) { return t1 == t2; })
.def("__hash__", [](const Dtype& t) {
return static_cast<int64_t>(t.val);
});
m.attr("bool_") = py::cast(bool_);
m.attr("uint8") = py::cast(uint8);
m.attr("uint16") = py::cast(uint16);
m.attr("uint32") = py::cast(uint32);
m.attr("uint64") = py::cast(uint64);
m.attr("int8") = py::cast(int8);
m.attr("int16") = py::cast(int16);
m.attr("int32") = py::cast(int32);
m.attr("int64") = py::cast(int64);
m.attr("float16") = py::cast(float16);
m.attr("float32") = py::cast(float32);
m.attr("bfloat16") = py::cast(bfloat16);
m.attr("complex64") = py::cast(complex64);
auto array_at_class = py::class_<ArrayAt>(
m,
"_ArrayAt",
R"pbdoc(
A helper object to apply updates at specific indices.
)pbdoc");
auto array_iterator_class = py::class_<ArrayPythonIterator>(
m,
"_ArrayIterator",
R"pbdoc(
A helper object to iterate over the 1st dimension of an array.
)pbdoc");
auto array_class = py::class_<array>(
m,
"array",
R"pbdoc(An N-dimensional array object.)pbdoc",
py::buffer_protocol());
{
py::options options;
options.disable_function_signatures();
array_class.def(
py::init([](array_init_type v, std::optional<Dtype> t) {
return create_array(v, t);
}),
"val"_a,
"dtype"_a = std::nullopt,
R"pbdoc(
__init__(self: array, val: Union[scalar, list, tuple, numpy.ndarray, array], dtype: Optional[Dtype] = None)
)pbdoc");
}
array_at_class
.def(
py::init([](const array& x) { return ArrayAt(x); }),
"x"_a,
R"pbdoc(
__init__(self, x: array)
)pbdoc")
.def("__getitem__", &ArrayAt::set_indices, "indices"_a)
.def("add", &ArrayAt::add, "value"_a)
.def("subtract", &ArrayAt::subtract, "value"_a)
.def("multiply", &ArrayAt::multiply, "value"_a)
.def("divide", &ArrayAt::divide, "value"_a)
.def("maximum", &ArrayAt::maximum, "value"_a)
.def("minimum", &ArrayAt::minimum, "value"_a);
array_iterator_class
.def(
py::init([](const array& x) { return ArrayPythonIterator(x); }),
"x"_a,
R"pbdoc(
__init__(self, x: array)
)pbdoc")
.def("__next__", &ArrayPythonIterator::next)
.def("__iter__", [](const ArrayPythonIterator& it) { return it; });
array_class
.def_buffer([](array& a) {
// Eval if not already evaled
if (!a.is_evaled()) {
a.eval();
}
return pybind11::buffer_info(
a.data<void>(),
a.itemsize(),
buffer_format(a).value_or("B"), // we use "B" because pybind uses a
// std::string which can't be null
a.ndim(),
a.shape(),
buffer_strides(a));
})
.def_property_readonly(
"size", &array::size, R"pbdoc(Number of elements 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
.def_property_readonly(
"shape",
[](const array& a) { return a.shape(); },
R"pbdoc(
The shape of the array as a Python list.
Returns:
list(int): A list containing the sizes of each dimension.
)pbdoc")
.def_property_readonly(
"dtype",
&array::dtype,
R"pbdoc(
The array's :class:`Dtype`.
)pbdoc")
.def(
"item",
&to_scalar,
R"pbdoc(
Access the value of a scalar array.
Returns:
Standard Python scalar.
)pbdoc")
.def(
"tolist",
&tolist,
R"pbdoc(
Convert the array to a Python :class:`list`.
Returns:
list: The Python list.
If the array is a scalar then a standard Python scalar is returned.
If the array has more than one dimension then the result is a nested
list of lists.
The value type of the list corresponding to the last dimension is either
``bool``, ``int`` or ``float`` depending on the ``dtype`` of the array.
)pbdoc")
.def(
"astype",
&astype,
"dtype"_a,
"stream"_a = none,
R"pbdoc(
Cast the array to a specified type.
Args:
dtype (Dtype): Type to which the array is cast.
stream (Stream): Stream (or device) for the operation.
Returns:
array: The array with type ``dtype``.
)pbdoc")
.def("__getitem__", mlx_get_item)
.def("__setitem__", mlx_set_item)
.def_property_readonly(
"at",
[](const array& a) { return ArrayAt(a); },
R"pbdoc(
Used to apply updates at the given indices.
.. note::
Python in place updates for all array frameworks map to
assignment. For instance ``x[idx] += y`` maps to ``x[idx] =
x[idx] + y``. As a result, assigning to the same index ignores
all but one updates. Using ``x.at[idx].add(y)`` will correctly
apply all the updates to all indices.
.. list-table::
:header-rows: 1
* - array.at syntax
- In-place syntax
* - ``x = x.at[idx].add(y)``
- ``x[idx] += y``
* - ``x = x.at[idx].subtract(y)``
- ``x[idx] -= y``
* - ``x = x.at[idx].multiply(y)``
- ``x[idx] *= y``
* - ``x = x.at[idx].divide(y)``
- ``x[idx] /= y``
* - ``x = x.at[idx].maximum(y)``
- ``x[idx] = mx.maximum(x[idx], y)``
* - ``x = x.at[idx].minimum(y)``
- ``x[idx] = mx.minimum(x[idx], y)``
)pbdoc")
.def(
"__len__",
[](const array& a) {
if (a.ndim() == 0) {
throw py::type_error("len() 0-dimensional array.");
}
return a.shape(0);
})
.def("__iter__", [](const array& a) { return ArrayPythonIterator(a); })
.def(
"__add__",
[](const array& a, const ScalarOrArray v) {
return add(a, to_array(v, a.dtype()));
},
"other"_a)
.def(
"__iadd__",
[](array& a, const ScalarOrArray v) {
a.overwrite_descriptor(add(a, to_array(v, a.dtype())));
return a;
},
"other"_a)
.def(
"__radd__",
[](const array& a, const ScalarOrArray v) {
return add(a, to_array(v, a.dtype()));
},
"other"_a)
.def(
"__sub__",
[](const array& a, const ScalarOrArray v) {
return subtract(a, to_array(v, a.dtype()));
},
"other"_a)
.def(
"__isub__",
[](array& a, const ScalarOrArray v) {
a.overwrite_descriptor(subtract(a, to_array(v, a.dtype())));
return a;
},
"other"_a)
.def(
"__rsub__",
[](const array& a, const ScalarOrArray v) {
return subtract(to_array(v, a.dtype()), a);
},
"other"_a)
.def(
"__mul__",
[](const array& a, const ScalarOrArray v) {
return multiply(a, to_array(v, a.dtype()));
},
"other"_a)
.def(
"__imul__",
[](array& a, const ScalarOrArray v) {
a.overwrite_descriptor(multiply(a, to_array(v, a.dtype())));
return a;
},
"other"_a)
.def(
"__rmul__",
[](const array& a, const ScalarOrArray v) {
return multiply(a, to_array(v, a.dtype()));
},
"other"_a)
.def(
"__truediv__",
[](const array& a, const ScalarOrArray v) {
return divide(a, to_array(v, a.dtype()));
},
"other"_a)
.def(
"__itruediv__",
[](array& a, const ScalarOrArray v) {
if (!is_floating_point(a.dtype())) {
throw std::invalid_argument(
"In place division cannot cast to non-floating point type.");
}
a.overwrite_descriptor(divide(a, to_array(v, a.dtype())));
return a;
},
"other"_a)
.def(
"__rtruediv__",
[](const array& a, const ScalarOrArray v) {
return divide(to_array(v, a.dtype()), a);
},
"other"_a)
.def(
"__div__",
[](const array& a, const ScalarOrArray v) {
return divide(a, to_array(v, a.dtype()));
},
"other"_a)
.def(
"__rdiv__",
[](const array& a, const ScalarOrArray v) {
return divide(to_array(v, a.dtype()), a);
},
"other"_a)
.def(
"__floordiv__",
[](const array& a, const ScalarOrArray v) {
return floor_divide(a, to_array(v, a.dtype()));
},
"other"_a)
.def(
"__ifloordiv__",
[](array& a, const ScalarOrArray v) {
a.overwrite_descriptor(floor_divide(a, to_array(v, a.dtype())));
return a;
},
"other"_a)
.def(
"__rfloordiv__",
[](const array& a, const ScalarOrArray v) {
auto b = to_array(v, a.dtype());
return floor_divide(b, a);
},
"other"_a)
.def(
"__mod__",
[](const array& a, const ScalarOrArray v) {
return remainder(a, to_array(v, a.dtype()));
},
"other"_a)
.def(
"__imod__",
[](array& a, const ScalarOrArray v) {
a.overwrite_descriptor(remainder(a, to_array(v, a.dtype())));
return a;
},
"other"_a)
.def(
"__rmod__",
[](const array& a, const ScalarOrArray v) {
return remainder(to_array(v, a.dtype()), a);
},
"other"_a)
.def(
"__eq__",
[](const array& a, const ScalarOrArray v) {
return equal(a, to_array(v, a.dtype()));
},
"other"_a)
.def(
"__lt__",
[](const array& a, const ScalarOrArray v) {
return less(a, to_array(v, a.dtype()));
},
"other"_a)
.def(
"__le__",
[](const array& a, const ScalarOrArray v) {
return less_equal(a, to_array(v, a.dtype()));
},
"other"_a)
.def(
"__gt__",
[](const array& a, const ScalarOrArray v) {
return greater(a, to_array(v, a.dtype()));
},
"other"_a)
.def(
"__ge__",
[](const array& a, const ScalarOrArray v) {
return greater_equal(a, to_array(v, a.dtype()));
},
"other"_a)
.def(
"__ne__",
[](const array& a, const ScalarOrArray v) {
return not_equal(a, to_array(v, a.dtype()));
},
"other"_a)
.def("__neg__", [](const array& a) { return -a; })
.def("__bool__", [](array& a) { return py::bool_(to_scalar(a)); })
.def(
"__repr__",
[](array& a) {
if (!a.is_evaled()) {
a.eval();
}
std::ostringstream os;
os << a;
return os.str();
})
.def(
"__matmul__",
[](const array& a, array& other) { return matmul(a, other); },
"other"_a)
.def(
"__imatmul__",
[](array& a, array& other) {
a.overwrite_descriptor(matmul(a, other));
return a;
},
"other"_a)
.def(
"__pow__",
[](const array& a, const ScalarOrArray v) {
return power(a, to_array(v, a.dtype()));
},
"other"_a)
.def(
"__ipow__",
[](array& a, const ScalarOrArray v) {
a.overwrite_descriptor(power(a, to_array(v, a.dtype())));
return a;
},
"other"_a)
.def(
"__invert__",
[](const array& a) {
if (is_floating_point(a.dtype())) {
throw std::invalid_argument(
"Floating point types not allowed with or bitwise inversion.");
}
if (a.dtype() != bool_) {
throw std::invalid_argument(
"Bitwise inversion not yet supported for integer types.");
}
return logical_not(a);
})
.def(
"__and__",
[](const array& a, const ScalarOrArray v) {
auto b = to_array(v, a.dtype());
if (is_floating_point(a.dtype()) || is_floating_point(b.dtype())) {
throw std::invalid_argument(
"Floating point types not allowed with bitwise and.");
}
if (a.dtype() != bool_ && b.dtype() != bool_) {
throw std::invalid_argument(
"Bitwise and not yet supported for integer types.");
}
return logical_and(a, b);