bitpolar 0.3.3

BitPolar: near-optimal vector quantization with zero training overhead — 3-bit precision, provably unbiased inner products (ICLR 2026)
Documentation
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
//! TurboQuantizer: two-stage composition of PolarQuant + QJL residual.
//!
//! Stage 1: Rotate the vector (Haar QR), then polar-encode it.
//! Stage 2: Compute the reconstruction residual and QJL-sketch it.
//! At query time, combine both estimates for an unbiased inner product.

use crate::error::{validate_finite, Result, TurboQuantError};
use crate::polar::{PolarCode, PolarQuantizer};
use crate::qjl::{QjlQuantizer, QjlSketch};
use crate::rotation::StoredRotation;
use crate::stats::BatchStats;
use crate::traits::{SerializableCode, VectorQuantizer};

// ---------------------------------------------------------------------------
// TurboCode
// ---------------------------------------------------------------------------

/// Compressed representation produced by [`TurboQuantizer`].
///
/// Combines a polar code (Stage 1) and a QJL residual sketch (Stage 2).
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde-support", derive(serde::Serialize, serde::Deserialize))]
pub struct TurboCode {
    /// Stage 1: polar coordinate code
    pub(crate) polar: PolarCode,
    /// Stage 2: QJL sketch of the residual
    pub(crate) residual_sketch: QjlSketch,
}

impl TurboCode {
    /// Approximate heap size in bytes.
    pub fn size_bytes(&self) -> usize {
        self.polar.size_bytes() + self.residual_sketch.size_bytes()
    }
}

// ---------------------------------------------------------------------------
// TurboQuantizer
// ---------------------------------------------------------------------------

/// TurboQuantizer: two-stage vector quantizer.
///
/// Fully defined by four integers `(dim, bits, projections, seed)` —
/// no training data required.
///
/// # Example
///
/// ```rust
/// use bitpolar::TurboQuantizer;
/// use bitpolar::traits::VectorQuantizer;
///
/// let q = TurboQuantizer::new(128, 4, 32, 42).unwrap();
/// let vector = vec![0.1_f32; 128];
/// let code = q.encode(&vector).unwrap();
/// let query = vec![0.05_f32; 128];
/// let score = q.inner_product_estimate(&code, &query).unwrap();
/// let reconstructed = q.decode(&code);
/// assert_eq!(reconstructed.len(), 128);
/// ```
#[derive(Debug, Clone)]
pub struct TurboQuantizer {
    dim: usize,
    bits: u8,
    num_projections: usize,
    seed: u64,
    rotation: StoredRotation,
    polar: PolarQuantizer,
    qjl: QjlQuantizer,
}

impl TurboQuantizer {
    /// Create a new TurboQuantizer.
    ///
    /// # Arguments
    /// - `dim` — vector dimension (must be even and > 0)
    /// - `bits` — polar angle quantization bit width (1..=16)
    /// - `projections` — number of QJL residual projections (> 0)
    /// - `seed` — RNG seed for the rotation and projection matrices
    ///
    /// # Errors
    /// - `ZeroDimension`, `OddDimension`, `InvalidBitWidth`, `ZeroProjections`
    pub fn new(dim: usize, bits: u8, projections: usize, seed: u64) -> Result<Self> {
        if dim == 0 {
            return Err(TurboQuantError::ZeroDimension);
        }
        if dim % 2 != 0 {
            return Err(TurboQuantError::OddDimension(dim));
        }
        let rotation = StoredRotation::new(dim, seed)?;
        let polar = PolarQuantizer::new(dim, bits)?;
        // QJL operates on the residual, which has the same dimension.
        let qjl = QjlQuantizer::new(dim, projections, seed.wrapping_add(1))?;
        Ok(Self { dim, bits, num_projections: projections, seed, rotation, polar, qjl })
    }

    /// The vector dimension.
    pub fn dim(&self) -> usize {
        self.dim
    }

    /// The polar angle quantization bit width.
    pub fn bits(&self) -> u8 {
        self.bits
    }

    /// The number of QJL residual projections.
    pub fn projections(&self) -> usize {
        self.num_projections
    }

    /// The RNG seed.
    pub fn seed(&self) -> u64 {
        self.seed
    }

