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
Prune survivor lists in place, out of shared memory
The pruning kernel compacts each row's survivors in its region of the
first filter's lists, with only the first 32 entries in shared memory,
so centroid batches grow to 2048 without lowering occupancy.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
  • Loading branch information
sherylll and claude committed Nov 2, 2026
commit 4bf9ccc8369f2928a19391a0aae72585d62cb337
232 changes: 130 additions & 102 deletions cpp/src/distance/detail/pruned_top_1_nn/prune.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ namespace cuvs::distance::detail::pruned_nn {

// Dims between the survivors' re-filters, and d' of the first filter
constexpr uint32_t kDimTileSize = 256;
// Centroids per batch: a row's survivor list of one batch lives in the pruning kernel's shared
// memory
constexpr uint32_t kCentroidBatchSize = 1024;
// Centroids per batch: one fused filter and pruning launch per row batch and centroid batch
constexpr uint32_t kCentroidBatchSize = 2048;
// Rows per batch: bounds the first filter's survivor lists (row batch x centroid batch slots)
constexpr int64_t kRowBatchSize = 128 * 1024;
// Survivors whose loads are batched over a dim tile
Expand All @@ -33,8 +32,6 @@ constexpr int kSurvivorIdShift = 32;
constexpr uint32_t kWarpSize = 32;
constexpr uint32_t kFullMask = 0xffffffff;

using survivor_id_t = std::conditional_t<(kCentroidBatchSize <= 65536), uint16_t, uint32_t>;

/**
* 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
Expand All @@ -57,8 +54,8 @@ inline auto adsampling_ratios(uint32_t dim, float eps0 = 1.5f) -> std::vector<fl
}

/**
* Sums N independent per-lane values across the warp (valid in the first lane), the N shuffles of
* each step issued together so they overlap.
* Sums N independent per-lane values across the warp (xor shuffles: every lane ends with the N
* sums), the N shuffles of each step issued together so they overlap.
*/
template <uint32_t N, typename T>
__device__ __forceinline__ void warp_reduce_sum_n(T* values)
Expand All @@ -68,7 +65,7 @@ __device__ __forceinline__ void warp_reduce_sum_n(T* values)
for (int offset = kWarpSize / 2; offset > 0; offset >>= 1) {
#pragma unroll
for (uint32_t i = 0; i < N; i++) {
shifted[i] = __shfl_down_sync(kFullMask, values[i], offset);
shifted[i] = __shfl_xor_sync(kFullMask, values[i], offset);
}
asm volatile("" ::: "memory");
#pragma unroll
Expand Down Expand Up @@ -145,42 +142,47 @@ void distance_to_assigned(const T* rows,

/**
* The first filter's survivor lists of one row batch x centroid batch (see launch_fused_filter):
* per (row, centroid tile) a segment of tile_n slots in `packed`, its count in `seg_count`.
* per (row, centroid tile) a segment of tile_n slots in `packed`, its count in `seg_count`. The
* pruning kernel compacts a row's survivors in place in the row's segments.
*/
struct first_filter_lists {
const int64_t* packed;
int64_t* packed;
const int32_t* seg_count;
uint32_t n_col_tiles;
uint32_t tile_n;
};

/**
* Distances over one dim tile between the row (`row_tile`, lane l holding dims l + 32 j) and N of
* its survivors, added to `distances` by lane 0. The N centroids' loads are issued back to back.
* Distances over one dim tile between the row (`row_tile`, lane l holding dims l + 32 j) and the N
* survivors at window positions [first, first + N), their ids held one per lane in `ids`; the lane
* holding each survivor adds its distance to `distance`. The N centroids' loads are issued back to
* back.
*/
template <uint32_t Dims, uint32_t N, typename T>
__device__ __forceinline__ void add_tile_distances(const float (&row_tile)[Dims / kWarpSize],
const T* __restrict__ centroids,
uint32_t stride,
const survivor_id_t* __restrict__ ids,
float* __restrict__ distances)
uint32_t ids,
uint32_t first,
float& distance)
{
const uint32_t lane = threadIdx.x % kWarpSize;
float partial[N];
#pragma unroll
for (uint32_t i = 0; i < N; i++) {
const T* centroid = centroids + static_cast<int64_t>(ids[i]) * stride;
partial[i] = 0;
const T* centroid =
centroids + static_cast<int64_t>(__shfl_sync(kFullMask, ids, first + i)) * stride;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we use raft::shfl ?

partial[i] = 0;
#pragma unroll
for (uint32_t j = 0; j < Dims / kWarpSize; j++) {
const float diff = row_tile[j] - static_cast<float>(centroid[lane + j * kWarpSize]);
partial[i] = fmaf(diff, diff, partial[i]);
}
}
warp_reduce_sum_n<N>(partial);
if (lane == 0) {
for (uint32_t i = 0; i < N; i++) {
distances[i] += partial[i];
}
#pragma unroll
for (uint32_t i = 0; i < N; i++) {
if (lane == first + i) { distance += partial[i]; }
}
}

Expand All @@ -192,119 +194,145 @@ __device__ __forceinline__ void for_each_halving(F&& f)
if constexpr (N > 1) { for_each_halving<N / 2>(f); }
}

/** A row's first-filter survivors, in centroid order, into the warp's list; returns how many. */
__device__ inline uint32_t load_survivors(first_filter_lists lists,
int64_t row,
survivor_id_t* __restrict__ ids,
float* __restrict__ distances)
__device__ __forceinline__ int64_t pack_survivor(uint32_t id, float distance)
{
const uint32_t lane = threadIdx.x % kWarpSize;
const int64_t* segments = lists.packed + row * lists.n_col_tiles * lists.tile_n;
const int32_t* counts = lists.seg_count + row * lists.n_col_tiles;
uint32_t n = 0;
for (uint32_t t = 0; t < lists.n_col_tiles; t++) {
return (static_cast<int64_t>(id) << kSurvivorIdShift) |
static_cast<uint32_t>(__float_as_int(distance));
}

/**
* A row's first-filter survivors, from its segments (`list`, the row's region), compacted to
* positions [0, n) in centroid order and written with put(position, entry). In place: a chunk's
* entries are all read before any is written, and a write never passes the read position.
* Returns n.
*/
template <typename Put>
__device__ __forceinline__ uint32_t compact_survivors(const int64_t* list,
const int32_t* __restrict__ counts,
uint32_t n_col_tiles,
uint32_t tile_n,
Put&& put)
{
const uint32_t lane = threadIdx.x % kWarpSize;
uint32_t n = 0;
for (uint32_t t = 0; t < n_col_tiles; t++) {
const auto count = static_cast<uint32_t>(counts[t]);
for (uint32_t j = lane; j < count; j += kWarpSize) {
const int64_t value = segments[t * lists.tile_n + j];
ids[n + j] = static_cast<survivor_id_t>(value >> kSurvivorIdShift);
distances[n + j] = __int_as_float(static_cast<int32_t>(value));
for (uint32_t j0 = 0; j0 < count; j0 += kWarpSize) {
const uint32_t j = j0 + lane;
int64_t value = 0;
if (j < count) { value = list[t * tile_n + j]; }
__syncwarp();
if (j < count && (n + j < kWarpSize || n != t * tile_n)) { put(n + j, value); }
}
n += count;
}
__syncwarp();
return n;
}

/** Keeps the n survivors under `threshold`, compacting the list in place; returns how many. */
__device__ inline uint32_t refilter(uint32_t n,
float threshold,
survivor_id_t* __restrict__ ids,
float* __restrict__ distances)
{
const uint32_t lane = threadIdx.x % kWarpSize;
const uint32_t lanes_below = (1u << lane) - 1;
uint32_t kept = 0;
for (uint32_t base = 0; base < n; base += kWarpSize) {
const uint32_t i = base + lane;
uint32_t id = 0;
float distance = 0.f;
bool keep = false;
if (i < n) {
id = ids[i];
distance = distances[i];
keep = distance < threshold;
}
const uint32_t ballot = __ballot_sync(kFullMask, keep);
__syncwarp(); // all lanes have read their entry before the compaction writes
if (keep) {
const uint32_t slot = kept + __popc(ballot & lanes_below);
ids[slot] = static_cast<survivor_id_t>(id);
distances[slot] = distance;
}
kept += __popc(ballot);
}
return kept;
}

/**
* A warp per block and row, against one centroid batch: the row's first-filter survivors (partial
* distances over the first partial_dim 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`.
*
* The survivor list stays in the row's region of `lists`, but for its first kWarpSize entries,
* kept in shared memory; the warp reads it a window of kWarpSize entries at a time, lane l holding
* entry l, and each re-filter compacts it in place. The launch bounds leave the compiler up to 64
* registers, for more centroid loads in flight.
*/
template <typename T>
RAFT_KERNEL prune_kernel(const T* __restrict__ rows,
const uint32_t* __restrict__ row_order,
const T* __restrict__ centroids,
uint32_t centroid_offset,
uint32_t dim,
uint32_t partial_dim,
const float* __restrict__ ratios,
first_filter_lists lists,
uint32_t* __restrict__ labels,
float* __restrict__ dist,
uint32_t* __restrict__ survivors)
RAFT_KERNEL __launch_bounds__(kWarpSize, 32) prune_kernel(const T* __restrict__ rows,
const uint32_t* __restrict__ row_order,
const T* __restrict__ centroids,
uint32_t centroid_offset,
uint32_t dim,
uint32_t partial_dim,
const float* __restrict__ ratios,
first_filter_lists lists,
uint32_t* __restrict__ labels,
float* __restrict__ dist,
uint32_t* __restrict__ survivors)
{
__shared__ survivor_id_t ids[kCentroidBatchSize];
__shared__ float distances[kCentroidBatchSize];
const uint32_t lane = threadIdx.x;
const int64_t row = row_order != nullptr ? row_order[blockIdx.x] : blockIdx.x;
const T* row_values = rows + row * dim;
uint32_t best_index = labels[row];
float best = dist[row];
const float bound = best;
__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 T* row_values = rows + row * dim;
uint32_t best_index = labels[row];
float best = dist[row];
const float bound = best;

uint32_t n_left = load_survivors(lists, row, ids, distances);
int64_t* list = lists.packed + row * lists.n_col_tiles * lists.tile_n;
auto put = [&](uint32_t position, int64_t value) {
if (position < kWarpSize) {
head[position] = value;
} else {
list[position] = value;
}
};
uint32_t n_left = compact_survivors(
list, lists.seg_count + row * lists.n_col_tiles, lists.n_col_tiles, lists.tile_n, put);
if (lane == 0) { survivors[row] += n_left; }
if (n_left == 0 || (n_left == 1 && centroid_offset + ids[0] == best_index)) { return; }
if (n_left == 0 ||
(n_left == 1 &&
centroid_offset + static_cast<uint32_t>(head[0] >> kSurvivorIdShift) == best_index)) {
return;
}

for (uint32_t d = partial_dim; d < dim && n_left; d += kDimTileSize) {
const bool last = d + kDimTileSize >= dim;
float row_tile[kDimTileSize / kWarpSize];
#pragma unroll
for (uint32_t j = 0; j < kDimTileSize / kWarpSize; j++) {
row_tile[j] = static_cast<float>(row_values[d + lane + j * kWarpSize]);
}
// Survivor tiles of kSurvivorTileSize, then halving sizes down to 1 for the ones left
uint32_t i = 0;
for_each_halving<kSurvivorTileSize>([&](auto tile) {
constexpr uint32_t kTile = decltype(tile)::value;
for (; i + kTile <= n_left; i += kTile) {
add_tile_distances<kDimTileSize, kTile>(
row_tile, centroids + d, dim, ids + i, distances + i);
}
});
__syncwarp(); // lane 0's distance updates are visible to the warp
// After the last tile the distances are complete: the argmin compares them with the best
if (d + kDimTileSize < dim) {
n_left = refilter(n_left, bound * ratios[d + kDimTileSize], ids, distances);
const float threshold = last ? 0.f : bound * ratios[d + kDimTileSize];
uint32_t kept = 0;
// The next window is loaded while the current one is computed
int64_t next = kWarpSize + lane < n_left ? list[kWarpSize + lane] : 0;
for (uint32_t base = 0; base < n_left; base += kWarpSize) {
const uint32_t w = min(kWarpSize, n_left - base);
const int64_t entry = base == 0 ? head[lane] : next;
if (base > 0 && base + kWarpSize + lane < n_left) { next = list[base + kWarpSize + lane]; }
const auto ids = static_cast<uint32_t>(entry >> kSurvivorIdShift);
float distance = lane < w ? __int_as_float(static_cast<int32_t>(entry)) : 0.f;
// Survivor tiles of kSurvivorTileSize, then halving sizes down to 1 for the ones left
uint32_t i = 0;
for_each_halving<kSurvivorTileSize>([&](auto tile) {
constexpr uint32_t kTile = decltype(tile)::value;
for (; i + kTile <= w; i += kTile) {
add_tile_distances<kDimTileSize, kTile>(row_tile, centroids + d, dim, ids, i, distance);
}
});
if (!last) {
// The kept survivors go below this window's end, never into a later window
const bool keep = lane < w && distance < threshold;
const uint32_t ballot = __ballot_sync(kFullMask, keep);
if (keep) { put(kept + __popc(ballot & lanes_below), pack_survivor(ids, distance)); }
kept += __popc(ballot);
} else if (lane < w && distance < best) {
best = distance;
best_index = centroid_offset + ids;
}
}
__syncwarp(); // the compacted list is visible to the warp
n_left = last ? 0 : kept;
}

for (uint32_t i = lane; i < n_left; i += kWarpSize) {
if (distances[i] < best) {
best = distances[i];
best_index = centroid_offset + ids[i];
// No tail dims (partial_dim = dim): the first-filter distances are full, the argmin is over the
// list itself
for (uint32_t base = 0; base < n_left; base += kWarpSize) {
const uint32_t position = base + lane;
if (position < n_left) {
const int64_t entry = base == 0 ? head[lane] : list[position];
const float distance = __int_as_float(static_cast<int32_t>(entry));
if (distance < best) {
best = distance;
best_index = centroid_offset + static_cast<uint32_t>(entry >> kSurvivorIdShift);
}
}
}
for (int offset = kWarpSize / 2; offset > 0; offset >>= 1) {
Expand Down
2 changes: 1 addition & 1 deletion cpp/tests/cluster/kmeans_superkmeans.cu
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ TEST(KmeansSuperKMeans, Pruned)
// dim tiles (one re-filter)
constexpr int64_t n_rows = 300000;
constexpr int64_t dim = 768;
constexpr int64_t n_clusters = 1100;
constexpr int64_t n_clusters = 2200;
constexpr uint32_t n_iters = 8;
constexpr uint64_t seed = 42;
raft::handle_t handle;
Expand Down