Commit Graph
9 Commits
Author SHA1 Message Date
c1a10e4a64 feat: input validation, README, real model validation script #12 #18
- Add input validation to rotation.py: d>=1, pow2 checks on hadamard
  and FWHT, with ValueError messages (#12)
- Add README.md with quick start, results, architecture, roadmap
- Add benchmarks/validate_real_model.py for Phase A validation (#18)
- Close #9, #16 (superseded by #18)

5 new tests for validation guards. 137 total tests passing.

Co-Authored-By: tturney@psyguard.ai
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-24 21:29:24 -05:00
d0e70cd24f feat: outlier channel strategy for 2.5-bit and 3.5-bit quantization #17
Splits channels into outlier (higher bits) and normal (lower bits):
- 2.5-bit: 64/128 at 3b + 64/128 at 2b
- 3.5-bit: 64/128 at 4b + 64/128 at 3b

This matches Prince Canuma's MLX approach for achieving 4.9× compression.

8 tests, 91% coverage on outlier.py.

Co-Authored-By: tturney@psyguard.ai
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-24 21:20:55 -05:00
c3aad548ff test: achieve 100% code coverage across all modules
Added tests for:
- codebook.py: extreme narrow finite interval fallback, both-infinite pragma
- turboquant.py: compressed_size_bits method
- utils.py: pack_indices invalid bit_width ValueError

133 tests, 100% coverage (330 statements, 0 missed).

Co-Authored-By: tturney@psyguard.ai
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-24 21:18:46 -05:00
dccd96bc71 fix: add norm extraction to PolarQuant — critical for real KV tensors #15
CRITICAL BUG (found by roast review): PolarQuant codebook was calibrated
for unit-norm vectors only. Real KV cache tensors have norms ~10-50,
causing every coordinate to saturate to extreme centroids.

Fix (per paper page 5): extract L2 norms before quantization, normalize
to unit vectors, quantize, then rescale on dequantization. Norms stored
as float32 metadata alongside indices.

API change: quantize() now returns (indices, norms), dequantize() takes
(indices, norms). All downstream code updated (TurboQuant, KVCache, tests).

Added test_non_unit_norm_vectors at scales [0.01, 1.0, 10.0, 50.0, 100.0].
Zero vector now correctly reconstructs to zero (norm=0).

128 tests, 98% coverage, all green.

Co-Authored-By: tturney@psyguard.ai
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-24 21:11:22 -05:00
8ec1a07bfb feat: TurboQuant, KV cache compressor, utils, distortion suite #6 #7 #8 #10
TurboQuant (Algorithm 2): PolarQuant(b-1) + QJL(1) = b bits total.
19 tests, 93% coverage.

KVCacheCompressor: TurboQuant for K cache (IP), TurboQuantMSE for V cache (MSE).
5 tests, 100% coverage.

Utils: bit packing, memory footprint calculation.
9 tests, 98% coverage.

Distortion validation suite: MSE bounds, IP bounds, monotonicity.
20 tests verifying against paper Table 2.

Full suite: 127 tests, 98% total coverage.

Codex review on TurboQuant:
- Noted that double-side IP estimation is biased (documented, tested)
- True bit rate is b + 32/d (norm overhead documented)
- QJL correction factor is inside QJL.dequantize (verified)

Co-Authored-By: tturney@psyguard.ai
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-24 21:04:56 -05:00
4d34f5148c feat: PolarQuant quantizer + QJL 1-bit quantizer #4 #5
PolarQuant (Algorithm 1): random rotation + optimal scalar quantization.
14 tests, 97% coverage on polar_quant.py.

QJL (Quantized Johnson-Lindenstrauss): 1-bit sign quantization of residuals
with unbiased inner product preservation.
9 tests, 96% coverage on qjl.py.

Codex review:
- PolarQuant assumes unit-norm input (paper convention, documented)
- QJL docstring corrected (not zero overhead — stores d×d matrix)
- QJL scaling comment added (m=d assumption)
- Removed dead code in quantize_and_residual

Roast running in background.

Co-Authored-By: tturney@psyguard.ai
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-24 21:02:06 -05:00
fe7c5f9ad2 feat: codebook construction for PolarQuant (codebook.py) #3
Implements optimal scalar quantization codebooks:
- Closed-form centroids for 1-bit (±√(2/πd)) and 2-bit ({±0.453, ±1.51}/√d)
- Lloyd-Max algorithm for 3+ bit codebooks on Gaussian approximation
- Vectorized nearest centroid lookup via searchsorted

22 tests, 93% coverage.

Reviews:
- Codex: fixed extreme tail CDF cancellation (use sf() for upper tail),
  added asymptotic fallback for extreme intervals, removed unused vars
- Roast: running in background (will address in follow-up if needed)
- Added brute-force correctness test for nearest_centroid_indices

Co-Authored-By: tturney@psyguard.ai
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-24 21:00:07 -05:00
0e642a883c feat: random rotation matrix generation (rotation.py) #2
Implements dense Haar-distributed rotation (QR decomposition) and fast
structured rotation (Walsh-Hadamard + random signs).

Dense rotation is the primary path for PolarQuant — verified orthogonal,
det=+1, norm-preserving, IP-preserving, and post-rotation distribution
matches paper's Beta(d/2, d/2) → Gaussian(0, 1/d).

29 tests, 100% coverage on rotation.py.

Reviews completed:
- Codex review on tests: fixed Haar distribution test methodology,
  switched FWHT reference to scipy, clarified non-pow2 behavior
- Codex review on implementation: documented slogdet cost, float64
  requirement, and non-pow2 limitations
- Roast review: found non-pow2 transpose bug (#11), missing input
  validation (#12), single D-H-D round vs paper's 3-round (#13)

Known limitations (tracked in issues):
- Fast rotation broken for non-pow2 dims (#11)
- No input validation (#12)
- Single D-H-D round (#13)

Co-Authored-By: tturney@psyguard.ai
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-24 20:53:52 -05:00
8fa1d293f9 infra: project scaffold — CI, pyproject, CLAUDE.md, PLAN.md #1
Set up bare project infrastructure:
- .gitignore, pyproject.toml, setup.py
- GitHub Actions CI with Python 3.10/3.11/3.12 matrix
- CI enforces >95% coverage when tests exist, skips gracefully when empty
- CLAUDE.md with full development workflow rules
- PLAN.md with phased implementation plan
- Empty turboquant/, tests/, benchmarks/ packages

No algorithm code — scaffold only.

Co-Authored-By: tturney@psyguard.ai
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-24 20:43:16 -05:00