#faiss #gpu #bindings #api #c-api #require #facebookresearch-faiss

faiss-next

Light weighted rust wrapper of c api of facebookresearch/faiss library

5 releases

0.2.0 Feb 2, 2024
0.1.3 Jan 30, 2024
0.1.2 Jan 23, 2024
0.1.1 Jan 23, 2024
0.1.0 Jan 23, 2024

#1032 in Machine learning

44 downloads per month

MIT license

410KB
11K SLoC

faiss-next

faiss-next is a simple rust bindings for facebookresearch/faiss. This crate is is inspired by Enet4/faiss-rs.

Windows, Linux and Macos is supported. facebookresearch/faiss v1.7.4 is wrapped currently.

WARN: test case won't give the correct result on windows with gpu enabled on a nvidia 1050 laptop with cuda11.8, don't know why yet, might be a problem of hecked source code?

faiss-next requires faiss compiled with FAISS_ENABLE_C_API=ON and BUILD_SHARED_LIBS=ON in advance. Please checkout README.md of faiss-next-sys for further info about building faiss from source.

Installation

Before linking with faiss-next, env variable FAISS_DIR should set and point to the dir faiss installed. If FAISS_DIR is not set, build.rs will search /usr or /usr/local or $HOME/faiss (%USERPROFILE%/faiss on windows) for library and include heeders by default.

[dependencies]
faiss-next = {version = "*", features = ["gpu"] }

Tutorial

use faiss_next::*;

use ndarray::{s, Array2};
use ndarray_rand::*;


fn main() {

	//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);
	}
}

Dependencies

~0.6–3MB
~59K SLoC