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
//! PolarQuant: polar coordinate vector encoding (Stage 1 of TurboQuant).
//!
//! Groups the `d`-dimensional rotated vector into `d/2` pairs, converts each
//! pair `(x, y)` to polar coordinates `(r, θ)`, stores radii losslessly as
//! f32, and quantizes angles to `bits` bits using a Lloyd-Max codebook.

use crate::codebook::LloydMaxCodebook;
use crate::error::{validate_finite, Result, TurboQuantError};
use crate::traits::{SerializableCode, VectorQuantizer};

// ---------------------------------------------------------------------------
// PolarCode
// ---------------------------------------------------------------------------

/// Compressed representation produced by [`PolarQuantizer`].
///
/// Contains lossless f32 radii and quantized angle indices.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde-support", derive(serde::Serialize, serde::Deserialize))]
pub struct PolarCode {
    /// Lossless radii for each coordinate pair (length = dim/2)
    pub(crate) radii: Vec<f32>,
    /// Quantized angle indices (length = dim/2)
    pub(crate) angle_indices: Vec<u16>,
    /// Bit width used to quantize angles
    pub(crate) bits: u8,
}

impl PolarCode {
    /// Approximate heap size in bytes.
    pub fn size_bytes(&self) -> usize {
        self.radii.len() * core::mem::size_of::<f32>()
            + self.angle_indices.len() * core::mem::size_of::<u16>()
            + 1 // bits
    }
}

// ---------------------------------------------------------------------------
// PolarQuantizer
// ---------------------------------------------------------------------------

/// PolarQuant vector quantizer.
///
/// Encodes a `dim`-dimensional f32 vector into polar coordinates:
/// - Radii stored losslessly (exact f32)
/// - Angles quantized to `bits` bits
///
/// # Example
///
/// ```rust
/// use bitpolar::polar::PolarQuantizer;
/// use bitpolar::traits::VectorQuantizer;
///
/// let q = PolarQuantizer::new(8, 4).unwrap();
/// let v = vec![0.1_f32; 8];
/// let code = q.encode(&v).unwrap();
/// let approx = q.decode(&code);
/// assert_eq!(approx.len(), 8);
/// ```
#[derive(Debug, Clone)]
pub struct PolarQuantizer {
    dim: usize,
    bits: u8,
    codebook: LloydMaxCodebook,
}

impl PolarQuantizer {
    /// Create a new PolarQuantizer.
    ///
    /// # Arguments
    /// - `dim` — must be even and > 0
    /// - `bits` — angle quantization bit width (1..=16)
    ///
    /// # Errors
    /// - `ZeroDimension`, `OddDimension`, `InvalidBitWidth`
    pub fn new(dim: usize, bits: u8) -> Result<Self> {
        if dim == 0 {
            return Err(TurboQuantError::ZeroDimension);
        }
        if dim % 2 != 0 {
            return Err(TurboQuantError::OddDimension(dim));
        }
        // num_pairs = dim/2 is stored as u16 in compact format; reject oversized dims.
        if dim / 2 > u16::MAX as usize {
            return Err(TurboQuantError::DimensionTooLarge(dim, u16::MAX as usize * 2));
        }
        let codebook = LloydMaxCodebook::compute(bits)?;
        Ok(Self { dim, bits, codebook })
    }

    /// The dimension this quantizer was created for.
    pub fn dim(&self) -> usize {
        self.dim
    }

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

    // -----------------------------------------------------------------------
    // Core encode / decode helpers
    // -----------------------------------------------------------------------

    #[inline]
    fn encode_inner(&self, vector: &[f32]) -> PolarCode {
        let pairs = self.dim / 2;
        let mut radii = Vec::with_capacity(pairs);
        let mut angle_indices = Vec::with_capacity(pairs);

        for i in 0..pairs {
            let x = vector[2 * i];
            let y = vector[2 * i + 1];
            let r = crate::compat::math::sqrtf(x * x + y * y);
            let theta = crate::compat::math::atan2f(y, x); // in [-π, π]
            radii.push(r);
            // Map θ from [-π, π] to a Gaussian-like value for the codebook.
            // We store θ directly and use the boundary-finding quantize step.
            // The codebook is calibrated for N(0,1); angle is ~ Uniform[-π,π].
            // We normalize to approximate Gaussian scale: divide by π/√3.
            let normalised = theta / core::f32::consts::PI; // in [-1, 1]
            angle_indices.push(self.codebook.quantize(normalised));
        }

        PolarCode { radii, angle_indices, bits: self.bits }
    }

