Skip to main content
Use MLX for running models on Apple Silicon Macs with Metal GPU acceleration.
MLX leverages unified memory architecture on Apple Silicon, allowing seamless data sharing between CPU and GPU. The mlx-lm package provides a simple interface for loading and serving LLMs.

Installation

Install the MLX language model package:
LFM3 and d1 models use MLX-VLM instead: see LFM3 and d1 Models.

Basic Usage

The mlx-lm package provides a simple interface for text generation with MLX models. See the Models page for all available MLX models, or browse MLX community models at mlx-community LFM2 models.

Generation Parameters

Control text generation behavior using parameters in the generate() function. Key parameters:
  • temperature (float, default 1.0): Controls randomness (0.0 = deterministic, higher = more random). Typical range: 0.1-2.0
  • top_p (float, default 1.0): Nucleus sampling - limits to tokens with cumulative probability ≤ top_p. Typical range: 0.1-1.0
  • top_k (int, default 50): Limits to top-k most probable tokens. Typical range: 1-100
  • max_tokens (int): Maximum number of tokens to generate
  • repetition_penalty (float, default 1.0): Penalty for repeating tokens (>1.0 = discourage repetition). Typical range: 1.0-1.5
Example with custom parameters:

Streaming Generation

Stream responses with stream_generate():

Serving with mlx-lm

MLX can serve models through an OpenAI-compatible API. Start a server with:

Using the Server

Once running, use the OpenAI Python client:
You can also use curl to interact with the server:

Vision Models

LFM2-VL models support both text and image inputs for multimodal inference. Use mlx_vlm to load and generate with vision models:

LFM3 and d1 Models

The LFM3 models and the d1 decision models run with MLX-VLM 0.7.11 or later:
The LFM3 models run with mlx_vlm.generate:
mlx_vlm.generate turns reasoning off unless you pass --enable-thinking. mlx-lm cannot load LFM3-1.7B yet. Support is pending in ml-explore/mlx-lm#2016. The d1 models answer questions instead of generating text, and d1-2B and d1-omni-2B refuse mlx_vlm.generate and chat apps. Ask them with python -m mlx_vlm decide, which prints the answers as JSON:
Swap the model id to ask d1-omni-2B, d1-3B, or d1-omni-600M. d1-3B and d1-omni-600M return yes/no answers as value and probability. The d1 model cards show the Python API.

Speculative Decoding

Some LFM2.5 vision models have DSpark draft models that make generation faster without changing the output. Pass the drafter to MLX-VLM (v0.7.2 or newer) with --draft-model:
DSpark in MLX-VLM currently supports greedy sampling only, so keep --temperature 0. See Speculative Decoding for which models have drafters.