mlx-rs 0.34.0

Unofficial rust wrapper for Apple's mlx machine learning library.
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 std::{cell::RefCell, ffi::CStr, thread::LocalKey};

use crate::{
    device::Device,
    error::Result,
    utils::{guard::Guarded, SUCCESS},
};

thread_local! {
    static THREAD_LOCAL_DEFAULT_STREAM: RefCell<Option<Stream>> = const { RefCell::new(None) };
}

struct ScopedValueGuard<T: 'static> {
    local: &'static LocalKey<RefCell<Option<T>>>,
    previous: Option<T>,
}

impl<T: 'static> Drop for ScopedValueGuard<T> {
    fn drop(&mut self) {
        self.local.with_borrow_mut(|stream| {
            *stream = self.previous.take();
        });
    }
}

fn with_scoped_value<T: 'static, R>(
    local: &'static LocalKey<RefCell<Option<T>>>,
    value: T,
    f: impl FnOnce() -> R,
) -> R {
    let previous = local.with_borrow_mut(|current| current.replace(value));
    let _guard = ScopedValueGuard { local, previous };
    f()
}

/// Gets the thread-local scoped default stream.
///
/// The value does not propagate across asynchronous task suspension or between operating-system
/// threads.
pub fn thread_local_default_stream() -> Option<Stream> {
    THREAD_LOCAL_DEFAULT_STREAM.with_borrow(|s| s.clone())
}

/// Uses `stream` for operations constructed during `f`.
///
/// Scopes are synchronous, thread-local, nestable, and restore the previous stream if `f` panics.
/// To select a stream for one operation, put only that operation in the closure:
///
/// ```rust
/// use mlx_rs::{with_stream, Array, Stream};
///
/// let input = Array::from_slice(&[1.0_f32, 2.0, 3.0, 4.0], &[4]);
/// let stream = Stream::cpu();
/// let output = with_stream(&stream, || mlx_rs::fft::fft(&input, None, None)).unwrap();
/// assert_eq!(output.shape(), &[4]);
/// ```
pub fn with_stream<F, T>(stream: &Stream, f: F) -> T
where
    F: FnOnce() -> T,
{
    with_scoped_value(&THREAD_LOCAL_DEFAULT_STREAM, stream.clone(), f)
}

/// Uses the default stream on `device` for operations constructed during `f`.
///
/// This is equivalent to creating a stream for the device and passing it to [`with_stream`].
/// Scopes are synchronous, thread-local, nestable, and panic-safe.
pub fn with_device<F, T>(device: Device, f: F) -> T
where
    F: FnOnce() -> T,
{
    let stream = Stream::new_with_device(&device);
    with_stream(&stream, f)
}

/// Gets the thread-local scoped default stream.
#[deprecated(since = "0.26.0", note = "use `thread_local_default_stream`")]
pub fn task_local_default_stream() -> Option<Stream> {
    thread_local_default_stream()
}

/// Uses a given default stream for the duration of `f`.
#[deprecated(since = "0.26.0", note = "use `with_stream(&stream, f)`")]
pub fn with_new_default_stream<F, T>(default_stream: Stream, f: F) -> T
where
    F: FnOnce() -> T,
{
    with_stream(&default_stream, f)
}

/// Parameter type for all MLX operations.
///
/// Use this to control where operations are evaluated:
///
/// If omitted it will use the [Default::default()], which will be [Device::gpu()] unless
/// set otherwise.
#[derive(PartialEq)]
pub struct StreamOrDevice {
    pub(crate) stream: Stream,
}

impl StreamOrDevice {
    /// Create a new [`StreamOrDevice`] with a [`Stream`].
    pub fn new(stream: Stream) -> StreamOrDevice {
        StreamOrDevice { stream }
    }

    /// Create a new [`StreamOrDevice`] with a [`Device`].
    pub fn new_with_device(device: &Device) -> StreamOrDevice {
        StreamOrDevice {
            stream: Stream::new_with_device(device),
        }
    }

    /// Current default CPU stream.
    pub fn cpu() -> StreamOrDevice {
        StreamOrDevice {
            stream: Stream::cpu(),
        }
    }

    /// Current default GPU stream.
    pub fn gpu() -> StreamOrDevice {
        StreamOrDevice {
            stream: Stream::gpu(),
        }
    }
}

impl Default for StreamOrDevice {
    /// The default stream on the default device.
    ///
    /// Uses MLX's process-wide default device. For safe, thread-local operation
    /// selection, use [with_device] or [with_stream].
    fn default() -> Self {
        Self {
            stream: Stream::new(),
        }
    }
}

