Faiss
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
Classes | Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | List of all members
faiss::HNSW Struct Reference

Classes

struct  MinimaxHeap
 
struct  NodeDistCloser
 to sort pairs of (id, distance) from nearest to fathest or the reverse More...
 
struct  NodeDistFarther
 

Public Types

typedef int storage_idx_t
 internal storage of vectors (32 bits: this is expensive)
 
typedef Index::idx_t idx_t
 Faiss results are 64-bit.
 
typedef std::pair< float,
storage_idx_t
Node
 

Public Member Functions

void set_default_probas (int M, float levelMult)
 
void set_nb_neighbors (int level_no, int n)
 set nb of neighbors for this level (before adding anything)
 
int nb_neighbors (int layer_no) const
 nb of neighbors for this level
 
int cum_nb_neighbors (int layer_no) const
 cumumlative nb up to (and excluding) this level
 
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
 
 HNSW (int M=32)
 only mandatory parameter: nb of neighbors
 
int random_level ()
 pick a random level for a new point
 
void fill_with_random_links (size_t n)
 add n random levels to table (for debugging...)
 
void add_links_starting_from (DistanceComputer &ptdis, storage_idx_t pt_id, storage_idx_t nearest, float d_nearest, int level, omp_lock_t *locks, VisitedTable &vt)
 
void add_with_locks (DistanceComputer &ptdis, int pt_level, int pt_id, std::vector< omp_lock_t > &locks, VisitedTable &vt)
 
int search_from_candidates (DistanceComputer &qdis, int k, idx_t *I, float *D, MinimaxHeap &candidates, VisitedTable &vt, int level, int nres_in=0) const
 
std::priority_queue< Node > search_from_candidate_unbounded (const Node &node, DistanceComputer &qdis, int ef, VisitedTable *vt) const
 
void search (DistanceComputer &qdis, int k, idx_t *I, float *D, VisitedTable &vt) const
 search interface
 
void reset ()
 
void clear_neighbor_tables (int level)
 
void print_neighbor_stats (int level) const
 
int prepare_level_tab (size_t n, bool preset_levels=false)
 

Static Public Member Functions

static void shrink_neighbor_list (DistanceComputer &qdis, std::priority_queue< NodeDistFarther > &input, std::vector< NodeDistFarther > &output, int max_size)
 

Public Attributes

std::vector< double > assign_probas
 assignment probability to each layer (sum=1)
 
std::vector< int > cum_nneighbor_per_level
 
std::vector< int > levels
 level of each vector (base level = 1), size = ntotal
 
std::vector< size_t > offsets
 
std::vector< storage_idx_tneighbors
 
storage_idx_t entry_point
 entry point in the search structure (one of the points with maximum level
 
faiss::RandomGenerator rng
 
int max_level
 maximum level
 
int efConstruction
 expansion factor at construction time
 
int efSearch
 expansion factor at search time
 
bool check_relative_distance = true
 during search: do we check whether the next best distance is good enough?
 
int upper_beam
 number of entry points in levels > 0.
 
bool search_bounded_queue = true
 use bounded queue during exploration
 

Detailed Description

Definition at line 46 of file HNSW.h.

Member Function Documentation

void faiss::HNSW::add_links_starting_from ( DistanceComputer ptdis,
storage_idx_t  pt_id,
storage_idx_t  nearest,
float  d_nearest,
int  level,
omp_lock_t *  locks,
VisitedTable vt 
)

Finds neighbors and builds links with them, starting from an entry point. The own neighbor list is assumed to be locked.

Definition at line 441 of file HNSW.cpp.

void faiss::HNSW::add_with_locks ( DistanceComputer ptdis,
int  pt_level,
int  pt_id,
std::vector< omp_lock_t > &  locks,
VisitedTable vt 
)

add point pt_id on all levels <= pt_level and build the link structure for them.

Definition at line 477 of file HNSW.cpp.

int faiss::HNSW::search_from_candidates ( DistanceComputer qdis,
int  k,
idx_t I,
float *  D,
MinimaxHeap candidates,
VisitedTable vt,
int  level,
int  nres_in = 0 
) const

Do a BFS on the candidates list

Definition at line 523 of file HNSW.cpp.

void faiss::HNSW::set_default_probas ( int  M,
float  levelMult 
)

initialize the assign_probas and cum_nneighbor_per_level to have 2*M links on level 0 and M links on levels > 0

Definition at line 76 of file HNSW.cpp.

void faiss::HNSW::shrink_neighbor_list ( DistanceComputer qdis,
std::priority_queue< NodeDistFarther > &  input,
std::vector< NodeDistFarther > &  output,
int  max_size 
)
static

Enumerate vertices from farthest to nearest from query, keep a neighbor only if there is no previous neighbor that is closer to that vertex than the query.

Definition at line 235 of file HNSW.cpp.

Member Data Documentation

std::vector<int> faiss::HNSW::cum_nneighbor_per_level

number of neighbors stored per layer (cumulative), should not be changed after first add

Definition at line 103 of file HNSW.h.

std::vector<storage_idx_t> faiss::HNSW::neighbors

neighbors[offsets[i]:offsets[i+1]] is the list of neighbors of vector i for all levels. this is where all storage goes.

Definition at line 114 of file HNSW.h.

std::vector<size_t> faiss::HNSW::offsets

offsets[i] is the offset in the neighbors array where vector i is stored size ntotal + 1

Definition at line 110 of file HNSW.h.


The documentation for this struct was generated from the following files: