BinaryQuantizer<T>
Models & Types · AiDotNet.RetrievalAugmentedGeneration.VectorSearch.Quantization
Sign-based binary quantizer that packs each dimension into a single bit (1 if the component is non-negative, 0 otherwise). Provides roughly 32x memory reduction versus 32-bit floats (64x versus 64-bit doubles) and enables extremely fast Hamming-distance ranking.
Implements: IVectorQuantizer<T>
For Beginners
Instead of remembering the exact value of each dimension we only remember whether it was positive or negative. That single yes/no fact takes one bit, so 8 dimensions fit in one byte. Comparing two vectors then just counts how many of these yes/no answers disagree.
How It Works
Binary quantization is the backbone of “binary embeddings” used by Qdrant and modern retrieval stacks for a cheap first-pass filter before an exact rerank. Distances between binary codes are computed with the Hamming distance (the number of differing bits), which maps to a popcount of the XOR of the two codes.
Constructors
| Constructor | Summary |
|---|---|
BinaryQuantizer() |
Initializes a new instance of the BinaryQuantizer class. |
Properties
| Property | Summary |
|---|---|
bool IsTrained { get; } |
|
int CodeLength { get; } |
|
int Dimension { get; } |
Methods
| Method | Summary |
|---|---|
Vector<T> Decode(byte[] code) |
|
byte[] Encode(Vector<T> vector) |
|
int HammingDistance(byte[] a, byte[] b) |
Computes the Hamming distance (number of differing bits) between two binary codes. |
void Train(IEnumerable<Vector<T>> vectors) |
Records the dimensionality of the vectors. |