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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
use crate::MlxBindingsContext;
use crate::MlxCError;
use crate::{MlxArray, MlxArrayVector, MlxDtype, raw};

impl MlxBindingsContext {
    /// Reorders array dimensions using an explicit complete permutation.
    pub fn transpose_axes(&self, input: &MlxArray, axes: &[i32]) -> Result<MlxArray, MlxCError> {
        self.output_array("transpose MLX array axes", |output, stream| {
            // SAFETY: Axes remain borrowed for this copying graph operation.
            unsafe {
                raw::mlx_transpose_axes(output, input.raw(), axes.as_ptr(), axes.len(), stream)
            }
        })
    }

    /// Reshapes an array without changing its logical element count.
    pub fn reshape(&self, input: &MlxArray, shape: &[i32]) -> Result<MlxArray, MlxCError> {
        self.output_array("reshape an MLX array", |output, stream| {
            // SAFETY: Shape remains borrowed for this copying graph operation.
            unsafe { raw::mlx_reshape(output, input.raw(), shape.as_ptr(), shape.len(), stream) }
        })
    }

    /// Selects indices along one array axis through MLX-C `mlx_take_axis`.
    ///
    /// Qwen3.5-MoE uses this for learned positions, quantized text embeddings, and the
    /// dense global-expert-ID to compact-page-slot lookup used by paged expert execution.
    /// This call records a lazy MLX operation on the runtime stream; it does not copy
    /// indices to Rust or synchronously evaluate the resulting array.
    pub fn take_axis(
        &self,
        input: &MlxArray,
        indices: &MlxArray,
        axis: i32,
    ) -> Result<MlxArray, MlxCError> {
        self.output_array("take values from an MLX array axis", |output, stream| {
            // SAFETY: Inputs and stream are live and output is uniquely writable.
            unsafe { raw::mlx_take_axis(output, input.raw(), indices.raw(), axis, stream) }
        })
    }

    /// Selects per-position indices along one array axis.
    pub fn take_along_axis(
        &self,
        input: &MlxArray,
        indices: &MlxArray,
        axis: i32,
    ) -> Result<MlxArray, MlxCError> {
        self.output_array("take MLX values along an axis", |output, stream| {
            // SAFETY: Inputs and stream are live and output is uniquely writable.
            unsafe { raw::mlx_take_along_axis(output, input.raw(), indices.raw(), axis, stream) }
        })
    }

    /// Writes selected values into a copy of one array along an existing axis.
    pub fn put_along_axis(
        &self,
        input: &MlxArray,
        indices: &MlxArray,
        values: &MlxArray,
        axis: i32,
    ) -> Result<MlxArray, MlxCError> {
        validate_put_along_axis_arguments(input, indices, values, axis)?;
        self.output_array("put MLX values along an axis", |output, stream| {
            // SAFETY: Inputs and stream are live, axis and shapes were
            // validated, and output is uniquely writable.
            unsafe {
                raw::mlx_put_along_axis(
                    output,
                    input.raw(),
                    indices.raw(),
                    values.raw(),
                    axis,
                    stream,
                )
            }
        })
    }

    /// Adds `updates` into a copy of `input` at `indices` along one axis.
    ///
    /// Compact mixture-of-experts split reduction lands several assignment rows
    /// on the same token. `put_along_axis` overwrites; this keeps every routed
    /// expert's contribution. MLX requires `updates.ndim() == input.ndim() +
    /// indices.ndim()`. For destination `[token_rows, hidden]`, pass indices
    /// `[assignment_count]` and updates `[assignment_count, 1, hidden]`.
    pub fn scatter_add(
        &self,
        input: &MlxArray,
        indices: &MlxArray,
        updates: &MlxArray,
        axis: i32,
    ) -> Result<MlxArray, MlxCError> {
        self.output_array("scatter-add values into an MLX array", |output, stream| {
            // SAFETY: Inputs and stream are live and output is uniquely writable.
            unsafe {
                raw::mlx_scatter_add_single(
                    output,
                    input.raw(),
                    indices.raw(),
                    updates.raw(),
                    axis,
                    stream,
                )
            }
        })
    }