    #[inline]
    fn decode_inner(&self, code: &PolarCode) -> Vec<f32> {
        let pairs = self.dim / 2;
        let mut out = vec![0.0_f32; self.dim];

        for i in 0..pairs.min(code.radii.len()).min(code.angle_indices.len()) {
            let r = code.radii[i];
            let normalised = self.codebook.dequantize(code.angle_indices[i]);
            let theta = normalised * core::f32::consts::PI;
            out[2 * i] = r * crate::compat::math::cosf(theta);
            out[2 * i + 1] = r * crate::compat::math::sinf(theta);
        }

        out
    }

    // -----------------------------------------------------------------------
    // Estimation helpers
    // -----------------------------------------------------------------------

    /// Estimate inner product `<original_vector, query>` from a PolarCode.
    #[cfg_attr(
        feature = "tracing-support",
        tracing::instrument(
            name = "bitpolar::polar::ip_estimate",
            skip(self, code, query),
            fields(dim = self.dim, bits = self.bits)
        )
    )]
    pub fn inner_product_estimate(&self, code: &PolarCode, 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_inner(code);
        let ip = decoded.iter().zip(query.iter()).map(|(a, b)| a * b).sum();
        Ok(ip)
    }

    /// Estimate L2 distance `||original_vector - query||` from a PolarCode.
    #[cfg_attr(
        feature = "tracing-support",
        tracing::instrument(
            name = "bitpolar::polar::l2_estimate",
            skip(self, code, query),
            fields(dim = self.dim, bits = self.bits)
        )
    )]
    pub fn l2_distance_estimate(&self, code: &PolarCode, 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_inner(code);
        let sq: f32 = decoded.iter().zip(query.iter()).map(|(a, b)| (a - b).powi(2)).sum();
        Ok(crate::compat::math::sqrtf(sq))
    }
}

// ---------------------------------------------------------------------------
// Compact binary serialization for PolarCode
// ---------------------------------------------------------------------------

impl PolarCode {
    /// Serialize to compact binary.
    ///
    /// Format:
    /// ```text
    /// [version: u8 = 0x01][bits: u8][num_pairs: u16 LE]
    /// [radii: num_pairs × f32 LE][angle_indices: num_pairs × u16 LE]
    /// ```
    pub fn to_compact_bytes(&self) -> Vec<u8> {
        let num_pairs: u16 = self.radii.len().try_into().expect(
            "PolarCode num_pairs exceeds u16::MAX; dimension too large for compact format",
        );
        let mut out = Vec::with_capacity(
            1 + 1 + 2 + self.radii.len() * 4 + self.angle_indices.len() * 2,
        );
        out.push(crate::COMPACT_FORMAT_VERSION);
        out.push(self.bits);
        out.extend_from_slice(&num_pairs.to_le_bytes());
        for &r in &self.radii {
            out.extend_from_slice(&r.to_le_bytes());
        }
        for &a in &self.angle_indices {
            out.extend_from_slice(&a.to_le_bytes());
        }
        out
    }

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

        if bytes.len() < 4 {
            return Err(err("buffer too short: need at least 4 bytes"));
        }
        if bytes[0] != crate::COMPACT_FORMAT_VERSION {
            return Err(err(&format!(
                "unsupported version 0x{:02X}, expected 0x{:02X}",
                bytes[0],
                crate::COMPACT_FORMAT_VERSION
            )));
        }
        let bits = bytes[1];
        if bits == 0 || bits > 16 {
            return Err(err(&format!(
                "invalid bit width {bits}: must be 1..=16"
            )));
        }
        let num_pairs = u16::from_le_bytes([bytes[2], bytes[3]]) as usize;

