rabitq

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package rabitq defines TreeDB's clean-room rabitq_1bit v1 reference codec.

The package is a correctness oracle for durable asset builders and future accelerated scorers. It deliberately does not publish collection assets or integrate search. The v1 storage contract is quantizedasset RolePackedCodes backed by typed-column packed_bit_vector rows: one LSB-first bit per code dimension, zero high padding bits in the final byte, and CodeWidthBits == 1.

For cosine column_graph indexes, callers encode finite non-zero float32 vectors after unit-L2 normalization. The reference rotation is a deterministic seeded signed permutation followed by a normalized Walsh-Hadamard transform over next-power-of-two padded dimensions. Data codes store the sign of each rotated component. Query encoding stores the rotated query sign bits and absolute per-dimension weights used by the pure-Go weighted-popcount scorer.

Index

Constants

View Source
const (
	// CodecName is the durable quantized codec name for TreeDB RaBitQ v1.
	CodecName = "rabitq_1bit"
	// CodecVersion is the first TreeDB RaBitQ codec contract version.
	CodecVersion uint32 = 1
	// CodeWidthBits is the number of stored bits per code dimension.
	CodeWidthBits = 1

	// StorageRole is the quantizedasset role selected by the v1 contract.
	StorageRole = "packed_codes"
	// StorageLogicalType is the typed-column logical/physical code shape.
	StorageLogicalType = "packed_bit_vector"
	// StorageEncoding is the raw typed-column encoding for StorageLogicalType.
	StorageEncoding = "raw_packed_bit_vector"
	// BitOrder documents the TreeDB packed-code bit order used on disk.
	BitOrder = "lsb0"

	// RotationName is part of the canonical codec config identity.
	RotationName = "signed_permutation_fwht_padded_v1"
	// DefaultSeed is the deterministic v1 seed used when callers do not supply a
	// workload-specific seed. It is intentionally non-zero and stable.
	DefaultSeed uint64 = 0x7261626974710001
)
View Source
const ByteMismatchTableEntries = 256

ByteMismatchTableEntries is the number of per-byte XOR masks in a query-local mismatch-weight table.

Variables

View Source
var (
	// ErrInvalidConfig reports an impossible or unsupported codec shape.
	ErrInvalidConfig = errors.New("rabitq: invalid config")
	// ErrDimensionMismatch reports vector/query/code shape mismatch.
	ErrDimensionMismatch = errors.New("rabitq: dimension mismatch")
	// ErrDegenerateVector reports zero, non-finite, or otherwise unencodable input.
	ErrDegenerateVector = errors.New("rabitq: degenerate vector")
)

Functions

func PrepareQueryByteMismatchWeights

func PrepareQueryByteMismatchWeights(query Query, ws *Workspace) ([]float64, float64, bool)

PrepareQueryByteMismatchWeights builds query-local byte/XOR mask tables for the v1 weighted sign-dot scorer. The returned slice aliases ws and remains valid until the next operation that reuses the same workspace scratch.

func QueryByteMismatchWeightsValid

func QueryByteMismatchWeightsValid(query Query, weights []float64, queryWeightSum float64) bool

QueryByteMismatchWeightsValid reports whether weights has the shape returned by PrepareQueryByteMismatchWeights for query.

Types

type Config

type Config struct {
	Seed uint64
}

Config is the v1 codec configuration identity. Vector dimensions are schema shape rather than serialized config; NewPlan binds Config to dimensions.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns the stable TreeDB rabitq_1bit v1 config.

func (Config) CanonicalBytes

func (c Config) CanonicalBytes() []byte

CanonicalBytes returns the stable byte identity used for manifest config comparison and hashing. The format is line-oriented ASCII by design so docs, tests, and future non-Go implementations can reproduce it exactly.

func (Config) Hash64

func (c Config) Hash64() uint64

Hash64 returns the FNV-1a hash of CanonicalBytes. Quantized manifests may use this as CodecDescriptor.ConfigHash while storing CanonicalBytes as Config.

type EncodedVector

type EncodedVector struct {
	Code                   []byte
	CodeCount              uint32
	QuantizedDotProductInv float32
}

