14 #include "IndexFlat.h"
16 #include "IndexScalarQuantizer.h"
57 virtual void set_query (
const float *x) = 0;
126 size_t * begin,
size_t * end)
const;
129 explicit HNSW(
int M = 32);
140 std::vector<omp_lock_t> & locks,
151 void clear_neighbor_tables(
int level);
152 void print_neighbor_stats(
int level)
const;
182 std::vector<float> codebook;
184 std::vector<uint8_t> codes;
189 size_t k=256,
size_t nsq=1);
193 void add_codes(
size_t n,
const float *x);
195 size_t compute_distances(
size_t n,
const idx_t *shortlist,
196 const float *query,
float *distances)
const;
199 void estimate_code(
const float *x, storage_idx_t i, uint8_t *code)
const;
202 void reconstruct(storage_idx_t i,
float *x,
float *tmp)
const;
204 void reconstruct_n(storage_idx_t n0, storage_idx_t ni,
float *x)
const;
236 void add(
idx_t n,
const float *x)
override;
243 float *distances,
idx_t *labels)
const override;
247 void reset ()
override;
249 void shrink_level_0_neighbors(
int size);
258 const storage_idx_t *nearest,
const float *nearest_d,
259 float *distances,
idx_t *labels,
int nprobe = 1,
260 int search_type = 1)
const;
264 int k,
const float *D,
const idx_t *I);
268 int npt,
const storage_idx_t *points,
269 const storage_idx_t *nearests);
272 void reorder_links();
274 void link_singletons();
318 float *distances,
idx_t *labels)
const override;
random generator that can be used in multithreaded contexts
void add_with_locks(DistanceComputer &ptdis, int pt_level, int pt_id, std::vector< omp_lock_t > &locks, VisitedTable &vt)
void neighbor_range(idx_t no, int layer_no, size_t *begin, size_t *end) const
range of entries in the neighbors table of vertex no at layer_no
void train(idx_t n, const float *x) override
Trains the storage if needed.
virtual float operator()(storage_idx_t i)=0
compute distance of vector i to current query
int nb_neighbors(int layer_no) const
nb of neighbors for this level
void search(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels) const override
entry point for search
storage_idx_t entry_point
entry point in the search structure (one of the points with maximum level
int cum_nb_neighbors(int layer_no) const
cumumlative nb up to (and excluding) this level
virtual float symmetric_dis(storage_idx_t i, storage_idx_t j)=0
compute distance between two stored vectors
std::vector< double > assign_probas
assignment probability to each layer (sum=1)
std::vector< int > cum_nneighbor_per_level
void get_neighbor_table(storage_idx_t i, float *out) const
get the M+1 -by-d table for neighbor coordinates for vector i
void train(idx_t n, const float *x) override
Trains the storage if needed.
std::vector< size_t > offsets
set implementation optimized for fast access.
void add(idx_t n, const float *x) override
void reconstruct(storage_idx_t i, float *x, float *tmp) const
called by compute_distances
int efSearch
expansion factor at search time
long idx_t
all indices are this type
virtual void set_query(const float *x)=0
called before computing distances
bool check_relative_distance
during search: do we check whether the next best distance is good enough?
HNSW(int M=32)
only mandatory parameter: nb of neighbors
int upper_beam
number of entry points in levels > 0.
void set_nb_neighbors(int level_no, int n)
set nb of neighbors for this level (before adding anything)
void estimate_code(const float *x, storage_idx_t i, uint8_t *code) const
called by add_codes
faiss::Index::idx_t idx_t
Faiss results are 64-bit.
void add_codes(size_t n, const float *x)
int random_level()
pick a random level for a new point
void set_default_probas(int M, float levelMult)
void reset() override
removes all elements from the database.
void search(DistanceComputer &qdis, int k, idx_t *I, float *D, VisitedTable &vt) const
search interface
void init_level_0_from_entry_points(int npt, const storage_idx_t *points, const storage_idx_t *nearests)
alternative graph building
void search(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels) const override
entry point for search
void init_level_0_from_knngraph(int k, const float *D, const idx_t *I)
alternative graph building
void search_level_0(idx_t n, const float *x, idx_t k, const storage_idx_t *nearest, const float *nearest_d, float *distances, idx_t *labels, int nprobe=1, int search_type=1) const
void fill_with_random_links(size_t n)
add n random levels to table (for debugging...)
std::vector< storage_idx_t > neighbors
void reconstruct(idx_t key, float *recons) const override
int efConstruction
expansion factor at construction time
int storage_idx_t
internal storage of vectors (32 bits: this is expensive)
std::vector< int > levels
level of each vector (base level = 1), size = ntotal
int max_level
maximum level