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

Interop between CAGRA and HNSW #3252

Open
wants to merge 46 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
753a109
start integration of cagra
divyegala Oct 5, 2023
6ce2467
merge upstream
divyegala Jan 23, 2024
f21c1f1
add public API layer
divyegala Jan 30, 2024
11c0c54
merge upstream
divyegala Jan 30, 2024
656f493
write tests, figure out a way to compare
divyegala Feb 1, 2024
de67ca6
Merge remote-tracking branch 'upstream/main' into raft-cagra
divyegala Feb 1, 2024
ed32954
passing tests
divyegala Feb 7, 2024
42ca862
remove cpp test file
divyegala Feb 7, 2024
2fdfc6f
Merge remote-tracking branch 'upstream/main' into raft-cagra
divyegala Feb 7, 2024
2c9e965
style check
divyegala Feb 7, 2024
2e434fe
add required methods
divyegala Feb 7, 2024
382c178
conditionally compile cagra
divyegala Feb 8, 2024
8675974
copyTo and copyFrom
divyegala Feb 14, 2024
c7fcf4a
style check
divyegala Feb 14, 2024
eae832d
Merge branch 'main' into raft-cagra-hnsw
divyegala Feb 14, 2024
4b76e5f
Merge branch 'main' into raft-cagra-hnsw
divyegala Feb 16, 2024
065f912
add read/write
divyegala Feb 20, 2024
301f429
Merge remote-tracking branch 'origin/raft-cagra-hnsw' into raft-cagra…
divyegala Feb 20, 2024
2b0ea76
add destructor
divyegala Feb 20, 2024
8c83bd2
destructor body, copyto reset
divyegala Feb 21, 2024
39fb35a
remove destructor
divyegala Feb 21, 2024
49e2610
move cmake sources around
divyegala Feb 21, 2024
11bf6b2
merge upstream
divyegala Feb 21, 2024
d4434bb
more protections for copying
divyegala Feb 21, 2024
ac65c2d
support default constructed IndexHnswCagra in copyTo
divyegala Feb 22, 2024
619c376
fix failing binary hnsw tests
divyegala Feb 22, 2024
e25f8a4
link faiss_gpu target to OpenMP
divyegala Feb 23, 2024
e835150
raft still can't find openmp
divyegala Feb 23, 2024
aeabe12
openmp flags and uint32 IndexType
divyegala Feb 26, 2024
4e80586
forgot conditional check in index_read
divyegala Feb 26, 2024
c4bcaba
minor changes
divyegala Mar 7, 2024
341a3fc
api change
divyegala Mar 7, 2024
0ae7702
Merge branch 'raft-api-changes' into raft-cagra-hnsw
divyegala Mar 7, 2024
172aa65
working python
divyegala Mar 20, 2024
0cd684e
compile option to swig
divyegala Mar 21, 2024
7ff8b3b
expose ivf pq params
divyegala Apr 3, 2024
66d236f
update comments style
divyegala Apr 22, 2024
f697eac
Merge remote-tracking branch 'upstream/main' into raft-cagra-hnsw
divyegala Apr 22, 2024
1d6e6b1
use raft::runtime where possible
divyegala Apr 22, 2024
4a01ad4
format
divyegala Apr 22, 2024
949e634
format properly
divyegala Apr 22, 2024
bccd54a
InnerProduct
divyegala Apr 30, 2024
320654c
Merge remote-tracking branch 'upstream/main' into raft-cagra-hnsw
divyegala Apr 30, 2024
2aaa6e9
passing ip tests
divyegala May 7, 2024
70b0ab8
address review
divyegala May 9, 2024
e5756cc
Merge remote-tracking branch 'upstream/main' into raft-cagra-hnsw
divyegala May 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Expand Up @@ -46,6 +46,8 @@ project(faiss
LANGUAGES ${FAISS_LANGUAGES})
include(GNUInstallDirs)

set(CMAKE_INSTALL_PREFIX "$ENV{CONDA_PREFIX}")
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure we want to keep this as Faiss is not necessarily compiled in a conda env.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, sorry. This was just for development purposes - I will remove it.


set(CMAKE_CXX_STANDARD 17)

list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
Expand Down
42 changes: 39 additions & 3 deletions faiss/IndexHNSW.cpp
Expand Up @@ -17,6 +17,7 @@
#include <cstdlib>
#include <cstring>

#include <memory>
#include <queue>
#include <unordered_set>

