From 4fc7db2d6ba4299a4581ac7e5d21c2f289f638ae Mon Sep 17 00:00:00 2001 From: Rahul Rampure Date: Thu, 14 May 2026 17:29:45 +0530 Subject: [PATCH 1/6] Add C APIs --- c_api/IndexBinary_c.cpp | 2 ++ c_api/IndexBinary_c.h | 3 +++ c_api/gpu/CMakeLists.txt | 1 + c_api/gpu/GpuIndexIVF_c.cpp | 39 +++++++++++++++++++++++++++++++++++++ c_api/gpu/GpuIndexIVF_c.h | 31 +++++++++++++++++++++++++++++ 5 files changed, 76 insertions(+) create mode 100644 c_api/gpu/GpuIndexIVF_c.cpp create mode 100644 c_api/gpu/GpuIndexIVF_c.h diff --git a/c_api/IndexBinary_c.cpp b/c_api/IndexBinary_c.cpp index 6f576ca9da..43f5816518 100644 --- a/c_api/IndexBinary_c.cpp +++ b/c_api/IndexBinary_c.cpp @@ -23,6 +23,8 @@ DEFINE_GETTER(IndexBinary, idx_t, ntotal) DEFINE_GETTER(IndexBinary, FaissMetricType, metric_type) +DEFINE_GETTER(IndexBinary, int, code_size) + DEFINE_GETTER(IndexBinary, int, verbose); DEFINE_SETTER(IndexBinary, int, verbose); diff --git a/c_api/IndexBinary_c.h b/c_api/IndexBinary_c.h index 9ad05a69ea..f4329575cd 100644 --- a/c_api/IndexBinary_c.h +++ b/c_api/IndexBinary_c.h @@ -40,6 +40,9 @@ FAISS_DECLARE_GETTER(IndexBinary, idx_t, ntotal) /// Getter for metric_type FAISS_DECLARE_GETTER(IndexBinary, FaissMetricType, metric_type) +/// Getter for code_size (number of bytes per vector = d / 8) +FAISS_DECLARE_GETTER(IndexBinary, int, code_size) + FAISS_DECLARE_GETTER_SETTER(IndexBinary, int, verbose) /** Perform training on a representative set of vectors diff --git a/c_api/gpu/CMakeLists.txt b/c_api/gpu/CMakeLists.txt index 3c7214a576..ab3986af03 100644 --- a/c_api/gpu/CMakeLists.txt +++ b/c_api/gpu/CMakeLists.txt @@ -8,6 +8,7 @@ target_sources(faiss_c PRIVATE GpuAutoTune_c.cpp GpuClonerOptions_c.cpp GpuIndex_c.cpp + GpuIndexIVF_c.cpp GpuResources_c.cpp StandardGpuResources_c.cpp ) diff --git a/c_api/gpu/GpuIndexIVF_c.cpp b/c_api/gpu/GpuIndexIVF_c.cpp new file mode 100644 index 0000000000..9e4224d41d --- /dev/null +++ b/c_api/gpu/GpuIndexIVF_c.cpp @@ -0,0 +1,39 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +// -*- c++ -*- + +#include "GpuIndexIVF_c.h" +#include +#include +#include +#include +#include "macros_impl.h" + +using faiss::gpu::GpuIndexIVF; +using faiss::gpu::GpuIndexIVFFlat; +using faiss::gpu::GpuIndexIVFPQ; +using faiss::gpu::GpuIndexIVFScalarQuantizer; + +int faiss_GpuIndexIVF_reserve_memory( + FaissGpuIndexIVF* index, + size_t num_vectors) { + try { + auto ivf = reinterpret_cast(index); + if (auto flat = dynamic_cast(ivf)) { + flat->reserveMemory(num_vectors); + } else if (auto pq = dynamic_cast(ivf)) { + pq->reserveMemory(num_vectors); + } else if (auto sq = dynamic_cast(ivf)) { + sq->reserveMemory(num_vectors); + } else { + throw std::runtime_error( + "reserve_memory not supported for this GpuIndexIVF subtype"); + } + } + CATCH_AND_HANDLE +} diff --git a/c_api/gpu/GpuIndexIVF_c.h b/c_api/gpu/GpuIndexIVF_c.h new file mode 100644 index 0000000000..cb9ec102bb --- /dev/null +++ b/c_api/gpu/GpuIndexIVF_c.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +// -*- c -*- + +#ifndef FAISS_GPU_INDEX_IVF_C_H +#define FAISS_GPU_INDEX_IVF_C_H + +#include + +#include "GpuIndex_c.h" + +#ifdef __cplusplus +extern "C" { +#endif + +FAISS_DECLARE_CLASS_INHERITED(GpuIndexIVF, GpuIndex) + +int faiss_GpuIndexIVF_reserve_memory( + FaissGpuIndexIVF* index, + size_t num_vectors); + +#ifdef __cplusplus +} +#endif + +#endif From b2c49b6d971539f15dbd87992b2aecdfc47ea6c1 Mon Sep 17 00:00:00 2001 From: Rahul Rampure Date: Thu, 14 May 2026 18:17:46 +0530 Subject: [PATCH 2/6] add APIs --- c_api/gpu/GpuIndex_c.cpp | 19 +++++++++++++++++++ c_api/gpu/GpuIndex_c.h | 19 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/c_api/gpu/GpuIndex_c.cpp b/c_api/gpu/GpuIndex_c.cpp index 92d675a2e8..d7602f0cc4 100644 --- a/c_api/gpu/GpuIndex_c.cpp +++ b/c_api/gpu/GpuIndex_c.cpp @@ -11,6 +11,25 @@ #include #include "macros_impl.h" +using faiss::gpu::GpuIndex; using faiss::gpu::GpuIndexConfig; DEFINE_GETTER(GpuIndexConfig, int, device) + +int faiss_GpuIndex_d(const FaissGpuIndex* index) { + return reinterpret_cast(index)->d; +} + +int faiss_GpuIndex_train(FaissGpuIndex* index, idx_t n, const float* x) { + try { + reinterpret_cast(index)->train(n, x); + } + CATCH_AND_HANDLE +} + +int faiss_GpuIndex_add(FaissGpuIndex* index, idx_t n, const float* x) { + try { + reinterpret_cast(index)->add(n, x); + } + CATCH_AND_HANDLE +} diff --git a/c_api/gpu/GpuIndex_c.h b/c_api/gpu/GpuIndex_c.h index 4b7aab061e..e82bfa9488 100644 --- a/c_api/gpu/GpuIndex_c.h +++ b/c_api/gpu/GpuIndex_c.h @@ -22,6 +22,25 @@ FAISS_DECLARE_GETTER(GpuIndexConfig, int, device) FAISS_DECLARE_CLASS_INHERITED(GpuIndex, Index) +/// Getter for d (dimension) +FAISS_DECLARE_GETTER(GpuIndex, int, d) + +/** Perform training on a representative set of vectors + * + * @param index opaque pointer to GPU index object + * @param n nb of training vectors + * @param x training vectors, size n * d + */ +int faiss_GpuIndex_train(FaissGpuIndex* index, idx_t n, const float* x); + +/** Add n vectors of dimension d to the index. + * + * @param index opaque pointer to GPU index object + * @param n nb of vectors to add + * @param x input vectors, size n * d + */ +int faiss_GpuIndex_add(FaissGpuIndex* index, idx_t n, const float* x); + #ifdef __cplusplus } #endif From a4cd195c1287d3dbacc1ebdccdde8b5f3daa6507 Mon Sep 17 00:00:00 2001 From: Rahul Rampure Date: Thu, 14 May 2026 18:19:01 +0530 Subject: [PATCH 3/6] use macro --- c_api/gpu/GpuIndex_c.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/c_api/gpu/GpuIndex_c.cpp b/c_api/gpu/GpuIndex_c.cpp index d7602f0cc4..67b7d4a33e 100644 --- a/c_api/gpu/GpuIndex_c.cpp +++ b/c_api/gpu/GpuIndex_c.cpp @@ -16,9 +16,7 @@ using faiss::gpu::GpuIndexConfig; DEFINE_GETTER(GpuIndexConfig, int, device) -int faiss_GpuIndex_d(const FaissGpuIndex* index) { - return reinterpret_cast(index)->d; -} +DEFINE_GETTER(GpuIndex, int, d) int faiss_GpuIndex_train(FaissGpuIndex* index, idx_t n, const float* x) { try { From f609b8820af9ab4a9b3f7cae283d6bd47ec2e8b3 Mon Sep 17 00:00:00 2001 From: Rahul Rampure Date: Thu, 14 May 2026 18:26:57 +0530 Subject: [PATCH 4/6] more APIs --- c_api/gpu/GpuIndex_c.cpp | 16 ++++++++++++++++ c_api/gpu/GpuIndex_c.h | 20 ++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/c_api/gpu/GpuIndex_c.cpp b/c_api/gpu/GpuIndex_c.cpp index 67b7d4a33e..f33f8bc4f7 100644 --- a/c_api/gpu/GpuIndex_c.cpp +++ b/c_api/gpu/GpuIndex_c.cpp @@ -31,3 +31,19 @@ int faiss_GpuIndex_add(FaissGpuIndex* index, idx_t n, const float* x) { } CATCH_AND_HANDLE } + +int faiss_GpuIndex_search( + const FaissGpuIndex* index, + idx_t n, + const float* x, + idx_t k, + float* distances, + idx_t* labels) { + try { + reinterpret_cast(index)->search( + n, x, k, distances, labels); + } + CATCH_AND_HANDLE +} + +DEFINE_DESTRUCTOR(GpuIndex) diff --git a/c_api/gpu/GpuIndex_c.h b/c_api/gpu/GpuIndex_c.h index e82bfa9488..d19362e3c2 100644 --- a/c_api/gpu/GpuIndex_c.h +++ b/c_api/gpu/GpuIndex_c.h @@ -41,6 +41,26 @@ int faiss_GpuIndex_train(FaissGpuIndex* index, idx_t n, const float* x); */ int faiss_GpuIndex_add(FaissGpuIndex* index, idx_t n, const float* x); +/** Query n vectors of dimension d to the index. + * + * @param index opaque pointer to GPU index object + * @param n number of vectors to search + * @param x input vectors to search, size n * d + * @param k number of nearest neighbors to return + * @param distances output pairwise distances, size n * k + * @param labels output labels of the NNs, size n * k + */ +int faiss_GpuIndex_search( + const FaissGpuIndex* index, + idx_t n, + const float* x, + idx_t k, + float* distances, + idx_t* labels); + +/// Free the GPU index +FAISS_DECLARE_DESTRUCTOR(GpuIndex) + #ifdef __cplusplus } #endif From cde947d1a5a42723cc78d684135de57041cb1180 Mon Sep 17 00:00:00 2001 From: Rahul Rampure Date: Thu, 14 May 2026 18:55:30 +0530 Subject: [PATCH 5/6] add downcast --- c_api/gpu/GpuIndex_c.cpp | 8 ++++++++ c_api/gpu/GpuIndex_c.h | 3 +++ 2 files changed, 11 insertions(+) diff --git a/c_api/gpu/GpuIndex_c.cpp b/c_api/gpu/GpuIndex_c.cpp index f33f8bc4f7..c808c58343 100644 --- a/c_api/gpu/GpuIndex_c.cpp +++ b/c_api/gpu/GpuIndex_c.cpp @@ -8,11 +8,14 @@ // -*- c++ -*- #include "GpuIndex_c.h" +#include "GpuIndexIVF_c.h" #include +#include #include "macros_impl.h" using faiss::gpu::GpuIndex; using faiss::gpu::GpuIndexConfig; +using faiss::gpu::GpuIndexIVF; DEFINE_GETTER(GpuIndexConfig, int, device) @@ -47,3 +50,8 @@ int faiss_GpuIndex_search( } DEFINE_DESTRUCTOR(GpuIndex) + +FaissGpuIndexIVF* faiss_GpuIndexIVF_cast(FaissGpuIndex* index) { + return reinterpret_cast( + dynamic_cast(reinterpret_cast(index))); +} diff --git a/c_api/gpu/GpuIndex_c.h b/c_api/gpu/GpuIndex_c.h index d19362e3c2..06571e9ace 100644 --- a/c_api/gpu/GpuIndex_c.h +++ b/c_api/gpu/GpuIndex_c.h @@ -61,6 +61,9 @@ int faiss_GpuIndex_search( /// Free the GPU index FAISS_DECLARE_DESTRUCTOR(GpuIndex) +/// Downcast a GpuIndex to GpuIndexIVF (returns NULL if not a GpuIndexIVF) +FaissGpuIndexIVF* faiss_GpuIndexIVF_cast(FaissGpuIndex* index); + #ifdef __cplusplus } #endif From 4f0614e30262781777dc921e2adf562918879662 Mon Sep 17 00:00:00 2001 From: Rahul Rampure Date: Thu, 14 May 2026 19:02:45 +0530 Subject: [PATCH 6/6] fix downcast --- c_api/gpu/GpuIndexIVF_c.cpp | 6 ++++++ c_api/gpu/GpuIndexIVF_c.h | 4 ++++ c_api/gpu/GpuIndex_c.cpp | 5 ----- c_api/gpu/GpuIndex_c.h | 3 --- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/c_api/gpu/GpuIndexIVF_c.cpp b/c_api/gpu/GpuIndexIVF_c.cpp index 9e4224d41d..0909795ea0 100644 --- a/c_api/gpu/GpuIndexIVF_c.cpp +++ b/c_api/gpu/GpuIndexIVF_c.cpp @@ -14,6 +14,7 @@ #include #include "macros_impl.h" +using faiss::gpu::GpuIndex; using faiss::gpu::GpuIndexIVF; using faiss::gpu::GpuIndexIVFFlat; using faiss::gpu::GpuIndexIVFPQ; @@ -37,3 +38,8 @@ int faiss_GpuIndexIVF_reserve_memory( } CATCH_AND_HANDLE } + +FaissGpuIndexIVF* faiss_GpuIndexIVF_cast(FaissGpuIndex* index) { + return reinterpret_cast( + dynamic_cast(reinterpret_cast(index))); +} \ No newline at end of file diff --git a/c_api/gpu/GpuIndexIVF_c.h b/c_api/gpu/GpuIndexIVF_c.h index cb9ec102bb..742c4c9494 100644 --- a/c_api/gpu/GpuIndexIVF_c.h +++ b/c_api/gpu/GpuIndexIVF_c.h @@ -20,10 +20,14 @@ extern "C" { FAISS_DECLARE_CLASS_INHERITED(GpuIndexIVF, GpuIndex) +/// Reserve memory for a given number of vectors, before adding them to the index. int faiss_GpuIndexIVF_reserve_memory( FaissGpuIndexIVF* index, size_t num_vectors); +/// Downcast a GpuIndex to GpuIndexIVF (returns NULL if not a GpuIndexIVF) +FaissGpuIndexIVF* faiss_GpuIndexIVF_cast(FaissGpuIndex* index); + #ifdef __cplusplus } #endif diff --git a/c_api/gpu/GpuIndex_c.cpp b/c_api/gpu/GpuIndex_c.cpp index c808c58343..660960b380 100644 --- a/c_api/gpu/GpuIndex_c.cpp +++ b/c_api/gpu/GpuIndex_c.cpp @@ -50,8 +50,3 @@ int faiss_GpuIndex_search( } DEFINE_DESTRUCTOR(GpuIndex) - -FaissGpuIndexIVF* faiss_GpuIndexIVF_cast(FaissGpuIndex* index) { - return reinterpret_cast( - dynamic_cast(reinterpret_cast(index))); -} diff --git a/c_api/gpu/GpuIndex_c.h b/c_api/gpu/GpuIndex_c.h index 06571e9ace..d19362e3c2 100644 --- a/c_api/gpu/GpuIndex_c.h +++ b/c_api/gpu/GpuIndex_c.h @@ -61,9 +61,6 @@ int faiss_GpuIndex_search( /// Free the GPU index FAISS_DECLARE_DESTRUCTOR(GpuIndex) -/// Downcast a GpuIndex to GpuIndexIVF (returns NULL if not a GpuIndexIVF) -FaissGpuIndexIVF* faiss_GpuIndexIVF_cast(FaissGpuIndex* index); - #ifdef __cplusplus } #endif