From 4d47c436b9778092a12080cc2179b48997006ea0 Mon Sep 17 00:00:00 2001 From: sherylll Date: Sun, 15 Nov 2026 19:07:13 -0800 Subject: [PATCH 1/4] Store both fused NN-descent products in the shared arena Write new/new and new/old distances after the final feature-loop barrier, then reuse the stored matrices during metric conversion and graph insertion. Both accumulator lifetimes end before the epilogues. SMEM capacity review covered sm_80, sm_86, sm_90, sm_100, sm_103, sm_107, and sm_120 from the architecture matrix, plus the CUDA-table capacity classes sm_70/75/87/89/110/12.x. Two 64x68 float distance matrices reuse the two 64x136 half vector tiles: both occupy 34,816 bytes. The SMEM residency ceiling is unchanged on these architectures; this is capacity analysis, not an architecture-wide register or performance validation. SMEM-only block ceilings stay at 2 on sm_70; 1 on sm_75; 4 on sm_80/87; 2 on sm_86/89/12.x; 6 on sm_90/100/103/110; and 6 on sm_107 (9 with its oversized 328-KiB opt-in). Thread/register limits may be lower. Measured on H100 (sm_90), CUDA 13.3.73, Falcon 1M x 1024 FP32 input with FP16 WMMA, graph degree 64/intermediate 128, 20 iterations, 16 OpenMP threads. Three cooled trials per configuration: Original unfused + bound 4 (485ffcfd2): join 3.805831s, build 6.712011s. Fused loops + bound 4 + arena: join 3.164726s, build 6.063527s. This reduces join time 16.8% and GNND build time 9.7%; join time is included in build time. Baselines came from separate three-trial batches with brief thermal flags, so these are indicative, not fixed-clock timings. Against the existing fused/bound-4 kernel, static spill stores/loads fall from 148/104 to 112/84 bytes; registers stay at 32/thread, shared memory at 35,344 bytes/block, and predicted residency at four blocks/SM. Validation of this identical kernel body: sm_90 compilation, memcheck and racecheck at both bounds 4/off on 256 Falcon rows for two iterations, and full Falcon sampled recall around 95% with no invalid sampled IDs. Other architectures were reviewed for SMEM capacity only. No V100 runtime or achieved-occupancy claim is made. --- cpp/src/neighbors/detail/nn_descent.cuh | 44 +++++++++++++------------ 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/cpp/src/neighbors/detail/nn_descent.cuh b/cpp/src/neighbors/detail/nn_descent.cuh index a710ee9951..68f8e6e143 100644 --- a/cpp/src/neighbors/detail/nn_descent.cuh +++ b/cpp/src/neighbors/detail/nn_descent.cuh @@ -900,15 +900,18 @@ __launch_bounds__(BLOCK_SIZE) constexpr int APAD = 8; constexpr int BPAD = 8; constexpr int TILE_COL_WIDTH = 128; - // One arena: the new and old vector tiles live in it during the pass over the features, and - // the distance block replaces them once that pass is done; their lifetimes never overlap. + // The vector tiles and the two distance blocks have disjoint lifetimes. + // Reuse the arena after the final feature-loop barrier to release both accumulators + // before metric conversion and graph insertion. constexpr size_t TILE_BYTES = sizeof(__half) * MAX_NUM_BI_SAMPLES * (TILE_COL_WIDTH + APAD); - constexpr size_t DIST_BYTES = sizeof(float) * MAX_NUM_BI_SAMPLES * SKEWED_MAX_NUM_BI_SAMPLES; + constexpr size_t DIST_BYTES = + align32::roundUp(sizeof(float) * MAX_NUM_BI_SAMPLES * SKEWED_MAX_NUM_BI_SAMPLES); static_assert(APAD == BPAD); - __shared__ __align__(32) unsigned char s_arena[std::max(2 * TILE_BYTES, DIST_BYTES)]; + __shared__ __align__(32) unsigned char s_arena[std::max(2 * TILE_BYTES, 2 * DIST_BYTES)]; auto s_nv = reinterpret_cast<__half(*)[TILE_COL_WIDTH + APAD]>(s_arena); // New vectors auto s_ov = reinterpret_cast<__half(*)[TILE_COL_WIDTH + BPAD]>(s_arena + TILE_BYTES); float* s_distances = reinterpret_cast(s_arena); + float* s_distances_old = reinterpret_cast(s_arena + DIST_BYTES); __shared__ int s_unique_counter[2]; if (threadIdx.x == 0) { @@ -1038,8 +1041,8 @@ __launch_bounds__(BLOCK_SIZE) __syncthreads(); } - // The tiles are dead from here on, so s_distances (which aliases them) can take the - // new x new block. + // The final loop barrier completed every tile read. Both products can now replace + // the tiles without keeping either accumulator live through the epilogues. if (warp_active) { wmma::store_matrix_sync( s_distances + warp_id_y * WMMA_M * SKEWED_MAX_NUM_BI_SAMPLES + warp_id_x * WMMA_N, @@ -1047,6 +1050,13 @@ __launch_bounds__(BLOCK_SIZE) SKEWED_MAX_NUM_BI_SAMPLES, wmma::mem_row_major); } + if (warp_active_2) { + wmma::store_matrix_sync( + s_distances_old + warp_id_y * WMMA_M * SKEWED_MAX_NUM_BI_SAMPLES + warp_id_x * WMMA_N, + c_no, + SKEWED_MAX_NUM_BI_SAMPLES, + wmma::mem_row_major); + } } __syncthreads(); @@ -1075,18 +1085,7 @@ __launch_bounds__(BLOCK_SIZE) __syncthreads(); - if (metric != cuvs::distance::DistanceType::BitwiseHamming) { - if (warp_active_2) { - wmma::store_matrix_sync( - s_distances + warp_id_y * WMMA_M * SKEWED_MAX_NUM_BI_SAMPLES + warp_id_x * WMMA_N, - c_no, - SKEWED_MAX_NUM_BI_SAMPLES, - wmma::mem_row_major); - } - __syncthreads(); - } - - calculate_metric(s_distances, + calculate_metric(s_distances_old, new_neighbors, list_new_size, old_neighbors, @@ -1108,11 +1107,14 @@ __launch_bounds__(BLOCK_SIZE) std::numeric_limits::max()}; if (idx_in_list < MAX_NUM_BI_SAMPLES) { auto temp_min_item = - get_min_item(s_list[idx_in_list], idx_in_list, old_neighbors, s_distances); + get_min_item(s_list[idx_in_list], idx_in_list, old_neighbors, s_distances_old); if (temp_min_item.dist() < min_elem.dist()) { min_elem = temp_min_item; } } else { - auto temp_min_item = get_min_item( - s_list[idx_in_list], idx_in_list - MAX_NUM_BI_SAMPLES, new_neighbors, s_distances, false); + auto temp_min_item = get_min_item(s_list[idx_in_list], + idx_in_list - MAX_NUM_BI_SAMPLES, + new_neighbors, + s_distances_old, + false); if (temp_min_item.dist() < min_elem.dist()) { min_elem = temp_min_item; } } From 2a2b684691bd38571c7bd284648bc71216882d23 Mon Sep 17 00:00:00 2001 From: sherylll Date: Mon, 16 Nov 2026 17:35:23 -0800 Subject: [PATCH 2/4] Simplify NN-descent tile staging and phase synchronization --- cpp/src/neighbors/detail/nn_descent.cuh | 7 ------- 1 file changed, 7 deletions(-) diff --git a/cpp/src/neighbors/detail/nn_descent.cuh b/cpp/src/neighbors/detail/nn_descent.cuh index 68f8e6e143..c7d531263a 100644 --- a/cpp/src/neighbors/detail/nn_descent.cuh +++ b/cpp/src/neighbors/detail/nn_descent.cuh @@ -990,7 +990,6 @@ __launch_bounds__(BLOCK_SIZE) int num_load_elems = (step == raft::ceildiv(data_dim, TILE_COL_WIDTH) - 1) ? data_dim - step * TILE_COL_WIDTH : TILE_COL_WIDTH; -#pragma unroll for (int i = 0; i < MAX_NUM_BI_SAMPLES / num_warps; i++) { int idx = i * num_warps + warp_id; if (idx < list_new_size) { @@ -1003,10 +1002,6 @@ __launch_bounds__(BLOCK_SIZE) TILE_COL_WIDTH, lane_id); } - } -#pragma unroll - for (int i = 0; i < MAX_NUM_BI_SAMPLES / num_warps; i++) { - int idx = i * num_warps + warp_id; if (idx < list_old_size) { size_t neighbor_id = old_neighbors[idx]; size_t idx_in_data = neighbor_id * data_dim; @@ -1083,8 +1078,6 @@ __launch_bounds__(BLOCK_SIZE) if (!has_old) return; - __syncthreads(); - calculate_metric(s_distances_old, new_neighbors, list_new_size, From 448e2f698c120cc872f762b3de69f4370a52d994 Mon Sep 17 00:00:00 2001 From: sherylll Date: Mon, 16 Nov 2026 17:59:20 -0800 Subject: [PATCH 3/4] Use scalar FP16 loads in NN-descent vector staging --- cpp/src/neighbors/detail/nn_descent.cuh | 42 +++++-------------------- 1 file changed, 7 insertions(+), 35 deletions(-) diff --git a/cpp/src/neighbors/detail/nn_descent.cuh b/cpp/src/neighbors/detail/nn_descent.cuh index c7d531263a..9bcaa3ff85 100644 --- a/cpp/src/neighbors/detail/nn_descent.cuh +++ b/cpp/src/neighbors/detail/nn_descent.cuh @@ -226,41 +226,13 @@ __device__ __forceinline__ void load_vec(Data_t* vec_buffer, const int padding_dims, const int lane_id) { - if constexpr (std::is_same_v or std::is_same_v or - std::is_same_v) { - constexpr int num_load_elems_per_warp = raft::warp_size(); - for (int step = 0; step < raft::ceildiv(padding_dims, num_load_elems_per_warp); step++) { - int idx = step * num_load_elems_per_warp + lane_id; - if (idx < load_dims) { - vec_buffer[idx] = d_vec[idx]; - } else if (idx < padding_dims) { - vec_buffer[idx] = 0.0f; - } - } - } - if constexpr (std::is_same_v) { - if ((size_t)d_vec % sizeof(float2) == 0 && (size_t)vec_buffer % sizeof(float2) == 0 && - load_dims % 4 == 0 && padding_dims % 4 == 0) { - constexpr int num_load_elems_per_warp = raft::warp_size() * 4; -#pragma unroll - for (int step = 0; step < raft::ceildiv(padding_dims, num_load_elems_per_warp); step++) { - int idx_in_vec = step * num_load_elems_per_warp + lane_id * 4; - if (idx_in_vec + 4 <= load_dims) { - *(float2*)(vec_buffer + idx_in_vec) = *(float2*)(d_vec + idx_in_vec); - } else if (idx_in_vec + 4 <= padding_dims) { - *(float2*)(vec_buffer + idx_in_vec) = float2({0.0f, 0.0f}); - } - } - } else { - constexpr int num_load_elems_per_warp = raft::warp_size(); - for (int step = 0; step < raft::ceildiv(padding_dims, num_load_elems_per_warp); step++) { - int idx = step * num_load_elems_per_warp + lane_id; - if (idx < load_dims) { - vec_buffer[idx] = d_vec[idx]; - } else if (idx < padding_dims) { - vec_buffer[idx] = 0.0f; - } - } + constexpr int num_load_elems_per_warp = raft::warp_size(); + for (int step = 0; step < raft::ceildiv(padding_dims, num_load_elems_per_warp); step++) { + int idx = step * num_load_elems_per_warp + lane_id; + if (idx < load_dims) { + vec_buffer[idx] = d_vec[idx]; + } else if (idx < padding_dims) { + vec_buffer[idx] = 0.0f; } } } From 6627f8eb0545e5fb0a73719559ad6c0de20f771e Mon Sep 17 00:00:00 2001 From: sherylll Date: Mon, 16 Nov 2026 19:58:43 -0800 Subject: [PATCH 4/4] Vectorize aligned NN-descent WMMA tile loads --- cpp/src/neighbors/detail/nn_descent.cuh | 89 +++++++++++++------------ 1 file changed, 47 insertions(+), 42 deletions(-) diff --git a/cpp/src/neighbors/detail/nn_descent.cuh b/cpp/src/neighbors/detail/nn_descent.cuh index 9bcaa3ff85..fd389f9dff 100644 --- a/cpp/src/neighbors/detail/nn_descent.cuh +++ b/cpp/src/neighbors/detail/nn_descent.cuh @@ -219,41 +219,40 @@ constexpr int WMMA_M = 16; constexpr int WMMA_N = 16; constexpr int WMMA_K = 16; -template -__device__ __forceinline__ void load_vec(Data_t* vec_buffer, - const Data_t* d_vec, - const int load_dims, - const int padding_dims, - const int lane_id) -{ - constexpr int num_load_elems_per_warp = raft::warp_size(); - for (int step = 0; step < raft::ceildiv(padding_dims, num_load_elems_per_warp); step++) { - int idx = step * num_load_elems_per_warp + lane_id; - if (idx < load_dims) { - vec_buffer[idx] = d_vec[idx]; - } else if (idx < padding_dims) { - vec_buffer[idx] = 0.0f; - } - } -} - -/** Converting load: loads Data_t from global memory into __half shared memory buffer. */ -template - requires(!std::is_same_v) -__device__ __forceinline__ void load_vec(__half* vec_buffer, - const Data_t* d_vec, +// Vectorized float/half calls rely on the alignment and row-stride checks in local_join(). +template +__device__ __forceinline__ void load_vec(DstT* vec_buffer, + const SrcT* d_vec, const int load_dims, const int padding_dims, const int lane_id) { - constexpr int num_load_elems_per_warp = raft::warp_size(); - const __half half_0 = __float2half(0.0f); - for (int step = 0; step < raft::ceildiv(padding_dims, num_load_elems_per_warp); step++) { - int idx = step * num_load_elems_per_warp + lane_id; - if (idx < load_dims) { - vec_buffer[idx] = d_vec[idx]; - } else if (idx < padding_dims) { - vec_buffer[idx] = half_0; + constexpr bool convert_pair = + Vectorized && std::is_same_v && std::is_same_v; + constexpr bool copy_halves = + Vectorized && std::is_same_v && std::is_same_v; + constexpr int width = convert_pair ? 2 : (copy_halves ? 4 : 1); + constexpr int elems_per_warp = raft::warp_size() * width; + for (int step = 0; step < raft::ceildiv(padding_dims, elems_per_warp); step++) { + int idx = step * elems_per_warp + lane_id * width; + if (idx + width <= load_dims) { + if constexpr (convert_pair) { + float2 value = *reinterpret_cast(d_vec + idx); + *reinterpret_cast<__half2*>(vec_buffer + idx) = __floats2half2_rn(value.x, value.y); + } else if constexpr (copy_halves) { + *reinterpret_cast(vec_buffer + idx) = + *reinterpret_cast(d_vec + idx); + } else { + vec_buffer[idx] = d_vec[idx]; + } + } else if (idx + width <= padding_dims) { + if constexpr (convert_pair) { + *reinterpret_cast<__half2*>(vec_buffer + idx) = __float2half2_rn(0.0f); + } else if constexpr (copy_halves) { + *reinterpret_cast(vec_buffer + idx) = float2{0.0f, 0.0f}; + } else { + vec_buffer[idx] = DstT{0}; + } } } } @@ -968,20 +967,20 @@ __launch_bounds__(BLOCK_SIZE) size_t neighbor_id = new_neighbors[idx]; size_t idx_in_data = neighbor_id * data_dim; // converted to fp16 on-the-fly while loading - load_vec(s_nv[idx], - data + idx_in_data + step * TILE_COL_WIDTH, - num_load_elems, - TILE_COL_WIDTH, - lane_id); + load_vec(s_nv[idx], + data + idx_in_data + step * TILE_COL_WIDTH, + num_load_elems, + TILE_COL_WIDTH, + lane_id); } if (idx < list_old_size) { size_t neighbor_id = old_neighbors[idx]; size_t idx_in_data = neighbor_id * data_dim; - load_vec(s_ov[idx], - data + idx_in_data + step * TILE_COL_WIDTH, - num_load_elems, - TILE_COL_WIDTH, - lane_id); + load_vec(s_ov[idx], + data + idx_in_data + step * TILE_COL_WIDTH, + num_load_elems, + TILE_COL_WIDTH, + lane_id); } } __syncthreads(); @@ -2387,7 +2386,13 @@ void GNND::local_join(cudaStream_t stream, DistEpilogue_t dist_ build_config_.metric == cuvs::distance::DistanceType::L1; auto launch_kernel = [&](auto* typed_ptr) { - if (use_simt) { + using launch_data_t = std::remove_cv_t>; + bool launch_simt = use_simt; + if constexpr (std::is_same_v || std::is_same_v) { + launch_simt = launch_simt || reinterpret_cast(typed_ptr) % sizeof(float2) != 0 || + (static_cast(ndim_) * sizeof(launch_data_t)) % sizeof(float2) != 0; + } + if (launch_simt) { local_join_kernel_simt<<>>(graph_.h_graph_new.data_handle(), h_rev_graph_new_.data_handle(), d_list_sizes_new_.data_handle(),