Skip to main content

aft_store/vector/
mod.rs

1//! The vector plane: embeddings for meaning search, stored once per family
2//! and searched exactly.
3//!
4//! A vector is keyed by the model that made it and the exact text it was
5//! made from ([`TextDigest`]); that text includes the file's path, so
6//! vectors are per (path, chunk text), and every checkout holding the same
7//! file at the same path shares them. A chunk map per (content, path) lists
8//! a file's chunks in order, each by the text it was embedded from.
9//!
10//! Each vector is stored twice: as int8 codes with the values that bound
11//! their error, which every search scans, and as the f32 vector the
12//! embedder returned, read only for the few members that could still reach
13//! the top results. The answer equals today's f32 brute-force answer: the
14//! same members, in the same order, with the same score bits (see
15//! [`VectorIndex::search`]).
16//!
17//! The codes are per-vector symmetric int8 of each vector's offset from its
18//! segment's mean vector (`cᵢ = round((vᵢ − μᵢ) / s)`, with
19//! `s = max |vᵢ − μᵢ| / 127`). Each vector also stores its residual norm
20//! and its largest component residual, so the bound needs nothing else.
21//! Centering was chosen over plain per-vector and per-dimension scaling by
22//! measurement (`examples/vector_gate.rs`): it rescored the fewest members on
23//! every model measured.
24
25mod model;
26mod score;
27mod search;
28mod sections;
29
30#[cfg(test)]
31mod tests;
32
33pub use model::{ChunkMapKey, ModelId, ModelIdentity, TextDigest};
34pub use score::{dot_product, rank_order, score, vector_norm};
35pub use search::{
36    Coverage, FileMembers, FullScan, Hit, MappedChunk, Member, MemberSet, ScoreBounds,
37    SearchReport, SearchRequest, SearchResult, SharedVectorIndex, UnitRef, VectorIndex,
38    default_rescore_limit,
39};
40pub use sections::{
41    Chunk, ChunkDetail, FileDetails, KNOWN, MAX_DIMENSIONS, SectionBytes, VectorSectionsBuilder,
42    VectorSegment, VectorSegmentParts,
43};
44
45use crate::segment::SegmentError;
46
47/// Why a vector operation failed.
48#[derive(Debug, thiserror::Error)]
49pub enum VectorError {
50    #[error(transparent)]
51    Segment(#[from] SegmentError),
52    /// The family's segments hold no vector of this model: meaning search
53    /// has nothing to search yet, which is not the same as finding nothing.
54    #[error("no vectors are stored for model {0}")]
55    NoVectors(ModelId),
56    #[error("the model has {expected} dimensions; the vector has {found}")]
57    DimensionMismatch { expected: u32, found: usize },
58    #[error("a model with {0} dimensions is not supported")]
59    UnsupportedDimensions(u32),
60    #[error("a vector has a component that is not finite, or a norm that overflows")]
61    NonFinite,
62    #[error("a member names vector {} of segment {}, which the index does not have", .0.ordinal, .0.segment)]
63    UnknownUnit(UnitRef),
64    #[error("the member set was prepared for another vector index")]
65    ForeignMembers,
66    #[error("more members than a search can rank")]
67    TooManyMembers,
68    #[error("more segments than an index can hold")]
69    TooManySegments,
70    /// Details given with a chunk map do not describe its chunks.
71    #[error("chunk details do not describe the chunk map's chunks")]
72    DetailsMismatch,
73}