Guides
RaBitQ low-bit search
Training-free 1, 2 and 4-bit codes with exact rerank, and when to use them.
RaBitQ mode keeps every vector at full precision on disk and adds a small
code per vector: 1, 2 or 4 bits per dimension after a random rotation, plus
one correction factor. A search scans the codes to find k × rerank
candidates, then rescores those candidates exactly against the full
vectors. The method follows RaBitQ (Gao and Long, SIGMOD 2024) and its
multi-bit extension; see ADR 0008.
- No training. Unlike
sq8andpq, nothing is learned from your data; the first vector can be added straight away. - Exact scores. With rerank on (the default) the scores you get back are true cosine similarities.
- Codes are derived data. If the code files are missing, damaged or were made with other settings, SrvDB rebuilds them from the vectors on open.
Using it
Section titled “Using it”import srvdb
db = srvdb.create("./db", dimension=384, index="rabitq", bits=4)db.add(["a", "b"], [[0.1] * 384, [0.2] * 384], ['{"t": 1}', '{"t": 2}'])db.save()print(db.search([0.1] * 384, k=1))
db.set_rabitq_rerank(20) # more candidates: higher recall, slowerdb.set_rabitq_rerank(0) # no rerank: estimated scores, fastestOptions: bits (1, 2 or 4), rerank and seed. srvdb.open("./db") reopens
the database with the recorded bit width and rerank factor.
From Rust:
use srvdb::{RabitqConfig, SrvDB};
let config = RabitqConfig::new(2)?.with_rerank(30);let db = SrvDB::new_rabitq("./db", 384, config)?;Settings
Section titled “Settings”| Setting | Values | Default |
|---|---|---|
bits |
1, 2 or 4 | 4 in Python |
rerank |
candidates per result; 0 turns rerank off | 40 (1 bit), 20 (2 bits), 10 (4 bits) |
seed |
rotation seed | fixed; changing it rebuilds the codes |
Fewer bits make the codes smaller and the scan faster but the estimates
rougher, so more candidates must be reranked for the same recall. Other bit
widths raise ValueError (Rust: Error::InvalidInput).
How the scan works
Section titled “How the scan works”When a database opens, SrvDB copies the codes into memory in blocks of 32
vectors. A query becomes small 8-bit lookup tables, and the CPU’s
shuffle instructions look up 32 codes at a time (AVX2 on x86_64, NEON
on ARM, chosen at runtime, with a portable fallback on other CPUs). The
8-bit tables add a small, bounded error to the candidate estimates. Rerank
removes that error from the returned scores. With rerank=0 you get the
estimates directly.
Memory: resident memory can hold the codes twice. One copy is the scan
copy (n × dimension × bits / 8 bytes, always resident). The other is
the pages of the code file that the operating system keeps cached after
reading it. If there is not enough memory for the scan copy, opening or
adding raises OSError (Rust: Error::Io, kind OutOfMemory). The
process does not abort. See
ADR 0012.
When to use it
Section titled “When to use it”- Flat search is too slow, and you want near-exact results without building a graph. The scan reads 8 to 32 times fewer bytes than flat.
- You cannot train a quantizer: data arrives a few vectors at a time, or changes a lot.
- Not to save disk space. Full vectors are kept for reranking, so the
directory is slightly larger than a flat one. Use
sq8orpqwhen disk is the limit and approximate scores are acceptable.
Always measure recall against flat on a sample of your own data (see
Choosing an index mode); tightly clustered
embeddings need a larger rerank. The benchmark harness runs the
rabitq1, rabitq2 and rabitq4 modes; see the
benchmarks page for how to reproduce numbers.
Was this page helpful? Tell us in Discussions.