faiss

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 18, 2021 License: MIT Imports: 3 Imported by: 12

README

go-faiss

Go Reference

Go bindings for Faiss, a library for vector similarity search.

Install

First you will need to build and install Faiss:

git clone https://github.com/facebookresearch/faiss.git
cd faiss
cmake -B build -DFAISS_ENABLE_GPU=OFF -DFAISS_ENABLE_C_API=ON -DBUILD_SHARED_LIBS=ON .
make -C build
sudo make -C build install

Building will produce the dynamic library faiss_c. You will need to install it in a place where your system will find it (e.g. /usr/lib on Linux). You can do this with:

sudo cp build/c_api/libfaiss_c.so /usr/lib

Now you can install the Go module:

go get github.com/DataIntelligenceCrew/go-faiss

Usage

API documentation is available at https://pkg.go.dev/github.com/DataIntelligenceCrew/go-faiss. See the Faiss wiki for more information.

Examples can be found in the _example directory.

Documentation

Overview

Package faiss provides bindings to Faiss, a library for vector similarity search. More detailed documentation can be found at the Faiss wiki: https://github.com/facebookresearch/faiss/wiki.

Index

Constants

View Source
const (
	MetricInnerProduct  = C.METRIC_INNER_PRODUCT
	MetricL2            = C.METRIC_L2
	MetricL1            = C.METRIC_L1
	MetricLinf          = C.METRIC_Linf
	MetricLp            = C.METRIC_Lp
	MetricCanberra      = C.METRIC_Canberra
	MetricBrayCurtis    = C.METRIC_BrayCurtis
	MetricJensenShannon = C.METRIC_JensenShannon
)

Metric type

View Source
const (
	IOFlagMmap     = C.FAISS_IO_FLAG_MMAP
	IOFlagReadOnly = C.FAISS_IO_FLAG_READ_ONLY
)

IO flags

Variables

This section is empty.

Functions

func WriteIndex added in v0.2.0

func WriteIndex(idx Index, filename string) error

WriteIndex writes an index to a file.

Types

type IDSelector

type IDSelector struct {
	// contains filtered or unexported fields
}

IDSelector represents a set of IDs to remove.

func NewIDSelectorBatch

func NewIDSelectorBatch(indices []int64) (*IDSelector, error)

NewIDSelectorBatch creates a new batch selector.

func NewIDSelectorRange

func NewIDSelectorRange(imin, imax int64) (*IDSelector, error)

NewIDSelectorRange creates a selector that removes IDs on [imin, imax).

func (*IDSelector) Delete

func (s *IDSelector) Delete()

Delete frees the memory associated with s.

type Index

type Index interface {
	// D returns the dimension of the indexed vectors.
	D() int

	// IsTrained returns true if the index has been trained or does not require
	// training.
	IsTrained() bool

	// Ntotal returns the number of indexed vectors.
	Ntotal() int64

	// MetricType returns the metric type of the index.
	MetricType() int

	// Train trains the index on a representative set of vectors.
	Train(x []float32) error

	// Add adds vectors to the index.
	Add(x []float32) error

	// AddWithIDs is like Add, but stores xids instead of sequential IDs.
	AddWithIDs(x []float32, xids []int64) error

	// Search queries the index with the vectors in x.
	// Returns the IDs of the k nearest neighbors for each query vector and the
	// corresponding distances.
	Search(x []float32, k int64) (distances []float32, labels []int64, err error)

	// RangeSearch queries the index with the vectors in x.
	// Returns all vectors with distance < radius.
	RangeSearch(x []float32, radius float32) (*RangeSearchResult, error)

	// Reset removes all vectors from the index.
	Reset() error

	// RemoveIDs removes the vectors specified by sel from the index.
	// Returns the number of elements removed and error.
	RemoveIDs(sel *IDSelector) (int, error)

	// Delete frees the memory used by the index.
	Delete()
	// contains filtered or unexported methods
}

Index is a Faiss index.

Note that some index implementations do not support all methods. Check the Faiss wiki to see what operations an index supports.

type IndexFlat

type IndexFlat struct {
	Index
}

IndexFlat is an index that stores the full vectors and performs exhaustive search.

func NewIndexFlat

func NewIndexFlat(d int, metric int) (*IndexFlat, error)

NewIndexFlat creates a new flat index.

func NewIndexFlatIP

func NewIndexFlatIP(d int) (*IndexFlat, error)

NewIndexFlatIP creates a new flat index with the inner product metric type.

func NewIndexFlatL2

func NewIndexFlatL2(d int) (*IndexFlat, error)

NewIndexFlatL2 creates a new flat index with the L2 metric type.

func (*IndexFlat) Xb

func (idx *IndexFlat) Xb() []float32

Xb returns the index's vectors. The returned slice becomes invalid after any add or remove operation.

type IndexImpl added in v0.2.0

type IndexImpl struct {
	Index
}

IndexImpl is an abstract structure for an index.

func IndexFactory

func IndexFactory(d int, description string, metric int) (*IndexImpl, error)

IndexFactory builds a composite index. description is a comma-separated list of components.

func ReadIndex added in v0.2.0

func ReadIndex(filename string, ioflags int) (*IndexImpl, error)

ReadIndex reads an index from a file.

func (*IndexImpl) AsFlat added in v0.2.0

func (idx *IndexImpl) AsFlat() *IndexFlat

AsFlat casts idx to a flat index. AsFlat panics if idx is not a flat index.

type ParameterSpace

type ParameterSpace struct {
	// contains filtered or unexported fields
}

func NewParameterSpace

func NewParameterSpace() (*ParameterSpace, error)

NewParameterSpace creates a new ParameterSpace.

func (*ParameterSpace) Delete

func (p *ParameterSpace) Delete()

Delete frees the memory associated with p.

func (*ParameterSpace) SetIndexParameter

func (p *ParameterSpace) SetIndexParameter(idx Index, name string, val float64) error

SetIndexParameter sets one of the parameters.

type RangeSearchResult

type RangeSearchResult struct {
	// contains filtered or unexported fields
}

RangeSearchResult is the result of a range search.

func (*RangeSearchResult) Delete

func (r *RangeSearchResult) Delete()

Delete frees the memory associated with r.

func (*RangeSearchResult) Labels

func (r *RangeSearchResult) Labels() (labels []int64, distances []float32)

Labels returns the unsorted IDs and respective distances for each query. The result for query i is labels[lims[i]:lims[i+1]].

func (*RangeSearchResult) Lims

func (r *RangeSearchResult) Lims() []int

Lims returns a slice containing start and end indices for queries in the distances and labels slices returned by Labels.

func (*RangeSearchResult) Nq

func (r *RangeSearchResult) Nq() int

Nq returns the number of queries.

Directories

Path Synopsis
_example
flat
Usage example for IndexFlat.
Usage example for IndexFlat.
ivfflat
Usage example for IndexIVFFlat.
Usage example for IndexIVFFlat.
ivfpq
Usage example for IndexIVFPQ.
Usage example for IndexIVFPQ.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL