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
Compute the pruning ratios from eps0 in the kernel, not from a device…
… array

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
  • Loading branch information
sherylll and claude committed Nov 2, 2026
commit 6a2ae57b1b6ed068b2e4929072dca26fa25927ea
16 changes: 4 additions & 12 deletions cpp/src/cluster/detail/superkmeans.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,8 @@ void copy_first_columns(const raft::resources& handle,

/**
* The pruned assignment (cuvs::distance::pruned_top_1_nn) and what it keeps across iterations: the
* rows' norms and FP16 copy, the pruning ratios, the row order, and the workspace. The centroids
* the 1-NN reads are T: for half rows, a half copy of the float centroids, which is also the
* filter's.
* rows' norms and FP16 copy, the row order, and the workspace. The centroids the 1-NN reads are
* T: for half rows, a half copy of the float centroids, which is also the filter's.
*/
template <typename T>
class pruned_assignment {
Expand All @@ -157,7 +156,6 @@ class pruned_assignment {
dim_(X.extent(1)),
n_clusters_(n_clusters),
filter_dim_(std::is_same_v<T, half> ? dim_ : pruned_nn::kDimTileSize),
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_)),
Expand All @@ -168,11 +166,7 @@ class pruned_assignment {
workspace_(raft::make_device_vector<char, int64_t>(
handle, cuvs::distance::pruned_top_1_nn_workspace_size(handle, n_rows_)))
{
auto stream = raft::resource::get_cuda_stream(handle).get();
const auto ratios = pruned_nn::adsampling_ratios(dim_);
params_.ratios = ratios_.data_handle();
params_.first_ratio = ratios[pruned_nn::kDimTileSize];
raft::copy(ratios_.data_handle(), ratios.data(), ratios.size(), stream);
auto stream = raft::resource::get_cuda_stream(handle).get();
if constexpr (std::is_same_v<T, half>) {
pruned_nn::squared_norms(
X.data_handle(), n_rows_, dim_, pruned_nn::kDimTileSize, row_norms_.data_handle(), stream);
Expand All @@ -185,8 +179,6 @@ class pruned_assignment {
filter_rows_.data_handle(),
row_norms_.data_handle());
}
// The host ratios are released when the copy is done
raft::resource::sync_stream(handle);
}

/**
Expand Down Expand Up @@ -233,7 +225,7 @@ class pruned_assignment {
raft::device_matrix_view<const T, int64_t> X_;
int64_t n_rows_, dim_, n_clusters_, filter_dim_;
cuvs::distance::PrunedTop1nnParams params_;
raft::device_vector<float, int64_t> ratios_, row_norms_, centroid_norms_, dist_;
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_rows_, filter_centroids_;
raft::device_vector<char, int64_t> workspace_;
Expand Down
38 changes: 16 additions & 22 deletions cpp/src/distance/detail/pruned_top_1_nn/prune.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include <cmath>
#include <cstdint>
#include <type_traits>
#include <vector>

namespace cuvs::distance::detail::pruned_nn {

Expand All @@ -33,24 +32,16 @@ constexpr uint32_t kWarpSize = 32;
constexpr uint32_t kFullMask = 0xffffffff;

/**
* ADSampling's pruning ratios: ratios[i] = (i / d) * (1 + eps0 / sqrt(i))^2 for i dims seen,
* i = 0..d (1 at both ends). A partial distance over the first i dims is compared against
* ratios[i] times the row's current best distance. The ratios assume every dim carries the same
* share of a distance, which ADSampling ensures with a random rotation of the data; without one
* they are a heuristic.
* ADSampling's pruning ratio for a partial distance over the first i of dim dims:
* (i / dim) * (1 + eps0 / sqrt(i))^2. A partial distance over i dims is compared against the ratio
* times the row's current best distance. The ratios assume every dim carries the same share of a
* distance, which ADSampling ensures with a random rotation of the data; without one they are a
* heuristic.
*/
inline auto adsampling_ratios(uint32_t dim, float eps0 = 1.5f) -> std::vector<float>
__host__ __device__ inline float adsampling_ratio(uint32_t i, uint32_t dim, float eps0)
{
std::vector<float> ratios(dim + 1);
for (uint32_t i = 0; i <= dim; i++) {
if (i == 0 || i == dim) {
ratios[i] = 1.0f;
continue;
}
const double f = 1.0 + eps0 / std::sqrt(static_cast<double>(i));
ratios[i] = static_cast<float>(static_cast<double>(i) / dim * f * f);
}
return ratios;
const float f = 1.0f + eps0 / sqrtf(static_cast<float>(i));
return static_cast<float>(i) / static_cast<float>(dim) * f * f;
}

/**
Expand Down Expand Up @@ -221,7 +212,7 @@ RAFT_KERNEL __launch_bounds__(kWarpSize, 32) prune_kernel(const T* __restrict__
uint32_t centroid_offset,
uint32_t dim,
uint32_t partial_dim,
const float* __restrict__ ratios,
float eps0,
first_filter_lists lists,
uint32_t* __restrict__ labels,
float* __restrict__ dist,
Expand Down Expand Up @@ -252,6 +243,8 @@ RAFT_KERNEL __launch_bounds__(kWarpSize, 32) prune_kernel(const T* __restrict__
// One pass per dim tile (partial_dim < dim); the last one ends with the argmin
for (uint32_t d = partial_dim; n_left; d += kDimTileSize) {
const bool last = d + kDimTileSize >= dim;
// The re-filter's ratio
const float ratio = last ? 0.0f : adsampling_ratio(d + kDimTileSize, dim, eps0);
float row_tile[kDimTileSize / kWarpSize];
#pragma unroll
for (uint32_t j = 0; j < kDimTileSize / kWarpSize; j++) {
Expand All @@ -273,7 +266,7 @@ RAFT_KERNEL __launch_bounds__(kWarpSize, 32) prune_kernel(const T* __restrict__
});
if (!last) {
// The kept survivors go below this window's end, never into a later window
const bool keep = lane < w && distance < best * ratios[d + kDimTileSize];
const bool keep = lane < w && distance < best * ratio;
const uint32_t ballot = __ballot_sync(kFullMask, keep);
if (keep) {
const uint32_t slot = kept + __popc(ballot & lanes_below);
Expand Down Expand Up @@ -307,7 +300,8 @@ RAFT_KERNEL __launch_bounds__(kWarpSize, 32) prune_kernel(const T* __restrict__
* 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.
* gets each row's first-filter survivors added. row_order (optional): the rows' order. eps0:
* ADSampling's margin, for the re-filters' ratios.
*/
template <typename T>
void prune(const T* rows,
Expand All @@ -317,7 +311,7 @@ void prune(const T* rows,
uint32_t centroid_offset,
int64_t dim,
uint32_t partial_dim,
const float* ratios,
float eps0,
first_filter_lists lists,
uint32_t* labels,
float* dist,
Expand All @@ -333,7 +327,7 @@ void prune(const T* rows,
centroid_offset,
static_cast<uint32_t>(dim),
partial_dim,
ratios,
eps0,
lists,
labels,
dist,
Expand Down
21 changes: 9 additions & 12 deletions cpp/src/distance/pruned_top_1_nn.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,9 @@ namespace cuvs::distance {

/** The pruned 1-NN's bounds, and the order its pruning kernel takes the rows in. */
struct PrunedTop1nnParams {
/** [k + 1] on device: ratios[i] scales a row's bound after i dims (see
* detail::pruned_nn::adsampling_ratios). */
const float* ratios = nullptr;
/** ratios[detail::pruned_nn::kDimTileSize] on host: the first filter's. */
float first_ratio = 1.0f;
/** ADSampling's margin: a row's bound after i dims is scaled by
* detail::pruned_nn::adsampling_ratio(i, k, eps0). */
float eps0 = 1.5f;
/** [m], optional: each row batch's rows in the order to prune them, heaviest first. */
const uint32_t* row_order = nullptr;
/** [m], optional: the order for the next call, from this call's first-filter survivors. May be
Expand Down Expand Up @@ -183,10 +181,10 @@ inline std::size_t pruned_top_1_nn_workspace_size(raft::resources const& handle,
/**
* Updates each row of x to its nearest row of y in L2, starting from its current one. Not exact:
* from the distance to the current neighbor (the bound), a fused partial GEMM over the first
* detail::pruned_nn::kDimTileSize dims (FP16) keeps the candidates under the bound times
* params.first_ratio; the pruning kernel adds the candidates' tail dims a tile at a time, keeping
* those under the bound times params.ratios[dims so far], and takes the closest left if it beats
* the current neighbor. A neighbor pruned away early can be missed.
* detail::pruned_nn::kDimTileSize dims (FP16) keeps the candidates under the bound times its
* ratio; the pruning kernel adds the candidates' tail dims a tile at a time, keeping those under
* the bound times the ratio for the dims so far (see PrunedTop1nnParams::eps0), and takes the
* closest left if it beats the current neighbor. A neighbor pruned away early can be missed.
*
* Rows go in batches of kRowBatchSize against batches of kCentroidBatchSize rows of y. The row
* batches round-robin over the main stream and, with a stream pool on the handle, its first stream,
Expand Down Expand Up @@ -228,7 +226,6 @@ void pruned_top_1_nn(raft::resources const& handle,
"Pruned 1-NN needs the fused filter's cuTile kernel and k a multiple of %u above it",
kDimTileSize);
RAFT_EXPECTS(m > 0 && n > 0, "Pruned 1-NN needs rows in x and y");
RAFT_EXPECTS(params.ratios != nullptr, "Pruned 1-NN needs the pruning ratios");
RAFT_EXPECTS(kHalf || (x_fp16 != nullptr && y_fp16 != nullptr),
"Pruned 1-NN on float needs FP16 copies of x's and y's first dims");
const workspace_layout layout(handle, m);
Expand Down Expand Up @@ -276,7 +273,7 @@ void pruned_top_1_nn(raft::resources const& handle,
xn + i,
yn + j,
row_dist,
params.first_ratio,
adsampling_ratio(kDimTileSize, static_cast<uint32_t>(k), params.eps0),
static_cast<int>(kDimTileSize),
packed,
count,
Expand All @@ -289,7 +286,7 @@ void pruned_top_1_nn(raft::resources const& handle,
static_cast<uint32_t>(j),
k,
kDimTileSize,
params.ratios,
params.eps0,
first_filter_lists{packed, count, kCentroidBatchSize},
row_labels,
row_dist,
Expand Down