mlx-c-rust 0.32.4-alpha.3

A complete, low-level, policy-free Rust bridge to the MLX-C API of Apple's MLX for Apple silicon, tracking MLX releases. Version = the verified MLX release (0.32.4; carries MLX-C 0.7 forward).
Documentation
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
//! Generated MLX-C wrappers for the array shape 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, MlxDtype};
impl MlxBindingsContext {
    /// Applies MLX-C `mlx_atleast_1d` on the runtime GPU stream.
    pub fn atleast_1d(&self, a: &MlxArray) -> Result<MlxArray, MlxCError> {
        self.output_array("apply MLX atleast_1d", |output, stream| {
            // SAFETY: Inputs and stream are live and output is uniquely writable.
            unsafe { raw::mlx_atleast_1d(output, a.raw(), stream) }
        })
    }

    /// Applies MLX-C `mlx_atleast_2d` on the runtime GPU stream.
    pub fn atleast_2d(&self, a: &MlxArray) -> Result<MlxArray, MlxCError> {
        self.output_array("apply MLX atleast_2d", |output, stream| {
            // SAFETY: Inputs and stream are live and output is uniquely writable.
            unsafe { raw::mlx_atleast_2d(output, a.raw(), stream) }
        })
    }

    /// Applies MLX-C `mlx_atleast_3d` on the runtime GPU stream.
    pub fn atleast_3d(&self, a: &MlxArray) -> Result<MlxArray, MlxCError> {
        self.output_array("apply MLX atleast_3d", |output, stream| {
            // SAFETY: Inputs and stream are live and output is uniquely writable.
            unsafe { raw::mlx_atleast_3d(output, a.raw(), stream) }
        })
    }

    /// Builds the MLX-C `mlx_broadcast_arrays` outputs on the runtime GPU stream.
    pub fn broadcast_arrays(&self, inputs: &MlxArrayVector) -> Result<MlxArrayVector, MlxCError> {
        self.output_vector_array("apply MLX broadcast_arrays", |output, stream| {
            // SAFETY: Inputs and stream are live and output is uniquely writable.
            unsafe { raw::mlx_broadcast_arrays(output, inputs.raw(), stream) }
        })
    }

    /// Applies MLX-C `mlx_copy` on the runtime GPU stream.
    pub fn copy(&self, a: &MlxArray) -> Result<MlxArray, MlxCError> {
        self.output_array("apply MLX copy", |output, stream| {
            // SAFETY: Inputs and stream are live and output is uniquely writable.
            unsafe { raw::mlx_copy(output, a.raw(), stream) }
        })
    }

    /// Synchronizes the evaluation of `dependencies` after `inputs` through
    /// MLX-C `mlx_depends`.
    pub fn depends(
        &self,
        inputs: &MlxArrayVector,
        dependencies: &MlxArrayVector,
    ) -> Result<MlxArrayVector, MlxCError> {
        let mut output = MlxArrayVector::empty("apply MLX depends")?;
        // SAFETY: Inputs are live and output is uniquely writable.
        let status =
            unsafe { raw::mlx_depends(output.raw_mut(), inputs.raw(), dependencies.raw()) };
        crate::error::check_status(status, "apply MLX depends")?;
        Ok(output)
    }

    /// Applies MLX-C `mlx_diag` on the runtime GPU stream.
    pub fn diag(&self, a: &MlxArray, k: i32) -> Result<MlxArray, MlxCError> {
        self.output_array("apply MLX diag", |output, stream| {
            // SAFETY: Inputs and stream are live and output is uniquely writable.
            unsafe { raw::mlx_diag(output, a.raw(), k, stream) }
        })
    }

    /// Applies MLX-C `mlx_diagonal` on the runtime GPU stream.
    pub fn diagonal(
        &self,
        a: &MlxArray,
        offset: i32,
        axis1: i32,
        axis2: i32,
    ) -> Result<MlxArray, MlxCError> {
        self.output_array("apply MLX diagonal", |output, stream| {
            // SAFETY: Inputs and stream are live and output is uniquely writable.
            unsafe { raw::mlx_diagonal(output, a.raw(), offset, axis1, axis2, stream) }
        })
    }