    /// Encode a vector into a [`TurboCode`].
    ///
    /// # Errors
    /// - `DimensionMismatch`, `NonFiniteInput`
    #[cfg_attr(
        feature = "tracing-support",
        tracing::instrument(
            name = "bitpolar::turbo::encode",
            skip(self, vector),
            fields(dim = self.dim, bits = self.bits, projections = self.num_projections)
        )
    )]
    pub fn encode(&self, vector: &[f32]) -> Result<TurboCode> {
        if vector.len() != self.dim {
            return Err(TurboQuantError::DimensionMismatch {
                expected: self.dim,
                actual: vector.len(),
            });
        }
        validate_finite(vector)?;

        // Stage 1: rotate, then polar-encode.
        let mut rotated = Vec::with_capacity(self.dim);
        self.rotation.apply_slice(vector, &mut rotated);
        let polar = self.polar.encode(&rotated)?;

        // Stage 2: compute residual and QJL-sketch it.
        let reconstructed = self.polar.decode(&polar);
        let residual: Vec<f32> = rotated
            .iter()
            .zip(reconstructed.iter())
            .map(|(orig, recon)| orig - recon)
            .collect();
        let residual_sketch = self.qjl.sketch(&residual)?;

        Ok(TurboCode { polar, residual_sketch })
    }

    /// Decode a [`TurboCode`] back to an approximate f32 vector.
    #[cfg_attr(
        feature = "tracing-support",
        tracing::instrument(
            name = "bitpolar::turbo::decode",
            skip(self, code),
            fields(dim = self.dim, bits = self.bits)
        )
    )]
    pub fn decode(&self, code: &TurboCode) -> Vec<f32> {
        // Decode polar in rotated space.
        let rotated_approx = self.polar.decode(&code.polar);
        // Un-rotate back to original space.
        let mut out = Vec::with_capacity(self.dim);
        self.rotation.apply_inverse_slice(&rotated_approx, &mut out);
        out
    }

    /// Estimate the inner product `<original_vector, query>` from a TurboCode.
    ///
    /// Combines Stage 1 (polar) and Stage 2 (QJL residual) estimates.
    ///
    /// # Errors
    /// - `DimensionMismatch`, `NonFiniteInput`
    #[cfg_attr(
        feature = "tracing-support",
        tracing::instrument(
            name = "bitpolar::turbo::ip_estimate",
            skip(self, code, query),
            fields(dim = self.dim, bits = self.bits, projections = self.num_projections)
        )
    )]
    pub fn inner_product_estimate(&self, code: &TurboCode, query: &[f32]) -> Result<f32> {
        if query.len() != self.dim {
            return Err(TurboQuantError::DimensionMismatch {
                expected: self.dim,
                actual: query.len(),
            });
        }
        validate_finite(query)?;

        // Rotate the query into the same space as the encoded vector.
        let mut rotated_query = Vec::with_capacity(self.dim);
        self.rotation.apply_slice(query, &mut rotated_query);

        // Stage 1: polar inner product estimate.
        let ip_polar = self.polar.inner_product_estimate(&code.polar, &rotated_query)?;

        // Stage 2: QJL residual inner product estimate.
        let ip_residual =
            self.qjl.inner_product_estimate(&code.residual_sketch, &rotated_query)?;

        Ok(ip_polar + ip_residual)
    }

    /// Estimate the L2 distance `||original_vector - query||` from a TurboCode.
    ///
    /// # Errors
    /// - `DimensionMismatch`, `NonFiniteInput`
    #[cfg_attr(
        feature = "tracing-support",
        tracing::instrument(
            name = "bitpolar::turbo::l2_estimate",
            skip(self, code, query),
            fields(dim = self.dim, bits = self.bits)
        )
    )]
    pub fn l2_distance_estimate(&self, code: &TurboCode, query: &[f32]) -> Result<f32> {
        if query.len() != self.dim {
            return Err(TurboQuantError::DimensionMismatch {
                expected: self.dim,
                actual: query.len(),
            });
        }
        validate_finite(query)?;

        let decoded = self.decode(code);
        let sq: f32 = decoded.iter().zip(query.iter()).map(|(a, b)| (a - b).powi(2)).sum();
        Ok(crate::compat::math::sqrtf(sq))
    }

    /// Compute aggregate [`BatchStats`] for a collection of codes.
    pub fn batch_stats(&self, codes: &[TurboCode]) -> BatchStats {
        let count = codes.len();
        let original_bytes = count * self.dim * core::mem::size_of::<f32>();
        let compressed_bytes: usize = codes.iter().map(|c| c.size_bytes()).sum();
        let compression_ratio = if compressed_bytes == 0 {
            0.0
        } else {
            original_bytes as f64 / compressed_bytes as f64
        };
        let bits_per_value = if count == 0 || self.dim == 0 {
            0.0
        } else {
            (compressed_bytes as f64 * 8.0) / (count as f64 * self.dim as f64)
        };
        BatchStats { count, original_bytes, compressed_bytes, compression_ratio, bits_per_value }
    }
}

