Repository navigation
Add Super K-Means #2948
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
sherylll
wants to merge
47
commits into
NVIDIA:release/26.12
Choose a base branch
from
sherylll:superkmeans
base: release/26.12
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.
+2,640
−3
Open
Add Super K-Means #2948
Changes from 1 commit
Commits
Show all changes
47 commits
Select commit
Hold shift + click to select a range
8b35602
Add SuperKMeans k-means API with an unpruned fallback
sherylll e83a28f
Add SuperKMeans' fused first-filter cuTile kernel
sherylll 19b3fb4
Add SuperKMeans' pruned assignment
sherylll 443bb05
Order SuperKMeans' pruning by survivor count (LPT)
sherylll 669fcc1
Run SuperKMeans' row batches on the handle's stream pool
sherylll 844f2f0
Move SuperKMeans' pruned assignment into a pruned 1-NN distance
sherylll 4bf9ccc
Prune survivor lists in place, out of shared memory
sherylll 18779b1
Write one contiguous first-filter survivor list per row
sherylll e58ee5c
Prune only with dims past the first filter's; simplify the kernel
sherylll 1edbba5
Return early from pruning rows with no survivors
sherylll 75ea3ab
Overlap row batches on the main stream and one pool stream
sherylll fca97b1
Fix style: SuperKMeans API reference, formatting, copyright
sherylll 2d869df
Compute the filter's norms from its FP16 values, with the FP16 copy
sherylll 6a2ae57
Compute the pruning ratios from eps0 in the kernel, not from a device…
sherylll a57a157
Assign in full for half rows the filter can't read in place; document…
sherylll e7cb068
Load the pruning kernel's tiles in 16-byte loads; drop its launch bounds
sherylll 0ee37a6
Add license
sherylll e02f9db
Replace custom warp reduce with cub::WarpReduceBatched
sherylll 31b9674
Merge branch 'main' into superkmeans
sherylll 6b3a662
Hardcode eps0
sherylll f2355d4
Halve pruned 1-NN centroid batch to 1024
sherylll 9565eb9
Merge branch 'main' into superkmeans
sherylll 4760f3b
Merge branch 'main' into superkmeans
sherylll 79bdbba
Prepare SuperKMeans filter rows per batch
sherylll b3e9476
Require supported layouts for high-dimensional SuperKMeans pruning
sherylll a71429f
Size SuperKMeans row batches from workspace budget
sherylll 3b2571b
Tune tile sizes for different arch (coarsely)
sherylll 4c2585a
Fix style
sherylll 26372e2
Add sm103 for completeness; update tile sizes based on measurements on
sherylll 0bbf70c
Merge upstream/main into superkmeans
sherylll 78d844a
Remove duplicate kmeans_type definition
sherylll 5a45435
Small refactor
sherylll 976b372
More refactoring: in F32 mode, use F32 data to calculate norm, even t…
sherylll f92bdc4
Use raft launch_kernel
sherylll 6f9fb0a
Peel single-survivor tail from pruned nearest-neighbor search
sherylll 91ee26a
Order pruning rows from each fused-filter tile
sherylll 7054a3f
Small cleanup
sherylll 3e2964b
Merge branch 'release/26.12' into superkmeans
sherylll e3a2550
Mark SuperKMeans API experimental
sherylll 0a70f8c
Use RAFT warp primitives in pruned assignment
sherylll 572253e
Support partial dimension tiles in pruned assignment
sherylll d2713a5
Remove redundant initial survivor check in prune
sherylll 7fa7a8b
Merge branch 'release/26.12' into superkmeans
sherylll 8cb98b7
Simplify prune survivor arguments and use RAFT warp helpers
sherylll 29970b1
Use workspace memory and dry-run sizing in SuperKMeans
sherylll 82ac907
Add NVTX ranges for pruned 1-NN filter and pruning
sherylll 20e5fbb
Use dry-run execution and round up SuperKMeans batches
sherylll 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
Order SuperKMeans' pruning by survivor count (LPT)
Within a row batch the pruning kernel takes the rows with the most first-filter survivors in the previous iteration first, as SuperKMeans does: the kernel counts each row's survivors and an approximate segmented radix sort of the counts orders the next call's rows. The smoke test now has several row batches. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Loading branch information
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,13 +13,18 @@ | |
| #include <raft/core/device_mdarray.hpp> | ||
| #include <raft/core/error.hpp> | ||
| #include <raft/core/resource/cuda_stream.hpp> | ||
| #include <raft/core/resource/device_memory_resource.hpp> | ||
| #include <raft/core/resources.hpp> | ||
| #include <raft/linalg/map.cuh> | ||
| #include <raft/linalg/map_then_reduce.cuh> | ||
| #include <raft/matrix/sample_rows.cuh> | ||
| #include <raft/random/rng_state.hpp> | ||
| #include <raft/util/cudart_utils.hpp> | ||
|
|
||
| #include <rmm/device_uvector.hpp> | ||
|
|
||
| #include <cub/device/device_segmented_radix_sort.cuh> | ||
|
|
||
| #include <algorithm> | ||
| #include <optional> | ||
| #include <type_traits> | ||
|
|
@@ -114,12 +119,29 @@ void copy_first_columns( | |
| }); | ||
| } | ||
|
|
||
| /** Each row's id within its row batch, and the row batches' bounds (n_row_batches + 1). */ | ||
| inline void row_batch_layout(const raft::resources& handle, | ||
| int64_t n_rows, | ||
| raft::device_vector_view<uint32_t, int64_t> local_ids, | ||
| raft::device_vector_view<int64_t, int64_t> batch_offsets) | ||
| { | ||
| raft::linalg::map_offset(handle, local_ids, [] __device__(int64_t i) { | ||
| return static_cast<uint32_t>(i % kRowBatchSize); | ||
| }); | ||
| raft::linalg::map_offset(handle, batch_offsets, [n_rows] __device__(int64_t b) { | ||
| return b * kRowBatchSize < n_rows ? b * kRowBatchSize : n_rows; | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * The pruned assignment: from each row's current centroid, the fused partial GEMM over the first | ||
| * kDimTileSize dims with the first filter, then the pruning kernel on the survivors' tail dims. | ||
| * Rows go in batches of kRowBatchSize against centroid batches of kCentroidBatchSize. The filter | ||
| * runs on FP16: float rows are filtered on half copies of their first dims, and the centroids the | ||
| * kernels read are T, a half copy of the float centroids for half rows. | ||
| * | ||
| * Within a row batch the pruning kernel's blocks take the heaviest rows first (LPT): the rows | ||
| * ordered by their first-filter survivors in the previous call. | ||
| */ | ||
| template <typename T> | ||
| class pruned_assignment { | ||
|
|
@@ -135,11 +157,17 @@ class pruned_assignment { | |
| n_clusters_(n_clusters), | ||
| filter_dim_(std::is_same_v<T, half> ? dim_ : kDimTileSize), | ||
| tile_(tile), | ||
| n_row_batches_(raft::ceildiv(n_rows_, kRowBatchSize)), | ||
| n_col_tiles_(raft::ceildiv<int64_t>(kCentroidBatchSize, tile.tile_n)), | ||
| ratios_(raft::make_device_vector<float, int64_t>(handle, dim_ + 1)), | ||
| row_norms_(raft::make_device_vector<float, int64_t>(handle, n_rows_)), | ||
| centroid_norms_(raft::make_device_vector<float, int64_t>(handle, n_clusters)), | ||
| dist_(raft::make_device_vector<float, int64_t>(handle, n_rows_)), | ||
| survivors_(raft::make_device_vector<uint32_t, int64_t>(handle, n_rows_)), | ||
| row_order_(raft::make_device_vector<uint32_t, int64_t>(handle, n_rows_)), | ||
| local_ids_(raft::make_device_vector<uint32_t, int64_t>(handle, n_rows_)), | ||
| sorted_keys_(raft::make_device_vector<uint32_t, int64_t>(handle, n_rows_)), | ||
| batch_offsets_(raft::make_device_vector<int64_t, int64_t>(handle, n_row_batches_ + 1)), | ||
| filter_rows_(raft::make_device_matrix<half, int64_t>( | ||
| handle, std::is_same_v<T, half> ? 0 : n_rows_, filter_dim_)), | ||
| filter_centroids_(raft::make_device_matrix<half, int64_t>(handle, n_clusters, filter_dim_)), | ||
|
|
@@ -156,15 +184,22 @@ class pruned_assignment { | |
| if constexpr (!std::is_same_v<T, half>) { | ||
| copy_columns(X.data_handle(), n_rows_, filter_rows_.view()); | ||
| } | ||
| // LPT's sort: each row's id within its batch, and the batches' bounds | ||
| row_batch_layout(handle, n_rows_, local_ids_.view(), batch_offsets_.view()); | ||
| // The host ratios are released when the copy is done | ||
| raft::resource::sync_stream(handle); | ||
| } | ||
|
|
||
| /** Updates `labels`, each row's centroid from the previous assignment, for `centroids`. */ | ||
| /** | ||
| * Updates `labels`, each row's centroid from the previous assignment, for `centroids`. | ||
| * `order_next`: another call follows, so order the rows for it. | ||
| */ | ||
| void assign(raft::device_matrix_view<const float, int64_t> centroids, | ||
| raft::device_vector_view<uint32_t, int64_t> labels) | ||
| raft::device_vector_view<uint32_t, int64_t> labels, | ||
| bool order_next) | ||
| { | ||
| #if CUVS_CUTILE_ENABLED | ||
| auto stream = raft::resource::get_cuda_stream(handle_).get(); | ||
| // The filter's FP16 copy of the centroids' first dims; for half rows, all dims, which is also | ||
| // the copy the other kernels read | ||
| copy_columns(centroids.data_handle(), n_clusters_, filter_centroids_.view()); | ||
|
|
@@ -179,12 +214,15 @@ class pruned_assignment { | |
| } | ||
| squared_norms( | ||
| handle_, kernel_centroids, n_clusters_, dim_, kDimTileSize, centroid_norms_.data_handle()); | ||
| RAFT_CUDA_TRY( | ||
| cudaMemsetAsync(survivors_.data_handle(), 0, survivors_.size() * sizeof(uint32_t), stream)); | ||
|
|
||
| for (int64_t i = 0; i < n_rows_; i += kRowBatchSize) { | ||
| const int64_t n_rows = std::min(kRowBatchSize, n_rows_ - i); | ||
| const T* rows = X_.data_handle() + i * dim_; | ||
| uint32_t* row_labels = labels.data_handle() + i; | ||
| float* row_dist = dist_.data_handle() + i; | ||
| const int64_t n_rows = std::min(kRowBatchSize, n_rows_ - i); | ||
| const T* rows = X_.data_handle() + i * dim_; | ||
| uint32_t* row_labels = labels.data_handle() + i; | ||
| float* row_dist = dist_.data_handle() + i; | ||
| const uint32_t* order = has_row_order_ ? row_order_.data_handle() + i : nullptr; | ||
| // Each row's bound: its distance to its current centroid | ||
| distance_to_assigned(handle_, rows, n_rows, kernel_centroids, dim_, row_labels, row_dist); | ||
| for (int64_t j = 0; j < n_clusters_; j += kCentroidBatchSize) { | ||
|
|
@@ -205,6 +243,7 @@ class pruned_assignment { | |
| prune(handle_, | ||
| rows, | ||
| n_rows, | ||
| order, | ||
| kernel_centroids + j * dim_, | ||
| static_cast<uint32_t>(j), | ||
| dim_, | ||
|
|
@@ -216,9 +255,14 @@ class pruned_assignment { | |
| static_cast<uint32_t>(raft::ceildiv<int64_t>(n_centroids, tile_.tile_n)), | ||
| static_cast<uint32_t>(tile_.tile_n)}, | ||
| row_labels, | ||
| row_dist); | ||
| row_dist, | ||
| survivors_.data_handle() + i); | ||
| } | ||
| } | ||
| if (order_next) { | ||
| order_rows_by_survivors(stream); | ||
| has_row_order_ = true; | ||
| } | ||
| #else | ||
| RAFT_FAIL("SuperKMeans pruning needs a build with cuTile"); | ||
| #endif | ||
|
|
@@ -232,13 +276,52 @@ class pruned_assignment { | |
| copy_first_columns(handle_, in, n, dim_, filter_dim_, out.data_handle()); | ||
| } | ||
|
|
||
| /** | ||
| * Each row batch's local row ids ordered by survivor count, descending, into row_order_. | ||
| * Approximate: one radix pass over the counts' top 8 significant bits (the counts are at most | ||
| * n_clusters), rows of a bucket keeping their order. | ||
| */ | ||
| void order_rows_by_survivors(cudaStream_t stream) | ||
| { | ||
| int end_bit = 1; | ||
| while (end_bit < 32 && (int64_t{1} << end_bit) <= n_clusters_) { | ||
| end_bit++; | ||
| } | ||
|
Comment on lines
+224
to
+231
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like this approach, but perhaps we can start with something smaller than |
||
| const int begin_bit = std::max(0, end_bit - 8); | ||
| auto sort = [&](void* temp, size_t& temp_bytes) { | ||
| RAFT_CUDA_TRY( | ||
| cub::DeviceSegmentedRadixSort::SortPairsDescending(temp, | ||
| temp_bytes, | ||
| survivors_.data_handle(), | ||
| sorted_keys_.data_handle(), | ||
| local_ids_.data_handle(), | ||
| row_order_.data_handle(), | ||
| n_rows_, | ||
| n_row_batches_, | ||
| batch_offsets_.data_handle(), | ||
| batch_offsets_.data_handle() + 1, | ||
| begin_bit, | ||
| end_bit, | ||
| stream)); | ||
| }; | ||
| size_t temp_bytes = 0; | ||
| sort(nullptr, temp_bytes); | ||
| rmm::device_uvector<char> temp( | ||
| temp_bytes, stream, raft::resource::get_workspace_resource_ref(handle_)); | ||
| sort(temp.data(), temp_bytes); | ||
| } | ||
|
|
||
| const raft::resources& handle_; | ||
| raft::device_matrix_view<const T, int64_t> X_; | ||
| int64_t n_rows_, dim_, n_clusters_, filter_dim_; | ||
| fused_filter_tile tile_; | ||
| int64_t n_row_batches_; | ||
| int64_t n_col_tiles_; | ||
| float ratio_; | ||
| float ratio_ = 1.0f; | ||
| bool has_row_order_ = false; | ||
| raft::device_vector<float, int64_t> ratios_, row_norms_, centroid_norms_, dist_; | ||
| raft::device_vector<uint32_t, int64_t> survivors_, row_order_, local_ids_, sorted_keys_; | ||
| raft::device_vector<int64_t, int64_t> batch_offsets_; | ||
| raft::device_matrix<half, int64_t> filter_rows_, filter_centroids_; | ||
| raft::device_vector<int64_t, int64_t> packed_; | ||
| raft::device_vector<int32_t, int64_t> seg_count_; | ||
|
|
@@ -299,7 +382,9 @@ void fit(const raft::resources& handle, | |
|
|
||
| for (uint32_t iter = 0; iter < params.n_iters; iter++) { | ||
| if (pruned_assign.has_value() && iter > 0) { | ||
| pruned_assign->assign(raft::make_const_mdspan(centroids), assigned.view()); | ||
| // The last pruned call orders no rows: the final labels come from a full assignment | ||
| pruned_assign->assign( | ||
| raft::make_const_mdspan(centroids), assigned.view(), iter + 1 < params.n_iters); | ||
| } else { | ||
| assign(); | ||
| } | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.