Repository navigation
Fix: Seed each context's level generator - #804
Merged
ashvardanian merged 2 commits intoOct 18, 2026
Merged
Conversation
swasik
force-pushed
the
fix-level-generator-seed
branch
from
October 18, 2026 07:14
bc3b695 to
e866eaa
Compare
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
force-pushed
the
fix-level-generator-seed
branch
from
October 18, 2026 07:14
e866eaa to
fd0326a
Compare
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.
This was referenced Oct 18, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #803.
Every
context_tdefault-constructed itsstd::default_random_engine level_generator, so all threads drew the same node levels.try_reserverecreates the contexts, so that sequence also started over after every reserve. This seeds each context fromstd::random_device, the waykmeans_clustering_gtalready seeds itself. Nothing else changes.Test
test_level_generatorsadds 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.16384 1536, identical on every run: level 1 50% too full, no level 216384 1013 57 2,16384 974 72 4,16384 1069 68 5 1build_release/test_cpp(Release, Ubuntu 26.04, x86-64, GCC) fails at the newexpecton main-dev and passes with the fix, 21 runs out of 21.Python clustering test.
test_index_clusteringrelied on the fixed sequence always putting a node on level 2 of a 500-vector index, whichclusterrequires (max_level() < 2fails 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:
invalid argument '-std=c11' not allowed with 'C++'.__imp_nk_capabilities,__imp_nk_find_kernel_punnedand other NumKong symbols (a dllimport mismatch with thenumkongcrate).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:
reserve, so the greedy descent can miss whole query clusters #803).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_tcould take a seed. I'm happy to switch to either.