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
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[workspace]
resolver = "2"
members = ["crates/mlx-sys", "crates/mlx"]
members = ["crates/mlxcore-sys", "crates/mlxcore"]

Comment thread
kerthcet marked this conversation as resolved.
[workspace.package]
edition = "2024"
Expand All @@ -9,4 +9,4 @@ repository = "https://github.com/InftyAI/mlx"
authors = ["kerthcet"]

[workspace.dependencies]
mlx-sys = { path = "crates/mlx-sys", version = "0.0.0" }
mlxcore-sys = { path = "crates/mlxcore-sys", version = "0.0.0" }
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ Rust bindings for Apple's [MLX](https://github.com/ml-explore/mlx) framework.
The bindings are layered, the same way as [MLX Swift](https://github.com/ml-explore/mlx-swift):

```
mlx (safe, idiomatic Rust API) <- crates/mlx
mlx-sys (raw unsafe FFI bindings) <- crates/mlx-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
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 itself is C++, which Rust cannot bind to directly. We bind against
Expand All @@ -32,7 +32,7 @@ The first build compiles MLX from source and takes several minutes.

## Examples

Runnable examples live in `crates/mlx/examples`:
Runnable examples live in `crates/mlxcore/examples`:

```sh
cargo run --example hello
Expand Down
18 changes: 0 additions & 18 deletions crates/mlx/examples/hello.rs

This file was deleted.

144 changes: 0 additions & 144 deletions crates/mlx/src/array.rs

This file was deleted.

58 changes: 0 additions & 58 deletions crates/mlx/src/dtype.rs

This file was deleted.

4 changes: 2 additions & 2 deletions crates/mlx-sys/Cargo.toml → crates/mlxcore-sys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[package]
name = "mlx-sys"
name = "mlxcore-sys"
version = "0.0.0"
# Edition 2021: bindgen 0.70 emits plain `extern "C"` blocks, which edition
# 2024 rejects (it requires `unsafe extern`). The safe `mlx` crate stays 2024.
# 2024 rejects (it requires `unsafe extern`). The safe `mlxcore` crate stays 2024.
edition = "2021"
license.workspace = true
repository.workspace = true
Expand Down
10 changes: 5 additions & 5 deletions crates/mlx-sys/build.rs → crates/mlxcore-sys/build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Build script for `mlx-sys`.
//! Build script for `mlxcore-sys`.
//!
//! 1. Builds the vendored `mlx-c` C API with CMake. `mlx-c` uses CMake
//! `FetchContent` to download and build MLX itself, so both `libmlxc` and
Expand All @@ -13,11 +13,11 @@ use std::path::PathBuf;

fn main() {
if !cfg!(target_os = "macos") {
panic!("mlx-sys currently only supports macOS on Apple Silicon");
panic!("mlxcore-sys currently only supports macOS on Apple Silicon");
}

let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
// Workspace layout: <root>/crates/mlx-sys -> <root>/third_party/mlx-c
// Workspace layout: <root>/crates/mlxcore-sys -> <root>/third_party/mlx-c
let mlx_c_dir = manifest_dir
.join("../../third_party/mlx-c")
.canonicalize()
Expand All @@ -39,12 +39,12 @@ fn main() {
}

// Build into a fixed dir under `target/<profile>/` rather than the default
// per-fingerprint `OUT_DIR`. `cargo clippy` fingerprints `mlx-sys`
// per-fingerprint `OUT_DIR`. `cargo clippy` fingerprints `mlxcore-sys`
// differently from `cargo build`/`cargo test`, so an `OUT_DIR`-based build
// would recompile MLX from scratch for each (~2.5 min of C++). A shared dir
// lets them all reuse the same CMake build.
//
// OUT_DIR = <target>/<profile>/build/mlx-sys-<hash>/out; three parents up is
// OUT_DIR = <target>/<profile>/build/mlxcore-sys-<hash>/out; three parents up is
// <target>/<profile>.
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
let profile_dir = out_dir
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//!
//! These bindings are generated at build time by `bindgen` from the pinned
//! `third_party/mlx-c` submodule. This crate is not meant to be used directly;
//! prefer the safe `mlx` crate that wraps it.
//! prefer the safe `mlxcore` crate that wraps it.
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
Expand Down
8 changes: 4 additions & 4 deletions crates/mlx/Cargo.toml → crates/mlxcore/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "mlx"
name = "mlxcore"
version = "0.0.0"
edition.workspace = true
license.workspace = true
Expand All @@ -9,8 +9,8 @@ description = "Safe, idiomatic Rust bindings for Apple's MLX array framework."

[features]
default = ["metal", "accelerate"]
metal = ["mlx-sys/metal"]
accelerate = ["mlx-sys/accelerate"]
metal = ["mlxcore-sys/metal"]
accelerate = ["mlxcore-sys/accelerate"]

[dependencies]
mlx-sys.workspace = true
mlxcore-sys.workspace = true
34 changes: 34 additions & 0 deletions crates/mlxcore/examples/hello.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//! A minimal MLX example.
//!
//! Run with:
//! ```sh
//! cargo run --example hello
//! ```

use mlxcore::{Array, Stream};

fn main() -> mlxcore::Result<()> {
println!("MLX version: {}", mlxcore::version());

let a = Array::from_slice(&[1.0f32, 2.0, 3.0, 4.0, 5.0, 6.0], &[2, 3]);
println!("shape: {:?}", a.shape());
println!("size: {}", a.size());
println!("ndim: {}", a.ndim());
println!("array:\n{a:?}");

let b = Array::from_slice(&[6.0f32, 5.0, 4.0, 3.0, 2.0, 1.0], &[2, 3]);

// Operators use the default stream and panic on error.
println!("a + b:\n{:?}", &a + &b);

// Or steer the default onto a specific device once, up front.
Stream::cpu().set_as_default();
let total = a.sum(false, &Stream::default())?;
println!("sum(a) on CPU: {}", total.item::<f32>());

// Explicit stream control is still available per-op; `?` propagates errors.
let product = a.multiply(&b, &Stream::gpu())?;
println!("a * b (GPU):\n{product:?}");

Ok(())
}
Loading
Loading