Prerequisites
The MLX runtime requires Apple Silicon with macOS 14 or newer, stable PyTorch 2.13.x, and stable MLX 0.32.0 or newer. Thesrt_mps extra installs PyTorch
2.13.0 and MLX 0.32.0 or newer; startup accepts stable PyTorch 2.13 patch
releases and newer stable MLX releases.
With SGLANG_USE_MLX=1, SGLang validates both framework versions and Metal
availability during argument initialization and stops before resolving or
downloading a model when the runtime is incompatible.
Building the optional native Metal kernels in sgl-kernel requires the Metal
shader compiler from the full Xcode application. The standalone Xcode Command
Line Tools are not sufficient. After installing Xcode, select it with:
xcrun -sdk macosx metal --version.
Install SGLang
You can install SGLang using one of the methods below.Install from Source
Launch of the Serving Engine
Launch the server with:SGLANG_USE_MLX=1- Enables the use of MLX as the SGLang runtime backend (if disabled, SGLang will fall back totorch.mps, which has less support)--disable-decode-cuda-graph --disable-prefill-cuda-graph- Disables usage of CUDA graph, which is not relevant for Apple Metal.--disable-overlap-schedule- Disables overlap scheduling (enabled/not present by default) achieved using MLX’sasync_eval()SGLANG_MLX_USE_CUSTOM_ROPE=1- Enables the optional custom Metal RoPE kernel. It is disabled by default, so the MLX backend uses the standard RoPE path unless you opt in for A/B testing.SGLANG_MLX_FUSE_SWIGLU=1- Enables the use of fused Swish-Gated Linear Unit kernel (disabled by default)SGLANG_MLX_CLEAR_CACHE_STEPS=256- Sets the number of decode steps before clearing the MLX cache (256 by default)
Quantization
The MLX backend supports two quantization paths on Apple Silicon:- Pre-quantized HF repos. Any
mlx-community/<model>-4bit(or-8bit) repo loads directly throughmlx_lm.load(...)— no extra flag needed. - On-the-fly quantization. For any fp16 model, pass
--quantization mlx_q4or--quantization mlx_q8to have sglang quantize the weights at load time viamlx_lm.utils.quantize_model(group size 64, the mlx-community default). The quantized weights stay in process memory; the on-disk model is untouched.Expected log line:The MLX backend silently ignores--quantization mlx_q4when the model is already quantized in its HF config (path 1), so the same flag is safe to pass either way.
Unlimited-OCR v2
Unlimited-OCR-2 runs on the standard PyTorch MPS runtime, withoutSGLANG_USE_MLX. The BF16 checkpoint is supported; quantized checkpoints are rejected on this path. DASH routing and sparse attention, the per-step page metadata, Page2D RoPE and the MoE experts run Metal kernels compiled with torch.mps.compile_shader; the remaining layers run PyTorch MPS operators.
- With
--uocr-doc-splitand--uocr-max-page-tokens, SGLang sizes the KV pool in document windows. One window is--uocr-doc-page-window*--uocr-max-page-tokensplus prompt and lookahead slack and one--chunked-prefill-sizechunk: 15,168 tokens for one page and 8000 tokens on a 16 GB Mac. The pool holds two windows on a 16 GB Mac (a Metal working-set limit of 10 GiB or more), one more for each 2 GiB above 12,124 MiB (the measured 16 GB M5) up to eight, and one window below 10 GiB. Only that M5 has been measured. Pass--max-total-tokensto size the pool yourself. - Without
--uocr-doc-splitand--uocr-max-page-tokensthe pool is not capped and takes most of the free memory, which on a 16 GB Mac can leave the vision encoder short and make the system swap. Pass--max-total-tokensthere, for example--max-total-tokens 32768as in the cookbook recipe. - Requests that send
DeepseekOCRNoRepeatNGramLogitProcessorascustom_logit_processorneed--enable-custom-logit-processor. --warmups unlimited_ocr_v2sends one drawn page through the model before the server starts listening. On an M5 the warmup takes 3.6 s with a warm Metal cache and 12.7 s with an empty one, and cuts the first request’s TTFT from 2.4 s to 1.6 s (warm) and from 7.5 s to 1.7 s (empty), so it helps a server that is idle for a while before its first request.- Overlap scheduling and CUDA graphs are not available on MPS.
Benchmarking with Requests
sglang.benchmark.one_batch calls the synchronous prefill/decode methods directly without going through the scheduler and the overlap code path.
sglang.benchmark.offline_throughput can toggle overlap scheduling as it uses the scheduler and the overlap code path by using the flag --disable-overlap-schedule.
