Skip to content

feat: SQ-HNSW scalar-quantized HNSW with online calibration and rerank (nightly 2026-07-07)#649

Draft
ruvnet wants to merge 2 commits into
mainfrom
research/nightly/2026-07-07-sq-hnsw-calibrated-search
Draft

feat: SQ-HNSW scalar-quantized HNSW with online calibration and rerank (nightly 2026-07-07)#649
ruvnet wants to merge 2 commits into
mainfrom
research/nightly/2026-07-07-sq-hnsw-calibrated-search

Conversation

@ruvnet

@ruvnet ruvnet commented Jul 7, 2026

Copy link
Copy Markdown
Owner

Summary

  • Adds new crate crates/ruvector-sq-hnsw implementing three HNSW index variants all behind a common AnnIndex trait: F32 baseline, SQ8 (int8 graph traversal), and SQ8+Rerank (int8 traversal + f32 exact rerank of top candidates).
  • Implements a calibrated ScalarQuantizer that maps each dimension linearly to [-127, 127] using per-dimension min/max statistics collected from the first N vectors.
  • Fixes a critical HNSW neighbor-pruning bug (single-arg vs two-arg dist_fn) that silently degraded recall from 0.77 to 0.03.
  • All benchmarks are real (cargo run --release); no placeholder numbers.

Benchmark results (n=10k, dims=128, k=10, rustc 1.94.1, x86_64 linux)

┌─────────────────┬──────────┬────────────┬────────────┬─────────────┬──────────────┬──────────────┐
│ Variant         │ Recall@10│ Mean(μs)   │ p95(μs)    │ QPS         │ Mem/vec(B)   │ Build(ms)    │
├─────────────────┼──────────┼────────────┼────────────┼─────────────┼──────────────┼──────────────┤
│ F32 (baseline)  │ 0.7704   │      396.7 │      464.0 │        2521 │          512 │         8333 │
│ SQ8 (no-rerank) │ 0.7682   │      256.6 │      302.3 │        3897 │          128 │         5556 │
│ SQ8 + Rerank    │ 0.7690   │      270.6 │      315.9 │        3696 │          640 │         5595 │
└─────────────────┴──────────┴────────────┴────────────┴─────────────┴──────────────┴──────────────┘

SQ8 vs F32: −35% latency, 4× less memory, −0.3% recall. All acceptance tests PASS.

New files

Path Description
crates/ruvector-sq-hnsw/Cargo.toml New crate manifest
crates/ruvector-sq-hnsw/src/lib.rs AnnIndex trait, HnswConfig, SearchResult, exact_knn
crates/ruvector-sq-hnsw/src/quantizer.rs ScalarQuantizer: calibrate, encode, decode, sq8_l2_sq
crates/ruvector-sq-hnsw/src/hnsw.rs HnswGraph: two-arg dist_fn, insert_node, beam search
crates/ruvector-sq-hnsw/src/index.rs F32Index, Sq8Index, Sq8RerankIndex with unit tests
crates/ruvector-sq-hnsw/src/main.rs Benchmark binary with acceptance tests
docs/adr/ADR-272-sq-hnsw-calibrated-search.md Architecture Decision Record
docs/research/nightly/2026-07-07-sq-hnsw-calibrated-search/README.md Full research document
docs/research/nightly/2026-07-07-sq-hnsw-calibrated-search/gist.md Public-facing implementation summary

Test plan

  • cargo test -p ruvector-sq-hnsw — 8 unit tests + 1 doc test, all pass
  • cargo run --release -p ruvector-sq-hnsw — benchmark binary prints RESULT: PASS
  • cargo build --workspace — no new warnings or errors
  • Integration into ruvector-core behind sq8 feature flag (future milestone M1)

🤖 Generated with claude-flow

https://claude.ai/code/session_01QwG2TymHzK9LapMySKWsRU


Generated by Claude Code

claude and others added 2 commits July 7, 2026 07:28
Implements three HNSW index variants with different distance precision:
- F32Index: full float32 baseline
- Sq8Index: int8 scalar-quantized traversal (4× memory, 1.5× faster)
- Sq8RerankIndex: int8 graph walk + float32 exact rerank on candidates

Calibration fits per-dimension min/max statistics over the corpus; the
quantizer is frozen before graph construction and shared across all
three variants.  Key design fix: insert_node takes a two-argument
dist_fn(i,j) so reverse-link pruning uses real distances from the
affected node (not the inserting node), preventing graph degradation.

Benchmark results (n=10k, 128-dim, ef_s=64, M=16, release build):
  F32     recall@10=0.770  mean=396μs  QPS=2521  mem/vec=512B
  SQ8     recall@10=0.768  mean=257μs  QPS=3897  mem/vec=128B
  Rerank  recall@10=0.769  mean=271μs  QPS=3696  mem/vec=640B

All acceptance tests pass.

Co-Authored-By: claude-flow <ruv@ruv.net>
Claude-Session: https://claude.ai/code/session_01QwG2TymHzK9LapMySKWsRU
- docs/research/nightly/2026-07-07-sq-hnsw-calibrated-search/README.md
  Full research document: background, architecture, benchmark analysis,
  practical and exotic applications, ecosystem integration notes.

- docs/adr/ADR-272-sq-hnsw-calibrated-search.md
  Architecture decision record covering: context, decision rationale,
  two-arg dist_fn correctness requirement, benchmark evidence, failure
  modes, security considerations, migration path, open questions.

- docs/research/nightly/2026-07-07-sq-hnsw-calibrated-search/gist.md
  Public-facing gist explaining the implementation, the neighbor-pruning
  bug fix, and real benchmark numbers.

Co-Authored-By: claude-flow <ruv@ruv.net>
Claude-Session: https://claude.ai/code/session_01QwG2TymHzK9LapMySKWsRU
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