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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
use std::ffi::{CString, NulError};

use crate::MlxBindingsContext;
use crate::{MlxArray, MlxArrayVector, MlxCError, MlxDtype, MlxStream, error::check_status, raw};

/// Output shape and dtype requested from one custom Metal kernel launch.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct MlxMetalKernelOutput {
    shape: Vec<i32>,
    dtype: MlxDtype,
}

impl MlxMetalKernelOutput {
    #[must_use]
    pub const fn new(shape: Vec<i32>, dtype: MlxDtype) -> Self {
        Self { shape, dtype }
    }
}

/// Compile-time template argument passed to an MLX custom Metal kernel launch.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum MlxMetalKernelTemplateArgument {
    Dtype {
        name: &'static str,
        dtype: MlxDtype,
    },
    Integer {
        name: &'static str,
        integer_template_argument: i32,
    },
    Boolean {
        name: &'static str,
        boolean_template_argument: bool,
    },
}

/// Owned MLX custom Metal kernel function compiled from a source string.
#[derive(Debug)]
pub struct MlxMetalKernel {
    raw_kernel: raw::mlx_fast_metal_kernel,
}

impl MlxMetalKernel {
    pub fn new(
        kernel_name: &str,
        input_names: &[&str],
        output_names: &[&str],
        kernel_source: &str,
    ) -> Result<Self, MlxCError> {
        Self::new_with_header(kernel_name, input_names, output_names, "", kernel_source)
    }

    pub fn new_with_header(
        kernel_name: &str,
        input_names: &[&str],
        output_names: &[&str],
        kernel_header: &str,
        kernel_source: &str,
    ) -> Result<Self, MlxCError> {
        Self::new_with_options(
            kernel_name,
            input_names,
            output_names,
            kernel_header,
            kernel_source,
            false,
        )
    }

    pub fn new_with_options(
        kernel_name: &str,
        input_names: &[&str],
        output_names: &[&str],
        kernel_header: &str,
        kernel_source: &str,
        has_atomic_outputs: bool,
    ) -> Result<Self, MlxCError> {
        const OPERATION: &str = "create an MLX custom Metal kernel";
        if input_names.is_empty() || output_names.is_empty() {
            return Err(MlxCError {
                operation: OPERATION,
                description: "custom Metal kernels must declare at least one input and output"
                    .to_owned(),
            });
        }
        let kernel_name = c_string(kernel_name, OPERATION, "kernel name")?;
        let kernel_source = c_string(kernel_source, OPERATION, "kernel source")?;
        let kernel_header = c_string(kernel_header, OPERATION, "kernel header")?;
        let input_name_vector = MlxStringVector::new(input_names, OPERATION)?;
        let output_name_vector = MlxStringVector::new(output_names, OPERATION)?;
        // SAFETY: C strings and vectors stay live through this copying constructor;
        // Row-contiguous inputs and the explicit atomic-output contract match this kernel.
        let raw_kernel = unsafe {
            raw::mlx_fast_metal_kernel_new(
                kernel_name.as_ptr(),
                input_name_vector.raw(),
                output_name_vector.raw(),
                kernel_source.as_ptr(),
                kernel_header.as_ptr(),
                true,
                has_atomic_outputs,
            )
        };
        if raw_kernel.ctx.is_null() {
            return Err(MlxCError {
                operation: OPERATION,
                description: "MLX returned an empty custom Metal kernel handle".to_owned(),
            });
        }
        Ok(Self { raw_kernel })
    }

    const fn raw(&self) -> raw::mlx_fast_metal_kernel {
        self.raw_kernel
    }
}

impl Drop for MlxMetalKernel {
    fn drop(&mut self) {
        // SAFETY: This owner releases its live kernel handle exactly once.
        unsafe {
            raw::mlx_fast_metal_kernel_free(self.raw_kernel);
        }
    }
}

