5 个版本
0.2.0 | 2024 年 2 月 2 日 |
---|---|
0.1.3 | 2024 年 1 月 30 日 |
0.1.2 | 2024 年 1 月 23 日 |
0.1.1 | 2024 年 1 月 23 日 |
0.1.0 | 2024 年 1 月 23 日 |
#572 in 机器学习
410KB
11K SLoC
faiss-next
faiss-next
是 facebookresearch/faiss 的简单 Rust 绑定。这个包受到了 Enet4/faiss-rs 的启发。
Windows
、Linux
和 Macos
支持。目前包装了 facebookresearch/faiss
v1.7.4
。
警告: 在启用 gpu
且在搭载 nvidia 1050
显卡和 cuda11.8
的笔记本电脑上,测试用例在 windows
上不会给出正确的结果,原因尚不清楚,可能是检查过的源代码的问题?
faiss-next
需要 faiss
编译时带有 FAISS_ENABLE_C_API=ON
和 BUILD_SHARED_LIBS=ON
。有关从源代码构建 faiss
的更多信息,请参阅 faiss-next-sys
的 README.md
。
安装
在链接到 faiss-next
之前,应设置环境变量 FAISS_DIR
并将其指向已安装的 faiss
目录。如果未设置 FAISS_DIR
,则 build.rs
将默认在 /usr
或 /usr/local
或 $HOME/faiss
(%USERPROFILE%/faiss
在 windows
) 中搜索库和头文件。
[dependencies]
faiss-next = {version = "*", features = ["gpu"] }
教程
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);
}
}
依赖项
~0.6–3MB
~58K SLoC