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
//! Quantization type discriminator for QUANT_SEG payloads.
/// Identifies the quantization method stored in a QUANT_SEG.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[repr(u8)]
pub enum QuantType {
/// Scalar quantization (min-max per dimension).
Scalar = 0,
/// Product quantization (codebook per subspace).
Product = 1,
/// Binary threshold quantization (sign bit per dimension).
BinaryThreshold = 2,
/// Residual product quantization.
ResidualPq = 3,
/// RaBitQ-style binary quantization (centroid-centered, randomly
/// rotated sign bits plus per-vector correction scalars).
RaBitQ = 4,
}
impl TryFrom<u8> for QuantType {
type Error = u8;
fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
0 => Ok(Self::Scalar),
1 => Ok(Self::Product),
2 => Ok(Self::BinaryThreshold),
3 => Ok(Self::ResidualPq),
4 => Ok(Self::RaBitQ),
other => Err(other),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn round_trip() {
for raw in 0..=4u8 {
let qt = QuantType::try_from(raw).unwrap();
assert_eq!(qt as u8, raw);
}
}
#[test]
fn invalid_value() {
assert_eq!(QuantType::try_from(5), Err(5));
assert_eq!(QuantType::try_from(255), Err(255));
}
}