Skip to content

fix: make HNSW graph build deterministic to stabilize test_ann_prefilter#6818

Merged
wjones127 merged 1 commit into
lance-format:mainfrom
wombatu-kun:fix/hnsw-deterministic-build
May 19, 2026
Merged

fix: make HNSW graph build deterministic to stabilize test_ann_prefilter#6818
wjones127 merged 1 commit into
lance-format:mainfrom
wombatu-kun:fix/hnsw-deterministic-build

Conversation

@wombatu-kun
Copy link
Copy Markdown
Contributor

Problem

test_ann_prefilter is flaky and failed on CI (linux-arm, Rust) — e.g. on the unrelated PR #6757 — with the HNSW+SQ parametrization returning a near-miss neighbor (row 10 instead of 6).

Root cause

HNSW node-level assignment uses an unseeded thread RNG (rand::rng()) in both the offline (HnswBuilder) and online (OnlineHnswBuilder) builders, so every index build produces a different random graph. On a tiny 300-vector dataset, an approximate HNSW+SQ search over a different graph each run can return a near neighbor instead of the exact one. main was green by luck of the RNG, not correctness.

This is not caused by #6757 (the StringUuid index-id refactor): index cache keys and on-disk index paths are byte-identical before/after that change; the test only surfaced the pre-existing flakiness.

Fix

  • Seed both level-assignment sites with a shared fixed constant (HNSW_LEVEL_RNG_SEED) via SmallRng, making graph construction reproducible. Recall is statistically unaffected (identical level distribution; only the draws are fixed). A constant — rather than a new HnswBuildParams field — keeps the change contained (no serde/proto/binding changes).
  • Harden test_ann_prefilter to assert the property it actually validates (prefilter honored: filterable > 5) instead of an exact nearest-neighbor id, per the repo guideline that vector-index tests assert recall, not exact matches.

Copy link
Copy Markdown

@claude claude Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@codecov
Copy link
Copy Markdown

codecov Bot commented May 18, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@wombatu-kun wombatu-kun force-pushed the fix/hnsw-deterministic-build branch from 782ad68 to f766bc2 Compare May 19, 2026 02:53
Copy link
Copy Markdown
Contributor

@wjones127 wjones127 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems good to me!

@wjones127 wjones127 merged commit 64d7b78 into lance-format:main May 19, 2026
28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants