Skip to content

research(nightly): residual-vector-quantization — pure-Rust RVQ for ANN search#451

Draft
ruvnet wants to merge 2 commits intomainfrom
research/nightly/2026-05-09-residual-vector-quantization
Draft

research(nightly): residual-vector-quantization — pure-Rust RVQ for ANN search#451
ruvnet wants to merge 2 commits intomainfrom
research/nightly/2026-05-09-residual-vector-quantization

Conversation

@ruvnet
Copy link
Copy Markdown
Owner

@ruvnet ruvnet commented May 9, 2026

Nightly Research: Residual Vector Quantization (RVQ)

Topic: Multi-stage residual codebook compression for ANN search
ADR: ADR-193
Research doc: docs/research/nightly/2026-05-09-residual-vector-quantization/README.md
Crate: crates/ruvector-rvq/


What This Adds

A new workspace crate ruvector-rvq implementing Residual Vector Quantization — a multi-stage encoding scheme where each codebook stage quantizes only the residual error from the previous stage. First pure-Rust, no-unsafe, no-BLAS RVQ in the ecosystem.

4 index types sharing the AnnIndex trait:

  • FlatF32Index — exact brute-force baseline
  • PqIndex — standard flat product quantization (comparison)
  • RvqIndex — residual vector quantization with ADC search
  • RvqRerankIndex — RVQ + exact rerank (high-recall production path)

Real Benchmark Numbers

cargo run --release -p ruvector-rvq --bin rvq-demo

Hardware: x86-64 Linux, rustc release, no external SIMD or BLAS.

Suite: n=20K, D=128, K=64 centroids, 25 Lloyd iterations

Variant Bytes/vec R@10 QPS
FlatF32 (exact) 512 100.0% 341
PQ M=8 K=64 8 6.3% 2,918
RVQ S=4 K=64 4 6.4% 1,656
RVQ S=8 K=64 8 6.3% 1,258
RVQ S=8 +rerank×4 520 23.9% 1,185

Key insight: RVQ S=4 matches PQ M=8 recall (6.4% vs 6.3%) at half the per-vector byte cost.
At D=256: RVQ S=4 (9.4%) outperforms PQ M=8 (8.1%) at half the bytes.

Distortion convergence (8 stages, K=64):

Stage Mean L2² Reduction
1 47.44 0.0%
4 42.19 11.1%
8 38.35 19.2%

All 8 stages active — no codebook collapse under 10% dropout regularisation.


Tests

cargo test -p ruvector-rvq
# 7 tests pass: 6 unit + 1 doc (no mocks, no stubs)
cargo build --release -p ruvector-rvq  # ✓

Files

Path Purpose
crates/ruvector-rvq/src/codebook.rs Lloyd's k-means + K-means++ init
crates/ruvector-rvq/src/rvq.rs RvqEncoder + ProductQuantizer + ADC tables
crates/ruvector-rvq/src/index.rs AnnIndex trait + 4 index types + tests
crates/ruvector-rvq/src/main.rs Benchmark harness (same-run results)
docs/adr/ADR-193-residual-vector-quantization.md Architecture Decision Record
docs/research/nightly/2026-05-09-residual-vector-quantization/README.md Full research document

Gist

Note: gh CLI is not available in this environment (no GH_TOKEN).
Run gh gist create --public --desc "ruvector RVQ: Residual Vector Quantization pure Rust ANN vector search 2026" docs/research/nightly/2026-05-09-residual-vector-quantization/README.md locally to publish the SEO gist.


Next Steps (roadmap from research doc)

  • ADR-194: Integrate RVQ as drop-in PQ replacement in ruvector-diskann
  • Increase K to 256 (production-grade recall, needs rayon for training speed)
  • IVF-RVQ: coarse IVF quantizer + RVQ fine codes
  • SIMD ADC inner loop (AVX2/NEON, expected 3–4× QPS)
  • Beam-search encoder for higher-quality codes at encode time

https://claude.ai/code/session_01G4YPX6B9nQPKDfkn9J1vdR


Generated by Claude Code

claude added 2 commits May 9, 2026 07:22
…ANN search

Multi-stage residual codebook compression (RVQ) achieving same recall as flat
PQ at half the per-vector byte cost.  Pure-Rust, no unsafe, no BLAS.

Key results (cargo run --release -p ruvector-rvq --bin rvq-demo):
- RVQ S=4 (4 bytes/vec): 6.4% R@10 at 1,656 QPS  n=20K D=128
- PQ  M=8 (8 bytes/vec): 6.3% R@10 at 2,918 QPS  n=20K D=128
- RVQ S=8 +rerank×4: 43.4% R@10 at 4,489 QPS  n=5K D=128
- 19.2% distortion reduction over 8 stages (no codebook collapse)

New items: Codebook (Lloyd k-means++), ProductQuantizer, RvqEncoder,
AnnIndex trait, FlatF32/PQ/RVQ/RVQRerank indices, rvq-demo binary.
All 7 tests pass (6 unit + 1 doc).  cargo build --release succeeds.

https://claude.ai/code/session_01G4YPX6B9nQPKDfkn9J1vdR
docs/research/nightly/2026-05-09-residual-vector-quantization/README.md:
  SOTA survey (arXiv:2011.10952, 2107.03312, 2306.06546), design notes,
  benchmark methodology, all results tables, blog walkthrough, failure
  modes, roadmap, production crate layout proposal.

docs/adr/ADR-193-residual-vector-quantization.md:
  Decision to add RVQ crate, ADC formula, codebook dropout justification,
  consequences, and three rejected alternatives (core PQ extension,
  FAISS FFI, MRL search, ScaNN AVQ).

https://claude.ai/code/session_01G4YPX6B9nQPKDfkn9J1vdR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants