Skip to content
Closed
Changes from all commits
Commits
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
162 changes: 67 additions & 95 deletions cpp/src/neighbors/detail/nn_descent.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -219,73 +219,44 @@ constexpr int WMMA_M = 16;
constexpr int WMMA_N = 16;
constexpr int WMMA_K = 16;

template <typename Data_t>
__device__ __forceinline__ void load_vec(Data_t* vec_buffer,
const Data_t* d_vec,
// Vectorized float/half calls rely on the alignment and row-stride checks in local_join().
template <bool Vectorized = false, typename DstT, typename SrcT>
__device__ __forceinline__ void load_vec(DstT* vec_buffer,
const SrcT* d_vec,
const int load_dims,
const int padding_dims,
const int lane_id)
{
if constexpr (std::is_same_v<Data_t, float> or std::is_same_v<Data_t, uint8_t> or
std::is_same_v<Data_t, int8_t>) {
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) {
constexpr bool convert_pair =
Vectorized && std::is_same_v<DstT, __half> && std::is_same_v<SrcT, float>;
constexpr bool copy_halves =
Vectorized && std::is_same_v<DstT, __half> && std::is_same_v<SrcT, __half>;
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<const float2*>(d_vec + idx);
*reinterpret_cast<__half2*>(vec_buffer + idx) = __floats2half2_rn(value.x, value.y);
} else if constexpr (copy_halves) {
*reinterpret_cast<float2*>(vec_buffer + idx) =
*reinterpret_cast<const float2*>(d_vec + idx);
} else {
vec_buffer[idx] = d_vec[idx];
} else if (idx < padding_dims) {
vec_buffer[idx] = 0.0f;
}
}
}
if constexpr (std::is_same_v<Data_t, __half>) {
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;
}
} 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<float2*>(vec_buffer + idx) = float2{0.0f, 0.0f};
} else {
vec_buffer[idx] = DstT{0};
}
}
}
}

/** Converting load: loads Data_t from global memory into __half shared memory buffer. */
template <typename Data_t>
requires(!std::is_same_v<Data_t, __half>)
__device__ __forceinline__ void load_vec(__half* 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();
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;
}
}
}

/** One warp per block. Computes squared L2 norm for each row. */
template <typename Data_t>
RAFT_KERNEL compute_l2_norms_kernel(const Data_t* data, int dim, DistData_t* l2_norms)
Expand Down Expand Up @@ -900,15 +871,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<float*>(s_arena);
float* s_distances_old = reinterpret_cast<float*>(s_arena + DIST_BYTES);
__shared__ int s_unique_counter[2];

if (threadIdx.x == 0) {
Expand Down Expand Up @@ -987,31 +961,26 @@ __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) {
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<true>(s_nv[idx],
data + idx_in_data + step * TILE_COL_WIDTH,
num_load_elems,
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;
load_vec(s_ov[idx],
data + idx_in_data + step * TILE_COL_WIDTH,
num_load_elems,
TILE_COL_WIDTH,
lane_id);
load_vec<true>(s_ov[idx],
data + idx_in_data + step * TILE_COL_WIDTH,
num_load_elems,
TILE_COL_WIDTH,
lane_id);
}
}
__syncthreads();
Expand All @@ -1038,15 +1007,22 @@ __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,
c_nn,
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();

Expand All @@ -1073,20 +1049,7 @@ __launch_bounds__(BLOCK_SIZE)

if (!has_old) return;

__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,
Expand All @@ -1108,11 +1071,14 @@ __launch_bounds__(BLOCK_SIZE)
std::numeric_limits<DistData_t>::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; }
}

Expand Down Expand Up @@ -2420,7 +2386,13 @@ void GNND<Data_t, Index_t>::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<std::remove_pointer_t<decltype(typed_ptr)>>;
bool launch_simt = use_simt;
if constexpr (std::is_same_v<launch_data_t, float> || std::is_same_v<launch_data_t, __half>) {
launch_simt = launch_simt || reinterpret_cast<size_t>(typed_ptr) % sizeof(float2) != 0 ||
(static_cast<size_t>(ndim_) * sizeof(launch_data_t)) % sizeof(float2) != 0;
}
if (launch_simt) {
local_join_kernel_simt<<<nrow_, BLOCK_SIZE, 0, stream>>>(graph_.h_graph_new.data_handle(),
h_rev_graph_new_.data_handle(),
d_list_sizes_new_.data_handle(),
Expand Down
Loading