    /// Evaluates and copies a float32 array into Rust-owned memory, including
    /// strided views.
    ///
    /// `MlxArray::to_vec_f32` reads the raw data pointer, which ignores the
    /// strides of a slice view over an evaluated parent. The flat reshape
    /// forces one contiguous materialization for strided views and is a
    /// zero-copy view for arrays that are already contiguous.
    pub fn array_to_vec_f32(&self, array: &MlxArray) -> Result<Vec<f32>, MlxCError> {
        let flat = self.reshape(array, &[-1])?;
        flat.to_vec_f32()
    }

    /// Slices an array with one static start, stop, and stride per axis.
    pub fn slice(
        &self,
        input: &MlxArray,
        starts: &[i32],
        stops: &[i32],
        strides: &[i32],
    ) -> Result<MlxArray, MlxCError> {
        validate_static_slice(input, starts, stops, strides)?;
        self.output_array("slice an MLX array", |output, stream| {
            // SAFETY: Slice bounds remain borrowed for this graph-building call,
            // input and stream are live, and output is uniquely writable.
            unsafe {
                raw::mlx_slice(
                    output,
                    input.raw(),
                    starts.as_ptr(),
                    starts.len(),
                    stops.as_ptr(),
                    stops.len(),
                    strides.as_ptr(),
                    strides.len(),
                    stream,
                )
            }
        })
    }

    /// Replaces one static slice through MLX-C `mlx_slice_update`.
    ///
    /// MLX arrays are immutable graph values: this returns a new lazy array. The
    /// visual splice uses it to replace contiguous `<|image_pad|>` embedding runs
    /// without copying the complete embedding tensor through Rust.
    pub fn slice_update(
        &self,
        source: &MlxArray,
        update: &MlxArray,
        starts: &[i32],
        stops: &[i32],
        strides: &[i32],
    ) -> Result<MlxArray, MlxCError> {
        validate_static_slice(source, starts, stops, strides)?;
        self.output_array("update an MLX array slice", |output, stream| {
            // SAFETY: Source, update, and stream are live; slice vectors match
            // the source rank; output is uniquely writable.
            unsafe {
                raw::mlx_slice_update(
                    output,
                    source.raw(),
                    update.raw(),
                    starts.as_ptr(),
                    starts.len(),
                    stops.as_ptr(),
                    stops.len(),
                    strides.as_ptr(),
                    strides.len(),
                    stream,
                )
            }
        })
    }

    /// Inserts one singleton dimension.
    pub fn expand_dims(&self, input: &MlxArray, axis: i32) -> Result<MlxArray, MlxCError> {
        self.output_array("expand an MLX array dimension", |output, stream| {
            // SAFETY: Input and stream are live and output is uniquely writable.
            unsafe { raw::mlx_expand_dims(output, input.raw(), axis, stream) }
        })
    }

    /// Concatenates arrays through MLX-C `mlx_concatenate_axis`.
    pub fn concatenate_axis(&self, arrays: &[&MlxArray], axis: i32) -> Result<MlxArray, MlxCError> {
        let array_vector = MlxArrayVector::new(arrays)?;
        self.output_array("concatenate MLX arrays", |output, stream| {
            // SAFETY: The vector and stream are live and output is uniquely writable.
            unsafe { raw::mlx_concatenate_axis(output, array_vector.raw(), axis, stream) }
        })
    }

    /// Broadcasts an array to a validated static shape.
    pub fn broadcast_to(&self, input: &MlxArray, shape: &[i32]) -> Result<MlxArray, MlxCError> {
        validate_static_shape("broadcast an MLX array", shape)?;
        let broadcast_values =
            self.output_array("broadcast an MLX array", |output_array, stream| {
                // SAFETY: Input and stream are live, shape remains borrowed for this
                // graph-building call, and output is uniquely writable.
                unsafe {
                    raw::mlx_broadcast_to(
                        output_array,
                        input.raw(),
                        shape.as_ptr(),
                        shape.len(),
                        stream,
                    )
                }
            })?;
        self.contiguous_row_major(
            &broadcast_values,
            "materialize broadcast MLX array contiguously",
        )
    }

