ProductQuantizer<T>

Models & Types · AiDotNet.RetrievalAugmentedGeneration.VectorSearch.Quantization

Product Quantizer (PQ) that splits each vector into M contiguous subspaces, learns a small k-means codebook (default 256 centroids) per subspace, and encodes a vector as one byte per subspace. Supports Asymmetric Distance Computation (ADC) for fast, memory-light nearest-neighbor ranking.

Implements: IVectorQuantizer<T>

For Beginners

We chop each vector into M pieces. For each piece position we learn 256 “typical pieces” (centroids). A vector is then described by which typical piece is closest in each position - just M small numbers. To compare a query we measure the query against those 256 typical pieces once, then adding up the right numbers gives the distance to any stored vector almost for free.

How It Works

PQ is the headline compression technique behind FAISS IndexPQ/IndexIVFPQ. A D-dimensional float vector (4*D bytes) is reduced to just M bytes. With the common choice of 256 centroids per subspace, each subspace index fits in a single byte, so an embedding of dimension 768 encoded with M=96 needs only 96 bytes versus 3072 bytes raw (32x reduction), or 768 bytes as SQ8.

Asymmetric Distance Computation (ADC): at query time we do NOT quantize the query. Instead, for each subspace we precompute the squared L2 distance from the raw query subvector to every centroid of that subspace, building an M x Ksub lookup table. The (approximate) squared distance between the query and any stored code is then just the sum of M table look-ups. This equals the exact squared L2 distance between the raw query and the reconstructed database vector, which is why ADC is both fast and accurate.

Constructors

Constructor Summary
ProductQuantizer(int subspaceCount, int centroidsPerSubspace, int maxIterations, int seed) Initializes a new instance of the ProductQuantizer class.

Properties

Property Summary
bool IsTrained { get; }
double[][][] Codebooks { get; } Gets the learned codebooks with shape [M][Ksub][Dsub], or null if untrained.
int CentroidsPerSubspace { get; } Gets the number of centroids learned per subspace (Ksub).
int CodeLength { get; }
int Dimension { get; }
int SubDimension { get; } Gets the dimensionality of each subspace (Dsub = D / M), or 0 if untrained.
int SubspaceCount { get; } Gets the number of subspaces (M).

Methods

Method Summary
Vector<T> Decode(byte[] code)
byte[] Encode(Vector<T> vector)
double ComputeAsymmetricDistance(double[][] distanceTable, byte[] code) Computes the approximate squared L2 distance between a query (via its ADC table) and an encoded vector, as the sum of one table look-up per subspace.
double[][] BuildDistanceTable(Vector<T> query) Builds the Asymmetric Distance Computation (ADC) lookup table for a query.
void Train(IEnumerable<Vector<T>> vectors)