Skip to content

DRAFT: Cagra connect components - #3067

Draft
mfoerste4 wants to merge 2 commits into
NVIDIA:release/26.12from
mfoerste4:cagra_connect_components
Draft

mfoerste4 wants to merge 2 commits into
NVIDIA:release/26.12from
mfoerste4:cagra_connect_components

Conversation

@mfoerste4

Copy link
Copy Markdown
Contributor

Before this change, a disconnected CAGRA graph was either left alone or, with guarantee_connectivity, patched by an MST step whose links ignore where the vectors are. (see #2955)

Now, whenever the check in optimize() finds more than one weakly connected component, the build calls bridge_components() with the dataset. It adds two-way links between nearest neighbours across components, gives in-edges to nodes that have none, and finishes with an exact fallback that guarantees one weak component.

CC @tfeher

@copy-pr-bot

copy-pr-bot Bot commented Nov 16, 2026

Copy link
Copy Markdown

Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

@rpelke

rpelke commented Nov 17, 2026

Copy link
Copy Markdown

Some initial benchmarks using the datasets from issue #2955.

Setup: NVIDIA H100 80GB HBM3, AMD EPYC 7413 (24 cores)

In all setups, CAGRA graph bridging pass 1 was sufficient (afterwards only 1 WCC remained).

cohere-v4-1024-angular-q8.hdf5

index build (s) L0 SCCs largest SCC L0 WCCs largest WCC recall@600
before (aac8966): IVF_PQ → CAGRA → HNSW (hierarchy=none) 4.2 214 1.5% 214 1.5% 0.1052
this PR (#3067): IVF_PQ → CAGRA → HNSW (hierarchy=none) 4.4 1 100.0% 1 100.0% 0.9692
baseline (#2955): faiss IndexHNSWFlat CPU (M=16, efC=256) 21.8 3 100.0% 1 100.0% 0.9879

cohere-v4-1024-angular-q200.hdf5

index build (s) L0 SCCs largest SCC L0 WCCs largest WCC recall@600
before (aac8966): IVF_PQ → CAGRA → HNSW (hierarchy=none) 4.2 217 1.2% 217 1.2% 0.1384
this PR (#3067): IVF_PQ → CAGRA → HNSW (hierarchy=none) 4.3 1 100.0% 1 100.0% 0.9965
baseline (#2955): faiss IndexHNSWFlat CPU (M=16, efC=256) 21.6 3 100.0% 1 100.0% 0.9995

@mfoerste4
mfoerste4 force-pushed the cagra_connect_components branch from 9b6cc78 to 8698fe7 Compare November 17, 2026 16:42
@mfoerste4
mfoerste4 changed the base branch from main to release/26.12 November 17, 2026 16:43
}

if constexpr (can_bridge) {
if (bridge) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use if (bridge && num_components > 1) here, too?

The host path (IVF-PQ/NN-Descent, build_cagra_host_graph_from_knn_params) only calls bridge_components if bridge && num_components > 1. Here, the iterative CAGRA build calls bridge_components independent of num_components. We could also skip bridge_components in the iterative CAGRA build when WCC = 1.

In search_and_optimize, the return value of graph::optimize is discarded (which is num_components already). We could pass it out of search_and_optimize and use it in the iterative build for the check, to be consistent with the IVF-PQ/NN-Descent path.

Comment on lines +854 to +855
constexpr bool can_bridge = std::is_same_v<IdxT, uint32_t> &&
!cuvs::neighbors::is_device_vpq_f16_dataset_view_v<DatasetViewT>;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
constexpr bool can_bridge = std::is_same_v<IdxT, uint32_t> &&
!cuvs::neighbors::is_device_vpq_f16_dataset_view_v<DatasetViewT>;
constexpr bool is_vpq = cuvs::neighbors::is_device_vpq_f16_dataset_view_v<DatasetViewT>;
constexpr bool can_bridge = std::is_same_v<IdxT, uint32_t> && !is_vpq;

Bridging isn't supported for VPQ yet, so a VPQ build with guarantee_connectivity = false can still end up disconnected. I'd add a warning when the final graph has WCC > 1 (see further down).

dev_dataset.extent(1),
dev_graph.view());
}
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
} else if constexpr (is_vpq) {
if (helpers::bridge_enabled() && !params.guarantee_connectivity && num_components > 1) {
RAFT_LOG_WARN(
"CAGRA graph bridging is not supported for VPQ datasets, so the graph keeps its %lu weakly "
"connected components. Set guarantee_connectivity = true to connect it with the MST.",
num_components);
}
}

In case you decide to remove the MST path, please remove !params.guarantee_connectivity from the condition and the warning comment.

cagra::detail::graph::optimize_device_graph(handle,
merged_graph.view(),
optimized_graph.view(),
params.guarantee_connectivity && !bridge);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
params.guarantee_connectivity && !bridge);
params.guarantee_connectivity && !bridge);
merged_graph = raft::make_device_matrix<uint32_t, int64_t>(handle, 0, 0);

This frees the merged kNN graph earlier, since it is not used afterwards. Otherwise it would stay allocated until the function returns, so it would still use GPU memory during bridge_components, which allocates its own temporary buffers.

@@ -962,6 +966,16 @@ auto iterative_build_graph(raft::resources const& res,
curr_graph_size = next_graph_size;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
}
dev_knn_graph =
raft::make_device_mdarray<IdxT, int64_t>(res, large_mr, raft::make_extents<int64_t>(0, 0));

This frees the kNN graph buffer after the last iteration. Only the search iterations use it. Without this line, it stays allocated during bridge_components.

@rpelke

rpelke commented Nov 19, 2026

Copy link
Copy Markdown

Maybe another thing to mention: I know it's not decided yet how to handle guarantee_connectivity and the MST. But right now we handle it differently in different places. Example:

  • For VPQ, the bridge can't run, so we keep the MST fallback for guarantee_connectivity=True -> graph is connected.
  • For BitwiseHamming or a large dim (shared memory limit), the bridge cannot run, but we have no MST fallback, even if guarantee_connectivity=True -> graph can stay disconnected.

const uint64_t i = threadIdx.x + static_cast<uint64_t>(blockDim.x) * blockIdx.x;
if (i >= n) { return; }
const uint32_t root = labels[i];
const uint32_t rate = max(ratio, raft::ceildiv(sizes[root], cap));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about this line: it caps the number of nodes per component used for bridging at approx. 128. It does not grow with the component size. Is 128 big enough for common problems and is this scalable?

E.g., if the disconnected components are much larger than 128 nodes, there may be few or no bridges near the component boundaries, which can make the graph less navigable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

2 participants