Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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"
Comment thread
hildebrandmw marked this conversation as resolved.

[dependencies]
bytemuck = { workspace = true, features = ["derive"] }
Expand Down
4 changes: 3 additions & 1 deletion 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
4 changes: 2 additions & 2 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
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
3 changes: 2 additions & 1 deletion diskann-quantization/src/alloc/mod.rs
Original file line number Diff line number Diff line change
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
4 changes: 3 additions & 1 deletion diskann-quantization/src/bits/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,9 @@ where
{
let count: Len = count.into();
debug_assert_eq!(precursor.precursor_len(), Self::bytes_for(count.value()));
Self::new_unchecked_internal(precursor.precursor_into(), count)

// SAFETY: Inherited from the caller.
unsafe { Self::new_unchecked_internal(precursor.precursor_into(), count) }
}

/// Construct a new `BitSlice` from the `precursor` capable of holding `count` encoded
Expand Down
1 change: 1 addition & 0 deletions diskann-quantization/src/flatbuffers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ macro_rules! import_schema {
($module:ident, $path:literal) => {
// The generated files don't compile cleanly - so we need to suppress some lints.
#[allow(
unsafe_op_in_unsafe_fn,
dead_code,
unused_imports,
clippy::extra_unused_lifetimes,
Expand Down
5 changes: 4 additions & 1 deletion diskann-quantization/src/minmax/multi/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,10 @@ where
/// - The caller guarantees that `ptr` was allocated with the correct layout.
unsafe fn drop(self, ptr: NonNull<u8>) {
let slice_ptr = std::ptr::slice_from_raw_parts_mut(ptr.as_ptr(), self.bytes());
let _ = Box::from_raw(slice_ptr);

// Safety: inherited from caller. All implementations that create a new owned
// `Mat<Self>` are compatible with `Box` deallocation.
let _ = unsafe { Box::from_raw(slice_ptr) };
Comment thread
hildebrandmw marked this conversation as resolved.
Outdated
}
}

Expand Down
19 changes: 15 additions & 4 deletions diskann-quantization/src/multi_vector/matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,13 @@ unsafe impl<T: Copy> Repr for Standard<T> {
debug_assert!(ptr.cast::<T>().is_aligned());
debug_assert!(i < self.nrows);

let row_ptr = ptr.as_ptr().cast::<T>().add(i * self.ncols);
std::slice::from_raw_parts(row_ptr, self.ncols)
// SAFETY: The caller asserts that `i` is less than `self.nrows()`. Since this type
// audits the constructors for `Mat` and friends, we know that there is room for at
// least `self.num_elements()` elements from the base pointer, so this access is safe.
let row_ptr = unsafe { ptr.as_ptr().cast::<T>().add(i * self.ncols) };

// SAFETY: The logic is the same to the previous `unsafe` block.
unsafe { std::slice::from_raw_parts(row_ptr, self.ncols) }
Comment thread
hildebrandmw marked this conversation as resolved.
Outdated
}
}

Expand All @@ -437,8 +442,14 @@ unsafe impl<T: Copy> ReprMut for Standard<T> {
debug_assert!(ptr.cast::<T>().is_aligned());
debug_assert!(i < self.nrows);

let row_ptr = ptr.as_ptr().cast::<T>().add(i * self.ncols);
std::slice::from_raw_parts_mut(row_ptr, self.ncols)
// SAFETY: The caller asserts that `i` is less than `self.nrows()`. Since this type
// audits the constructors for `Mat` and friends, we know that there is room for at
// least `self.num_elements()` elements from the base pointer, so this access is safe.
let row_ptr = unsafe { ptr.as_ptr().cast::<T>().add(i * self.ncols) };

// SAFETY: The logic is the same to the previous `unsafe` block. Further, the caller
// attests that creating a mutable reference is safe.
unsafe { std::slice::from_raw_parts_mut(row_ptr, self.ncols) }
Comment thread
hildebrandmw marked this conversation as resolved.
Outdated
}
}

Expand Down
3 changes: 2 additions & 1 deletion diskann-quantization/src/test_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ unsafe impl AllocatorCore for LimitedAllocator {
}

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

Expand Down
Loading