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
446
447
448
449
450
451
452
453
454
455
456
457
458
use crate::MlxBindingsContext;
use crate::{MlxArray, MlxArrayVector};
use crate::{MlxCError, MlxDtype, error::check_status, raw};

impl MlxBindingsContext {
    /// Builds lazy matrix multiplication on the runtime's GPU stream.
    pub fn matmul(&self, left: &MlxArray, right: &MlxArray) -> Result<MlxArray, MlxCError> {
        self.output_array("multiply MLX arrays", |output, stream| {
            // SAFETY: Inputs and stream are live and output is uniquely writable.
            unsafe { raw::mlx_matmul(output, left.raw(), right.raw(), stream) }
        })
    }

    /// Builds matrix multiplication while selecting matrix batches on-device.
    ///
    /// This maps directly to MLX `gather_mm`; unlike `take` followed by
    /// `matmul`, it does not materialize one weight matrix per assignment.
    pub fn gather_dense_matmul(
        &self,
        left: &MlxArray,
        right: &MlxArray,
        left_indices: Option<&MlxArray>,
        right_indices: Option<&MlxArray>,
        sorted_indices: bool,
    ) -> Result<MlxArray, MlxCError> {
        if left_indices.is_none() && right_indices.is_none() {
            return Err(MlxCError {
                operation: "build dense gather_mm",
                description: "at least one matrix-batch index array is required".to_owned(),
            });
        }
        let raw_left_indices = left_indices.map_or_else(MlxArray::empty_raw, MlxArray::raw);
        let raw_right_indices = right_indices.map_or_else(MlxArray::empty_raw, MlxArray::raw);
        self.output_array("build dense gather_mm", |output, stream| {
            // SAFETY: Arrays and stream are live; absent index arrays use MLX's
            // official empty-handle convention; output is uniquely writable.
            unsafe {
                raw::mlx_gather_mm(
                    output,
                    left.raw(),
                    right.raw(),
                    raw_left_indices,
                    raw_right_indices,
                    sorted_indices,
                    stream,
                )
            }
        })
    }

    /// Applies MLX-C `mlx_addmm`: `alpha * (left @ right) + beta * bias`.
    ///
    /// See `mlx-c/mlx/c/ops.h::mlx_addmm` and its forwarding implementation in
    /// `ops.cpp`. Keep affine model layers on this fused operation: separate
    /// `mlx_matmul` then `mlx_add` changes BF16 accumulation/rounding and violates
    /// the Qwen3.5 vision numerical contract.
    pub fn addmm(
        &self,
        bias: &MlxArray,
        left: &MlxArray,
        right: &MlxArray,
        alpha: f32,
        beta: f32,
    ) -> Result<MlxArray, MlxCError> {
        self.output_array("apply fused MLX addmm", |output, stream| {
            // SAFETY: Arrays and stream are live and output is uniquely writable.
            unsafe {
                raw::mlx_addmm(
                    output,
                    bias.raw(),
                    left.raw(),
                    right.raw(),
                    alpha,
                    beta,
                    stream,
                )
            }
        })
    }

    /// Casts an array while preserving lazy evaluation.
    pub fn astype(&self, input: &MlxArray, dtype: MlxDtype) -> Result<MlxArray, MlxCError> {
        self.output_array("cast an MLX array", |output, stream| {
            // SAFETY: Input and stream are live and output is uniquely writable.
            unsafe { raw::mlx_astype(output, input.raw(), dtype.to_raw(), stream) }
        })
    }

    /// Applies fused RMS normalization with a required weight.
    pub fn rms_norm(
        &self,
        input: &MlxArray,
        weight: &MlxArray,
        epsilon: f32,
    ) -> Result<MlxArray, MlxCError> {
        self.output_array("apply MLX RMS normalization", |output, stream| {
            // SAFETY: Inputs and stream are live and output is uniquely writable.
            unsafe { raw::mlx_fast_rms_norm(output, input.raw(), weight.raw(), epsilon, stream) }
        })
    }

    /// Applies fused RMS normalization without an affine weight.
    pub fn rms_norm_without_weight(
        &self,
        input: &MlxArray,
        epsilon: f32,
    ) -> Result<MlxArray, MlxCError> {
        self.output_array(
            "apply unweighted MLX RMS normalization",
            |output, stream| {
                // SAFETY: Input and stream are live, the empty handle is MLX's
                // official convention for no affine weight, and output is unique.
                unsafe {
                    raw::mlx_fast_rms_norm(
                        output,
                        input.raw(),
                        MlxArray::empty_raw(),
                        epsilon,
                        stream,
                    )
                }
            },
        )
    }

