mlx-native 0.3.2

Pure-Rust Metal GPU compute library for MLX-compatible inference on Apple Silicon
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
//! Tests for the GraphExecutor single-encoder dispatch pattern.
//!
//! Proves that encoding multiple ops through a single GraphSession (one
//! CommandEncoder, one commit_and_wait) produces the same results as
//! dispatching each op with its own encoder, AND is measurably faster.

#![allow(clippy::expect_used, clippy::unwrap_used, clippy::panic)]
#![cfg(target_vendor = "apple")]

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

// --------------------------------------------------------------------------
// Helpers
// --------------------------------------------------------------------------

fn pseudo_random_f32(seed: u64, n: usize) -> Vec<f32> {
    let mut state = seed;
    (0..n)
        .map(|_| {
            state = state
                .wrapping_mul(6364136223846793005)
                .wrapping_add(1442695040888963407);
            let frac = ((state >> 33) as f32) / (u32::MAX as f32) - 0.5;
            frac
        })
        .collect()
}

fn setup() -> (MlxDevice, KernelRegistry) {
    let device = MlxDevice::new().expect("MlxDevice::new");
    let registry = KernelRegistry::new();
    (device, registry)
}

// --------------------------------------------------------------------------
// Test 1: Single op through GraphSession matches direct dispatch
// --------------------------------------------------------------------------

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

    let n = 1024;
    let byte_len = n * std::mem::size_of::<f32>();
    let a_data = pseudo_random_f32(42, n);
    let b_data = pseudo_random_f32(99, n);

    // -- Direct dispatch (baseline) --
    let mut a_buf = device.alloc_buffer(byte_len, DType::F32, vec![n]).expect("a");
    let mut b_buf = device.alloc_buffer(byte_len, DType::F32, vec![n]).expect("b");
    let out_direct = device.alloc_buffer(byte_len, DType::F32, vec![n]).expect("out");

    a_buf.as_mut_slice::<f32>().expect("a").copy_from_slice(&a_data);
    b_buf.as_mut_slice::<f32>().expect("b").copy_from_slice(&b_data);

    {
        let mut encoder = device.command_encoder().expect("enc");
        mlx_native::ops::elementwise::elementwise_add(
            &mut encoder,
            &mut registry,
            device.metal_device(),
            &a_buf,
            &b_buf,
            &out_direct,
            n,
            DType::F32,
        )
        .expect("direct add");
        encoder.commit_and_wait().expect("commit");
    }

    let direct_result: Vec<f32> = out_direct.as_slice::<f32>().expect("read").to_vec();

    // -- GraphSession dispatch --
    let out_graph = device.alloc_buffer(byte_len, DType::F32, vec![n]).expect("out");

    let executor = GraphExecutor::new(MlxDevice::new().expect("device2"));
    {
        let mut session = executor.begin().expect("begin");
        session
            .elementwise_add(
                &mut registry,
                device.metal_device(),
                &a_buf,
                &b_buf,
                &out_graph,
                n,
                DType::F32,
            )
            .expect("graph add");
        session.finish().expect("finish");
    }

    let graph_result: Vec<f32> = out_graph.as_slice::<f32>().expect("read").to_vec();

    // -- Compare --
    for i in 0..n {
        let diff = (direct_result[i] - graph_result[i]).abs();
        assert!(
            diff < 1e-6,
            "Mismatch at {i}: direct={}, graph={}, diff={diff}",
            direct_result[i],
            graph_result[i]
        );
    }
}

