rabitq

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 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

func DotFour

func DotFour(c0, c1, c2, c3 *Code, q []float64) (d0, d1, d2, d3 float64)

DotFour is Dot of four codes of one length with q, at least that long.

func DotPairs

func DotPairs(c0, c1 *Code, q0, q1 []float64) (d00, d01, d10, d11 float64)

DotPairs is Dot of codes c0 and c1, of one length, with queries q0 and q1, each at least that long: four interleaved sums, not inlined so that they stay in registers.

func ValidNumExBits

func ValidNumExBits(numExBits int) bool

ValidNumExBits reports whether the encoder supports numExBits extra bits: 1 to 8, the range Java's RaBitQuantizer constructor checks (RaBitQuantizer.java:76, TIGHT_START's length).

Types

type Code

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

Code is a stored code decoded once, so estimates against it skip the unpack.

func (*Code) Dot

func (c *Code) Dot(q []float64) float64

Dot is the sum Score accumulates for the query q.

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 vectorindex.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 vectorindex.VectorQuantizer using RaBitQ.

func NewQuantizer

func NewQuantizer(metric Metric, numExBits int) *Quantizer

NewQuantizer creates a new RaBitQ quantizer implementing VectorQuantizer. numExBits is kept as given: a count outside what the encoder supports (ValidNumExBits) is refused by the caller where it first quantizes, as Java refuses it where it constructs its RaBitQuantizer, never replaced by another count, which would store codes of a width the configuration does not name.

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) DecodeCode

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

DecodeCode is Decode that also returns the code, from the same unpack.

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) DistanceCode

func (q *Quantizer) DistanceCode(query []float64, code *Code) (float64, error)

DistanceCode is Distance to the bytes code was decoded from by this quantizer.

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).

func (*Quantizer) NumExBits

func (q *Quantizer) NumExBits() int

NumExBits is the quantizer's count of extra bits.

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) Finish

func (s *Scorer) Finish(code *Code, dot float64) (float64, error)

Finish is Score of the code given its Dot sum for this scorer's query.

func (*Scorer) Reset

func (s *Scorer) Reset(query []float64)

Reset makes s the scorer NewScorer(query) of its quantizer, reusing its buffers.

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