mlx-native 0.10.2

Pure-Rust Metal GPU compute library for MLX-compatible inference on Apple Silicon
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
//! Tests for the Rotary Position Embedding (RoPE) GPU kernel.

#![allow(clippy::expect_used, clippy::unwrap_used, clippy::panic)]

use mlx_native::{DType, KernelRegistry, MlxDevice};

/// Reference RoPE implementation in pure Rust.
///
/// Processes a flat `[seq_len, head_dim]` array with given positions and theta.
fn rope_ref(input: &[f32], positions: &[u32], head_dim: usize, theta: f32) -> Vec<f32> {
    let seq_len = positions.len();
    let half_dim = head_dim / 2;
    let mut output = vec![0.0f32; seq_len * head_dim];

    for s in 0..seq_len {
        let pos = positions[s] as f32;
        for p in 0..half_dim {
            let dim_ratio = (2 * p) as f32 / head_dim as f32;
            let freq = 1.0 / theta.powf(dim_ratio);
            let angle = pos * freq;
            let cos_a = angle.cos();
            let sin_a = angle.sin();

            let base = s * head_dim + 2 * p;
            let x0 = input[base];
            let x1 = input[base + 1];

            output[base] = x0 * cos_a - x1 * sin_a;
            output[base + 1] = x0 * sin_a + x1 * cos_a;
        }
    }
    output
}

fn setup() -> (MlxDevice, KernelRegistry) {
    let device = MlxDevice::new().expect("MlxDevice::new");
    let mut registry = KernelRegistry::new();
    mlx_native::ops::rope::register(&mut registry);
    (device, registry)
}

fn run_low_precision_long_theta(dtype: DType) {
    let (device, mut registry) = setup();
    let theta = 1_000_000.0_f32;
    let seq_len = 1_u32;
    let head_dim = 16_u32;
    let input_data: Vec<f32> = (0..head_dim as usize)
        .map(|i| ((i as f32) * 0.3).sin())
        .collect();
    let positions = [2048_u32];

    let mut input = device
        .alloc_buffer(
            head_dim as usize * dtype.size_of(),
            dtype,
            vec![1, head_dim as usize],
        )
        .expect("alloc low-precision input");
    let output = device
        .alloc_buffer(
            head_dim as usize * dtype.size_of(),
            dtype,
            vec![1, head_dim as usize],
        )
        .expect("alloc low-precision output");
    match dtype {
        DType::F16 => input
            .as_mut_slice::<half::f16>()
            .expect("f16 input")
            .iter_mut()
            .zip(&input_data)
            .for_each(|(dst, &src)| *dst = half::f16::from_f32(src)),
        DType::BF16 => input
            .as_mut_slice::<half::bf16>()
            .expect("bf16 input")
            .iter_mut()
            .zip(&input_data)
            .for_each(|(dst, &src)| *dst = half::bf16::from_f32(src)),
        other => panic!("unexpected low-precision dtype {other}"),
    }

    let mut params = device
        .alloc_buffer(16, DType::F32, vec![4])
        .expect("alloc params");
    params
        .as_mut_slice::<f32>()
        .expect("params")
        .copy_from_slice(&[theta, head_dim as f32, 0.0, 0.0]);
    let mut positions_buf = device
        .alloc_buffer(4, DType::U32, vec![1])
        .expect("alloc positions");
    positions_buf.as_mut_slice::<u32>().expect("positions")[0] = positions[0];

    let mut encoder = device.command_encoder().expect("encoder");
    mlx_native::ops::rope::dispatch_rope(
        &mut encoder,
        &mut registry,
        device.metal_device(),
        &input,
        &output,
        &params,
        &positions_buf,
        seq_len,
        head_dim,
    )
    .expect("dispatch low-precision rope");
    encoder.commit_and_wait().expect("commit");

    let quantized_input: Vec<f32> = match dtype {
        DType::F16 => input
            .as_slice::<half::f16>()
            .expect("read f16 input")
            .iter()
            .map(|v| v.to_f32())
            .collect(),
        DType::BF16 => input
            .as_slice::<half::bf16>()
            .expect("read bf16 input")
            .iter()
            .map(|v| v.to_f32())
            .collect(),
        _ => unreachable!(),
    };
    let expected = rope_ref(&quantized_input, &positions, head_dim as usize, theta);
    let actual: Vec<f32> = match dtype {
        DType::F16 => output
            .as_slice::<half::f16>()
            .expect("read f16 output")
            .iter()
            .map(|v| v.to_f32())
            .collect(),
        DType::BF16 => output
            .as_slice::<half::bf16>()
            .expect("read bf16 output")
            .iter()
            .map(|v| v.to_f32())
            .collect(),
        _ => unreachable!(),
    };
    let tolerance = if dtype == DType::F16 { 1.0e-3 } else { 8.0e-3 };
    for (index, (&got, &want)) in actual.iter().zip(&expected).enumerate() {
        assert!(
            (got - want).abs() <= tolerance,
            "{dtype} long-theta RoPE mismatch at {index}: expected={want}, got={got}"
        );
    }
}