    /// Applies fused LayerNorm through MLX-C `mlx_fast_layer_norm`.
    ///
    /// The declaration lives in `mlx-c/mlx/c/fast.h`; `fast.cpp` forwards to
    /// `mlx::core::fast::layer_norm`, allowing MLX to choose its Metal kernel.
    pub fn layer_norm(
        &self,
        input: &MlxArray,
        weight: &MlxArray,
        bias: &MlxArray,
        epsilon: f32,
    ) -> Result<MlxArray, MlxCError> {
        self.output_array("apply MLX LayerNorm", |output, stream| {
            // SAFETY: Inputs and stream are live and output is uniquely writable.
            unsafe {
                raw::mlx_fast_layer_norm(
                    output,
                    input.raw(),
                    weight.raw(),
                    bias.raw(),
                    epsilon,
                    stream,
                )
            }
        })
    }

    /// Adds two broadcast-compatible arrays.
    pub fn add(&self, left: &MlxArray, right: &MlxArray) -> Result<MlxArray, MlxCError> {
        self.output_array("add MLX arrays", |output, stream| {
            // SAFETY: Inputs and stream are live and output is uniquely writable.
            unsafe { raw::mlx_add(output, left.raw(), right.raw(), stream) }
        })
    }

    /// Subtracts two broadcast-compatible arrays.
    pub fn subtract(&self, left: &MlxArray, right: &MlxArray) -> Result<MlxArray, MlxCError> {
        self.output_array("subtract MLX arrays", |output, stream| {
            // SAFETY: Inputs and stream are live and output is uniquely writable.
            unsafe { raw::mlx_subtract(output, left.raw(), right.raw(), stream) }
        })
    }

    /// Divides two broadcast-compatible arrays.
    pub fn divide(&self, left: &MlxArray, right: &MlxArray) -> Result<MlxArray, MlxCError> {
        self.output_array("divide MLX arrays", |output, stream| {
            // SAFETY: Inputs and stream are live and output is uniquely writable.
            unsafe { raw::mlx_divide(output, left.raw(), right.raw(), stream) }
        })
    }

    /// Applies elementwise floor division to broadcast-compatible arrays.
    pub fn floor_divide(&self, left: &MlxArray, right: &MlxArray) -> Result<MlxArray, MlxCError> {
        self.output_array("floor-divide MLX arrays", |output, stream| {
            // SAFETY: Inputs and stream are live and output is uniquely writable.
            unsafe { raw::mlx_floor_divide(output, left.raw(), right.raw(), stream) }
        })
    }

    /// Negates every element in an array.
    pub fn negative(&self, input: &MlxArray) -> Result<MlxArray, MlxCError> {
        self.output_array("negate an MLX array", |output, stream| {
            // SAFETY: Input and stream are live and output is uniquely writable.
            unsafe { raw::mlx_negative(output, input.raw(), stream) }
        })
    }

    /// Applies the elementwise natural exponential.
    pub fn exp(&self, input: &MlxArray) -> Result<MlxArray, MlxCError> {
        self.output_array("apply MLX exp", |output, stream| {
            // SAFETY: Input and stream are live and output is uniquely writable.
            unsafe { raw::mlx_exp(output, input.raw(), stream) }
        })
    }

    /// Applies the elementwise natural logarithm of one plus the input.
    pub fn log1p(&self, input: &MlxArray) -> Result<MlxArray, MlxCError> {
        self.output_array("apply MLX log1p", |output, stream| {
            // SAFETY: Input and stream are live and output is uniquely writable.
            unsafe { raw::mlx_log1p(output, input.raw(), stream) }
        })
    }

    /// Applies the numerically stable elementwise `log(exp(left) + exp(right))`.
    pub fn logaddexp(&self, left: &MlxArray, right: &MlxArray) -> Result<MlxArray, MlxCError> {
        self.output_array("apply MLX logaddexp", |output, stream| {
            // SAFETY: Inputs and stream are live and output is uniquely writable.
            unsafe { raw::mlx_logaddexp(output, left.raw(), right.raw(), stream) }
        })
    }

    /// Multiplies two broadcast-compatible arrays.
    pub fn multiply(&self, left: &MlxArray, right: &MlxArray) -> Result<MlxArray, MlxCError> {
        self.output_array("multiply MLX arrays elementwise", |output, stream| {
            // SAFETY: Inputs and stream are live and output is uniquely writable.
            unsafe { raw::mlx_multiply(output, left.raw(), right.raw(), stream) }
        })
    }

    /// Multiplies an array by a scalar represented in the array's dtype.
    pub fn multiply_scalar(&self, input: &MlxArray, scalar: f32) -> Result<MlxArray, MlxCError> {
        let float_scalar = self.array_from_f32(&[scalar], &[])?;
        let typed_scalar = self.astype(&float_scalar, input.dtype())?;
        self.multiply(input, &typed_scalar)
    }

