You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Upload the IVF list pointers with one copy per array - #2782
ivf::detail::recompute_internal_state refreshes the device arrays that hold each list's data and index pointers. It uploaded them with one 8-byte raft::copy per list and per array, each from a host stack variable: 2 × n_lists pageable host-to-device copies. The function runs on every IVF-Flat, IVF-PQ and IVF-SQ extend, deserialize, extend_list/erase_list, clone and device refine. ivf_flat::extend runs it twice, once in clone and once after adding the rows.
This PR gathers the pointers into two host vectors and uploads each array with a single raft::copy.
The values are unchanged: list ? list->data_ptr() : nullptr and list ? list->indices_ptr() : nullptr, from the same index.lists().
The copies are enqueued on the same stream, before the same sort kernel.
The function already synchronizes the stream before returning, so the host vectors outlive the copies.
Microbenchmark
IVF-Flat, float, dim 8, 16 rows per list, all lists non-empty, on an RTX 6000 Ada. The same binary was run against libcuvs.so built from main (f751cad) with and without this commit, 3 alternated runs each. Each cell is the median over the 3 runs of the per-run median of 21 calls (11 for extend), with the range of the 3 run medians in parentheses.
n_lists
ivf_flat::helpers::recompute_internal_state, before
after
in-place ivf_flat::extend of 1 row, before
after
1,024
2.74 ms (2.71–2.84)
0.04 ms (0.04–0.05)
5.52 ms (5.52–5.54)
0.23 ms (0.23–0.32)
4,096
10.97 ms (10.88–11.02)
0.09 ms (0.09–0.10)
22.53 ms (22.32–22.55)
0.47 ms (0.44–0.48)
16,384
44.66 ms (44.36–46.05)
0.32 ms (0.30–0.32)
91.44 ms (91.41–94.31)
1.57 ms (1.45–1.58)
65,536
189.81 ms (189.19–192.25)
1.41 ms (1.40–1.46)
385.71 ms (384.92–390.30)
6.41 ms (6.23–6.59)
Before this change the function cost about 2.9 µs per list, which is linear in n_lists. A one-row extend of a 65,536-list index took 386 ms; it now takes 6.4 ms.
The C++ test suites barely notice, because their indexes have few lists. On the same GPU, single process, fastest run: NEIGHBORS_ANN_IVF_FLAT_TEST 228.1 → 219.3 s, NEIGHBORS_ANN_IVF_PQ_TEST 401.7 → 395.5 s and NEIGHBORS_ANN_IVF_SQ_TEST 33.8 → 32.6 s, all close to run-to-run noise. The full ctest suite passes, including the IVF-Flat, IVF-PQ, IVF-SQ, MG, tiered-index, dynamic-batching and refine tests.
Benchmark source
// Times ivf_flat::helpers::recompute_internal_state and a one-row ivf_flat::extend (which calls it)// on IVF-Flat indexes with many lists.
#include<cuvs/neighbors/ivf_flat.hpp>
#include<raft/core/device_mdarray.hpp>
#include<raft/core/resource/cuda_stream.hpp>
#include<raft/core/resources.hpp>
#include<raft/util/cudart_utils.hpp>
#include<raft/random/rng.cuh>
#include<algorithm>
#include<chrono>
#include<cstdio>
#include<vector>template <typename F>
static std::pair<double, double> time_ms(const raft::resources& res, int reps, F&& f)
{
std::vector<double> t;
for (int i = 0; i < reps; i++) {
raft::resource::sync_stream(res);
auto t0 = std::chrono::steady_clock::now();
f();
raft::resource::sync_stream(res);
t.push_back(std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - t0).count());
}
std::sort(t.begin(), t.end());
return {t[t.size() / 2], t.front()};
}
intmain()
{
raft::resources res;
raft::random::RngState rng(42);
constexprint64_t dim = 8;
std::printf("n_lists\tnonempty_lists\trecompute_median_ms\trecompute_min_ms\textend1_median_ms\textend1_min_ms\n");
for (uint32_t n_lists : {1024u, 4096u, 16384u, 65536u}) {
int64_t n_rows = int64_t(n_lists) * 16;
auto data = raft::make_device_matrix<float, int64_t>(res, n_rows, dim);
raft::random::uniform(res, rng, data.data_handle(), n_rows * dim, -1.0f, 1.0f);
cuvs::neighbors::ivf_flat::index_params params;
params.n_lists = n_lists;
params.kmeans_n_iters = 2;
params.kmeans_trainset_fraction = 1.0;
params.add_data_on_build = true;
auto index = cuvs::neighbors::ivf_flat::build(res, params, raft::make_const_mdspan(data.view()));
raft::resource::sync_stream(res);
uint32_t nonempty = 0;
for (auto& l : index.lists()) { nonempty += l != nullptr; }
auto recompute = [&]() { cuvs::neighbors::ivf_flat::helpers::recompute_internal_state(res, &index); };
time_ms(res, 3, recompute); // warm-upauto [rc_med, rc_min] = time_ms(res, 21, recompute);
auto one_row = raft::make_device_matrix_view<constfloat, int64_t>(data.data_handle(), 1, dim);
auto one_idx = raft::make_device_vector<int64_t, int64_t>(res, 1);
int64_t next_id = n_rows;
raft::copy(one_idx.data_handle(), &next_id, 1, raft::resource::get_cuda_stream(res));
auto extend = [&]() {
cuvs::neighbors::ivf_flat::extend(
res, one_row, std::make_optional(raft::make_const_mdspan(one_idx.view())), &index);
};
time_ms(res, 2, extend); // warm-upauto [ex_med, ex_min] = time_ms(res, 11, extend);
std::printf("%u\t%u\t%.3f\t%.3f\t%.3f\t%.3f\n", n_lists, nonempty, rc_med, rc_min, ex_med, ex_min);
std::fflush(stdout);
}
return0;
}
recompute_internal_state copied the data and indices pointers of every list to the device with a
separate one-element copy from pageable host memory, i.e. 2 * n_lists small copies. Gather the
pointers on the host and upload each array with one copy; the function already synchronizes the
stream before returning.
Reviewing files that changed from the base of the PR and between b329a49 and cef7c2e.
You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.
Use the checkbox below for a quick retry:
Trigger review
No actionable comments were generated in the recent review.
Summary
Summary by CodeRabbit
Performance
Reduced the overhead of updating internal index state by batching data and index transfers.
recompute_internal_state now gathers list data and index pointers into host vectors, then copies each vector to device in one operation. The host vectors remain in scope through the existing stream synchronization.
Changes: IVF pointer state recomputation
Layer / File(s)
Summary
Gather and copy list pointers cpp/src/neighbors/ivf_common.cuh
The function gathers list data and index pointers into host vectors, preserving null pointers for absent lists. It copies each vector to device in one operation. The host vectors remain in scope through stream synchronization.
[TRACKER] CI test time improvements #2968: This tracker includes PR #2782 as a library change and tracks CI test-time improvements. This PR reduces pointer-upload work in recompute_internal_state.
Suggested reviewers:lowener
Merge Risk: Minimal · previous review bf503
This change speeds up IVF list-pointer recomputation by batching the uploads into two copies instead of two per list. I found no behavior change or concrete risk in the supplied evidence, so it looks safe to merge once CI passes.
Pre-merge checks: 4
Check name
Status
Explanation
Title check
The title "Upload the IVF list pointers with one copy per array" matches the main change: recompute_internal_state now gathers list data and index pointers into two host vectors and uploads each array…
Description check
The description explains the problem in recompute_internal_state (two 8-byte pageable host-to-device copies per list), describes the fix of gathering pointers into two host vectors with one raft::copy…
Linked Issues check
Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check
Check skipped because no linked issues were found for this pull request.
Full details: Title check
Explanation
The title "Upload the IVF list pointers with one copy per array" matches the main change: recompute_internal_state now gathers list data and index pointers into two host vectors and uploads each array with a single raft::copy instead of one copy per list. The title is short, specific, and identifies the primary change.
Full details: Description check
Explanation
The description explains the problem in recompute_internal_state (two 8-byte pageable host-to-device copies per list), describes the fix of gathering pointers into two host vectors with one raft::copy per array, and includes a microbenchmark and test results. It is closely related to the changeset.
Finishing Touches
Generate unit tests (beta)
Create a new PR
Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Reviewing files that changed from the base of the PR and between 4dd07b8 and bf503b6.
Files selected for processing: 1
cpp/src/neighbors/ivf_common.cuh
Files skipped from review as they are similar to previous changes: 1
cpp/src/neighbors/ivf_common.cuh
Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 11 remain after this review.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
ivf::detail::recompute_internal_staterefreshes the device arrays that hold each list's data and index pointers. It uploaded them with one 8-byteraft::copyper list and per array, each from a host stack variable: 2 ×n_listspageable host-to-device copies. The function runs on every IVF-Flat, IVF-PQ and IVF-SQ extend, deserialize,extend_list/erase_list, clone and devicerefine.ivf_flat::extendruns it twice, once incloneand once after adding the rows.This PR gathers the pointers into two host vectors and uploads each array with a single
raft::copy.list ? list->data_ptr() : nullptrandlist ? list->indices_ptr() : nullptr, from the sameindex.lists().Microbenchmark
IVF-Flat, float, dim 8, 16 rows per list, all lists non-empty, on an RTX 6000 Ada. The same binary was run against
libcuvs.sobuilt from main (f751cad) with and without this commit, 3 alternated runs each. Each cell is the median over the 3 runs of the per-run median of 21 calls (11 forextend), with the range of the 3 run medians in parentheses.n_listsivf_flat::helpers::recompute_internal_state, beforeivf_flat::extendof 1 row, beforeBefore this change the function cost about 2.9 µs per list, which is linear in
n_lists. A one-rowextendof a 65,536-list index took 386 ms; it now takes 6.4 ms.The C++ test suites barely notice, because their indexes have few lists. On the same GPU, single process, fastest run:
NEIGHBORS_ANN_IVF_FLAT_TEST228.1 → 219.3 s,NEIGHBORS_ANN_IVF_PQ_TEST401.7 → 395.5 s andNEIGHBORS_ANN_IVF_SQ_TEST33.8 → 32.6 s, all close to run-to-run noise. The fullctestsuite passes, including the IVF-Flat, IVF-PQ, IVF-SQ, MG, tiered-index, dynamic-batching and refine tests.Benchmark source