rabitq

package
v0.0.0-...-36c938f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 6, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const TypeByte byte = 3

TypeByte is the wire ordinal for RaBitQ-encoded vectors. Matches Java's VectorType.RABITQ.ordinal() = 3.

Variables

This section is empty.

Functions

This section is empty.

Types

type DistanceEstimate

type DistanceEstimate struct {
	Distance float64
	Error    float64
}

DistanceEstimate holds estimated distance and error bound.

type EncodedVector

type EncodedVector struct {
	// Encoded stores one quantized level per dimension.
	// Each value is in [0, 2^(numExBits+1) - 1].
	// Matches Java's int[] encoded field.
	Encoded []int

	// FAddEx is the precomputed additive factor (||residual||^2 for Euclidean metrics).
	FAddEx float64

	// FRescaleEx is the precomputed rescale factor for the dot product term.
	FRescaleEx float64

	// FErrorEx is the precomputed error bound scaling factor.
	FErrorEx float64

	// NumExBits is the number of extra bits used for quantization (1-8).
	NumExBits int
}

EncodedVector is the quantized representation of a real vector using RaBitQ. Wire-compatible with Java's EncodedRealVector.

func EncodedVectorFromBytes

func EncodedVectorFromBytes(data []byte, numDimensions, numExBits int) (*EncodedVector, error)

EncodedVectorFromBytes deserializes an EncodedVector from bytes. Wire-compatible with Java's EncodedRealVector.fromBytes().

func (*EncodedVector) NumDimensions

func (e *EncodedVector) NumDimensions() int

NumDimensions returns the number of dimensions of the encoded vector.

func (*EncodedVector) ToBytes

func (e *EncodedVector) ToBytes() []byte

ToBytes serializes the encoded vector to bytes, wire-compatible with Java's EncodedRealVector.getRawData(). Format:

[1 byte: type ordinal 3 = RABITQ]
[8 bytes: fAddEx as big-endian float64]
[8 bytes: fRescaleEx as big-endian float64]
[8 bytes: fErrorEx as big-endian float64]
[remaining: bit-packed encoded components, big-endian]

Each component is packed in (numExBits+1) bits, big-endian bit order.

type Metric

type Metric int

Metric identifies a distance metric for RaBitQ quantization. Values match recordlayer.VectorMetric so that a simple type conversion works.

const (
	MetricEuclidean Metric = iota
	MetricCosine
	MetricInnerProduct
)

type Quantizer

type Quantizer struct {
	// contains filtered or unexported fields
}

Quantizer implements recordlayer.VectorQuantizer using RaBitQ.

func NewQuantizer

func NewQuantizer(metric Metric, numExBits int) *Quantizer

NewQuantizer creates a new RaBitQ quantizer implementing VectorQuantizer. numExBits is clamped to [1, 8]; out-of-range values default to 4.

func (*Quantizer) Decode

func (q *Quantizer) Decode(storedBytes []byte, numDimensions int) ([]float64, error)

Decode reconstructs an approximate float64 vector from stored quantized bytes. Used for pairwise distance in the neighbor selection heuristic.

func (*Quantizer) Distance

func (q *Quantizer) Distance(query []float64, storedBytes []byte, numDimensions int) (float64, error)

Distance estimates the distance between a raw query vector and stored quantized bytes. Returns the estimated distance.

func (*Quantizer) Encode

func (q *Quantizer) Encode(vector []float64) []byte

Encode quantizes a float64 vector into compact bytes for storage.

func (*Quantizer) GetTypeByte

func (q *Quantizer) GetTypeByte() byte

GetTypeByte returns the type ordinal byte used as the first byte of encoded data.

func (*Quantizer) NewScorer

func (q *Quantizer) NewScorer(query []float64) *Scorer

NewScorer prepares a scorer for one query vector. Not safe for concurrent use (the component buffer is shared across Score calls).

type RaBitEstimator

type RaBitEstimator struct {
	Metric    Metric
	NumExBits int
}

RaBitEstimator estimates distance between a raw query vector and an encoded vector. Matches Java's RaBitEstimator.

func NewRaBitEstimator

func NewRaBitEstimator(metric Metric, numExBits int) *RaBitEstimator

NewRaBitEstimator creates a new estimator with the given metric and precision.

func (*RaBitEstimator) Distance

func (e *RaBitEstimator) Distance(query []float64, encoded *EncodedVector) (float64, error)

Distance returns the estimated distance between a raw query and an encoded vector. Returns an error if the result is non-finite (Inf or NaN), matching Java's behavior of throwing on non-finite distance estimates.

func (*RaBitEstimator) EstimateDistance

func (e *RaBitEstimator) EstimateDistance(query []float64, encoded *EncodedVector) DistanceEstimate

EstimateDistance estimates the distance between a raw query vector and an encoded vector, returning both the estimated distance and the error bound. Matches Java's RaBitEstimator.estimateDistanceAndErrorBound().

type RaBitQuantizer

type RaBitQuantizer struct {
	NumExBits int
	Metric    Metric
}

RaBitQuantizer implements the RaBitQ quantization scheme for compressing high-dimensional vectors into compact integer-based representations. Matches Java's RaBitQuantizer.

func NewRaBitQuantizer

func NewRaBitQuantizer(metric Metric, numExBits int) *RaBitQuantizer

NewRaBitQuantizer creates a new quantizer with the given metric and bit precision. numExBits must be in [1, 8].

func (*RaBitQuantizer) Encode

func (q *RaBitQuantizer) Encode(vec []float64) *EncodedVector

Encode quantizes a real-valued vector into an EncodedVector. Matches Java's RaBitQuantizer.encode().

type Scorer

type Scorer struct {
	// contains filtered or unexported fields
}

Scorer is a single-threaded, per-query distance estimator: the query's self-dot is computed ONCE and the component buffer is reused across codes. The general Distance path paid a fresh []int allocation, a bit-unpack, an estimator construction AND a re-derived query norm PER CODE — at SPFresh posting-scan volume (thousands of codes per query) that overhead dominated the estimate cost (RFC-094 094.4 tuning).

func (*Scorer) Score

func (s *Scorer) Score(data []byte, numDimensions int) (float64, error)

Score estimates the distance to one stored code. Identical math to Distance (EstimateDistance), minus the per-code allocations.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL