Skip to content

ml-explore/mlx-swift

MLX Swift

Installation | Documentation | Examples

MLX Swift is a Swift API for MLX.

MLX is an array framework for machine learning on Apple silicon. MLX Swift expands MLX to the Swift language, making research and experimentation easier on Apple silicon.

Language Models

LLM and VLM implementations are available in mlx-swift-lm.

Examples

MLX Swift has many examples, including:

  • MNISTTrainer: An example that runs on both iOS and macOS that downloads MNIST training data and trains a LeNet.

  • MLXChatExample: An example chat app that runs on both iOS and macOS that supports LLMs and VLMs.

  • LLMEval: A simple example that runs on both iOS and macOS that downloads an LLM and tokenizer from Hugging Face and generates text from a given prompt.

  • StableDiffusionExample: An example that runs on both iOS and macOS that downloads a stable diffusion model from Hugging Face and generates an image from a given prompt.

  • llm-tool: A command line tool for generating text using a variety of LLMs available on the Hugging Face hub.

The MLX Swift Examples repo contains the complete code and documentation for these examples, including guidelines on porting models from MLX Python.

Installation

The MLX Swift package can be built and run from Xcode or SwiftPM. A CMake installation is also provided, featuring a native Linux build option.

More details are in the documentation.

Xcode

In Xcode you can add https://github.com/ml-explore/mlx-swift.git as a package dependency and link MLX, MLXNN, MLXOptimizers and MLXRandom as needed.

SwiftPM

To use MLX with SwiftPM you can add this to your Package.swift:

dependencies: [
    .package(url: "https://github.com/ml-explore/mlx-swift", from: "0.10.0")
]

and add the libraries as dependencies:

dependencies: [.product(name: "MLX", package: "mlx-swift"),
               .product(name: "MLXNN", package: "mlx-swift"),
               .product(name: "MLXOptimizers", package: "mlx-swift")]

Note

SwiftPM (command line) cannot build the Metal shaders so the ultimate build has to be done via Xcode.

xcodebuild

Although SwiftPM (command line) cannot build the Metal shaders, xcodebuild can and it can be used to do command line builds:

# build and run tests
xcodebuild test -scheme mlx-swift-Package -destination 'platform=OS X'

# build Tutorial
xcodebuild build -scheme Tutorial -destination 'platform=OS X'

CMake

Building with CMake requires both CMake and Ninja to be installed. You can do this with Homebrew:

brew install cmake
brew install ninja

With CMake:

mkdir -p build
cd build
cmake .. -G Ninja
ninja
./example1
./tutorial
Expand Native Linux Build Instructions

(1) Install Dependencies

RHEL/Fedora:

sudo dnf install -y blas-devel lapack-devel openblas-devel clang llvm cmake make ninja
# Then install Swift by following the instructions at https://swift.org

Ubuntu/Debian:

sudo apt update;
sudo apt install -y libblas-dev liblapack-dev libopenblas-dev clang llvm cmake make ninja;
# Then install Swift by following the instructions at https://swift.org

Refer to swift.org for installation options and instructions specific to your Linux distribution.

(2) Build + Run Examples

On Linux, the examples use the CPU backend by default.

Note: GPU+CUDA support is a work in progress for mlx-swift on Linux, but is available in the Python-based MLX.

Note: SwiftPM builds are not currently supported for native Linux targets.

mkdir -p build
cd build
cmake -DMLX_BUILD_METAL=OFF .. -G Ninja
ninja
./example1
./tutorial

Contributing

Check out the contribution guidelines for more information on contributing to MLX. See the docs for more information on building from source, and running tests.

We are grateful for all of our contributors. If you contribute to MLX Swift and wish to be acknowledged, please add your name to the list in your pull request.

MLX Swift was initially developed by David Koski and Ronan Collobert, and is now maintained by David Koski. MLX Swift is built on top of MLX, which was initially developed with equal contribution by Awni Hannun, Jagrit Digani, Angelos Katharopoulos, and Ronan Collobert.

Versions

See Releases. Generally the MLX Swift version number corresponds to the same version number in MLX. Release notes indicate specifics.

All capabilities in MLX (Python) should be available in MLX Swift. If you encounter any that are missing please file an issue or feel free to submit a PR.