You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hybrid KV Cache Compression via Stable Orthogonal Rotation and Topology-Aware Mixed-Precision Quantization for Long-Context LLM Inference on Consumer Hardware
Opensens DarkLab | April 2026
TurboMOQ extends TurboQuant (ICLR 2026) and TurboQuant+ with topology-aware mixed-precision allocation, MLX Metal acceleration, attention-weighted tier demotion, QJL sub-2-bit residual correction, and a llama.cpp C extension for multi-agent long-context inference on consumer hardware (16 GB Apple Silicon).
Core insight: TurboQuant answers how to quantize (rotation + codebooks). MOQ answers where to allocate bits (topology scoring). They are orthogonal and composable. TurboMOQ combines both — and now accelerates them on Metal GPU via MLX.
Key Results (Mac Mini M4 16 GB)
Metric
Value
Key cosine @ 4-bit
0.9754 (benchmark) / 0.9997 (live Ollama)
Value cosine @ 4-bit
0.9843 (benchmark) / 0.9986 (live Ollama)
Context expansion (Qwen3-8B)
55K → 352K (6.4x)
Progressive context
55K → ~752K (13.7x)
Attention-weighted compression
4.2x (avg 3.8 bits)
C extension scoring speedup
2.2x over Python
QJL 1-bit recovery
+3.1% cosine at 1-bit base
Multi-agent capacity @ 4K
13 agents (FP16) → 85 agents (MOQ-2bit)
Tests passing
635
What's New in v0.3.0
Component
What It Does
Impact
MLX Backend
Metal GPU acceleration for rotation, quantize, codebook
Native Apple Silicon; overtakes numpy at dim ≥ 2048
Attention-Weighted Demotion
Tier tokens by actual attention score, not just age
System prompts stay high-fidelity; filler compresses aggressively
QJL Sub-2-bit Residual
1-bit sign correction via JL projection
+3.1% cosine recovery at 1-bit; per-vector scale + multi-round
llama.cpp C Extension
Topology-aware scoring + quantize at kernel level
2.2x faster head scoring; ctypes wrapper with auto-compile
Full Feature Set
Component
What It Does
Impact
Stable QR Rotation
LAPACK QR replaces pure-Python Gram-Schmidt
Cosine 0.265 → 0.995 (275% improvement)
Topology-Aware Allocation
Score heads by importance, allocate bits non-uniformly
Hub heads 6-8 bit, peripheral 1-2 bit
Split K/V Strategy
Rotation for keys, Lloyd-Max codebooks for values
+1.04% value cosine over uniform
Progressive Tiering
4 temporal tiers: Active → Warm → Cold → Archive
13.7x effective context expansion
Attention-Weighted Demotion
5-tier system based on attention percentiles
4.2x compression with pin protection
MLX Metal Backend
GPU-accelerated rotation, quantize, codebook
Native Apple Silicon acceleration
QJL Residual Correction
1-bit sign-encoded JL projection for sub-2-bit
+3.1% cosine recovery at extreme compression
C Extension
Compiled head scoring + quantize + rotation
2.2x scoring speedup, llama.cpp integration ready
Multi-Agent Pool
Shared KV budget with priority eviction
85 agents on 16 GB
Architecture
Input: KV cache per attention head
|
+-----------+-----------+
| KEYS | VALUES |
| | |
v v
HeadScorer HeadScorer ← MOQ topology scoring (Python or C extension)
(importance) (importance) (spectral + gradient + position bell)
| |
v v
Numpy QR / Lloyd-Max ← Split quantization strategy
MLX Rotate Codebook Keys: decorrelate outliers (Metal GPU or CPU)
(LAPACK) (k-means) Values: preserve hub distributions
| |
v v
Symmetric Grid ← Per-head adaptive bit-width (1-8 bits)
Quantize Quantize
| |
v v
[QJL Residual Correction] ← Optional: +3% cosine at sub-2-bit
| |
+-----------+
|
CompressedMOQCache
|
AttentionDemotionPolicy ← Ongoing: reassign tiers by real attention
|
MemoryPool ← Multi-agent: priority eviction within budget
fromturbomoqimportMemoryPoolpool=MemoryPool(budget_mb=8192) # 8 GB poolpool.allocate("research-agent-1", "research", priority=1.0)
pool.allocate("coding-agent-2", "coding", priority=0.8)
pool.store("research-agent-1", compressed_cache)
# Auto-evict lowest priority when over budgetevicted=pool.evict_if_needed()
MLX Metal acceleration (Apple Silicon)
fromturbomoqimportMLX_AVAILABLEifMLX_AVAILABLE:
fromturbomoq.mlx_backendimportMLXRotation, MLXCodebook, MLXQuantizerimportmlx.coreasmx# Metal-accelerated rotationrot=MLXRotation(dim=128, seed=42)
x=mx.random.normal((4096, 128))
x_rot=rot.rotate(x) # Runs on Metal GPUx_hat=rot.unrotate(x_rot) # Perfect roundtrip
Attention-weighted tier demotion
fromturbomoqimportAttentionDemotionPolicypolicy=AttentionDemotionPolicy(pin_threshold=0.5)
policy.register_tokens_batch(list(range(seq_len)))
# Update with real attention weights each decode steppolicy.update_attention(attn_weights, current_step=step)
# Assign compression tiers based on actual importanceresult=policy.assign_tiers()
# result.assignments: {position: AttentionTier}# result.stats: {'compression_ratio': 4.2, 'avg_bits': 3.8, ...}
QJL residual correction (sub-2-bit)
fromturbomoqimportQJLCorrector, QJLConfigcorrector=QJLCorrector(QJLConfig(head_dim=128, jl_dim=128))
# After 1-bit quantization, correct the residualresidual=original-quantized_1bitcorrection=corrector.encode_decode(residual)
corrected=quantized_1bit+correction# Cosine: 0.796 → 0.827 (+3.1%)
# All tests (635 total)
python -m pytest tests/ -q
# New feature tests only
python -m pytest tests/test_mlx_backend.py tests/test_attention_demotion.py \
tests/test_qjl_residual.py tests/test_llamacpp_ext.py -v
# Run benchmark
python benchmarks/benchmark_mac_mini_16gb.py
Build C Extension
cd turbomoq/llamacpp_ext
make # Builds libturbomoq.dylib (macOS) or .so (Linux)# Or auto-compile from Python
python -c "from turbomoq.llamacpp_ext import compile_extension; compile_extension()"
Limitations
MLX crossover at dim ≥ 2048. For typical LLM head_dim (128), numpy on NEON is faster due to Metal kernel launch overhead. MLX benefits large batch/dimension operations.
No Triton/CUDA kernels. On NVIDIA GPU, the original TurboQuant is faster.
No Ollama integration. llama.cpp manages KV cache in C++; the C extension is designed for future llama.cpp integration.
Calibration required. Lloyd-Max codebooks need sample data. Falls back to symmetric quantization without calibration.
Prototype scope. Context-length projections are memory-budget feasibility estimates, not end-to-end perplexity measurements.