2 |
mlx-rs
3 |
4 | Rust bindings for Apple's mlx machine learning library.
5 |
6 | [](https://discord.gg/jZvTsxDX49)
7 | [](https://crates.io/crates/mlx-rs)
8 | []()
9 | [](https://github.com/oxideai/mlx-rs/actions/workflows/validate.yml)
10 | [](https://runblaze.dev)
11 | [](https://releases.rs/docs/1.81.0)
12 | 
13 |
14 | > **⚠️ Project is in active development - contributors welcome!**
15 |
16 | ---
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 | _[Blaze](https://runblaze.dev) supports this project by providing ultra-fast Apple Silicon macOS Github Action Runners. Apply the discount code `AI25` at checkout to enjoy 25% off your first year._
29 |
30 |
31 |
32 |
33 |
34 | ## Documentation
35 |
36 | Due to known limitation of docsrs, we are hosting the documentation on github pages [here](https://oxideai.github.io/mlx-rs/mlx_rs/).
37 |
38 | ## Features
39 |
40 | MLX is an array framework for machine learning on Apple Silicon. mlx-rs provides Rust bindings for MLX, allowing you to use MLX in your Rust projects.
41 |
42 | Some key features of MLX and `mlx-rs` include:
43 | - **Performance**: MLX is optimized for Apple Silicon, providing fast performance for machine learning tasks.
44 | - **Lazy Evaluation**: MLX uses lazy evaluation to optimize performance and memory usage. Arrays are only materialized when needed.
45 | - **Dynamic Graphs**: Computation graphs in MLX are constructed dynamically, allowing for flexible and efficient computation. Changing the shapes of function arguments does not require recompilation.
46 | - **Mutli-Device Support**: MLX supports running computations on any of the supported devices (for now the CPU and GPU).
47 | - **Unified memory**: MLX provides a unified memory model, meaning arrays live in the same memory space, regardless of the device they are computed on. Operations can be performed on arrays on different devices without copying data between them.
48 |
49 | `mlx-rs` is designed to be a safe and idiomatic Rust interface to MLX, providing a seamless experience for Rust developers.
50 |
51 | ## Examples
52 | The [examples](examples/) directory contains sample projects demonstrating different uses cases of our library.
53 | - [mnist](examples/mnist/): Train a basic neural network on the MNIST digit dataset
54 | - [mistral](examples/mistral/): Text generation using the pre-trained Mistral model
55 |
56 | ## Installation
57 |
58 | Add this to your `Cargo.toml`:
59 | ```toml
60 | [dependencies]
61 | mlx-rs = "0.21.0"
62 | ```
63 |
64 | ## Feature Flags
65 |
66 | * `metal` - enables metal (GPU) usage in MLX
67 | * `accelerate` - enables using the accelerate framework in MLX
68 |
69 | ## Important Notes on Automatic Differentiation
70 |
71 | When using automatic differentiation in mlx-rs, there's an important difference in how closures work compared to Python's MLX. In Python, variables are implicitly captured and properly traced in the compute graph. However, in Rust, we need to be more explicit about which arrays should be traced.
72 |
73 | ❌ This approach may cause segfaults:
74 | ```rust
75 | // Don't do this
76 | let x = random::normal::