// ---------------------------------------------------------------------------
// BatchQuantizer impl (parallel feature)
// ---------------------------------------------------------------------------

#[cfg(feature = "parallel")]
impl crate::traits::BatchQuantizer for TurboQuantizer {
    /// Encode multiple vectors in parallel using rayon.
    ///
    /// All vectors must have length `self.dim()`. Returns one [`TurboCode`]
    /// per input vector, in the same order. If any vector fails validation
    /// the first error encountered is returned.
    #[cfg_attr(
        feature = "tracing-support",
        tracing::instrument(
            name = "bitpolar::turbo::batch_encode",
            skip(self, vectors),
            fields(dim = self.dim, bits = self.bits, batch_size = vectors.len())
        )
    )]
    fn batch_encode(&self, vectors: &[&[f32]]) -> crate::error::Result<Vec<Self::Code>> {
        use rayon::prelude::*;
        vectors
            .par_iter()
            .map(|v| self.encode(v))
            .collect::<crate::error::Result<Vec<_>>>()
    }

    /// Estimate inner products between multiple codes and a single query in parallel.
    ///
    /// Returns one score per code, in the same order. The query must have
    /// length `self.dim()`. If the query is invalid the first error is returned.
    #[cfg_attr(
        feature = "tracing-support",
        tracing::instrument(
            name = "bitpolar::turbo::batch_inner_product",
            skip(self, codes, query),
            fields(dim = self.dim, bits = self.bits, batch_size = codes.len())
        )
    )]
    fn batch_inner_product(
        &self,
        codes: &[Self::Code],
        query: &[f32],
    ) -> crate::error::Result<Vec<f32>> {
        use rayon::prelude::*;
        codes
            .par_iter()
            .map(|c| self.inner_product_estimate(c, query))
            .collect::<crate::error::Result<Vec<_>>>()
    }

    /// Decode multiple codes in parallel.
    ///
    /// Returns one reconstructed f32 vector per code, in the same order.
    #[cfg_attr(
        feature = "tracing-support",
        tracing::instrument(
            name = "bitpolar::turbo::batch_decode",
            skip(self, codes),
            fields(dim = self.dim, bits = self.bits, batch_size = codes.len())
        )
    )]
    fn batch_decode(&self, codes: &[Self::Code]) -> Vec<Vec<f32>> {
        use rayon::prelude::*;
        codes.par_iter().map(|c| self.decode(c)).collect()
    }
}

// ---------------------------------------------------------------------------
// VectorQuantizer impl
// ---------------------------------------------------------------------------

impl VectorQuantizer for TurboQuantizer {
    type Code = TurboCode;

    fn encode(&self, vector: &[f32]) -> Result<Self::Code> {
        TurboQuantizer::encode(self, vector)
    }

    fn decode(&self, code: &Self::Code) -> Vec<f32> {
        TurboQuantizer::decode(self, code)
    }

    fn inner_product_estimate(&self, code: &Self::Code, query: &[f32]) -> Result<f32> {
        TurboQuantizer::inner_product_estimate(self, code, query)
    }

    fn l2_distance_estimate(&self, code: &Self::Code, query: &[f32]) -> Result<f32> {
        TurboQuantizer::l2_distance_estimate(self, code, query)
    }

    fn dim(&self) -> usize {
        self.dim
    }

    fn code_size_bytes(&self, code: &Self::Code) -> usize {
        code.size_bytes()
    }
}