    /// Applies MLX-C `mlx_expand_dims_axes` on the runtime GPU stream.
    pub fn expand_dims_axes(&self, a: &MlxArray, axes: &[i32]) -> Result<MlxArray, MlxCError> {
        self.output_array("apply MLX expand_dims_axes", |output, stream| {
            // SAFETY: Inputs and stream are live and output is uniquely writable.
            unsafe {
                raw::mlx_expand_dims_axes(
                    output,
                    a.raw(),
                    optional_i32_slice(axes),
                    axes.len(),
                    stream,
                )
            }
        })
    }

    /// Applies MLX-C `mlx_flatten` on the runtime GPU stream.
    pub fn flatten(
        &self,
        a: &MlxArray,
        start_axis: i32,
        end_axis: i32,
    ) -> Result<MlxArray, MlxCError> {
        self.output_array("apply MLX flatten", |output, stream| {
            // SAFETY: Inputs and stream are live and output is uniquely writable.
            unsafe { raw::mlx_flatten(output, a.raw(), start_axis, end_axis, stream) }
        })
    }

    /// Applies MLX-C `mlx_flip` on the runtime GPU stream.
    pub fn flip(&self, a: &MlxArray) -> Result<MlxArray, MlxCError> {
        self.output_array("apply MLX flip", |output, stream| {
            // SAFETY: Inputs and stream are live and output is uniquely writable.
            unsafe { raw::mlx_flip(output, a.raw(), stream) }
        })
    }

    /// Applies MLX-C `mlx_flip_axes` on the runtime GPU stream.
    pub fn flip_axes(&self, a: &MlxArray, axes: &[i32]) -> Result<MlxArray, MlxCError> {
        self.output_array("apply MLX flip_axes", |output, stream| {
            // SAFETY: Inputs and stream are live and output is uniquely writable.
            unsafe {
                raw::mlx_flip_axes(
                    output,
                    a.raw(),
                    optional_i32_slice(axes),
                    axes.len(),
                    stream,
                )
            }
        })
    }

    /// Applies MLX-C `mlx_flip_axis` on the runtime GPU stream.
    pub fn flip_axis(&self, a: &MlxArray, axis: i32) -> Result<MlxArray, MlxCError> {
        self.output_array("apply MLX flip_axis", |output, stream| {
            // SAFETY: Inputs and stream are live and output is uniquely writable.
            unsafe { raw::mlx_flip_axis(output, a.raw(), axis, stream) }
        })
    }

    /// Applies MLX-C `mlx_identity` on the runtime GPU stream.
    pub fn identity(&self, n: i32, dtype: MlxDtype) -> Result<MlxArray, MlxCError> {
        self.output_array("apply MLX identity", |output, stream| {
            // SAFETY: Inputs and stream are live and output is uniquely writable.
            unsafe { raw::mlx_identity(output, n, dtype.to_raw(), stream) }
        })
    }

    /// Builds the MLX-C `mlx_meshgrid` outputs on the runtime GPU stream.
    pub fn meshgrid(
        &self,
        arrays: &MlxArrayVector,
        sparse: bool,
        indexing: &str,
    ) -> Result<MlxArrayVector, MlxCError> {
        let indexing_argument = c_string_argument(indexing, "meshgrid")?;
        self.output_vector_array("apply MLX meshgrid", |output, stream| {
            // SAFETY: Inputs and stream are live and output is uniquely writable.
            unsafe {
                raw::mlx_meshgrid(
                    output,
                    arrays.raw(),
                    sparse,
                    indexing_argument.as_ptr(),
                    stream,
                )
            }
        })
    }

    /// Applies MLX-C `mlx_moveaxis` on the runtime GPU stream.
    pub fn moveaxis(
        &self,
        a: &MlxArray,
        source: i32,
        destination: i32,
    ) -> Result<MlxArray, MlxCError> {
        self.output_array("apply MLX moveaxis", |output, stream| {
            // SAFETY: Inputs and stream are live and output is uniquely writable.
            unsafe { raw::mlx_moveaxis(output, a.raw(), source, destination, stream) }
        })
    }

    /// Applies MLX-C `mlx_repeat` on the runtime GPU stream.
    pub fn repeat(&self, arr: &MlxArray, repeats: i32) -> Result<MlxArray, MlxCError> {
        self.output_array("apply MLX repeat", |output, stream| {
            // SAFETY: Inputs and stream are live and output is uniquely writable.
            unsafe { raw::mlx_repeat(output, arr.raw(), repeats, stream) }
        })
    }

    /// Applies MLX-C `mlx_roll` on the runtime GPU stream.
    pub fn roll(&self, a: &MlxArray, shift: &[i32]) -> Result<MlxArray, MlxCError> {
        self.output_array("apply MLX roll", |output, stream| {
            // SAFETY: Inputs and stream are live and output is uniquely writable.
            unsafe {
                raw::mlx_roll(
                    output,
                    a.raw(),
                    optional_i32_slice(shift),
                    shift.len(),
                    stream,
                )
            }
        })
    }

    /// Applies MLX-C `mlx_roll_axes` on the runtime GPU stream.
    pub fn roll_axes(
        &self,
        a: &MlxArray,
        shift: &[i32],
        axes: &[i32],
    ) -> Result<MlxArray, MlxCError> {
        self.output_array("apply MLX roll_axes", |output, stream| {
            // SAFETY: Inputs and stream are live and output is uniquely writable.
            unsafe {
                raw::mlx_roll_axes(
                    output,
                    a.raw(),
                    optional_i32_slice(shift),
                    shift.len(),
                    optional_i32_slice(axes),
                    axes.len(),
                    stream,
                )
            }
        })
    }

    /// Applies MLX-C `mlx_roll_axis` on the runtime GPU stream.
    pub fn roll_axis(&self, a: &MlxArray, shift: &[i32], axis: i32) -> Result<MlxArray, MlxCError> {
        self.output_array("apply MLX roll_axis", |output, stream| {
            // SAFETY: Inputs and stream are live and output is uniquely writable.
            unsafe {
                raw::mlx_roll_axis(
                    output,
                    a.raw(),
                    optional_i32_slice(shift),
                    shift.len(),
                    axis,
                    stream,
                )
            }
        })
    }

    /// Builds the MLX-C `mlx_split` outputs on the runtime GPU stream.
    pub fn split(
        &self,
        a: &MlxArray,
        num_splits: i32,
        axis: i32,
    ) -> Result<MlxArrayVector, MlxCError> {
        self.output_vector_array("apply MLX split", |output, stream| {
            // SAFETY: Inputs and stream are live and output is uniquely writable.
            unsafe { raw::mlx_split(output, a.raw(), num_splits, axis, stream) }
        })
    }

    /// Builds the MLX-C `mlx_split_sections` outputs on the runtime GPU stream.
    pub fn split_sections(
        &self,
        a: &MlxArray,
        indices: &[i32],
        axis: i32,
    ) -> Result<MlxArrayVector, MlxCError> {
        self.output_vector_array("apply MLX split_sections", |output, stream| {
            // SAFETY: Inputs and stream are live and output is uniquely writable.
            unsafe {
                raw::mlx_split_sections(
                    output,
                    a.raw(),
                    optional_i32_slice(indices),
                    indices.len(),
                    axis,
                    stream,
                )
            }
        })
    }

    /// Applies MLX-C `mlx_squeeze` on the runtime GPU stream.
    pub fn squeeze(&self, a: &MlxArray) -> Result<MlxArray, MlxCError> {
        self.output_array("apply MLX squeeze", |output, stream| {
            // SAFETY: Inputs and stream are live and output is uniquely writable.
            unsafe { raw::mlx_squeeze(output, a.raw(), stream) }
        })
    }

    /// Applies MLX-C `mlx_squeeze_axes` on the runtime GPU stream.
    pub fn squeeze_axes(&self, a: &MlxArray, axes: &[i32]) -> Result<MlxArray, MlxCError> {
        self.output_array("apply MLX squeeze_axes", |output, stream| {
            // SAFETY: Inputs and stream are live and output is uniquely writable.
            unsafe {
                raw::mlx_squeeze_axes(
                    output,
                    a.raw(),
                    optional_i32_slice(axes),
                    axes.len(),
                    stream,
                )
            }
        })
    }

