mirror of
https://github.com/TheTom/turboquant_plus.git
synced 2026-10-19 01:30:50 -05:00
2.5-bit: 4.9× compression (Prince: 4.9×) ✅ 3.5-bit: 3.8× compression (Prince: 3.8×) ✅ Quality at 3.5-bit: MSE 0.0009, cosine 0.947 Quality at 2.5-bit: MSE 0.0029, cosine 0.856 Pending: real KV tensor validation from llama.cpp NIAH test. Co-Authored-By: tturney@psyguard.ai Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
82 lines
2.8 KiB
Python
82 lines
2.8 KiB
Python
"""Compare our TurboQuant outlier results with Prince Canuma's MLX implementation.
|
||
|
||
Run: python3 benchmarks/test_outlier_comparison.py
|
||
"""
|
||
|
||
import numpy as np
|
||
import time
|
||
from turboquant.outlier import OutlierTurboQuant
|
||
from turboquant import TurboQuant
|
||
|
||
|
||
def main():
|
||
print("=" * 70)
|
||
print("TURBOQUANT OUTLIER STRATEGY — PRINCE CANUMA COMPARISON")
|
||
print("=" * 70)
|
||
|
||
d = 128 # Qwen head_dim
|
||
rng = np.random.default_rng(42)
|
||
n_vectors = 1000
|
||
|
||
# Generate test vectors (unit norm, simulating normalized KV)
|
||
X = rng.standard_normal((n_vectors, d))
|
||
X = X / np.linalg.norm(X, axis=1, keepdims=True)
|
||
|
||
print(f"\nTest: {n_vectors} random unit vectors, d={d}")
|
||
print(f"{'─' * 70}")
|
||
print(f"{'Config':<20} {'Eff. Bits':>10} {'Compress':>10} {'MSE':>12} {'Cosine':>10}")
|
||
print(f"{'─' * 70}")
|
||
|
||
configs = [
|
||
("full fp16", 16, None),
|
||
("TurboQuant 2b", 2, "uniform"),
|
||
("TurboQuant 2.5b", 2.5, "outlier"),
|
||
("TurboQuant 3b", 3, "uniform"),
|
||
("TurboQuant 3.5b", 3.5, "outlier"),
|
||
("TurboQuant 4b", 4, "uniform"),
|
||
]
|
||
|
||
for name, bits, mode in configs:
|
||
if mode is None:
|
||
print(f"{'full fp16':<20} {'16.0':>10} {'1.0×':>10} {'0.0':>12} {'1.000000':>10}")
|
||
continue
|
||
|
||
if mode == "outlier":
|
||
q = OutlierTurboQuant(d=d, target_bits=bits, seed=42)
|
||
else:
|
||
q = TurboQuant(d=d, bit_width=int(bits), seed=42)
|
||
|
||
mses = []
|
||
cosines = []
|
||
t0 = time.perf_counter()
|
||
for x in X:
|
||
c = q.quantize(x)
|
||
x_hat = q.dequantize(c)
|
||
mses.append(np.mean((x - x_hat) ** 2))
|
||
cos = np.dot(x, x_hat) / (np.linalg.norm(x) * np.linalg.norm(x_hat))
|
||
cosines.append(cos)
|
||
elapsed = time.perf_counter() - t0
|
||
|
||
ratio = q.compression_ratio() if hasattr(q, 'compression_ratio') else 16 / bits
|
||
|
||
print(f"{name:<20} {bits:>10.1f} {ratio:>9.1f}× {np.mean(mses):>12.6f} {np.mean(cosines):>10.6f}")
|
||
|
||
print(f"\n{'─' * 70}")
|
||
print("PRINCE CANUMA'S MLX RESULTS (Qwen3.5-35B-A3B, real NIAH test):")
|
||
print(f"{'─' * 70}")
|
||
print(f" full: 0.703 GB, 6/6 exact match, 1.0×")
|
||
print(f" TurboQuant 2.5b: 0.143 GB, 6/6 exact match, 4.9× smaller")
|
||
print(f" TurboQuant 3.5b: 0.185 GB, 6/6 exact match, 3.8× smaller")
|
||
|
||
print(f"\n{'─' * 70}")
|
||
print("VERDICT:")
|
||
print(f"{'─' * 70}")
|
||
print(f" ✅ Compression ratios match: 2.5b=4.9×, 3.5b=3.8×")
|
||
print(f" ⏳ Quality validation pending: need real KV tensors from llama.cpp")
|
||
print(f" ⏳ Speed: Python prototype — need Metal/C kernels for real inference")
|
||
print(f"\n Next: extract KV tensors from running Qwen model for NIAH validation")
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|