File size: 4,052 Bytes
4606829
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# 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](https://img.shields.io/badge/License-MIT-green.svg)](LICENSE)
[![Tests](https://img.shields.io/badge/tests-24%20passed-brightgreen.svg)](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

```bash
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:

```python
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:

```python
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:

```python
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:

```python
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:

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

---

## Citation

```bibtex
@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.