MLX

Also known as: Apple MLX

On this page 7

MLX is an open-source array framework for machine learning, designed by Apple machine learning research for Apple silicon and released under the MIT license in December 2023. Among AI frameworks & tools, it occupies the niche of local training and inference on Macs, offering a NumPy-like Python API together with C++, C and Swift interfaces.

Its defining ideas are a unified memory model, lazy evaluation and composable function transformations. Together they let developers in artificial intelligence write familiar research code that runs on the CPU or the integrated GPU without copying data between separate memory pools.

How MLX executes computations

MLX executes array operations by building a computation graph lazily and evaluating it only when a result is actually needed. A call such as mx.matmul(a, b) records the operation; the work happens when the program prints the array, converts it, or calls mx.eval() explicitly.

The unified memory architecture of Apple silicon is central to the design. CPU and GPU share one physical memory pool, so an MLX array is not bound to a device. The device is chosen per operation through a stream argument, and the same buffer can feed a CPU operation and then a GPU kernel with no host-to-device transfer.

GPU work is dispatched through Metal, the Apple graphics and compute interface, using custom kernels for matrix multiplication, attention and reductions. The table below summarizes the core mechanisms and what each one changes for the developer.

MechanismWhat it doesPractical effect
Lazy evaluationDefers computation until a value is neededFuses work and skips unused branches
Unified memoryArrays live in memory shared by CPU and GPUNo explicit copies between devices
Dynamic graphsGraph is rebuilt on every callChanging shapes need no recompilation
Function transformationsgrad, vmap and compile wrap functionsGradients and batching compose freely
StreamsOperations target a CPU or GPU streamParallel work across both processors
Core execution mechanisms in MLX

Components of the MLX ecosystem

The MLX ecosystem consists of a compact core library plus higher-level packages that follow familiar conventions. The core module mlx.core provides arrays, linear algebra, random numbers and the transformations, while mlx.nn and mlx.optimizers supply layers and training algorithms in a style close to PyTorch.

Most practical adoption comes from the companion projects. The MLX LM package loads, quantizes, fine-tunes and serves large language models, and a community organization on the Hugging Face Hub publishes thousands of pre-converted models. The following table lists the main parts and their roles.

ComponentRoleTypical use
mlx.coreArrays, operations, transformationsNumerical code and custom models
mlx.nnLayers, losses, module systemDefining neural networks
mlx.optimizersSGD, Adam, AdamW and schedulersTraining loops
MLX LMLanguage model loading, LoRA, quantizationLocal chat and fine-tuning
MLX SwiftSwift bindings for appsOn-device features in macOS and iOS apps
mlx.distributedCollective communication between machinesSharding models across several Macs
Main building blocks around MLX

How MLX compares with PyTorch, JAX and Core ML

MLX borrows the imperative style of PyTorch and the functional transformations of JAX, but it targets one hardware family instead of data-center accelerators. PyTorch can also use Apple GPUs through its MPS backend, yet MLX was built around unified memory from the start and is usually the faster and leaner option for Apple silicon.

Core ML serves a different purpose: it is a deployment format and runtime that can use the Neural Engine, whereas MLX is a research and development framework. Unlike Ollama or llama.cpp, which are inference tools, MLX also supports gradients and training. The comparison below clarifies the boundaries.

FrameworkPrimary purposeHardware focusTraining support
MLXResearch and local model workApple silicon CPU and GPUFull, with autodiff
PyTorchGeneral deep learningNVIDIA, AMD, CPU, Apple MPSFull, with autodiff
JAXFunctional numerical computingTPU, NVIDIA GPU, CPUFull, with autodiff
Core MLApp deployment runtimeCPU, GPU, Neural EngineLimited on-device updates
llama.cppQuantized language model inferenceCPU and many GPUsNot a training framework
MLX against related frameworks

Using MLX for language models

Language model work is the most common use of MLX today. A developer can download a model, run text generation and expose an OpenAI-compatible local server with a few commands, all without sending data to a cloud service.

Quantization determines how large a model fits in memory. MLX supports group-wise quantization at 2 to 8 bits, with 4-bit weights as the usual compromise. As a rough rule, a model needs about 0.5 to 0.6 gigabytes per billion parameters at 4 bits, plus memory for the key-value cache, which grows with context length.

GoalRecommended approachReason
Private local chat4-bit model through MLX LMGood quality with modest memory
Domain adaptationLoRA or QLoRA fine-tuningTrains small adapters, not full weights
Maximum accuracy8-bit or 16-bit weightsLess quantization error, more memory
Very large modelsDistributed inference over several MacsSplits layers across machines
Shipping in an appMLX Swift or conversion to Core MLNative integration and Neural Engine access
Choosing an MLX workflow by goal

Low-rank adaptation (LoRA) is the standard fine-tuning path because it updates only small adapter matrices. On a Mac with 32 to 64 gigabytes of unified memory, LoRA training of a 7 to 8 billion parameter model is practical, while full fine-tuning of the same model is usually not.

Limitations and common mistakes

The main limitation of MLX is its narrow hardware scope. Its mature backend targets Apple silicon; an experimental CUDA backend exists, but MLX is not a replacement for large-scale cluster training on data-center GPUs. MLX also does not program the Neural Engine directly, which remains the domain of Core ML.

  • Forgetting evaluation in training loops: without mx.eval() on parameters and optimizer state, the lazy graph grows and memory use climbs.
  • Timing code without synchronization: lazy execution makes naive benchmarks measure graph building, not computation.
  • Ignoring the key-value cache: long contexts can exhaust memory even when the weights fit comfortably.
  • Expecting PyTorch checkpoints to load directly: weights usually need conversion, often to the safetensors format.
  • Overestimating quantized quality: 2-bit and 3-bit models can lose noticeable accuracy on reasoning tasks.

The ecosystem is also younger and smaller than that of PyTorch. Specialized operators, third-party libraries and production tooling appear later, so teams should verify that a required feature exists before committing a project to MLX.

A plain-language summary

MLX is a toolkit from Apple for building and running AI models on a Mac. It is made for the chips inside modern Macs. These chips keep all data in one shared memory. MLX uses this well, so nothing has to be copied back and forth.

Think of a kitchen where the cook and the helper share one counter. Nobody carries ingredients between rooms. In practice, MLX lets people run capable AI models privately on their own computer. They can even teach those models new skills without renting cloud servers.

An example from practice

A legal technology team at a mid-sized law firm wants a private assistant for contract review. Client documents may not leave the office network, so a cloud model is not acceptable. The team uses a Mac Studio with 128 gigabytes of unified memory and runs MLX LM.

The engineers load an 8 billion parameter open model in 4-bit quantization, which needs roughly 5 gigabytes for weights. They then run LoRA fine-tuning on about 3,000 annotated clauses for a few hours. The adapter file is only tens of megabytes and sits beside the base model.

The result is a local review tool that flags unusual indemnity and termination clauses. It answers through an OpenAI-compatible endpoint on the internal network. Response times stay at several dozen tokens per second, and no client text leaves the building.