Skip to content
Open
Show file tree
Hide file tree
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 Oct 26, 2026
e83a28f
Add SuperKMeans' fused first-filter cuTile kernel
sherylll Oct 26, 2026
19b3fb4
Add SuperKMeans' pruned assignment
sherylll Oct 26, 2026
443bb05
Order SuperKMeans' pruning by survivor count (LPT)
sherylll Oct 26, 2026
669fcc1
Run SuperKMeans' row batches on the handle's stream pool
sherylll Oct 26, 2026
844f2f0
Move SuperKMeans' pruned assignment into a pruned 1-NN distance
sherylll Oct 26, 2026
4bf9ccc
Prune survivor lists in place, out of shared memory
sherylll Oct 28, 2026
18779b1
Write one contiguous first-filter survivor list per row
sherylll Oct 28, 2026
e58ee5c
Prune only with dims past the first filter's; simplify the kernel
sherylll Oct 28, 2026
1edbba5
Return early from pruning rows with no survivors
sherylll Oct 28, 2026
75ea3ab
Overlap row batches on the main stream and one pool stream
sherylll Oct 28, 2026
fca97b1
Fix style: SuperKMeans API reference, formatting, copyright
sherylll Oct 28, 2026
2d869df
Compute the filter's norms from its FP16 values, with the FP16 copy
sherylll Oct 30, 2026
6a2ae57
Compute the pruning ratios from eps0 in the kernel, not from a device…
sherylll Nov 2, 2026
a57a157
Assign in full for half rows the filter can't read in place; document…
sherylll Nov 2, 2026
e7cb068
Load the pruning kernel's tiles in 16-byte loads; drop its launch bounds
sherylll Nov 2, 2026
0ee37a6
Add license
sherylll Nov 3, 2026
e02f9db
Replace custom warp reduce with cub::WarpReduceBatched
sherylll Nov 3, 2026
31b9674
Merge branch 'main' into superkmeans
sherylll Nov 4, 2026
6b3a662
Hardcode eps0
sherylll Nov 5, 2026
f2355d4
Halve pruned 1-NN centroid batch to 1024
sherylll Nov 6, 2026
9565eb9
Merge branch 'main' into superkmeans
sherylll Nov 6, 2026
4760f3b
Merge branch 'main' into superkmeans
sherylll Nov 8, 2026
79bdbba
Prepare SuperKMeans filter rows per batch
sherylll Nov 9, 2026
b3e9476
Require supported layouts for high-dimensional SuperKMeans pruning
sherylll Nov 9, 2026
a71429f
Size SuperKMeans row batches from workspace budget
sherylll Nov 9, 2026
3b2571b
Tune tile sizes for different arch (coarsely)
sherylll Nov 9, 2026
4c2585a
Fix style
sherylll Nov 9, 2026
26372e2
Add sm103 for completeness; update tile sizes based on measurements on
sherylll Nov 10, 2026
0bbf70c
Merge upstream/main into superkmeans
sherylll Nov 11, 2026
78d844a
Remove duplicate kmeans_type definition
sherylll Nov 11, 2026
5a45435
Small refactor
sherylll Nov 11, 2026
976b372
More refactoring: in F32 mode, use F32 data to calculate norm, even t…
sherylll Nov 11, 2026
f92bdc4
Use raft launch_kernel
sherylll Nov 12, 2026
6f9fb0a
Peel single-survivor tail from pruned nearest-neighbor search
sherylll Nov 13, 2026
91ee26a
Order pruning rows from each fused-filter tile
sherylll Nov 13, 2026
7054a3f
Small cleanup
sherylll Nov 16, 2026
3e2964b
Merge branch 'release/26.12' into superkmeans
sherylll Nov 16, 2026
e3a2550
Mark SuperKMeans API experimental
sherylll Nov 18, 2026
0a70f8c
Use RAFT warp primitives in pruned assignment
sherylll Nov 18, 2026
572253e
Support partial dimension tiles in pruned assignment
sherylll Nov 18, 2026
d2713a5
Remove redundant initial survivor check in prune
sherylll Nov 18, 2026
7fa7a8b
Merge branch 'release/26.12' into superkmeans
sherylll Nov 18, 2026
8cb98b7
Simplify prune survivor arguments and use RAFT warp helpers
sherylll Nov 18, 2026
29970b1
Use workspace memory and dry-run sizing in SuperKMeans
sherylll Nov 19, 2026
82ac907
Add NVTX ranges for pruned 1-NN filter and pruning
sherylll Nov 19, 2026
20e5fbb
Use dry-run execution and round up SuperKMeans batches
sherylll Nov 19, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Order pruning rows from each fused-filter tile
  • Loading branch information
sherylll committed Nov 13, 2026
commit 91ee26aad294d198feac94c5c708aa504bfb0fca
27 changes: 10 additions & 17 deletions cpp/src/cluster/detail/superkmeans.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ inline bool prunes(int64_t n_clusters, int64_t dim)
return dim > kMinPrunedDim && n_clusters > kMinPrunedK;
}