    /// Repeats each value `repeat_count` times along one existing axis.
    pub fn repeat_axis(
        &self,
        input: &MlxArray,
        repeat_count: i32,
        axis: i32,
    ) -> Result<MlxArray, MlxCError> {
        validate_repeat_axis_arguments(input, repeat_count, axis)?;
        self.output_array("repeat MLX values along an axis", |output_array, stream| {
            // SAFETY: Input and stream are live, scalar arguments were validated,
            // and output is uniquely writable for MLX to populate.
            unsafe { raw::mlx_repeat_axis(output_array, input.raw(), repeat_count, axis, stream) }
        })
    }

    /// Stacks arrays with identical shapes along one new axis.
    pub fn stack_axis(&self, arrays: &[&MlxArray], axis: i32) -> Result<MlxArray, MlxCError> {
        validate_stack_axis_arguments(arrays, axis)?;
        let array_vector = MlxArrayVector::new(arrays)?;
        self.output_array(
            "stack MLX arrays along a new axis",
            |output_array, stream| {
                // SAFETY: The vector keeps all source arrays live for this graph-building
                // call, axis was validated, stream is live, and output is uniquely writable.
                unsafe { raw::mlx_stack_axis(output_array, array_vector.raw(), axis, stream) }
            },
        )
    }

    /// Removes one existing singleton axis.
    pub fn squeeze_axis(&self, input: &MlxArray, axis: i32) -> Result<MlxArray, MlxCError> {
        validate_squeeze_axis_arguments(input, axis)?;
        self.output_array("squeeze one MLX singleton axis", |output_array, stream| {
            // SAFETY: Input and stream are live, axis was validated to name a
            // singleton dimension, and output is uniquely writable.
            unsafe { raw::mlx_squeeze_axis(output_array, input.raw(), axis, stream) }
        })
    }
}

fn validate_static_slice(
    input: &MlxArray,
    starts: &[i32],
    stops: &[i32],
    strides: &[i32],
) -> Result<(), MlxCError> {
    let rank = input.shape().len();
    if starts.len() != rank || stops.len() != rank || strides.len() != rank {
        return Err(MlxCError {
            operation: "slice an MLX array",
            description: "slice starts, stops, and strides must match the input rank".to_owned(),
        });
    }
    if strides.contains(&0) {
        return Err(MlxCError {
            operation: "slice an MLX array",
            description: "slice strides must be nonzero".to_owned(),
        });
    }
    Ok(())
}

fn validate_put_along_axis_arguments(
    input: &MlxArray,
    indices: &MlxArray,
    values: &MlxArray,
    axis: i32,
) -> Result<(), MlxCError> {
    const OPERATION: &str = "put MLX values along an axis";
    let input_shape = input.shape();
    let index_shape = indices.shape();
    if input.dtype() != values.dtype() || index_shape != values.shape() {
        return Err(MlxCError {
            operation: OPERATION,
            description: "input/value dtypes and index/value shapes must match".to_owned(),
        });
    }
    if !matches!(
        indices.dtype(),
        MlxDtype::UInt8
            | MlxDtype::UInt16
            | MlxDtype::UInt32
            | MlxDtype::UInt64
            | MlxDtype::Int8
            | MlxDtype::Int16
            | MlxDtype::Int32
            | MlxDtype::Int64
    ) {
        return Err(MlxCError {
            operation: OPERATION,
            description: "put-along-axis indices must have an integral dtype".to_owned(),
        });
    }
    let rank = i32::try_from(input_shape.len()).map_err(|_| MlxCError {
        operation: OPERATION,
        description: "input rank exceeds the MLX integer range".to_owned(),
    })?;
    if rank == 0 || index_shape.len() != input_shape.len() || axis < -rank || axis >= rank {
        return Err(MlxCError {
            operation: OPERATION,
            description: "axis and index rank must match the destination array".to_owned(),
        });
    }
    let normalized_axis = if axis < 0 { axis + rank } else { axis } as usize;
    if index_shape
        .iter()
        .enumerate()
        .any(|(dimension_index, dimension)| {
            dimension_index != normalized_axis && *dimension != input_shape[dimension_index]
        })
    {
        return Err(MlxCError {
            operation: OPERATION,
            description: "non-selected dimensions must match the destination shape".to_owned(),
        });
    }
    Ok(())
}