    /// Compares two broadcast-compatible arrays elementwise.
    pub fn greater_equal(&self, left: &MlxArray, right: &MlxArray) -> Result<MlxArray, MlxCError> {
        self.output_array("compare MLX arrays", |output, stream| {
            // SAFETY: Inputs and stream are live and output is uniquely writable.
            unsafe { raw::mlx_greater_equal(output, left.raw(), right.raw(), stream) }
        })
    }

    /// Compares two broadcast-compatible arrays elementwise for strict greater-than.
    pub fn greater(&self, left: &MlxArray, right: &MlxArray) -> Result<MlxArray, MlxCError> {
        self.output_array(
            "compare MLX arrays for strict greater-than",
            |output, stream| {
                // SAFETY: Inputs and stream are live and output is uniquely writable.
                unsafe { raw::mlx_greater(output, left.raw(), right.raw(), stream) }
            },
        )
    }

    /// Compares two broadcast-compatible arrays elementwise for strict less-than.
    pub fn less(&self, left: &MlxArray, right: &MlxArray) -> Result<MlxArray, MlxCError> {
        self.output_array(
            "compare MLX arrays for strict less-than",
            |output, stream| {
                // SAFETY: Inputs and stream are live and output is uniquely writable.
                unsafe { raw::mlx_less(output, left.raw(), right.raw(), stream) }
            },
        )
    }

    /// Computes the cumulative sum along one axis.
    pub fn cumsum(
        &self,
        input: &MlxArray,
        axis: i32,
        reverse: bool,
        inclusive: bool,
    ) -> Result<MlxArray, MlxCError> {
        self.output_array("compute MLX cumulative sum", |output, stream| {
            // mlx-c v0.7.0 splits the axis-taking variant into mlx_cumsum_axis;
            // the dtype stays absent, preserving the previous float-identity
            // behavior of the axis-taking overload.
            let absent_cumulative_dtype = raw::mlx_optional_dtype {
                value: raw::mlx_dtype__MLX_FLOAT32,
                has_value: false,
            };
            // SAFETY: Input and stream are live and output is uniquely writable.
            unsafe {
                raw::mlx_cumsum_axis(
                    output,
                    input.raw(),
                    axis,
                    reverse,
                    inclusive,
                    absent_cumulative_dtype,
                    stream,
                )
            }
        })
    }

    /// Selects values from two broadcast-compatible arrays using a boolean mask.
    pub fn where_select(
        &self,
        condition: &MlxArray,
        when_true: &MlxArray,
        when_false: &MlxArray,
    ) -> Result<MlxArray, MlxCError> {
        self.output_array("select values with an MLX mask", |output, stream| {
            // SAFETY: Inputs and stream are live and output is uniquely writable.
            unsafe {
                raw::mlx_where(
                    output,
                    condition.raw(),
                    when_true.raw(),
                    when_false.raw(),
                    stream,
                )
            }
        })
    }

    /// Applies precise softmax along one axis.
    pub fn softmax_axis(&self, input: &MlxArray, axis: i32) -> Result<MlxArray, MlxCError> {
        self.output_array("apply MLX softmax", |output, stream| {
            // SAFETY: Input and stream are live and output is uniquely writable.
            unsafe { raw::mlx_softmax_axis(output, input.raw(), axis, true, stream) }
        })
    }

    /// Sums values along one axis.
    pub fn sum_axis(
        &self,
        input: &MlxArray,
        axis: i32,
        keep_dimensions: bool,
    ) -> Result<MlxArray, MlxCError> {
        self.output_array("sum MLX array values along an axis", |output, stream| {
            // SAFETY: Input and stream are live and output is uniquely writable.
            unsafe { raw::mlx_sum_axis(output, input.raw(), axis, keep_dimensions, stream) }
        })
    }

    /// Returns maximum values along one axis.
    pub fn max_axis(
        &self,
        input: &MlxArray,
        axis: i32,
        keep_dimensions: bool,
    ) -> Result<MlxArray, MlxCError> {
        self.output_array("max MLX array values along an axis", |output, stream| {
            // SAFETY: Input and stream are live and output is uniquely writable.
            unsafe { raw::mlx_max_axis(output, input.raw(), axis, keep_dimensions, stream) }
        })
    }

    /// Applies the elementwise sigmoid function.
    pub fn sigmoid(&self, input: &MlxArray) -> Result<MlxArray, MlxCError> {
        self.output_array("apply MLX sigmoid", |output, stream| {
            // SAFETY: Input and stream are live and output is uniquely writable.
            unsafe { raw::mlx_sigmoid(output, input.raw(), stream) }
        })
    }

