[BUG] CAGRA variable graph degree can leave nodes without incoming edges

Aperta
#3,119 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
12/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
cpp
Ambito
hpc, search

Direzione di ricerca

The bug is in the variable-degree branch of kern_merge_graph in graph_core.cuh, reached from optimize(), where reverse edges are thinned and make_reverse_graph_gpu counts incoming edges. Start by reproducing the 4-vector graph with the seed in the report on the lucene-10.2 module, then check whether every node has an incoming edge. Done means no unreachable nodes at small pruned degrees, with recall and graph-size checks, but the fix approach is still an open design choice in the thread.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

bug

Describe the bug

With variable_graph_degree_fraction < 1 (#2031), optimize() can produce a graph where a node has no incoming edges. No search starting from another node can reach such a node, so it is never returned. from_hnsw_params with the SAME_GRAPH_FOOTPRINT heuristic sets the fraction to 0.35, and cuvs-lucene builds every index that way by default. #3046's connected-components check doesn't detect it, because it checks weak connectivity and the orphaned node still has outgoing edges.

cuvs-lucene caught it: TestLucene99AcceleratedHNSWVectorsFormat.testRandomWithUpdatesAndGraph failed about 0.07% of the time (2 of 3000 runs) with expected:<1> but was:<0>. A segment of 4 vectors with a single live document got this graph:

entry=0   0->[2,3]  1->[2,3]  2->[0,3]  3->[0,2]

Node 1, the only live document, is unreachable, so a k=1 search returns no hits. Reproduces deterministically with -Dtests.seed=B2C47EA2D979DB2F:D6E500B02D2EAC8 on the lucene-10.2 module.

What's the impact and how often does this happen

To figure this out I asked Claude to build graphs with NN-descent on random vectors (10–28 dimensions) for me and count nodes not reachable from node 0, at fraction 0.35 and at 1.0. Intermediate degree was 1.5× the graph degree, as with SAME_GRAPH_FOOTPRINT. "Pruned degree" is the minimum degree the variable path aims for, ceil(D · 0.35), where D is the graph degree after check_graph_degree lowers it to at most n_rows - 1.

cuvs-lucene's defaults (M=32, so the requested graph degree is 64):

rows D pruned degree graphs with an unreachable node, fraction 0.35 fraction 1.0
3 2 1 71 / 200 0 / 200
4 3 2 20 / 200 0 / 200
5 4 2 46 / 200 0 / 200
6 5 2 17 / 200 0 / 200
10 9 4 1 / 200 0 / 200
16 15 6 0 / 200 0 / 200
34–64 33–63 12–23 0 / 2500 not run
128, 1000, 10000 64 23 0 / 220 0 / 220

Explicit small graph degrees, at any size:

D pruned degree rows fraction 0.35: graphs affected (unreachable nodes) fraction 1.0
4 2 100 128 / 200 (216) 1 / 200 (2)
4 2 1000 10 / 10 (116) 0 / 10
4 2 10000 10 / 10 (1540) 6 / 10 (9)
8 3 100 11 / 200 (11) 0 / 200
8 3 1000 1 / 10 (1) 0 / 10
8 3 10000 7 / 10 (17) 0 / 10

So the risk follows the pruned degree, not the dataset size: graphs with a pruned degree of 4 or less lose nodes, from 6 up none did. Tiny datasets are affected only because their degree is lowered to n_rows - 1. With D = 4, about 1.5% of a 10000-node graph is unreachable at fraction 0.35, about 100 times more than with a constant degree of 4, which isn't fully reachable either.

Small datasets

When the graph degree after clamping is n_rows - 1, a constant degree gives the complete graph, so the PR makes optimize() keep the constant degree when output_graph_degree >= graph_size - 1. That covers the tiny datasets in the first table at no cost in graph size, since such graphs have at most graph_degree + 1 nodes. It doesn't help small graph degrees on larger datasets (the second table).

I have a PR with a small fix that makes CAGRA's optimize() to keep a constant degree when the graph degree is n_rows - 1, which solves at least the lucene test falkiness. I can open it if it sounds like a valid approach.

Possible follow-up: a floor under the pruned degree

Since the risk follows the pruned degree, never pruning below a minimum would cover both tables. It is a small change in optimize() and leaves the code that drops the edges (see "Why it happens") as it is:

const uint64_t target_pruned_degree = std::max<uint64_t>(
  std::ceil(output_graph_degree * variable_graph_degree_fraction),
  std::min<uint64_t>(output_graph_degree, kMinPrunedDegree));

Any graph with D <= kMinPrunedDegree then keeps a constant degree, which includes every small complete graph, so the complete-graph check in the PR would no longer be needed. The data above puts kMinPrunedDegree around 8 (3 still failed on large graphs, 6 didn't), but 4–10 should be measured on large graphs before picking it. It is a behaviour change: users who choose a small M and a variable degree would get larger graphs, so it needs a footprint and recall check.

Why it happens (according to Claude)

Two things in kern_merge_graph's variable-degree branch (graph_core.cuh) combine:

  1. Reverse edges are thinned at random: an edge is skipped with probability (D + ed0) / 2D, where ed0 is the natural degree of the node that would hold it. That is at least 1/2, and exactly 1 when ed0 == D. The "don't prune highways" guard only applies when both in-degrees are at least 2D, which is rare on small graphs and small degrees. A node that no pruned row keeps depends entirely on reverse edges for an incoming edge, and with a small pruned degree many nodes are in that position.
  2. make_reverse_graph_gpu builds rev_graph / rev_graph_count from every column of the pruned graph, but the variable-degree path later truncates each row to its natural degree. So rev_graph_count(u) includes edges that are discarded afterwards, and a node can look like it has incoming edges when it won't. (The comment on kern_make_rev_graph_k mentions a natural-degree mask that the kernel doesn't apply.)

Because of (2), simply not thinning reverse edges into nodes with rev_graph_count == 0 does not seem to help: tried it, and the results were unchanged. A fix that needs no threshold could:

  • count incoming edges over the natural-degree prefix only, and never thin the last reverse edge into a node; or
  • add a pass after the merge that finds nodes with no incoming edges and links each one from its nearest neighbour, like Lucene's HnswGraphBuilder.connectComponents.

Draft #3067 already has such a pass: bridge_components() calls repair_in_degree(), which gives every node with no incoming edges one from a node it links to. But build_cagra_host_graph_from_knn_params (the NN-descent and IVF-PQ path) only calls bridge_components() when optimize() reports more than one weakly connected component. A node with no incoming edges still has outgoing ones, so a graph like the one above is a single weak component and is never repaired. Running the in-degree repair on that path regardless of the component count would cover this bug, including small datasets.

Independently, log_connected_components could also count nodes with zero in-degree in the final graph and warn when there are any, so this shows up in logs the way #3046 does for disconnected components.

guarantee_connectivity with a variable degree

guarantee_connectivity = true is not a workaround, and the combination has two problems of its own:

  • It throws. In the variable-degree path, effective_degree starts at the node's natural degree (about ceil(D · fraction)) instead of D, so a node with more MST edges than that fails the num_protected_edges > effective_degree check. The build fails with "Failed to merge the MST, pruned, and reverse edge graphs. Some nodes have too many MST optimization edges." A node that is the nearest neighbour of many others is enough. I hit it on a 4-row dataset. Starting effective_degree at max(natural degree, mst_graph_num_edges) would avoid it.
  • It can overflow shared memory. The constant-degree path returns early when num_protected_edges == output_graph_degree, but the variable-degree path skips that return (it still has to write the padding). If a node's MST edges fill its whole row and its natural degree is D, a reverse edge that is not in the row gets pos == D, and num_shift = output_graph_degree - num_protected_edges - 1 wraps to 0xFFFFFFFF in uint32_t. warp_shift_array_one_right then walks far past the warp's shared-memory buffer. Skipping the reverse-edge loop in that case, while still writing the padding and degree, would avoid it. I found this by reading the code and haven't reproduced it.

Iterative-search build skips the degree clamping (separate, pre-existing)

build_from_device_matrix (cagra_build.cuh) lowers local copies of intermediate_graph_degree and graph_degree with check_graph_degree, but then calls iterative_build_graph(res, params, dataset), which reads the unclamped values from params again. So with iterative_search_params a dataset with fewer rows than the graph degree reaches the final optimize() with an output graph wider than n_rows - 1, where pruning can't fill each row with distinct neighbours. I found this by reading the code and haven't run it. Passing the clamped degrees into iterative_build_graph would fix it. The small-dataset fix above assumes the degree was clamped, so this path doesn't get it.

Related: #2031 (variable degree), #2955 (disconnected IVF-PQ graphs), #3046 (connected-components check), #3067 (draft: bridges components and repairs in-degree, see above), #3082 (recall regression in a cuvs-lucene test from the same change).

cc @achirkin

Lingua principale
Cuda
Stelle
902
Fork
267
Merge medio
2g 21h
PR unite (30g)
57

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di NVIDIA/cuvs

Tutte le issue di NVIDIA/cuvs

Issue simili

Altre issue su HPC

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.