Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add L2SqrtExpanded support to ivf_flat ANN indices #1133

Merged
merged 7 commits into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
9 changes: 7 additions & 2 deletions cpp/include/raft/neighbors/ivf_flat_types.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, NVIDIA CORPORATION.
* Copyright (c) 2022-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -252,8 +252,13 @@ struct index : ann::index {
* Replace the content of the index with new uninitialized mdarrays to hold the indicated amount
* of data.
*/
void allocate(const handle_t& handle, IdxT index_size, bool allocate_center_norms)
void allocate(const handle_t& handle, IdxT index_size)
{
bool allocate_center_norms = ((metric_ == raft::distance::DistanceType::L2Expanded) ||
(metric_ == raft::distance::DistanceType::L2SqrtExpanded) ||
(metric_ == raft::distance::DistanceType::L2Unexpanded) ||
(metric_ == raft::distance::DistanceType::L2SqrtUnexpanded));

data_ = make_device_mdarray<T>(handle, make_extents<IdxT>(index_size, dim()));
indices_ = make_device_mdarray<IdxT>(handle, make_extents<IdxT>(index_size));
center_norms_ =
Expand Down
4 changes: 3 additions & 1 deletion cpp/include/raft/spatial/knn/detail/ann_quantized.cuh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021-2022, NVIDIA CORPORATION.
* Copyright (c) 2021-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -122,6 +122,8 @@ void approx_knn_build_index(const handle_t& handle,

if (ivf_ft_pams && (metric == raft::distance::DistanceType::L2Unexpanded ||
metric == raft::distance::DistanceType::L2Expanded ||
metric == raft::distance::DistanceType::L2SqrtExpanded ||
metric == raft::distance::DistanceType::L2SqrtUnexpanded ||
achirkin marked this conversation as resolved.
Show resolved Hide resolved
metric == raft::distance::DistanceType::InnerProduct)) {
auto new_params = from_legacy_index_params(*ivf_ft_pams, metric, metricArg);
index->ivf_flat<T, int64_t>() = std::make_unique<const ivf_flat::index<T, int64_t>>(
Expand Down
10 changes: 4 additions & 6 deletions cpp/include/raft/spatial/knn/detail/ivf_flat_build.cuh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, NVIDIA CORPORATION.
* Copyright (c) 2022-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -191,8 +191,7 @@ inline auto extend(const handle_t& handle,
update_host(&index_size, list_offsets_ptr + n_lists, 1, stream);
handle.sync_stream(stream);

ext_index.allocate(
handle, index_size, ext_index.metric() == raft::distance::DistanceType::L2Expanded);
ext_index.allocate(handle, index_size);

// Populate index with the old data
if (orig_index.size() > 0) {
Expand Down Expand Up @@ -359,8 +358,7 @@ inline void fill_refinement_index(const handle_t& handle,
stream);

IdxT index_size = n_roundup * n_lists;
refinement_index->allocate(
handle, index_size, refinement_index->metric() == raft::distance::DistanceType::L2Expanded);
refinement_index->allocate(handle, index_size);

RAFT_CUDA_TRY(cudaMemsetAsync(list_sizes_ptr, 0, n_lists * sizeof(uint32_t), stream));

Expand Down Expand Up @@ -454,7 +452,7 @@ auto load(const handle_t& handle, const std::string& filename) -> index<T, IdxT>
index<T, IdxT> index_ =
raft::spatial::knn::ivf_flat::index<T, IdxT>(handle, metric, n_lists, adaptive_centers, dim);

index_.allocate(handle, n_rows, metric == raft::distance::DistanceType::L2Expanded);
index_.allocate(handle, n_rows);
auto data = index_.data();
read_mdspan(handle, infile, data);
read_mdspan(handle, infile, index_.indices());
Expand Down
14 changes: 12 additions & 2 deletions cpp/include/raft/spatial/knn/detail/ivf_flat_search.cuh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, NVIDIA CORPORATION.
* Copyright (c) 2022-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -935,6 +935,8 @@ void launch_with_fixed_consts(raft::distance::DistanceType metric, Args&&... arg
switch (metric) {
case raft::distance::DistanceType::L2Expanded:
case raft::distance::DistanceType::L2Unexpanded:
case raft::distance::DistanceType::L2SqrtExpanded:
case raft::distance::DistanceType::L2SqrtUnexpanded:
return launch_kernel<Capacity,
Veclen,
Ascending,
Expand Down Expand Up @@ -1105,7 +1107,8 @@ void search_impl(const handle_t& handle,
float beta = 0.0f;

// todo(lsugy): raft distance? (if performance is similar/better than gemm)
if (index.metric() == raft::distance::DistanceType::L2Expanded) {
if ((index.metric() == raft::distance::DistanceType::L2Expanded) ||
(index.metric() == raft::distance::DistanceType::L2SqrtExpanded)) {
alpha = -2.0f;
beta = 1.0f;
raft::linalg::rowNorm(query_norm_dev.data(),
Expand Down Expand Up @@ -1215,6 +1218,13 @@ void search_impl(const handle_t& handle,
stream,
search_mr);
}

// post-process
if (index.metric() == raft::distance::DistanceType::L2SqrtExpanded ||
index.metric() == raft::distance::DistanceType::L2SqrtUnexpanded) {
raft::linalg::unaryOp<float>(
distances, distances, n_queries * k, raft::sqrt_op(), handle.get_stream());
}
achirkin marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down
4 changes: 3 additions & 1 deletion cpp/test/neighbors/ann_ivf_flat.cu
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, NVIDIA CORPORATION.
* Copyright (c) 2022-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -294,6 +294,8 @@ const std::vector<AnnIvfFlatInputs<int64_t>> inputs = {
{1000, 10000, 4, 16, 40, 1024, raft::distance::DistanceType::L2Expanded, false},
{1000, 10000, 5, 16, 40, 1024, raft::distance::DistanceType::InnerProduct, false},
{1000, 10000, 8, 16, 40, 1024, raft::distance::DistanceType::InnerProduct, true},
{1000, 10000, 5, 16, 40, 1024, raft::distance::DistanceType::L2SqrtExpanded, false},
{1000, 10000, 8, 16, 40, 1024, raft::distance::DistanceType::L2SqrtExpanded, true},

// test dims that do not fit into kernel shared memory limits
{1000, 10000, 2048, 16, 40, 1024, raft::distance::DistanceType::L2Expanded, false},
Expand Down