Crate faiss_next

source ·
Expand description

§Faiss

faiss is a light weight rust wrapper for facebookresearch/faiss c api. Quick example:

use faiss_next::{index_factory, FaissMetricType, Index};
use ndarray::{s, Array2};
use ndarray_rand::*;

//create index
let mut index = index_factory(128, "Flat", FaissMetricType::METRIC_L2).expect("failed to create cpu index");

//create some random feature
let feats = Array2::random((1024, 128), rand::distributions::Uniform::new(0., 1.));

//get query from position 42
let query = feats.slice(s![42..43, ..]);

//add features in index
index.add(feats.as_slice_memory_order().unwrap()).expect("failed to add feature");

//do the search
let ret = index.search(query.as_slice_memory_order().unwrap(), 1).expect("failed to search");
assert_eq!(ret.labels[0], 42i64);

//move index from cpu to gpu, only available when gpu feature is enabled
#[cfg(feature = "gpu")]
{
let index = index.into_gpu(0).expect("failed to move index to gpu");
let ret = index.search(query.as_slice_memory_order().unwrap(), 1).expect("failed to search");
assert_eq!(ret.labels[0], 42i64);
}

Structs§

Enums§

Traits§

  • Index trait, all index should implement this trait

Functions§

  • helper function to create cpu index, please refer to doc for details of description and metric

Type Aliases§