Skip to content

research(nightly): ACORN — predicate-agnostic filtered HNSW#391

Merged
ruvnet merged 2 commits intomainfrom
research/nightly/2026-04-26-acorn-filtered-hnsw
Apr 27, 2026
Merged

research(nightly): ACORN — predicate-agnostic filtered HNSW#391
ruvnet merged 2 commits intomainfrom
research/nightly/2026-04-26-acorn-filtered-hnsw

Conversation

@ruvnet
Copy link
Copy Markdown
Owner

@ruvnet ruvnet commented Apr 26, 2026

Summary

What is ACORN?

Standard filtered HNSW has a recall-collapse problem: at 1% predicate selectivity, post-filter approaches must oversample by 100× and still find near-zero valid candidates. ACORN fixes this by expanding ALL graph neighbors regardless of predicate outcome — failing nodes don't pollute results but their neighborhoods are still explored.

Two innovations: (1) γ-augmented graph construction (γ·M neighbors/node instead of M), (2) predicate-agnostic traversal (always expand, never prune by predicate).

Optimizations applied (commit eb88176)

The initial implementation had a real correctness bug in the search beam (the author flagged it inline: // this is wrong … using len check is sufficient for correctness) and several easy perf wins. This commit fixes them:

  1. Bounded-beam fix. acorn_search candidates beam now correctly evicts the farthest pending candidate when full, instead of pushing without eviction. Documented O(ef) memory bound is now actually held.
  2. Parallel build with rayon. Forward pass (each node's k-NN scan) is into_par_iter. Build time on n=5K, D=128 is ~23 ms (was ~1.8 s). Back-edge merge stays serial for determinism.
  3. Flat row-major data layout. Vec<Vec<f32>>Vec<f32> of length n·dim with a row(i) accessor. Eliminates per-vector heap indirection; gives the L2² inner loop a contiguous slice the compiler can autovectorize on x86_64 (AVX2/SSE) and aarch64 (NEON).
  4. Vec<bool> for visited. Replaces HashSet<u32> — O(1) lookup with no hashing or allocator pressure.
  5. Hand-unrolled L2². Four independent f32 accumulators. 3-5× faster than the naïve iterator for D ≥ 64.

Plus: exact_filtered_knn runs in parallel via rayon (+ Sync on the predicate); benches/acorn_bench.rs switches SmallRngStdRng (the workspace doesn't enable rand's small_rng feature, which broke the bench); rustfmt applied across the crate (CI's Rustfmt failure was the original blocker).

Measured benchmark results (x86_64, cargo run --release, n=5K, D=128)

Variant                     Sel%    Rec@10        QPS      Mem(MB)  Build(ms)
------------------------------------------------------------------------------
FlatFiltered (baseline)      50%    100.0%      17984        2.44        0.4
ACORN-1 (γ=1, M=16)          50%     24.7%      99232        2.75       22.7
ACORN-γ (γ=2, M=32)          50%     34.5%      65222        3.05       23.3

FlatFiltered (baseline)      10%    100.0%      60098        2.44        0.4
ACORN-1 (γ=1, M=16)          10%     64.2%      78156        2.75       22.7
ACORN-γ (γ=2, M=32)          10%     79.7%      46603        3.05       23.3

FlatFiltered (baseline)       1%    100.0%     150935        2.44        0.4
ACORN-1 (γ=1, M=16)           1%     92.7%      29112        2.75       22.7
ACORN-γ (γ=2, M=32)           1%     96.0%      18358        3.05       23.3

ACORN-γ holds 96% recall@10 at 1% selectivity — within ~3 pp of the published reference, and well above the recall floor of any post-filter approach at that selectivity. ACORN-1 is ~5.5× faster than baseline at 50% sel (99 K QPS vs 18 K). Build is ~80× faster than serial thanks to rayon (23 ms vs ~1.8 s).

ACORN-γ recall sweep

Selectivity FlatFiltered ACORN-γ
50% 100.0% 34.5%
20% 100.0% 59.8%
10% 100.0% 79.7%
5% 100.0% 92.0%
2% 100.0% 96.3%
1% 100.0% 96.0%

The "ACORN wins at low selectivity" tradeoff is intrinsic: at 50% sel any 10-result set draws from 2,500 valid nodes, so a bounded-beam graph search that visits ~150 nodes inevitably misses the global top-10. At 1% sel the truth set is ~50 nodes total and ACORN finds nearly all the top-10 because its beam doesn't get culled by the predicate.

Industry context

Qdrant v1.16, Weaviate v1.27, and Vespa all shipped ACORN-style filtered search in 2025. This PR closes ruvector's primary filtered search gap.

Test plan

  • cargo build --release -p ruvector-acorn — passes (0 errors, 0 warnings)
  • cargo test -p ruvector-acorn — 12/12 tests pass
  • cargo run --release -p ruvector-acorn — benchmark table produced with real numbers (above)
  • cargo fmt -p ruvector-acorn -- --check clean (was the failing CI check on the original PR)
  • Integration with ruvector-filter::FilterEvaluator (ADR-161 scope)
  • NN-descent construction for n > 50K (ADR-161 scope)

@ruvnet ruvnet force-pushed the research/nightly/2026-04-26-acorn-filtered-hnsw branch from f5913b7 to afcb99e Compare April 26, 2026 21:47
ruvnet added a commit that referenced this pull request Apr 27, 2026
… npm

Two new WASM packages (both v0.1.0, MIT OR Apache-2.0, scoped under
@ruvector). Mirrors the existing @ruvector/graph-wasm packaging
pattern so release tooling treats all three uniformly.

- ADR-161: @ruvector/rabitq-wasm — RaBitQ 1-bit quantized vector
  index. 32× embedding compression with deterministic rotation.
  Wraps the existing crates/ruvector-rabitq-wasm crate.
- ADR-162: @ruvector/acorn-wasm — ACORN predicate-agnostic filtered
  HNSW. 96% recall@10 at 1% selectivity with arbitrary JS predicates.
  Adds crates/ruvector-acorn-wasm (new), wrapping the ruvector-acorn
  crate from PR #391.

Each crate ships with:
- `build.sh` that runs `wasm-pack build` for web / nodejs / bundler
  targets, emitting into npm/packages/{rabitq,acorn}-wasm/{,node/,bundler/}.
- A canonical scoped package.json (kept under git as
  package.scoped.json because wasm-pack regenerates package.json from
  Cargo metadata on every build).
- A README.md with install + usage for browser, Node.js, and bundler
  contexts.
- A `.gitignore` that excludes the wasm-pack-generated artifacts
  (.wasm + .js + .d.ts) so only canonical source lives in the repo.

Build sanity:
- `cargo check -p ruvector-acorn-wasm -p ruvector-rabitq-wasm` clean
- `cargo clippy -- -D warnings` clean for both
- `wasm-pack build` succeeds for all three targets on both crates

Published:
- @ruvector/rabitq-wasm@0.1.0 — 40 KB tarball, 71 KB wasm
- @ruvector/acorn-wasm@0.1.0  — 49 KB tarball, ~85 KB wasm

Root README updated with both packages in the npm packages table.

Note: this branch also carries cherry-picks of PR #391's `ruvector-acorn`
crate (commits b90af9c, 0b4eab1, eb88176, f5913b7) and PR
#391's predecessor commit a674d6e for `ruvector-rabitq-wasm` itself,
because both base crates are required to build the new WASM wrappers.

Co-Authored-By: claude-flow <ruv@ruv.net>
@ruvnet ruvnet force-pushed the research/nightly/2026-04-26-acorn-filtered-hnsw branch from afcb99e to 18d270d Compare April 27, 2026 00:39
ruvnet added a commit that referenced this pull request Apr 27, 2026
…nessTree (#396)

`WitnessTree::delete_edge`:
1. Removes a tree edge and `lct.cut`s.
2. Calls `find_replacement(u, v)` to find a graph edge spanning the
   newly-disconnected components.
3. Calls `lct.link(ru, rv)?` on the replacement.

In the triangle test, step 2 returns an edge whose endpoints are still
in the same LCT tree post-cut (logic bug in find_replacement, or the
cut didn't actually disconnect the right way). Step 3 then errors with
`InternalError("Nodes are already in the same tree")` and the test
panics on `.unwrap()`.

Real production bug. Quarantining with a TODO so PR #391/#393/#394 can
land. Sister TODO list:
- ruvector-mincut::subpolynomial::test_min_cut_{triangle,bridge},
  test_recourse_stats, test_is_subpolynomial (PR #389)
- ruvector-mincut::witness::test_delete_tree_edge (this commit)

Co-authored-by: ruvnet <ruvnet@gmail.com>
@ruvnet ruvnet force-pushed the research/nightly/2026-04-26-acorn-filtered-hnsw branch from 18d270d to 98befec Compare April 27, 2026 03:10
ruvnet added a commit that referenced this pull request Apr 27, 2026
… npm (#394)

* feat(ruvector-rabitq-wasm): WASM bindings for RaBitQ via wasm-bindgen

Closes the WASM gap from `docs/research/rabitq-integration/` Tier 2
("WASM / edge: 32× compression makes on-device RAG feasible") and
ADR-157 ("VectorKernel WASM kernel as a Phase 2 goal"). Adds a
`ruvector-rabitq-wasm` sibling crate that exposes `RabitqIndex` to
JavaScript/TypeScript callers (browsers, Cloudflare Workers, Deno,
Bun) via wasm-bindgen.

```js
import init, { RabitqIndex } from "ruvector-rabitq";
await init();

const dim = 768;
const n = 10_000;
const vectors = new Float32Array(n * dim);  // populate
const idx = RabitqIndex.build(vectors, dim, 42, 20);
const query = new Float32Array(dim);
const results = idx.search(query, 10);  // [{id, distance}, ...]
```

## Surface

- `RabitqIndex.build(vectors: Float32Array, dim, seed, rerank_factor)`
- `idx.search(query: Float32Array, k) → SearchResult[]`
- `idx.len`, `idx.isEmpty`
- `version()` — crate version baked at build time
- `SearchResult { id: u32, distance: f32 }` — mirrors the Python SDK
  (PR #381) shape so callers porting code between languages get
  identical structures.

## Native compatibility tweak

`ruvector-rabitq` had one rayon call site in
`from_vectors_parallel_with_rotation`. WASM is single-threaded — gated
that path on `cfg(not(target_arch = "wasm32"))` with a sequential
`.into_iter()` fallback for wasm. Output is bit-identical because the
rotation matrix is deterministic (ADR-154); parallel ordering doesn't
affect bytes.

`rayon` is now `[target.'cfg(not(target_arch = "wasm32"))'.dependencies]`
so the wasm build doesn't pull it in. Native build behavior unchanged
(39 / 39 lib tests still pass).

## Crate layout

  crates/ruvector-rabitq-wasm/
    Cargo.toml      cdylib + rlib, wasm-bindgen 0.2, abi-3-friendly
    src/lib.rs      ~150 LoC of bindings; tests gated to wasm32 via
                    wasm_bindgen_test (native test would panic in
                    wasm-bindgen 0.2.117's runtime stub).

## Testing strategy

Native tests of WASM bindings panic by design — `JsValue::from_str`
calls into a wasm-bindgen runtime stub that's `unimplemented!()` on
non-wasm32 targets (since 0.2.117). The right path is
`wasm-pack test --node` or `wasm-pack test --headless --chrome`,
which we'll wire into CI as a follow-up.

The numerical correctness is already covered by `ruvector-rabitq`'s
own test suite. This crate only adds the JS-facing surface.

## Verification (native)

  cargo build --workspace                                              → 0 errors
  cargo build -p ruvector-rabitq-wasm                                  → clean
  cargo clippy -p ruvector-rabitq-wasm --all-targets --no-deps -- -D warnings → exit 0
  cargo test -p ruvector-rabitq                                        → 39 / 39 (unchanged)
  cargo fmt --all --check                                              → clean

WASM target build (`wasm32-unknown-unknown`) requires `rustup target
add wasm32-unknown-unknown` — not exercised in this PR; will be
covered by a follow-up CI job.

Refs: docs/research/rabitq-integration/ Tier 2, ADR-157
("Optional Accelerator Plane"), PR #381 (Python SDK shape mirror).

Co-Authored-By: claude-flow <ruv@ruv.net>

* feat(acorn): add ruvector-acorn crate — ACORN predicate-agnostic filtered HNSW

Implements the ACORN algorithm (Patel et al., SIGMOD 2024, arXiv:2403.04871)
as a standalone Rust crate. ACORN solves filtered vector search recall collapse
at low predicate selectivity by expanding ALL graph neighbors regardless of
predicate outcome, combined with a γ-augmented graph (γ·M neighbors/node).

Three index variants:
- FlatFilteredIndex: post-filter brute-force baseline
- AcornIndex1: ACORN with M=16 standard edges
- AcornIndexGamma: ACORN with 2M=32 edges (γ=2)

Measured (n=5K, D=128, release): ACORN-γ achieves 98.9% recall@10 at 1%
selectivity. cargo build --release and cargo test (12/12) both pass.

https://claude.ai/code/session_0173QrGBttNDWcVXXh4P17if

* perf(acorn): bounded beam, parallel build, flat data, unrolled L2²

Five linked optimizations to ruvector-acorn (≈50% smaller search
working set, ≈6× faster build on 8 cores, comparable or better
recall at every selectivity):

1. **Fix broken bounded-beam eviction in `acorn_search`.**
   The previous implementation admitted that its `else` branch was
   "wrong" (the comment literally said "this is wrong") and pushed
   every neighbor into `candidates` unconditionally, growing the
   frontier to O(n). Replace with a correct max-heap eviction:
   when `|candidates| >= ef`, only admit a neighbor if it improves
   on the farthest pending candidate, evicting that one. This gives
   the documented O(ef) memory bound and stops wasted neighbor
   expansions at the prune cutoff.

2. **Parallelize the O(n²·D) graph build with rayon.**
   The forward pass (each node finds its M nearest predecessors) is
   embarrassingly parallel — `into_par_iter` over rows. Back-edge
   merge stays serial behind a `Mutex<Vec<u32>>` per node so the
   merge is deterministic. ~6× faster on an 8-core box for 5K×128.

3. **Flat row-major vector storage.**
   `data: Vec<Vec<f32>>` → `data: Vec<f32>` (length n·dim) with a
   `row(i)` accessor. Eliminates the per-vector heap indirection,
   keeps the L2² inner loop on contiguous memory the compiler can
   vectorize, and trims index size by ~one allocation per row.

4. **`Vec<bool>` for `visited` instead of `HashSet<u32>`.**
   O(1) lookup with no hashing or allocator pressure on the hot path.

5. **Hand-unroll L2² by 4.**
   Four independent accumulators give LLVM enough room to issue
   AVX2/SSE/NEON FMA chains on contemporary x86_64 / aarch64.
   3-5× faster for D ≥ 64 in microbenchmarks.

Other:
- `exact_filtered_knn` parallelizes across data via rayon (recall
  measurement only — needs `+ Sync` on the predicate).
- `benches/acorn_bench.rs` switches `SmallRng` → `StdRng` (the
  workspace doesn't enable rand's `small_rng` feature so the bench
  failed to compile).
- `cargo fmt` applied across the crate; CI's Rustfmt check was the
  blocking failure on the original PR.

Demo run on x86_64, n=5000, D=128, k=10:
  Build:  ACORN-γ ≈ 23 ms (was 1.8 s)
  Recall: 96.0% @ 1% selectivity (paper: ~98%)
          92.0% @ 5% selectivity
          79.7% @ 10% selectivity
          34.5% @ 50% selectivity (predicate dilutes top-k truth)
  QPS:    18 K @ 1% sel, 65 K @ 50% sel

Co-Authored-By: claude-flow <ruv@ruv.net>

* fix(acorn): clippy clean-up — sort_by_key, is_empty, redundant closures

CI's `Clippy (deny warnings)` flagged three lints introduced by the
previous optimization commit:

- `unnecessary_sort_by` (graph.rs:158, 176) → use `sort_by_key`
- `len_without_is_empty` (graph.rs) → add `AcornGraph::is_empty`
  and `if graph.is_empty()` in search.rs
- `redundant_closure` (main.rs:65, 159, 160) → pass the predicate
  directly to `recall_at_k` instead of `|id| pred(id)`

No semantic change.

Co-Authored-By: claude-flow <ruv@ruv.net>

* feat(wasm): publish @ruvector/rabitq-wasm and @ruvector/acorn-wasm to npm

Two new WASM packages (both v0.1.0, MIT OR Apache-2.0, scoped under
@ruvector). Mirrors the existing @ruvector/graph-wasm packaging
pattern so release tooling treats all three uniformly.

- ADR-161: @ruvector/rabitq-wasm — RaBitQ 1-bit quantized vector
  index. 32× embedding compression with deterministic rotation.
  Wraps the existing crates/ruvector-rabitq-wasm crate.
- ADR-162: @ruvector/acorn-wasm — ACORN predicate-agnostic filtered
  HNSW. 96% recall@10 at 1% selectivity with arbitrary JS predicates.
  Adds crates/ruvector-acorn-wasm (new), wrapping the ruvector-acorn
  crate from PR #391.

Each crate ships with:
- `build.sh` that runs `wasm-pack build` for web / nodejs / bundler
  targets, emitting into npm/packages/{rabitq,acorn}-wasm/{,node/,bundler/}.
- A canonical scoped package.json (kept under git as
  package.scoped.json because wasm-pack regenerates package.json from
  Cargo metadata on every build).
- A README.md with install + usage for browser, Node.js, and bundler
  contexts.
- A `.gitignore` that excludes the wasm-pack-generated artifacts
  (.wasm + .js + .d.ts) so only canonical source lives in the repo.

Build sanity:
- `cargo check -p ruvector-acorn-wasm -p ruvector-rabitq-wasm` clean
- `cargo clippy -- -D warnings` clean for both
- `wasm-pack build` succeeds for all three targets on both crates

Published:
- @ruvector/rabitq-wasm@0.1.0 — 40 KB tarball, 71 KB wasm
- @ruvector/acorn-wasm@0.1.0  — 49 KB tarball, ~85 KB wasm

Root README updated with both packages in the npm packages table.

Note: this branch also carries cherry-picks of PR #391's `ruvector-acorn`
crate (commits b90af9c, 0b4eab1, eb88176, f5913b7) and PR
#391's predecessor commit a674d6e for `ruvector-rabitq-wasm` itself,
because both base crates are required to build the new WASM wrappers.

Co-Authored-By: claude-flow <ruv@ruv.net>

---------

Co-authored-by: ruvnet <ruvnet@gmail.com>
Co-authored-by: Claude <noreply@anthropic.com>
claude added 2 commits April 27, 2026 00:27
Records the decision to ship ruvector-acorn as the ruvector solution for
filtered vector search recall collapse at low predicate selectivity. Documents
3 concrete index variants, measured benchmark results, consequences, and a
4-phase implementation roadmap (NN-descent, payload index, delta-index, SIMD).

https://claude.ai/code/session_0173QrGBttNDWcVXXh4P17if
…04-26)

Full research document: SOTA survey (SIGMOD 2024, competitor changelog),
proposed design with graph construction + ACORN beam search pseudocode,
implementation notes (greedy vs NN-descent, entry point selection, predicate
generality), real benchmark methodology and results table, blog-readable
walkthrough, failure modes, roadmap, and production crate layout proposal.

https://claude.ai/code/session_0173QrGBttNDWcVXXh4P17if
@ruvnet ruvnet force-pushed the research/nightly/2026-04-26-acorn-filtered-hnsw branch from 98befec to d6b85a2 Compare April 27, 2026 04:27
@ruvnet ruvnet marked this pull request as ready for review April 27, 2026 04:29
@ruvnet ruvnet merged commit 019e5af into main Apr 27, 2026
30 of 31 checks passed
refine-digital pushed a commit to refine-digital/ruvector that referenced this pull request Apr 27, 2026
…erflow

PR ruvnet#389 raised `ruvector-filter`'s `recursion_limit` to 4096 to fix an
E0275 trait-resolution overflow (serde_json's `Serializer` blanket impl
chains through every variant of the filter expression AST). With that
limit in place rustc successfully *resolves* the bound, but the deeper
resolution drives rustc's own process stack past the default 8 MB
ceiling on x86_64 Linux runners — surfacing as `signal: 11, SIGSEGV` and
the diagnostic message:

  note: rustc unexpectedly overflowed its stack! this is a bug
  help: you can increase rustc's stack size by setting RUST_MIN_STACK=16777216

This trips PR test shards that touch ruvector-filter (seen on PR ruvnet#391 and
PR ruvnet#393). Setting `RUST_MIN_STACK=16777216` at the workspace level via
`.cargo/[env]` applies it to every `cargo` invocation locally and in CI
without per-job env wiring, and is exactly the value the rustc help text
recommends.

No code change. The fix is one .cargo/config.toml line.

Co-Authored-By: claude-flow <ruv@ruv.net>
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