RaBitQ Rust Library
This crate provides a pure-Rust implementation of the RaBitQ quantization scheme and an IVF + RaBitQ searcher that mirrors the
behavior of the C++ RaBitQ Library. The library focuses on efficient approximate nearest-neighbor search for high-dimensional vectors and now ships with tooling to reproduce the GIST benchmark pipeline described in example.sh.
Highlights
- Full IVF + RaBitQ searcher – the
IvfRabitqIndexsupports both L2 and inner-product metrics, fastscan-style pruning, and optional extended codes. - Pre-clustered training support –
IvfRabitqIndex::train_with_clusterslets you reuse centroids and cluster assignments generated by external tooling (e.g. thepython/ivf.pyhelper that wraps FAISS), matching the workflow used by the upstream C++ library. - Dataset utilities – the new
rabitq_rs::iomodule parses.fvecsand.ivecsfiles, including convenience helpers for cluster-id lists and ground-truth tables. - Command-line evaluation –
cargo run --bin ivf_rabitqbuilds an IVF + RaBitQ index from any .fvecs dataset and reports recall and throughput for a configurablenprobe/top-kbudget.
Quick start
Add the crate to your project by pointing Cargo.toml at this repository, adding rabitq-rs from crates.io, or by linking to a
local checkout. The snippet below constructs an IVF index from randomly generated vectors, queries it, and prints the nearest
neighbour id.
use ;
use ;
use *;
Training with pre-computed clusters
When you already have k-means centroids and assignments (for example produced by FAISS), call train_with_clusters:
use IvfRabitqIndex;
use ;
let index = train_with_clusters?;
Faster quantization with faster_config
By default, RaBitQ computes an optimal scaling factor for each vector during quantization, which provides the best accuracy but can
be slow. The faster_config mode precomputes a single constant scaling factor for all vectors, trading <1% accuracy for 100-500x
faster quantization.
When to use faster_config:
- Large datasets (>100K vectors) where training time is a bottleneck
- Production scenarios where index build time matters
- When the small accuracy loss (<1%) is acceptable
When NOT to use faster_config:
- Small datasets where training is already fast
- When you need the absolute best accuracy
- Research scenarios where precision is critical
Example usage:
use IvfRabitqIndex;
use ;
// With faster_config enabled
let index = train?;
// Or from CLI:
// cargo run --release --bin ivf_rabitq -- \
// --base data.fvecs \
// --nlist 4096 \
// --bits 7 \
// --faster-config \
// --save index.bin
Reproducing the GIST IVF + RaBitQ benchmark
Follow the same data preparation steps shown in example.sh:
-
Download and unpack the dataset
If FTP is blocked in your environment, fetch the files from an alternative mirror and place them under
data/gist/with the same filenames (gist_base.fvecs,gist_query.fvecs,gist_groundtruth.ivecs).
Typical Workflow (with FAISS clustering)
-
Cluster the base vectors using the Python helper:
-
Build the index:
Add
--faster-configfor 100-500x faster training with <1% accuracy loss. -
Query with benchmark mode (nprobe sweep + 5-round benchmark):
This performs an automatic nprobe sweep (5, 10, 20...15000), stops when recall plateaus, then runs a 5-round benchmark and outputs a table:
nprobe | QPS | recall.
Alternative: Build with Rust k-means
Skip Python clustering and use built-in k-means:
Single-Config Evaluation
For a specific nprobe value (without sweep):
This evaluates at the specified nprobe and reports recall, QPS, and latency percentiles.
Build and Query in One Command
All CLI options are documented in cargo run --bin ivf_rabitq -- --help.
Testing and linting
The test suite now includes regression checks for the dataset readers and the pre-clustered IVF flow. Run the full suite along with the standard linters before submitting changes:
For dataset-backed evaluation, invoke the gist binary as described above.
Publishing to crates.io
The crate is configured for publication on crates.io. Before publishing a new release:
-
Update the version – bump the
versionfield inCargo.tomlfollowing semantic versioning. -
Log in to crates.io – authenticate once per workstation:
-
Validate the package – ensure the crate builds cleanly and packages without missing files:
Inspect the generated
.cratearchive undertarget/package/if you need to double-check the bundle contents. -
Publish – when you are ready, push the package live:
If you need to yank a release, run cargo yank --vers <version> (optionally with --undo). Remember that published versions
are immutable, so double-check the README and API docs before releasing.
Project structure
src/
bin/ivf_rabitq.rs # CLI for building & evaluating IVF + RaBitQ on any .fvecs dataset
io.rs # .fvecs/.ivecs readers and helpers
ivf.rs # IVF + RaBitQ searcher and training routines
kmeans.rs # Lightweight k-means used for in-crate training
math.rs # Vector math helpers
quantizer.rs # Core RaBitQ quantisation logic
rotation.rs # Random orthonormal rotator
Refer to README.origin.md for the original upstream documentation.