Skip to content

Commit

Permalink
merge upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
tarang-jain committed Nov 21, 2024
2 parents 3201ee6 + eaab46c commit 096a826
Show file tree
Hide file tree
Showing 78 changed files with 928 additions and 173 deletions.
2 changes: 0 additions & 2 deletions benchs/bench_cppcontrib_sa_decode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
#include <faiss/Index2Layer.h>
#include <faiss/IndexIVFPQ.h>
#include <faiss/IndexPQ.h>
#include <faiss/impl/io.h>
#include <faiss/index_factory.h>
#include <faiss/index_io.h>

#include <faiss/IndexRowwiseMinMax.h>
#include <faiss/cppcontrib/SaDecodeKernels.h>
Expand Down
13 changes: 8 additions & 5 deletions benchs/bench_fw/descriptors.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ class CodecDescriptor(IndexBaseDescriptor):
factory: Optional[str] = None
construction_params: Optional[List[Dict[str, int]]] = None
training_vectors: Optional[DatasetDescriptor] = None
FILENAME_PREFIX: str = "xt"

def __post_init__(self):
self.get_name()
Expand Down Expand Up @@ -254,7 +255,7 @@ def name_from_factory(self) -> str:
name += f"d_{self.d}.{self.metric.upper()}."
if self.factory != "Flat":
assert self.training_vectors is not None
name += self.training_vectors.get_filename("xt")
name += self.training_vectors.get_filename(CodecDescriptor.FILENAME_PREFIX)
name += IndexBaseDescriptor.param_dict_list_to_name(self.construction_params)
return name

Expand All @@ -278,6 +279,7 @@ def alias(self, benchmark_io: BenchmarkIO):
class IndexDescriptor(IndexBaseDescriptor):
codec_desc: Optional[CodecDescriptor] = None
database_desc: Optional[DatasetDescriptor] = None
FILENAME_PREFIX: str = "xb"

def __hash__(self):
return hash(str(self))
Expand All @@ -290,14 +292,14 @@ def is_built(self):

def get_name(self) -> str:
if self.desc_name is None:
self.desc_name = self.codec_desc.get_name() + self.database_desc.get_filename(prefix="xb")
self.desc_name = self.codec_desc.get_name() + self.database_desc.get_filename(prefix=IndexDescriptor.FILENAME_PREFIX)

return self.desc_name

def flat_name(self):
if self.flat_desc_name is not None:
return self.flat_desc_name
self.flat_desc_name = self.codec_desc.flat_name() + self.database_desc.get_filename(prefix="xb")
self.flat_desc_name = self.codec_desc.flat_name() + self.database_desc.get_filename(prefix=IndexDescriptor.FILENAME_PREFIX)
return self.flat_desc_name

# alias is used to refer when index is uploaded to blobstore and refered again
Expand All @@ -313,6 +315,7 @@ class KnnDescriptor(IndexBaseDescriptor):
query_dataset: Optional[DatasetDescriptor] = None
search_params: Optional[Dict[str, int]] = None
reconstruct: bool = False
FILENAME_PREFIX: str = "q"
# range metric definitions
# key: name
# value: one of the following:
Expand Down Expand Up @@ -340,7 +343,7 @@ def __hash__(self):
def get_name(self):
name = self.index_desc.get_name()
name += IndexBaseDescriptor.param_dict_to_name(self.search_params)
name += self.query_dataset.get_filename("q")
name += self.query_dataset.get_filename(KnnDescriptor.FILENAME_PREFIX)
name += f"k_{self.k}."
name += f"t_{self.num_threads}."
if self.reconstruct:
Expand All @@ -353,7 +356,7 @@ def flat_name(self):
if self.flat_desc_name is not None:
return self.flat_desc_name
name = self.index_desc.flat_name()
name += self.query_dataset.get_filename("q")
name += self.query_dataset.get_filename(KnnDescriptor.FILENAME_PREFIX)
name += f"k_{self.k}."
name += f"t_{self.num_threads}."
if self.reconstruct:
Expand Down
1 change: 0 additions & 1 deletion benchs/bench_hamming_computer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* LICENSE file in the root directory of this source tree.
*/

