Skip to content

Commit 2946cee

Browse files
danieldkDaniël de Kok
authored andcommitted
Update to reductive 0.9
1 parent c79324d commit 2946cee

3 files changed

Lines changed: 22 additions & 6 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ ndarray = "0.15"
2424
ordered-float = "2"
2525
rand = "0.8"
2626
rand_chacha = "0.3"
27-
reductive = "0.8"
27+
reductive = "0.9"
2828
serde = { version = "1", features = ["derive"] }
2929
smallvec = "1.7"
3030
thiserror = "1"

src/chunks/storage/quantized.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@ use std::io::{Read, Seek, SeekFrom, Write};
33
use std::mem::size_of;
44

55
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
6+
#[cfg(feature = "memmap")]
7+
pub use mmap::MmapQuantizedArray;
68
use ndarray::{Array, Array1, Array2, ArrayView1, ArrayView2, Axis, CowArray, IntoDimension, Ix1};
79
use rand::{CryptoRng, RngCore, SeedableRng};
10+
use rand_chacha::ChaChaRng;
11+
use reductive::error::ReductiveError;
812
use reductive::pq::{Pq, QuantizeVector, Reconstruct as _, TrainPq};
913

1014
use super::{sealed::CloneFromMapping, Storage, StorageView};
@@ -419,6 +423,17 @@ where
419423
T: TrainPq<f32>,
420424
R: CryptoRng + RngCore + SeedableRng + Send,
421425
{
426+
// Since we are storing quantized values in an u8 array, we have a stricter
427+
// upper bound than reductive.
428+
let max_subquantizer_bits = std::cmp::min(8, (self.shape().0 as f64).log2().trunc() as u32);
429+
if n_subquantizer_bits > max_subquantizer_bits {
430+
return Err(Error::QuantizationError(
431+
ReductiveError::IncorrectNSubquantizerBits {
432+
max_subquantizer_bits,
433+
},
434+
));
435+
}
436+
422437
let (embeds, norms) = if normalize {
423438
let norms = self.view().outer_iter().map(|e| e.dot(&e).sqrt()).collect();
424439
let mut normalized = self.view().to_owned();
@@ -437,7 +452,8 @@ where
437452
n_attempts,
438453
embeds.view(),
439454
&mut rng,
440-
)?;
455+
)
456+
.map_err(Error::QuantizationError)?;
441457

442458
let quantized_embeddings = quantizer.quantize_batch(embeds.view());
443459

@@ -668,10 +684,6 @@ mod mmap {
668684
}
669685
}
670686

671-
#[cfg(feature = "memmap")]
672-
pub use mmap::MmapQuantizedArray;
673-
use rand_chacha::ChaChaRng;
674-
675687
#[cfg(test)]
676688
mod tests {
677689
use std::io::{Cursor, Read, Seek, SeekFrom};

src/error.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::io;
44

55
use ndarray::ShapeError;
66
use rand::Error as RandError;
7+
use reductive::error::ReductiveError;
78
use thiserror::Error;
89

910
/// `Result` type alias for operations that can lead to I/O errors.
@@ -44,6 +45,9 @@ pub enum Error {
4445

4546
#[error("Can't convert {from:?} to {to:?}")]
4647
ConversionError { from: String, to: String },
48+
49+
#[error("Cannot quantize embeddings")]
50+
QuantizationError(#[source] ReductiveError),
4751
}
4852

4953
impl Error {

0 commit comments

Comments
 (0)