        // 4 bytes header + num_pairs * f32 (4 bytes) + num_pairs * u16 (2 bytes)
        let payload_size = num_pairs
            .checked_mul(4)
            .and_then(|r| r.checked_add(num_pairs.checked_mul(2)?))
            .ok_or_else(|| err("num_pairs causes size overflow"))?;
        let expected_len = 4 + payload_size;
        if bytes.len() < expected_len {
            return Err(err(&format!(
                "buffer too short: need {expected_len}, got {}",
                bytes.len()
            )));
        }

        let mut radii = Vec::with_capacity(num_pairs);
        let radii_start = 4;
        for i in 0..num_pairs {
            let off = radii_start + i * 4;
            let r = f32::from_le_bytes([bytes[off], bytes[off + 1], bytes[off + 2], bytes[off + 3]]);
            radii.push(r);
        }

        let angles_start = radii_start + num_pairs * 4;
        let mut angle_indices = Vec::with_capacity(num_pairs);
        for i in 0..num_pairs {
            let off = angles_start + i * 2;
            let a = u16::from_le_bytes([bytes[off], bytes[off + 1]]);
            angle_indices.push(a);
        }

        Ok(Self { radii, angle_indices, bits })
    }
}

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

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

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

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

    /// Estimate inner products between multiple codes and a single query in parallel.
    ///
    /// Returns one score per code in the same order.
    #[cfg_attr(
        feature = "tracing-support",
        tracing::instrument(
            name = "bitpolar::polar::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]) -> Result<Vec<f32>> {
        use rayon::prelude::*;
        codes
            .par_iter()
            .map(|c| self.inner_product_estimate(c, query))
            .collect::<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::polar::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 PolarQuantizer {
    type Code = PolarCode;

    #[cfg_attr(
        feature = "tracing-support",
        tracing::instrument(
            name = "bitpolar::polar::encode",
            skip(self, vector),
            fields(dim = self.dim, bits = self.bits)
        )
    )]
    fn encode(&self, vector: &[f32]) -> Result<Self::Code> {
        if vector.len() != self.dim {
            return Err(TurboQuantError::DimensionMismatch {
                expected: self.dim,
                actual: vector.len(),
            });
        }
        validate_finite(vector)?;
        Ok(self.encode_inner(vector))
    }

    #[cfg_attr(
        feature = "tracing-support",
        tracing::instrument(
            name = "bitpolar::polar::decode",
            skip(self, code),
            fields(dim = self.dim, bits = self.bits)
        )
    )]
    fn decode(&self, code: &Self::Code) -> Vec<f32> {
        self.decode_inner(code)
    }

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

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

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

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

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

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

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

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

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

    #[test]
    fn test_non_finite_error() {
        let q = PolarQuantizer::new(4, 4).unwrap();
        let v = vec![1.0_f32, f32::NAN, 0.0, 0.0];
        assert!(matches!(q.encode(&v), Err(TurboQuantError::NonFiniteInput { .. })));
    }

    #[test]
    fn test_zero_vector_encode_decode() {
        let q = PolarQuantizer::new(4, 4).unwrap();
        let v = vec![0.0_f32; 4];
        let code = q.encode(&v).unwrap();
        let decoded = q.decode(&code);
        assert_eq!(decoded.len(), 4);
        // All radii are 0, so decoded values should be ~0.
        for x in decoded {
            assert!(x.abs() < 1e-5);
        }
    }

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

    #[test]
    fn test_polar_code_roundtrip() {
        let q = PolarQuantizer::new(8, 4).unwrap();
        let v: Vec<f32> = (0..8).map(|i| i as f32 * 0.25 - 1.0).collect();
        let code = q.encode(&v).unwrap();
        let bytes = code.to_compact_bytes();
        let decoded = PolarCode::from_compact_bytes(&bytes).unwrap();
        assert_eq!(decoded.bits, code.bits);
        assert_eq!(decoded.radii, code.radii);
        assert_eq!(decoded.angle_indices, code.angle_indices);
    }

    #[test]
    fn test_polar_code_roundtrip_empty_pairs() {
        // 2-dim → 1 pair
        let q = PolarQuantizer::new(2, 1).unwrap();
        let v = vec![1.0_f32, 0.0];
        let code = q.encode(&v).unwrap();
        let bytes = code.to_compact_bytes();
        let back = PolarCode::from_compact_bytes(&bytes).unwrap();
        assert_eq!(back.radii.len(), 1);
    }

    #[test]
    fn test_polar_code_wrong_version() {
        let q = PolarQuantizer::new(4, 4).unwrap();
        let v = vec![0.5_f32; 4];
        let code = q.encode(&v).unwrap();
        let mut bytes = code.to_compact_bytes();
        bytes[0] = 0xAB;
        assert!(matches!(
            PolarCode::from_compact_bytes(&bytes),
            Err(TurboQuantError::DeserializationError { .. })
        ));
    }

    #[test]
    fn test_polar_code_truncated() {
        let q = PolarQuantizer::new(8, 4).unwrap();
        let v = vec![0.1_f32; 8];
        let code = q.encode(&v).unwrap();
        let bytes = code.to_compact_bytes();
        // Truncate to 5 bytes (header OK but payload missing)
        let truncated = &bytes[..5];
        assert!(matches!(
            PolarCode::from_compact_bytes(truncated),
            Err(TurboQuantError::DeserializationError { .. })
        ));
    }

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

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

    // -----------------------------------------------------------------------
    // Batch operation tests
    // -----------------------------------------------------------------------

    #[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).cos()).collect())
                .collect()
        }

        #[test]
        fn test_batch_encode_matches_sequential() {
            let q = PolarQuantizer::new(8, 4).unwrap();
            let vecs = make_vectors(8, 8);
            let refs: Vec<&[f32]> = vecs.iter().map(|v| v.as_slice()).collect();
            let batch = q.batch_encode(&refs).unwrap();
            for (i, code) in batch.iter().enumerate() {
                let seq = q.encode(&vecs[i]).unwrap();
                assert_eq!(code.radii, seq.radii);
                assert_eq!(code.angle_indices, seq.angle_indices);
            }
        }

        #[test]
        fn test_batch_inner_product_matches_sequential() {
            let q = PolarQuantizer::new(8, 4).unwrap();
            let vecs = make_vectors(8, 8);
            let refs: Vec<&[f32]> = vecs.iter().map(|v| v.as_slice()).collect();
            let codes = q.batch_encode(&refs).unwrap();
            let query = vec![0.2_f32; 8];
            let batch_scores = q.batch_inner_product(&codes, &query).unwrap();
            for (i, &score) in batch_scores.iter().enumerate() {
                let seq = q.inner_product_estimate(&codes[i], &query).unwrap();
                assert!((score - seq).abs() < 1e-6);
            }
        }

        #[test]
        fn test_batch_decode_matches_sequential() {
            let q = PolarQuantizer::new(8, 4).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_dec = q.batch_decode(&codes);
            for (i, dec) in batch_dec.iter().enumerate() {
                let seq = q.decode(&codes[i]);
                for (a, b) in dec.iter().zip(seq.iter()) {
                    assert!((a - b).abs() < 1e-6);
                }
            }
        }

        #[test]
        fn test_batch_empty() {
            let q = PolarQuantizer::new(8, 4).unwrap();
            assert!(q.batch_encode(&[]).unwrap().is_empty());
            assert!(q.batch_inner_product(&[], &[0.0_f32; 8]).unwrap().is_empty());
            assert!(q.batch_decode(&[]).is_empty());
        }

        #[test]
        fn test_batch_single() {
            let q = PolarQuantizer::new(8, 4).unwrap();
            let v = vec![0.3_f32; 8];
            let codes = q.batch_encode(&[&v]).unwrap();
            assert_eq!(codes.len(), 1);
        }

        #[test]
        fn test_batch_encode_error_propagates() {
            let q = PolarQuantizer::new(8, 4).unwrap();
            let v_bad = vec![0.0_f32; 3]; // wrong dim
            assert!(q.batch_encode(&[&v_bad]).is_err());
        }
    }
}