Expand Down Expand Up @@ -192,7 +193,9 @@ void hnsw_add_vertices(

int i1 = n;

for (int pt_level = hist.size() - 1; pt_level >= 0; pt_level--) {
for (int pt_level = hist.size() - 1;
pt_level >= !index_hnsw.init_level0;
pt_level--) {
int i0 = i1 - hist[pt_level];

if (verbose) {
Expand Down Expand Up @@ -228,7 +231,13 @@ void hnsw_add_vertices(
continue;
}

hnsw.add_with_locks(*dis, pt_level, pt_id, locks, vt);
hnsw.add_with_locks(
*dis,
pt_level,
pt_id,
locks,
vt,
index_hnsw.keep_max_size_level0 && (pt_level == 0));

if (prev_display >= 0 && i - i0 > prev_display + 10000) {
prev_display = i - i0;
Expand All @@ -248,7 +257,11 @@ void hnsw_add_vertices(
}
i1 = i0;
}
FAISS_ASSERT(i1 == 0);
if (index_hnsw.init_level0) {
FAISS_ASSERT(i1 == 0);
} else {
FAISS_ASSERT((i1 - hist[0]) == 0);
}
}
if (verbose) {
printf("Done in %.3f ms\n", getmillisecs() - t0);
Expand Down Expand Up @@ -910,4 +923,27 @@ void IndexHNSW2Level::flip_to_ivf() {
delete storage2l;
}

/**************************************************************
* IndexHNSWCagra implementation
**************************************************************/

IndexHNSWCagra::IndexHNSWCagra() {
is_trained = true;
}

IndexHNSWCagra::IndexHNSWCagra(int d, int M, MetricType metric)
: IndexHNSW(
(metric == METRIC_L2)
? static_cast<IndexFlat*>(new IndexFlatL2(d))
: static_cast<IndexFlat*>(new IndexFlatIP(d)),
M) {
FAISS_THROW_IF_NOT_MSG(
((metric == METRIC_L2) || (metric == METRIC_INNER_PRODUCT)),
"unsupported metric type for IndexHNSWCagra");
own_fields = true;
is_trained = true;
init_level0 = true;
keep_max_size_level0 = true;
}

} // namespace faiss
17 changes: 17 additions & 0 deletions faiss/IndexHNSW.h
Expand Up @@ -34,6 +34,18 @@ struct IndexHNSW : Index {
bool own_fields = false;
Index* storage = nullptr;

// When set to false, level 0 in the knn graph is not initialized.
// This option is used by GpuIndexCagra::copyTo(IndexHNSWCagra*)
// as level 0 knn graph is copied over from the index built by
// GpuIndexCagra.
bool init_level0 = true;
divyegala marked this conversation as resolved.
Show resolved Hide resolved

// When set to true, all neighbors in level 0 are filled up
// to the maximum size allowed (2 * M). This option is used by
// IndexHHNSWCagra to create a full base layer graph that is
// used when GpuIndexCagra::copyFrom(IndexHNSWCagra*) is invoked.
bool keep_max_size_level0 = false;

explicit IndexHNSW(int d = 0, int M = 32, MetricType metric = METRIC_L2);
explicit IndexHNSW(Index* storage, int M = 32);

Expand Down Expand Up @@ -148,4 +160,9 @@ struct IndexHNSW2Level : IndexHNSW {
const SearchParameters* params = nullptr) const override;
};

struct IndexHNSWCagra : IndexHNSW {
IndexHNSWCagra();
IndexHNSWCagra(int d, int M, MetricType metric = METRIC_L2);
};

} // namespace faiss
8 changes: 6 additions & 2 deletions faiss/gpu/CMakeLists.txt
Expand Up @@ -238,11 +238,15 @@ generate_ivf_interleaved_code()

if(FAISS_ENABLE_RAFT)
list(APPEND FAISS_GPU_HEADERS
GpuIndexCagra.h
impl/RaftCagra.cuh
impl/RaftFlatIndex.cuh
impl/RaftIVFFlat.cuh
impl/RaftIVFPQ.cuh
utils/RaftUtils.h)
list(APPEND FAISS_GPU_SRC
GpuIndexCagra.cu
impl/RaftCagra.cu
impl/RaftFlatIndex.cu
impl/RaftIVFFlat.cu
impl/RaftIVFPQ.cu
Expand Down Expand Up @@ -316,5 +320,5 @@ __nv_relfatbin : { *(__nv_relfatbin) }
target_link_options(faiss_gpu PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/fatbin.ld")

find_package(CUDAToolkit REQUIRED)
target_link_libraries(faiss_gpu PRIVATE CUDA::cudart CUDA::cublas $<$<BOOL:${FAISS_ENABLE_RAFT}>:raft::raft> $<$<BOOL:${FAISS_ENABLE_RAFT}>:raft::compiled> $<$<BOOL:${FAISS_ENABLE_RAFT}>:nvidia::cutlass::cutlass>)
target_compile_options(faiss_gpu PRIVATE $<$<COMPILE_LANGUAGE:CUDA>:-Xfatbin=-compress-all --expt-extended-lambda --expt-relaxed-constexpr>)
target_link_libraries(faiss_gpu PRIVATE CUDA::cudart CUDA::cublas $<$<BOOL:${FAISS_ENABLE_RAFT}>:raft::raft> $<$<BOOL:${FAISS_ENABLE_RAFT}>:raft::compiled> $<$<BOOL:${FAISS_ENABLE_RAFT}>:nvidia::cutlass::cutlass> $<$<BOOL:${FAISS_ENABLE_RAFT}>:OpenMP::OpenMP_CXX>)
target_compile_options(faiss_gpu PRIVATE $<$<COMPILE_LANGUAGE:CUDA>:-Xfatbin=-compress-all --expt-extended-lambda --expt-relaxed-constexpr $<$<BOOL:${FAISS_ENABLE_RAFT}>:-Xcompiler=${OpenMP_CXX_FLAGS}>>)