// --------------------------------------------------------------------------
// Test 2: Multi-op sequence through single GraphSession
// --------------------------------------------------------------------------

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

    let n = 512;
    let byte_len = n * std::mem::size_of::<f32>();
    let a_data = pseudo_random_f32(100, n);
    let b_data = pseudo_random_f32(200, n);
    let c_data = pseudo_random_f32(300, n);

    let mut a_buf = device.alloc_buffer(byte_len, DType::F32, vec![n]).expect("a");
    let mut b_buf = device.alloc_buffer(byte_len, DType::F32, vec![n]).expect("b");
    let mut c_buf = device.alloc_buffer(byte_len, DType::F32, vec![n]).expect("c");

    a_buf.as_mut_slice::<f32>().expect("a").copy_from_slice(&a_data);
    b_buf.as_mut_slice::<f32>().expect("b").copy_from_slice(&b_data);
    c_buf.as_mut_slice::<f32>().expect("c").copy_from_slice(&c_data);

    // -- Sequential dispatch: each op gets own encoder + commit_and_wait --
    let tmp_seq = device.alloc_buffer(byte_len, DType::F32, vec![n]).expect("tmp");
    let out_seq = device.alloc_buffer(byte_len, DType::F32, vec![n]).expect("out");

    // Step 1: tmp = a + b
    {
        let mut enc = device.command_encoder().expect("enc");
        mlx_native::ops::elementwise::elementwise_add(
            &mut enc,
            &mut registry,
            device.metal_device(),
            &a_buf,
            &b_buf,
            &tmp_seq,
            n,
            DType::F32,
        )
        .expect("add");
        enc.commit_and_wait().expect("commit");
    }

    // Step 2: out = tmp * c
    {
        let mut enc = device.command_encoder().expect("enc");
        mlx_native::ops::elementwise::elementwise_mul(
            &mut enc,
            &mut registry,
            device.metal_device(),
            &tmp_seq,
            &c_buf,
            &out_seq,
            n,
            DType::F32,
        )
        .expect("mul");
        enc.commit_and_wait().expect("commit");
    }

    let seq_result: Vec<f32> = out_seq.as_slice::<f32>().expect("read").to_vec();

    // -- Batched dispatch: all ops in one GraphSession --
    let tmp_graph = device.alloc_buffer(byte_len, DType::F32, vec![n]).expect("tmp");
    let out_graph = device.alloc_buffer(byte_len, DType::F32, vec![n]).expect("out");

    let executor = GraphExecutor::new(MlxDevice::new().expect("device2"));
    {
        let mut session = executor.begin().expect("begin");

        // Step 1: tmp = a + b (same encoder)
        session
            .elementwise_add(
                &mut registry,
                device.metal_device(),
                &a_buf,
                &b_buf,
                &tmp_graph,
                n,
                DType::F32,
            )
            .expect("graph add");

        // Barrier: tmp_graph was written by add, will be read by mul.
        session.barrier();

        // Step 2: out = tmp * c (same encoder, NO intermediate commit_and_wait)
        session
            .elementwise_mul(
                &mut registry,
                device.metal_device(),
                &tmp_graph,
                &c_buf,
                &out_graph,
                n,
                DType::F32,
            )
            .expect("graph mul");

        // Single sync point
        session.finish().expect("finish");
    }

    let graph_result: Vec<f32> = out_graph.as_slice::<f32>().expect("read").to_vec();

    // -- Compare: results must match --
    for i in 0..n {
        let diff = (seq_result[i] - graph_result[i]).abs();
        assert!(
            diff < 1e-5,
            "Mismatch at {i}: sequential={}, graph={}, diff={diff}",
            seq_result[i],
            graph_result[i]
        );
    }
}

// --------------------------------------------------------------------------
// Test 3: Microbench — sequential vs batched dispatch speed
// --------------------------------------------------------------------------

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

    // Simulate 30 decoder layers of elementwise add (cheap ops to isolate
    // dispatch overhead). Using hf2q's actual hidden size: 4096.
    let dim = 4096;
    let n_layers = 30;
    let n_ops_per_layer = 4; // add, mul, add, mul — simulating residual + scaling
    let total_ops = n_layers * n_ops_per_layer;
    let byte_len = dim * std::mem::size_of::<f32>();
    let iterations = 100;

    // Pre-allocate all buffers.
    let mut bufs = Vec::new();
    for i in 0..3 {
        let mut buf = device
            .alloc_buffer(byte_len, DType::F32, vec![dim])
            .expect("alloc");
        let data = pseudo_random_f32(i as u64, dim);
        buf.as_mut_slice::<f32>().expect("write").copy_from_slice(&data);
        bufs.push(buf);
    }

    // Warm up both paths.
    for _ in 0..3 {
        // Sequential warm-up
        for _ in 0..total_ops {
            let mut enc = device.command_encoder().expect("enc");
            mlx_native::ops::elementwise::elementwise_add(
                &mut enc,
                &mut registry,
                device.metal_device(),
                &bufs[0],
                &bufs[1],
                &bufs[2],
                dim,
                DType::F32,
            )
            .expect("add");
            enc.commit_and_wait().expect("commit");
        }

        // Batched warm-up
        let executor = GraphExecutor::new(MlxDevice::new().expect("dev"));
        let mut session = executor.begin().expect("begin");
        for _ in 0..total_ops {
            session
                .elementwise_add(
                    &mut registry,
                    device.metal_device(),
                    &bufs[0],
                    &bufs[1],
                    &bufs[2],
                    dim,
                    DType::F32,
                )
                .expect("add");
        }
        session.finish().expect("finish");
    }

    // -- Measure sequential --
    let mut seq_times = Vec::with_capacity(iterations);
    for _ in 0..iterations {
        let start = std::time::Instant::now();
        for _ in 0..total_ops {
            let mut enc = device.command_encoder().expect("enc");
            mlx_native::ops::elementwise::elementwise_add(
                &mut enc,
                &mut registry,
                device.metal_device(),
                &bufs[0],
                &bufs[1],
                &bufs[2],
                dim,
                DType::F32,
            )
            .expect("add");
            enc.commit_and_wait().expect("commit");
        }
        seq_times.push(start.elapsed());
    }

    // -- Measure batched --
    let mut batch_times = Vec::with_capacity(iterations);
    for _ in 0..iterations {
        let executor = GraphExecutor::new(MlxDevice::new().expect("dev"));
        let start = std::time::Instant::now();
        let mut session = executor.begin().expect("begin");
        for _ in 0..total_ops {
            session
                .elementwise_add(
                    &mut registry,
                    device.metal_device(),
                    &bufs[0],
                    &bufs[1],
                    &bufs[2],
                    dim,
                    DType::F32,
                )
                .expect("add");
        }
        session.finish().expect("finish");
        batch_times.push(start.elapsed());
    }

    // Sort and take median.
    seq_times.sort();
    batch_times.sort();
    let seq_median = seq_times[iterations / 2];
    let batch_median = batch_times[iterations / 2];

    let seq_us = seq_median.as_micros();
    let batch_us = batch_median.as_micros();
    let speedup = seq_us as f64 / batch_us as f64;

    println!("=== GraphExecutor Microbench ===");
    println!("  Ops per iteration: {total_ops} (simulating {n_layers} decoder layers)");
    println!("  Iterations: {iterations}");
    println!("  Sequential (per-op encoder+commit): {seq_us} us (median)");
    println!("  Batched (single encoder+commit):    {batch_us} us (median)");
    println!("  Speedup: {speedup:.2}x");
    println!("================================");

    // The batched path should be faster. We assert at least 1.5x speedup to
    // avoid flaky failures on loaded CI, but real hardware typically shows 5-20x.
    assert!(
        speedup > 1.5,
        "Expected batched dispatch to be at least 1.5x faster, got {speedup:.2}x \
         (seq={seq_us}us, batch={batch_us}us)"
    );
}