EncodedVector contains the data-code row and side-array values produced by Encode. Code is a packed_bit_vector row using TreeDB LSB-first layout.

type Plan

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

Plan binds a Config to a vector dimension and owns deterministic rotation metadata. Plan is immutable and safe for concurrent use; caller-provided Workspace values are not safe for concurrent sharing.

func NewPlan

func NewPlan(vectorDimensions int, cfg Config) (*Plan, error)

NewPlan validates dimensions, derives code shape, and precomputes the v1 deterministic signed permutation. CodeDimensions is next_power_of_two(dims), matching the padded Walsh-Hadamard rotation length.

func (*Plan) BytesPerCode

func (p *Plan) BytesPerCode() int

BytesPerCode returns ceil(CodeDimensions/8), the packed row width.

func (*Plan) CodeDimensions

func (p *Plan) CodeDimensions() int

CodeDimensions returns the number of one-bit dimensions stored per row.

func (*Plan) Config

func (p *Plan) Config() Config

Config returns the plan's codec config.

func (*Plan) CountCodeBits

func (p *Plan) CountCodeBits(code []byte) uint32

CountCodeBits returns the popcount over logical code dimensions, excluding any zero padding bits in the final byte.

func (*Plan) Encode

func (p *Plan) Encode(dst []byte, vector []float32, ws *Workspace) (EncodedVector, error)

Encode normalizes vector, rotates it, packs sign bits into dst, and returns code_count plus quantized_dot_product_inv side-array values. It allocates only when dst or workspace scratch lacks capacity.

func (*Plan) EncodeQuery

func (p *Plan) EncodeQuery(query []float32, ws *Workspace) (Query, error)

EncodeQuery normalizes and rotates query, then prepares the sign bits and absolute weights consumed by ScoreCosine. Returned slices alias ws.

func (*Plan) ScoreCosine

func (p *Plan) ScoreCosine(query Query, code []byte, codeCount uint32, quantizedDotProductInv float32) (float64, error)

ScoreCosine validates side-array inputs and returns the v1 estimated cosine score for one encoded data vector. This is the reference oracle; accelerated implementations may skip repeated validation only after matching this result.

func (*Plan) ScoreEncoded

func (p *Plan) ScoreEncoded(query Query, encoded EncodedVector) (float64, error)

ScoreEncoded is a convenience wrapper around ScoreCosine.

func (*Plan) ValidateCode

func (p *Plan) ValidateCode(code []byte, codeCount uint32) error

ValidateCode verifies row width, zero high padding bits, and code_count.

func (*Plan) ValidateQuantizedDotProductInv

func (p *Plan) ValidateQuantizedDotProductInv(value float32) error

ValidateQuantizedDotProductInv verifies the finite side-array range implied by unit-L2 vectors and an orthonormal rotation: L1(rotated) is in [1, sqrt(CodeDimensions)], so its inverse is in [1/sqrt(CodeDimensions), 1] modulo float32 rounding tolerance.

func (*Plan) ValidateQuery

func (p *Plan) ValidateQuery(q Query) error

ValidateQuery verifies query shape, sign-bit padding, finite non-negative weights, and finite positive WeightSum.

func (*Plan) VectorDimensions

func (p *Plan) VectorDimensions() int

VectorDimensions returns the source float32 vector dimensions.

type Query

type Query struct {
	SignBits       []byte
	AbsWeights     []float32
	WeightSum      float32
	CodeDimensions int
}

Query contains weighted-popcount inputs for ScoreCosine. SignBits uses the same LSB-first bit order as data codes. AbsWeights[i] is abs(rotated_query[i]).

func (Query) ValidFor

func (q Query) ValidFor(p *Plan) bool

ValidFor reports whether q can be used with p without allocation or shape adaptation.

type Workspace

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

Workspace holds reusable scratch for Encode, EncodeQuery, and query byte-table preparation. Query values returned by EncodeQuery alias query-specific workspace buffers and remain valid until the next EncodeQuery call using the same Workspace.

Jump to

Keyboard shortcuts

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