Skip to content

Upload the IVF list pointers with one copy per array - #2782

Open
bdice wants to merge 4 commits into
NVIDIA:mainfrom
bdice:ivf-list-pointers-single-copy
Open

bdice wants to merge 4 commits into
NVIDIA:mainfrom
bdice:ivf-list-pointers-single-copy

Conversation

@bdice

@bdice bdice commented Oct 10, 2026

Copy link
Copy Markdown
Contributor

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()};
}

int main()
{
  raft::resources res;
  raft::random::RngState rng(42);
  constexpr int64_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-up
    auto [rc_med, rc_min] = time_ms(res, 21, recompute);
    auto one_row = raft::make_device_matrix_view<const float, 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-up
    auto [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);
  }
  return 0;
}

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.
@bdice bdice added improvement Improves an existing functionality non-breaking Introduces a non-breaking change labels Oct 10, 2026
@copy-pr-bot

copy-pr-bot Bot commented Oct 10, 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.

@bdice

bdice commented Oct 11, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test bbca1f0

@bdice
bdice marked this pull request as ready for review October 29, 2026 18:32
@bdice
bdice requested a review from a team as a code owner October 29, 2026 18:32
@coderabbitai

coderabbitai Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Explain Change →

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.

Estimated code review effort: 2 (Simple) | ~8 minutes

Change: Refactor

Possibly related issues

Suggested reviewers: lowener

Merge Risk: Minimal · up to 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: Passed 4

Check name Status Explanation
Title check Passed 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 Passed 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 Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed 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
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: NVIDIA/cuvs
  • Branch: main
  • File: .coderabbit.yaml
  • Commit: aac8966
  • Layers: Repository YAML
  • Review profile: CHILL
  • Plan: Enterprise
  • Run ID: 40e06bab-d8bc-4a07-8c37-b8bcab33b057
Commits

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.

@bdice

bdice commented Nov 15, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test 4dd07b8

@irina-resh-nvda irina-resh-nvda left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

A nice minimal change, looks good to me. Only one comment nitpick from me

Comment thread cpp/src/neighbors/ivf_common.cuh Outdated

// Actualize the list pointers
// Actualize the list pointers: gather them on the host and upload each array with a single copy
// instead of one tiny pageable H2D copy per list. The host buffers outlive the sync_stream below.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The readers of this comment won't have the context for the historical "instead of one tiny pageable H2D copy per list".

@bdice

bdice commented Nov 16, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test bf503b6

@lowener lowener left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

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

Labels

improvement Improves an existing functionality non-breaking Introduces a non-breaking change

Projects

Status: Approved

Development

Successfully merging this pull request may close these issues.

3 participants