mlx-agent-tools / README.md
ChengyiX's picture
Initial release of mlx-agent-tools (v1.0.0)
4606829 verified
|
Raw History Blame Contribute Delete
4.05 kB

MLX Agent Tools

Reusable acceleration, quantization, conversion, and serving utilities for Apple Silicon AI agents.

Developed by Chengyi Xu and Nexora — Nexora Research

License: MIT Tests


Overview

Deploying large language and multimodal models (e.g. Qwen, Coder390, Fable) on Apple Silicon unified memory faces strict memory and architectural constraints. Standard tooling often causes out-of-memory kernel panics during quantization, corrupts speculative weights during format translation, or fails under background swap.

mlx-agent-tools provides five verified, zero-regression utility modules to overcome these hurdles:

Module Purpose Key Property
stream_convert.py Bounded CPU streaming affine 4-bit quantizer Converts 27B+ checkpoints in 128-row chunks; peak RSS ≤ 3.08 GB.
separate_mtp.py MTP sidecar extractor & repackager Separates embedded MTP tensors from trunk weights to fix RMSNorm double-gain corruption.
inference_window.py Exclusive inference window harness Manages single-large-model transitions, process locks, and multi-turn TTFT measurements.
cpu_checkpoint.py Bounded checkpoint row reader Reads partial tensor slices directly from disk into CPU memory without loading the full model.
redacting_log.py Zero-leakage credential log stream Redacts API keys and secrets across split chunks before stdout/log persistence.

Installation

pip install -e .

Requires Python ≥ 3.10 and mlx ≥ 0.22.0.


Usage

1. Bounded Streaming Quantization (stream_convert.py)

Quantize large checkpoints to MLX affine 4-bit format entirely on CPU with bounded memory:

from mlx_agent_tools import stream_convert

# Converts shard by shard in bounded row chunks
stream_convert(
    source_dir="path/to/bf16_model",
    output_dir="path/to/mlx_q4_model",
    hashes={"model-00001-of-00004.safetensors": {"sha256": "...", "size": 12345678}},
    row_chunk=128
)

2. MTP Sidecar Separation (separate_mtp.py)

When MLX models embed mtp.* weights in primary trunk shards, mlx-lm erroneously applies an extra +1.0 shift to already-normalized RMSNorm weights. separate_mtp extracts MTP weights into a dedicated mtp.safetensors container while preserving exact trunk bytes:

from mlx_agent_tools import separate_mtp

separate_mtp(
    source_dir="path/to/embedded_mtp_model",
    output_dir="path/to/clean_model"
)

3. CPU-Bounded Checkpoint Inspection (cpu_checkpoint.py)

Read exact weight rows from large safetensors files without allocating GPU memory:

from mlx_agent_tools import rows

# Read rows 0..128 of an embedding table
tensor_slice = rows("path/to/model", "model.language_model.embed_tokens.weight", start=0, end=128)

4. Zero-Leakage Credential Redaction (redacting_log.py)

Wrap model server process output streams to guarantee API tokens never reach logs:

import sys
from mlx_agent_tools import SecretLogStream

# Safe streaming wrapper
safe_stdout = SecretLogStream(sys.stdout, secret="sk-live-secret-token")
safe_stdout.write("Serving model with key sk-live-secret-token initialized.\n")
# Output: Serving model with key [redacted] initialized.

Test Suite

All tools are accompanied by a comprehensive test suite (24 unit tests) covering bounded allocation, byte-exactness, SHA-256 verification, and error handling:

python3 -m unittest discover -s tests -p "test_*.py"

Citation

@software{xu2026mlxagenttools,
  title  = {MLX Agent Tools: Reusable Acceleration, Quantization, and Serving Utilities for Apple Silicon},
  author = {Xu, Chengyi and {{Nexora}}},
  year   = {2026},
  month  = oct,
  url    = {https://github.com/chengyixu/mlx-agent-tools}
}

License

MIT License. Copyright (c) 2026 Chengyi Xu and Nexora.