MLX LM — Run and Fine-Tune LLMs on Apple Silicon
by Apple (ml-explore)
LoRA, QLoRA and DoRA fine-tuning plus serving on a Mac, with no cloud GPU and no data leaving the machine.
Overview
mlx-lm (6.5k GitHub stars, 851 commits) is the LLM package built on Apple's MLX array framework from its machine-learning research team, exposing both a CLI and a Python API. Generation covers mlx_lm.generate and an interactive chat, streaming output, prompt caching for repeated long prefixes, model quantization with upload back to the Hugging Face Hub, and distributed inference and fine-tuning through mx.distributed; thousands of ready-converted models are available from the mlx-community organization. The fine-tuning guide (LORA.md) is the substantive part: mlx_lm.lora trains LoRA by default, with QLoRA against a pre-quantized base, DoRA, or full-weight fine-tuning, and mlx_lm.fuse merges adapters back into the base model, with GGUF export for select architectures. Datasets are JSONL in four documented shapes — chat, completions, raw text, and tools for function-calling data — loaded locally or from the Hub, with prompt masking to exclude instruction tokens from the loss. Memory guidance is concrete: drop batch size from the default 4 to 1-2, fine-tune 4-8 layers instead of the default 16, enable --grad-checkpoint, use gradient accumulation, or shorten sequences; the docs report roughly 250 tokens per second on an M1 Max with 32GB at batch size 1 with minimal layers. Validation loss is reported during training, with Weights & Biases and SwanLab integration. Documented model families include Mistral, Llama, Phi2, Mixtral, Qwen2, Gemma, OLMo, MiniCPM and InternLM2.
At a Glance
- Topic
- Fine-Tuning
- Level
- Intermediate
- Format
- Documentation
- Cost
- Free
- Duration
- ~3-4 hours to run your first LoRA fine-tune end to end and fuse the adapter
- Provider
- Apple (ml-explore)
- Hands-on
- Yes — code/exercises
- Certificate
- None
What You’ll Learn
- ✓Run a LoRA, QLoRA, DoRA or full fine-tune from one mlx_lm.lora command
- ✓Format training data as JSONL in chat, completions, text or tools shape
- ✓Quantize a Hugging Face model to 4-bit and push it back to the Hub
- ✓Trim memory with batch size, layer count, gradient checkpointing and accumulation
- ✓Fuse a trained adapter into base weights and export GGUF where supported
- ✓Serve generations from the CLI or Python API with streaming and prompt caching
- ✓Track validation loss and log runs to Weights & Biases or SwanLab
- ✓Use prompt masking so instruction tokens are excluded from the training loss
- ✓Scale across machines with distributed inference and fine-tuning via mx.distributed
Highlights
- •Fine-tuning on a 16GB Mac is realistic with a 4-bit 8B model — no cloud GPU, no data egress
- •Independent benchmarking on an M2 Ultra found MLX had the highest sustained generation throughput of the Apple-silicon engines tested
- •Apple's unified memory means one shared pool, which is what makes local training feasible at all
- •Written and maintained by Apple's own machine-learning research team, not a community wrapper
- •Covers the whole loop — quantize, train, evaluate, fuse, serve — in one package
- •Memory-reduction advice is specific numbers and flags, not general hand-waving
Who It’s For
Best For
- ✓Engineers fine-tuning open models on confidential data that cannot leave their machine
- ✓Mac-based developers who want to iterate on adapters without renting cloud GPUs
- ✓Practitioners prototyping a fine-tune locally before scaling it on NVIDIA hardware
- ✓Anyone building on-device LLM features for macOS or iOS
Prerequisites
- •An Apple silicon Mac (M1 or newer) on macOS 13.5+ with Python 3.10+ — Intel Macs are unsupported
- •Working Python and comfort with the command line and JSONL data preparation
- •Understanding of LoRA, quantization and learning-rate tuning basics
- •Models must be in Hugging Face safetensors format; GGUF files cannot be trained
FAQ
What is MLX LM — Run and Fine-Tune LLMs on Apple Silicon?
The documentation for mlx-lm, Apple's Python package for text generation and fine-tuning of large language models on Apple silicon using the MLX framework. It is for engineers who want to fine-tune and serve open models on a Mac — for privacy, cost, or because they simply have a MacBook — rather than renting a cloud GPU. Working through it you can quantize a model, run a LoRA or QLoRA fine-tune on your own JSONL data, evaluate it, and fuse the adapter back into servable weights.
Is MLX LM — Run and Fine-Tune LLMs on Apple Silicon free?
MLX LM — Run and Fine-Tune LLMs on Apple Silicon is free to access.
What level is MLX LM — Run and Fine-Tune LLMs on Apple Silicon for?
MLX LM — Run and Fine-Tune LLMs on Apple Silicon is aimed at a intermediate audience. Recommended background: An Apple silicon Mac (M1 or newer) on macOS 13.5+ with Python 3.10+ — Intel Macs are unsupported, Working Python and comfort with the command line and JSONL data preparation, Understanding of LoRA, quantization and learning-rate tuning basics, Models must be in Hugging Face safetensors format; GGUF files cannot be trained.
How long does MLX LM — Run and Fine-Tune LLMs on Apple Silicon take?
Expect roughly ~3-4 hours to run your first LoRA fine-tune end to end and fuse the adapter. Most learners work through it at their own pace.
What will I learn from MLX LM — Run and Fine-Tune LLMs on Apple Silicon?
You'll learn: Run a LoRA, QLoRA, DoRA or full fine-tune from one mlx_lm.lora command; Format training data as JSONL in chat, completions, text or tools shape; Quantize a Hugging Face model to 4-bit and push it back to the Hub; Trim memory with batch size, layer count, gradient checkpointing and accumulation; Fuse a trained adapter into base weights and export GGUF where supported; Serve generations from the CLI or Python API with streaming and prompt caching; Track validation loss and log runs to Weights & Biases or SwanLab; Use prompt masking so instruction tokens are excluded from the training loss; Scale across machines with distributed inference and fine-tuning via mx.distributed.
Topics
Sources
This page was written from 4 sources, 2 on domains other than github.com.