1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//! # bitpolar
//!
//! Zero-overhead vector quantization for semantic search and KV cache compression.
//!
//! Implements three algorithms from Google Research:
//! - **TurboQuant** (ICLR 2026) — two-stage composition for near-optimal compression
//! - **PolarQuant** (AISTATS 2026) — polar coordinate encoding with lossless radii
//! - **QJL** (AAAI 2025) — 1-bit Johnson-Lindenstrauss sketching
//!
//! ## Key Properties
//!
//! - **Data-oblivious**: No training, no codebooks, no calibration data
//! - **Deterministic**: Fully defined by 4 integers: `(dimension, bits, projections, seed)`
//! - **Provably unbiased**: Inner product estimates are unbiased at 3+ bits
//! - **Near-optimal**: Within ~2.7x of Shannon rate-distortion limit
//! - **Instant indexing**: Vectors compress on arrival — no offline training
//!
//! ## Quick Start
//!
//! ```rust
//! use bitpolar::TurboQuantizer;
//! use bitpolar::traits::VectorQuantizer;
//!
//! // Create quantizer from 4 integers — no training needed
//! let q = TurboQuantizer::new(128, 4, 32, 42).unwrap();
//!
//! // Encode a vector
//! let vector = vec![0.1_f32; 128];
//! let code = q.encode(&vector).unwrap();
//!
//! // Estimate inner product without decompression
//! let query = vec![0.05_f32; 128];
//! let score = q.inner_product_estimate(&code, &query).unwrap();
//! println!("Estimated IP: {score}");
//!
//! // Decode back to approximate vector
//! let reconstructed = q.decode(&code);
//! assert_eq!(reconstructed.len(), 128);
//! ```
//!
//! ## Architecture
//!
//! ```text
//! Input f32 vector
//! │
//! ▼
//! ┌─────────────────┐
//! │ Random Rotation │ Haar-distributed orthogonal matrix (QR of Gaussian)
//! │ (StoredRotation) │ Spreads energy uniformly across coordinates
//! └────────┬────────┘
//! │
//! ▼
//! ┌─────────────────┐
//! │ PolarQuant │ Groups d dims into d/2 pairs → polar coords
//! │ (Stage 1) │ Radii: lossless f32 | Angles: b-bit quantized
//! └────────┬────────┘
//! │
//! ▼
//! ┌─────────────────┐
//! │ QJL Residual │ Sketches reconstruction error
//! │ (Stage 2) │ 1 sign bit per projection → unbiased correction
//! └────────┬────────┘
//! │
//! ▼
//! TurboCode { polar: PolarCode, residual_sketch: QjlSketch }
//! ```
//!
//! ## Feature Flags
//!
//! | Feature | Default | Description |
//! |---------|---------|-------------|
//! | `std` | Yes | Standard library (nalgebra QR decomposition) |
//! | `serde-support` | Yes | Serde serialization for all types |
//! | `simd` | No | Hand-tuned NEON/AVX2 kernels |
//! | `parallel` | No | Parallel batch operations via rayon |
//! | `tracing-support` | No | OpenTelemetry-compatible instrumentation |
//! | `ffi` | No | C FFI exports |
// Forbid unsafe in the default code path — only allowed behind simd or ffi features
// Enable no_std when the `std` feature is not active.
// The `alloc` feature provides Vec/String without full std.
extern crate alloc;
// ============================================================================
// Compatibility layer (std/alloc/no_std)
// ============================================================================
/// Compatibility module for std/alloc/no_std switching.
/// All modules import Vec, math functions, etc. from here.
pub
// ============================================================================
// Core modules (always available)
// ============================================================================
/// Error types — all public APIs return `Result<T, TurboQuantError>`
/// Core traits for ecosystem integration: VectorQuantizer, BatchQuantizer, etc.
/// Compression statistics and quality metrics
/// Haar-distributed orthogonal rotation matrix (O(d²) memory)
/// Walsh-Hadamard Transform rotation (O(d) memory, O(d log d) time)
/// Lloyd-Max optimal scalar quantizer for N(0,1) distribution
pub
/// PolarQuant: polar coordinate vector encoding (Stage 1)
/// Quantized Johnson-Lindenstrauss 1-bit sketching (Stage 2)
/// TurboQuantizer: two-stage composition (Polar + QJL)
/// KV cache compressor for transformer attention
/// Online distortion tracking for quality monitoring
/// Tiered quantization: hot, warm, and cold storage tiers
/// Resilient quantization with automatic primary→fallback strategy
/// Oversampled approximate nearest-neighbor search with exact re-ranking
/// Adaptive per-vector bit-width selection with promote/demote
/// Prometheus-compatible metrics export for monitoring
// ============================================================================
// Optional modules (behind feature flags)
// ============================================================================
/// SIMD-accelerated kernels (NEON on aarch64, AVX2 on x86_64)
/// C FFI exports for cross-language bindings.
///
/// Enable with `features = ["ffi"]` in your `Cargo.toml`.
/// Use `cbindgen` to generate the corresponding C header.
// ============================================================================
// Public re-exports — the primary API surface
// ============================================================================
// Error types
pub use ;
// Core quantizers
pub use ;
pub use ;
pub use ;
// KV cache
pub use ;
// Statistics and monitoring
pub use DistortionTracker;
pub use ;
// Rotation
pub use StoredRotation;
pub use WhtRotation;
// Tiered, resilient, and search modules
pub use ;
pub use ;
pub use OversampledSearch;
/// Compact binary format version for all serialized codes.
///
/// Incremented when the wire format changes in a backward-incompatible way.
/// Readers must reject versions they don't understand.
pub const COMPACT_FORMAT_VERSION: u8 = 0x01;