Instructions to use jacklarmer/laguna-s-2.1-mlx-optimized with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- MLX
How to use jacklarmer/laguna-s-2.1-mlx-optimized with MLX:
# Make sure mlx-lm is installed # pip install --upgrade mlx-lm # if on a CUDA device, also pip install mlx[cuda] # Generate text with mlx-lm from mlx_lm import load, generate model, tokenizer = load("jacklarmer/laguna-s-2.1-mlx-optimized") prompt = "Once upon a time in" text = generate(model, tokenizer, prompt=prompt, verbose=True) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- LM Studio
- MLX LM
How to use jacklarmer/laguna-s-2.1-mlx-optimized with MLX LM:
Generate or start a chat session
# Install MLX LM uv tool install mlx-lm # Generate some text mlx_lm.generate --model "jacklarmer/laguna-s-2.1-mlx-optimized" --prompt "Once upon a time"
Laguna S 2.1 โ Apple Silicon MLX Optimization
Maximize Laguna S 2.1 (117B MoE,3B active) decode speed on Apple Silicon via pure-MLX, holding per-token logit correlation โฅ0.95 vs the BF16 reference.
Results
| Metric | Value |
|---|---|
| Decode speed | 77.9 tok/s peak (77.6 avg) on M5 Max 128GB |
| Quality | 0.9555 Pearson logit correlation vs BF16 reference (โฅ0.95 gate) |
| Baseline | 65.2 tok/s stock mlx-lm, โ +19% with our optimizations |
| Custom baseline | 7.5 tok/s (naive Python MoE loop) โ +937% |
What's in this repo
bench_speed.pyโ the optimized decode benchmark (one-big-function@mx.compile+ zmlx sigmoid gating fix)test_quality.pyโ quality verification (Pearson logit correlation vs BF16 reference)test_generation.pyโ end-to-end generation smoke testkernels/fused_sdpa_decode.pyโ custom Metal SDPA kernel (correctness-verified, seekernels/test_fused_sdpa.py)laguna_pipenetwork.pyโ model registration script (copieslaguna.pyinto mlx-lm's models directory)serve.pyโ OpenAI-compatible API serverEXHAUSTION.mdโ full documentation of every optimization lever tried and why the remaining ones can't helpprobes/โ scratch/profiling scripts from the optimization process
Quick start
Prerequisites
- Apple Silicon Mac (M-series, unified memory). Tested on M5 Max 128GB. The 4-bit model needs ~66GB free RAM. Smaller-memory Macs may swap.
- Python 3.11+
- ~66GB disk for model download
Install
git clone https://github.com/jacklarmer/laguna-apple.git
cd laguna-apple
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
Register the Laguna model architecture with mlx-lm:
python laguna_pipenetwork.py
Run the benchmark
python bench_speed.py
Expected output:
Fused SDPA kernel verified on decode path (kernels/test_fused_sdpa.py: 4/4 PASS)
Decode: 77.6 tok/s for 20 tokens
Verify quality
# First time: generate the BF16 reference (needs poolside/Laguna-S-2.1-NVFP4-mlx)
python make_reference.py
# Then: run quality test (threshold 0.95)
LAGUNA_QUALITY_THRESHOLD=0.95 python test_quality.py
Interactive generation
python run.py
OpenAI-compatible server
python serve.py
# POST to http://127.0.0.1:8090/v1/chat/completions
How it works
The speed comes from five stacked optimizations, all preserving the 0.955 quality gate:
One-big-function
@mx.compileโ all 48 layers compiled into a single MLX function. Model parameters are closure constants; KV cache arrays are explicit arguments. This eliminates ~96 PythonโMetal boundary crossings per decode step.zmlx
moe_mlppatch โ applies optimized MoE combine logic from ZMLX. Gives ~3% speed on the 47 MoE layers.Sigmoid gating fix โ the key quality breakthrough. ZMLX's default gating uses softmax, but Laguna uses sigmoid routing with
e_score_correction_bias. We monkey-patch ZMLX's_gatingfunction to usemx.sigmoid()instead of softmax. Without this fix, zmlx drops quality from 0.955 to 0.810.Precomputed attention masks โ masks for each decode offset are computed outside the compiled function and passed as arguments, eliminating ~8 mask dispatches per layer per step.
bfloat16 attention gating โ the softplus gate (
logaddexp(g_proj(x), 0)) is computed in bfloat16 instead of upcasting to float32, saving 2 dispatches per layer.
A custom Metal SDPA kernel (kernels/fused_sdpa_decode.py) was also written and
correctness-verified (4/4 layer types pass), but stock MLX's scaled_dot_product_attention
is faster because it uses optimized matrix-multiply units. The custom kernel uses
scalar float loops and can't compete. See EXHAUSTION.md for the full analysis.
โ ๏ธ LM Studio / Ollama compatibility
This model will NOT load in LM Studio or Ollama right now. Here's why:
LM Studio (since v0.3.4) and Ollama both support MLX engines, but neither
supports trust_remote_code โ they only load model architectures already
built into their bundled mlx-lm/llama.cpp. Laguna is a custom
architecture โ the laguna.py model file is not in upstream mlx-lm
(we install it manually via laguna_pipenetwork.py). LM Studio's mlx-engine
does not execute custom repo code,
so it cannot load Laguna.
What you can do instead (works today):
- Use the
run.py/mlx-lmpath in this repo (3-command setup, see Quick Start above) - Use
serve.pyfor an OpenAI-compatible API server onlocalhost:8090(any client that speaks OpenAI API can connect to it)
Path to LM Studio support (future):
- A PR to
mlx-lmmain merging the Laguna architecture code - Wait for an
mlx-lmrelease that includes it - Wait for LM Studio to update its bundled
mlx-lm
This is external to this repo โ it depends on Apple's mlx-lm maintainers
accepting the PR. We can't control that timeline.
GGUF alternative: A separate GGUF conversion (for llama.cpp / LM Studio / Ollama) is possible but would lose all our MLX-specific optimizations (zmlx, compile, the sigmoid gating fix). That's a different project โ not in scope here.
Model
- Base model: poolside/Laguna-S-2.1 (BF16, 117B MoE)
- 4-bit MLX build used: pipenetwork/Laguna-S-2.1-MLX-4bit (affine 4-bit, group_size=64, ~66GB)
- BF16 reference model: poolside/Laguna-S-2.1-NVFP4-mlx (BF16 attention + NVFP4 experts, ~70GB)
We did not requantize or modify the model weights. All optimizations are runtime (Python/Metal code), not weight changes.
Hardware
Tested on Apple M5 Max with 128GB unified memory (~600GB/s peak bandwidth). The model is bandwidth-bound: ~2.5GB of expert weights are read per decode step (top-10 of 256 experts ร 47 MoE layers) at ~400GB/s effective = ~6.25ms. The ~77.9 tok/s = 12.8ms/step is within 1.5ms of the raw bandwidth floor.
See EXHAUSTION.md for the full bottleneck analysis and why ~78 tok/s is the
ceiling on this hardware holding the 0.95 quality gate.
License
The optimization code in this repo is MIT-licensed. The model weights belong to
their respective creators (see model links above). The Laguna model architecture
code (laguna.py registered via laguna_pipenetwork.py) is Copyright PipeNetwork,
Apache 2.0.
Model tree for jacklarmer/laguna-s-2.1-mlx-optimized
Base model
poolside/Laguna-S-2.1
# Install MLX LM uv tool install mlx-lm# Generate some text mlx_lm.generate --model "jacklarmer/laguna-s-2.1-mlx-optimized" --prompt "Once upon a time"