impl MlxBindingsContext {
    /// Applies one owned custom Metal kernel on the runtime's GPU stream.
    pub fn apply_metal_kernel(
        &self,
        kernel: &MlxMetalKernel,
        input_arrays: &[&MlxArray],
        output_specs: &[MlxMetalKernelOutput],
        grid: [i32; 3],
        thread_group: [i32; 3],
        template_arguments: &[MlxMetalKernelTemplateArgument],
    ) -> Result<Vec<MlxArray>, MlxCError> {
        self.apply_metal_kernel_with_output_initialization(
            kernel,
            input_arrays,
            output_specs,
            grid,
            thread_group,
            template_arguments,
            None,
        )
    }

    #[allow(clippy::too_many_arguments)]
    pub fn apply_metal_kernel_with_output_initialization(
        &self,
        kernel: &MlxMetalKernel,
        input_arrays: &[&MlxArray],
        output_specs: &[MlxMetalKernelOutput],
        grid: [i32; 3],
        thread_group: [i32; 3],
        template_arguments: &[MlxMetalKernelTemplateArgument],
        output_initial_value: Option<f32>,
    ) -> Result<Vec<MlxArray>, MlxCError> {
        const OPERATION: &str = "apply an MLX custom Metal kernel";
        validate_kernel_launch(input_arrays, output_specs, grid, thread_group)?;
        let input_vector = MlxArrayVector::new(input_arrays)?;
        let kernel_config =
            MlxMetalKernelConfig::new(output_specs, grid, thread_group, output_initial_value)?;
        kernel_config.add_template_arguments(template_arguments)?;
        let mut output_vector = MlxArrayVector::empty(OPERATION)?;
        // SAFETY: Kernel, input vector, config, stream, and output vector are live;
        // MLX populates the output vector synchronously as a lazy graph result.
        let status = unsafe {
            raw::mlx_fast_metal_kernel_apply(
                output_vector.raw_mut(),
                kernel.raw(),
                input_vector.raw(),
                kernel_config.raw(),
                self.gpu_stream().raw(),
            )
        };
        check_status(status, OPERATION)?;
        let output_count = output_vector.len();
        if output_count != output_specs.len() {
            return Err(MlxCError {
                operation: OPERATION,
                description: format!(
                    "custom Metal kernel returned {output_count} outputs but {} were requested",
                    output_specs.len()
                ),
            });
        }
        (0..output_count)
            .map(|output_index| output_vector.array_at(output_index, OPERATION))
            .collect::<Result<Vec<_>, MlxCError>>()
    }
}

/// Applies one custom Metal kernel inside a compiled-graph trace.
///
/// Identical to [`MlxBindingsContext::apply_metal_kernel`] minus the runtime handle:
/// compiled-graph builders run without a runtime, on the stream the trace is
/// being built for. Errors surface as plain status codes; the input contract
/// (row-contiguous arrays, validated launch dimensions) is the builder's
/// responsibility.
#[allow(clippy::too_many_arguments)]
pub fn apply_metal_kernel_in_graph(
    kernel: &MlxMetalKernel,
    input_arrays: &[&MlxArray],
    output_specs: &[MlxMetalKernelOutput],
    grid: [i32; 3],
    thread_group: [i32; 3],
    template_arguments: &[MlxMetalKernelTemplateArgument],
    gpu_stream: &MlxStream,
) -> Result<Vec<MlxArray>, i32> {
    let input_vector = MlxArrayVector::new(input_arrays).map_err(|_| 1)?;
    let kernel_config =
        MlxMetalKernelConfig::new(output_specs, grid, thread_group, None).map_err(|_| 1)?;
    kernel_config
        .add_template_arguments(template_arguments)
        .map_err(|_| 1)?;
    let mut output_vector =
        MlxArrayVector::empty("apply a Metal kernel in a graph trace").map_err(|_| 1)?;
    // SAFETY: Kernel, input vector, config, stream, and output vector are live;
    // MLX populates the output vector synchronously as a lazy graph result.
    let status = unsafe {
        raw::mlx_fast_metal_kernel_apply(
            output_vector.raw_mut(),
            kernel.raw(),
            input_vector.raw(),
            kernel_config.raw(),
            gpu_stream.raw(),
        )
    };
    if status != 0 {
        return Err(status);
    }
    let output_count = output_vector.len();
    if output_count != output_specs.len() {
        return Err(1);
    }
    let mut outputs = Vec::with_capacity(output_count);
    for output_index in 0..output_count {
        outputs.push(
            output_vector
                .array_at(output_index, "apply a Metal kernel in a graph trace")
                .map_err(|_| 1)?,
        );
    }
    Ok(outputs)
}

