Repository navigation
Description
Describe the bug
Node levels aren't random:
index_gt::context_tholds a default-seededstd::default_random_engine level_generator{}. So every thread context draws exactly the same level sequence.try_reserve()replacescontexts_with fresh contexts, so that sequence also starts over at every reserve.
With T threads and R reserve steps, each level draw is repeated T × R times, and a build's level histogram doesn't depend on its data at all.
There are two consequences:
- The upper levels are wrong, and identical in every build. The top level gets several times too many nodes, and the levels above it are missing.
- The top level is built in bursts. The same draw index comes up on all T threads at about the same moment, so T same-level nodes are inserted concurrently. The entry point is always the first node of the first burst, inserted while the index is still tiny.
This matters at scale because search descends the upper levels greedily. In ScyllaDB Vector Store (the Rust crate, 16 threads, 1M-vector reserve steps), we rebuilt the same 10M-vector index (Cohere Wikipedia embeddings, 768 dimensions, cosine, f16, connectivity 16, expansion_add 128, expansion_search 64) many times:
- Stock usearch, 6 builds: five reached 90.1–90.6% recall@10, and one reached 80.5%.
- The bad build: 55 of 1,000 test queries, all from one topic, returned zero relevant results at expansion_search 512. Overall recall stays at 87.3% even at expansion_search 4096.
- For every one of those queries, the greedy descent goes from the entry point to one level-4 node that has 3–4 neighbors per level, all about 0.9 cosine distance from the query. It stops there.
- Starting from the true nearest neighbor instead, the same level-0 graph finds 87% for those queries.
- Every stock build has 40,159–40,160 / 3,679–3,680 / 320 nodes at levels 2–4 and no level 5, where about 39,062 / 2,441 / 152 / 9 are expected.
- All 320 level-4 slots fall at two fixed offsets inside each 1M reserve step: 32 per step, which is 16 threads × 2 draws.
- The bad build's entry point has 7/8/6/6 neighbors on levels 4–1; a good build's has 14/10/15/16.
- With only the generator seeded (
std::default_random_engine level_generator{std::random_device{}()}), 17 builds out of 17 were good: 90.1–91.0%, mean 90.42 against 90.41 for the good stock builds.- The levels came out random: about 625K / 39K / 2.4K / 120–168 / 2–18, with a level 6 now and then.
- Throughput didn't change (19.7K vs 19.6K QPS).
Steps to reproduce
The level part reproduces in 12 seconds with the Python package and random data:
import numpy as np
from usearch.index import Index
N, STEPS, THREADS, M, dim = 1_000_000, 10, 16, 16, 8
rng = np.random.default_rng(1) # any seed: the level counts do not change
index = Index(ndim=dim, metric="l2sq", connectivity=M, dtype="f32")
step = N // STEPS
for s in range(STEPS):
keys = np.arange(s * step, (s + 1) * step, dtype=np.uint64)
index.add(keys, rng.random((step, dim), dtype=np.float32), threads=THREADS)
print([int(level.nodes) for level in index.levels_stats])The output is identical for every data seed:
| level | nodes | expected N / M^level |
|---|---|---|
| 0 | 1000000 | 1000000 |
| 1 | 60528 | 62500 |
| 2 | 4256 | 3906 |
| 3 | 528 | 244 |
| 4 | 128 | 15 |
max_level is 4, where about 5 is expected. In C++, adding 16,384 vectors through 16 thread contexts in 16 reserve steps gives level counts 16384 1536 on every run: level 1 is 50% too full and level 2 is missing, where about 1024 / 64 / 4 are expected.
Expected behavior
Each node's level is drawn independently from the geometric distribution. The level histogram and build quality don't depend on the number of threads or on reserve steps.
USearch version
v2.26.4 (main-dev at 1e8a192). The default-seeded per-context generator is in every release back to v0.1.8.
Operating System
Ubuntu 24.04 (AWS Graviton4 nodes), Ubuntu 26.04 (x86 laptop for the repro)
Hardware architecture
Arm
Which interface are you using?
C++ implementation
Contact Details
No response
Are you open to being tagged as a contributor?
- I am open to being mentioned in the project
.githistory as a contributor
Is there an existing issue for this?
- I have searched the existing issues
Activity
Fix: Seed each context's level generator
Fix: Seed each context's level generator
Fix: Preserve distinct node-level streams across threads and reserves