/** Budget the per-stream survivor lists and filter inputs together, as in balanced prediction. */
/** Budget the per-stream survivor lists, counts, row order, and optional FP16 filter rows. */
template <typename T>
auto calc_minibatch_size(const raft::resources& handle, int64_t n_rows) -> int64_t
{
std::size_t mem_per_row = pruned_nn::kCentroidBatchSize * sizeof(int64_t) + sizeof(int32_t);
std::size_t mem_per_row =
pruned_nn::kCentroidBatchSize * sizeof(int64_t) + sizeof(int32_t) + sizeof(uint32_t);
if constexpr (!std::is_same_v<T, half>) { mem_per_row += pruned_nn::kPartialDim * sizeof(half); }
const auto pool_size = raft::resource::is_stream_pool_initialized(handle)
? static_cast<int64_t>(raft::resource::get_stream_pool_size(handle))
Expand Down Expand Up @@ -127,7 +128,7 @@ void predict(const raft::resources& handle,

/**
* The pruned assignment (cuvs::distance::pruned_top_1_nn) and what it keeps across iterations: the
* row norms, the row order, and the workspace. Float filter rows are prepared per batch.
* row norms and the workspace. Float filter rows are prepared per batch.
* The centroids the 1-NN reads are T: for half rows, a half copy of the float centroids, which is
* also the filter's.
*/
Expand All @@ -146,12 +147,11 @@ class pruned_assignment {
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_)),
row_order_(raft::make_device_vector<uint32_t, int64_t>(handle, n_rows_)),
filter_centroids_(raft::make_device_matrix<half, int64_t>(handle, n_clusters, filter_dim_)),
params_{.row_batch_size = calc_minibatch_size<T>(handle, n_rows_)},
row_batch_size_(calc_minibatch_size<T>(handle, n_rows_)),
workspace_(raft::make_device_vector<char, int64_t>(
handle,
cuvs::distance::pruned_top_1_nn_workspace_size<T>(handle, n_rows_, params_.row_batch_size)))
cuvs::distance::pruned_top_1_nn_workspace_size<T>(handle, n_rows_, row_batch_size_)))
{
// The input rows do not change between iterations, so cache their filter norms once.
pruned_nn::squared_norms(
Expand All @@ -160,11 +160,9 @@ class pruned_assignment {

/**
* 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,
bool order_next)
raft::device_vector_view<uint32_t, int64_t> labels)
{
pruned_nn::squared_norms(handle_,
centroids.data_handle(),
Expand All @@ -180,7 +178,6 @@ class pruned_assignment {
} else {
kernel_centroids = centroids.data_handle();
}
params_.next_row_order = order_next ? row_order_.data_handle() : nullptr;
cuvs::distance::pruned_top_1_nn<T>(handle_,
{labels.data_handle(), dist_.data_handle()},
X_.data_handle(),
Expand All @@ -191,20 +188,18 @@ class pruned_assignment {
n_rows_,
n_clusters_,
dim_,
params_,
row_batch_size_,
workspace_.data_handle(),
workspace_.size());
if (order_next) { params_.row_order = row_order_.data_handle(); }
}

private:
const raft::resources& handle_;
raft::device_matrix_view<const T, int64_t> X_;
int64_t n_rows_, dim_, n_clusters_, filter_dim_;
raft::device_vector<float, int64_t> row_norms_, centroid_norms_, dist_;
raft::device_vector<uint32_t, int64_t> row_order_;
raft::device_matrix<half, int64_t> filter_centroids_;
cuvs::distance::PrunedTop1nnParams params_;
int64_t row_batch_size_;
raft::device_vector<char, int64_t> workspace_;
};

Expand Down Expand Up @@ -272,9 +267,7 @@ void fit(const raft::resources& handle,

for (uint32_t iter = 0; iter < params.n_iters; iter++) {
if (pruned_assign.has_value() && iter > 0) {
// 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);
pruned_assign->assign(raft::make_const_mdspan(centroids), assigned.view());
} else {
assign();
}
Expand Down
25 changes: 10 additions & 15 deletions cpp/src/distance/detail/pruned_top_1_nn/prune.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,7 @@ __device__ __forceinline__ int64_t pack_survivor(uint32_t id, float distance)
* A warp per block and row, against one centroid batch: the row's first-filter survivors (partial
* distances over the first kPartialDim dims), the tail dims in dim tiles with a re-filter after
* each but the last, then the argmin, kept if closer than the row's current centroid. Block b
* handles row row_order[b] when given (heaviest rows first), and each row's first-filter survivors
* are added to `survivors`.
* handles row row_order[b], ordered using this tile's first-filter survivor counts.
*
* The survivor list stays in the row's region of `lists`, but for its first kWarpSize entries,
* copied to shared memory; the warp reads it a window of kWarpSize entries at a time, lane l
Expand All @@ -272,25 +271,23 @@ RAFT_KERNEL prune_kernel(const T* __restrict__ rows,
uint32_t dim,
first_filter_lists lists,
uint32_t* __restrict__ labels,
float* __restrict__ dist,
uint32_t* __restrict__ survivors)
float* __restrict__ dist)
{
__shared__ int64_t head[kWarpSize];
const uint32_t lane = threadIdx.x;
const uint32_t lanes_below = (1u << lane) - 1;
const int64_t row = row_order != nullptr ? row_order[blockIdx.x] : blockIdx.x;
const int64_t row = row_order[blockIdx.x];
uint32_t n_left = static_cast<uint32_t>(lists.count[row]);
// Nothing survived: nothing else to read or write
if (n_left == 0) { return; }
const T* row_values = rows + row * dim;
uint32_t best_index = labels[row];
uint32_t best_index = UINT32_MAX; // No improving candidate yet.
// The row's distance to its current centroid until the last tile's argmin
float best = dist[row];

int64_t* list = lists.packed + row * lists.row_stride;
if (lane < n_left) { head[lane] = list[lane]; }
__syncwarp();
if (lane == 0) { survivors[row] += n_left; }

// One pass per remaining dim tile; the last one ends with the argmin
uint32_t d = kPartialDim;
Expand Down Expand Up @@ -340,7 +337,7 @@ RAFT_KERNEL prune_kernel(const T* __restrict__ rows,
const int64_t entry = head[0];
const auto centroid_id = static_cast<uint32_t>(entry >> kSurvivorIdShift);
// The assigned centroid's full distance is already known.
if (centroid_offset + centroid_id == best_index) { return; }
if (centroid_offset + centroid_id == labels[row]) { return; }
float distance = __int_as_float(static_cast<int32_t>(entry));
const T* centroid = centroids + static_cast<int64_t>(centroid_id) * dim;
for (; d < dim; d += kDimTileSize) {
Expand Down Expand Up @@ -375,7 +372,7 @@ RAFT_KERNEL prune_kernel(const T* __restrict__ rows,
best_index = other_index;
}
}
if (lane == 0) {
if (lane == 0 && best_index != UINT32_MAX) {
labels[row] = best_index;
dist[row] = best;
}
Expand All @@ -384,8 +381,8 @@ RAFT_KERNEL prune_kernel(const T* __restrict__ rows,
/**
* The pruning kernel for n_rows rows against one centroid batch (its first centroid
* centroid_offset), after the fused first filter wrote `lists`. `labels` and `dist` hold each
* row's current centroid and distance, and get the closer survivor if there is one; `survivors`
* gets each row's first-filter survivors added. row_order (optional): the rows' order.
* row's current centroid and distance, and get the closer survivor if there is one.
* row_order: the rows' order.
*/
template <typename T>
void prune(const raft::resources& handle,
Expand All @@ -397,8 +394,7 @@ void prune(const raft::resources& handle,
int64_t dim,
first_filter_lists lists,
uint32_t* labels,
float* dist,
uint32_t* survivors)
float* dist)
{
RAFT_EXPECTS(dim > kPartialDim && (dim - kPartialDim) % kDimTileSize == 0,
"Pruned 1-NN needs a positive tail after %u dims in tiles of %u",
Expand All @@ -415,8 +411,7 @@ void prune(const raft::resources& handle,
static_cast<uint32_t>(dim),
lists,
labels,
dist,
survivors);
dist);
Comment on lines +404 to +416

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.

As far as I remember our previous discussion, the survivors distribution may be very long-tailed, so the kernel becomes very unbalanced if we process one block per whole list.
Could you try to refactor the kernel and its launch grid to make the work balanced? One idea that we sounded earlier:
0) Run a cumulative sum pass over number of survivors per vector to get the total number of survivors and the O(1) mapping from global survivor index to (query id, local survivor id)

  1. make a grid that runs exactly as many blocks as fits in the GPU
  2. within the kernel loop over a fixed number of survivors using their global ids (possibly loading more than one query within the loop).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This approach could save the runtime survivor list compaction, which can be deferred to a standalone reduction kernel. But there are still some caveats:

  • if we keep the progressive pruning mechanism this doesn't solve the imbalance issue, because each row might end up in different time
  • the pruning bound (distance to assigned centroid) is per-row, so if we anchor on centroids and fetch rows we'll fetch all the pruning bounds too; whether this increase in memory traffic matters need to be tested
  • size of a row batch, as chosen based on workspace size, is much larger than the centroid batch size. So currently L2 is mainly serving the smaller centroid batch which fits, if we change mapping we'd also resize the batches, which might affect the performance of the cuTile kernel, which was tuned on the original centroid batch size, and the effect of this also needs to be checked

}

} // namespace cuvs::distance::detail::pruned_nn
Loading
Loading