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
//! Training-free vector quantization.
mod bits4;
mod int8;
mod rescore;
/// Actual query quantizer calls on an explicitly observed caller thread.
#[cfg(any(test, feature = "test-seams"))]
#[derive(Default, Debug)]
#[doc(hidden)]
pub struct QueryPreparationTestObservations {
/// Logical dimensions and rounding seed at each Bit4 preparation.
pub bit4: Vec<(usize, u64)>,
/// Logical dimensions at each affine Int8 preparation.
pub int8: Vec<usize>,
}
#[cfg(any(test, feature = "test-seams"))]
thread_local! {
static QUERY_PREPARATIONS: std::cell::RefCell<Option<QueryPreparationTestObservations>> =
const { std::cell::RefCell::new(None) };
}
/// Starts observing actual quantizer calls; ordinary execution retains no trace.
#[cfg(any(test, feature = "test-seams"))]
#[doc(hidden)]
pub fn begin_query_preparation_test_observations() {
QUERY_PREPARATIONS
.with(|calls| *calls.borrow_mut() = Some(QueryPreparationTestObservations::default()));
}
/// Returns recorded calls and disables collection.
#[cfg(any(test, feature = "test-seams"))]
#[doc(hidden)]
pub fn take_query_preparation_test_observations() -> QueryPreparationTestObservations {
QUERY_PREPARATIONS.with(|calls| calls.take().unwrap_or_default())
}
pub(crate) use bits4::{Bit4ControlError, est_dot_bit4_controlled, prepare_bit4_query_controlled};
pub use bits4::{
Bit4Factors, Bit4Query, dequantize_bit4, est_dot_bit4, est_dot_bit4_batch, prepare_bit4_query,
quantize_bit4,
};
pub use int8::{
Int8Query, Int8Vec, dequantize_int8, dot_int8_query, prepare_int8_query, quantize_int8,
};
pub(crate) use rescore::{
ExactScoreReuse, RescoreCheckError, exact_squared_l2_with_sink, rescore_top_k_reusing,
squared_l2_f64,
};
pub use rescore::{
RescoreError, RescoreHit, RescoreMetric, RescorePool, RescoreResult, SearchByteCounts,
rescore_top_k,
};
/// Typed failure from a training-free quantizer or estimator.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum QuantError {
/// Scratch or output backing could not be allocated.
Allocation,
/// The input dimension is zero, for which a code direction is undefined.
EmptyVector,
/// The vector exceeds the dimension supported by the reused i8 kernel.
DimensionTooLarge {
/// Supplied coordinate count.
actual: usize,
/// Largest accepted coordinate count.
maximum: usize,
},
/// A coordinate was NaN or infinite.
NonFinite {
/// Zero-based coordinate of the first rejected value.
index: usize,
},
/// A caller-declared L2-normalized vector was outside the unit-norm band.
NonUnitNorm {
/// Computed squared L2 norm as IEEE binary64 bits.
squared_norm_bits: u64,
/// Absolute permitted deviation from one as IEEE binary64 bits.
tolerance_bits: u64,
},
/// A caller-owned output buffer had the wrong byte length.
OutputLength {
/// Required byte count.
expected: usize,
/// Supplied byte count.
actual: usize,
},
/// A packed row had the wrong byte length for the query dimension.
CodeLength {
/// Required byte count.
expected: usize,
/// Supplied byte count.
actual: usize,
},
/// Unused low-order fields in a final partial byte were not zero.
NonZeroPadding {
/// Final byte containing non-canonical padding.
byte: u8,
/// Mask selecting the unused low-order bits.
mask: u8,
},
}
impl std::fmt::Display for QuantError {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Allocation => formatter.write_str("quantization allocation failed"),
Self::EmptyVector => formatter.write_str("quantization vector must not be empty"),
Self::DimensionTooLarge { actual, maximum } => write!(
formatter,
"quantization dimension {actual} exceeds supported maximum {maximum}"
),
Self::NonFinite { index } => {
write!(
formatter,
"quantization input is non-finite at coordinate {index}"
)
}
Self::NonUnitNorm {
squared_norm_bits,
tolerance_bits,
} => write!(
formatter,
"declared L2-normalized vector has squared norm {}; expected 1 within {}",
f64::from_bits(*squared_norm_bits),
f64::from_bits(*tolerance_bits)
),
Self::OutputLength { expected, actual } => write!(
formatter,
"quantization output length mismatch: expected {expected}, got {actual}"
),
Self::CodeLength { expected, actual } => write!(
formatter,
"packed quantization code length mismatch: expected {expected}, got {actual}"
),
Self::NonZeroPadding { byte, mask } => write!(
formatter,
"packed quantization code has non-zero padding: byte={byte:#04x}, mask={mask:#04x}"
),
}
}
}
impl std::error::Error for QuantError {}
/// Persisted quantization scheme identifier.
///
/// Discriminants are an append-only storage contract. They must never be
/// reordered, renumbered, or reused. Ids 3 and 5 are retired and permanently
/// reserved by `docs/adr/ADR-002-retire-bit1-bit2.md`.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
#[repr(u8)]
pub enum QuantScheme {
/// Uncompressed IEEE binary32 coordinates.
F32 = 0,
/// IEEE binary16 coordinates.
F16 = 1,
/// Signed eight-bit scalar quantization.
Int8 = 2,
/// Four-bit Extended-RaBitQ codes.
#[default]
Bit4 = 4,
}
impl QuantScheme {
/// Returns the permanent persisted identifier.
#[must_use]
pub const fn id(self) -> u8 {
self as u8
}
/// Decodes a permanent persisted identifier.
#[must_use]
pub const fn from_id(id: u8) -> Option<Self> {
match id {
0 => Some(Self::F32),
1 => Some(Self::F16),
2 => Some(Self::Int8),
4 => Some(Self::Bit4),
_ => None,
}
}
}
#[cfg(test)]
mod tests;
#[cfg(test)]
pub(crate) use rescore::rescore_top_k_with_check;