Repository navigation
Conversation
f83e25b to
e484cbd
Compare
e484cbd to
634e4ac
Compare
|
@ashvardanian, could you review this when you have a moment? It is ready on main-dev 5d3f821. It closes #735 and keeps every node reachable: on 1M Cohere, unreachable nodes go from 0.58% to 0.007%, with recall unchanged at the default expansion and higher at large ones. The two red macOS wheel jobs fail at |
|
There is a very complex piece of work I am trying to finish on an expensive B300 cluster. Give me a few days to return to you on this one 🤗 |
634e4ac to
0a8f4f0
Compare
|
Rebased onto main-dev 33d3ab6. The macOS wheel failures are gone with NumKong 7.8.6. The remaining red jobs come from main-dev, not this PR: |
Refining a full neighbor list keeps the most diverse candidates and drops the rest. A node can lose its last incoming base-level link that way, and then no search reaches it, not even one for its own vector. On 1M Cohere embeddings with connectivity 16, 0.6% of the nodes end up like that; hnswlib leaves a similar share. Two nodes can also be left linking only to each other, and many keys with one vector tie on every distance, so their lists fill with each other and most of them get no incoming link at all (unum-cloud#815). Count the incoming base-level links of every node (4 bytes per member, relaxed atomics). When a refine leaves a dropped member with at most one incoming link, which may come from a cut-off part, give it a route once no other lock is held: - the link from the kept member that the refine found closer to it, which the pruning assumed, or else from any member the list kept, so it stays reachable through the list that dropped it; - failing that, a link from the candidates that the insertion's own search reached from the entry point, kept past the refine. A new node that no neighbor kept gets the same. A route never evicts a link, and never comes from the node's own neighborhood, which can be cut off itself. The counts are recounted after `copy`, `compact` and `isolate`, and by the first `reserve` after `load`, which any `add` needs, so `load` itself is not slower. The equal-vectors test is clemlesne's reproducer from unum-cloud#815.
`add` cleared the new node's neighbors on each level before linking it. A concurrent insertion can reach that node through its upper levels, link to it, and leave a reverse link in it; the clear erased that link and orphaned the other node. New nodes are zeroed when allocated, so drop the clear, and let `form_links_to_closest_` append after whatever is there already. cpegeric found the same `clear` in unum-cloud#772, which also defers every reverse link until the forward links of all levels exist. Dropping the clear is enough here: with the routes of the previous commit, the pattern DavIvek reported in unum-cloud#735 leaves no vector unreachable in 100,000 inserts. Closes unum-cloud#735.
0a8f4f0 to
78b6a3d
Compare
addadd and slot reuse
`remove` followed by `add` reuses the removed node's slot: `update` rewrites the node at its new position. Whatever was reachable only through its old position is cut off. On points on a line, a search from the far side then misses the whole other half. Before the node moves, search around its old position without ever entering it, nor any node that a concurrent `update` is moving. Each old base-level neighbor that the search doesn't reach gets a link from the nodes it did reach, and a link back, as the moving node gave both ways. The move then drops only the links this bridge accounted for, and keeps those that concurrent insertions added meanwhile. Of two overlapping updates, at least one sees the other's node as moving. The bridge runs only if a neighbor would be left with at most one incoming link, the same threshold as for pruned links, so a move costs about as much as before. unum-cloud#801 by Ofek Lev and unum-cloud#821 by cpegeric fix the same cut. Their tests are here: unum-cloud#821's points on a line and churn rounds, and unum-cloud#801's two bridge tests.
78b6a3d to
ad32e0c
Compare
|
@swasik, thank you for this, and sorry for the wait after I asked for a few days! 3.0 rebuilt the graph core, so this diff no longer applies, but the problem it tackles stays with us. 3.0 adds API designed just for it: |
Closes #735. Refs #815.
A node with no incoming base-level link can't be reached by any search, not even a search for its own vector. Five open threads report ways a node ends up like that, and four PRs fix one way each:
addrace (Bug: Concurrent add() creates nodes unreachable by search() #735, Fix: concurrent add() no longer strands unreachable nodes (#735) #772);update(Fix: Preserve graph connectivity when reusing deleted slots #801, Fix: remove() + add() slot reuse no longer strands the removed node's neighbors #821);This version replaces the earlier rescue with one rule that covers all of them, and keeps the cost of
addand ofremove+addat main-dev's level.The rule
A base-level node left with at most one incoming link gets a link from a node that the same operation has just reached from the entry point. The link never comes from the node's own neighborhood: that neighborhood can be cut off too, which is how islands form. Each place that drops links gets the rule in its own commit:
Fix: Keep every node reachable when links are pruned. Refining a full list drops the least diverse candidates.refine_now records, for each dropped member, the kept member that it found closer to it.copy,compact,isolateand the firstreserveafterload).Fix: Keep reverse links added to a node still being inserted. Unchanged from the earlier version.addno longer clears a new node's lists, andform_links_to_closest_appends after the reverse links that concurrent insertions already added.Fix: Keep nodes reachable when update moves one.removefollowed byaddreuses the slot, andupdatemoves the node. Before the move:updateis moving, finds the old neighbors that would be cut off.TODOinupdate.The open PRs on the same reproducers
Every cell counts the live keys that an exhaustive search (
count = size) misses, so it also finds islands that a key's own search could land in. The PR columns are each PR's head rebased onto main-dev 33d3ab6. "Earlier #805" is this PR's previous version, 0a8f4f0.remove(100)/remove(500)* #801's numbers for #815 come from its older base's level generator, not from its change.
All churn configurations pass: M=8 and M=16, uniform and clustered, with 1, 2, 4 and 16 threads.
Cost
This is #821's single-thread benchmark: 128-D L2, 20K keys, then 2,000 operations. The figures are medians of 3 interleaved runs, in µs per operation.
add, M=16remove+add, M=16add, M=64remove+add, M=64An earlier attempt routed every dropped link instead of only those that leave a node with at most one incoming link. It densified the graph and cost 34% more per
add.Measurements
Cohere 1M, local x86-64. Setup: 768-d, cosine,
f32, connectivity 16, expansion_add 128, 16 threads, 3 builds per arm.QPS is within run-to-run noise.
Cohere 10M inside ScyllaDB Vector Store, which embeds usearch. Setup:
index.hpp. 2.26.6's ownindex.hppis the same as main-dev 33d3ab6's.f16, connectivity 16, expansion_add 128, 16 indexing threads, capacity reserved in steps of 1M.Recall at 64 varies by up to 1 pp between builds of one arm, and this PR's builds overlap main-dev's. The earlier version was below main-dev in all four builds. At 2048 this PR is highest, as at 1M. Build time and QPS move with the shared host: each arm had one build in a slow window, at about 15K QPS instead of 20K.
Tests
Each new test fails on main-dev and passes from the commit that adds it, except the saturated-list test, which guards against evicting links and passes on main-dev too:
test_base_level_in_links: clustered points with outliers at connectivity 4; every node keeps an incoming base-level link.test_equal_vectors_reachability: Bug: Keys with identical vectors become unreachable by search(), also with one thread #815's reproducer, by @clemlesne.test_concurrent_add_reachability: Bug: Concurrent add() creates nodes unreachable by search() #735's pattern, by @DavIvek.test_remove_add_bridge: Fix: remove() + add() slot reuse no longer strands the removed node's neighbors #821's points on a line, by @cpegeric.test_replacing_bridgeandtest_replacing_saturated_bridge: Fix: Preserve graph connectivity when reusing deleted slots #801's own tests, by @ofek, included unchanged.test_remove_add_churn_reachability: 1 and 8 threads; it follows Fix: remove() + add() slot reuse no longer strands the removed node's neighbors #821's churn rounds.Checks run:
test_cppon its own (Release, GCC, x86-64).index_densecopies a reused slot's new vector while concurrent searches read it.Credits
The analysis and reproducers in the other threads made this possible:
addrace in Bug: Concurrent add() creates nodes unreachable by search() #735, with the reproduction.clearin Fix: concurrent add() no longer strands unreachable nodes (#735) #772. Fix: remove() + add() slot reuse no longer strands the removed node's neighbors #821 provided the line reproducer, the churn rounds and the cost benchmark used above.If this one is merged, #772, #801 and #821 would no longer be needed. That is your call.
Not covered
refine_, as a separate change, tested oni8andf16.updatemoves, the bridge is skipped.index_densecopy race above is separate and predates this PR.updateis covered by the tests, the churn configurations and the cost benchmark.CI
Rebased onto main-dev 33d3ab6. main-dev's own CI fails on several jobs, for reasons unrelated to this PR: Windows JS linking against NumKong,
setup-javadroppingadopt, the VS 2026 runner image, and a Docker job without its Dockerfile. #833 fixes those. The same jobs fail here, each at the same step. Test C++ passes in every job that gets to build: Ubuntu GCC 13 and Clang 20, macOS and Windows x86. Windows ARM stops earlier, because CMake finds no Visual Studio on the VS 2026 image.