Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 4 additions & 20 deletions diskann-benchmark/src/index/bftree/spherical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,7 @@ use diskann_providers::{
model::graph::provider::async_::common::Quantized,
storage::{FileStorageProvider, SaveWith},
};
use diskann_quantization::{
alloc::{AllocatorError, GlobalAllocator, Poly},
spherical::{
iface::{self as spherical_iface, Quantizer},
SphericalQuantizer,
},
};
use diskann_quantization::alloc::GlobalAllocator;
use diskann_utils::views::Matrix;
use rand::SeedableRng;

Expand Down Expand Up @@ -67,16 +61,6 @@ impl BfTreeSpherical {
}
}

fn new_quantizer<const NBITS: usize>(
quantizer: SphericalQuantizer,
) -> Result<Poly<dyn Quantizer>, AllocatorError>
where
spherical_iface::Impl<NBITS>: spherical_iface::Constructible + Quantizer,
{
let imp = spherical_iface::Impl::<NBITS>::new(quantizer)?;
diskann_quantization::poly!(Quantizer, imp, GlobalAllocator)
}

impl Benchmark for BfTreeSpherical {
type Input = BfTreeSphericalBuild;
type Output = BuildResult;
Expand Down Expand Up @@ -153,9 +137,9 @@ impl Benchmark for BfTreeSpherical {

// 2. Dispatch on num_bits to create the type-erased quantizer.
let quantizer_poly = match input.num_bits().get() {
1 => new_quantizer::<1>(quantizer)?,
2 => new_quantizer::<2>(quantizer)?,
4 => new_quantizer::<4>(quantizer)?,
1 => quantizer.as_quantizer::<1>()?,
2 => quantizer.as_quantizer::<2>()?,
4 => quantizer.as_quantizer::<4>()?,
_ => unreachable!("try_match handles bit validation"),
};

Expand Down
22 changes: 4 additions & 18 deletions diskann-benchmark/src/index/bftree/spherical_streaming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@ use diskann_providers::{
model::graph::provider::async_::common::Quantized,
storage::{FileStorageProvider, SaveWith},
};
use diskann_quantization::alloc::{AllocatorError, GlobalAllocator, Poly};
use diskann_quantization::spherical::{
iface::{self as spherical_iface, Quantizer},
SphericalQuantizer,
};
use diskann_quantization::alloc::GlobalAllocator;
use diskann_utils::views::{Matrix, MatrixView};
use rand::SeedableRng;

Expand All @@ -53,16 +49,6 @@ use crate::{
type BfTreeSQProvider = BfTreeProvider<f32, QuantVectorProvider>;
type BfTreeSQIndex = Arc<DiskANNIndex<BfTreeSQProvider>>;

fn new_quantizer<const NBITS: usize>(
quantizer: SphericalQuantizer,
) -> Result<Poly<dyn Quantizer>, AllocatorError>
where
spherical_iface::Impl<NBITS>: spherical_iface::Constructible + Quantizer,
{
let imp = spherical_iface::Impl::<NBITS>::new(quantizer)?;
diskann_quantization::poly!(Quantizer, imp, GlobalAllocator)
}

struct BfTreeSQStream {
index: BfTreeSQIndex,
search: TopkSearchPhase,
Expand Down Expand Up @@ -262,9 +248,9 @@ fn bftree_sq_streaming_impl(
)?;

let quantizer_poly = match input.num_bits().get() {
1 => new_quantizer::<1>(quantizer)?,
2 => new_quantizer::<2>(quantizer)?,
4 => new_quantizer::<4>(quantizer)?,
1 => quantizer.as_quantizer::<1>()?,
2 => quantizer.as_quantizer::<2>()?,
4 => quantizer.as_quantizer::<4>()?,
_ => unreachable!("try_match handles bit validation"),
};

Expand Down
10 changes: 5 additions & 5 deletions diskann-bftree/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,14 @@ use diskann_providers::storage::{LoadWith, SaveWith, StorageReadProvider, Storag
/// let dim = 4;
/// let data = Matrix::new(Init(|| 1.0f32), 4, dim);
/// let mut rng = StdRng::seed_from_u64(42);
/// let sq = SphericalQuantizer::train(
/// let quantizer = SphericalQuantizer::train(
/// data.as_view(), TransformKind::Null,
/// SupportedMetric::SquaredL2, PreScale::None,
/// &mut rng, GlobalAllocator,
/// ).unwrap();
/// let imp = iface::Impl::<1>::new(sq).unwrap();
/// let poly = Poly::new(imp, GlobalAllocator).unwrap();
/// let quantizer: Poly<dyn iface::Quantizer> = poly!(iface::Quantizer, poly);
/// )
Comment on lines +151 to +155
/// .unwrap()
/// .as_quantizer::<1>()
/// .unwrap();
///
/// let parameters = BfTreeProviderParameters {
/// max_points: 5,
Expand Down
12 changes: 5 additions & 7 deletions diskann-bftree/src/quant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,7 @@ impl QuantVectorProvider {
pub(crate) fn create_test_quantizer(dim: usize) -> Poly<dyn Quantizer> {
use diskann_quantization::{
algorithms::TransformKind,
alloc::poly,
spherical::{iface, PreScale, SphericalQuantizer, SupportedMetric},
spherical::{PreScale, SphericalQuantizer, SupportedMetric},
};
use diskann_utils::views::Init;
use diskann_utils::views::Matrix;
Expand All @@ -255,18 +254,17 @@ pub(crate) fn create_test_quantizer(dim: usize) -> Poly<dyn Quantizer> {
);

let mut rng = StdRng::seed_from_u64(42);
let quantizer = SphericalQuantizer::train(
SphericalQuantizer::train(
data.as_view(),
TransformKind::Null,
SupportedMetric::SquaredL2,
PreScale::None,
&mut rng,
GlobalAllocator,
)
.unwrap();

let imp = iface::Impl::<1>::new(quantizer).unwrap();
poly!(Quantizer, imp, GlobalAllocator).unwrap()
.unwrap()
.as_quantizer::<1>()
.unwrap()
}

///////////
Expand Down
18 changes: 9 additions & 9 deletions diskann-quantization/src/spherical/iface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@
//! PreScale::None,
//! &mut rand::rng(),
//! GlobalAllocator
//! ).unwrap();
//! )
//! .unwrap()
//! .as_quantizer::<1>()
//! .unwrap();
//!
//! let quantizer: Box<dyn iface::Quantizer> = Box::new(
//! iface::Impl::<1>::new(quantizer).unwrap()
//! );
//!
//! let alloc = AlignedAllocator::new(PowerOfTwo::new(1).unwrap());
//! let mut buf = Poly::broadcast(u8::default(), quantizer.bytes(), alloc).unwrap();
Expand Down Expand Up @@ -2385,7 +2385,7 @@ mod tests {
)
.unwrap();

(Impl::<NBITS>::new(quantizer).unwrap(), data)
(Impl::new(quantizer).unwrap(), data)
}

#[test]
Expand Down Expand Up @@ -3497,9 +3497,9 @@ mod tests {
metric: SupportedMetric,
transform: DataTransform,
pre_scale: ScaleConfig,
) -> (Impl<NBITS>, Matrix<f32>)
) -> (Poly<dyn Quantizer>, Matrix<f32>)
where
Impl<NBITS>: Constructible,
Impl<NBITS>: Constructible + Quantizer,
{
let data = test_dataset();
let mut rng = StdRng::seed_from_u64(TRAINING_SEED);
Expand All @@ -3514,7 +3514,7 @@ mod tests {
)
.unwrap();

(Impl::<NBITS>::new(quantizer).unwrap(), data)
(quantizer.as_quantizer::<NBITS>().unwrap(), data)
}

fn run_compatibility_test<const NBITS: usize>(
Expand All @@ -3528,7 +3528,7 @@ mod tests {
let dataset = data.as_view();

let baseline = if should_overwrite() {
let baseline = generate_baseline(&quantizer, transform, pre_scale, dataset);
let baseline = generate_baseline(&*quantizer, transform, pre_scale, dataset);
save_baseline(&baseline);
baseline
} else {
Expand Down
14 changes: 13 additions & 1 deletion diskann-quantization/src/spherical/quantizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use thiserror::Error;

use super::{
CompensatedCosine, CompensatedIP, CompensatedSquaredL2, DataMeta, DataMetaError, DataMut,
FullQueryMeta, FullQueryMut, QueryMeta, QueryMut, SupportedMetric,
FullQueryMeta, FullQueryMut, QueryMeta, QueryMut, SupportedMetric, iface,
};
use crate::{
AsFunctor, CompressIntoWith,
Expand Down Expand Up @@ -379,6 +379,18 @@ where
inner_product_with_centroid,
})
}

/// Construct an [`iface::Quantizer`] trait object from `self`.
pub fn as_quantizer<const NBITS: usize>(
self,
) -> Result<Poly<dyn iface::Quantizer>, AllocatorError>
where
A: 'static,
iface::Impl<NBITS, A>: iface::Constructible<A> + iface::Quantizer,
{
let iface = iface::Impl::<NBITS, A>::new(self)?;
crate::poly!({ iface::Quantizer }, iface, GlobalAllocator)
}
}

/// Pre-scaling selector for spherical quantization training. Pre-scaling adjusts the
Expand Down
Loading