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
use crate::MlxBindingsContext;
use crate::MlxCError;
use crate::{MlxArray, MlxDtype, raw};

impl MlxBindingsContext {
    /// Dequantizes selected affine rows, as required by quantized embedding lookup.
    pub fn dequantize_affine(
        &self,
        quantized_weights: &MlxArray,
        scales: &MlxArray,
        biases: &MlxArray,
        group_size: i32,
        bits: i32,
    ) -> Result<MlxArray, MlxCError> {
        validate_affine_dequantize_arguments(quantized_weights, scales, biases, group_size, bits)?;
        let optional_group_size = raw::mlx_optional_int {
            value: group_size,
            has_value: true,
        };
        let optional_bits = raw::mlx_optional_int {
            value: bits,
            has_value: true,
        };
        let absent_output_dtype = raw::mlx_optional_dtype {
            value: raw::mlx_dtype__MLX_FLOAT32,
            has_value: false,
        };
        self.output_array("dequantize selected affine rows", |output, stream| {
            // SAFETY: All array handles and the stream are live, scalar options
            // are copied by value, absent global scale/output dtype use official
            // empty conventions, and output is uniquely writable.
            unsafe {
                raw::mlx_dequantize(
                    output,
                    quantized_weights.raw(),
                    scales.raw(),
                    biases.raw(),
                    optional_group_size,
                    optional_bits,
                    c"affine".as_ptr(),
                    MlxArray::empty_raw(),
                    absent_output_dtype,
                    stream,
                )
            }
        })
    }

    /// Builds an affine quantized matrix multiplication using parameters supported by MLX.
    #[allow(clippy::too_many_arguments)]
    pub fn quantized_matmul_affine(
        &self,
        activations: &MlxArray,
        quantized_weights: &MlxArray,
        scales: &MlxArray,
        biases: &MlxArray,
        transpose_weights: bool,
        group_size: i32,
        bits: i32,
    ) -> Result<MlxArray, MlxCError> {
        validate_affine_quantized_matmul_arguments(
            "build affine quantized matmul",
            activations,
            quantized_weights,
            scales,
            biases,
            transpose_weights,
            group_size,
            bits,
        )?;
        let optional_group_size = raw::mlx_optional_int {
            value: group_size,
            has_value: true,
        };
        let optional_bits = raw::mlx_optional_int {
            value: bits,
            has_value: true,
        };
        self.output_array("build affine quantized matmul", |output, stream| {
            // SAFETY: All array handles and the stream are live, scalar options
            // are copied by value, the mode points to a static NUL-terminated
            // string, and output is uniquely writable for MLX to populate.
            unsafe {
                raw::mlx_quantized_matmul(
                    output,
                    activations.raw(),
                    quantized_weights.raw(),
                    scales.raw(),
                    biases.raw(),
                    transpose_weights,
                    optional_group_size,
                    optional_bits,
                    c"affine".as_ptr(),
                    stream,
                )
            }
        })
    }

    /// Builds selected affine quantized matrix multiplications for MoE experts.
    #[allow(clippy::too_many_arguments)]
    pub fn gather_quantized_matmul_affine(
        &self,
        activations: &MlxArray,
        quantized_weights: &MlxArray,
        scales: &MlxArray,
        biases: &MlxArray,
        lhs_indices: Option<&MlxArray>,
        rhs_indices: Option<&MlxArray>,
        transpose_weights: bool,
        group_size: i32,
        bits: i32,
        sorted_indices: bool,
    ) -> Result<MlxArray, MlxCError> {
        validate_affine_quantized_matmul_arguments(
            "build affine gather_qmm",
            activations,
            quantized_weights,
            scales,
            biases,
            transpose_weights,
            group_size,
            bits,
        )?;
        validate_gather_quantized_matmul_indices(lhs_indices, rhs_indices)?;
        let optional_group_size = raw::mlx_optional_int {
            value: group_size,
            has_value: true,
        };
        let optional_bits = raw::mlx_optional_int {
            value: bits,
            has_value: true,
        };
        let raw_lhs_indices = lhs_indices.map_or_else(MlxArray::empty_raw, MlxArray::raw);
        let raw_rhs_indices = rhs_indices.map_or_else(MlxArray::empty_raw, MlxArray::raw);
        self.output_array("build affine gather_qmm", |output, stream| {
            // SAFETY: All array handles and the stream are live, absent optional
            // index arrays are represented by MLX's empty handle convention,
            // scalar options are copied by value, the mode is a static C string,
            // and output is uniquely writable for MLX to populate.
            unsafe {
                raw::mlx_gather_qmm(
                    output,
                    activations.raw(),
                    quantized_weights.raw(),
                    scales.raw(),
                    biases.raw(),
                    raw_lhs_indices,
                    raw_rhs_indices,
                    transpose_weights,
                    optional_group_size,
                    optional_bits,
                    c"affine".as_ptr(),
                    sorted_indices,
                    stream,
                )
            }
        })
    }
}

