Why long PDFs and multi-file chats bloat RAM or lose needle recall
Long contexts inflate the KV cache linearly (448 MB in FP16 at 16K on Qwen2.5-1.5B), while naive per-token 2-bit KV quantization collapses completely (0/5 Passkey retrieval, 41,622 WT2 PPL).
HOW XFERMAT FIXES IT
Keep 16K sessions accurate in 5.06× less KV memory
xFermat solves long-context memory with two complementary engines: live GQA-packed Mixed KIVI (--kv-bits 2 --kv-outlier-ratio 0.625) promotes 62.5% of salient key channels per block to 4-bit with 2-bit per-token values and a 64-token FP16 tail (3.16 bpw), cutting 16K KV cache 5.06× (448 → 88.6 MB) at 5/5 Passkey retrieval and 10.51 WT2 PPL @ 8K (decode parity at 8K: 43.3 vs 43.6 tok/s). For custom/distilled backbones, 3-stage MOHAWK converts 6 of 24 Qwen2.5-0.5B layers to SSD, cutting 8K cache 24.6% (96.0 → 72.4 MB) and reaching 3.71 vs. 5.54 scratch PPL on equal-compute synthetic runs.
How it works step-by-step
01 / DROP-IN LONG CHAT MEMORY
Pass --kv-bits 4 or --kv-bits 2 --kv-outlier-ratio 0.625
Block-grouped per-channel K quantization handles rotary position outliers cleanly—giving 3.16 bpw Mixed KIVI 5/5 Passkey accuracy through 16K (vs. 0/5 naive 2-bit).
02 / ZERO-PENALTY ATTENTION ROUTING
Automatic fast native Apple attention & Split-KV Metal
flash_tiled_attention sends standard head sizes to Apple Silicon SDPA and uses GQA-packed Stage-1 Metal kernels where grouped-query KIVI unpacking applies.
03 / CONSTANT-MEMORY HYBRID DISTILLATION
3-Stage MOHAWK attention-to-SSD conversion
Converts 6 of 24 attention layers of Qwen2.5-0.5B into Mamba-2 SSD mixers (distill_pretrained_layers_to_ssd), reducing 8K cache 24.6% (96.0 → 72.4 MB) while recovering 14.46 val PPL.
Runnable Python quickstart (long-documents.py)
RUNNABLE PYTHON QUICKSTART
Compress long-context chat & document memory in the Open Runtime
Works immediately with free pip install "xfermat[llm]" (xfermat run <model> --kv-bits 4) or Python FermatKIVICache + FermatModel.
xFermat quickstart (long-documents.py)
# CLI: xfermat run Qwen/Qwen2.5-1.5B-Instruct --kv-bits 4 --prompt "Summarize key termination clauses in this NDA..."
import mlx.core as mx
import mlx_lm
from fermat import FermatConfig, FermatModel, make_kivi_prompt_cache
# 1. Cut live KV memory 2.86x–3.66x on any existing Hugging Face / mlx-lm model
model, tokenizer = mlx_lm.load("Qwen/Qwen2.5-1.5B-Instruct")
kivi_cache = make_kivi_prompt_cache(model, bits=4) # use bits=2 for 5.06x 8K KV reduction
out_text = mlx_lm.generate(model, tokenizer, prompt="List all indemnification obligations:", prompt_cache=kivi_cache, max_tokens=256)
# 2. Custom 3:1 Mamba-2 Hybrid backbone with constant SSD state + KIVI KV cache
hybrid = FermatModel(
FermatConfig(d_model=256, n_heads=4, n_layers=4, d_ff=512, vocab_size=1024, mixer_type="hybrid_ssd_flash", ssd_d_conv=4)
)
tokens = hybrid.generate(mx.random.randint(0, 1024, shape=(1, 512)), max_new_tokens=64, kv_bits=4)
Start with pip install "xfermat[llm]" or book a fleet pilot
Evaluate free on 1 personal Mac via PyPI Open Runtime, or unlock Pro commercial bundling (€39/seat) and Enterprise air-gapped MDM fleets (from €750/mo).
Ready. Ask about Apple Silicon RAM sizing, Clip-GPTQ vs. mlx_lm.awq, --kv-bits 4|2, 2-bit E2E-QP / VPTQ PV-tuning, Qwen2.5-3B 8 GB Mac capacity, or Pro/Enterprise licensing.