impl AsRef<Stream> for StreamOrDevice {
    fn as_ref(&self) -> &Stream {
        &self.stream
    }
}

impl std::fmt::Debug for StreamOrDevice {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{}", self.stream)
    }
}

impl std::fmt::Display for StreamOrDevice {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{}", self.stream)
    }
}

/// A stream of evaluation attached to a particular device.
///
/// Typically, this is used via the `stream:` parameter on a method with a [StreamOrDevice]:
pub struct Stream {
    pub(crate) c_stream: mlx_sys::mlx_stream,
}

impl AsRef<Stream> for Stream {
    fn as_ref(&self) -> &Stream {
        self
    }
}

impl Clone for Stream {
    fn clone(&self) -> Self {
        Stream::try_from_op(|res| unsafe { mlx_sys::mlx_stream_set(res, self.c_stream) })
            .expect("Failed to clone stream")
    }
}

impl Stream {
    /// Create a new stream on the default device, or return the thread-local
    /// default stream if present.
    pub fn thread_local_or_default() -> Self {
        thread_local_default_stream().unwrap_or_default()
    }

    /// Create a new stream on the default cpu device, or return the thread-local
    /// default stream if present.
    pub fn thread_local_or_cpu() -> Self {
        thread_local_default_stream().unwrap_or_else(Stream::cpu)
    }

    /// Create a new stream on the default gpu device, or return the thread-local
    /// default stream if present.
    pub fn thread_local_or_gpu() -> Self {
        thread_local_default_stream().unwrap_or_else(Stream::gpu)
    }

    /// Returns the thread-local scoped stream or the default stream.
    #[deprecated(since = "0.26.0", note = "use `Stream::thread_local_or_default`")]
    pub fn task_local_or_default() -> Self {
        Self::thread_local_or_default()
    }

    /// Returns the thread-local scoped stream or the CPU stream.
    #[deprecated(since = "0.26.0", note = "use `Stream::thread_local_or_cpu`")]
    pub fn task_local_or_cpu() -> Self {
        Self::thread_local_or_cpu()
    }

    /// Returns the thread-local scoped stream or the GPU stream.
    #[deprecated(since = "0.26.0", note = "use `Stream::thread_local_or_gpu`")]
    pub fn task_local_or_gpu() -> Self {
        Self::thread_local_or_gpu()
    }

    /// Create a new stream on the default device. Panics if fails.
    pub fn new() -> Stream {
        Self::try_default_on_device(&Device::default()).expect("Failed to create stream")
    }

    /// Try to get the default stream on the given device.
    pub fn try_default_on_device(device: &Device) -> Result<Stream> {
        Stream::try_from_op(|res| unsafe { mlx_sys::mlx_get_default_stream(res, device.c_device) })
    }

    /// Create a new stream on the given device
    pub fn new_with_device(device: &Device) -> Stream {
        crate::error::infallible(|| unsafe {
            let c_stream = mlx_sys::mlx_stream_new_device(device.c_device);
            Stream { c_stream }
        })
    }

    /// Get the underlying C pointer.
    pub fn as_ptr(&self) -> mlx_sys::mlx_stream {
        self.c_stream
    }

    /// Current default CPU stream.
    pub fn cpu() -> Self {
        crate::error::infallible(|| unsafe {
            let c_stream = mlx_sys::mlx_default_cpu_stream_new();
            Stream { c_stream }
        })
    }

    /// Current default GPU stream.
    pub fn gpu() -> Self {
        crate::error::infallible(|| unsafe {
            let c_stream = mlx_sys::mlx_default_gpu_stream_new();
            Stream { c_stream }
        })
    }

    /// Returns the device this stream executes on.
    pub fn device(&self) -> Result<Device> {
        Device::try_from_op(|res| unsafe { mlx_sys::mlx_stream_get_device(res, self.c_stream) })
    }

    /// Get the index of the stream.
    pub fn get_index(&self) -> Result<i32> {
        i32::try_from_op(|res| unsafe { mlx_sys::mlx_stream_get_index(res, self.c_stream) })
    }

    fn describe(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        unsafe {
            let mut mlx_str = mlx_sys::mlx_string_new();
            let result = match mlx_sys::mlx_stream_tostring(&mut mlx_str as *mut _, self.c_stream) {
                SUCCESS => {
                    let ptr = mlx_sys::mlx_string_data(mlx_str);
                    let c_str = CStr::from_ptr(ptr);
                    write!(f, "{}", c_str.to_string_lossy())
                }
                _ => Err(std::fmt::Error),
            };
            mlx_sys::mlx_string_free(mlx_str);
            result
        }
    }
}

