faiss-next

Crates.iofaiss-next
lib.rsfaiss-next
version0.2.0
sourcesrc
created_at2024-01-23 12:24:59.453954
updated_at2024-02-02 15:28:35.534546
descriptionLight weighted rust wrapper of c api of facebookresearch/faiss library
homepagehttp://github.com/yexiangyu/faiss-next
repositoryhttps://github.com/yexiangyu/faiss-next
max_upload_size
id1110768
size15,595
(yexiangyu)

documentation

https://docs.rs/faiss-next

README

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);
	}
}
Commit count: 0

cargo fmt