Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "third_party/mlx-c"]
path = third_party/mlx-c
path = crates/mlxcore-sys/third_party/mlx-c
url = https://github.com/ml-explore/mlx-c.git
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,18 @@ The bindings are layered, the same way as [MLX Swift](https://github.com/ml-expl
```
mlxcore (safe, idiomatic Rust API) <- crates/mlxcore
mlxcore-sys (raw unsafe FFI bindings) <- crates/mlxcore-sys, generated by bindgen
mlx-c (Apple's C API for MLX) <- third_party/mlx-c (git submodule)
mlx (Apple's C++ framework) <- fetched by mlx-c's CMake build
mlx-c (Apple's C API for MLX) <- crates/mlxcore-sys/third_party/mlx-c (git submodule)
mlx (Apple's C++ framework) <- fetched by mlx-c's CMake build
```

MLX itself is C++, which Rust cannot bind to directly. We bind against
[`mlx-c`](https://github.com/ml-explore/mlx-c), Apple's official C API, whose
CMake build pulls in MLX via `FetchContent`.

The mlx-c submodule sits inside `mlxcore-sys` rather than at the repository root
because `cargo package` only ships files under the crate directory — from the
root it would be missing from the published crate.

## Building from source

```sh
Expand All @@ -64,7 +68,7 @@ Runnable examples live in `crates/mlxcore/examples`:

```sh
cargo run --example hello # arrays, shapes, streams
cargo run --example linear # y = relu(x @ W + b) with random weights
cargo run --example relu # y = relu(x @ W + b) with random weights
```

## Note: suffix float literals with `f32`
Expand Down
2 changes: 2 additions & 0 deletions crates/mlxcore-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ license.workspace = true
repository.workspace = true
authors.workspace = true
description = "Low-level FFI bindings to mlx-c, the C API for Apple's MLX framework."
keywords = ["mlx", "apple", "metal", "ffi", "bindings"]
categories = ["external-ffi-bindings"]
links = "mlxc"
build = "build.rs"

Expand Down
11 changes: 8 additions & 3 deletions crates/mlxcore-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@ fn main() {
}

let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
// Workspace layout: <root>/crates/mlxcore-sys -> <root>/third_party/mlx-c
// mlx-c lives *inside* this crate rather than at the workspace root, because
// `cargo package` only ships files under the crate directory. A path that
// escaped upwards would build here and then fail for anyone installing from
// crates.io.
let mlx_c_dir = manifest_dir
.join("../../third_party/mlx-c")
.join("third_party/mlx-c")
.canonicalize()
.expect("third_party/mlx-c submodule not found — run `git submodule update --init`");
.expect(
"third_party/mlx-c submodule not found — run `git submodule update --init --recursive`",
);

// --- 1. Build mlx-c (+ MLX via FetchContent) with CMake ---------------
let mut cfg = cmake::Config::new(&mlx_c_dir);
Expand Down
3 changes: 3 additions & 0 deletions crates/mlxcore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ license.workspace = true
repository.workspace = true
authors.workspace = true
description = "Safe, idiomatic Rust bindings for Apple's MLX array framework."
readme = "../../README.md"
keywords = ["mlx", "apple", "metal", "array", "machine-learning"]
categories = ["api-bindings", "mathematics"]

[features]
default = ["metal", "accelerate"]
Expand Down
2 changes: 1 addition & 1 deletion crates/mlxcore/examples/hello.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! A tour of the `mlxcore` API: constructors, arithmetic, reductions, shape
//! manipulation, comparisons, and stream control.
//!
//! For a worked end-to-end computation, see `examples/linear.rs`.
//! For a worked end-to-end computation, see `examples/relu.rs`.
//!
//! Run with:
//! ```sh
Expand Down
2 changes: 1 addition & 1 deletion crates/mlxcore/examples/relu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//!
//! Run with:
//! ```sh
//! cargo run --example linear
//! cargo run --example relu
//! ```

use mlxcore::{Array, Stream, random};
Expand Down
Loading