    /// Returns argmax indices along one axis.
    pub fn argmax_axis(&self, input: &MlxArray, axis: i32) -> Result<MlxArray, MlxCError> {
        self.output_array("compute MLX argmax", |output, stream| {
            // SAFETY: Input and stream are live and output is uniquely writable.
            unsafe { raw::mlx_argmax_axis(output, input.raw(), axis, false, stream) }
        })
    }

    /// Returns indices that partition values along one axis around `kth`.
    pub fn argpartition_axis(
        &self,
        input: &MlxArray,
        kth: i32,
        axis: i32,
    ) -> Result<MlxArray, MlxCError> {
        self.output_array("compute MLX argpartition", |output, stream| {
            // SAFETY: Input and stream are live and output is uniquely writable.
            unsafe { raw::mlx_argpartition_axis(output, input.raw(), kth, axis, stream) }
        })
    }

    /// Returns indices that sort values along one axis in ascending order.
    pub fn argsort_axis(&self, input: &MlxArray, axis: i32) -> Result<MlxArray, MlxCError> {
        self.output_array("compute MLX argsort", |output, stream| {
            // SAFETY: Input and stream are live and output is uniquely writable.
            unsafe { raw::mlx_argsort_axis(output, input.raw(), axis, stream) }
        })
    }

    /// Returns top values along one axis and materializes them contiguously for bounded Rust copies.
    pub fn topk_axis(
        &self,
        input: &MlxArray,
        count: i32,
        axis: i32,
    ) -> Result<MlxArray, MlxCError> {
        let top_values = self.output_array("compute MLX top-k values", |output, stream| {
            // SAFETY: Input and stream are live and output is uniquely writable.
            unsafe { raw::mlx_topk_axis(output, input.raw(), count, axis, stream) }
        })?;
        self.contiguous_row_major(&top_values, "materialize MLX top-k values contiguously")
    }

    /// Evaluates a bounded group together so KV state does not retain prior graphs.
    pub fn evaluate_arrays(&self, arrays: &[&MlxArray]) -> Result<(), MlxCError> {
        let array_vector = MlxArrayVector::new(arrays)?;
        // SAFETY: The vector retains all live arrays for this synchronous evaluation.
        let status = unsafe { raw::mlx_eval(array_vector.raw()) };
        check_status(status, "evaluate an MLX array group")
    }

    /// Asynchronously submits a bounded group to the GPU without blocking the CPU.
    ///
    /// A later host read waits if execution is still in flight. Dependent graphs
    /// can be built and submitted meanwhile, keeping CPU graph construction and
    /// GPU execution overlapped.
    pub fn async_eval_arrays(&self, arrays: &[&MlxArray]) -> Result<(), MlxCError> {
        let array_vector = MlxArrayVector::new(arrays)?;
        // SAFETY: The vector retains all live arrays for this asynchronous evaluation.
        let status = unsafe { raw::mlx_async_eval(array_vector.raw()) };
        check_status(status, "asynchronously evaluate an MLX array group")
    }

    /// Materializes one uint32 array contiguously and copies its bounded values.
    pub fn copy_u32_values(&self, input: &MlxArray) -> Result<Vec<u32>, MlxCError> {
        let contiguous_values = self.build_contiguous_row_major_copy(input)?;
        contiguous_values.to_vec_u32()
    }

    /// Materializes one uint8 array contiguously and copies it in one host transfer.
    pub fn copy_u8_values(&self, input: &MlxArray) -> Result<Vec<u8>, MlxCError> {
        let contiguous_values = self.build_contiguous_row_major_copy(input)?;
        contiguous_values.to_vec_u8()
    }

    /// Builds a lazy row-major contiguous copy without evaluating it.
    pub fn build_contiguous_row_major_copy(&self, input: &MlxArray) -> Result<MlxArray, MlxCError> {
        self.contiguous_row_major(input, "build an MLX row-major contiguous copy")
    }

    pub(crate) fn contiguous_row_major(
        &self,
        input: &MlxArray,
        operation: &'static str,
    ) -> Result<MlxArray, MlxCError> {
        self.output_array(operation, |output, stream| {
            // SAFETY: Input and stream are live and output is uniquely writable.
            unsafe { raw::mlx_contiguous(output, input.raw(), false, stream) }
        })
    }

    /// Runs one MLX operation through a populated output-array callback.
    pub(crate) fn output_array(
        &self,
        operation: &'static str,
        build_graph: impl FnOnce(*mut raw::mlx_array, raw::mlx_stream) -> i32,
    ) -> Result<MlxArray, MlxCError> {
        let mut output = MlxArray::empty();
        let status = build_graph(output.raw_mut(), self.gpu_stream().raw());
        check_status(status, operation)?;
        output.require_populated(operation)?;
        Ok(output)
    }
}