#[test]
fn test_rope_f16_theta_1000000() {
    run_low_precision_long_theta(DType::F16);
}

#[test]
fn test_rope_bf16_theta_1000000() {
    run_low_precision_long_theta(DType::BF16);
}

#[test]
fn test_rope_f32_theta_10000() {
    let (device, mut registry) = setup();
    let theta = 10000.0_f32;
    let seq_len: u32 = 4;
    let head_dim: u32 = 8;
    let n = (seq_len as usize) * (head_dim as usize);

    // Generate deterministic input data
    let input_data: Vec<f32> = (0..n).map(|i| (i as f32) * 0.1 - 1.6).collect();
    let positions: Vec<u32> = (0..seq_len).collect();

    let byte_len = n * std::mem::size_of::<f32>();
    let mut input_buf = device
        .alloc_buffer(byte_len, DType::F32, vec![seq_len as usize, head_dim as usize])
        .expect("alloc input");
    let output_buf = device
        .alloc_buffer(byte_len, DType::F32, vec![seq_len as usize, head_dim as usize])
        .expect("alloc output");

    // Params: [theta, head_dim, 0, 0]
    let params_byte_len = 4 * std::mem::size_of::<f32>();
    let mut params_buf = device
        .alloc_buffer(params_byte_len, DType::F32, vec![4])
        .expect("alloc params");

    // Positions buffer
    let pos_byte_len = (seq_len as usize) * std::mem::size_of::<u32>();
    let mut positions_buf = device
        .alloc_buffer(pos_byte_len, DType::U32, vec![seq_len as usize])
        .expect("alloc positions");

    // Write data
    {
        let slice: &mut [f32] = input_buf.as_mut_slice().expect("as_mut_slice");
        slice.copy_from_slice(&input_data);
    }
    {
        let slice: &mut [f32] = params_buf.as_mut_slice().expect("as_mut_slice");
        slice[0] = theta;
        slice[1] = head_dim as f32;
        slice[2] = 0.0;
        slice[3] = 0.0;
    }
    {
        let slice: &mut [u32] = positions_buf.as_mut_slice().expect("as_mut_slice");
        slice.copy_from_slice(&positions);
    }

    let mut encoder = device.command_encoder().expect("command_encoder");
    mlx_native::ops::rope::dispatch_rope(
        &mut encoder,
        &mut registry,
        device.metal_device(),
        &input_buf,
        &output_buf,
        &params_buf,
        &positions_buf,
        seq_len,
        head_dim,
    )
    .expect("dispatch_rope");
    encoder.commit_and_wait().expect("commit_and_wait");

    let expected = rope_ref(&input_data, &positions, head_dim as usize, theta);
    let output: &[f32] = output_buf.as_slice().expect("as_slice");

    for i in 0..n {
        let diff = (output[i] - expected[i]).abs();
        assert!(
            diff <= 1e-5,
            "RoPE f32 theta=10000 mismatch at index {}: expected={}, got={}, diff={}",
            i, expected[i], output[i], diff
        );
    }
}

#[test]
fn test_rope_f32_theta_1000000() {
    let (device, mut registry) = setup();
    let theta = 1000000.0_f32;
    let seq_len: u32 = 4;
    let head_dim: u32 = 16;
    let n = (seq_len as usize) * (head_dim as usize);

    let input_data: Vec<f32> = (0..n).map(|i| ((i as f32) * 0.3).sin()).collect();
    let positions: Vec<u32> = vec![0, 100, 500, 2048];

    let byte_len = n * std::mem::size_of::<f32>();
    let mut input_buf = device
        .alloc_buffer(byte_len, DType::F32, vec![seq_len as usize, head_dim as usize])
        .expect("alloc input");
    let output_buf = device
        .alloc_buffer(byte_len, DType::F32, vec![seq_len as usize, head_dim as usize])
        .expect("alloc output");

    let params_byte_len = 4 * std::mem::size_of::<f32>();
    let mut params_buf = device
        .alloc_buffer(params_byte_len, DType::F32, vec![4])
        .expect("alloc params");

    let pos_byte_len = (seq_len as usize) * std::mem::size_of::<u32>();
    let mut positions_buf = device
        .alloc_buffer(pos_byte_len, DType::U32, vec![seq_len as usize])
        .expect("alloc positions");

    {
        let slice: &mut [f32] = input_buf.as_mut_slice().expect("as_mut_slice");
        slice.copy_from_slice(&input_data);
    }
    {
        let slice: &mut [f32] = params_buf.as_mut_slice().expect("as_mut_slice");
        slice[0] = theta;
        slice[1] = head_dim as f32;
        slice[2] = 0.0;
        slice[3] = 0.0;
    }
    {
        let slice: &mut [u32] = positions_buf.as_mut_slice().expect("as_mut_slice");
        slice.copy_from_slice(&positions);
    }

    let mut encoder = device.command_encoder().expect("command_encoder");
    mlx_native::ops::rope::dispatch_rope(
        &mut encoder,
        &mut registry,
        device.metal_device(),
        &input_buf,
        &output_buf,
        &params_buf,
        &positions_buf,
        seq_len,
        head_dim,
    )
    .expect("dispatch_rope");
    encoder.commit_and_wait().expect("commit_and_wait");

    let expected = rope_ref(&input_data, &positions, head_dim as usize, theta);
    let output: &[f32] = output_buf.as_slice().expect("as_slice");

    for i in 0..n {
        let diff = (output[i] - expected[i]).abs();
        assert!(
            diff <= 1e-5,
            "RoPE f32 theta=1000000 mismatch at index {}: expected={}, got={}, diff={}",
            i, expected[i], output[i], diff
        );
    }
}

