Example MLX Swift programs. The language model examples use models implemented in MLX Swift LM.
MNISTTrainer: An example that runs on both iOS and macOS that downloads MNIST training data and trains a LeNet.
LLMBasic: A minimal LLM chat example application. It has only two features: load the model and evaluate a prompt.
LLMEval: An example that runs on both iOS and macOS that downloads an LLM and tokenizer from Hugging Face and generates text from a given prompt. It has some preset prompts, tool integration, etc. Additionally it shows detailed statistics on the run.
MLXChatExample: An example chat app that runs on both iOS and macOS that supports LLMs and VLMs.
LoRATrainingExample: An example that runs on macOS that downloads an LLM and fine-tunes it using LoRA (Low-Rank Adaptation) with training data.
LinearModelTraining: An example that trains a simple linear model.
StableDiffusionExample: An example that runs on both iOS and macOS that downloads a stable diffusion model from Hugging Face and 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.
image-tool: A command line tool for generating images using a stable diffusion model from Hugging Face.
mnist-tool: A command line tool for training a a LeNet on MNIST.
Examples that use MLX for general numerical computing (no ML model involved),
useful for seeing how MLX array idioms, compile, and custom Metal kernels
apply to classic numerical problems.
CurveFit: Live visualization of gradient
descent fitting a quadratic to noisy samples, using MLX.grad for
automatic differentiation.
HeatTransfer: 2D heat-diffusion
simulation comparing three Jacobi/SOR stencil implementations
(conv2d, compiled roll, red/black SOR).
Mandelbrot: Mandelbrot set renderer
comparing plain MLX, compiled MLX, and a custom Metal kernel
(MLXFast.metalKernel) against a reference CPU implementation.
[!IMPORTANT]
MLXLMCommon,MLXLLM,MLXVLMandMLXEmbeddershave moved to a new repository containing only reusable libraries: mlx-swift-lm.
Previous URLs and tags will continue to work, but going forward all updates to these libraries will be done in the other repository. Previous tags are supported in the new repository.
[!TIP] Contributors that wish to edit both
mlx-swift-examplesandmlx-swift-lmcan use this technique in Xcode.
LLM and VLM implementations are available in MLX Swift LM:
MLX Swift Examples also contains a few reusable libraries that can be imported with this code in your Package.swift or by referencing the URL in Xcode:
.package(url: "https://github.com/ml-explore/mlx-swift-examples/", branch: "main"),
Then add one or more libraries to the target as a dependency:
.target(
name: "YourTargetName",
dependencies: [
.product(name: "StableDiffusion", package: "mlx-libraries")
]),
The application and command line tool examples can be run from Xcode or from the command line:
./mlx-run llm-tool --prompt "swift programming language"
Note: mlx-run is a shell script that uses xcode command line tools to
locate the built binaries. It is equivalent to running from Xcode itself.
See also:
(top 30 of 58)
Swift
96.5%
Shell
2.5%
Example MLX Swift programs. The language model examples use models implemented in MLX Swift LM.
MNISTTrainer: An example that runs on both iOS and macOS that downloads MNIST training data and trains a LeNet.
LLMBasic: A minimal LLM chat example application. It has only two features: load the model and evaluate a prompt.
LLMEval: An example that runs on both iOS and macOS that downloads an LLM and tokenizer from Hugging Face and generates text from a given prompt. It has some preset prompts, tool integration, etc. Additionally it shows detailed statistics on the run.
MLXChatExample: An example chat app that runs on both iOS and macOS that supports LLMs and VLMs.
LoRATrainingExample: An example that runs on macOS that downloads an LLM and fine-tunes it using LoRA (Low-Rank Adaptation) with training data.
LinearModelTraining: An example that trains a simple linear model.
StableDiffusionExample: An example that runs on both iOS and macOS that downloads a stable diffusion model from Hugging Face and 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.
image-tool: A command line tool for generating images using a stable diffusion model from Hugging Face.
mnist-tool: A command line tool for training a a LeNet on MNIST.
Examples that use MLX for general numerical computing (no ML model involved),
useful for seeing how MLX array idioms, compile, and custom Metal kernels
apply to classic numerical problems.
CurveFit: Live visualization of gradient
descent fitting a quadratic to noisy samples, using MLX.grad for
automatic differentiation.
HeatTransfer: 2D heat-diffusion
simulation comparing three Jacobi/SOR stencil implementations
(conv2d, compiled roll, red/black SOR).
Mandelbrot: Mandelbrot set renderer
comparing plain MLX, compiled MLX, and a custom Metal kernel
(MLXFast.metalKernel) against a reference CPU implementation.
[!IMPORTANT]
MLXLMCommon,MLXLLM,MLXVLMandMLXEmbeddershave moved to a new repository containing only reusable libraries: mlx-swift-lm.
Previous URLs and tags will continue to work, but going forward all updates to these libraries will be done in the other repository. Previous tags are supported in the new repository.
[!TIP] Contributors that wish to edit both
mlx-swift-examplesandmlx-swift-lmcan use this technique in Xcode.
LLM and VLM implementations are available in MLX Swift LM:
MLX Swift Examples also contains a few reusable libraries that can be imported with this code in your Package.swift or by referencing the URL in Xcode:
.package(url: "https://github.com/ml-explore/mlx-swift-examples/", branch: "main"),
Then add one or more libraries to the target as a dependency:
.target(
name: "YourTargetName",
dependencies: [
.product(name: "StableDiffusion", package: "mlx-libraries")
]),
The application and command line tool examples can be run from Xcode or from the command line:
./mlx-run llm-tool --prompt "swift programming language"
Note: mlx-run is a shell script that uses xcode command line tools to
locate the built binaries. It is equivalent to running from Xcode itself.
See also:
(top 30 of 58)
Swift
96.5%
Shell
2.5%