From d54375e6d05b4e878dac1553a04cdbca1c7db91f Mon Sep 17 00:00:00 2001 From: Bradley Dice Date: Mon, 5 Oct 2026 01:34:27 -0500 Subject: [PATCH 1/2] Search CAGRA queries in batches regardless of row alignment search_main_core only batched queries whose row pitch was a multiple of 16 bytes; other dims, and queries carrying CAGRA row padding, were copied and then searched one query per plan call. The kernels read query rows only in setup_workspace, with scalar loads at queries + dim * query_id, so dense [n, dim] queries can be searched in place in batches for any dim. Queries with CAGRA row padding (the iterative graph build) are packed one batch at a time into a dense workspace buffer and then searched as a batch. --- .../neighbors/detail/cagra/cagra_build.cuh | 4 +- .../neighbors/detail/cagra/cagra_search.cuh | 113 ++++++++---------- 2 files changed, 50 insertions(+), 67 deletions(-) diff --git a/cpp/src/neighbors/detail/cagra/cagra_build.cuh b/cpp/src/neighbors/detail/cagra/cagra_build.cuh index 9a22676334..1377841d1f 100644 --- a/cpp/src/neighbors/detail/cagra/cagra_build.cuh +++ b/cpp/src/neighbors/detail/cagra/cagra_build.cuh @@ -2347,8 +2347,8 @@ void reconstruct_vpq_queries(raft::resources const& res, // Query source: // - omitted VPQ arguments: queries are read directly from `dev_query_view` // (uncompressed build; the view is a slice of the resident padded device dataset, including -// CAGRA row padding). `cagra::detail::search_main` accepts that padded row width so search -// does not depad/re-pad the chunk. +// CAGRA row padding). `cagra::detail::search_main` accepts that padded row width and packs +// each search batch into a dense workspace buffer before the batched search. // - VPQ arguments present: `dev_query_view` is ignored and each chunk of queries is reconstructed // on the fly from the VPQ codes into `reconstructed_batch_queries` with CAGRA row padding, // so we never materialize the whole (up to N x stride) reconstructed dataset. diff --git a/cpp/src/neighbors/detail/cagra/cagra_search.cuh b/cpp/src/neighbors/detail/cagra/cagra_search.cuh index e9902d049e..8189d71177 100644 --- a/cpp/src/neighbors/detail/cagra/cagra_search.cuh +++ b/cpp/src/neighbors/detail/cagra/cagra_search.cuh @@ -92,76 +92,59 @@ void search_main_core( RAFT_LOG_DEBUG("Cagra search"); const uint32_t max_queries = plan->max_queries; - // Same 16B row-pitch rule as make_device_padded_dataset. Tight [n, dim] rows can be misaligned - // between rows (e.g. float, dim=1) and trigger misaligned access in CAGRA search. Callers may - // also pass an already-padded [n, stride] matrix; `query_logical_dim` is the feature width used - // by setup_workspace (`+= dim * query_id`). If query_row_stride > logical dim, run one query per - // plan call so every kernel sees query_id==0 and the base pointer selects the row (keeps the - // batched path when stride==dim). - const DataT* queries_buf{}; - uint32_t query_row_stride{}; - std::unique_ptr> queries_padded_own; - if (cuvs::neighbors::matrix_row_width_matches_cagra_required(queries)) { - auto v = cuvs::neighbors::make_device_padded_dataset_view(res, queries); - queries_buf = v.view().data_handle(); - query_row_stride = v.stride(); - } else { - queries_padded_own = cuvs::neighbors::make_device_padded_dataset(res, queries); - auto v = queries_padded_own->as_dataset_view(); - queries_buf = v.view().data_handle(); - query_row_stride = v.stride(); + // The search kernels read query `i` of a batch element by element (scalar loads, no vectorized + // access) starting at `queries + query_logical_dim * i` (see setup_workspace). A batch therefore + // needs a dense row pitch of exactly `query_logical_dim` elements; the 16B CAGRA row alignment + // applies to the dataset, not to the queries. Dense [n, dim] queries are searched in place. + // Queries that carry CAGRA row padding ([n, stride], stride > dim; e.g. a slice of a padded + // dataset in the iterative build) are packed one batch at a time into a dense workspace buffer. + // Either way every batch of up to `max_queries` queries is searched with one plan call. + const uint32_t query_row_width = static_cast(queries.extent(1)); + const bool pack_queries = query_row_width != query_logical_dim; + const DataT* queries_ptr = + cuvs::neighbors::detail::expect_device_accessible_data_handle( + queries, "CAGRA search: queries must be device-accessible."); + lightweight_uvector packed_queries(res); + if (pack_queries) { + packed_queries.resize( + std::min(max_queries, queries.extent(0)) * static_cast(query_logical_dim), + raft::resource::get_cuda_stream(res)); } - const bool can_batch_n_queries = (query_row_stride == query_logical_dim); for (unsigned qid = 0; qid < queries.extent(0); qid += max_queries) { const uint32_t n_queries = std::min(max_queries, queries.extent(0) - qid); - if (can_batch_n_queries) { - auto _topk_indices_ptr = neighbors.data_handle() + (topk * qid); - auto _topk_distances_ptr = distances.data_handle() + (topk * qid); - const auto* _query_ptr = - queries_buf + (static_cast(query_row_stride) * static_cast(qid)); - const auto* _seed_ptr = - plan->num_seeds > 0 - ? reinterpret_cast(plan->dev_seed.data()) + (plan->num_seeds * qid) - : nullptr; - uint32_t* _num_executed_iterations = nullptr; - - (*plan)(res, - graph, - source_indices, - _topk_indices_ptr, - _topk_distances_ptr, - _query_ptr, - n_queries, - _seed_ptr, - _num_executed_iterations, - topk, - set_offset(sample_filter, qid)); - } else { - for (uint32_t qi = 0; qi < n_queries; ++qi) { - const size_t g = static_cast(qid) + static_cast(qi); - auto _topk_indices_ptr = neighbors.data_handle() + (topk * g); - auto _topk_distances_ptr = distances.data_handle() + (topk * g); - const auto* _query_ptr = queries_buf + (query_row_stride * g); - const auto* _seed_ptr = - plan->num_seeds > 0 - ? reinterpret_cast(plan->dev_seed.data()) + (plan->num_seeds * g) - : nullptr; - uint32_t* _num_executed_iterations = nullptr; - - (*plan)(res, - graph, - source_indices, - _topk_indices_ptr, - _topk_distances_ptr, - _query_ptr, - 1u, - _seed_ptr, - _num_executed_iterations, - topk, - set_offset(sample_filter, g)); - } + auto _topk_indices_ptr = neighbors.data_handle() + (static_cast(topk) * qid); + auto _topk_distances_ptr = distances.data_handle() + (static_cast(topk) * qid); + const DataT* _query_ptr = queries_ptr + (static_cast(query_row_width) * qid); + if (pack_queries) { + raft::copy_matrix(packed_queries.data(), + query_logical_dim, + _query_ptr, + query_row_width, + query_logical_dim, + n_queries, + raft::resource::get_cuda_stream(res)); + // The persistent kernel takes jobs from the host without waiting on the `res` stream. + if (plan->persistent) { raft::resource::sync_stream(res); } + _query_ptr = packed_queries.data(); } + const auto* _seed_ptr = + plan->num_seeds > 0 + ? reinterpret_cast(plan->dev_seed.data()) + (plan->num_seeds * qid) + : nullptr; + uint32_t* _num_executed_iterations = nullptr; + + (*plan)(res, + graph, + source_indices, + _topk_indices_ptr, + _topk_distances_ptr, + _query_ptr, + n_queries, + _seed_ptr, + _num_executed_iterations, + topk, + set_offset(sample_filter, qid)); } } From a6c7f8331a2c7b29bbbee9616d0ccaf209c8a6c1 Mon Sep 17 00:00:00 2001 From: Bradley Dice Date: Sun, 15 Nov 2026 08:22:41 -0600 Subject: [PATCH 2/2] Restore upstream VPQ dataset view and stride checks in CAGRA search_main. --- cpp/src/neighbors/detail/cagra/cagra_search.cuh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cpp/src/neighbors/detail/cagra/cagra_search.cuh b/cpp/src/neighbors/detail/cagra/cagra_search.cuh index 8189d71177..90eb4092a4 100644 --- a/cpp/src/neighbors/detail/cagra/cagra_search.cuh +++ b/cpp/src/neighbors/detail/cagra/cagra_search.cuh @@ -239,7 +239,7 @@ void search_main(raft::resources const& res, params.smem_dtype = cuvs::neighbors::cagra::internal_dtype::F16; } auto desc = dataset_descriptor_init_with_cache( - res, params, vv.dset(), index.metric(), nullptr); + res, params, vv, index.metric(), nullptr); search_main_core( res, params, @@ -252,8 +252,9 @@ void search_main(raft::resources const& res, query_logical_dim, sample_filter); } else if constexpr (cuvs::neighbors::is_device_standard_dataset_view_v) { + auto const stride = index.dataset().data_const_view().stride(0); RAFT_EXPECTS( - cuvs::neighbors::matrix_row_width_matches_cagra_required(index.dataset().view()), + cuvs::neighbors::cagra_required_row_width(stride) == stride, "CAGRA search requires each dataset row to have the CAGRA-aligned stride. Create a padded " "dataset with make_device_padded_dataset() and attach it with cagra::update_dataset()."); run_strided_like(index.dataset());