ml-explore/mlx-swift

Swift API for MLX

MLX Swift – Swift bindings for the MLX machine‑learning array library

What it is – MLX Swift is a Swift‑language wrapper around MLX, an array‑oriented framework for machine‑learning that runs natively on Apple‑silicon GPUs (Metal) and, via CUDA, on Linux. It exposes the same tensor operations, neural‑network layers and optimizers that the Python version provides, but lets you write the code in Swift and integrate it directly into iOS, macOS, or server‑side Swift projects.

Why it matters – Apple’s hardware (M‑series chips) offers a powerful GPU that is not directly reachable from most ML libraries. MLX gives you a low‑level, high‑performance tensor library that talks to Metal, and MLX Swift makes that accessible to the Swift ecosystem, enabling on‑device training, inference and research without leaving the Apple development stack.

Key components

  • MLX – core tensor types and operations.
  • MLXNN – building‑block neural‑network layers (convolutions, linear, activation, etc.).
  • MLXOptimizers – standard optimizers such as SGD, Adam, etc.
  • MLXRandom – reproducible random number generation.

How you use it

  1. Add the package to an Xcode project or a Package.swift file (e.g. .package(url: "https://github.com/ml-explore/mlx-swift", from: "0.10.0")).
  2. Import the desired modules (import MLX, import MLXNN, …) and write Swift code that creates tensors, defines a model, and runs training or inference.
  3. Build with Xcode (which compiles the required Metal shaders) or via CMake/Ninja for command‑line or Linux builds.

Typical workflow –

import MLX, MLXNN, MLXOptimizers

let x = MLXArray.randomNormal(shape: [64, 784])   // input batch
let model = Sequential {
    Linear(inFeatures: 784, outFeatures: 128)
    Relu()
    Linear(inFeatures: 128, outFeatures: 10)
}
let opt = Adam(parameters: model.parameters, learningRate: 0.001)

let logits = model(x)
let loss = softmaxCrossEntropy(logits, targets: …)
opt.step(loss: loss)

The same code runs on macOS/iOS (Metal GPU) or on Linux (CPU/CUDA) depending on the build flags.

Examples shipped with the repo –

  • MNISTTrainer – trains a LeNet model on the classic MNIST dataset, runnable on iOS and macOS.
  • MLXChatExample – a small chat UI that loads a language model and a vision‑language model from Hugging Face and runs inference locally.
  • LLMEval – downloads an LLM and tokenizer, then generates text from a prompt.
  • StableDiffusionExample – runs a Stable Diffusion diffusion model on‑device to produce images.
  • llm‑tool – a command‑line utility for quick text generation with any Hugging Face LLM. All examples live in the companion mlx‑swift‑examples repository and demonstrate how to port models from the Python MLX ecosystem.

Installation options

  • Xcode – add the repo as a Swift Package, then link the needed libraries. For complex dependency graphs you can also build MLX as a framework via the provided xcode/MLX.xcodeproj.
  • SwiftPM (CLI) – declare the package in Package.swift; note that Metal shaders must still be compiled with Xcode.
  • CMake/Ninja – works on macOS (Metal) and Linux (CPU or CUDA). Submodules for the underlying C‑MLX code must be initialised first.

Community & contribution – The project is open‑source under a permissive license, welcomes pull requests, and maintains a release‑planning issue view. Contributions are guided by a CONTRIBUTING.md and acknowledgements are listed in ACKNOWLEDGMENTS.md.

Where to learn more – Full API docs are hosted on SwiftPackageIndex, and the upstream MLX documentation explains the underlying tensor library.


TL;DR – MLX Swift brings Apple‑silicon‑accelerated MLX to the Swift world, letting developers write and run neural‑network code (including LLMs, vision‑language models and diffusion) directly on iOS/macOS or Linux with a familiar Swift API.

Related

  • Project
  • Project
  • Project
  • Project
  • Project