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

Replace faiss bfKnn #1202

Merged
merged 92 commits into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
92 commits
Select commit Hold shift + click to select a range
57cfa20
Replace faiss bfKnn
benfred Jan 27, 2023
984c641
Merge branch 'branch-23.02' into bfknn
benfred Jan 27, 2023
805abc7
fix merge
benfred Jan 27, 2023
3e21478
Merge branch 'branch-23.02' into bfknn
cjnolet Jan 27, 2023
74bd44f
Fix bug with col_tiles < K
benfred Jan 27, 2023
c69054d
Merge branch 'bfknn' of github.com:benfred/raft into bfknn
benfred Jan 27, 2023
1d9581b
Include metric_arg in bfknn
benfred Jan 30, 2023
b4cf88c
speedup compile times
benfred Jan 30, 2023
98ffb70
Merge branch 'branch-23.02' into bfknn
benfred Jan 31, 2023
5442d31
Suggestions from code review
benfred Jan 31, 2023
0f5d206
fixes
benfred Jan 31, 2023
cb2b750
Merge branch 'branch-23.04' into bfknn
benfred Feb 3, 2023
e870eb3
use pairwise_distance specialization to speed up compile times
benfred Feb 7, 2023
cd84397
Merge branch 'branch-23.04' into bfknn
benfred Feb 7, 2023
8445aed
Use distance specializations
benfred Feb 7, 2023
e87633a
Merge branch 'branch-23.04' into bfknn
cjnolet Feb 9, 2023
52bf729
Merge branch 'branch-23.04' into bfknn
benfred Feb 11, 2023
d97ddb8
Merge branch 'branch-23.04' into bfknn
cjnolet Feb 11, 2023
5905b2d
use specializations in RBC code
benfred Feb 14, 2023
d905266
Merge branch 'branch-23.04' into bfknn
benfred Feb 14, 2023
8eaba84
use pw specializations in rbc
benfred Feb 14, 2023
fe728e9
use matrix::select_k in bfknn call
benfred Feb 14, 2023
96e05e1
expose bf detail specialization
benfred Feb 15, 2023
59060b2
Revert "use pw specializations in rbc"
benfred Feb 15, 2023
c734bac
Add tests for other metrics
benfred Feb 15, 2023
c65e4bb
Fix parameter order
benfred Feb 16, 2023
3830e53
Fix Lp distance
benfred Feb 17, 2023
3f0b9a7
Revert "use matrix::select_k in bfknn call"
benfred Feb 17, 2023
3900570
re-enable failing tests
benfred Feb 17, 2023
8e71915
fix cosine/innerproduct in bfknn
benfred Feb 17, 2023
f806bf6
Test JensenShannon distance
benfred Feb 17, 2023
3315dca
support k up to 2048 in faiss select
benfred Feb 18, 2023
1b6eda2
Merge remote-tracking branch 'origin/branch-23.04' into bfknn
benfred Feb 18, 2023
a83bef3
cmake format
benfred Feb 18, 2023
3b811a1
support k up to 2048 in faiss select
benfred Feb 18, 2023
9a19456
Merge branch 'branch-23.04' into faiss_largek
cjnolet Feb 18, 2023
c39dc65
style
benfred Feb 18, 2023
c60e17f
Merge branch 'faiss_largek' of github.com:benfred/raft into faiss_largek
benfred Feb 18, 2023
2752294
code review suggestions
benfred Feb 18, 2023
84f7a42
Merge remote-tracking branch 'bf/faiss_largek' into bfknn
benfred Feb 18, 2023
901b898
Merge branch 'branch-23.04' into bfknn
cjnolet Feb 20, 2023
1548a78
Remove ENABLE_NN_DEPENDENCIES option
benfred Feb 21, 2023
3fdc712
Merge branch 'branch-23.04' into bfknn
benfred Feb 21, 2023
31c9cf2
temporarily re-add faiss build targets
benfred Feb 21, 2023
f7fd6a7
couple more files to re-add faiss
benfred Feb 21, 2023
37d66d2
re-add faiss_mr
benfred Feb 22, 2023
a61c92f
explicitly include faiss_mr
benfred Feb 23, 2023
dbd31b2
Allow col_major input to bfknn
benfred Feb 24, 2023
fddecc3
fix faiss queryempty test
benfred Feb 24, 2023
4687144
exclude LP from fused
benfred Feb 27, 2023
bd3ff51
Merge branch 'branch-23.04' into bfknn
benfred Feb 27, 2023
06c8674
use metric processor for cosine/correlation
benfred Feb 27, 2023
b44d15c
exclude cosine
benfred Feb 28, 2023
5a582cd
Merge branch 'branch-23.04' into bfknn
benfred Mar 1, 2023
eb0271a
avoid l2expanded distance
benfred Mar 1, 2023
616455c
Merge branch 'bfknn' of github.com:benfred/raft into bfknn
benfred Mar 1, 2023
4c41c63
Expanded L2 Changes
benfred Mar 6, 2023
cdf1962
correct for small instabilities in l2sqrtexpanded distance
benfred Mar 7, 2023
6a1e2d8
warp divergence
benfred Mar 7, 2023
1e2817c
clamp to 0
benfred Mar 7, 2023
4b56fac
threshold
benfred Mar 7, 2023
4b41e2c
Transpose for fusedl2knn as well
benfred Mar 8, 2023
455c952
fix
benfred Mar 8, 2023
df46b65
Fix stream handling on col-major inputs
benfred Mar 8, 2023
97a3c01
Merge branch 'bfknn' of github.com:benfred/raft into bfknn
benfred Mar 8, 2023
105bc96
Merge branch 'branch-23.04' into bfknn
benfred Mar 9, 2023
2828c3b
Merge branch 'branch-23.04' into bfknn
benfred Mar 9, 2023
6e45267
Merge branch 'branch-23.04' into bfknn
benfred Mar 11, 2023
28ebeef
fix build for missing symbols
benfred Mar 13, 2023
9917324
Merge branch 'branch-23.04' into bfknn
cjnolet Mar 13, 2023
65d7725
code review feedback
benfred Mar 14, 2023
e41ff88
matrix::fill and linalg::map_offset
benfred Mar 14, 2023
5eb7d22
build fix
benfred Mar 14, 2023
e36d089
fix
benfred Mar 14, 2023
50e366f
Merge branch 'branch-23.04' into bfknn
benfred Mar 14, 2023
e8f9c55
move faiss_select into raft::neighbors namespace
benfred Mar 14, 2023
9f211a0
move knn_merge parts to its own file
benfred Mar 14, 2023
9cbac3c
Merge branch 'branch-23.04' into bfknn
cjnolet Mar 14, 2023
0667526
Merge remote-tracking branch 'origin/branch-23.04' into bfknn
benfred Mar 15, 2023
9593ae1
Use stream pool
benfred Mar 15, 2023
76d2b19
Merge branch 'bfknn' of github.com:benfred/raft into bfknn
benfred Mar 15, 2023
97753b0
use right handle for transpose
benfred Mar 15, 2023
a534538
set blas stream
benfred Mar 15, 2023
237b7e1
error handling
benfred Mar 15, 2023
07290bc
try to isolate stream failure
benfred Mar 16, 2023
2671a0e
Move transpose code out of loop
benfred Mar 16, 2023
7e0bb9b
fix
benfred Mar 16, 2023
b4c3284
try transpose inside streampool again
benfred Mar 16, 2023
92d82db
one more try with cublasSetStream
benfred Mar 17, 2023
c84e560
Merge branch 'branch-23.04' into bfknn
benfred Mar 17, 2023
21a1953
fix
benfred Mar 17, 2023
80fb76c
Merge branch 'bfknn' of github.com:benfred/raft into bfknn
benfred Mar 17, 2023
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
2 changes: 1 addition & 1 deletion ci/checks/copyright.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
re.compile(r"setup[.]cfg$"),
re.compile(r"meta[.]yaml$")
]
ExemptFiles = ["cpp/include/raft/spatial/knn/detail/faiss_select/"]
ExemptFiles = ["cpp/include/raft/neighbors/detail/faiss_select/"]

# this will break starting at year 10000, which is probably OK :)
CheckSimple = re.compile(
Expand Down
4 changes: 3 additions & 1 deletion cpp/include/raft/core/resource/cublas_handle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ inline cublasHandle_t get_cublas_handle(resources const& res)
cudaStream_t stream = get_cuda_stream(res);
res.add_resource_factory(std::make_shared<cublas_resource_factory>(stream));
}
return *res.get_resource<cublasHandle_t>(resource_type::CUBLAS_HANDLE);
auto ret = *res.get_resource<cublasHandle_t>(resource_type::CUBLAS_HANDLE);
RAFT_CUBLAS_TRY(cublasSetStream(ret, get_cuda_stream(res)));
return ret;
};

/**
Expand Down
22 changes: 21 additions & 1 deletion cpp/include/raft/distance/distance_types.hpp
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 @@ -66,6 +66,26 @@ enum DistanceType : unsigned short {
Precomputed = 100
};

/**
* Whether minimal distance corresponds to similar elements (using the given metric).
*/
inline bool is_min_close(DistanceType metric)
{
bool select_min;
switch (metric) {
case DistanceType::InnerProduct:
case DistanceType::CosineExpanded:
case DistanceType::CorrelationExpanded:
// Similarity metrics have the opposite meaning, i.e. nearest neighbors are those with larger
// similarity (See the same logic at cpp/include/raft/sparse/spatial/detail/knn.cuh:362
// {perform_k_selection})
select_min = false;
break;
default: select_min = true;
}
return select_min;
}

