Algebraic Weight-and-KV Compression with Hardware-Aware Dispatch
An empirical systems monograph uniting 18 scientific works (15 peer-reviewed venue papers and 3 arXiv preprints) with custom Metal Shading Language kernels and hysteresis-guarded autotuning—delivering verified quantization quality across 0.5B, TinyLlama-1.1B, 1.5B, and 3B, unlocking 8 GB Apple Silicon memory limits on 3B with 3-bit & mixed 2/3-bit, extending context to 16K with Mixed KIVI (3.16 bpw, 5/5 Passkey), and achieving 0.0 relative error on wide-exponent GEMM with 150-bit Garner CRT.
Executive Thesis: Consumer Apple Silicon systems share a unified physical memory pool between CPU and GPU. While this removes PCIe transfer overhead, local LLM inference fails whenever weight and KV footprints cross the recommended GPU working-set limit (5.33 GB on 8 GB hardware) or when naive quantization amplifies outlier channels. Worse, academic speedups—from sub-cubic bilinear tensors to ternary slicing—frequently lose to native vendor GEMMs on dense GPU execution. xFermat pairs second-order incoherent compression with strict runtime measurement, rejecting any optimization path that fails end-to-end wall-clock or perplexity parity.
01 / SYSTEM CONSTRAINTS
The Unified-Memory Capacity & Outlier Wall
Measured on an 8 GB Apple Silicon Unified GPU running mlx 0.32.4 and mlx-lm 0.32.2, native BF16 execution hits three structural failure modes before raw FLOP throughput becomes limiting:
FAILURE MODE 01 & 02
Working-Set Swap & Activation Outliers
Standard Qwen2.5-3B-Instruct requires 5,886 MB (~5.75 GiB) for static BF16 weights, crossing the 5.33 GiB recommended working-set limit on 8 GB Apple Silicon systems and aborting (error_or_oom). On 1.5B and 0.5B, naive round-to-nearest (RTN) 3-bit shrinks weights yet degrades WikiText-2 perplexity sharply (17.75 vs. 9.96 BF16 on 1.5B; 39.12 vs. 14.42 BF16 on 0.5B), and RTN 2-bit collapses completely (56,956.39 PPL on 0.5B).
FAILURE MODE 03
RoPE Context Drift & Uncalibrated Asymptotics
Modern LLMs apply high-base Rotary Position Embeddings (θ = 10^6 in Qwen2.5), inducing persistent per-channel Key outliers that crush per-token 2-bit KV caches (0.485–0.557 cosine vs. FP16 SDPA). Meanwhile, theoretically sub-cubic schemes (Strassen 11.15 ms, AlphaEvolve 12.76 ms, GPU CRT FP64 9.40 ms) lose to native vendor paths without hardware autotuning.
02 / SYSTEM DESIGN
Why 18 Papers Require End-to-End Co-Design
Rather than implementing isolated kernels, xFermat links four complementary research lineages inside a single pipeline spanning offline weight surgery, Metal kernel execution, KV memory residency, and empirical backend routing:
Applying a randomized signed Hadamard transform Q = H diag(s) satisfies Q Q^T = I and flattens extreme activation spikes prior to quantization. Folding Q offline into adjacent RMSNorm and weight tensors yields W' = W Q, leaving hadamard_4bit completely native in nn.QuantizedLinear with zero runtime overhead. For hadamard_3bit, a fused SRAM Metal Walsh–Hadamard kernel (R4) rotates residual states dynamically before down_proj. We apply regularized inverse Cholesky Hessian factorization with adaptive damping (damp=0.05) and per-row group scale clipping:
U = chol((X^T X / N + λI)^-1)^T (λ = 0.05 · diag), Δw = (w[:, j] − q(w[:, j], s*)) / U[j, j], s* = argmin_s (w − w_hat(s)) H (w − w_hat(s))^T
In v0.7.0, we introduce mixed_2_3bit: mapping down-projection layers (which concentrate the largest parameter volume) to 2-bit while keeping attention and gate/up projections at 3-bit. On Qwen2.5-3B, this unlocks smooth 8 GB execution at 56.9 tok/s (1,193 MB, 16.01 WT2 PPL) while avoiding the extreme outlier sensitivity of pure scalar 2-bit quantization.
At 2 bits/weight, scalar grids collapse. Vector Post-Training Quantization (VPTQ) groups vectors across 4 channels onto antipodal codebooks. In v0.7.0, two breakthroughs push 2-bit feasibility forward:
VPTQ + PV-Tuning (pv_tune_vq_blocks): Jointly optimizes residual vector codebook centroids and per-vector scaling factors with NaN-guarded AdamW steps and finite-safe rollback. Reaches 26.73 WikiText-2 PPL on Qwen2.5-0.5B at 4.02× compression (234.6 MB) and 16.63 WikiText-2 PPL on Qwen2.5-1.5B at 5.00× compression (589.0 MB, lower WT2 PPL than mlx_native_3bit 17.75, trading lower MC 51.0% vs. 58.0% and decode speed 34.4 vs. 49.2 t/s).
End-to-End Quantized Parameter Tuning (finetune_e2e_qp): Runs straight-through estimator AdamW over quantized representation parameters across entire transformer blocks, reaching 79.36 WikiText-2 PPL on Qwen2.5-0.5B (244.9 MB) without collapsing vocabulary projections.
3.3 16K Mixed KIVI Attention & Pretrained MOHAWK Hybrid Distillation
High-base RoPE frequencies induce severe per-channel Key outliers over long context windows. v0.7.0 introduces Mixed KIVI (kv_quant.py):
16K Mixed KIVI KV Cache: Promotes the top 62.5% salient Key channels per 64-token block to 4-bit (remaining 37.5% at 2-bit), quantizes Values 2-bit per-token, and retains a 64-token FP16 streaming tail. Across a 16,384-token context on Apple Silicon, Mixed KIVI achieves an effective bitwidth of 3.16 bpw, compressing KV memory from 448.0 MB to 88.6 MB (5.06× reduction) while achieving 5/5 Passkey retrieval accuracy and decode parity at 8K (43.3 vs. 43.6 tok/s FP16).
Pretrained Qwen2.5-0.5B MOHAWK Hybrid Distillation: Replaces 6 of 24 quadratic attention layers (layers 3, 7, 11, 15, 19, 23) with linear State-Space Duality (SSD) recurrent mixers. On 3×256 validation subsets, this slashes 8K KV cache memory by −24.56% (96.0 MB → 72.42 MB) while recovering perplexity from 129.96 down to 14.46 val PPL (teacher baseline: 12.50).
Addressing numerical drift in scientific matrix operations and GPU tensor execution:
150-Bit Garner Chinese Remainder Theorem (rns_ozaki.py): Employs 24 pairwise-coprime moduli (packed into 3 × 50-bit Garner limbs with Knuth two-sum combination) plus 11-modulus GPU mantissa slicing, achieving 0.0 relative error across p=25..45 and exact 0.0 error vs. 50-digit reference on ill-conditioned κ=10^12 via ozaki_crt_sliced.
Shuffle FWHT (shuffle_fwht.metal): Exploits SIMD shuffle instructions for fast Walsh–Hadamard transforms, executing a 512×4096 transform in 0.0936 ms vs. 0.1611 ms SRAM (1.72× speedup) and 0.4251 ms dense (4.54× speedup).
Fused SwiGLU + RHT: Fuses elementwise SwiGLU gating with randomized Hadamard transform in a single Metal kernel, running in 0.1812 ms vs. 0.3882 ms naive (2.14× speedup).
VQ4 GEMV: SRAM-tiled 4D vector GEMV decode completes in 0.1044 ms vs. 0.3976 ms FP16 at M=1 (3.81× speedup, crossing over above M≥16 where dense FP16 wins).
Zero-Pad Ternary: Eliminates power-of-2 padding waste, reducing Hadamard padding overhead from 40.6% to 0.0% across 4864 dimensions (with 1 alignment coordinate: 4865).
All measurements reflect verified runs on an 8 GB Apple Silicon Unified GPU from benchmarks/v07_rigorous_results.json, using streamed 16,384 calibration tokens (> D_max), full 301,680-token WikiText-2 on 0.5B and 65,504 tokens on 1.1B/1.5B/3B, 4 MC tasks (n=100), and 5-run median ± IQR timings.
v0.6.0 vs. v0.7.0 Architectural & KPI Progression (Protocol-Normalized)
Withdrawn Early Throughput Claim ("1.47× / 1.44× vs. AWQ"): In v0.6.0, an uncalibrated single-run benchmark reported AWQ 3-bit at 98.3 tok/s vs. xFermat at 144.1 tok/s. Under standardized 5-run median evaluation in v0.7.0 (median ± IQR) on Qwen2.5-0.5B (tied embeddings), mlx_lm.awq 3-bit decodes at 147.7 ± 5.4 tok/s (44.5 GB/s) and xfermat_3bit decodes at 143.0 ± 5.7 tok/s (43.1 GB/s)—both at the memory-bandwidth roofline, with xFermat cutting perplexity −22.7% (17.99 vs. 23.27 PPL) and operating at decode parity in 4-bit (181.8 vs. 173.4 AWQ / 153.3 native, 15.20 vs. 16.06 PPL). The speedup claim is formally withdrawn.
Withdrawn Swap Claim ("1.5B swap-throttled to 21.3 tok/s"): Previous documentation misclassified 1.5B BF16 as swap-throttled. At 2,944.4 MB (~2.88 GiB), Qwen2.5-1.5B BF16 fits below the 5.33 GiB working-set limit and streams steadily at 25.8 ± 0.1 tok/s (79.8 GB/s), which 4-bit quantization accelerates to 67.0 tok/s (matching bandwidth scaling). The genuine memory swap cliff occurs on Qwen2.5-3B (5,886 MB / ~5.75 GiB BF16 static weights, strictly exceeding the 5.33 GiB limit on 8 GB Macs and causing OOM allocation failure), which xFermat rescues at 1,804 MB (4b, 27.8 tok/s, 9.43 PPL), 1,474 MB (3b, 29.1 tok/s, 10.42 PPL), and 1,193 MB (mixed, 56.9 tok/s, 16.01 PPL).
2-Bit VPTQ + NaN-Guarded PV-Tuning (surgery.py): Fixed a training step overflow in AdamW block PV-tuning (math.isfinite gradient/loss checks + finite-safe MSE rollback) enabling stable 5.00× sub-3-bit compression on Qwen2.5-1.5B (16.63 WT2 PPL, 589.0 MB, 51.0% avg 4-task MC accuracy, 34.4 tok/s) alongside Qwen2.5-0.5B PV-tuning (26.73 WT2 PPL, 234.6 MB) and E2E-QP (79.36 WT2 PPL).
Sub-Cubic Tensors, Ternary & CRT Dense Latency: At dense matrix shapes on Apple Silicon, Winograd-Strassen, AlphaEvolve rank-48, 1-slice ternary Metal, and explicit GPU CRT FP64 are slower than native vendor GEMMs. FermatEngine(mode="auto") and backend="auto" enforce empirical median timing + 10% hysteresis so slow academic primitives never leak into default inference paths.
Live KIVI Decode Speed Scope: On Qwen2.5-1.5B at L=4096/8192, Mixed KIVI (3.16 bpw) shrinks KV RAM 5.06× while operating slower than FP16 at 4K (42.9 vs. 51.9 tok/s) and at parity at 8K (43.3 vs. 43.6 tok/s). Microbenchmark speedups up to 2.34× occur specifically in the 4-bit G=7, D=64 synthetic kernel (which drops Passkey accuracy to 1/5 at 16K), whereas 1.5B G=6, D=128 mixed KIVI runs 0.41–0.67× SDPA in isolated kernel timing while delivering critical capacity headroom.
CRT Moduli Note: Exact modular GPU FP64 (rns_ozaki.py) uses 24 pairwise-coprime moduli (packed into 3 × 50-bit Garner limbs) and 11-modulus GPU slicing across p=25..45 and κ=1012.
06 / ADOPTION & ROADMAP
Production Configuration Matrix & Next Milestones
Deployment Goal
Recommended CLI Command / Config
Verified Production Profile
Near-lossless 4-bit inference
xfermat compress MODEL --method hadamard_4bit
181.8 tok/s (0.5B), 15.20 WT2 PPL, 0.55 s load, zero Hadamard overhead
Upcoming v0.8.0 Milestones: (1) Completion of multi-model VPTQ 2-bit scaling runs across 1.5B, 3B, and 7B architectures; (2) Full 32K context evaluation pairing Mixed KIVI with GQA Split-KV decoders on 16 GB Apple Silicon; (3) MOHAWK hybrid distillation scaling from 0.5B to 1.5B and 3B backbones.
07 / COMPLETE CITATION APPENDIX
All 18 Scientific Foundations (15 Peer-Reviewed + 3 arXiv Preprints)
#
Paper & Venue / arXiv
Role in xFermat Architecture
01
Ootomo, Ozaki & Yokota (2024) — DGEMM on Integer Matrix Multiplication Unit (2306.11975) · IJHPCA 2024
Wang, Paliotta, May, Rush & Dao (2024) — The Mamba in the Llama: Distilling & Accelerating Hybrid Models (2408.15237) · NeurIPS 2024
Hybrid attention-to-SSM conversion roadmap
xFermat Concierge
Verified v0.7.0 Specs · Offline Zero-Cost
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.