Skip to content
Merged
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
2 changes: 1 addition & 1 deletion diskann-quantization/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description.workspace = true
authors.workspace = true
documentation.workspace = true
license.workspace = true
edition.workspace = true
edition = "2024"

[dependencies]
bytemuck = { workspace = true, features = ["derive"] }
Expand Down
2 changes: 1 addition & 1 deletion diskann-quantization/src/__codegen/x86_64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
//! alternatives.
use diskann_wide::{
arch::x86_64::{V3, V4},
Architecture,
arch::x86_64::{V3, V4},
};

use crate::{
Expand Down
2 changes: 1 addition & 1 deletion diskann-quantization/src/algorithms/hadamard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,9 +359,9 @@ where
#[cfg(test)]
mod tests {
use rand::{
SeedableRng,
distr::{Distribution, StandardUniform},
rngs::StdRng,
SeedableRng,
};

use super::*;
Expand Down
36 changes: 28 additions & 8 deletions diskann-quantization/src/algorithms/heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ impl<'a, T: Ord + Copy> SliceHeap<'a, T> {
/// `pos < self.len()` (checked in debug mode).
unsafe fn get_unchecked(&self, pos: usize) -> &T {
debug_assert!(pos < self.len());
self.data.get_unchecked(pos)

// SAFETY: Inherited from caller.
unsafe { self.data.get_unchecked(pos) }
}

/// Swap the two elements as positions `a` and `b`.
Expand Down Expand Up @@ -189,7 +191,7 @@ impl<'a, T: Ord + Copy> SliceHeap<'a, T> {
mod tests {
use std::collections::BinaryHeap;

use rand::{rngs::StdRng, Rng, SeedableRng};
use rand::{Rng, SeedableRng, rngs::StdRng};

use super::*;

Expand Down Expand Up @@ -348,15 +350,25 @@ mod tests {

// Verify they have the same maximum
assert_eq!(
slice_old_max, binary_old_max,
slice_old_max,
binary_old_max,
"Iteration {}: Old maxima differ after updating {} to {}. SliceHeap old max: {:?}, BinaryHeap old max: {:?}",
iteration, slice_old_max.unwrap_or(0), new_value, slice_old_max, binary_old_max
iteration,
slice_old_max.unwrap_or(0),
new_value,
slice_old_max,
binary_old_max
);

assert_eq!(
slice_new_max, binary_new_max,
slice_new_max,
binary_new_max,
"Iteration {}: Maxima differ after updating {} to {}. SliceHeap max: {:?}, BinaryHeap max: {:?}",
iteration, slice_old_max.unwrap_or(0), new_value, slice_new_max, binary_new_max
iteration,
slice_old_max.unwrap_or(0),
new_value,
slice_new_max,
binary_new_max
);

// Verify heap property is maintained in slice heap
Expand Down Expand Up @@ -438,15 +450,23 @@ mod tests {
assert!(
slice[i] >= slice[left],
"Heap property violated: parent {} at index {} < left child {} at index {}. Full heap: {:?}",
slice[i], i, slice[left], left, slice
slice[i],
i,
slice[left],
left,
slice
);
}

if right < slice.len() {
assert!(
slice[i] >= slice[right],
"Heap property violated: parent {} at index {} < right child {} at index {}. Full heap: {:?}",
slice[i], i, slice[right], right, slice
slice[i],
i,
slice[right],
right,
slice
);
}
}
Expand Down
6 changes: 3 additions & 3 deletions diskann-quantization/src/algorithms/kmeans/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,13 +221,13 @@ impl<const N: usize> BlockTranspose<N> {
/// Block must be in-bounds (i.e., `block < self.num_blocks()`).
pub unsafe fn block_ptr_unchecked(&self, block: usize) -> *const f32 {
debug_assert!(block < self.num_blocks());
// SAFETY: If we assume `block < self.num_blocks()`, then
// SAFETY: If we assume `block < self.num_blocks()`, (which the caller attests) then
//
// 1. Our base pointer was allocated in the first place, so this computed offset
// must fit within an `isize`.
// 2. This pointer (and an offset `self.block_stride()`) higher all live within
// a single allocated object.
self.data.as_ptr().add(self.block_offset(block))
unsafe { self.data.as_ptr().add(self.block_offset(block)) }
}

/// Return a pointer to the start of data segment.
Expand Down Expand Up @@ -367,9 +367,9 @@ impl<const N: usize> std::ops::Index<(usize, usize)> for BlockTranspose<N> {
mod tests {
use diskann_utils::{lazy_format, views::Matrix};
use rand::{
Rng, SeedableRng,
distr::{Distribution, Uniform},
rngs::StdRng,
Rng, SeedableRng,
};

use super::*;
Expand Down
6 changes: 3 additions & 3 deletions diskann-quantization/src/algorithms/kmeans/lloyds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use diskann_wide::{SIMDMask, SIMDMulAdd, SIMDPartialOrd, SIMDSelect, SIMDSumTree, SIMDVector};

use super::common::{square_norm, BlockTranspose};
use super::common::{BlockTranspose, square_norm};
use diskann_utils::{
strided::StridedView,
views::{Matrix, MatrixView, MutMatrixView},
Expand Down Expand Up @@ -458,12 +458,12 @@ pub fn lloyds(
#[cfg(test)]
mod tests {
use diskann_utils::{lazy_format, views::Matrix};
use diskann_vector::{distance::SquaredL2, PureDistanceFunction};
use diskann_vector::{PureDistanceFunction, distance::SquaredL2};
use rand::{
Rng, SeedableRng,
distr::{Distribution, Uniform},
rngs::StdRng,
seq::{IndexedRandom, SliceRandom},
Rng, SeedableRng,
};

use super::*;
Expand Down
2 changes: 1 addition & 1 deletion diskann-quantization/src/algorithms/kmeans/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
*/

pub(crate) mod common;
pub(crate) use common::square_norm;
pub use common::BlockTranspose;
pub(crate) use common::square_norm;

pub mod lloyds;
pub mod plusplus;
8 changes: 4 additions & 4 deletions diskann-quantization/src/algorithms/kmeans/plusplus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ use diskann_utils::{
};
use diskann_wide::{SIMDMulAdd, SIMDPartialOrd, SIMDSelect, SIMDVector};
use rand::{
distr::{Distribution, Uniform},
RngCore,
distr::{Distribution, Uniform},
};
use thiserror::Error;

use super::common::{square_norm, BlockTranspose};
use super::common::{BlockTranspose, square_norm};

/// An internal trait implemented for `BlockTranspose` used to accelerate
///
Expand Down Expand Up @@ -520,8 +520,8 @@ pub fn kmeans_plusplus_into(
#[cfg(test)]
mod tests {
use diskann_utils::{lazy_format, views::Matrix};
use diskann_vector::{distance::SquaredL2, PureDistanceFunction};
use rand::{rngs::StdRng, seq::SliceRandom, Rng, SeedableRng};
use diskann_vector::{PureDistanceFunction, distance::SquaredL2};
use rand::{Rng, SeedableRng, rngs::StdRng, seq::SliceRandom};

use super::*;
use crate::utils;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ use std::num::NonZeroUsize;
#[cfg(feature = "flatbuffers")]
use flatbuffers::{FlatBufferBuilder, WIPOffset};
use rand::{
distr::{Distribution, StandardUniform},
Rng,
distr::{Distribution, StandardUniform},
};
use thiserror::Error;

#[cfg(feature = "flatbuffers")]
use super::utils::{bool_to_sign, sign_to_bool};
use super::{
utils::{check_dims, is_sign, subsample_indices, TransformFailed},
TargetDim,
utils::{TransformFailed, check_dims, is_sign, subsample_indices},
};
#[cfg(feature = "flatbuffers")]
use crate::flatbuffers as fb;
Expand Down Expand Up @@ -121,11 +121,7 @@ where
// Generate random signs for the diagonal matrices
let mut sample = |_: usize| {
let sign: bool = StandardUniform {}.sample(rng);
if sign {
0x8000_0000
} else {
0
}
if sign { 0x8000_0000 } else { 0 }
};

// Since implicit zero padding is used for this stage, we only create space for
Expand Down Expand Up @@ -389,11 +385,11 @@ where
#[cfg(test)]
mod tests {
use diskann_utils::lazy_format;
use rand::{rngs::StdRng, SeedableRng};
use rand::{SeedableRng, rngs::StdRng};

use super::*;
use crate::{
algorithms::transforms::{test_utils, Transform, TransformKind},
algorithms::transforms::{Transform, TransformKind, test_utils},
alloc::GlobalAllocator,
};

Expand Down
2 changes: 1 addition & 1 deletion diskann-quantization/src/algorithms/transforms/null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use flatbuffers::{FlatBufferBuilder, WIPOffset};
#[cfg(feature = "flatbuffers")]
use thiserror::Error;

use super::utils::{check_dims, TransformFailed};
use super::utils::{TransformFailed, check_dims};
#[cfg(feature = "flatbuffers")]
use crate::flatbuffers as fb;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ use std::num::NonZeroUsize;
#[cfg(feature = "flatbuffers")]
use flatbuffers::{FlatBufferBuilder, WIPOffset};
use rand::{
distr::{Distribution, StandardUniform},
Rng,
distr::{Distribution, StandardUniform},
};
use thiserror::Error;

#[cfg(feature = "flatbuffers")]
use super::utils::{bool_to_sign, sign_to_bool};
use super::{
utils::{check_dims, is_sign, subsample_indices, TransformFailed},
TargetDim,
utils::{TransformFailed, check_dims, is_sign, subsample_indices},
};
#[cfg(feature = "flatbuffers")]
use crate::flatbuffers as fb;
Expand Down Expand Up @@ -103,11 +103,7 @@ where
let signs = Poly::from_iter(
(0..dim.get()).map(|_| {
let sign: bool = StandardUniform {}.sample(rng);
if sign {
0x8000_0000
} else {
0
}
if sign { 0x8000_0000 } else { 0 }
}),
allocator.clone(),
)?;
Expand Down Expand Up @@ -367,11 +363,11 @@ where
#[cfg(test)]
mod tests {
use diskann_utils::lazy_format;
use rand::{rngs::StdRng, SeedableRng};
use rand::{SeedableRng, rngs::StdRng};

use super::*;
use crate::{
algorithms::transforms::{test_utils, Transform, TransformKind},
algorithms::transforms::{Transform, TransformKind, test_utils},
alloc::GlobalAllocator,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
*/

use diskann_vector::{
Norm, PureDistanceFunction,
distance::{InnerProduct, SquaredL2},
norm::FastL2NormSquared,
Norm, PureDistanceFunction,
};
use rand::{distr::Distribution, rngs::StdRng};
use rand_distr::StandardNormal;
Expand Down
6 changes: 1 addition & 5 deletions diskann-quantization/src/algorithms/transforms/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,7 @@ pub(super) fn sign_to_bool(x: u32) -> bool {

#[cfg(feature = "flatbuffers")]
pub(super) fn bool_to_sign(x: bool) -> u32 {
if x {
0x8000_0000
} else {
0
}
if x { 0x8000_0000 } else { 0 }
}

pub(super) fn subsample_indices<R, A>(
Expand Down
6 changes: 5 additions & 1 deletion diskann-quantization/src/alloc/aligned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ unsafe impl AllocatorCore for AlignedAllocator {
let layout = layout
.align_to(self.alignment())
.expect("invalid layout provided");
GlobalAllocator.deallocate(ptr, layout)

// SAFETY: If the caller upheld the safety contract of `deallocate`, then this
// pointer is safe to deallocate and the layout is compatible with the layout
// created with `allocate`.
unsafe { GlobalAllocator.deallocate(ptr, layout) }
}
}

Expand Down
4 changes: 2 additions & 2 deletions diskann-quantization/src/alloc/bump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use std::{
cell::UnsafeCell,
ptr::NonNull,
sync::{
atomic::{AtomicUsize, Ordering},
Arc,
atomic::{AtomicUsize, Ordering},
},
};

Expand Down Expand Up @@ -172,9 +172,9 @@ unsafe impl AllocatorCore for BumpAllocator {
#[cfg(test)]
mod tests {
use rand::{
SeedableRng,
distr::{Distribution, Uniform},
rngs::StdRng,
SeedableRng,
};

use super::*;
Expand Down
5 changes: 3 additions & 2 deletions diskann-quantization/src/alloc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ mod traits;

pub use aligned::{AlignedAllocator, NotPowerOfTwo};
pub use bump::BumpAllocator;
pub use poly::{poly, CompoundError, Poly, TrustedIter};
pub use poly::{CompoundError, Poly, TrustedIter, poly};
pub use traits::{Allocator, AllocatorCore, AllocatorError};

/// A handle to Rust's global allocator. This type does not support allocations of size 0.
Expand Down Expand Up @@ -86,7 +86,8 @@ unsafe impl AllocatorCore for ScopedAllocator<'_> {
}

unsafe fn deallocate(&self, ptr: NonNull<[u8]>, layout: std::alloc::Layout) {
self.allocator.deallocate(ptr, layout)
// SAFETY: Inherited from caller.
unsafe { self.allocator.deallocate(ptr, layout) }
}
}

Expand Down
6 changes: 3 additions & 3 deletions diskann-quantization/src/binary/quantizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
*/

use crate::{
AsFunctor, CompressInto,
bits::{Binary, MutBitSlice, Representation},
distances::Hamming,
AsFunctor, CompressInto,
};

/// A simple, training-free binary quantizer.
Expand Down Expand Up @@ -129,8 +129,8 @@ impl AsFunctor<Hamming> for BinaryQuantizer {

#[cfg(test)]
mod tests {
use diskann_utils::{views::Matrix, ReborrowMut};
use rand::{rngs::StdRng, seq::SliceRandom, SeedableRng};
use diskann_utils::{ReborrowMut, views::Matrix};
use rand::{SeedableRng, rngs::StdRng, seq::SliceRandom};

use super::*;
use crate::bits::{Binary, BoxedBitSlice};
Expand Down
Loading
Loading