MLX
Loading...
Searching...
No Matches
ops.h
Go to the documentation of this file.
1// Copyright © 2023-2024 Apple Inc.
2
3#pragma once
4
5#include <optional>
6
7#include "mlx/array.h"
8#include "mlx/device.h"
9#include "mlx/stream.h"
10#include "mlx/utils.h"
11
12namespace mlx::core {
13
23 double start,
24 double stop,
25 double step,
26 Dtype dtype,
27 StreamOrDevice s = {});
28array arange(double start, double stop, double step, StreamOrDevice s = {});
29array arange(double start, double stop, Dtype dtype, StreamOrDevice s = {});
30array arange(double start, double stop, StreamOrDevice s = {});
31array arange(double stop, Dtype dtype, StreamOrDevice s = {});
32array arange(double stop, StreamOrDevice s = {});
33
34array arange(int start, int stop, int step, StreamOrDevice s = {});
35array arange(int start, int stop, StreamOrDevice s = {});
36array arange(int stop, StreamOrDevice s = {});
37
40 double start,
41 double stop,
42 int num = 50,
43 Dtype dtype = float32,
44 StreamOrDevice s = {});
45
48
51 array a,
52 std::vector<int> shape,
53 std::vector<size_t> strides,
54 size_t offset,
55 StreamOrDevice s = {});
56
59
62 std::vector<int> shape,
63 array vals,
64 Dtype dtype,
65 StreamOrDevice s = {});
66array full(std::vector<int> shape, array vals, StreamOrDevice s = {});
67template <typename T>
68array full(std::vector<int> shape, T val, Dtype dtype, StreamOrDevice s = {}) {
69 return full(std::move(shape), array(val, dtype), to_stream(s));
70}
71template <typename T>
72array full(std::vector<int> shape, T val, StreamOrDevice s = {}) {
73 return full(std::move(shape), array(val), to_stream(s));
74}
75
77array zeros(const std::vector<int>& shape, Dtype dtype, StreamOrDevice s = {});
78inline array zeros(const std::vector<int>& shape, StreamOrDevice s = {}) {
79 return zeros(shape, float32, s);
80}
82
84array ones(const std::vector<int>& shape, Dtype dtype, StreamOrDevice s = {});
85inline array ones(const std::vector<int>& shape, StreamOrDevice s = {}) {
86 return ones(shape, float32, s);
87}
89
92array eye(int n, int m, int k, Dtype dtype, StreamOrDevice s = {});
93inline array eye(int n, Dtype dtype, StreamOrDevice s = {}) {
94 return eye(n, n, 0, dtype, s);
95}
96inline array eye(int n, int m, StreamOrDevice s = {}) {
97 return eye(n, m, 0, float32, s);
98}
99inline array eye(int n, int m, int k, StreamOrDevice s = {}) {
100 return eye(n, m, k, float32, s);
101}
102inline array eye(int n, StreamOrDevice s = {}) {
103 return eye(n, n, 0, float32, s);
104}
105
108array identity(int n, Dtype dtype, StreamOrDevice s = {});
109inline array identity(int n, StreamOrDevice s = {}) {
110 return identity(n, float32, s);
111}
112
113array tri(int n, int m, int k, Dtype type, StreamOrDevice s = {});
114inline array tri(int n, Dtype type, StreamOrDevice s = {}) {
115 return tri(n, n, 0, type, s);
116}
117
118array tril(array x, int k = 0, StreamOrDevice s = {});
119array triu(array x, int k = 0, StreamOrDevice s = {});
120
122array reshape(const array& a, std::vector<int> shape, StreamOrDevice s = {});
123
126 const array& a,
127 int start_axis,
128 int end_axis = -1,
129 StreamOrDevice s = {});
130
133
136 const array& a,
137 const std::vector<int>& axes,
138 StreamOrDevice s = {});
139
141inline array squeeze(const array& a, int axis, StreamOrDevice s = {}) {
142 return squeeze(a, std::vector<int>{axis}, s);
143}
144
147
150 const array& a,
151 const std::vector<int>& axes,
152 StreamOrDevice s = {});
153
155array expand_dims(const array& a, int axis, StreamOrDevice s = {});
156
159 const array& a,
160 std::vector<int> start,
161 std::vector<int> stop,
162 std::vector<int> strides,
163 StreamOrDevice s = {});
164
167 const array& a,
168 const std::vector<int>& start,
169 const std::vector<int>& stop,
170 StreamOrDevice s = {});
171
174 const array& src,
175 const array& update,
176 std::vector<int> start,
177 std::vector<int> stop,
178 std::vector<int> strides,
179 StreamOrDevice s = {});
180
183 const array& src,
184 const array& update,
185 std::vector<int> start,
186 std::vector<int> stop,
187 StreamOrDevice s = {});
188
190std::vector<array>
191split(const array& a, int num_splits, int axis, StreamOrDevice s = {});
192std::vector<array> split(const array& a, int num_splits, StreamOrDevice s = {});
193std::vector<array> split(
194 const array& a,
195 const std::vector<int>& indices,
196 int axis,
197 StreamOrDevice s = {});
198std::vector<array>
199split(const array& a, const std::vector<int>& indices, StreamOrDevice s = {});
200
202std::vector<array> meshgrid(
203 const std::vector<array>& arrays,
204 bool sparse = false,
205 std::string indexing = "xy",
206 StreamOrDevice s = {});
207
212 const array& a,
213 const std::optional<array>& a_min = std::nullopt,
214 const std::optional<array>& a_max = std::nullopt,
215 StreamOrDevice s = {});
216
219 const std::vector<array>& arrays,
220 int axis,
221 StreamOrDevice s = {});
222array concatenate(const std::vector<array>& arrays, StreamOrDevice s = {});
223
225array stack(const std::vector<array>& arrays, int axis, StreamOrDevice s = {});
226array stack(const std::vector<array>& arrays, StreamOrDevice s = {});
227
229array repeat(const array& arr, int repeats, int axis, StreamOrDevice s = {});
230array repeat(const array& arr, int repeats, StreamOrDevice s = {});
231
232array tile(const array& arr, std::vector<int> reps, StreamOrDevice s = {});
233
235array transpose(const array& a, std::vector<int> axes, StreamOrDevice s = {});
237 const array& a,
238 std::initializer_list<int> axes,
239 StreamOrDevice s = {}) {
240 return transpose(a, std::vector<int>(axes), s);
241}
242
244array swapaxes(const array& a, int axis1, int axis2, StreamOrDevice s = {});
245
248 const array& a,
249 int source,
250 int destination,
251 StreamOrDevice s = {});
252
255 const array& a,
256 const std::vector<int>& axes,
257 const std::vector<int>& low_pad_size,
258 const std::vector<int>& high_pad_size,
259 const array& pad_value = array(0),
260 StreamOrDevice s = {});
261
264 const array& a,
265 const std::vector<std::pair<int, int>>& pad_width,
266 const array& pad_value = array(0),
267 StreamOrDevice s = {});
269 const array& a,
270 const std::pair<int, int>& pad_width,
271 const array& pad_value = array(0),
272 StreamOrDevice s = {});
274 const array& a,
275 int pad_width,
276 const array& pad_value = array(0),
277 StreamOrDevice s = {});
278
281
284 const array& a,
285 const std::vector<int>& shape,
286 StreamOrDevice s = {});
287
289std::vector<array> broadcast_arrays(
290 const std::vector<array>& inputs,
291 StreamOrDevice s = {});
292
294array equal(const array& a, const array& b, StreamOrDevice s = {});
295inline array operator==(const array& a, const array& b) {
296 return equal(a, b);
297}
298template <typename T>
299array operator==(T a, const array& b) {
300 return equal(array(a), b);
301}
302template <typename T>
303array operator==(const array& a, T b) {
304 return equal(a, array(b));
305}
306
308array not_equal(const array& a, const array& b, StreamOrDevice s = {});
309inline array operator!=(const array& a, const array& b) {
310 return not_equal(a, b);
311}
312template <typename T>
313array operator!=(T a, const array& b) {
314 return not_equal(array(a), b);
315}
316template <typename T>
317array operator!=(const array& a, T b) {
318 return not_equal(a, array(b));
319}
320
322array greater(const array& a, const array& b, StreamOrDevice s = {});
323inline array operator>(const array& a, const array& b) {
324 return greater(a, b);
325}
326template <typename T>
327array operator>(T a, const array& b) {
328 return greater(array(a), b);
329}
330template <typename T>
331array operator>(const array& a, T b) {
332 return greater(a, array(b));
333}
334
336array greater_equal(const array& a, const array& b, StreamOrDevice s = {});
337inline array operator>=(const array& a, const array& b) {
338 return greater_equal(a, b);
339}
340template <typename T>
341array operator>=(T a, const array& b) {
342 return greater_equal(array(a), b);
343}
344template <typename T>
345array operator>=(const array& a, T b) {
346 return greater_equal(a, array(b));
347}
348
350array less(const array& a, const array& b, StreamOrDevice s = {});
351inline array operator<(const array& a, const array& b) {
352 return less(a, b);
353}
354template <typename T>
355array operator<(T a, const array& b) {
356 return less(array(a), b);
357}
358template <typename T>
359array operator<(const array& a, T b) {
360 return less(a, array(b));
361}
362
364array less_equal(const array& a, const array& b, StreamOrDevice s = {});
365inline array operator<=(const array& a, const array& b) {
366 return less_equal(a, b);
367}
368template <typename T>
369array operator<=(T a, const array& b) {
370 return less_equal(array(a), b);
371}
372template <typename T>
373array operator<=(const array& a, T b) {
374 return less_equal(a, array(b));
375}
376
379 const array& a,
380 const array& b,
381 bool equal_nan,
382 StreamOrDevice s = {});
383inline array
384array_equal(const array& a, const array& b, StreamOrDevice s = {}) {
385 return array_equal(a, b, false, s);
386}
387
388array isnan(const array& a, StreamOrDevice s = {});
389
390array isinf(const array& a, StreamOrDevice s = {});
391
393
395
398 const array& condition,
399 const array& x,
400 const array& y,
401 StreamOrDevice s = {});
402
404array all(const array& a, bool keepdims, StreamOrDevice s = {});
405inline array all(const array& a, StreamOrDevice s = {}) {
406 return all(a, false, to_stream(s));
407}
408
411 const array& a,
412 const array& b,
413 double rtol = 1e-5,
414 double atol = 1e-8,
415 bool equal_nan = false,
416 StreamOrDevice s = {});
417
421 const array& a,
422 const array& b,
423 double rtol = 1e-5,
424 double atol = 1e-8,
425 bool equal_nan = false,
426 StreamOrDevice s = {});
427
433 const array& a,
434 const std::vector<int>& axes,
435 bool keepdims = false,
436 StreamOrDevice s = {});
437
443 const array& a,
444 int axis,
445 bool keepdims = false,
446 StreamOrDevice s = {});
447
449array any(const array& a, bool keepdims, StreamOrDevice s = {});
450inline array any(const array& a, StreamOrDevice s = {}) {
451 return any(a, false, to_stream(s));
452}
453
459 const array& a,
460 const std::vector<int>& axes,
461 bool keepdims = false,
462 StreamOrDevice s = {});
463
469 const array& a,
470 int axis,
471 bool keepdims = false,
472 StreamOrDevice s = {});
473
475array sum(const array& a, bool keepdims, StreamOrDevice s = {});
476inline array sum(const array& a, StreamOrDevice s = {}) {
477 return sum(a, false, to_stream(s));
478}
479
482 const array& a,
483 const std::vector<int>& axes,
484 bool keepdims = false,
485 StreamOrDevice s = {});
486
489 const array& a,
490 int axis,
491 bool keepdims = false,
492 StreamOrDevice s = {});
493
495array mean(const array& a, bool keepdims, StreamOrDevice s = {});
496inline array mean(const array& a, StreamOrDevice s = {}) {
497 return mean(a, false, to_stream(s));
498}
499
502 const array& a,
503 const std::vector<int>& axes,
504 bool keepdims = false,
505 StreamOrDevice s = {});
506
509 const array& a,
510 int axis,
511 bool keepdims = false,
512 StreamOrDevice s = {});
513
515array var(const array& a, bool keepdims, int ddof = 0, StreamOrDevice s = {});
516inline array var(const array& a, StreamOrDevice s = {}) {
517 return var(a, false, 0, to_stream(s));
518}
519
523 const array& a,
524 const std::vector<int>& axes,
525 bool keepdims = false,
526 int ddof = 0,
527 StreamOrDevice s = {});
528
532 const array& a,
533 int axis,
534 bool keepdims = false,
535 int ddof = 0,
536 StreamOrDevice s = {});
537
539array std(const array& a, bool keepdims, int ddof = 0, StreamOrDevice s = {});
540inline array std(const array& a, StreamOrDevice s = {}) {
541 return std(a, false, 0, to_stream(s));
542}
543
547 const array& a,
548 const std::vector<int>& axes,
549 bool keepdims = false,
550 int ddof = 0,
551 StreamOrDevice s = {});
552
556 const array& a,
557 int axis,
558 bool keepdims = false,
559 int ddof = 0,
560 StreamOrDevice s = {});
561
563array prod(const array& a, bool keepdims, StreamOrDevice s = {});
564inline array prod(const array& a, StreamOrDevice s = {}) {
565 return prod(a, false, to_stream(s));
566}
567
570 const array& a,
571 const std::vector<int>& axes,
572 bool keepdims = false,
573 StreamOrDevice s = {});
574
577 const array& a,
578 int axis,
579 bool keepdims = false,
580 StreamOrDevice s = {});
581
583array max(const array& a, bool keepdims, StreamOrDevice s = {});
584inline array max(const array& a, StreamOrDevice s = {}) {
585 return max(a, false, to_stream(s));
586}
587
590 const array& a,
591 const std::vector<int>& axes,
592 bool keepdims = false,
593 StreamOrDevice s = {});
594
597 const array& a,
598 int axis,
599 bool keepdims = false,
600 StreamOrDevice s = {});
601
603array min(const array& a, bool keepdims, StreamOrDevice s = {});
604inline array min(const array& a, StreamOrDevice s = {}) {
605 return min(a, false, to_stream(s));
606}
607
610 const array& a,
611 const std::vector<int>& axes,
612 bool keepdims = false,
613 StreamOrDevice s = {});
614
617 const array& a,
618 int axis,
619 bool keepdims = false,
620 StreamOrDevice s = {});
621
623array argmin(const array& a, bool keepdims, StreamOrDevice s = {});
624inline array argmin(const array& a, StreamOrDevice s = {}) {
625 return argmin(a, false, s);
626}
627
630 const array& a,
631 int axis,
632 bool keepdims = false,
633 StreamOrDevice s = {});
634
636array argmax(const array& a, bool keepdims, StreamOrDevice s = {});
637inline array argmax(const array& a, StreamOrDevice s = {}) {
638 return argmax(a, false, s);
639}
640
643 const array& a,
644 int axis,
645 bool keepdims = false,
646 StreamOrDevice s = {});
647
649array sort(const array& a, StreamOrDevice s = {});
650
652array sort(const array& a, int axis, StreamOrDevice s = {});
653
656
658array argsort(const array& a, int axis, StreamOrDevice s = {});
659
664array partition(const array& a, int kth, StreamOrDevice s = {});
665
670array partition(const array& a, int kth, int axis, StreamOrDevice s = {});
671
676array argpartition(const array& a, int kth, StreamOrDevice s = {});
677
682array argpartition(const array& a, int kth, int axis, StreamOrDevice s = {});
683
685array topk(const array& a, int k, StreamOrDevice s = {});
686
688array topk(const array& a, int k, int axis, StreamOrDevice s = {});
689
691array logsumexp(const array& a, bool keepdims, StreamOrDevice s = {});
692inline array logsumexp(const array& a, StreamOrDevice s = {}) {
693 return logsumexp(a, false, to_stream(s));
694}
695
698 const array& a,
699 const std::vector<int>& axes,
700 bool keepdims = false,
701 StreamOrDevice s = {});
702
705 const array& a,
706 int axis,
707 bool keepdims = false,
708 StreamOrDevice s = {});
709
711array abs(const array& a, StreamOrDevice s = {});
712
716
718array sign(const array& a, StreamOrDevice s = {});
719
722
724array logical_and(const array& a, const array& b, StreamOrDevice s = {});
725array operator&&(const array& a, const array& b);
726
728array logical_or(const array& a, const array& b, StreamOrDevice s = {});
729array operator||(const array& a, const array& b);
730
733
735array add(const array& a, const array& b, StreamOrDevice s = {});
736array operator+(const array& a, const array& b);
737template <typename T>
738array operator+(T a, const array& b) {
739 return add(array(a), b);
740}
741template <typename T>
742array operator+(const array& a, T b) {
743 return add(a, array(b));
744}
745
747array subtract(const array& a, const array& b, StreamOrDevice s = {});
748array operator-(const array& a, const array& b);
749template <typename T>
750array operator-(T a, const array& b) {
751 return subtract(array(a), b);
752}
753template <typename T>
754array operator-(const array& a, T b) {
755 return subtract(a, array(b));
756}
757
759array multiply(const array& a, const array& b, StreamOrDevice s = {});
760array operator*(const array& a, const array& b);
761template <typename T>
762array operator*(T a, const array& b) {
763 return multiply(array(a), b);
764}
765template <typename T>
766array operator*(const array& a, T b) {
767 return multiply(a, array(b));
768}
769
771array divide(const array& a, const array& b, StreamOrDevice s = {});
772array operator/(const array& a, const array& b);
773array operator/(double a, const array& b);
774array operator/(const array& a, double b);
775
777std::vector<array>
778divmod(const array& a, const array& b, StreamOrDevice s = {});
779
781array floor_divide(const array& a, const array& b, StreamOrDevice s = {});
782
784array remainder(const array& a, const array& b, StreamOrDevice s = {});
785array operator%(const array& a, const array& b);
786template <typename T>
787array operator%(T a, const array& b) {
788 return remainder(array(a), b);
789}
790template <typename T>
791array operator%(const array& a, T b) {
792 return remainder(a, array(b));
793}
794
796array maximum(const array& a, const array& b, StreamOrDevice s = {});
797
799array minimum(const array& a, const array& b, StreamOrDevice s = {});
800
802array floor(const array& a, StreamOrDevice s = {});
803
805array ceil(const array& a, StreamOrDevice s = {});
806
809
811array exp(const array& a, StreamOrDevice s = {});
812
814array sin(const array& a, StreamOrDevice s = {});
815
817array cos(const array& a, StreamOrDevice s = {});
818
820array tan(const array& a, StreamOrDevice s = {});
821
824
827
830
832array arctan2(const array& a, const array& b, StreamOrDevice s = {});
833
835array sinh(const array& a, StreamOrDevice s = {});
836
838array cosh(const array& a, StreamOrDevice s = {});
839
841array tanh(const array& a, StreamOrDevice s = {});
842
845
848
851
854
857
859array log(const array& a, StreamOrDevice s = {});
860
862array log2(const array& a, StreamOrDevice s = {});
863
865array log10(const array& a, StreamOrDevice s = {});
866
868array log1p(const array& a, StreamOrDevice s = {});
869
871array logaddexp(const array& a, const array& b, StreamOrDevice s = {});
872
875
877array erf(const array& a, StreamOrDevice s = {});
878
881
883array expm1(const array& a, StreamOrDevice s = {});
884
887
889array round(const array& a, int decimals, StreamOrDevice s = {});
890inline array round(const array& a, StreamOrDevice s = {}) {
891 return round(a, 0, s);
892}
893
895array matmul(const array& a, const array& b, StreamOrDevice s = {});
896
899 const array& a,
900 const std::vector<array>& indices,
901 const std::vector<int>& axes,
902 const std::vector<int>& slice_sizes,
903 StreamOrDevice s = {});
905 const array& a,
906 const array& indices,
907 int axis,
908 const std::vector<int>& slice_sizes,
909 StreamOrDevice s = {}) {
910 return gather(a, {indices}, std::vector<int>{axis}, slice_sizes, s);
911}
912
915 const array& a,
916 const array& indices,
917 int axis,
918 StreamOrDevice s = {});
919
921array take(const array& a, const array& indices, StreamOrDevice s = {});
922
925 const array& a,
926 const array& indices,
927 int axis,
928 StreamOrDevice s = {});
929
1029 const array& a,
1030 const std::vector<array>& indices,
1031 const array& updates,
1032 const std::vector<int>& axes,
1033 StreamOrDevice s = {});
1035 const array& a,
1036 const array& indices,
1037 const array& updates,
1038 int axis,
1039 StreamOrDevice s = {}) {
1040 return scatter(a, {indices}, updates, std::vector<int>{axis}, s);
1041}
1042
1045 const array& a,
1046 const std::vector<array>& indices,
1047 const array& updates,
1048 const std::vector<int>& axes,
1049 StreamOrDevice s = {});
1051 const array& a,
1052 const array& indices,
1053 const array& updates,
1054 int axis,
1055 StreamOrDevice s = {}) {
1056 return scatter_add(a, {indices}, updates, std::vector<int>{axis}, s);
1057}
1058
1061 const array& a,
1062 const std::vector<array>& indices,
1063 const array& updates,
1064 const std::vector<int>& axes,
1065 StreamOrDevice s = {});
1067 const array& a,
1068 const array& indices,
1069 const array& updates,
1070 int axis,
1071 StreamOrDevice s = {}) {
1072 return scatter_prod(a, {indices}, updates, std::vector<int>{axis}, s);
1073}
1074
1077 const array& a,
1078 const std::vector<array>& indices,
1079 const array& updates,
1080 const std::vector<int>& axes,
1081 StreamOrDevice s = {});
1083 const array& a,
1084 const array& indices,
1085 const array& updates,
1086 int axis,
1087 StreamOrDevice s = {}) {
1088 return scatter_max(a, {indices}, updates, std::vector<int>{axis}, s);
1089}
1092 const array& a,
1093 const std::vector<array>& indices,
1094 const array& updates,
1095 const std::vector<int>& axes,
1096 StreamOrDevice s = {});
1098 const array& a,
1099 const array& indices,
1100 const array& updates,
1101 int axis,
1102 StreamOrDevice s = {}) {
1103 return scatter_min(a, {indices}, updates, std::vector<int>{axis}, s);
1104}
1105
1107array sqrt(const array& a, StreamOrDevice s = {});
1108
1111
1114 const array& a,
1115 const std::vector<int>& axes,
1116 bool precise = false,
1117 StreamOrDevice s = {});
1118
1120array softmax(const array& a, bool precise = false, StreamOrDevice s = {});
1121
1123inline array
1124softmax(const array& a, int axis, bool precise = false, StreamOrDevice s = {}) {
1125 return softmax(a, std::vector<int>{axis}, precise, s);
1126}
1127
1129array power(const array& a, const array& b, StreamOrDevice s = {});
1130
1133 const array& a,
1134 int axis,
1135 bool reverse = false,
1136 bool inclusive = true,
1137 StreamOrDevice s = {});
1138
1141 const array& a,
1142 int axis,
1143 bool reverse = false,
1144 bool inclusive = true,
1145 StreamOrDevice s = {});
1146
1149 const array& a,
1150 int axis,
1151 bool reverse = false,
1152 bool inclusive = true,
1153 StreamOrDevice s = {});
1154
1157 const array& a,
1158 int axis,
1159 bool reverse = false,
1160 bool inclusive = true,
1161 StreamOrDevice s = {});
1162
1165 array input,
1166 array weight,
1167 std::vector<int> stride = {},
1168 std::vector<int> padding_lo = {},
1169 std::vector<int> padding_hi = {},
1170 std::vector<int> kernel_dilation = {},
1171 std::vector<int> input_dilation = {},
1172 int groups = 1,
1173 bool flip = false,
1174 StreamOrDevice s = {});
1175
1178 const array& input,
1179 const array& weight,
1180 std::vector<int> stride = {},
1181 std::vector<int> padding = {},
1182 std::vector<int> kernel_dilation = {},
1183 std::vector<int> input_dilation = {},
1184 int groups = 1,
1185 bool flip = false,
1186 StreamOrDevice s = {}) {
1187 return conv_general(
1188 /* const array& input = */ input,
1189 /* const array& weight = */ weight,
1190 /* std::vector<int> stride = */ stride,
1191 /* std::vector<int> padding_lo = */ padding,
1192 /* std::vector<int> padding_hi = */ padding,
1193 /* std::vector<int> kernel_dilation = */ kernel_dilation,
1194 /* std::vector<int> input_dilation = */ input_dilation,
1195 /* int groups = */ groups,
1196 /* bool flip = */ flip,
1197 /* StreamOrDevice s = */ s);
1198}
1199
1202 const array& input,
1203 const array& weight,
1204 int stride = 1,
1205 int padding = 0,
1206 int dilation = 1,
1207 int groups = 1,
1208 StreamOrDevice s = {});
1209
1212 const array& input,
1213 const array& weight,
1214 const std::pair<int, int>& stride = {1, 1},
1215 const std::pair<int, int>& padding = {0, 0},
1216 const std::pair<int, int>& dilation = {1, 1},
1217 int groups = 1,
1218 StreamOrDevice s = {});
1219
1222 const array& input,
1223 const array& weight,
1224 const std::tuple<int, int, int>& stride = {1, 1, 1},
1225 const std::tuple<int, int, int>& padding = {0, 0, 0},
1226 const std::tuple<int, int, int>& dilation = {1, 1, 1},
1227 int groups = 1,
1228 StreamOrDevice s = {});
1229
1232 const array& x,
1233 const array& w,
1234 const array& scales,
1235 const array& biases,
1236 bool transpose = true,
1237 int group_size = 64,
1238 int bits = 4,
1239 StreamOrDevice s = {});
1240
1242std::tuple<array, array, array> quantize(
1243 const array& w,
1244 int group_size = 64,
1245 int bits = 4,
1246 StreamOrDevice s = {});
1247
1250 const array& w,
1251 const array& scales,
1252 const array& biases,
1253 int group_size = 64,
1254 int bits = 4,
1255 StreamOrDevice s = {});
1256
1259 const array& x,
1260 const array& w,
1261 const array& scales,
1262 const array& biases,
1263 std::optional<array> lhs_indices = std::nullopt,
1264 std::optional<array> rhs_indices = std::nullopt,
1265 bool transpose = true,
1266 int group_size = 64,
1267 int bits = 4,
1268 StreamOrDevice s = {});
1269
1272 const array& a,
1273 const array& b,
1274 const int axis = 2,
1275 StreamOrDevice s = {});
1276
1278 const array& a,
1279 const array& b,
1280 const std::vector<int>& axes_a,
1281 const std::vector<int>& axes_b,
1282 StreamOrDevice s = {});
1283
1285array outer(const array& a, const array& b, StreamOrDevice s = {});
1286
1288array inner(const array& a, const array& b, StreamOrDevice s = {});
1289
1292 array c,
1293 array a,
1294 array b,
1295 const float& alpha = 1.f,
1296 const float& beta = 1.f,
1297 StreamOrDevice s = {});
1298
1301 array a,
1302 array b,
1303 int block_size,
1304 std::optional<array> mask_out = std::nullopt,
1305 std::optional<array> mask_lhs = std::nullopt,
1306 std::optional<array> mask_rhs = std::nullopt,
1307 StreamOrDevice s = {});
1308
1311 array a,
1312 array b,
1313 std::optional<array> lhs_indices = std::nullopt,
1314 std::optional<array> rhs_indices = std::nullopt,
1315 StreamOrDevice s = {});
1316
1319 const array& a,
1320 int offset = 0,
1321 int axis1 = 0,
1322 int axis2 = 1,
1323 StreamOrDevice s = {});
1324
1326array diag(const array& a, int k = 0, StreamOrDevice s = {});
1327
1330 const array& a,
1331 int offset,
1332 int axis1,
1333 int axis2,
1334 Dtype dtype,
1335 StreamOrDevice s = {});
1337 const array& a,
1338 int offset,
1339 int axis1,
1340 int axis2,
1341 StreamOrDevice s = {});
1343
1349std::vector<array> depends(
1350 const std::vector<array>& inputs,
1351 const std::vector<array>& dependencies);
1352
1355std::vector<array> atleast_1d(
1356 const std::vector<array>& a,
1357 StreamOrDevice s = {});
1359std::vector<array> atleast_2d(
1360 const std::vector<array>& a,
1361 StreamOrDevice s = {});
1363std::vector<array> atleast_3d(
1364 const std::vector<array>& a,
1365 StreamOrDevice s = {});
1366
1372 const array& a,
1373 std::vector<int> axes,
1374 bool inverted,
1375 Dtype dtype = int32,
1376 StreamOrDevice s = {});
1377
1379
1381array bitwise_and(const array& a, const array& b, StreamOrDevice s = {});
1382array operator&(const array& a, const array& b);
1383
1385array bitwise_or(const array& a, const array& b, StreamOrDevice s = {});
1386array operator|(const array& a, const array& b);
1387
1389array bitwise_xor(const array& a, const array& b, StreamOrDevice s = {});
1390array operator^(const array& a, const array& b);
1391
1393array left_shift(const array& a, const array& b, StreamOrDevice s = {});
1394array operator<<(const array& a, const array& b);
1395
1397array right_shift(const array& a, const array& b, StreamOrDevice s = {});
1398array operator>>(const array& a, const array& b);
1399
1400array view(const array& a, const Dtype& dtype, StreamOrDevice s = {});
1403} // namespace mlx::core
Definition array.h:20
array scatter_max(const array &a, const std::vector< array > &indices, const array &updates, const std::vector< int > &axes, StreamOrDevice s={})
Scatter and max updates to given linear indices.
array floor_divide(const array &a, const array &b, StreamOrDevice s={})
Compute integer division.
array radians(const array &a, StreamOrDevice s={})
Convert the elements of an array from Degrees to Radians.
array arccos(const array &a, StreamOrDevice s={})
Arc Cosine of the elements of an array.
array scatter_min(const array &a, const std::vector< array > &indices, const array &updates, const std::vector< int > &axes, StreamOrDevice s={})
Scatter and min updates to given linear indices.
array less_equal(const array &a, const array &b, StreamOrDevice s={})
Returns bool array with (a <= b) element-wise.
array cumprod(const array &a, int axis, bool reverse=false, bool inclusive=true, StreamOrDevice s={})
Cumulative product of an array.
array astype(array a, Dtype dtype, StreamOrDevice s={})
Convert an array to the given data type.
array rsqrt(const array &a, StreamOrDevice s={})
Square root and reciprocal the elements of an array.
array diag(const array &a, int k=0, StreamOrDevice s={})
Extract diagonal from a 2d array or create a diagonal matrix.
array square(const array &a, StreamOrDevice s={})
Square the elements of an array.
array ceil(const array &a, StreamOrDevice s={})
Ceil the element of an array.
array log2(const array &a, StreamOrDevice s={})
Log base 2 of the elements of an array.
array clip(const array &a, const std::optional< array > &a_min=std::nullopt, const std::optional< array > &a_max=std::nullopt, StreamOrDevice s={})
Clip (limit) the values in an array.
array isnan(const array &a, StreamOrDevice s={})
array isneginf(const array &a, StreamOrDevice s={})
array subtract(const array &a, const array &b, StreamOrDevice s={})
Subtract two arrays.
array cummin(const array &a, int axis, bool reverse=false, bool inclusive=true, StreamOrDevice s={})
Cumulative min of an array.
array log10(const array &a, StreamOrDevice s={})
Log base 10 of the elements of an array.
array log1p(const array &a, StreamOrDevice s={})
Natural logarithm of one plus elements in the array: log(1 + a).
array sign(const array &a, StreamOrDevice s={})
The sign of the elements in an array.
array cosh(const array &a, StreamOrDevice s={})
Hyperbolic Cosine of the elements of an array.
array conv_general(array input, array weight, std::vector< int > stride={}, std::vector< int > padding_lo={}, std::vector< int > padding_hi={}, std::vector< int > kernel_dilation={}, std::vector< int > input_dilation={}, int groups=1, bool flip=false, StreamOrDevice s={})
General convolution with a filter.
array logical_or(const array &a, const array &b, StreamOrDevice s={})
Logical or of two arrays.
array moveaxis(const array &a, int source, int destination, StreamOrDevice s={})
Move an axis of an array.
array operator*(const array &a, const array &b)
array operator+(const array &a, const array &b)
array operator||(const array &a, const array &b)
array not_equal(const array &a, const array &b, StreamOrDevice s={})
Returns the bool array with (a != b) element-wise.
array erf(const array &a, StreamOrDevice s={})
Computes the error function of the elements of an array.
array sqrt(const array &a, StreamOrDevice s={})
Square root the elements of an array.
array std(const array &a, bool keepdims, int ddof=0, StreamOrDevice s={})
Computes the standard deviation of the elements of an array.
array add(const array &a, const array &b, StreamOrDevice s={})
Add two arrays.
array round(const array &a, int decimals, StreamOrDevice s={})
Round a floating point number.
array conv1d(const array &input, const array &weight, int stride=1, int padding=0, int dilation=1, int groups=1, StreamOrDevice s={})
1D convolution with a filter
array bitwise_xor(const array &a, const array &b, StreamOrDevice s={})
Bitwise exclusive or.
array equal(const array &a, const array &b, StreamOrDevice s={})
Returns the bool array with (a == b) element-wise.
array zeros(const std::vector< int > &shape, Dtype dtype, StreamOrDevice s={})
Fill an array of the given shape with zeros.
array view(const array &a, const Dtype &dtype, StreamOrDevice s={})
array gather_qmm(const array &x, const array &w, const array &scales, const array &biases, std::optional< array > lhs_indices=std::nullopt, std::optional< array > rhs_indices=std::nullopt, bool transpose=true, int group_size=64, int bits=4, StreamOrDevice s={})
Compute matrix products with matrix-level gather.
array stop_gradient(const array &a, StreamOrDevice s={})
Stop the flow of gradients.
array scatter_prod(const array &a, const std::vector< array > &indices, const array &updates, const std::vector< int > &axes, StreamOrDevice s={})
Scatter and prod updates to given indices.
array slice_update(const array &src, const array &update, std::vector< int > start, std::vector< int > stop, std::vector< int > strides, StreamOrDevice s={})
Update a slice from the source array.
array cos(const array &a, StreamOrDevice s={})
Cosine of the elements of an array.
array operator>=(const array &a, const array &b)
Definition ops.h:337
array degrees(const array &a, StreamOrDevice s={})
Convert the elements of an array from Radians to Degrees.
array all(const array &a, bool keepdims, StreamOrDevice s={})
True if all elements in the array are true (or non-zero).
array tan(const array &a, StreamOrDevice s={})
Tangent of the elements of an array.
array eye(int n, int m, int k, Dtype dtype, StreamOrDevice s={})
Fill an array of the given shape (n,m) with ones in the specified diagonal k, and zeros everywhere el...
array identity(int n, Dtype dtype, StreamOrDevice s={})
Create a square matrix of shape (n,n) of zeros, and ones in the major diagonal.
array operator>>(const array &a, const array &b)
array minimum(const array &a, const array &b, StreamOrDevice s={})
Element-wise minimum between two arrays.
array prod(const array &a, bool keepdims, StreamOrDevice s={})
The product of all elements of the array.
array atleast_3d(const array &a, StreamOrDevice s={})
array operator<=(const array &a, const array &b)
Definition ops.h:365
array reciprocal(const array &a, StreamOrDevice s={})
The reciprocal (1/x) of the elements in an array.
array tri(int n, int m, int k, Dtype type, StreamOrDevice s={})
array pad(const array &a, const std::vector< int > &axes, const std::vector< int > &low_pad_size, const std::vector< int > &high_pad_size, const array &pad_value=array(0), StreamOrDevice s={})
Pad an array with a constant value.
array flatten(const array &a, int start_axis, int end_axis=-1, StreamOrDevice s={})
Flatten the dimensions in the range [start_axis, end_axis] .
array isclose(const array &a, const array &b, double rtol=1e-5, double atol=1e-8, bool equal_nan=false, StreamOrDevice s={})
Returns a boolean array where two arrays are element-wise equal within the specified tolerance.
array operator|(const array &a, const array &b)
array topk(const array &a, int k, StreamOrDevice s={})
Returns topk elements of the flattened array.
array expm1(const array &a, StreamOrDevice s={})
Computes the expm1 function of the elements of an array.
array ones(const std::vector< int > &shape, Dtype dtype, StreamOrDevice s={})
Fill an array of the given shape with ones.
array abs(const array &a, StreamOrDevice s={})
Absolute value of elements in an array.
std::vector< array > meshgrid(const std::vector< array > &arrays, bool sparse=false, std::string indexing="xy", StreamOrDevice s={})
A vector of coordinate arrays from coordinate vectors.
array conjugate(const array &a, StreamOrDevice s={})
array tanh(const array &a, StreamOrDevice s={})
Hyperbolic Tangent of the elements of an array.
array quantized_matmul(const array &x, const array &w, const array &scales, const array &biases, bool transpose=true, int group_size=64, int bits=4, StreamOrDevice s={})
Quantized matmul multiplies x with a quantized matrix w.
array inner(const array &a, const array &b, StreamOrDevice s={})
Compute the inner product of two vectors.
array block_masked_mm(array a, array b, int block_size, std::optional< array > mask_out=std::nullopt, std::optional< array > mask_lhs=std::nullopt, std::optional< array > mask_rhs=std::nullopt, StreamOrDevice s={})
Compute matrix product with block masking.
array arctan2(const array &a, const array &b, StreamOrDevice s={})
Inverse tangent of the ratio of two arrays.
array number_of_elements(const array &a, std::vector< int > axes, bool inverted, Dtype dtype=int32, StreamOrDevice s={})
Extract the number of elements along some axes as a scalar array.
array conv3d(const array &input, const array &weight, const std::tuple< int, int, int > &stride={1, 1, 1}, const std::tuple< int, int, int > &padding={0, 0, 0}, const std::tuple< int, int, int > &dilation={1, 1, 1}, int groups=1, StreamOrDevice s={})
3D convolution with a filter
array log(const array &a, StreamOrDevice s={})
Natural logarithm of the elements of an array.
array sigmoid(const array &a, StreamOrDevice s={})
Element-wise logistic sigmoid of the array: 1 / (1 + exp(-x).
array squeeze(const array &a, const std::vector< int > &axes, StreamOrDevice s={})
Remove singleton dimensions at the given axes.
array greater_equal(const array &a, const array &b, StreamOrDevice s={})
Returns bool array with (a >= b) element-wise.
array expand_dims(const array &a, const std::vector< int > &axes, StreamOrDevice s={})
Add a singleton dimension at the given axes.
array conv2d(const array &input, const array &weight, const std::pair< int, int > &stride={1, 1}, const std::pair< int, int > &padding={0, 0}, const std::pair< int, int > &dilation={1, 1}, int groups=1, StreamOrDevice s={})
2D convolution with a filter
array operator>(const array &a, const array &b)
Definition ops.h:323
array bitwise_and(const array &a, const array &b, StreamOrDevice s={})
Bitwise and.
std::vector< array > split(const array &a, int num_splits, int axis, StreamOrDevice s={})
Split an array into sub-arrays along a given axis.
array matmul(const array &a, const array &b, StreamOrDevice s={})
Matrix-matrix multiplication.
array logical_and(const array &a, const array &b, StreamOrDevice s={})
Logical and of two arrays.
array erfinv(const array &a, StreamOrDevice s={})
Computes the inverse error function of the elements of an array.
array divide(const array &a, const array &b, StreamOrDevice s={})
Divide two arrays.
array power(const array &a, const array &b, StreamOrDevice s={})
Raise elements of a to the power of b element-wise.
array maximum(const array &a, const array &b, StreamOrDevice s={})
Element-wise maximum between two arrays.
array reshape(const array &a, std::vector< int > shape, StreamOrDevice s={})
Reshape an array to the given shape.
array argmin(const array &a, bool keepdims, StreamOrDevice s={})
Returns the index of the minimum value in the array.
array var(const array &a, bool keepdims, int ddof=0, StreamOrDevice s={})
Computes the variance of the elements of an array.
array full(std::vector< int > shape, array vals, Dtype dtype, StreamOrDevice s={})
Fill an array of the given shape with the given value(s).
array softmax(const array &a, const std::vector< int > &axes, bool precise=false, StreamOrDevice s={})
Softmax of an array.
array sort(const array &a, StreamOrDevice s={})
Returns a sorted copy of the flattened array.
array max(const array &a, bool keepdims, StreamOrDevice s={})
The maximum of all elements of the array.
array addmm(array c, array a, array b, const float &alpha=1.f, const float &beta=1.f, StreamOrDevice s={})
Compute D = beta * C + alpha * (A @ B)
array tril(array x, int k=0, StreamOrDevice s={})
array any(const array &a, bool keepdims, StreamOrDevice s={})
True if any elements in the array are true (or non-zero).
array outer(const array &a, const array &b, StreamOrDevice s={})
Compute the outer product of two vectors.
array arcsin(const array &a, StreamOrDevice s={})
Arc Sine of the elements of an array.
array left_shift(const array &a, const array &b, StreamOrDevice s={})
Shift bits to the left.
array where(const array &condition, const array &x, const array &y, StreamOrDevice s={})
Select from x or y depending on condition.
array exp(const array &a, StreamOrDevice s={})
Exponential of the elements of an array.
array bitwise_or(const array &a, const array &b, StreamOrDevice s={})
Bitwise inclusive or.
array gather_mm(array a, array b, std::optional< array > lhs_indices=std::nullopt, std::optional< array > rhs_indices=std::nullopt, StreamOrDevice s={})
Compute matrix product with matrix-level gather.
array floor(const array &a, StreamOrDevice s={})
Floor the element of an array.
array as_strided(array a, std::vector< int > shape, std::vector< size_t > strides, size_t offset, StreamOrDevice s={})
Create a view of an array with the given shape and strides.
array argsort(const array &a, StreamOrDevice s={})
Returns indices that sort the flattened array.
array array_equal(const array &a, const array &b, bool equal_nan, StreamOrDevice s={})
True if two arrays have the same shape and elements.
array isinf(const array &a, StreamOrDevice s={})
array less(const array &a, const array &b, StreamOrDevice s={})
Returns bool array with (a < b) element-wise.
array diagonal(const array &a, int offset=0, int axis1=0, int axis2=1, StreamOrDevice s={})
Extract a diagonal or construct a diagonal array.
array ones_like(const array &a, StreamOrDevice s={})
array negative(const array &a, StreamOrDevice s={})
Negate an array.
array linspace(double start, double stop, int num=50, Dtype dtype=float32, StreamOrDevice s={})
A 1D array of num evenly spaced numbers in the range [start, stop]
array remainder(const array &a, const array &b, StreamOrDevice s={})
Compute the element-wise remainder of division.
array arctan(const array &a, StreamOrDevice s={})
Arc Tangent of the elements of an array.
std::vector< array > divmod(const array &a, const array &b, StreamOrDevice s={})
Compute the element-wise quotient and remainder.
array triu(array x, int k=0, StreamOrDevice s={})
array arccosh(const array &a, StreamOrDevice s={})
Inverse Hyperbolic Cosine of the elements of an array.
array tile(const array &arr, std::vector< int > reps, StreamOrDevice s={})
array min(const array &a, bool keepdims, StreamOrDevice s={})
The minimum of all elements of the array.
array operator%(const array &a, const array &b)
std::tuple< array, array, array > quantize(const array &w, int group_size=64, int bits=4, StreamOrDevice s={})
Quantize a matrix along its last axis.
array arctanh(const array &a, StreamOrDevice s={})
Inverse Hyperbolic Tangent of the elements of an array.
array repeat(const array &arr, int repeats, int axis, StreamOrDevice s={})
Repeat an array along an axis.
array gather(const array &a, const std::vector< array > &indices, const std::vector< int > &axes, const std::vector< int > &slice_sizes, StreamOrDevice s={})
Gather array entries given indices and slices.
std::vector< array > broadcast_arrays(const std::vector< array > &inputs, StreamOrDevice s={})
Broadcast a vector of arrays against one another.
array atleast_1d(const array &a, StreamOrDevice s={})
convert an array to an atleast ndim array
array swapaxes(const array &a, int axis1, int axis2, StreamOrDevice s={})
Swap two axes of an array.
array logical_not(const array &a, StreamOrDevice s={})
Logical not of an array.
array concatenate(const std::vector< array > &arrays, int axis, StreamOrDevice s={})
Concatenate arrays along a given axis.
array trace(const array &a, int offset, int axis1, int axis2, Dtype dtype, StreamOrDevice s={})
Return the sum along a specified diagonal in the given array.
array dequantize(const array &w, const array &scales, const array &biases, int group_size=64, int bits=4, StreamOrDevice s={})
Dequantize a matrix produced by quantize()
array transpose(const array &a, std::vector< int > axes, StreamOrDevice s={})
Permutes the dimensions according to the given axes.
array partition(const array &a, int kth, StreamOrDevice s={})
Returns a partitioned copy of the flattened array such that the smaller kth elements are first.
array take(const array &a, const array &indices, int axis, StreamOrDevice s={})
Take array slices at the given indices of the specified axis.
array operator^(const array &a, const array &b)
std::vector< array > depends(const std::vector< array > &inputs, const std::vector< array > &dependencies)
Implements the identity function but allows injecting dependencies to other arrays.
array arcsinh(const array &a, StreamOrDevice s={})
Inverse Hyperbolic Sine of the elements of an array.
array scatter_add(const array &a, const std::vector< array > &indices, const array &updates, const std::vector< int > &axes, StreamOrDevice s={})
Scatter and add updates to given indices.
array logsumexp(const array &a, bool keepdims, StreamOrDevice s={})
The logsumexp of all elements of the array.
array broadcast_to(const array &a, const std::vector< int > &shape, StreamOrDevice s={})
Broadcast an array to a given shape.
array scatter(const array &a, const std::vector< array > &indices, const array &updates, const std::vector< int > &axes, StreamOrDevice s={})
Scatter updates to the given indices.
array operator<<(const array &a, const array &b)
array slice(const array &a, std::vector< int > start, std::vector< int > stop, std::vector< int > strides, StreamOrDevice s={})
Slice an array.
array isposinf(const array &a, StreamOrDevice s={})
array cumsum(const array &a, int axis, bool reverse=false, bool inclusive=true, StreamOrDevice s={})
Cumulative sum of an array.
array operator-(const array &a)
array mean(const array &a, bool keepdims, StreamOrDevice s={})
Computes the mean of the elements of an array.
array sum(const array &a, bool keepdims, StreamOrDevice s={})
Sums the elements of an array.
array take_along_axis(const array &a, const array &indices, int axis, StreamOrDevice s={})
Take array entries given indices along the axis.
array argmax(const array &a, bool keepdims, StreamOrDevice s={})
Returns the index of the maximum value in the array.
array sin(const array &a, StreamOrDevice s={})
Sine of the elements of an array.
array operator&&(const array &a, const array &b)
array cummax(const array &a, int axis, bool reverse=false, bool inclusive=true, StreamOrDevice s={})
Cumulative max of an array.
array operator<(const array &a, const array &b)
Definition ops.h:351
array atleast_2d(const array &a, StreamOrDevice s={})
array operator/(const array &a, const array &b)
array allclose(const array &a, const array &b, double rtol=1e-5, double atol=1e-8, bool equal_nan=false, StreamOrDevice s={})
True if the two arrays are equal within the specified tolerance.
array operator&(const array &a, const array &b)
array argpartition(const array &a, int kth, StreamOrDevice s={})
Returns indices that partition the flattened array such that the smaller kth elements are first.
array greater(const array &a, const array &b, StreamOrDevice s={})
Returns bool array with (a > b) element-wise.
array sinh(const array &a, StreamOrDevice s={})
Hyperbolic Sine of the elements of an array.
array multiply(const array &a, const array &b, StreamOrDevice s={})
Multiply two arrays.
array tensordot(const array &a, const array &b, const int axis=2, StreamOrDevice s={})
Returns a contraction of a and b over multiple dimensions.
array stack(const std::vector< array > &arrays, int axis, StreamOrDevice s={})
Stack arrays along a new axis.
array logaddexp(const array &a, const array &b, StreamOrDevice s={})
Log-add-exp of one elements in the array: log(exp(a) + exp(b)).
array right_shift(const array &a, const array &b, StreamOrDevice s={})
Shift bits to the right.
array zeros_like(const array &a, StreamOrDevice s={})
Definition allocator.h:7
void arange(const std::vector< array > &inputs, array &out, double start, double step)
Definition arange.h:24
Stream to_stream(StreamOrDevice s)
void copy(const array &src, array &dst, CopyType ctype)
constexpr Dtype int32
Definition dtype.h:69
constexpr Dtype float32
Definition dtype.h:73
bool operator==(const Device &lhs, const Device &rhs)
bool operator!=(const Device &lhs, const Device &rhs)
std::variant< std::monostate, Stream, Device > StreamOrDevice
Definition utils.h:14
Definition dtype.h:15