fn validate_static_shape(operation: &'static str, shape: &[i32]) -> Result<(), MlxCError> {
    if shape.iter().any(|dimension_size| *dimension_size < 0) {
        return Err(runtime_operation_error(
            operation,
            "shape dimensions must be nonnegative",
        ));
    }

    let element_count = shape
        .iter()
        .try_fold(1_usize, |element_count, dimension_size| {
            let dimension_size = usize::try_from(*dimension_size).ok()?;
            element_count.checked_mul(dimension_size)
        });
    if element_count.is_none() {
        return Err(runtime_operation_error(
            operation,
            "shape element count overflows usize",
        ));
    }
    Ok(())
}

fn validate_repeat_axis_arguments(
    input: &MlxArray,
    repeat_count: i32,
    axis: i32,
) -> Result<(), MlxCError> {
    const OPERATION: &str = "repeat MLX values along an axis";
    if repeat_count <= 0 {
        return Err(runtime_operation_error(
            OPERATION,
            "repeat count must be positive",
        ));
    }

    let rank = i32::try_from(input.shape().len())
        .map_err(|_| runtime_operation_error(OPERATION, "input rank exceeds i32 range"))?;
    if rank == 0 || axis < -rank || axis >= rank {
        return Err(runtime_operation_error(
            OPERATION,
            "axis must refer to an existing input dimension",
        ));
    }
    Ok(())
}

fn validate_stack_axis_arguments(arrays: &[&MlxArray], axis: i32) -> Result<(), MlxCError> {
    const OPERATION: &str = "stack MLX arrays along a new axis";
    let first_array = arrays
        .first()
        .ok_or_else(|| runtime_operation_error(OPERATION, "at least one array is required"))?;
    let first_shape = first_array.shape();
    if arrays
        .iter()
        .any(|candidate_array| candidate_array.shape() != first_shape)
    {
        return Err(runtime_operation_error(
            OPERATION,
            "all stacked arrays must have the same shape",
        ));
    }

    let output_rank = i32::try_from(first_shape.len() + 1)
        .map_err(|_| runtime_operation_error(OPERATION, "output rank exceeds i32 range"))?;
    if axis < -output_rank || axis >= output_rank {
        return Err(runtime_operation_error(
            OPERATION,
            "axis must refer to a valid inserted output dimension",
        ));
    }
    Ok(())
}

fn validate_squeeze_axis_arguments(input: &MlxArray, axis: i32) -> Result<(), MlxCError> {
    const OPERATION: &str = "squeeze one MLX singleton axis";
    let input_shape = input.shape();
    let rank = i32::try_from(input_shape.len())
        .map_err(|_| runtime_operation_error(OPERATION, "input rank exceeds i32 range"))?;
    if rank == 0 || axis < -rank || axis >= rank {
        return Err(runtime_operation_error(
            OPERATION,
            "axis must refer to an existing input dimension",
        ));
    }

    let normalized_axis = if axis < 0 { axis + rank } else { axis };
    let axis_index = usize::try_from(normalized_axis)
        .map_err(|_| runtime_operation_error(OPERATION, "axis conversion failed"))?;
    if input_shape[axis_index] != 1 {
        return Err(runtime_operation_error(
            OPERATION,
            "squeezed axis must have size one",
        ));
    }
    Ok(())
}

fn runtime_operation_error(operation: &'static str, description: &'static str) -> MlxCError {
    MlxCError {
        operation,
        description: description.to_owned(),
    }
}