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
//! Generated MLX-C wrappers for the linear algebra family.
//!
//! Each wrapper maps one MLX-C operation onto the runtime GPU stream
//! with the crate's owned-handle and captured-error semantics. The
//! coverage contract keeps this family in lockstep with the pinned
//! upstream headers.
use crate::bindings::support::{c_string_argument, optional_i32_slice};
use crate::raw;
use crate::{MlxArray, MlxArrayVector, MlxBindingsContext, MlxCError};
impl MlxBindingsContext {
/// Applies MLX-C `mlx_linalg_cholesky` on the runtime GPU stream.
pub fn linalg_cholesky(&self, a: &MlxArray, upper: bool) -> Result<MlxArray, MlxCError> {
self.output_array("apply MLX linalg_cholesky", |output, stream| {
// SAFETY: Inputs and stream are live and output is uniquely writable.
unsafe { raw::mlx_linalg_cholesky(output, a.raw(), upper, stream) }
})
}
/// Applies MLX-C `mlx_linalg_cholesky_inv` on the runtime GPU stream.
pub fn linalg_cholesky_inv(&self, a: &MlxArray, upper: bool) -> Result<MlxArray, MlxCError> {
self.output_array("apply MLX linalg_cholesky_inv", |output, stream| {
// SAFETY: Inputs and stream are live and output is uniquely writable.
unsafe { raw::mlx_linalg_cholesky_inv(output, a.raw(), upper, stream) }
})
}
/// Applies MLX-C `mlx_linalg_cross` on the runtime GPU stream.
pub fn linalg_cross(
&self,
a: &MlxArray,
b: &MlxArray,
axis: i32,
) -> Result<MlxArray, MlxCError> {
self.output_array("apply MLX linalg_cross", |output, stream| {
// SAFETY: Inputs and stream are live and output is uniquely writable.
unsafe { raw::mlx_linalg_cross(output, a.raw(), b.raw(), axis, stream) }
})
}
/// Applies MLX-C `mlx_linalg_det` on the runtime GPU stream.
pub fn linalg_det(&self, a: &MlxArray) -> Result<MlxArray, MlxCError> {
self.output_array("apply MLX linalg_det", |output, stream| {
// SAFETY: Inputs and stream are live and output is uniquely writable.
unsafe { raw::mlx_linalg_det(output, a.raw(), stream) }
})
}
/// Applies MLX-C `mlx_linalg_eig`, producing both outputs on the runtime GPU stream.
pub fn linalg_eig(&self, a: &MlxArray) -> Result<(MlxArray, MlxArray), MlxCError> {
self.output_array_pair("apply MLX linalg_eig", |output, second_output, stream| {
// SAFETY: Inputs and stream are live and output is uniquely writable.
unsafe { raw::mlx_linalg_eig(output, second_output, a.raw(), stream) }
})
}
/// Applies MLX-C `mlx_linalg_eigh`, producing both outputs on the runtime GPU stream.
pub fn linalg_eigh(
&self,
a: &MlxArray,
upper_lower: &str,
) -> Result<(MlxArray, MlxArray), MlxCError> {
let upper_lower_argument = c_string_argument(upper_lower, "linalg_eigh")?;
self.output_array_pair("apply MLX linalg_eigh", |output, second_output, stream| {
// SAFETY: Inputs and stream are live and output is uniquely writable.
unsafe {
raw::mlx_linalg_eigh(
output,
second_output,
a.raw(),
upper_lower_argument.as_ptr(),
stream,
)
}
})
}
/// Applies MLX-C `mlx_linalg_eigvals` on the runtime GPU stream.
pub fn linalg_eigvals(&self, a: &MlxArray) -> Result<MlxArray, MlxCError> {
self.output_array("apply MLX linalg_eigvals", |output, stream| {
// SAFETY: Inputs and stream are live and output is uniquely writable.
unsafe { raw::mlx_linalg_eigvals(output, a.raw(), stream) }
})
}
/// Applies MLX-C `mlx_linalg_eigvalsh` on the runtime GPU stream.
pub fn linalg_eigvalsh(&self, a: &MlxArray, upper_lower: &str) -> Result<MlxArray, MlxCError> {
let upper_lower_argument = c_string_argument(upper_lower, "linalg_eigvalsh")?;
self.output_array("apply MLX linalg_eigvalsh", |output, stream| {
// SAFETY: Inputs and stream are live and output is uniquely writable.
unsafe {
raw::mlx_linalg_eigvalsh(output, a.raw(), upper_lower_argument.as_ptr(), stream)
}
})
}
/// Applies MLX-C `mlx_linalg_inv` on the runtime GPU stream.
pub fn linalg_inv(&self, a: &MlxArray) -> Result<MlxArray, MlxCError> {
self.output_array("apply MLX linalg_inv", |output, stream| {
// SAFETY: Inputs and stream are live and output is uniquely writable.
unsafe { raw::mlx_linalg_inv(output, a.raw(), stream) }
})
}
/// Builds the MLX-C `mlx_linalg_lu` outputs on the runtime GPU stream.
pub fn linalg_lu(&self, a: &MlxArray) -> Result<MlxArrayVector, MlxCError> {
self.output_vector_array("apply MLX linalg_lu", |output, stream| {
// SAFETY: Inputs and stream are live and output is uniquely writable.
unsafe { raw::mlx_linalg_lu(output, a.raw(), stream) }
})
}
/// Applies MLX-C `mlx_linalg_lu_factor`, producing both outputs on the runtime GPU stream.
pub fn linalg_lu_factor(&self, a: &MlxArray) -> Result<(MlxArray, MlxArray), MlxCError> {
self.output_array_pair(
"apply MLX linalg_lu_factor",
|output, second_output, stream| {
// SAFETY: Inputs and stream are live and output is uniquely writable.
unsafe { raw::mlx_linalg_lu_factor(output, second_output, a.raw(), stream) }
},
)
}
/// Applies MLX-C `mlx_linalg_norm` on the runtime GPU stream.
pub fn linalg_norm(
&self,
a: &MlxArray,
ord: f64,
axis: &[i32],
keepdims: bool,
) -> Result<MlxArray, MlxCError> {
self.output_array("apply MLX linalg_norm", |output, stream| {
// SAFETY: Inputs and stream are live and output is uniquely writable.
unsafe {
raw::mlx_linalg_norm(
output,
a.raw(),
ord,
optional_i32_slice(axis),
axis.len(),
keepdims,
stream,
)
}
})
}
/// Applies MLX-C `mlx_linalg_norm_l2` on the runtime GPU stream.
pub fn linalg_norm_l2(
&self,
a: &MlxArray,
axis: &[i32],
keepdims: bool,
) -> Result<MlxArray, MlxCError> {
self.output_array("apply MLX linalg_norm_l2", |output, stream| {
// SAFETY: Inputs and stream are live and output is uniquely writable.
unsafe {
raw::mlx_linalg_norm_l2(
output,
a.raw(),
optional_i32_slice(axis),
axis.len(),
keepdims,
stream,
)
}
})
}
/// Applies MLX-C `mlx_linalg_norm_matrix` on the runtime GPU stream.
pub fn linalg_norm_matrix(
&self,
a: &MlxArray,
ord: &str,
axis: &[i32],
keepdims: bool,
) -> Result<MlxArray, MlxCError> {
let ord_argument = c_string_argument(ord, "linalg_norm_matrix")?;
self.output_array("apply MLX linalg_norm_matrix", |output, stream| {
// SAFETY: Inputs and stream are live and output is uniquely writable.
unsafe {
raw::mlx_linalg_norm_matrix(
output,
a.raw(),
ord_argument.as_ptr(),
optional_i32_slice(axis),
axis.len(),
keepdims,
stream,
)
}
})
}
/// Applies MLX-C `mlx_linalg_pinv` on the runtime GPU stream.
pub fn linalg_pinv(&self, a: &MlxArray) -> Result<MlxArray, MlxCError> {
self.output_array("apply MLX linalg_pinv", |output, stream| {
// SAFETY: Inputs and stream are live and output is uniquely writable.
unsafe { raw::mlx_linalg_pinv(output, a.raw(), stream) }
})
}
/// Applies MLX-C `mlx_linalg_qr`, producing both outputs on the runtime GPU stream.
pub fn linalg_qr(&self, a: &MlxArray) -> Result<(MlxArray, MlxArray), MlxCError> {
self.output_array_pair("apply MLX linalg_qr", |output, second_output, stream| {
// SAFETY: Inputs and stream are live and output is uniquely writable.
unsafe { raw::mlx_linalg_qr(output, second_output, a.raw(), stream) }
})
}
/// Applies MLX-C `mlx_linalg_slogdet`, producing both outputs on the runtime GPU stream.
pub fn linalg_slogdet(&self, a: &MlxArray) -> Result<(MlxArray, MlxArray), MlxCError> {
self.output_array_pair(
"apply MLX linalg_slogdet",
|output, second_output, stream| {
// SAFETY: Inputs and stream are live and output is uniquely writable.
unsafe { raw::mlx_linalg_slogdet(output, second_output, a.raw(), stream) }
},
)
}
/// Applies MLX-C `mlx_linalg_solve` on the runtime GPU stream.
pub fn linalg_solve(&self, a: &MlxArray, b: &MlxArray) -> Result<MlxArray, MlxCError> {
self.output_array("apply MLX linalg_solve", |output, stream| {
// SAFETY: Inputs and stream are live and output is uniquely writable.
unsafe { raw::mlx_linalg_solve(output, a.raw(), b.raw(), stream) }
})
}
/// Applies MLX-C `mlx_linalg_solve_triangular` on the runtime GPU stream.
pub fn linalg_solve_triangular(
&self,
a: &MlxArray,
b: &MlxArray,
upper: bool,
) -> Result<MlxArray, MlxCError> {
self.output_array("apply MLX linalg_solve_triangular", |output, stream| {
// SAFETY: Inputs and stream are live and output is uniquely writable.
unsafe { raw::mlx_linalg_solve_triangular(output, a.raw(), b.raw(), upper, stream) }
})
}
/// Builds the MLX-C `mlx_linalg_svd` outputs on the runtime GPU stream.
pub fn linalg_svd(&self, a: &MlxArray, compute_uv: bool) -> Result<MlxArrayVector, MlxCError> {
self.output_vector_array("apply MLX linalg_svd", |output, stream| {
// SAFETY: Inputs and stream are live and output is uniquely writable.
unsafe { raw::mlx_linalg_svd(output, a.raw(), compute_uv, stream) }
})
}
/// Applies MLX-C `mlx_linalg_tri_inv` on the runtime GPU stream.
pub fn linalg_tri_inv(&self, a: &MlxArray, upper: bool) -> Result<MlxArray, MlxCError> {
self.output_array("apply MLX linalg_tri_inv", |output, stream| {
// SAFETY: Inputs and stream are live and output is uniquely writable.
unsafe { raw::mlx_linalg_tri_inv(output, a.raw(), upper, stream) }
})
}
}