// ---------------------------------------------------------------------------
// Compact binary serialization for TurboCode
// ---------------------------------------------------------------------------

impl TurboCode {
    /// Serialize to compact binary.
    ///
    /// Format:
    /// ```text
    /// [version: u8][polar_len: u32 LE][polar_compact_bytes...][qjl_compact_bytes...]
    /// ```
    pub fn to_compact_bytes(&self) -> Vec<u8> {
        let polar_bytes = self.polar.to_compact_bytes();
        let qjl_bytes = self.residual_sketch.to_compact_bytes();

        let mut out =
            Vec::with_capacity(1 + 4 + polar_bytes.len() + qjl_bytes.len());
        out.push(crate::COMPACT_FORMAT_VERSION);
        let polar_len: u32 = polar_bytes.len().try_into().expect(
            "polar payload exceeds u32::MAX; dimension too large for compact format",
        );
        out.extend_from_slice(&polar_len.to_le_bytes());
        out.extend_from_slice(&polar_bytes);
        out.extend_from_slice(&qjl_bytes);
        out
    }

    /// Deserialize from compact binary bytes produced by [`to_compact_bytes`](Self::to_compact_bytes).
    ///
    /// # Errors
    /// - `DeserializationError` if the buffer is too short, has a wrong version,
    ///   or the inner polar/qjl payloads are invalid.
    pub fn from_compact_bytes(bytes: &[u8]) -> Result<Self> {
        let err = |reason: &str| TurboQuantError::DeserializationError {
            reason: reason.to_string(),
        };

        if bytes.is_empty() {
            return Err(err("buffer is empty"));
        }
        if bytes[0] != crate::COMPACT_FORMAT_VERSION {
            return Err(err(&format!(
                "unsupported version 0x{:02X}, expected 0x{:02X}",
                bytes[0],
                crate::COMPACT_FORMAT_VERSION
            )));
        }
        // Need at least version(1) + polar_len(4)
        if bytes.len() < 5 {
            return Err(err("buffer too short for polar length prefix"));
        }
        let polar_len =
            u32::from_le_bytes([bytes[1], bytes[2], bytes[3], bytes[4]]) as usize;
        let polar_start = 5;
        let polar_end = polar_start + polar_len;
        if bytes.len() < polar_end {
            return Err(err("buffer too short for polar payload"));
        }
        let polar = PolarCode::from_compact_bytes(&bytes[polar_start..polar_end])?;
        let qjl_bytes = &bytes[polar_end..];
        if qjl_bytes.is_empty() {
            return Err(err("missing qjl payload"));
        }
        let residual_sketch = QjlSketch::from_compact_bytes(qjl_bytes)?;
        Ok(Self { polar, residual_sketch })
    }
}

impl SerializableCode for TurboCode {
    #[inline]
    fn to_compact_bytes(&self) -> Vec<u8> {
        TurboCode::to_compact_bytes(self)
    }

    #[inline]
    fn from_compact_bytes(bytes: &[u8]) -> Result<Self> {
        TurboCode::from_compact_bytes(bytes)
    }
}

// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::traits::VectorQuantizer;

    #[test]
    fn test_zero_dimension_error() {
        assert!(matches!(TurboQuantizer::new(0, 4, 32, 42), Err(TurboQuantError::ZeroDimension)));
    }

    #[test]
    fn test_odd_dimension_error() {
        assert!(matches!(
            TurboQuantizer::new(3, 4, 32, 42),
            Err(TurboQuantError::OddDimension(3))
        ));
    }

    #[test]
    fn test_encode_decode_shape() {
        let q = TurboQuantizer::new(8, 4, 16, 42).unwrap();
        let v: Vec<f32> = (0..8).map(|i| i as f32 * 0.1).collect();
        let code = q.encode(&v).unwrap();
        let decoded = q.decode(&code);
        assert_eq!(decoded.len(), 8);
    }

    #[test]
    fn test_dimension_mismatch() {
        let q = TurboQuantizer::new(8, 4, 16, 42).unwrap();
        let v = vec![0.0_f32; 4];
        assert!(matches!(q.encode(&v), Err(TurboQuantError::DimensionMismatch { .. })));
    }

    #[test]
    fn test_batch_stats() {
        let q = TurboQuantizer::new(8, 4, 16, 42).unwrap();
        let v: Vec<f32> = (0..8).map(|i| i as f32 * 0.1).collect();
        let codes: Vec<TurboCode> = (0..10).map(|_| q.encode(&v).unwrap()).collect();
        let stats = q.batch_stats(&codes);
        assert_eq!(stats.count, 10);
        assert!(stats.compression_ratio > 0.0);
    }

    #[test]
    fn test_inner_product_positive() {
        let q = TurboQuantizer::new(16, 4, 32, 42).unwrap();
        let v: Vec<f32> = (0..16).map(|i| (i as f32 + 1.0) * 0.1).collect();
        let code = q.encode(&v).unwrap();
        let est = q.inner_product_estimate(&code, &v).unwrap();
        let exact: f32 = v.iter().map(|x| x * x).sum();
        // Should be in the right direction.
        assert!(est > 0.0, "estimate={est} exact={exact}");
    }

    #[test]
    fn test_trait_object_compiles() {
        let q: Box<dyn VectorQuantizer<Code = TurboCode>> =
            Box::new(TurboQuantizer::new(8, 4, 16, 42).unwrap());
        assert_eq!(q.dim(), 8);
    }

    // -----------------------------------------------------------------------
    // Compact serialization tests
    // -----------------------------------------------------------------------

    #[test]
    fn test_turbo_code_roundtrip() {
        let q = TurboQuantizer::new(8, 4, 16, 42).unwrap();
        let v: Vec<f32> = (0..8).map(|i| i as f32 * 0.3 - 1.0).collect();
        let code = q.encode(&v).unwrap();
        let bytes = code.to_compact_bytes();
        let decoded = TurboCode::from_compact_bytes(&bytes).unwrap();
        assert_eq!(decoded.polar.radii, code.polar.radii);
        assert_eq!(decoded.polar.angle_indices, code.polar.angle_indices);
        assert_eq!(decoded.polar.bits, code.polar.bits);
        assert_eq!(decoded.residual_sketch.signs, code.residual_sketch.signs);
        assert_eq!(decoded.residual_sketch.num_projections, code.residual_sketch.num_projections);
        assert_eq!(decoded.residual_sketch.norm, code.residual_sketch.norm);
    }

    #[test]
    fn test_turbo_code_wrong_version() {
        let q = TurboQuantizer::new(8, 4, 16, 42).unwrap();
        let v = vec![0.1_f32; 8];
        let code = q.encode(&v).unwrap();
        let mut bytes = code.to_compact_bytes();
        bytes[0] = 0xFF;
        assert!(matches!(
            TurboCode::from_compact_bytes(&bytes),
            Err(TurboQuantError::DeserializationError { .. })
        ));
    }

    #[test]
    fn test_turbo_code_truncated() {
        let q = TurboQuantizer::new(8, 4, 16, 42).unwrap();
        let v = vec![0.1_f32; 8];
        let code = q.encode(&v).unwrap();
        let bytes = code.to_compact_bytes();
        // Try various truncations
        for len in [0usize, 1, 3, 5, 8] {
            let truncated = &bytes[..len.min(bytes.len() - 1)];
            assert!(
                TurboCode::from_compact_bytes(truncated).is_err(),
                "expected error for len={len}"
            );
        }
    }

    #[test]
    fn test_turbo_code_empty_buffer() {
        assert!(matches!(
            TurboCode::from_compact_bytes(&[]),
            Err(TurboQuantError::DeserializationError { .. })
        ));
    }

    #[test]
    fn test_serializable_code_trait() {
        use crate::traits::SerializableCode;
        let q = TurboQuantizer::new(8, 4, 16, 42).unwrap();
        let v = vec![0.5_f32; 8];
        let code = q.encode(&v).unwrap();
        let bytes = <TurboCode as SerializableCode>::to_compact_bytes(&code);
        let decoded = <TurboCode as SerializableCode>::from_compact_bytes(&bytes).unwrap();
        assert_eq!(decoded.polar.bits, code.polar.bits);
    }

    // -----------------------------------------------------------------------
    // Batch operation tests (compiled always, executed under parallel feature)
    // -----------------------------------------------------------------------

    #[cfg(feature = "parallel")]
    mod batch_tests {
        use super::*;
        use crate::traits::BatchQuantizer;

        fn make_vectors(n: usize, dim: usize) -> Vec<Vec<f32>> {
            (0..n)
                .map(|i| (0..dim).map(|j| ((i * dim + j) as f32 * 0.1).sin()).collect())
                .collect()
        }

        #[test]
        fn test_batch_encode_matches_sequential() {
            let q = TurboQuantizer::new(8, 4, 16, 42).unwrap();
            let vecs = make_vectors(10, 8);
            let refs: Vec<&[f32]> = vecs.iter().map(|v| v.as_slice()).collect();

            let batch_codes = q.batch_encode(&refs).unwrap();
            for (i, code) in batch_codes.iter().enumerate() {
                let seq_code = q.encode(&vecs[i]).unwrap();
                assert_eq!(code.polar.radii, seq_code.polar.radii);
                assert_eq!(code.polar.angle_indices, seq_code.polar.angle_indices);
                assert_eq!(code.residual_sketch.signs, seq_code.residual_sketch.signs);
            }
        }

        #[test]
        fn test_batch_inner_product_matches_sequential() {
            let q = TurboQuantizer::new(8, 4, 16, 42).unwrap();
            let vecs = make_vectors(10, 8);
            let refs: Vec<&[f32]> = vecs.iter().map(|v| v.as_slice()).collect();
            let codes = q.batch_encode(&refs).unwrap();
            let query = vec![0.1_f32; 8];

            let batch_scores = q.batch_inner_product(&codes, &query).unwrap();
            for (i, &score) in batch_scores.iter().enumerate() {
                let seq_score = q.inner_product_estimate(&codes[i], &query).unwrap();
                assert!((score - seq_score).abs() < 1e-6, "score mismatch at index {i}");
            }
        }

        #[test]
        fn test_batch_decode_matches_sequential() {
            let q = TurboQuantizer::new(8, 4, 16, 42).unwrap();
            let vecs = make_vectors(5, 8);
            let refs: Vec<&[f32]> = vecs.iter().map(|v| v.as_slice()).collect();
            let codes = q.batch_encode(&refs).unwrap();

            let batch_decoded = q.batch_decode(&codes);
            for (i, decoded) in batch_decoded.iter().enumerate() {
                let seq_decoded = q.decode(&codes[i]);
                for (a, b) in decoded.iter().zip(seq_decoded.iter()) {
                    assert!((a - b).abs() < 1e-6);
                }
            }
        }

        #[test]
        fn test_batch_encode_empty() {
            let q = TurboQuantizer::new(8, 4, 16, 42).unwrap();
            let codes = q.batch_encode(&[]).unwrap();
            assert!(codes.is_empty());
        }

        #[test]
        fn test_batch_encode_single() {
            let q = TurboQuantizer::new(8, 4, 16, 42).unwrap();
            let v = vec![0.1_f32; 8];
            let refs: &[&[f32]] = &[&v];
            let codes = q.batch_encode(refs).unwrap();
            assert_eq!(codes.len(), 1);
            let seq = q.encode(&v).unwrap();
            assert_eq!(codes[0].polar.radii, seq.polar.radii);
        }

        #[test]
        fn test_batch_encode_error_propagates() {
            let q = TurboQuantizer::new(8, 4, 16, 42).unwrap();
            let v_ok = vec![0.1_f32; 8];
            let v_bad = vec![0.0_f32; 4]; // wrong dim
            let refs: &[&[f32]] = &[&v_ok, &v_bad];
            assert!(q.batch_encode(refs).is_err());
        }

        #[test]
        fn test_batch_inner_product_empty() {
            let q = TurboQuantizer::new(8, 4, 16, 42).unwrap();
            let query = vec![0.1_f32; 8];
            let scores = q.batch_inner_product(&[], &query).unwrap();
            assert!(scores.is_empty());
        }

        #[test]
        fn test_batch_decode_empty() {
            let q = TurboQuantizer::new(8, 4, 16, 42).unwrap();
            let result = q.batch_decode(&[]);
            assert!(result.is_empty());
        }
    }
}