#include <omp.h>
#include <cstdio>
#include <vector>

Expand Down
1 change: 0 additions & 1 deletion benchs/bench_heap_replace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* LICENSE file in the root directory of this source tree.
*/

#include <omp.h>
#include <cstdio>

#include <faiss/impl/FaissAssert.h>
Expand Down
17 changes: 15 additions & 2 deletions contrib/torch_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ def swig_ptr_from_FloatTensor(x):
return faiss.cast_integer_to_float_ptr(
x.untyped_storage().data_ptr() + x.storage_offset() * 4)

def swig_ptr_from_BFloat16Tensor(x):
""" gets a Faiss SWIG pointer from a pytorch tensor (on CPU or GPU) """
assert x.is_contiguous()
assert x.dtype == torch.bfloat16
return faiss.cast_integer_to_void_ptr(
x.untyped_storage().data_ptr() + x.storage_offset() * 2)


def swig_ptr_from_IntTensor(x):
""" gets a Faiss SWIG pointer from a pytorch tensor (on CPU or GPU) """
Expand Down Expand Up @@ -606,8 +613,11 @@ def torch_replacement_knn_gpu(res, xq, xb, k, D=None, I=None, metric=faiss.METRI
elif xb.dtype == torch.float16:
xb_type = faiss.DistanceDataType_F16
xb_ptr = swig_ptr_from_HalfTensor(xb)
elif xb.dtype == torch.bfloat16:
xb_type = faiss.DistanceDataType_BF16
xb_ptr = swig_ptr_from_BFloat16Tensor(xb)
else:
raise TypeError('xb must be f32 or f16')
raise TypeError('xq must be float32, float16 or bfloat16')

nq, d2 = xq.size()
assert d2 == d
Expand All @@ -625,8 +635,11 @@ def torch_replacement_knn_gpu(res, xq, xb, k, D=None, I=None, metric=faiss.METRI
elif xq.dtype == torch.float16:
xq_type = faiss.DistanceDataType_F16
xq_ptr = swig_ptr_from_HalfTensor(xq)
elif xq.dtype == torch.bfloat16:
xq_type = faiss.DistanceDataType_BF16
xq_ptr = swig_ptr_from_BFloat16Tensor(xq)
else:
raise TypeError('xq must be f32 or f16')
raise TypeError('xq must be float32, float16 or bfloat16')

if D is None:
D = torch.empty(nq, k, device=xb.device, dtype=torch.float32)
Expand Down
1 change: 0 additions & 1 deletion demos/demo_imi_flat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include <faiss/IndexFlat.h>
#include <faiss/IndexIVFFlat.h>
#include <faiss/IndexPQ.h>
#include <faiss/index_io.h>

double elapsed() {
struct timeval tv;
Expand Down
2 changes: 0 additions & 2 deletions demos/demo_sift1M.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
#include <cstring>

#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include <sys/time.h>

Expand Down
9 changes: 9 additions & 0 deletions faiss/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ set(FAISS_HEADERS
IndexIVFPQR.h
IndexIVFSpectralHash.h
IndexLSH.h
IndexNeuralNetCodec.h
IndexLattice.h
IndexNNDescent.h
IndexNSG.h
Expand Down Expand Up @@ -168,6 +169,7 @@ set(FAISS_HEADERS
impl/ScalarQuantizer.h
impl/ThreadedIndex-inl.h
impl/ThreadedIndex.h
impl/index_read_utils.h
impl/io.h
impl/io_macros.h
impl/kmeans1d.h
Expand All @@ -179,13 +181,17 @@ set(FAISS_HEADERS
impl/code_distance/code_distance.h
impl/code_distance/code_distance-generic.h
impl/code_distance/code_distance-avx2.h
impl/code_distance/code_distance-avx512.h
impl/code_distance/code_distance-sve.h
invlists/BlockInvertedLists.h
invlists/DirectMap.h
invlists/InvertedLists.h
invlists/InvertedListsIOHook.h
invlists/OnDiskInvertedLists.h
utils/AlignedTable.h
utils/bf16.h
utils/Heap.h
utils/NeuralNet.h
utils/WorkerThread.h
utils/distances.h
utils/extra_distances-inl.h
Expand All @@ -204,8 +210,10 @@ set(FAISS_HEADERS
utils/sorting.h
utils/simdlib.h
utils/simdlib_avx2.h
utils/simdlib_avx512.h
utils/simdlib_emulated.h
utils/simdlib_neon.h
utils/simdlib_ppc64.h
utils/utils.h
utils/distances_fused/avx512.h
utils/distances_fused/distances_fused.h
Expand All @@ -216,6 +224,7 @@ set(FAISS_HEADERS
utils/approx_topk/mode.h
utils/approx_topk_hamming/approx_topk_hamming.h
utils/transpose/transpose-avx2-inl.h
utils/transpose/transpose-avx512-inl.h
utils/hamming_distance/common.h
utils/hamming_distance/generic-inl.h
utils/hamming_distance/hamdis-inl.h
Expand Down
1 change: 0 additions & 1 deletion faiss/IndexAdditiveQuantizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include <faiss/impl/ResultHandler.h>
#include <faiss/utils/distances.h>
#include <faiss/utils/extra_distances.h>
#include <faiss/utils/utils.h>

namespace faiss {

Expand Down
3 changes: 0 additions & 3 deletions faiss/IndexAdditiveQuantizerFastScan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@
#include <faiss/IndexAdditiveQuantizerFastScan.h>

#include <cassert>
#include <climits>
#include <memory>

#include <omp.h>

#include <faiss/impl/FaissAssert.h>
#include <faiss/impl/LocalSearchQuantizer.h>
#include <faiss/impl/LookupTableScaler.h>
Expand Down
1 change: 0 additions & 1 deletion faiss/IndexBinaryFlat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include <faiss/impl/IDSelector.h>
#include <faiss/utils/Heap.h>
#include <faiss/utils/hamming.h>
#include <faiss/utils/utils.h>
#include <cstring>

namespace faiss {
Expand Down
5 changes: 0 additions & 5 deletions faiss/IndexBinaryHNSW.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@
#include <cstring>
#include <memory>

#include <queue>
#include <unordered_set>

#include <sys/stat.h>
#include <sys/types.h>
#include <cstdint>

#include <faiss/IndexBinaryFlat.h>
Expand Down
1 change: 0 additions & 1 deletion faiss/IndexBinaryHash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

#include <faiss/impl/AuxIndexStructures.h>
#include <faiss/impl/FaissAssert.h>
#include <faiss/impl/platform_macros.h>

namespace faiss {

Expand Down
4 changes: 0 additions & 4 deletions faiss/IndexFastScan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@
#include <faiss/impl/IDSelector.h>
#include <faiss/impl/LookupTableScaler.h>
#include <faiss/impl/ResultHandler.h>
#include <faiss/utils/distances.h>
#include <faiss/utils/extra_distances.h>
#include <faiss/utils/hamming.h>
#include <faiss/utils/random.h>
#include <faiss/utils/utils.h>

#include <faiss/impl/pq4_fast_scan.h>
#include <faiss/impl/simd_result_handlers.h>
Expand Down
1 change: 0 additions & 1 deletion faiss/IndexFlat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include <faiss/utils/extra_distances.h>
#include <faiss/utils/prefetch.h>
#include <faiss/utils/sorting.h>
#include <faiss/utils/utils.h>
#include <cstring>

namespace faiss {
Expand Down
4 changes: 0 additions & 4 deletions faiss/IndexHNSW.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@
#include <memory>
#include <queue>
#include <random>
#include <unordered_set>

#include <sys/stat.h>
#include <sys/types.h>
#include <cstdint>

#include <faiss/Index2Layer.h>
Expand All @@ -31,7 +28,6 @@
#include <faiss/impl/AuxIndexStructures.h>
#include <faiss/impl/FaissAssert.h>
#include <faiss/impl/ResultHandler.h>
#include <faiss/utils/distances.h>
#include <faiss/utils/random.h>
#include <faiss/utils/sorting.h>

Expand Down
1 change: 0 additions & 1 deletion faiss/IndexIDMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include <cinttypes>
#include <cstdint>
#include <cstdio>
#include <limits>

#include <faiss/impl/AuxIndexStructures.h>
#include <faiss/impl/FaissAssert.h>
Expand Down
1 change: 0 additions & 1 deletion faiss/IndexIVF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include <cinttypes>
#include <cstdio>
#include <limits>
#include <memory>

#include <faiss/utils/hamming.h>
#include <faiss/utils/utils.h>
Expand Down
1 change: 0 additions & 1 deletion faiss/IndexIVFAdditiveQuantizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include <faiss/impl/ResultHandler.h>
#include <faiss/utils/distances.h>
#include <faiss/utils/extra_distances.h>
#include <faiss/utils/utils.h>

namespace faiss {

Expand Down
3 changes: 0 additions & 3 deletions faiss/IndexIVFAdditiveQuantizerFastScan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
#include <cinttypes>
#include <cstdio>

#include <omp.h>

#include <memory>

#include <faiss/impl/AuxIndexStructures.h>
Expand All @@ -23,7 +21,6 @@
#include <faiss/utils/distances.h>
#include <faiss/utils/hamming.h>
#include <faiss/utils/quantize_lut.h>
#include <faiss/utils/simdlib.h>
#include <faiss/utils/utils.h>

namespace faiss {
Expand Down
1 change: 0 additions & 1 deletion faiss/IndexIVFFastScan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include <faiss/impl/pq4_fast_scan.h>
#include <faiss/impl/simd_result_handlers.h>
#include <faiss/invlists/BlockInvertedLists.h>
#include <faiss/utils/distances.h>
#include <faiss/utils/hamming.h>
#include <faiss/utils/quantize_lut.h>
#include <faiss/utils/utils.h>
Expand Down
1 change: 0 additions & 1 deletion faiss/IndexIVFPQ.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include <faiss/utils/utils.h>

#include <faiss/Clustering.h>
#include <faiss/IndexFlat.h>

#include <faiss/utils/hamming.h>

Expand Down
4 changes: 0 additions & 4 deletions faiss/IndexIVFPQFastScan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,17 @@
#include <cinttypes>
#include <cstdio>

#include <omp.h>

#include <memory>

#include <faiss/impl/AuxIndexStructures.h>
#include <faiss/impl/FaissAssert.h>
#include <faiss/utils/distances.h>
#include <faiss/utils/simdlib.h>
#include <faiss/utils/utils.h>

#include <faiss/invlists/BlockInvertedLists.h>

#include <faiss/impl/pq4_fast_scan.h>
#include <faiss/impl/simd_result_handlers.h>
#include <faiss/utils/quantize_lut.h>

namespace faiss {

Expand Down
1 change: 0 additions & 1 deletion faiss/IndexIVFSpectralHash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include <faiss/impl/AuxIndexStructures.h>
#include <faiss/impl/FaissAssert.h>
#include <faiss/utils/hamming.h>
#include <faiss/utils/utils.h>

namespace faiss {

Expand Down
1 change: 0 additions & 1 deletion faiss/IndexLSH.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

#include <faiss/impl/FaissAssert.h>
#include <faiss/utils/hamming.h>
#include <faiss/utils/utils.h>

namespace faiss {

Expand Down
Loading

0 comments on commit 096a826

Please sign in to comment.