forked from ml-explore/mlx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathops.cpp
More file actions
5146 lines (4635 loc) · 171 KB
/
ops.cpp
File metadata and controls
5146 lines (4635 loc) · 171 KB
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-2024 Apple Inc.
#include <numeric>
#include <ostream>
#include <variant>
#include <nanobind/nanobind.h>
#include <nanobind/stl/optional.h>
#include <nanobind/stl/pair.h>
#include <nanobind/stl/string.h>
#include <nanobind/stl/tuple.h>
#include <nanobind/stl/variant.h>
#include <nanobind/stl/vector.h>
#include "mlx/einsum.h"
#include "mlx/ops.h"
#include "mlx/utils.h"
#include "python/src/load.h"
#include "python/src/utils.h"
namespace mx = mlx::core;
namespace nb = nanobind;
using namespace nb::literals;
using Scalar = std::variant<bool, int, double>;
mx::Dtype scalar_to_dtype(Scalar s) {
if (std::holds_alternative<int>(s)) {
return mx::int32;
} else if (std::holds_alternative<double>(s)) {
return mx::float32;
} else {
return mx::bool_;
}
}
double scalar_to_double(Scalar s) {
if (auto pv = std::get_if<int>(&s); pv) {
return static_cast<double>(*pv);
} else if (auto pv = std::get_if<double>(&s); pv) {
return *pv;
} else {
return static_cast<double>(std::get<bool>(s));
}
}
void init_ops(nb::module_& m) {
m.def(
"reshape",
&mx::reshape,
nb::arg(),
"shape"_a,
nb::kw_only(),
"stream"_a = nb::none(),
nb::sig("def reshape(a: array, /, shape: Sequence[int], *, stream: "
"Union[None, Stream, Device] = None) -> array"),
R"pbdoc(
Reshape an array while preserving the size.
Args:
a (array): Input array.
shape (tuple(int)): New shape.
stream (Stream, optional): Stream or device. Defaults to ``None``
in which case the default stream of the default device is used.
Returns:
array: The reshaped array.
)pbdoc");
m.def(
"flatten",
[](const mx::array& a,
int start_axis,
int end_axis,
const mx::StreamOrDevice& s) {
return mx::flatten(a, start_axis, end_axis);
},
nb::arg(),
"start_axis"_a = 0,
"end_axis"_a = -1,
nb::kw_only(),
"stream"_a = nb::none(),
nb::sig("def flatten(a: array, /, start_axis: int = 0, end_axis: int = "
"-1, *, stream: Union[None, Stream, Device] = None) -> array"),
R"pbdoc(
Flatten an array.
The axes flattened will be between ``start_axis`` and ``end_axis``,
inclusive. Negative axes are supported. After converting negative axis to
positive, axes outside the valid range will be clamped to a valid value,
``start_axis`` to ``0`` and ``end_axis`` to ``ndim - 1``.
Args:
a (array): Input array.
start_axis (int, optional): The first dimension to flatten. Defaults to ``0``.
end_axis (int, optional): The last dimension to flatten. Defaults to ``-1``.
stream (Stream, optional): Stream or device. Defaults to ``None``
in which case the default stream of the default device is used.
Returns:
array: The flattened array.
Example:
>>> a = mx.array([[1, 2], [3, 4]])
>>> mx.flatten(a)
array([1, 2, 3, 4], dtype=int32)
>>>
>>> mx.flatten(a, start_axis=0, end_axis=-1)
array([1, 2, 3, 4], dtype=int32)
)pbdoc");
m.def(
"unflatten",
&mx::unflatten,
nb::arg(),
"axis"_a,
"shape"_a,
nb::kw_only(),
"stream"_a = nb::none(),
nb::sig(
"def unflatten(a: array, /, axis: int, shape: Sequence[int], *, stream: Union[None, Stream, Device] = None) -> array"),
R"pbdoc(
Unflatten an axis of an array to a shape.
Args:
a (array): Input array.
axis (int): The axis to unflatten.
shape (tuple(int)): The shape to unflatten to. At most one
entry can be ``-1`` in which case the corresponding size will be
inferred.
stream (Stream, optional): Stream or device. Defaults to ``None``
in which case the default stream of the default device is used.
Returns:
array: The unflattened array.
Example:
>>> a = mx.array([1, 2, 3, 4])
>>> mx.unflatten(a, 0, (2, -1))
array([[1, 2], [3, 4]], dtype=int32)
)pbdoc");
m.def(
"squeeze",
[](const mx::array& a, const IntOrVec& v, const mx::StreamOrDevice& s) {
if (std::holds_alternative<std::monostate>(v)) {
return mx::squeeze(a, s);
} else if (auto pv = std::get_if<int>(&v); pv) {
return mx::squeeze(a, *pv, s);
} else {
return mx::squeeze(a, std::get<std::vector<int>>(v), s);
}
},
nb::arg(),
"axis"_a = nb::none(),
nb::kw_only(),
"stream"_a = nb::none(),
nb::sig(
"def squeeze(a: array, /, axis: Union[None, int, Sequence[int]] = "
"None, *, stream: Union[None, Stream, Device] = None) -> array"),
R"pbdoc(
Remove length one axes from an array.
Args:
a (array): Input array.
axis (int or tuple(int), optional): Axes to remove. Defaults
to ``None`` in which case all size one axes are removed.
Returns:
array: The output array with size one axes removed.
)pbdoc");
m.def(
"expand_dims",
[](const mx::array& a,
const std::variant<int, std::vector<int>>& v,
mx::StreamOrDevice s) {
if (auto pv = std::get_if<int>(&v); pv) {
return mx::expand_dims(a, *pv, s);
} else {
return mx::expand_dims(a, std::get<std::vector<int>>(v), s);
}
},
nb::arg(),
"axis"_a,
nb::kw_only(),
"stream"_a = nb::none(),
nb::sig("def expand_dims(a: array, /, axis: Union[int, Sequence[int]], "
"*, stream: Union[None, Stream, Device] = None) -> array"),
R"pbdoc(
Add a size one dimension at the given axis.
Args:
a (array): Input array.
axes (int or tuple(int)): The index of the inserted dimensions.
Returns:
array: The array with inserted dimensions.
)pbdoc");
m.def(
"abs",
[](const ScalarOrArray& a, mx::StreamOrDevice s) {
return mx::abs(to_array(a), s);
},
nb::arg(),
nb::kw_only(),
"stream"_a = nb::none(),
nb::sig(
"def abs(a: array, /, *, stream: Union[None, Stream, Device] = None) -> array"),
R"pbdoc(
Element-wise absolute value.
Args:
a (array): Input array.
Returns:
array: The absolute value of ``a``.
)pbdoc");
m.def(
"sign",
[](const ScalarOrArray& a, mx::StreamOrDevice s) {
return mx::sign(to_array(a), s);
},
nb::arg(),
nb::kw_only(),
"stream"_a = nb::none(),
nb::sig(
"def sign(a: array, /, *, stream: Union[None, Stream, Device] = None) -> array"),
R"pbdoc(
Element-wise sign.
Args:
a (array): Input array.
Returns:
array: The sign of ``a``.
)pbdoc");
m.def(
"negative",
[](const ScalarOrArray& a, mx::StreamOrDevice s) {
return mx::negative(to_array(a), s);
},
nb::arg(),
nb::kw_only(),
"stream"_a = nb::none(),
nb::sig(
"def negative(a: array, /, *, stream: Union[None, Stream, Device] = None) -> array"),
R"pbdoc(
Element-wise negation.
Args:
a (array): Input array.
Returns:
array: The negative of ``a``.
)pbdoc");
m.def(
"add",
[](const ScalarOrArray& a_,
const ScalarOrArray& b_,
mx::StreamOrDevice s) {
auto [a, b] = to_arrays(a_, b_);
return mx::add(a, b, s);
},
nb::arg(),
nb::arg(),
nb::kw_only(),
"stream"_a = nb::none(),
nb::sig(
"def add(a: Union[scalar, array], b: Union[scalar, array], stream: Union[None, Stream, Device] = None) -> array"),
R"pbdoc(
Element-wise addition.
Add two arrays with numpy-style broadcasting semantics. Either or both input arrays
can also be scalars.
Args:
a (array): Input array or scalar.
b (array): Input array or scalar.
Returns:
array: The sum of ``a`` and ``b``.
)pbdoc");
m.def(
"subtract",
[](const ScalarOrArray& a_,
const ScalarOrArray& b_,
mx::StreamOrDevice s) {
auto [a, b] = to_arrays(a_, b_);
return mx::subtract(a, b, s);
},
nb::arg(),
nb::arg(),
nb::kw_only(),
"stream"_a = nb::none(),
nb::sig(
"def subtract(a: Union[scalar, array], b: Union[scalar, array], stream: Union[None, Stream, Device] = None) -> array"),
R"pbdoc(
Element-wise subtraction.
Subtract one array from another with numpy-style broadcasting semantics. Either or both
input arrays can also be scalars.
Args:
a (array): Input array or scalar.
b (array): Input array or scalar.
Returns:
array: The difference ``a - b``.
)pbdoc");
m.def(
"multiply",
[](const ScalarOrArray& a_,
const ScalarOrArray& b_,
mx::StreamOrDevice s) {
auto [a, b] = to_arrays(a_, b_);
return mx::multiply(a, b, s);
},
nb::arg(),
nb::arg(),
nb::kw_only(),
"stream"_a = nb::none(),
nb::sig(
"def multiply(a: Union[scalar, array], b: Union[scalar, array], stream: Union[None, Stream, Device] = None) -> array"),
R"pbdoc(
Element-wise multiplication.
Multiply two arrays with numpy-style broadcasting semantics. Either or both
input arrays can also be scalars.
Args:
a (array): Input array or scalar.
b (array): Input array or scalar.
Returns:
array: The multiplication ``a * b``.
)pbdoc");
m.def(
"divide",
[](const ScalarOrArray& a_,
const ScalarOrArray& b_,
mx::StreamOrDevice s) {
auto [a, b] = to_arrays(a_, b_);
return mx::divide(a, b, s);
},
nb::arg(),
nb::arg(),
nb::kw_only(),
"stream"_a = nb::none(),
nb::sig(
"def divide(a: Union[scalar, array], b: Union[scalar, array], stream: Union[None, Stream, Device] = None) -> array"),
R"pbdoc(
Element-wise division.
Divide two arrays with numpy-style broadcasting semantics. Either or both
input arrays can also be scalars.
Args:
a (array): Input array or scalar.
b (array): Input array or scalar.
Returns:
array: The quotient ``a / b``.
)pbdoc");
m.def(
"divmod",
[](const ScalarOrArray& a_,
const ScalarOrArray& b_,
mx::StreamOrDevice s) {
auto [a, b] = to_arrays(a_, b_);
return mx::divmod(a, b, s);
},
nb::arg(),
nb::arg(),
nb::kw_only(),
"stream"_a = nb::none(),
nb::sig(
"def divmod(a: Union[scalar, array], b: Union[scalar, array], stream: Union[None, Stream, Device] = None) -> array"),
R"pbdoc(
Element-wise quotient and remainder.
The fuction ``divmod(a, b)`` is equivalent to but faster than
``(a // b, a % b)``. The function uses numpy-style broadcasting
semantics. Either or both input arrays can also be scalars.
Args:
a (array): Input array or scalar.
b (array): Input array or scalar.
Returns:
tuple(array, array): The quotient ``a // b`` and remainder ``a % b``.
)pbdoc");
m.def(
"floor_divide",
[](const ScalarOrArray& a_,
const ScalarOrArray& b_,
mx::StreamOrDevice s) {
auto [a, b] = to_arrays(a_, b_);
return mx::floor_divide(a, b, s);
},
nb::arg(),
nb::arg(),
nb::kw_only(),
"stream"_a = nb::none(),
nb::sig(
"def floor_divide(a: Union[scalar, array], b: Union[scalar, array], stream: Union[None, Stream, Device] = None) -> array"),
R"pbdoc(
Element-wise integer division.
If either array is a floating point type then it is equivalent to
calling :func:`floor` after :func:`divide`.
Args:
a (array): Input array or scalar.
b (array): Input array or scalar.
Returns:
array: The quotient ``a // b``.
)pbdoc");
m.def(
"remainder",
[](const ScalarOrArray& a_,
const ScalarOrArray& b_,
mx::StreamOrDevice s) {
auto [a, b] = to_arrays(a_, b_);
return mx::remainder(a, b, s);
},
nb::arg(),
nb::arg(),
nb::kw_only(),
"stream"_a = nb::none(),
nb::sig(
"def remainder(a: Union[scalar, array], b: Union[scalar, array], stream: Union[None, Stream, Device] = None) -> array"),
R"pbdoc(
Element-wise remainder of division.
Computes the remainder of dividing a with b with numpy-style
broadcasting semantics. Either or both input arrays can also be
scalars.
Args:
a (array): Input array or scalar.
b (array): Input array or scalar.
Returns:
array: The remainder of ``a // b``.
)pbdoc");
m.def(
"equal",
[](const ScalarOrArray& a_,
const ScalarOrArray& b_,
mx::StreamOrDevice s) {
auto [a, b] = to_arrays(a_, b_);
return mx::equal(a, b, s);
},
nb::arg(),
nb::arg(),
nb::kw_only(),
"stream"_a = nb::none(),
nb::sig(
"def equal(a: Union[scalar, array], b: Union[scalar, array], stream: Union[None, Stream, Device] = None) -> array"),
R"pbdoc(
Element-wise equality.
Equality comparison on two arrays with numpy-style broadcasting semantics.
Either or both input arrays can also be scalars.
Args:
a (array): Input array or scalar.
b (array): Input array or scalar.
Returns:
array: The element-wise comparison ``a == b``.
)pbdoc");
m.def(
"not_equal",
[](const ScalarOrArray& a_,
const ScalarOrArray& b_,
mx::StreamOrDevice s) {
auto [a, b] = to_arrays(a_, b_);
return mx::not_equal(a, b, s);
},
nb::arg(),
nb::arg(),
nb::kw_only(),
"stream"_a = nb::none(),
nb::sig(
"def not_equal(a: Union[scalar, array], b: Union[scalar, array], stream: Union[None, Stream, Device] = None) -> array"),
R"pbdoc(
Element-wise not equal.
Not equal comparison on two arrays with numpy-style broadcasting semantics.
Either or both input arrays can also be scalars.
Args:
a (array): Input array or scalar.
b (array): Input array or scalar.
Returns:
array: The element-wise comparison ``a != b``.
)pbdoc");
m.def(
"less",
[](const ScalarOrArray& a_,
const ScalarOrArray& b_,
mx::StreamOrDevice s) {
auto [a, b] = to_arrays(a_, b_);
return mx::less(a, b, s);
},
nb::arg(),
nb::arg(),
nb::kw_only(),
"stream"_a = nb::none(),
nb::sig(
"def less(a: Union[scalar, array], b: Union[scalar, array], stream: Union[None, Stream, Device] = None) -> array"),
R"pbdoc(
Element-wise less than.
Strict less than on two arrays with numpy-style broadcasting semantics.
Either or both input arrays can also be scalars.
Args:
a (array): Input array or scalar.
b (array): Input array or scalar.
Returns:
array: The element-wise comparison ``a < b``.
)pbdoc");
m.def(
"less_equal",
[](const ScalarOrArray& a_,
const ScalarOrArray& b_,
mx::StreamOrDevice s) {
auto [a, b] = to_arrays(a_, b_);
return mx::less_equal(a, b, s);
},
nb::arg(),
nb::arg(),
nb::kw_only(),
"stream"_a = nb::none(),
nb::sig(
"def less_equal(a: Union[scalar, array], b: Union[scalar, array], stream: Union[None, Stream, Device] = None) -> array"),
R"pbdoc(
Element-wise less than or equal.
Less than or equal on two arrays with numpy-style broadcasting semantics.
Either or both input arrays can also be scalars.
Args:
a (array): Input array or scalar.
b (array): Input array or scalar.
Returns:
array: The element-wise comparison ``a <= b``.
)pbdoc");
m.def(
"greater",
[](const ScalarOrArray& a_,
const ScalarOrArray& b_,
mx::StreamOrDevice s) {
auto [a, b] = to_arrays(a_, b_);
return mx::greater(a, b, s);
},
nb::arg(),
nb::arg(),
nb::kw_only(),
"stream"_a = nb::none(),
nb::sig(
"def greater(a: Union[scalar, array], b: Union[scalar, array], stream: Union[None, Stream, Device] = None) -> array"),
R"pbdoc(
Element-wise greater than.
Strict greater than on two arrays with numpy-style broadcasting semantics.
Either or both input arrays can also be scalars.
Args:
a (array): Input array or scalar.
b (array): Input array or scalar.
Returns:
array: The element-wise comparison ``a > b``.
)pbdoc");
m.def(
"greater_equal",
[](const ScalarOrArray& a_,
const ScalarOrArray& b_,
mx::StreamOrDevice s) {
auto [a, b] = to_arrays(a_, b_);
return mx::greater_equal(a, b, s);
},
nb::arg(),
nb::arg(),
nb::kw_only(),
"stream"_a = nb::none(),
nb::sig(
"def greater_equal(a: Union[scalar, array], b: Union[scalar, array], stream: Union[None, Stream, Device] = None) -> array"),
R"pbdoc(
Element-wise greater or equal.
Greater than or equal on two arrays with numpy-style broadcasting semantics.
Either or both input arrays can also be scalars.
Args:
a (array): Input array or scalar.
b (array): Input array or scalar.
Returns:
array: The element-wise comparison ``a >= b``.
)pbdoc");
m.def(
"array_equal",
[](const ScalarOrArray& a_,
const ScalarOrArray& b_,
bool equal_nan,
mx::StreamOrDevice s) {
auto [a, b] = to_arrays(a_, b_);
return mx::array_equal(a, b, equal_nan, s);
},
nb::arg(),
nb::arg(),
nb::kw_only(),
"equal_nan"_a = false,
"stream"_a = nb::none(),
nb::sig(
"def array_equal(a: Union[scalar, array], b: Union[scalar, array], equal_nan: bool = False, stream: Union[None, Stream, Device] = None) -> array"),
R"pbdoc(
Array equality check.
Compare two arrays for equality. Returns ``True`` if and only if the arrays
have the same shape and their values are equal. The arrays need not have
the same type to be considered equal.
Args:
a (array): Input array or scalar.
b (array): Input array or scalar.
equal_nan (bool): If ``True``, NaNs are considered equal.
Defaults to ``False``.
Returns:
array: A scalar boolean array.
)pbdoc");
m.def(
"matmul",
&mx::matmul,
nb::arg(),
nb::arg(),
nb::kw_only(),
"stream"_a = nb::none(),
nb::sig(
"def matmul(a: array, b: array, /, *, stream: Union[None, Stream, Device] = None) -> array"),
R"pbdoc(
Matrix multiplication.
Perform the (possibly batched) matrix multiplication of two arrays. This function supports
broadcasting for arrays with more than two dimensions.
- If the first array is 1-D then a 1 is prepended to its shape to make it
a matrix. Similarly if the second array is 1-D then a 1 is appended to its
shape to make it
4E22
a matrix. In either case the singleton dimension is removed
from the result.
- A batched matrix multiplication is performed if the arrays have more than
2 dimensions. The matrix dimensions for the matrix product are the last
two dimensions of each input.
- All but the last two dimensions of each input are broadcast with one another using
standard numpy-style broadcasting semantics.
Args:
a (array): Input array or scalar.
b (array): Input array or scalar.
Returns:
array: The matrix product of ``a`` and ``b``.
)pbdoc");
m.def(
"square",
[](const ScalarOrArray& a, mx::StreamOrDevice s) {
return mx::square(to_array(a), s);
},
nb::arg(),
nb::kw_only(),
"stream"_a = nb::none(),
nb::sig(
"def square(a: array, /, *, stream: Union[None, Stream, Device] = None) -> array"),
R"pbdoc(
Element-wise square.
Args:
a (array): Input array.
Returns:
array: The square of ``a``.
)pbdoc");
m.def(
"sqrt",
[](const ScalarOrArray& a, mx::StreamOrDevice s) {
return mx::sqrt(to_array(a), s);
},
nb::arg(),
nb::kw_only(),
"stream"_a = nb::none(),
nb::sig(
"def sqrt(a: array, /, *, stream: Union[None, Stream, Device] = None) -> array"),
R"pbdoc(
Element-wise square root.
Args:
a (array): Input array.
Returns:
array: The square root of ``a``.
)pbdoc");
m.def(
"rsqrt",
[](const ScalarOrArray& a, mx::StreamOrDevice s) {
return mx::rsqrt(to_array(a), s);
},
nb::arg(),
nb::kw_only(),
"stream"_a = nb::none(),
nb::sig(
"def rsqrt(a: array, /, *, stream: Union[None, Stream, Device] = None) -> array"),
R"pbdoc(
Element-wise reciprocal and square root.
Args:
a (array): Input array.
Returns:
array: One over the square root of ``a``.
)pbdoc");
m.def(
"reciprocal",
[](const ScalarOrArray& a, mx::StreamOrDevice s) {
return mx::reciprocal(to_array(a), s);
},
nb::arg(),
nb::kw_only(),
"stream"_a = nb::none(),
nb::sig(
"def reciprocal(a: array, /, *, stream: Union[None, Stream, Device] = None) -> array"),
R"pbdoc(
Element-wise reciprocal.
Args:
a (array): Input array.
Returns:
array: The reciprocal of ``a``.
)pbdoc");
m.def(
"logical_not",
[](const ScalarOrArray& a, mx::StreamOrDevice s) {
return mx::logical_not(to_array(a), s);
},
nb::arg(),
nb::kw_only(),
"stream"_a = nb::none(),
nb::sig(
"def logical_not(a: array, /, *, stream: Union[None, Stream, Device] = None) -> array"),
R"pbdoc(
Element-wise logical not.
Args:
a (array): Input array or scalar.
Returns:
array: The boolean array containing the logical not of ``a``.
)pbdoc");
m.def(
"logical_and",
[](const ScalarOrArray& a, const ScalarOrArray& b, mx::StreamOrDevice s) {
return mx::logical_and(to_array(a), to_array(b), s);
},
nb::arg(),
nb::arg(),
nb::kw_only(),
"stream"_a = nb::none(),
nb::sig(
"def logical_and(a: array, b: array, /, *, stream: Union[None, Stream, Device] = None) -> array"),
R"pbdoc(
Element-wise logical and.
Args:
a (array): First input array or scalar.
b (array): Second input array or scalar.
Returns:
array: The boolean array containing the logical and of ``a`` and ``b``.
)pbdoc");
m.def(
"logical_or",
[](const ScalarOrArray& a, const ScalarOrArray& b, mx::StreamOrDevice s) {
return mx::logical_or(to_array(a), to_array(b), s);
},
nb::arg(),
nb::arg(),
nb::kw_only(),
"stream"_a = nb::none(),
nb::sig(
"def logical_or(a: array, b: array, /, *, stream: Union[None, Stream, Device] = None) -> array"),
R"pbdoc(
Element-wise logical or.
Args:
a (array): First input array or scalar.
b (array): Second input array or scalar.
Returns:
array: The boolean array containing the logical or of ``a`` and ``b``.
)pbdoc");
m.def(
"logaddexp",
[](const ScalarOrArray& a_,
const ScalarOrArray& b_,
mx::StreamOrDevice s) {
auto [a, b] = to_arrays(a_, b_);
return mx::logaddexp(a, b, s);
},
nb::arg(),
nb::arg(),
nb::kw_only(),
"stream"_a = nb::none(),
nb::sig(
"def logaddexp(a: Union[scalar, array], b: Union[scalar, array], /, *, stream: Union[None, Stream, Device] = None) -> array"),
R"pbdoc(
Element-wise log-add-exp.
This is a numerically stable log-add-exp of two arrays with numpy-style
broadcasting semantics. Either or both input arrays can also be scalars.
The computation is is a numerically stable version of ``log(exp(a) + exp(b))``.
Args:
a (array): Input array or scalar.
b (array): Input array or scalar.
Returns:
array: The log-add-exp of ``a`` and ``b``.
)pbdoc");
m.def(
"exp",
[](const ScalarOrArray& a, mx::StreamOrDevice s) {
return mx::exp(to_array(a), s);
},
nb::arg(),
nb::kw_only(),
"stream"_a = nb::none(),
nb::sig(
"def exp(a: array, /, *, stream: Union[None, Stream, Device] = None) -> array"),
R"pbdoc(
Element-wise exponential.
Args:
a (array): Input array.
Returns:
array: The exponential of ``a``.
)pbdoc");
m.def(
"expm1",
[](const ScalarOrArray& a, mx::StreamOrDevice s) {
return mx::expm1(to_array(a), s);
},
nb::arg(),
nb::kw_only(),
"stream"_a = nb::none(),
nb::sig(
"def expm1(a: array, /, *, stream: Union[None, Stream, Device] = None) -> array"),
R"pbdoc(
Element-wise exponential minus 1.
Computes ``exp(x) - 1`` with greater precision for small ``x``.
Args:
a (array): Input array.
Returns:
array: The expm1 of ``a``.
)pbdoc");
m.def(
"erf",
[](const ScalarOrArray& a, mx::StreamOrDevice s) {
return mx::erf(to_array(a), s);
},
nb::arg(),
nb::kw_only(),
"stream"_a = nb::none(),
nb::sig(
"def erf(a: array, /, *, stream: Union[None, Stream, Device] = None) -> array"),
R"pbdoc(
Element-wise error function.
.. math::
\mathrm{erf}(x) = \frac{2}{\sqrt{\pi}} \int_0^x e^{-t^2} \, dt
Args:
a (array): Input array.
Returns:
array: The error function of ``a``.
)pbdoc");
m.def(
"erfinv",
[](const ScalarOrArray& a, mx::StreamOrDevice s) {
return mx::erfinv(to_array(a), s);
},
nb::arg(),
nb::kw_only(),
"stream"_a = nb::none(),
nb::sig(
"def erfinv(a: array, /, *, stream: Union[None, Stream, Device] = None) -> array"),
R"pbdoc(
Element-wise inverse of :func:`erf`.
Args:
a (array): Input array.
Returns:
array: The inverse error function of ``a``.
)pbdoc");
m.def(
"sin",
[](const ScalarOrArray& a, mx::StreamOrDevice s) {
return mx::sin(to_array(a), s);
},
nb::arg(),
nb::kw_only(),
"stream"_a = nb::none(),
nb::sig(
"def sin(a: array, /, *, stream: Union[None, Stream, Device] = None) -> array"),
R"pbdoc(
Element-wise sine.
Args:
a (array): Input array.
Returns:
array: The sine of ``a``.
)pbdoc");
m.def(
"cos",
[](const ScalarOrArray& a, mx::StreamOrDevice s) {
return mx::cos(to_array(a), s);
},
nb::arg(),
nb::kw_only(),
"stream"_a = nb::none(),
nb::sig(
"def cos(a: array, /, *, stream: Union[None, Stream, Device] = None) -> array"),
R"pbdoc(
Element-wise cosine.
Args:
a (array): Input array.
Returns:
array: The cosine of ``a``.
)pbdoc");
m.def(
"tan",
[](const ScalarOrArray& a, mx::StreamOrDevice s) {
return mx::tan(to_array(a), s);
},
nb::arg(),
nb::kw_only(),
"stream"_a = nb::none(),
nb::sig(
"def tan(a: array, /, *, stream: Union[None, Stream, Device] = None) -> array"),
R"pbdoc(
Element-wise tangent.
Args:
a (array): Input array.
Returns:
array: The tangent of ``a``.
)pbdoc");
m.def(
"arcsin",
[](const ScalarOrArray& a, mx::StreamOrDevice s) {
return mx::arcsin(to_array(a), s);
},
nb::arg(),
nb::kw_only(),
"stream"_a = nb::none(),
nb::sig(
"def arcsin(a: array, /, *, stream: Union[None, Stream, Device] = None) -> array"),
R"pbdoc(
Element-wise inverse sine.
Args:
a (array): Input array.
Returns:
array: The inverse sine of ``a``.
)pbdoc");
m.def(
"arccos",
[](const ScalarOrArray& a, mx::StreamOrDevice s) {
return mx::arccos(to_array(a), s);
},
nb::arg(),
nb::kw_only(),