|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +//! TurboQuant array definition: stores quantized coordinate codes, norms, |
| 5 | +//! centroids (codebook), rotation signs, and optional QJL correction fields. |
| 6 | +
|
| 7 | +use vortex_array::ArrayId; |
| 8 | +use vortex_array::ArrayRef; |
| 9 | +use vortex_array::dtype::DType; |
| 10 | +use vortex_array::stats::ArrayStats; |
| 11 | +use vortex_array::vtable; |
| 12 | +use vortex_error::VortexExpect; |
| 13 | +use vortex_error::VortexResult; |
| 14 | +use vortex_error::vortex_ensure; |
| 15 | + |
| 16 | +/// Encoding marker type for TurboQuant. |
| 17 | +#[derive(Clone, Debug)] |
| 18 | +pub struct TurboQuant; |
| 19 | + |
| 20 | +impl TurboQuant { |
| 21 | + pub const ID: ArrayId = ArrayId::new_ref("vortex.turboquant"); |
| 22 | +} |
| 23 | + |
| 24 | +vtable!(TurboQuant, TurboQuant, TurboQuantData); |
| 25 | + |
| 26 | +/// Protobuf metadata for TurboQuant encoding. |
| 27 | +#[derive(Clone, prost::Message)] |
| 28 | +pub struct TurboQuantMetadata { |
| 29 | + /// Vector dimension d. |
| 30 | + #[prost(uint32, tag = "1")] |
| 31 | + pub dimension: u32, |
| 32 | + /// MSE bits per coordinate (1-8). |
| 33 | + #[prost(uint32, tag = "2")] |
| 34 | + pub bit_width: u32, |
| 35 | + /// Whether QJL correction children are present. |
| 36 | + #[prost(bool, tag = "3")] |
| 37 | + pub has_qjl: bool, |
| 38 | +} |
| 39 | + |
| 40 | +/// Optional QJL (Quantized Johnson-Lindenstrauss) correction for unbiased |
| 41 | +/// inner product estimation. When present, adds 3 additional children. |
| 42 | +#[derive(Clone, Debug)] |
| 43 | +pub struct QjlCorrection { |
| 44 | + /// Sign bits: `BoolArray`, length `num_rows * padded_dim`. |
| 45 | + pub(crate) signs: ArrayRef, |
| 46 | + /// Residual norms: `PrimitiveArray<f32>`, length `num_rows`. |
| 47 | + pub(crate) residual_norms: ArrayRef, |
| 48 | + /// QJL rotation signs: `BoolArray`, length `3 * padded_dim` (inverse order). |
| 49 | + pub(crate) rotation_signs: ArrayRef, |
| 50 | +} |
| 51 | + |
| 52 | +impl QjlCorrection { |
| 53 | + /// The QJL sign bits. |
| 54 | + pub fn signs(&self) -> &ArrayRef { |
| 55 | + &self.signs |
| 56 | + } |
| 57 | + |
| 58 | + /// The residual norms. |
| 59 | + pub fn residual_norms(&self) -> &ArrayRef { |
| 60 | + &self.residual_norms |
| 61 | + } |
| 62 | + |
| 63 | + /// The QJL rotation signs (BoolArray, inverse application order). |
| 64 | + pub fn rotation_signs(&self) -> &ArrayRef { |
| 65 | + &self.rotation_signs |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +/// Slot positions for TurboQuantArray children. |
| 70 | +#[repr(usize)] |
| 71 | +#[derive(Clone, Copy, Debug)] |
| 72 | +pub(crate) enum Slot { |
| 73 | + Codes = 0, |
| 74 | + Norms = 1, |
| 75 | + Centroids = 2, |
| 76 | + RotationSigns = 3, |
| 77 | + QjlSigns = 4, |
| 78 | + QjlResidualNorms = 5, |
| 79 | + QjlRotationSigns = 6, |
| 80 | +} |
| 81 | + |
| 82 | +impl Slot { |
| 83 | + pub(crate) const COUNT: usize = 7; |
| 84 | + |
| 85 | + pub(crate) fn name(self) -> &'static str { |
| 86 | + match self { |
| 87 | + Self::Codes => "codes", |
| 88 | + Self::Norms => "norms", |
| 89 | + Self::Centroids => "centroids", |
| 90 | + Self::RotationSigns => "rotation_signs", |
| 91 | + Self::QjlSigns => "qjl_signs", |
| 92 | + Self::QjlResidualNorms => "qjl_residual_norms", |
| 93 | + Self::QjlRotationSigns => "qjl_rotation_signs", |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + pub(crate) fn from_index(idx: usize) -> Self { |
| 98 | + match idx { |
| 99 | + 0 => Self::Codes, |
| 100 | + 1 => Self::Norms, |
| 101 | + 2 => Self::Centroids, |
| 102 | + 3 => Self::RotationSigns, |
| 103 | + 4 => Self::QjlSigns, |
| 104 | + 5 => Self::QjlResidualNorms, |
| 105 | + 6 => Self::QjlRotationSigns, |
| 106 | + _ => vortex_error::vortex_panic!("invalid slot index {idx}"), |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +/// TurboQuant array. |
| 112 | +/// |
| 113 | +/// Slots (always present): |
| 114 | +/// - 0: `codes` — `FixedSizeListArray<u8>` (quantized indices, list_size=padded_dim) |
| 115 | +/// - 1: `norms` — `PrimitiveArray<f32>` (one per vector row) |
| 116 | +/// - 2: `centroids` — `PrimitiveArray<f32>` (codebook, length 2^bit_width) |
| 117 | +/// - 3: `rotation_signs` — `BitPackedArray` (3 * padded_dim, 1-bit u8 0/1, inverse order) |
| 118 | +/// |
| 119 | +/// Optional QJL slots (None when MSE-only): |
| 120 | +/// - 4: `qjl_signs` — `FixedSizeListArray<u8>` (num_rows * padded_dim, 1-bit) |
| 121 | +/// - 5: `qjl_residual_norms` — `PrimitiveArray<f32>` (one per row) |
| 122 | +/// - 6: `qjl_rotation_signs` — `BitPackedArray` (3 * padded_dim, 1-bit, QJL rotation) |
| 123 | +#[derive(Clone, Debug)] |
| 124 | +pub struct TurboQuantData { |
| 125 | + pub(crate) dtype: DType, |
| 126 | + pub(crate) slots: Vec<Option<ArrayRef>>, |
| 127 | + pub(crate) dimension: u32, |
| 128 | + pub(crate) bit_width: u8, |
| 129 | + pub(crate) stats_set: ArrayStats, |
| 130 | +} |
| 131 | + |
| 132 | +impl TurboQuantData { |
| 133 | + /// Build a TurboQuant array with MSE-only encoding (no QJL correction). |
| 134 | + #[allow(clippy::too_many_arguments)] |
| 135 | + pub fn try_new_mse( |
| 136 | + dtype: DType, |
| 137 | + codes: ArrayRef, |
| 138 | + norms: ArrayRef, |
| 139 | + centroids: ArrayRef, |
| 140 | + rotation_signs: ArrayRef, |
| 141 | + dimension: u32, |
| 142 | + bit_width: u8, |
| 143 | + ) -> VortexResult<Self> { |
| 144 | + vortex_ensure!( |
| 145 | + (1..=8).contains(&bit_width), |
| 146 | + "MSE bit_width must be 1-8, got {bit_width}" |
| 147 | + ); |
| 148 | + let mut slots = vec![None; Slot::COUNT]; |
| 149 | + slots[Slot::Codes as usize] = Some(codes); |
| 150 | + slots[Slot::Norms as usize] = Some(norms); |
| 151 | + slots[Slot::Centroids as usize] = Some(centroids); |
| 152 | + slots[Slot::RotationSigns as usize] = Some(rotation_signs); |
| 153 | + Ok(Self { |
| 154 | + dtype, |
| 155 | + slots, |
| 156 | + dimension, |
| 157 | + bit_width, |
| 158 | + stats_set: Default::default(), |
| 159 | + }) |
| 160 | + } |
| 161 | + |
| 162 | + /// Build a TurboQuant array with QJL correction (MSE + QJL). |
| 163 | + #[allow(clippy::too_many_arguments)] |
| 164 | + pub fn try_new_qjl( |
| 165 | + dtype: DType, |
| 166 | + codes: ArrayRef, |
| 167 | + norms: ArrayRef, |
| 168 | + centroids: ArrayRef, |
| 169 | + rotation_signs: ArrayRef, |
| 170 | + qjl: QjlCorrection, |
| 171 | + dimension: u32, |
| 172 | + bit_width: u8, |
| 173 | + ) -> VortexResult<Self> { |
| 174 | + vortex_ensure!( |
| 175 | + (1..=8).contains(&bit_width), |
| 176 | + "MSE bit_width must be 1-8, got {bit_width}" |
| 177 | + ); |
| 178 | + let mut slots = vec![None; Slot::COUNT]; |
| 179 | + slots[Slot::Codes as usize] = Some(codes); |
| 180 | + slots[Slot::Norms as usize] = Some(norms); |
| 181 | + slots[Slot::Centroids as usize] = Some(centroids); |
| 182 | + slots[Slot::RotationSigns as usize] = Some(rotation_signs); |
| 183 | + slots[Slot::QjlSigns as usize] = Some(qjl.signs); |
| 184 | + slots[Slot::QjlResidualNorms as usize] = Some(qjl.residual_norms); |
| 185 | + slots[Slot::QjlRotationSigns as usize] = Some(qjl.rotation_signs); |
| 186 | + Ok(Self { |
| 187 | + dtype, |
| 188 | + slots, |
| 189 | + dimension, |
| 190 | + bit_width, |
| 191 | + stats_set: Default::default(), |
| 192 | + }) |
| 193 | + } |
| 194 | + |
| 195 | + /// The vector dimension d. |
| 196 | + pub fn dimension(&self) -> u32 { |
| 197 | + self.dimension |
| 198 | + } |
| 199 | + |
| 200 | + /// MSE bits per coordinate. |
| 201 | + pub fn bit_width(&self) -> u8 { |
| 202 | + self.bit_width |
| 203 | + } |
| 204 | + |
| 205 | + /// Padded dimension (next power of 2 >= dimension). |
| 206 | + pub fn padded_dim(&self) -> u32 { |
| 207 | + self.dimension.next_power_of_two() |
| 208 | + } |
| 209 | + |
| 210 | + /// Whether QJL correction is present. |
| 211 | + pub fn has_qjl(&self) -> bool { |
| 212 | + self.slots[Slot::QjlSigns as usize].is_some() |
| 213 | + } |
| 214 | + |
| 215 | + fn slot(&self, idx: usize) -> &ArrayRef { |
| 216 | + self.slots[idx] |
| 217 | + .as_ref() |
| 218 | + .vortex_expect("required slot is None") |
| 219 | + } |
| 220 | + |
| 221 | + /// The quantized codes child (FixedSizeListArray). |
| 222 | + pub fn codes(&self) -> &ArrayRef { |
| 223 | + self.slot(Slot::Codes as usize) |
| 224 | + } |
| 225 | + |
| 226 | + /// The norms child (`PrimitiveArray<f32>`). |
| 227 | + pub fn norms(&self) -> &ArrayRef { |
| 228 | + self.slot(Slot::Norms as usize) |
| 229 | + } |
| 230 | + |
| 231 | + /// The centroids (codebook) child (`PrimitiveArray<f32>`). |
| 232 | + pub fn centroids(&self) -> &ArrayRef { |
| 233 | + self.slot(Slot::Centroids as usize) |
| 234 | + } |
| 235 | + |
| 236 | + /// The MSE rotation signs child (BitPackedArray, length 3 * padded_dim). |
| 237 | + pub fn rotation_signs(&self) -> &ArrayRef { |
| 238 | + self.slot(Slot::RotationSigns as usize) |
| 239 | + } |
| 240 | + |
| 241 | + /// The optional QJL correction fields, reconstructed from slots. |
| 242 | + pub fn qjl(&self) -> Option<QjlCorrection> { |
| 243 | + Some(QjlCorrection { |
| 244 | + signs: self.slots[Slot::QjlSigns as usize].clone()?, |
| 245 | + residual_norms: self.slots[Slot::QjlResidualNorms as usize].clone()?, |
| 246 | + rotation_signs: self.slots[Slot::QjlRotationSigns as usize].clone()?, |
| 247 | + }) |
| 248 | + } |
| 249 | + |
| 250 | + /// Set the QJL correction fields on this array. |
| 251 | + pub(crate) fn set_qjl(&mut self, qjl: QjlCorrection) { |
| 252 | + self.slots[Slot::QjlSigns as usize] = Some(qjl.signs); |
| 253 | + self.slots[Slot::QjlResidualNorms as usize] = Some(qjl.residual_norms); |
| 254 | + self.slots[Slot::QjlRotationSigns as usize] = Some(qjl.rotation_signs); |
| 255 | + } |
| 256 | +} |
0 commit comments