Skip to main content

HnswIndex

Struct HnswIndex 

Source
pub struct HnswIndex {
    pub m: usize,
    pub m_max0: usize,
    pub ef_construction: usize,
    pub entry_point: u32,
    pub max_level: u32,
    pub ef_search: usize,
    /* private fields */
}
Expand description

A built HNSW index. Reads borrow from the on-disk payload at open time; the graph is owned (small relative to embeddings).

Fields§

§m: usize§m_max0: usize§ef_construction: usize§entry_point: u32§max_level: u32§ef_search: usize

the search beam floor, ef_construction at decode time: a caller’s ef widens the beam above it (max(ef, ef_search)) and never narrows it, so the recall measured at build stays the floor too.

Implementations§

Source§

impl HnswIndex

Source

pub fn build( vectors: Vec<f32>, n: usize, dim: usize, m: usize, ef_construction: usize, seed: u64, ) -> Self

Build an HNSW index from f32 vectors. Deterministic given the same seed. vectors is row-major n*dim.

Source

pub fn build_from_int8( view: &Int8EmbeddingsView<'_>, m: usize, ef: usize, seed: u64, ) -> Self

Build from int8 quantized embeddings. Dequantizes once (lossy) and hands off to build. Recall ends up bounded by quantization noise; in practice still well above 0.95 @ k=10 for real corpora.

Source

pub fn build_from_f16( bytes: &[u8], n: usize, dim: usize, m: usize, ef: usize, seed: u64, ) -> Self

Build from float16 LE bytes. Decodes once into f32 and builds.

Source

pub fn build_from_f32( bytes: &[u8], n: usize, dim: usize, m: usize, ef: usize, seed: u64, ) -> Self

Build from raw f32 LE bytes. Copies into an owned buffer.

Source§

impl HnswIndex

Source

pub fn to_bytes(&self) -> Vec<u8> ⓘ

Encode the index to bytes for embedding in section 0x07 (v2).

Source

pub fn from_bytes( bytes: &[u8], n: usize, dim: usize, ) -> Result<Self, RuntimeError>

Parse an HNSW payload (v1 or v2). The vectors are reconstructed from the embeddings section by the caller; this constructor is for the on-disk graph only. Call attach_vectors before search.

Source§

impl HnswIndex

Source

pub fn attach_vectors(&mut self, vectors: Vec<f32>)

Attach f32 vectors as the search store. Kept for callers that already hold an f32 buffer (build, tests); the runtime open path uses attach_store to avoid an f32 expansion.

Source

pub fn level0_neighbors(&self, i: usize) -> &[u32]

Level-0 (densest layer) out-neighbors of node i, or an empty slice if i is out of range. Read-only accessor the graph build path uses to derive top-m semantic edges from the already-built hnsw graph without re-running an O(n^2) exact knn. The neighbor SET is the build-order hnsw adjacency; the graph builder sorts canonically before writing.

Source

pub fn search(&self, q: &[f32], ef: usize) -> Vec<usize>

Search for the ef closest candidates to q. Returns ids only - the runtime reranks with the exact dot product to produce the final cosine score. the beam is max(ef, self.ef_search): ef widens the search above the file’s floor and never narrows it.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.