Skip to main content

mlx-ternary

PyPI Version Release License Apple Silicon Metal

High-performance Apple Silicon runtime for 1.58-bit ternary language models.

mlx-ternary enables extreme-compression ternary LLMs (such as Prism ML's Ternary-Bonsai-2-27B) to run natively at full speed on Apple Silicon with custom Metal Shading Language (MSL) JIT kernels, hardware vector operations, shape-aware hybrid GEMV dispatch, and multi-engine speculative decoding.


💡 Understanding the Silicon Envelope

27.2 Billion parameters running locally on an everyday laptop.
In standard FP16, a 27B model demands 54.4 GB VRAM just to load weights—requiring multi-GPU workstations or high-end Mac Studios.

With mlx-ternary on a base Apple M4 Pro (24 GB Unified Memory, 273 GB/s peak bandwidth, AC Mains):

  • Total Working Footprint: 6.37 GB flat ($0.78\text{ GB}$ / $11%$ lighter than official Bonsai, leaving $17.6\text{ GB}$ completely free for 32k KV context and OS buffers with zero memory leaks).
  • Prefill Latency: $116.3\text{ tok/s}$ ($1.15\times$ faster than official Bonsai).
  • Autoregressive Decode: $15.2 - 16.5\text{ tok/s}$ ($60.6 - 65.8\text{ ms/tok}$).
  • Numerical Precision: 100% Bit-Exact matching official baseline.

Performance Matrix

Benchmarking Ternary-Bonsai-2-27B (27.2B parameters, 64 Transformer layers, 5120 hidden dimension, 17408 MLP dimension) on Apple M4 Pro (24 GB Unified Memory, AC Mains):

End-to-End Workload Telemetry

Workload Profile Engine Prefill Throughput Decode Throughput Active VRAM Parity
Workload A (Coding / JSON) Official Bonsai
mlx-ternary v0.6.0
$101.36\text{ tok/s}$ ($9.87\text{ ms}$)
$116.34\text{ tok/s}$ ($8.59\text{ ms}$)
$16.51\text{ tok/s}$ ($60.57\text{ ms}$)
$16.51\text{ tok/s}$ ($60.57\text{ ms}$)
$7.15\text{ GB}$
$6.37\text{ GB}$ (-0.78 GB)
Reference
100% Bit-Exact
Workload B (Agentic Dialog) Official Bonsai
mlx-ternary v0.6.0
$99.63\text{ tok/s}$ ($10.04\text{ ms}$)
$111.56\text{ tok/s}$ ($8.96\text{ ms}$)
$15.20\text{ tok/s}$ ($65.81\text{ ms}$)
$15.20\text{ tok/s}$ ($65.81\text{ ms}$)
$7.15\text{ GB}$
$6.37\text{ GB}$ (-0.78 GB)
Reference
100% Bit-Exact
Workload C (GSM8K Math) Official Bonsai
mlx-ternary v0.6.0
$101.99\text{ tok/s}$ ($9.80\text{ ms}$)
$95.18\text{ tok/s}$ ($10.51\text{ ms}$)
$15.46\text{ tok/s}$ ($64.67\text{ ms}$)
$15.46\text{ tok/s}$ ($64.67\text{ ms}$)
$7.15\text{ GB}$
$6.37\text{ GB}$ (-0.78 GB)
Reference
100% Bit-Exact

Kernel Micro-Benchmarks (M4 Pro Hardware Testbed)

  • MLP Gate/Up ($5120 \times 17408$): $234.3\text{ }\mu\text{s}$ ($95.1\text{ GB/s}$ sustained, 1D SRAM activation caching).
  • MLP Down ($17408 \times 5120$): $321.7\text{ }\mu\text{s}$ ($69.3\text{ GB/s}$ sustained, high-$K$ SRAM caching).
  • Attention KV ($5120 \times 512$): $111.7\text{ }\mu\text{s}$ ($5.9\text{ GB/s}$, 32-lane cooperative SIMD warp reduction, 0 barriers).
  • Prefill JIT Fast-Path ($M > 1$): Dispatches directly into Apple AMX block GEMM, delivering $>110\text{ tok/s}$.

Technical Documentation & Build Logs


Installation

pip install mlx-ternary

Or install from source:

git clone https://github.com/FaizPalwala/mlx-ternary.git
cd mlx-ternary
pip install -e .

Command-Line Interface (CLI)

mlx-ternary registers both mlx-ternary and mlx_ternary console entrypoints:

1. Streaming Text Generation (generate)

Run direct streaming inference with the production Tier 3 Fused engine:

mlx-ternary generate prism-ml/Ternary-Bonsai-2-27B-mlx-2bit --prompt "def quicksort(arr):"

To test experimental speculative decoding on structured code or repetitive prompt schemas:

# Prompt Lookup Decoding (PLD)
mlx-ternary generate prism-ml/Ternary-Bonsai-2-27B-mlx-2bit -p "def fibonacci(n):" --speculative-experimental pld

# 16-Layer Self-Speculative Draft Proposal
mlx-ternary generate prism-ml/Ternary-Bonsai-2-27B-mlx-2bit -p "Explain quantum superposition" --speculative-experimental self

2. OpenAI-Compatible Server (serve)

Serve your local model directly to LM Studio, OpenWebUI, or any OpenAI-compatible client:

# Standard server (Port 8080)
mlx-ternary serve prism-ml/Ternary-Bonsai-2-27B-mlx-2bit --port 8080

# Long-Context Mode (4-bit KV Cache)
mlx-ternary serve prism-ml/Ternary-Bonsai-2-27B-mlx-2bit --kv-bits 4 --port 8080

3. Silicon Performance Probe (bench)

Run an empirical hardware audit of your Apple Silicon GPU:

mlx-ternary bench

4. Weight Transcoding & Packing (pack)

Pre-pack weights offline into 32-byte hardware-vectorized interleaved format:

mlx-ternary pack prism-ml/Ternary-Bonsai-2-27B-mlx-2bit -o /path/to/packed_model

Python API

import mlx.core as mx
from mlx_ternary import load_model, TernaryLinear, ternary_matmul

# 1. Load Model with Automatic Operator Fusion (Tier 3 Fused SOTA)
model, tokenizer = load_model("prism-ml/Ternary-Bonsai-2-27B-mlx-2bit", fuse=True)

# 2. Low-Level Core Matmul Kernel Dispatch
# Dispatches automatically between 32-lane SIMD warp reduction and 1D SRAM activation caching
layer = TernaryLinear(in_features=5120, out_features=17408, group_size=128)
x = mx.random.normal((1, 5120)).astype(mx.float16)
out = layer(x)

Testing & Verification

Run the full unit test suite:

pytest tests/ -v
# 58 passed in 5.34s

Support the Project

Benchmarking, training adapter checkpoints, and developing custom Metal kernels on an Apple Silicon laptop takes real time and compute. As a new grad building open-source systems on a student budget, evaluation compute and hardware testbeds add up quickly.

If mlx-ternary saved you from purchasing an expensive workstation or paying for continuous cloud GPU instances, consider supporting the work. Any and all contributions go directly toward buying hardware testbeds, evaluation compute, and just paying bills that support open-source development:


License

MIT License. See LICENSE for details.

Metadata

Release files for mlx-ternary 0.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mlx-ternary 0.6.0
File Size Uploaded Provenance Actions
mlx_ternary-0.6.0.tar.gz 57.1 kB With provenance Details for mlx_ternary-0.6.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for mlx-ternary 0.6.0
File Interpreter ABI Platform Actions
mlx_ternary-0.6.0-py3-none-any.whl Python 3 none any Details for mlx_ternary-0.6.0-py3-none-any.whl

Total release size: 105.2 kB

Release files / mlx_ternary-0.6.0.tar.gz

Download URL mlx_ternary-0.6.0.tar.gz
Size 57.1 kB
Tags Source
SHA-256 checksum
How to use checksums
abef79807be077b378ade304fbb92eac128acbc75dc4a204cdda45e992088590
BLAKE2b-256 checksum
How to use checksums
2b0c69826a3cd5ffee5f1791fe3941041665847aef17025bb1ea4fac3ca3d573
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 16, 2026.

Transparency log

Release files / mlx_ternary-0.6.0-py3-none-any.whl

Download URL mlx_ternary-0.6.0-py3-none-any.whl
Size 48.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1e2d30af419b6fd062047705c31abfd6478f2bbdd848f27671204036178dde49
BLAKE2b-256 checksum
How to use checksums
b273b3221de13b24a33a455300dd8a36906549f67df40861a37bf47830d6ad15
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 16, 2026.

Transparency log

Security for mlx-ternary 0.6.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for mlx_ternary-0.6.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 16, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.6.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page