// --------------------------------------------------------------------------
// Test 4: Session drop without commit does not crash
// --------------------------------------------------------------------------

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

    let n = 256;
    let byte_len = n * std::mem::size_of::<f32>();
    let mut a_buf = device.alloc_buffer(byte_len, DType::F32, vec![n]).expect("a");
    let mut b_buf = device.alloc_buffer(byte_len, DType::F32, vec![n]).expect("b");
    let out_buf = device.alloc_buffer(byte_len, DType::F32, vec![n]).expect("out");

    a_buf
        .as_mut_slice::<f32>()
        .expect("a")
        .copy_from_slice(&pseudo_random_f32(1, n));
    b_buf
        .as_mut_slice::<f32>()
        .expect("b")
        .copy_from_slice(&pseudo_random_f32(2, n));

    let executor = GraphExecutor::new(MlxDevice::new().expect("dev"));

    // Encode ops but intentionally drop the session without calling finish().
    // This should not panic or leave the GPU in a bad state.
    {
        let mut session = executor.begin().expect("begin");
        session
            .elementwise_add(
                &mut registry,
                device.metal_device(),
                &a_buf,
                &b_buf,
                &out_buf,
                n,
                DType::F32,
            )
            .expect("add");
        // Intentional: no session.finish() — dropped here.
    }

    // Verify the GPU is still usable after the abandoned session.
    let mut enc = device.command_encoder().expect("enc after drop");
    enc.commit_and_wait().expect("commit after drop");
}

// --------------------------------------------------------------------------
// Test 5: encoder_mut() escape hatch works
// --------------------------------------------------------------------------

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

    let n = 128;
    let byte_len = n * std::mem::size_of::<f32>();
    let mut a_buf = device.alloc_buffer(byte_len, DType::F32, vec![n]).expect("a");
    let mut b_buf = device.alloc_buffer(byte_len, DType::F32, vec![n]).expect("b");
    let out_buf = device.alloc_buffer(byte_len, DType::F32, vec![n]).expect("out");

    let a_data = pseudo_random_f32(77, n);
    let b_data = pseudo_random_f32(88, n);
    a_buf.as_mut_slice::<f32>().expect("a").copy_from_slice(&a_data);
    b_buf.as_mut_slice::<f32>().expect("b").copy_from_slice(&b_data);

    let executor = GraphExecutor::new(MlxDevice::new().expect("dev"));
    let mut session = executor.begin().expect("begin");

    // Use encoder_mut() to call the op directly.
    mlx_native::ops::elementwise::elementwise_add(
        session.encoder_mut(),
        &mut registry,
        device.metal_device(),
        &a_buf,
        &b_buf,
        &out_buf,
        n,
        DType::F32,
    )
    .expect("direct add via encoder_mut");

    session.finish().expect("finish");

    let result: Vec<f32> = out_buf.as_slice::<f32>().expect("read").to_vec();
    for i in 0..n {
        let expected = a_data[i] + b_data[i];
        let diff = (result[i] - expected).abs();
        assert!(
            diff < 1e-6,
            "Mismatch at {i}: expected={expected}, got={}, diff={diff}",
            result[i]
        );
    }
}