ScalarQuantizer<T>

Models & Types · AiDotNet.RetrievalAugmentedGeneration.VectorSearch.Quantization

Per-dimension scalar quantizer that maps each floating-point component to a single unsigned byte (uint8) using learned min/max ranges. Provides roughly 4x memory reduction versus 32-bit floats (8x versus 64-bit doubles).

Implements: IVectorQuantizer<T>

For Beginners

Imagine each dimension has values between, say, -3 and +5. We slice that range into 256 equal buckets and store just the bucket number (0-255, one byte) instead of the full number. To decode we return the middle of the bucket. Close, but far smaller.

How It Works

Each dimension is linearly mapped from its observed [min, max] range onto the integer range [0, 255]. Reconstruction reverses the mapping using the bin center. This is the same idea used by FAISS ScalarQuantizer (SQ8) and pgvector’s halfvec/quantization paths.

Constructors

Constructor Summary
ScalarQuantizer() Initializes a new instance of the ScalarQuantizer 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)
void Train(IEnumerable<Vector<T>> vectors)