Skip to content

Fix: Seed each context's level generator - #804

Merged
ashvardanian merged 2 commits into
unum-cloud:main-devfrom
swasik:fix-level-generator-seed
Oct 18, 2026
Merged

ashvardanian merged 2 commits into
unum-cloud:main-devfrom
swasik:fix-level-generator-seed

Conversation

@swasik

@swasik swasik commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Closes #803.

Every context_t default-constructed its std::default_random_engine level_generator, so all threads drew the same node levels. try_reserve recreates the contexts, so that sequence also started over after every reserve. This seeds each context from std::random_device, the way kmeans_clustering_gt already seeds itself. Nothing else changes.

Test

test_level_generators adds 16,384 2-d vectors through 16 thread contexts in 16 reserve steps. With shared, restarting generators, every per-level node count is a multiple of 16 × 16 = 256, on any standard library. With independent generators, about 1024 / 64 / 4 nodes land on levels 1–3, which practically never all come out as multiples of 256.

level counts (levels 0, 1, 2, …)
main-dev 16384 1536, identical on every run: level 1 50% too full, no level 2
this PR e.g. 16384 1013 57 2, 16384 974 72 4, 16384 1069 68 5 1

build_release/test_cpp (Release, Ubuntu 26.04, x86-64, GCC) fails at the new expect on main-dev and passes with the fix, 21 runs out of 21.

Python clustering test. test_index_clustering relied on the fixed sequence always putting a node on level 2 of a 500-vector index, which cluster requires (max_level() < 2 fails with "Index too small to cluster!"). With random levels at the default connectivity of 16, I measured 23 of 200 such 500-node indexes with no level 2, and 7 of 200 at 1,024 nodes. The test now builds its indexes with connectivity 4: a node reaches level 2 with probability 1/16, so 500 nodes miss it with odds of about 1e-14. It still runs in about 1.6 s. All 1,440 runs of it passed (20 repeats), and the Python suite (test_index, test_distances, test_tooling, test_sparse, test_gil_release) passed three runs out of three.

CI failures not caused by this change. Both appeared identically in #797's run on Oct 8:

  • macOS fails on the Python build: invalid argument '-std=c11' not allowed with 'C++'.
  • Windows (X86) fails in Test Rust with unresolved __imp_nk_capabilities, __imp_nk_find_kernel_punned and other NumKong symbols (a dllimport mismatch with the numkong crate).

Every other job passes, including Ubuntu GCC 14 and Clang 20, Windows ARM, Android, the cross-compiles and the Linux wheels.

Effect on a real index

In ScyllaDB Vector Store (the Rust crate, 16 threads, 1M reserve steps), we rebuilt a 10M-vector index (Cohere embeddings, 768-d, cosine, f16, M=16, expansion 128/64) repeatedly:

Trade-off

Single-threaded builds are no longer bit-for-bit reproducible across runs; multi-threaded builds never were. If you'd rather keep determinism, the generators could instead be seeded from the context index plus a counter that keeps advancing across reserves, or index_config_t could take a seed. I'm happy to switch to either.

@swasik
swasik force-pushed the fix-level-generator-seed branch from bc3b695 to e866eaa Compare October 18, 2026 07:14
Every `context_t` default-constructed its `std::default_random_engine`, so
all threads drew the same node levels, and `try_reserve` recreates the
contexts, so that sequence started over after every reserve. The upper
levels came out several times too full, with the levels above them
missing, and were built in bursts of same-level nodes inserted at once.
On a 10M-vector index, the greedy descent then missed whole query
clusters in some builds. Seed each context from `std::random_device`.

The Python clustering test relied on the fixed sequence always putting a
node on level 2, which `cluster` requires. With random levels, about one
500-node index in seven has no level 2, so the test now builds its
indexes with connectivity 4.

Closes unum-cloud#803
@swasik
swasik force-pushed the fix-level-generator-seed branch from e866eaa to fd0326a Compare October 18, 2026 07:14
@ashvardanian

Copy link
Copy Markdown
Member

This is clearly very useful! Thanks, @swasik 🤗

Use a compact noexcept SplitMix64 generator sharing the integer hash mixer.
Preserve per-context streams across reserves and copies, retain thread
reservations, and sample levels outside the shared lock with a guarded
logarithm endpoint.
@ashvardanian
ashvardanian merged commit 30d75a4 into unum-cloud:main-dev Oct 18, 2026
9 of 11 checks passed
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