#[test]
fn test_rope_f32_position_zero() {
    // At position 0, cos(0)=1, sin(0)=0, so output should equal input.
    let (device, mut registry) = setup();
    let theta = 10000.0_f32;
    let seq_len: u32 = 1;
    let head_dim: u32 = 4;
    let n = (seq_len as usize) * (head_dim as usize);

    let input_data: Vec<f32> = vec![1.0, 2.0, 3.0, 4.0];

    let byte_len = n * std::mem::size_of::<f32>();
    let mut input_buf = device
        .alloc_buffer(byte_len, DType::F32, vec![1, 4])
        .expect("alloc input");
    let output_buf = device
        .alloc_buffer(byte_len, DType::F32, vec![1, 4])
        .expect("alloc output");

    let params_byte_len = 4 * std::mem::size_of::<f32>();
    let mut params_buf = device
        .alloc_buffer(params_byte_len, DType::F32, vec![4])
        .expect("alloc params");

    let pos_byte_len = std::mem::size_of::<u32>();
    let mut positions_buf = device
        .alloc_buffer(pos_byte_len, DType::U32, vec![1])
        .expect("alloc positions");

    {
        let slice: &mut [f32] = input_buf.as_mut_slice().expect("as_mut_slice");
        slice.copy_from_slice(&input_data);
    }
    {
        let slice: &mut [f32] = params_buf.as_mut_slice().expect("as_mut_slice");
        slice[0] = theta;
        slice[1] = head_dim as f32;
        slice[2] = 0.0;
        slice[3] = 0.0;
    }
    {
        let slice: &mut [u32] = positions_buf.as_mut_slice().expect("as_mut_slice");
        slice[0] = 0;
    }

    let mut encoder = device.command_encoder().expect("command_encoder");
    mlx_native::ops::rope::dispatch_rope(
        &mut encoder,
        &mut registry,
        device.metal_device(),
        &input_buf,
        &output_buf,
        &params_buf,
        &positions_buf,
        seq_len,
        head_dim,
    )
    .expect("dispatch_rope");
    encoder.commit_and_wait().expect("commit_and_wait");

    let output: &[f32] = output_buf.as_slice().expect("as_slice");
    for i in 0..n {
        let diff = (output[i] - input_data[i]).abs();
        assert!(
            diff <= 1e-5,
            "RoPE at position 0 should equal input: index {}, expected={}, got={}",
            i, input_data[i], output[i]
        );
    }
}

#[test]
fn test_rope_invalid_odd_head_dim() {
    let (device, mut registry) = setup();

    let input_buf = device
        .alloc_buffer(12, DType::F32, vec![1, 3])
        .expect("alloc input");
    let output_buf = device
        .alloc_buffer(12, DType::F32, vec![1, 3])
        .expect("alloc output");
    let params_buf = device
        .alloc_buffer(16, DType::F32, vec![4])
        .expect("alloc params");
    let positions_buf = device
        .alloc_buffer(4, DType::U32, vec![1])
        .expect("alloc positions");

    let mut encoder = device.command_encoder().expect("command_encoder");
    let result = mlx_native::ops::rope::dispatch_rope(
        &mut encoder,
        &mut registry,
        device.metal_device(),
        &input_buf,
        &output_buf,
        &params_buf,
        &positions_buf,
        1,
        3, // odd head_dim
    );
    assert!(result.is_err(), "Should error on odd head_dim");
}