fn validate_affine_dequantize_arguments(
    quantized_weights: &MlxArray,
    scales: &MlxArray,
    biases: &MlxArray,
    group_size: i32,
    bits: i32,
) -> Result<(), MlxCError> {
    const OPERATION: &str = "dequantize selected affine rows";
    if !is_mlx_supported_affine_group_size(group_size) {
        return Err(runtime_operation_error(
            OPERATION,
            "affine group size must be 32, 64, or 128",
        ));
    }
    if !is_mlx_supported_affine_bit_width(bits) {
        return Err(runtime_operation_error(
            OPERATION,
            "affine bit width must be 2, 3, 4, 5, 6, or 8",
        ));
    }
    if quantized_weights.dtype() != MlxDtype::UInt32 {
        return Err(runtime_operation_error(
            OPERATION,
            "quantized weights must have uint32 dtype",
        ));
    }
    if !is_supported_quantized_float_dtype(scales.dtype())
        || !is_supported_quantized_float_dtype(biases.dtype())
    {
        return Err(runtime_operation_error(
            OPERATION,
            "affine scales and biases must use a supported floating-point dtype",
        ));
    }
    let quantized_shape = quantized_weights.shape();
    let scale_shape = scales.shape();
    if quantized_shape.is_empty() || scale_shape != biases.shape() {
        return Err(runtime_operation_error(
            OPERATION,
            "quantized weights must have rank and scale/bias shapes must match",
        ));
    }
    let packed_tail_dimension = positive_last_dimension(&quantized_shape, OPERATION)?;
    let expanded_tail_dimension = packed_tail_dimension
        .checked_mul(32)
        .and_then(|packed_bits| (packed_bits % bits == 0).then_some(packed_bits / bits))
        .ok_or_else(|| {
            runtime_operation_error(
                OPERATION,
                "packed weight shape overflows or is incompatible with the requested bit width",
            )
        })?;
    let mut expected_scale_shape = quantized_shape;
    let scale_tail_position = expected_scale_shape.len() - 1;
    expected_scale_shape[scale_tail_position] =
        exact_group_count(expanded_tail_dimension, group_size, OPERATION)?;
    if scale_shape != expected_scale_shape {
        return Err(runtime_operation_error(
            OPERATION,
            "affine scale and bias shapes do not match the packed weight shape",
        ));
    }
    Ok(())
}

#[allow(clippy::too_many_arguments)]
fn validate_affine_quantized_matmul_arguments(
    operation: &'static str,
    activations: &MlxArray,
    quantized_weights: &MlxArray,
    scales: &MlxArray,
    biases: &MlxArray,
    transpose_weights: bool,
    group_size: i32,
    bits: i32,
) -> Result<(), MlxCError> {
    if !is_mlx_supported_affine_group_size(group_size) {
        return Err(runtime_operation_error(
            operation,
            "affine group size must be 32, 64, or 128",
        ));
    }
    if !is_mlx_supported_affine_bit_width(bits) {
        return Err(runtime_operation_error(
            operation,
            "affine bit width must be 2, 3, 4, 5, 6, or 8",
        ));
    }
    if !is_supported_quantized_float_dtype(activations.dtype()) {
        return Err(runtime_operation_error(
            operation,
            "activation dtype must be float16, bfloat16, or float32",
        ));
    }
    if quantized_weights.dtype() != MlxDtype::UInt32 {
        return Err(runtime_operation_error(
            operation,
            "quantized weights must have uint32 dtype",
        ));
    }
    if !is_supported_quantized_float_dtype(scales.dtype()) {
        return Err(runtime_operation_error(
            operation,
            "affine scales must be float16, bfloat16, or float32",
        ));
    }
    if !is_supported_quantized_float_dtype(biases.dtype()) {
        return Err(runtime_operation_error(
            operation,
            "affine biases must be float16, bfloat16, or float32",
        ));
    }

    let activation_shape = activations.shape();
    let weight_shape = quantized_weights.shape();
    let scale_shape = scales.shape();
    let bias_shape = biases.shape();
    if activation_shape.len() < 2 || weight_shape.len() < 2 {
        return Err(runtime_operation_error(
            operation,
            "activation and weight arrays must both have rank at least two",
        ));
    }
    if scale_shape != bias_shape {
        return Err(runtime_operation_error(
            operation,
            "affine scales and biases must have the same shape",
        ));
    }

    let activation_inner_dimension = positive_last_dimension(&activation_shape, operation)?;
    let packed_weight_tail_dimension = positive_last_dimension(&weight_shape, operation)?;
    let weight_row_dimension = positive_second_last_dimension(&weight_shape, operation)?;
    let expanded_tail_dimension = packed_weight_tail_dimension
        .checked_mul(32)
        .and_then(|packed_bits| (packed_bits % bits == 0).then_some(packed_bits / bits))
        .ok_or_else(|| {
            runtime_operation_error(
                operation,
                "packed weight shape overflows or is incompatible with the requested bit width",
            )
        })?;

    let scale_group_count = if transpose_weights {
        if activation_inner_dimension != expanded_tail_dimension {
            return Err(runtime_operation_error(
                operation,
                "activation inner dimension does not match the expanded quantized weight input",
            ));
        }
        exact_group_count(expanded_tail_dimension, group_size, operation)?
    } else {
        if activation_inner_dimension != weight_row_dimension {
            return Err(runtime_operation_error(
                operation,
                "activation inner dimension does not match the untransposed quantized weight rows",
            ));
        }
        exact_group_count(expanded_tail_dimension, group_size, operation)?
    };
    let mut expected_scale_shape = weight_shape;
    let expected_scale_tail_position = expected_scale_shape.len() - 1;
    expected_scale_shape[expected_scale_tail_position] = scale_group_count;
    if scale_shape != expected_scale_shape {
        return Err(runtime_operation_error(
            operation,
            "affine scale and bias shapes do not match the packed weight shape",
        ));
    }
    Ok(())
}

