-
Notifications
You must be signed in to change notification settings - Fork 150
Deduplicate calc_chunk_indices_kernel
#1657
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
Merged
rapids-bot
merged 9 commits into
rapidsai:main
from
jinsolp:bs/dedup-calc_chunk_indices_kernel
Jan 16, 2026
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
724afcd
separate .cu for calc_chunk_indices_kernel
jinsolp 7599d93
Merge branch 'main' into bs/dedup-calc_chunk_indices_kernel
jinsolp 583d7d1
ptr in same TU
jinsolp 444d00a
Merge branch 'main' into bs/dedup-calc_chunk_indices_kernel
jinsolp f93b2fe
Merge branch 'main' into bs/dedup-calc_chunk_indices_kernel
jinsolp 44f56d2
launch in same TU
jinsolp a50cce9
move arithmetic back to header
jinsolp 0e97eda
Merge branch 'main' into bs/dedup-calc_chunk_indices_kernel
jinsolp 4d6c6bc
Merge branch 'main' into bs/dedup-calc_chunk_indices_kernel
jinsolp 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
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,75 @@ | ||
| /* | ||
| * SPDX-FileCopyrightText: Copyright (c) 2024-2026, NVIDIA CORPORATION. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #include "ivf_common.cuh" | ||
|
|
||
| #include <raft/core/cudart_utils.hpp> | ||
| #include <raft/util/pow2_utils.cuh> | ||
|
|
||
| #include <cub/cub.cuh> | ||
|
|
||
| namespace cuvs::neighbors::ivf::detail { | ||
|
|
||
| /** | ||
| * For each query, we calculate a cumulative sum of the cluster sizes that we probe, and return that | ||
| * in chunk_indices. Essentially this is a segmented inclusive scan of the cluster sizes. The total | ||
| * number of samples per query (sum of the cluster sizes that we probe) is returned in n_samples. | ||
| */ | ||
| template <int BlockDim> | ||
| __launch_bounds__(BlockDim) RAFT_KERNEL | ||
| calc_chunk_indices_kernel(uint32_t n_probes, | ||
| const uint32_t* cluster_sizes, // [n_clusters] | ||
| const uint32_t* clusters_to_probe, // [n_queries, n_probes] | ||
| uint32_t* chunk_indices, // [n_queries, n_probes] | ||
| uint32_t* n_samples // [n_queries] | ||
| ) | ||
| { | ||
| using block_scan = cub::BlockScan<uint32_t, BlockDim>; | ||
| __shared__ typename block_scan::TempStorage shm; | ||
|
|
||
| // locate the query data | ||
| clusters_to_probe += n_probes * blockIdx.x; | ||
| chunk_indices += n_probes * blockIdx.x; | ||
|
|
||
| // block scan | ||
| const uint32_t n_probes_aligned = raft::Pow2<BlockDim>::roundUp(n_probes); | ||
| uint32_t total = 0; | ||
| for (uint32_t probe_ix = threadIdx.x; probe_ix < n_probes_aligned; probe_ix += BlockDim) { | ||
| auto label = probe_ix < n_probes ? clusters_to_probe[probe_ix] : 0u; | ||
| auto chunk = probe_ix < n_probes ? cluster_sizes[label] : 0u; | ||
| if (threadIdx.x == 0) { chunk += total; } | ||
| block_scan(shm).InclusiveSum(chunk, chunk, total); | ||
| __syncthreads(); | ||
| if (probe_ix < n_probes) { chunk_indices[probe_ix] = chunk; } | ||
| } | ||
| // save the total size | ||
| if (threadIdx.x == 0) { n_samples[blockIdx.x] = total; } | ||
| } | ||
|
|
||
| void calc_chunk_indices::configured::operator()(const uint32_t* cluster_sizes, | ||
| const uint32_t* clusters_to_probe, | ||
| uint32_t* chunk_indices, | ||
| uint32_t* n_samples, | ||
| rmm::cuda_stream_view stream) | ||
| { | ||
| void* kernel = nullptr; | ||
| switch (block_dim.x) { | ||
| case 32: kernel = reinterpret_cast<void*>(calc_chunk_indices_kernel<32>); break; | ||
| case 64: kernel = reinterpret_cast<void*>(calc_chunk_indices_kernel<64>); break; | ||
| case 128: kernel = reinterpret_cast<void*>(calc_chunk_indices_kernel<128>); break; | ||
| case 256: kernel = reinterpret_cast<void*>(calc_chunk_indices_kernel<256>); break; | ||
| case 512: kernel = reinterpret_cast<void*>(calc_chunk_indices_kernel<512>); break; | ||
| case 1024: kernel = reinterpret_cast<void*>(calc_chunk_indices_kernel<1024>); break; | ||
| default: | ||
| RAFT_FAIL("Unsupported block dimension for calc_chunk_indices::configured() : %d", | ||
| block_dim.x); | ||
| } | ||
|
|
||
| void* args[] = // NOLINT | ||
| {&n_probes, &cluster_sizes, &clusters_to_probe, &chunk_indices, &n_samples}; | ||
| RAFT_CUDA_TRY(cudaLaunchKernel(kernel, grid_dim, block_dim, args, 0, stream)); | ||
| } | ||
|
|
||
| } // namespace cuvs::neighbors::ivf::detail |
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
The kernel needs to be launched in the same TU as which it is defined. We can (but should ideally avoid) pass the pointer around to other TUs but they shouldn't be attempting to launch the kernel.