namespace kernels {
enum KernelType { LINEAR, POLYNOMIAL, RBF, TANH };

Expand Down
3 changes: 3 additions & 0 deletions cpp/include/raft/linalg/detail/transpose.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ void transpose(raft::device_resources const& handle,
cudaStream_t stream)
{
cublasHandle_t cublas_h = handle.get_cublas_handle();
RAFT_CUBLAS_TRY(cublasSetStream(cublas_h, stream));

int out_n_rows = n_cols;
int out_n_cols = n_rows;
Expand Down Expand Up @@ -90,6 +91,7 @@ void transpose_row_major_impl(
auto out_n_cols = in.extent(0);
T constexpr kOne = 1;
T constexpr kZero = 0;

CUBLAS_TRY(cublasgeam(handle.get_cublas_handle(),
CUBLAS_OP_T,
CUBLAS_OP_N,
Expand All @@ -116,6 +118,7 @@ void transpose_col_major_impl(
auto out_n_cols = in.extent(0);
T constexpr kOne = 1;
T constexpr kZero = 0;

CUBLAS_TRY(cublasgeam(handle.get_cublas_handle(),
CUBLAS_OP_T,
CUBLAS_OP_N,
Expand Down
51 changes: 25 additions & 26 deletions cpp/include/raft/neighbors/brute_force.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@

#include <raft/core/device_mdspan.hpp>
#include <raft/distance/distance_types.hpp>
#include <raft/neighbors/detail/knn_brute_force.cuh>
#include <raft/spatial/knn/detail/fused_l2_knn.cuh>
#include <raft/spatial/knn/detail/knn_brute_force_faiss.cuh>
#include <raft/spatial/knn/detail/selection_faiss.cuh>

namespace raft::neighbors::brute_force {

Expand Down Expand Up @@ -96,15 +95,15 @@ inline void knn_merge_parts(
"Number of columns in output indices and distances matrices must be equal to k");

auto n_parts = in_keys.extent(0) / n_samples;
spatial::knn::detail::knn_merge_parts(in_keys.data_handle(),
in_values.data_handle(),
out_keys.data_handle(),
out_values.data_handle(),
n_samples,
n_parts,
in_keys.extent(1),
handle.get_stream(),
translations.value_or(nullptr));
detail::knn_merge_parts(in_keys.data_handle(),
Copy link
Member

Choose a reason for hiding this comment

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

We should make a note to eventually get these out of this file. These are k-selection specific helpers that really belong in raft/matrix/select_k.cuh.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've moved these to neighbors/detail/knn_merge_parts.cuh for now -

in_values.data_handle(),
out_keys.data_handle(),
out_values.data_handle(),
n_samples,
n_parts,
in_keys.extent(1),
handle.get_stream(),
translations.value_or(nullptr));
}

/**
Expand Down Expand Up @@ -181,21 +180,21 @@ void knn(raft::device_resources const& handle,

std::vector<idx_t>* trans_arg = global_id_offset.has_value() ? &trans : nullptr;

raft::spatial::knn::detail::brute_force_knn_impl(handle,
inputs,
sizes,
static_cast<value_int>(index[0].extent(1)),
// TODO: This is unfortunate. Need to fix.
const_cast<value_t*>(search.data_handle()),
static_cast<value_int>(search.extent(0)),
indices.data_handle(),
distances.data_handle(),
k,
rowMajorIndex,
rowMajorQuery,
trans_arg,
metric,
metric_arg.value_or(2.0f));
raft::neighbors::detail::brute_force_knn_impl(handle,
inputs,
sizes,
static_cast<value_int>(index[0].extent(1)),
// TODO: This is unfortunate. Need to fix.
const_cast<value_t*>(search.data_handle()),
static_cast<value_int>(search.extent(0)),
indices.data_handle(),
distances.data_handle(),
k,
rowMajorIndex,
rowMajorQuery,
trans_arg,
metric,
metric_arg.value_or(2.0f));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <cuda.h>
#include <cuda_fp16.h>

namespace raft::spatial::knn::detail::faiss_select {
namespace raft::neighbors::detail::faiss_select {

template <typename T>
struct Comparator {
Expand All @@ -26,4 +26,4 @@ struct Comparator<half> {
__device__ static inline bool gt(half a, half b) { return __hgt(a, b); }
};

} // namespace raft::spatial::knn::detail::faiss_select
} // namespace raft::neighbors::detail::faiss_select
52 changes: 52 additions & 0 deletions cpp/include/raft/neighbors/detail/faiss_select/DistanceUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file thirdparty/LICENSES/LICENSE.faiss
*/

#pragma once

namespace raft::neighbors::detail::faiss_select {
// If the inner size (dim) of the vectors is small, we want a larger query tile
// size, like 1024
inline void chooseTileSize(size_t numQueries,
size_t numCentroids,
size_t dim,
size_t elementSize,
size_t totalMem,
size_t& tileRows,
size_t& tileCols)
{
// The matrix multiplication should be large enough to be efficient, but if
// it is too large, we seem to lose efficiency as opposed to
// double-streaming. Each tile size here defines 1/2 of the memory use due
// to double streaming. We ignore available temporary memory, as that is
// adjusted independently by the user and can thus meet these requirements
// (or not). For <= 4 GB GPUs, prefer 512 MB of usage. For <= 8 GB GPUs,
// prefer 768 MB of usage. Otherwise, prefer 1 GB of usage.
size_t targetUsage = 0;

if (totalMem <= ((size_t)4) * 1024 * 1024 * 1024) {
targetUsage = 512 * 1024 * 1024;
} else if (totalMem <= ((size_t)8) * 1024 * 1024 * 1024) {
targetUsage = 768 * 1024 * 1024;
} else {
targetUsage = 1024 * 1024 * 1024;
}

targetUsage /= 2 * elementSize;

// 512 seems to be a batch size sweetspot for float32.
// If we are on float16, increase to 512.
// If the k size (vec dim) of the matrix multiplication is small (<= 32),
// increase to 1024.
size_t preferredTileRows = 512;
if (dim <= 32) { preferredTileRows = 1024; }

tileRows = std::min(preferredTileRows, numQueries);

// tileCols is the remainder size
tileCols = std::min(targetUsage / preferredTileRows, numCentroids);
}
} // namespace raft::neighbors::detail::faiss_select
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
#pragma once

#include <cuda.h>
#include <raft/spatial/knn/detail/faiss_select/MergeNetworkUtils.cuh>
#include <raft/spatial/knn/detail/faiss_select/StaticUtils.h>
#include <raft/neighbors/detail/faiss_select/MergeNetworkUtils.cuh>
#include <raft/neighbors/detail/faiss_select/StaticUtils.h>

namespace raft::spatial::knn::detail::faiss_select {
namespace raft::neighbors::detail::faiss_select {

// Merge pairs of lists smaller than blockDim.x (NumThreads)
template <int NumThreads,
Expand Down Expand Up @@ -274,4 +274,4 @@ inline __device__ void blockMerge(K* listK, V* listV)
BlockMerge<NumThreads, K, V, N, L, Dir, Comp, kSmallerThanBlock, FullMerge>::merge(listK, listV);
}

} // namespace raft::spatial::knn::detail::faiss_select
} // namespace raft::neighbors::detail::faiss_select
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#pragma once

namespace raft::spatial::knn::detail::faiss_select {
namespace raft::neighbors::detail::faiss_select {

template <typename T>
inline __device__ void swap(bool swap, T& x, T& y)
Expand All @@ -22,4 +22,4 @@ inline __device__ void assign(bool assign, T& x, T y)
{
x = assign ? y : x;
}
} // namespace raft::spatial::knn::detail::faiss_select
} // namespace raft::neighbors::detail::faiss_select
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

#pragma once

#include <raft/spatial/knn/detail/faiss_select/MergeNetworkUtils.cuh>
#include <raft/spatial/knn/detail/faiss_select/StaticUtils.h>
#include <raft/neighbors/detail/faiss_select/MergeNetworkUtils.cuh>
#include <raft/neighbors/detail/faiss_select/StaticUtils.h>

#include <raft/util/cuda_utils.cuh>

namespace raft::spatial::knn::detail::faiss_select {
namespace raft::neighbors::detail::faiss_select {

//
// This file contains functions to:
Expand Down Expand Up @@ -518,4 +518,4 @@ inline __device__ void warpSortAnyRegisters(K k[N], V v[N])
BitonicSortStep<K, V, N, Dir, Comp>::sort(k, v);
}

} // namespace raft::spatial::knn::detail::faiss_select
} // namespace raft::neighbors::detail::faiss_select
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@

#pragma once

#include <raft/spatial/knn/detail/faiss_select/Comparators.cuh>
#include <raft/spatial/knn/detail/faiss_select/MergeNetworkBlock.cuh>
#include <raft/spatial/knn/detail/faiss_select/MergeNetworkWarp.cuh>
#include <raft/neighbors/detail/faiss_select/Comparators.cuh>
#include <raft/neighbors/detail/faiss_select/MergeNetworkBlock.cuh>
#include <raft/neighbors/detail/faiss_select/MergeNetworkWarp.cuh>

#include <raft/core/kvp.hpp>
#include <raft/util/cuda_utils.cuh>

namespace raft::spatial::knn::detail::faiss_select {
namespace raft::neighbors::detail::faiss_select {

// Specialization for block-wide monotonic merges producing a merge sort
// since what we really want is a constexpr loop expansion
Expand Down Expand Up @@ -552,4 +552,4 @@ struct WarpSelect<K, V, Dir, Comp, 1, NumThreadQ, ThreadsPerBlock> {
V threadV;
};

} // namespace raft::spatial::knn::detail::faiss_select
} // namespace raft::neighbors::detail::faiss_select
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#define __device__
#endif

namespace raft::spatial::knn::detail::faiss_select::utils {
namespace raft::neighbors::detail::faiss_select::utils {

template <typename T>
constexpr __host__ __device__ bool isPowerOf2(T v)
Expand Down Expand Up @@ -45,4 +45,4 @@ static_assert(nextHighestPowerOf2(1536000000u) == 2147483648u, "nextHighestPower
static_assert(nextHighestPowerOf2((size_t)2147483648ULL) == (size_t)4294967296ULL,
"nextHighestPowerOf2");

} // namespace raft::spatial::knn::detail::faiss_select::utils
} // namespace raft::neighbors::detail::faiss_select::utils
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@

#pragma once

#include <raft/spatial/knn/detail/faiss_select/MergeNetworkUtils.cuh>
#include <raft/spatial/knn/detail/faiss_select/Select.cuh>
#include <raft/neighbors/detail/faiss_select/MergeNetworkUtils.cuh>
#include <raft/neighbors/detail/faiss_select/Select.cuh>

// TODO: Need to think further about the impact (and new boundaries created) on the registers
// because this will change the max k that can be processed. One solution might be to break
// up k into multiple batches for larger k.

namespace raft::spatial::knn::detail::faiss_select {
namespace raft::neighbors::detail::faiss_select {

// `Dir` true, produce largest values.
// `Dir` false, produce smallest values.
Expand Down Expand Up @@ -221,4 +221,4 @@ struct KeyValueBlockSelect {
int kMinus1;
};

} // namespace raft::spatial::knn::detail::faiss_select
} // namespace raft::neighbors::detail::faiss_select
22 changes: 1 addition & 21 deletions cpp/include/raft/neighbors/detail/ivf_flat_search.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -1244,26 +1244,6 @@ void search_impl(raft::device_resources const& handle,
}
}

/**
* Whether minimal distance corresponds to similar elements (using the given metric).
*/
inline bool is_min_close(distance::DistanceType metric)
{
bool select_min;
switch (metric) {
case raft::distance::DistanceType::InnerProduct:
case raft::distance::DistanceType::CosineExpanded:
case raft::distance::DistanceType::CorrelationExpanded:
// Similarity metrics have the opposite meaning, i.e. nearest neighbors are those with larger
// similarity (See the same logic at cpp/include/raft/sparse/spatial/detail/knn.cuh:362
// {perform_k_selection})
select_min = false;
break;
default: select_min = true;
}
return select_min;
}

/** See raft::neighbors::ivf_flat::search docs */
template <typename T, typename IdxT>
inline void search(raft::device_resources const& handle,
Expand Down Expand Up @@ -1295,7 +1275,7 @@ inline void search(raft::device_resources const& handle,
n_queries,
k,
n_probes,
is_min_close(index.metric()),
raft::distance::is_min_close(index.metric()),
neighbors,
distances,
mr);
Expand Down
Loading