#[derive(Debug)]
pub struct MlxMetalKernelConfig {
    raw_config: raw::mlx_fast_metal_kernel_config,
}

impl MlxMetalKernelConfig {
    pub fn new(
        output_specs: &[MlxMetalKernelOutput],
        grid: [i32; 3],
        thread_group: [i32; 3],
        output_initial_value: Option<f32>,
    ) -> Result<Self, MlxCError> {
        const OPERATION: &str = "configure an MLX custom Metal kernel";
        // SAFETY: The runtime error handler is installed before model loading,
        // and MLX returns one owned config handle.
        let raw_config = unsafe { raw::mlx_fast_metal_kernel_config_new() };
        if raw_config.ctx.is_null() {
            return Err(MlxCError {
                operation: OPERATION,
                description: "MLX returned an empty custom Metal kernel config".to_owned(),
            });
        }
        let kernel_config = Self { raw_config };
        if let Some(output_initial_value) = output_initial_value {
            // SAFETY: The scalar initialization value is copied by MLX.
            let status = unsafe {
                raw::mlx_fast_metal_kernel_config_set_init_value(
                    kernel_config.raw(),
                    output_initial_value,
                )
            };
            check_status(status, OPERATION)?;
        }
        for output_spec in output_specs {
            // SAFETY: The shape slice remains live for this copying call.
            let status = unsafe {
                raw::mlx_fast_metal_kernel_config_add_output_arg(
                    kernel_config.raw(),
                    output_spec.shape.as_ptr(),
                    output_spec.shape.len(),
                    output_spec.dtype.to_raw(),
                )
            };
            check_status(status, OPERATION)?;
        }
        // SAFETY: Scalar launch dimensions are validated before construction.
        let status = unsafe {
            raw::mlx_fast_metal_kernel_config_set_grid(
                kernel_config.raw(),
                grid[0],
                grid[1],
                grid[2],
            )
        };
        check_status(status, OPERATION)?;
        // SAFETY: Scalar launch dimensions are validated before construction.
        let status = unsafe {
            raw::mlx_fast_metal_kernel_config_set_thread_group(
                kernel_config.raw(),
                thread_group[0],
                thread_group[1],
                thread_group[2],
            )
        };
        check_status(status, OPERATION)?;
        Ok(kernel_config)
    }

    const fn raw(&self) -> raw::mlx_fast_metal_kernel_config {
        self.raw_config
    }

    fn add_template_arguments(
        &self,
        template_arguments: &[MlxMetalKernelTemplateArgument],
    ) -> Result<(), MlxCError> {
        const OPERATION: &str = "configure an MLX custom Metal kernel";
        for template_argument in template_arguments {
            match *template_argument {
                MlxMetalKernelTemplateArgument::Dtype { name, dtype } => {
                    let argument_name = c_string(name, OPERATION, "template dtype name")?;
                    // SAFETY: Name is live for this copying call; dtype is validated by MLX.
                    let status = unsafe {
                        raw::mlx_fast_metal_kernel_config_add_template_arg_dtype(
                            self.raw(),
                            argument_name.as_ptr(),
                            dtype.to_raw(),
                        )
                    };
                    check_status(status, OPERATION)?;
                }
                MlxMetalKernelTemplateArgument::Integer {
                    name,
                    integer_template_argument,
                } => {
                    let argument_name = c_string(name, OPERATION, "template integer name")?;
                    // SAFETY: Name is live for this copying call; the scalar argument is copied.
                    let status = unsafe {
                        raw::mlx_fast_metal_kernel_config_add_template_arg_int(
                            self.raw(),
                            argument_name.as_ptr(),
                            integer_template_argument,
                        )
                    };
                    check_status(status, OPERATION)?;
                }
                MlxMetalKernelTemplateArgument::Boolean {
                    name,
                    boolean_template_argument,
                } => {
                    let argument_name = c_string(name, OPERATION, "template boolean name")?;
                    // SAFETY: Name is live for this copying call; the scalar argument is copied.
                    let status = unsafe {
                        raw::mlx_fast_metal_kernel_config_add_template_arg_bool(
                            self.raw(),
                            argument_name.as_ptr(),
                            boolean_template_argument,
                        )
                    };
                    check_status(status, OPERATION)?;
                }
            }
        }
        Ok(())
    }