    /// Applies MLX-C `mlx_swapaxes` on the runtime GPU stream.
    pub fn swapaxes(&self, a: &MlxArray, axis1: i32, axis2: i32) -> Result<MlxArray, MlxCError> {
        self.output_array("apply MLX swapaxes", |output, stream| {
            // SAFETY: Inputs and stream are live and output is uniquely writable.
            unsafe { raw::mlx_swapaxes(output, a.raw(), axis1, axis2, stream) }
        })
    }

    /// Applies MLX-C `mlx_tile` on the runtime GPU stream.
    pub fn tile(&self, arr: &MlxArray, reps: &[i32]) -> Result<MlxArray, MlxCError> {
        self.output_array("apply MLX tile", |output, stream| {
            // SAFETY: Inputs and stream are live and output is uniquely writable.
            unsafe {
                raw::mlx_tile(
                    output,
                    arr.raw(),
                    optional_i32_slice(reps),
                    reps.len(),
                    stream,
                )
            }
        })
    }

    /// Applies MLX-C `mlx_trace` on the runtime GPU stream.
    pub fn trace(&self, a: &MlxArray) -> Result<MlxArray, MlxCError> {
        self.output_array("apply MLX trace", |output, stream| {
            // SAFETY: Inputs and stream are live and output is uniquely writable.
            unsafe { raw::mlx_trace(output, a.raw(), stream) }
        })
    }

    /// Applies MLX-C `mlx_trace_axes` on the runtime GPU stream.
    pub fn trace_axes(
        &self,
        a: &MlxArray,
        offset: i32,
        axis1: i32,
        axis2: i32,
        dtype: MlxDtype,
    ) -> Result<MlxArray, MlxCError> {
        self.output_array("apply MLX trace_axes", |output, stream| {
            // SAFETY: Inputs and stream are live and output is uniquely writable.
            unsafe {
                raw::mlx_trace_axes(
                    output,
                    a.raw(),
                    offset,
                    axis1,
                    axis2,
                    dtype.to_raw(),
                    stream,
                )
            }
        })
    }

    /// Applies MLX-C `mlx_transpose` on the runtime GPU stream.
    pub fn transpose(&self, a: &MlxArray) -> Result<MlxArray, MlxCError> {
        self.output_array("apply MLX transpose", |output, stream| {
            // SAFETY: Inputs and stream are live and output is uniquely writable.
            unsafe { raw::mlx_transpose(output, a.raw(), stream) }
        })
    }

    /// Applies MLX-C `mlx_unflatten` on the runtime GPU stream.
    pub fn unflatten(&self, a: &MlxArray, axis: i32, shape: &[i32]) -> Result<MlxArray, MlxCError> {
        self.output_array("apply MLX unflatten", |output, stream| {
            // SAFETY: Inputs and stream are live and output is uniquely writable.
            unsafe {
                raw::mlx_unflatten(
                    output,
                    a.raw(),
                    axis,
                    optional_i32_slice(shape),
                    shape.len(),
                    stream,
                )
            }
        })
    }

    /// Builds the MLX-C `mlx_unstack` outputs on the runtime GPU stream.
    pub fn unstack(&self, a: &MlxArray) -> Result<MlxArrayVector, MlxCError> {
        self.output_vector_array("apply MLX unstack", |output, stream| {
            // SAFETY: Inputs and stream are live and output is uniquely writable.
            unsafe { raw::mlx_unstack(output, a.raw(), stream) }
        })
    }

    /// Builds the MLX-C `mlx_unstack_axis` outputs on the runtime GPU stream.
    pub fn unstack_axis(&self, a: &MlxArray, axis: i32) -> Result<MlxArrayVector, MlxCError> {
        self.output_vector_array("apply MLX unstack_axis", |output, stream| {
            // SAFETY: Inputs and stream are live and output is uniquely writable.
            unsafe { raw::mlx_unstack_axis(output, a.raw(), axis, stream) }
        })
    }

    /// Applies MLX-C `mlx_view` on the runtime GPU stream.
    pub fn view(&self, a: &MlxArray, dtype: MlxDtype) -> Result<MlxArray, MlxCError> {
        self.output_array("apply MLX view", |output, stream| {
            // SAFETY: Inputs and stream are live and output is uniquely writable.
            unsafe { raw::mlx_view(output, a.raw(), dtype.to_raw(), stream) }
        })
    }
}