const fn is_mlx_supported_affine_group_size(group_size: i32) -> bool {
    matches!(group_size, 32 | 64 | 128)
}

const fn is_mlx_supported_affine_bit_width(bits: i32) -> bool {
    matches!(bits, 2 | 3 | 4 | 5 | 6 | 8)
}

fn validate_gather_quantized_matmul_indices(
    lhs_indices: Option<&MlxArray>,
    rhs_indices: Option<&MlxArray>,
) -> Result<(), MlxCError> {
    const OPERATION: &str = "build affine gather_qmm";
    if lhs_indices.is_none() && rhs_indices.is_none() {
        return Err(runtime_operation_error(
            OPERATION,
            "gather_qmm requires at least one index array",
        ));
    }
    if let Some(lhs_index_array) = lhs_indices
        && !is_integral_index_dtype(lhs_index_array.dtype())
    {
        return Err(runtime_operation_error(
            OPERATION,
            "lhs indices must have an integral dtype",
        ));
    }
    if let Some(rhs_index_array) = rhs_indices
        && !is_integral_index_dtype(rhs_index_array.dtype())
    {
        return Err(runtime_operation_error(
            OPERATION,
            "rhs indices must have an integral dtype",
        ));
    }
    Ok(())
}

fn positive_last_dimension(shape: &[i32], operation: &'static str) -> Result<i32, MlxCError> {
    shape
        .last()
        .copied()
        .filter(|dimension| *dimension > 0)
        .ok_or_else(|| {
            runtime_operation_error(operation, "array shape must have a positive tail dimension")
        })
}

fn positive_second_last_dimension(
    shape: &[i32],
    operation: &'static str,
) -> Result<i32, MlxCError> {
    shape
        .get(shape.len().saturating_sub(2))
        .copied()
        .filter(|dimension| *dimension > 0)
        .ok_or_else(|| {
            runtime_operation_error(operation, "array shape must have a positive row dimension")
        })
}

fn exact_group_count(
    expanded_dimension: i32,
    group_size: i32,
    operation: &'static str,
) -> Result<i32, MlxCError> {
    if expanded_dimension % group_size != 0 {
        return Err(runtime_operation_error(
            operation,
            "expanded quantized dimension must be divisible by the affine group size",
        ));
    }
    Ok(expanded_dimension / group_size)
}

fn is_supported_quantized_float_dtype(dtype: MlxDtype) -> bool {
    matches!(
        dtype,
        MlxDtype::Float16 | MlxDtype::Float32 | MlxDtype::BFloat16
    )
}

fn is_integral_index_dtype(dtype: MlxDtype) -> bool {
    matches!(
        dtype,
        MlxDtype::UInt8
            | MlxDtype::UInt16
            | MlxDtype::UInt32
            | MlxDtype::UInt64
            | MlxDtype::Int8
            | MlxDtype::Int16
            | MlxDtype::Int32
            | MlxDtype::Int64
    )
}

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