impl Drop for Stream {
    fn drop(&mut self) {
        unsafe { mlx_sys::mlx_stream_free(self.c_stream) };
    }
}

impl Default for Stream {
    fn default() -> Self {
        Stream::new()
    }
}

impl std::fmt::Debug for Stream {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        self.describe(f)
    }
}

impl std::fmt::Display for Stream {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        self.describe(f)
    }
}

impl PartialEq for Stream {
    fn eq(&self, other: &Self) -> bool {
        unsafe { mlx_sys::mlx_stream_equal(self.c_stream, other.c_stream) }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn canonical_scopes_nest_and_restore_after_panic() {
        let outer = Stream::cpu();
        with_stream(&outer, || {
            assert_eq!(thread_local_default_stream(), Some(outer.clone()));

            let panic = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
                with_device(Device::cpu(), || panic!("scope panic"));
            }));

            assert!(panic.is_err());
            assert_eq!(thread_local_default_stream(), Some(outer.clone()));
        });

        assert!(thread_local_default_stream().is_none());
    }

    #[test]
    #[cfg(feature = "metal")]
    fn test_scoped_default_stream() {
        with_device(Device::cpu(), || {
            let cpu_stream = Stream::thread_local_or_default();
            let task_default_stream = Stream::gpu();
            with_stream(&task_default_stream, || {
                let task_local_stream_0 = Stream::thread_local_or_default();
                let task_local_stream_1 = Stream::thread_local_or_default();
                assert_eq!(task_local_stream_0, task_local_stream_1);
                assert_ne!(task_local_stream_0, cpu_stream);
            });
            assert_eq!(Stream::thread_local_or_default(), cpu_stream);
        });
    }

    #[test]
    fn test_scoped_default_stream_restored_after_panic() {
        let cpu = Device::cpu();
        let outer_stream = Stream::new_with_device(&cpu);
        with_stream(&outer_stream, || {
            let panic = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
                let inner = Stream::new_with_device(&cpu);
                with_stream(&inner, || panic!("stream panic"));
            }));

            assert!(panic.is_err());
            assert_eq!(thread_local_default_stream(), Some(outer_stream.clone()));
        });

        assert!(thread_local_default_stream().is_none());
    }

    #[test]
    fn test_stream_clone() {
        let stream = Stream::new();
        let cloned_stream = stream.clone();
        assert_eq!(stream, cloned_stream);
    }

    #[test]
    #[cfg(feature = "metal")]
    fn test_cpu_gpu_stream_not_equal() {
        const CHILD: &str = "MLX_RS_DEFAULT_DEVICE_CHILD";
        if std::env::var_os(CHILD).is_some() {
            // SAFETY: This child runs only this test, with no other MLX threads.
            unsafe { Device::set_default(&Device::cpu()).unwrap() };
            assert_eq!(Device::default(), Device::cpu());
            let cpu_stream = Stream::default();

            unsafe { Device::set_default(&Device::gpu()).unwrap() };
            assert_eq!(Device::default(), Device::gpu());
            let gpu_stream = Stream::default();
            assert_ne!(cpu_stream, gpu_stream);
            return;
        }

        let output = std::process::Command::new(std::env::current_exe().unwrap())
            .args([
                "--exact",
                "stream::tests::test_cpu_gpu_stream_not_equal",
                "--nocapture",
                "--test-threads=1",
            ])
            .env(CHILD, "1")
            .output()
            .unwrap();

        assert!(
            output.status.success(),
            "child status: {:?}\nstdout:\n{}\nstderr:\n{}",
            output.status,
            String::from_utf8_lossy(&output.stdout),
            String::from_utf8_lossy(&output.stderr)
        );
    }
}

#[cfg(test)]
mod device_tests {
    use super::*;

    #[test]
    fn stream_device_survives_stream_drop_and_respects_scopes() -> Result<()> {
        for device in [
            Device::cpu(),
            #[cfg(feature = "metal")]
            Device::gpu(),
        ] {
            let stream = Stream::new_with_device(&device);
            let queried = stream.device()?;
            assert_eq!(queried, device);
            with_stream(&stream, || -> Result<()> {
                assert_eq!(Stream::thread_local_or_default().device()?, device);
                Ok(())
            })?;
            drop(stream);
            assert_eq!(queried, device);
        }
        Ok(())
    }
}