Repository navigation
[WIP] ANNs for clustering at training time #1938
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
HowardHuang1
wants to merge
22
commits into
NVIDIA:main
Choose a base branch
from
HowardHuang1:HH-ANNs-for-clustering-at-training
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
9fd71ef
add CAGRA ANN to replace brute force kmeans for prediction + test cas…
HowardHuang1 80070c4
fix test cases + fix threshold
HowardHuang1 fa6fd70
add test cases for entire IVF-PQ pipeline
HowardHuang1 d8543a6
rename variable to use_ann_for_extend since previous changes are only…
HowardHuang1 d26e916
Replace brute force with CAGRA ANN in build()
HowardHuang1 ad054af
add test cases
HowardHuang1 0f14da1
Merge remote-tracking branch 'upstream' into HH-ANNs-for-clustering-a…
HowardHuang1 e69259f
rename use_cagra and use_ann variables to use_cagra_for_cluster_assig…
HowardHuang1 1d76516
fix null list or empty trained indices error for IVF-PQ serialize cau…
HowardHuang1 8e62682
rename benchmark names so that Build_NearestCentroidLookup_Speedup mi…
HowardHuang1 167f0c4
refactor out common code shared by BM_IVFPQ_Build_NearestCentroidLook…
HowardHuang1 64586ad
add ann for cluster assignment to postfit. Added use_ann_for_build_po…
HowardHuang1 de80d26
Merge remote-tracking branch 'upstream' into HH-ANNs-for-clustering-a…
HowardHuang1 7daf9cb
add tuning script for ann_rebuild_interval balancing recall and speedup
HowardHuang1 76ebb73
Merge remote-tracking branch 'upstream' into HH-ANNs-for-clustering-a…
HowardHuang1 525cd4c
Merge remote-tracking branch 'upstream/main' into HH-ANNs-for-cluster…
HowardHuang1 1673e39
fix merge conflict with build now taking only dataset views and not d…
HowardHuang1 3951248
Replace hand-picked cluster-assignment bench args with controlled K s…
HowardHuang1 1daabbe
Add controlled fixed-K/vary-N, grid, search-only, dim, and rebuild-in…
HowardHuang1 52ddd3d
Merge remote-tracking branch 'upstream/main' into HH-ANNs-for-cluster…
HowardHuang1 3de0823
Fix benchmark plots and make fit-tuning quality metrics meaningful
HowardHuang1 73825bd
Read benchmark datasets from .fbin files and redesign plot panel 4
HowardHuang1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
add test cases for entire IVF-PQ pipeline
- Loading branch information
commit fa6fd704dc82437dbc4c09548592795f3ebc25db
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| /* | ||
| * SPDX-FileCopyrightText: Copyright (c) 2025, NVIDIA CORPORATION. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * Benchmark: full IVF-PQ build path and compare brute-force vs CAGRA cluster assignment. | ||
| * For each scenario we run one benchmark that does both a brute and a CAGRA full build, | ||
| * reports brute_ms and cagra_ms, and speedup = brute_ms / cagra_ms (>1 means CAGRA faster). | ||
| * | ||
| * n_lists = number of cluster centroids. We use at least 5 vectors per cluster | ||
| * (n_vectors >= 5 * n_lists). "Time" = wall time for one iteration (brute + CAGRA build). | ||
| * | ||
| * Full build includes kmeans, PQ codebook training, and assignment. Threshold 200K in | ||
| * ivf_pq_build.cuh was chosen from assignment-only benchmarks. | ||
| */ | ||
| #include <benchmark/benchmark.h> | ||
|
|
||
| #include <cuvs/neighbors/ivf_pq.hpp> | ||
|
|
||
| #include <chrono> | ||
| #include <memory> | ||
| #include <raft/core/device_mdarray.hpp> | ||
| #include <raft/core/device_resources.hpp> | ||
| #include <raft/core/resources.hpp> | ||
| #include <raft/core/resource/cuda_stream_pool.hpp> | ||
| #include <raft/random/rng.cuh> | ||
| #include <raft/random/rng_state.hpp> | ||
| #include <raft/util/cudart_utils.hpp> | ||
|
|
||
| #include <rmm/cuda_stream_pool.hpp> | ||
| #include <rmm/device_uvector.hpp> | ||
|
|
||
| namespace { | ||
|
|
||
| void init_random_dataset(raft::resources const& handle, | ||
| float* data, | ||
| int64_t n_rows, | ||
| int64_t dim) | ||
| { | ||
| raft::random::RngState rng(12345ULL); | ||
| raft::random::uniform(handle, rng, data, n_rows * dim, float(-1), float(1)); | ||
| raft::resource::sync_stream(handle); | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| static void BM_IVFPQ_Build_Speedup(benchmark::State& state) | ||
| { | ||
| int64_t n_rows = static_cast<int64_t>(state.range(0)); | ||
| uint32_t n_lists = static_cast<uint32_t>(state.range(1)); | ||
| int64_t dim = static_cast<int64_t>(state.range(2)); | ||
|
|
||
| raft::device_resources handle; | ||
| rmm::device_uvector<float> dataset(static_cast<size_t>(n_rows) * static_cast<size_t>(dim), | ||
| raft::resource::get_cuda_stream(handle)); | ||
| init_random_dataset(handle, dataset.data(), n_rows, dim); | ||
|
|
||
| cuvs::neighbors::ivf_pq::index_params params_brute; | ||
| params_brute.n_lists = n_lists; | ||
| params_brute.kmeans_n_iters = 3; | ||
| params_brute.kmeans_trainset_fraction = 0.2; | ||
| params_brute.add_data_on_build = true; | ||
| params_brute.metric = cuvs::distance::DistanceType::L2Expanded; | ||
| params_brute.use_ann_for_cluster_assignment = false; | ||
|
|
||
| cuvs::neighbors::ivf_pq::index_params params_cagra = params_brute; | ||
| params_cagra.use_ann_for_cluster_assignment = true; | ||
|
|
||
| raft::resource::set_cuda_stream_pool(handle, std::make_shared<rmm::cuda_stream_pool>(1)); | ||
|
|
||
| auto dataset_view = raft::make_device_matrix_view<const float, int64_t, raft::row_major>( | ||
| dataset.data(), n_rows, dim); | ||
|
|
||
| double total_brute_ms = 0.0, total_cagra_ms = 0.0; | ||
| int64_t iterations = 0; | ||
|
|
||
| for (auto _ : state) { | ||
| auto start = std::chrono::steady_clock::now(); | ||
| auto idx_brute = cuvs::neighbors::ivf_pq::build(handle, params_brute, dataset_view); | ||
| benchmark::DoNotOptimize(idx_brute.size()); | ||
| raft::resource::sync_stream(handle); | ||
| auto end = std::chrono::steady_clock::now(); | ||
| total_brute_ms += 1e-6 * std::chrono::duration<double, std::nano>(end - start).count(); | ||
|
|
||
| start = std::chrono::steady_clock::now(); | ||
| auto idx_cagra = cuvs::neighbors::ivf_pq::build(handle, params_cagra, dataset_view); | ||
| benchmark::DoNotOptimize(idx_cagra.size()); | ||
| raft::resource::sync_stream(handle); | ||
| end = std::chrono::steady_clock::now(); | ||
| total_cagra_ms += 1e-6 * std::chrono::duration<double, std::nano>(end - start).count(); | ||
|
|
||
| ++iterations; | ||
| } | ||
|
|
||
| if (total_cagra_ms > 0) { | ||
| state.counters["speedup"] = total_brute_ms / total_cagra_ms; | ||
| } | ||
| state.counters["brute_ms"] = benchmark::Counter(total_brute_ms, | ||
| benchmark::Counter::kAvgIterations); | ||
| state.counters["cagra_ms"] = benchmark::Counter(total_cagra_ms, | ||
| benchmark::Counter::kAvgIterations); | ||
| } | ||
|
|
||
| constexpr int64_t kDim = 128; | ||
|
|
||
| // At least 5 vectors per cluster (n_vectors = 5 * n_lists). One row per config: brute_ms, cagra_ms, speedup. | ||
|
|
||
| // 1. 64K centroids, 5 vecs/cluster | ||
| BENCHMARK(BM_IVFPQ_Build_Speedup) | ||
| ->Args({327680, 65536, kDim}) | ||
| ->Unit(benchmark::kMillisecond) | ||
| ->UseRealTime() | ||
| ->ArgNames({"n_vectors", "n_lists", "dim"}); | ||
|
|
||
| // 2. 200K centroids, 5 vecs/cluster | ||
| BENCHMARK(BM_IVFPQ_Build_Speedup) | ||
| ->Args({1000000, 200000, kDim}) | ||
| ->Unit(benchmark::kMillisecond) | ||
| ->UseRealTime() | ||
| ->ArgNames({"n_vectors", "n_lists", "dim"}); | ||
|
|
||
| // 3. 400K centroids, 5 vecs/cluster | ||
| BENCHMARK(BM_IVFPQ_Build_Speedup) | ||
| ->Args({2000000, 400000, kDim}) | ||
| ->Unit(benchmark::kMillisecond) | ||
| ->UseRealTime() | ||
| ->ArgNames({"n_vectors", "n_lists", "dim"}); | ||
|
|
||
| // 4. 600K centroids, 5 vecs/cluster | ||
| BENCHMARK(BM_IVFPQ_Build_Speedup) | ||
| ->Args({3000000, 600000, kDim}) | ||
| ->Unit(benchmark::kMillisecond) | ||
| ->UseRealTime() | ||
| ->ArgNames({"n_vectors", "n_lists", "dim"}); | ||
|
|
||
| // 5. 800K centroids, 5 vecs/cluster | ||
| BENCHMARK(BM_IVFPQ_Build_Speedup) | ||
| ->Args({4000000, 800000, kDim}) | ||
| ->Unit(benchmark::kMillisecond) | ||
| ->UseRealTime() | ||
| ->ArgNames({"n_vectors", "n_lists", "dim"}); | ||
|
|
||
| // 6. 1M centroids, 5 vecs/cluster | ||
| BENCHMARK(BM_IVFPQ_Build_Speedup) | ||
| ->Args({5000000, 1000000, kDim}) | ||
| ->Unit(benchmark::kMillisecond) | ||
| ->UseRealTime() | ||
| ->ArgNames({"n_vectors", "n_lists", "dim"}); | ||
|
|
||
| BENCHMARK_MAIN(); |
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
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updates to IVF-PQ are not needed for this PR. Lets just stick to Lloyd's algorithm (regular kmeans). Balanced kmeans can be looked into later.