    /// Toggles MLX's verbose logging for this kernel launch.
    pub fn set_verbose(&mut self, verbose: bool) -> Result<(), MlxCError> {
        const OPERATION: &str = "configure MLX custom Metal kernel logging";
        // SAFETY: The config handle is live and the flag is a plain value.
        let status = unsafe { raw::mlx_fast_metal_kernel_config_set_verbose(self.raw(), verbose) };
        check_status(status, OPERATION)
    }
}

impl Drop for MlxMetalKernelConfig {
    fn drop(&mut self) {
        // SAFETY: This owner releases its live config handle exactly once.
        unsafe {
            raw::mlx_fast_metal_kernel_config_free(self.raw_config);
        }
    }
}

#[derive(Debug)]
struct MlxStringVector {
    raw_vector: raw::mlx_vector_string,
}

impl MlxStringVector {
    fn new(strings: &[&str], operation: &'static str) -> Result<Self, MlxCError> {
        let c_strings = strings
            .iter()
            .map(|source_string| c_string(source_string, operation, "string vector entry"))
            .collect::<Result<Vec<_>, _>>()?;
        let mut string_pointers = c_strings
            .iter()
            .map(|c_string| c_string.as_ptr())
            .collect::<Vec<_>>();
        // SAFETY: Pointers remain live for this copying constructor.
        let raw_vector = unsafe {
            raw::mlx_vector_string_new_data(string_pointers.as_mut_ptr(), string_pointers.len())
        };
        if raw_vector.ctx.is_null() {
            return Err(MlxCError {
                operation,
                description: "MLX returned an empty string vector handle".to_owned(),
            });
        }
        Ok(Self { raw_vector })
    }

    const fn raw(&self) -> raw::mlx_vector_string {
        self.raw_vector
    }
}

impl Drop for MlxStringVector {
    fn drop(&mut self) {
        // SAFETY: This owner releases its live string vector handle exactly once.
        unsafe {
            raw::mlx_vector_string_free(self.raw_vector);
        }
    }
}

fn validate_kernel_launch(
    input_arrays: &[&MlxArray],
    output_specs: &[MlxMetalKernelOutput],
    grid: [i32; 3],
    thread_group: [i32; 3],
) -> Result<(), MlxCError> {
    const OPERATION: &str = "apply an MLX custom Metal kernel";
    if input_arrays.is_empty() || output_specs.is_empty() {
        return Err(MlxCError {
            operation: OPERATION,
            description: "custom Metal kernel launches require at least one input and output"
                .to_owned(),
        });
    }
    if grid.iter().any(|dimension| *dimension <= 0)
        || thread_group.iter().any(|dimension| *dimension <= 0)
    {
        return Err(MlxCError {
            operation: OPERATION,
            description: "custom Metal kernel grid and threadgroup dimensions must be positive"
                .to_owned(),
        });
    }
    Ok(())
}

fn c_string(
    source_text: &str,
    operation: &'static str,
    value_description: &'static str,
) -> Result<CString, MlxCError> {
    CString::new(source_text).map_err(|source| nul_error(operation, value_description, source))
}

fn nul_error(
    operation: &'static str,
    value_description: &'static str,
    source: NulError,
) -> MlxCError {
    MlxCError {
        operation,
        description: format!("{value_description} contains an interior NUL byte: {source}"),
    }
}