Summary:
When I reconstruct with by_residual turned off, the distance was greatly increased.
This is because the reconstruct_from_offset function did not check if the by_residual option was off.
I fix this bug with simple if statement.
(like this https://github.com/facebookresearch/faiss/blob/main/faiss/IndexIVFPQ.cpp#L365)
Pull Request resolved: https://github.com/facebookresearch/faiss/pull/2298
Reviewed By: alexanderguzhva
Differential Revision: D35746566
Pulled By: mdouze
fbshipit-source-id: 50f98c7cc97c7936507573fe41b65a79ecdbc4ca
Summary:
Pull Request resolved: https://github.com/facebookresearch/faiss/pull/2295
Makes a common ancestor for quantizer classes.
As a first application, adds a clone_Quantizer function
Reviewed By: alexanderguzhva
Differential Revision: D35561960
fbshipit-source-id: 896a4f3fc4ab992511cdc0642689a440f170f683
Summary:
Start migration of existing benchmarks to Google's Benchmark library + register benchmark to servicelab.
The benchmark should be automatically registered to servicelab once this diff lands according to https://www.internalfb.com/intern/wiki/ServiceLab/Use_Cases/Benchmarks_(C++)/#servicelab-job.
Reviewed By: mdouze
Differential Revision: D35397782
fbshipit-source-id: 317db2527f12ddde0631cacc3085c634afdd0e37
Summary:
Pull Request resolved: https://github.com/facebookresearch/faiss/pull/2274
All input matrices needed to be of the correct type and to be C-contiguous. This diff passes the main entry points of the api through `np.ascontiguousarray` so that the function parameters are transparently converted to the suitable format if needed.
We did not have this before because users need to be made aware of the performance impact, but it seems that maybe usability is more useful.
This diff is an alternative to
D35007365
https://github.com/facebookresearch/faiss/pull/2250
Reviewed By: beauby
Differential Revision: D35009612
fbshipit-source-id: fa0d5cfdfbff6b0916d47bd33c620e3ca9d5dd40
Summary:
Pull Request resolved: https://github.com/facebookresearch/faiss/pull/2280
Add a new function call fvec_L2sqr_ny_nearest and a demonstration of its implementation for 4 bits
Reviewed By: mdouze
Differential Revision: D35189945
fbshipit-source-id: d1b2ba42851df195123c7e318a8dcf26f775eaba
Summary:
Pull Request resolved: https://github.com/facebookresearch/faiss/pull/2277
* extend a specialized AVX2 version for IVFPQScannerT::scan_list_with_table to cover IVFPQScannerT::scan_list_polysemous_hc as well
* lower the comparison precision in test_lowlevel_ivf tests from EXPECT_EQ to EXPECT-FLOAT_EQ because of the AVX2 change in IVFPQScannerT::scan_list_polysemous_hc, otherwise tests fail
Reviewed By: mdouze
Differential Revision: D34964138
fbshipit-source-id: 1d304a8f6eda040fa4c626676b4d492f2c12f04f
Summary:
Pull Request resolved: https://github.com/facebookresearch/faiss/pull/2255
The `DistanceComputer` object is derived from an Index (obtained with `get_distance_computer()`). It maintains a current query and quickly computes distances from that query to any item in the database. This is useful, eg. for the IndexHNSW and IndexNSG that rely on query-to-point comparisons in the datasets.
This diff introduces the `FlatCodesDistanceComputer`, that inherits from `DistanceComputer` for Flat indexes. In addition to the distance-to-item function, it adds a `distance_to_code` that computes the distance from any code to the current query, even if it is not stored in the index.
This is implemented for all FlatCode indexes (IndexFlat, IndexPQ, IndexScalarQuantizer and IndexAdditiveQuantizer).
In the process, the two classes were extracted to their own header file `impl/DistanceComputer.h`
Reviewed By: beauby
Differential Revision: D34863609
fbshipit-source-id: 39d8c66475e55c3223c4a6a210827aa48bca292d
Summary:
Pull Request resolved: https://github.com/facebookresearch/faiss/pull/2251
the fast_scan and fast_scan_ivf tests are irregularly timing out on the FB test infra
This diff:
- breaks down more tests into sub-tests
- makes tests cheaper by reducing the test dataset sizes
- corrects a nasty local variable binding bug that prevented all cases of `implem` to be covered.
I also tried to fix the polysemous tests that also timeout but I could not reproduce the timeout.
https://www.internalfb.com/intern/test/562949978542309?ref_report_id=0
Reviewed By: beauby
Differential Revision: D34852254
fbshipit-source-id: b005ffb3723e7d9df75516a539540d9165249cea
Summary:
Pull Request resolved: https://github.com/facebookresearch/faiss/pull/2263
Pull Request resolved: https://github.com/facebookresearch/faiss/pull/2258
GPU IVF indices could not be properly deserialized or copied from CPU then added to after the fact, this resulted in the following C++ assertion:
```
Faiss assertion 'indices->numVecs == oldNumVecs' failed in int faiss::gpu::IVFBase::addVectors(faiss::gpu::Tensor<float, 2, true>&, faiss::gpu::Tensor<long int, 1, true>&) at faiss/gpu/impl/IVFBase.cu:581
```
as the count of vectors present was not updated properly everywhere, as discovered internally by vtantia.
This diff fixes this issue by properly updating the count, as well as cleaning up stream usage in the IVF code. The problem is that the code was previously using `thrust::device_vector` which does not have a means to control on which stream copies or other work is performed. This is fixed by replacing all usage of `thrust::device_vector` with our own `DeviceVector` which was already used to store IVF data but not metadata. `DeviceVector` provides sufficient control over the proper CUDA stream usage.
Reviewed By: vtantia, mdouze
Differential Revision: D34886859
fbshipit-source-id: 70577bb386ff7dc0f4443ec4562d3ee80afc24e3
Summary:
Pull Request resolved: https://github.com/facebookresearch/faiss/pull/2245
This changeset makes the `heap_replace_top()` function of the FAISS heap implementation break distance ties by the element's ID, according to the heap's min/max property.
Reviewed By: mdouze
Differential Revision: D34669542
fbshipit-source-id: 0db24fd12442eedeee917fbb3e811ba4a070ce0f
Summary:
If the number of dimensions per sub-quantizer is not in the specialized list, it falls back to the generalized batch GEMM implementation.
When I implemented this, I had in a d2h copy so I could look at the computed distances. I removed the debugging code but not this copy.
Prior to this, PQ16 on 1024 dims was 6x slower than PQ32. Now, it is only 1.5x slower (it is slower because there is a higher number of dims per sub-q, despite there being more sub-qs).
Reviewed By: beauby
Differential Revision: D34526043
fbshipit-source-id: de6f70f0f0b91608eb6ae2a05da2af812546e4bc
Summary:
Pull Request resolved: https://github.com/facebookresearch/faiss/pull/2217
This diff introduces a new Faiss contrib module that contains:
- generic k-means implemented in python (was in distributed_ondisk)
- the two-level clustering code, including a simple function that runs it on a Faiss IVF index.
- sparse clustering code (new)
The main idea is that that code is often re-used so better have it in contrib.
Reviewed By: beauby
Differential Revision: D34170932
fbshipit-source-id: cc297cc56d241b5ef421500ed410d8e2be0f1b77
Summary:
Fix an OMP bug and a memory leakage bug. The first one would lead to non-deterministic results and even worse.
Pull Request resolved: https://github.com/facebookresearch/faiss/pull/2168
Test Plan: buck test //faiss/tests/:test_lsq -- test_deterministic
Reviewed By: beauby
Differential Revision: D33975589
Pulled By: mdouze
fbshipit-source-id: c1cf2589b0e718354ccf0221c3474633bcb8c7ee
Summary:
As discussed in https://github.com/facebookresearch/faiss/issues/2072, here is a PR to use InvertedListScanner with Python. It might be slower but it gives access to this features in Python for those who want to avoid C++.
Pull Request resolved: https://github.com/facebookresearch/faiss/pull/2200
Test Plan: check that it compiles
Reviewed By: beauby
Differential Revision: D33975686
Pulled By: mdouze
fbshipit-source-id: dd731f90ce1609d555a17551fcc8c39eadf3fbd7
Summary:
Pull Request resolved: https://github.com/facebookresearch/faiss/pull/2166
RQ training is done progressively from one quantizer to the next, maintaining a current set of codes and quantization centroids.
However, for RQ as for any additive quantizer, there is a closed form solution for the centroids that minimizes the quantization error for fixed codes.
This diff offers the option to estimate that codebook at the end of the optimization. It performs this estimation iteratively, ie. several rounds of code computation - codebook refinement are performed.
A pure python implementation + results is here:
https://github.com/fairinternal/faiss_improvements/blob/dbcc746/decoder/refine_aq_codebook.ipynb
Reviewed By: wickedfoo
Differential Revision: D33309409
fbshipit-source-id: 55c13425292e73a1b05f00e90f4dcfdc8b3549e8
Summary:
Pull Request resolved: https://github.com/facebookresearch/faiss/pull/2147
There was a bug in the OPQ string parsing. This diff adds a test and fixes the error.
Reviewed By: aijanai
Differential Revision: D33020167
fbshipit-source-id: 32e43653849b258a3b6d0cfdc44a6c637433f2c8
Summary:
A recent CUDA driver is required for building packages for CUDA 11.3.
Pull Request resolved: https://github.com/facebookresearch/faiss/pull/2146
Reviewed By: wickedfoo
Differential Revision: D33020204
Pulled By: beauby
fbshipit-source-id: 01257b1dcb4987f4866cc058c22d1dd5977d76ce
Summary:
- Disable problematic tests on OSX.
- Ensure compiler compatibility with CUDA builds.
- Fix path for Python extension libraries.
- Use CentOS for CUDA packaging.
- Update CUDA versions in CI (10.2 and 11.3).
Pull Request resolved: https://github.com/facebookresearch/faiss/pull/2121
Reviewed By: mdouze
Differential Revision: D32921117
Pulled By: beauby
fbshipit-source-id: 588c18add8084b8228ff5abc651eaa4567919cc6
Summary:
IndexHNSW has a deadlock in the add() method, which is fixed by
temporarily releasing the lock on the current element while updating
its neighbors' adjacency lists.
This bug concerns multi-threaded insertion only, and seems to manifest
itself only with certain OpenMP configurations.
Pull Request resolved: https://github.com/facebookresearch/faiss/pull/2143
Reviewed By: mdouze
Differential Revision: D32919041
Pulled By: beauby
fbshipit-source-id: e515541c1b22bfcb79d29c0bde1843e63f5175fb
Summary:
Pull Request resolved: https://github.com/facebookresearch/faiss/pull/2134
The old parsing was very complex and grew out of hand.
this diff just uses regex parsing.
Reviewed By: wickedfoo
Differential Revision: D32759110
fbshipit-source-id: 243029bba8a7fe70c71323f5edc7e2ce4e669757
Summary:
Pull Request resolved: https://github.com/facebookresearch/faiss/pull/2132
This diff adds the class IndexFlatCodes that becomes the parent of all "flat" encodings.
IndexPQ
IndexFlat
IndexAdditiveQuantizer
IndexScalarQuantizer
IndexLSH
Index2Layer
The other changes are:
- for IndexFlat, there is no vector<float> with the data anymore. It is replaced with a `get_xb()` function. This broke quite a few external codes, that this diff also attempts to fix.
- I/O functions needed to be adapted. This is done without changing the I/O format for any index.
- added a small contrib function to get the data from the IndexFlat
- the functionality has been made uniform, for example remove_ids and add are now in the parent class.
Eventually, we may support generic storage for flat indexes, similar to `InvertedLists`, eg to memmap the data, but this will again require a big change.
Reviewed By: wickedfoo
Differential Revision: D32646769
fbshipit-source-id: 04a1659173fd51b130ae45d345176b72183cae40
Summary:
It looks like loading FAISS codecs which have the flag set fails in dev mode since it builds in ASAN and nothing in the code checks for that.
The error is not present in opt mode, however the sandcastle test fails
For best practice, this diff introduces combination of train_type_t flags as enums
Reviewed By: mdouze
Differential Revision: D32746904
fbshipit-source-id: f20820350e0b07b35e04c965dee01b790194e6f3
Summary:
Pull Request resolved: https://github.com/facebookresearch/faiss/pull/2123
One of the encodings used by LCC is based on a RCQ coarse quantizer and a "payload" of ITQ. The codes are compared with Hamming distances.
The index type `IndexIVFSpectralHash` can be re-purposed to perfrorm this type of index.
This diff contains a small demo demo_rcq_itq script in python to show how:
* the RCQ + ITQ are trained
* the RCQ + ITQ index add and search work (with a very inefficient python implementation)
* they can be transferred to an `IndexIVFSpectralHash`
* the python implementation and `IndexIVFSpectralHash` give the same results
The advantage of using to an `IndexIVFSpectralHash` is that in C++ it offers an `InvertedListScanner` object that can be used to compute query to code distances with its `distance_to_code` method. This is generic and will generalize to other types of encodings and coarse quantizers.
What is missing is an index_factory to make instanciation easier.
Reviewed By: sc268
Differential Revision: D32642900
fbshipit-source-id: 284f3029d239b7946bbca44a748def4e058489bd
Summary:
Pull Request resolved: https://github.com/facebookresearch/faiss/pull/2117
This supports 2 concatenated codecs. It is based on IndexRefine, that already does this but did not have a standalone codec interface.
The main use case for now is a residual quantizer + ITQ.
The test below demonstrates how to instantiate that.
The advantage is that the index_factory parser already exists.
The IndexRefine decoder just uses the second index decoder, that is supposed to be more accurate than the first.
Reviewed By: beauby
Differential Revision: D32569997
fbshipit-source-id: 3fe9cd02eaa7d1cfe23b0f1168cc034821f1c362
Summary:
The results returned by `NSG::search` are already sorted. Calling `maxheap_reorder` will make the results unorder.
Fixed https://github.com/facebookresearch/faiss/issues/2081.
Pull Request resolved: https://github.com/facebookresearch/faiss/pull/2086
Test Plan: buck test //faiss/tests/:test_index -- test_order
Reviewed By: beauby
Differential Revision: D32593924
Pulled By: mdouze
fbshipit-source-id: 794b94681610657bd2f305f7e3d6cd5d25c6bdba
Summary:
PCA whitening implies to multiply eigenvectors with 1/sqrt(singular values of convariance matrix). The singular values are sometimes 0 (because the vector subspace is not full-rank) or negative (because of numerical issues).
Therefore, this diff adds an epsilon to the denominator above (default 0).
Reviewed By: edpizzi
Differential Revision: D31725075
fbshipit-source-id: dae68bda9f7452220785d76e30ce4b2ac8582413
Summary:
This diff implemented non-uniform quantization of vector norms in additive quantizers. index_factory and I/O are supported.
index_factory: `XXX_Ncqint{nbits}` where `nbits` is the number of bits to quantize vector norm.
For 8 bits code, it is almost the same as 8-bit uniform quantization. It will slightly improve the accuracy if the code size is less than 8 bits.
```
RQ4x8_Nqint8: R@1 0.1116
RQ4x8_Ncqint8: R@1 0.1117
RQ4x8_Nqint4: R@1 0.0901
RQ4x8_Ncqint4: R@1 0.0989
```
Pull Request resolved: https://github.com/facebookresearch/faiss/pull/2037
Test Plan:
buck test //faiss/tests/:test_clustering -- TestClustering1D
buck test //faiss/tests/:test_lsq -- test_index_accuracy_cqint
buck test //faiss/tests/:test_residual_quantizer -- test_norm_cqint
buck test //faiss/tests/:test_residual_quantizer -- test_search_L2
Reviewed By: beauby
Differential Revision: D31083476
Pulled By: mdouze
fbshipit-source-id: f34c3dafc4eb1c6f44a63e68137158911aa4a2f4
Summary:
Following up on issue https://github.com/facebookresearch/faiss/issues/2054 it seems that this code crashes Faiss (instead of just leaking memory).
Findings:
- when running in MT mode, each search in an indexflat used as coarse quantizer consumes some memory
- this mem consumption does not appear in single-thread mode or with few threads
- in gdb it appears that even when the nb of queries is 1, each search spawns max_threads threads (80 on the test machine)
This diff:
- adds a C++ test that checks how much mem is used when repeatedly searching a vector
- adjusts the number of search threads to the number of query vectors. This is especially useful for single-vector queries.
Reviewed By: beauby
Differential Revision: D31142383
fbshipit-source-id: 134ddaf141e7c52a854cea398f5dbf89951a7ff8
Summary:
I am correcting a minor typo in the error checking.
Pull Request resolved: https://github.com/facebookresearch/faiss/pull/2055
Reviewed By: beauby, wickedfoo
Differential Revision: D31052509
Pulled By: mdouze
fbshipit-source-id: 36063107503afa5c37a98a0ae0d62df8bc8832e8