Skip to content

Instantly share code, notes, and snippets.

View Enet4's full-sized avatar

Eduardo Pinho Enet4

View GitHub Profile
@Enet4
Enet4 / std_gpu.rs
Created April 12, 2018 19:25
StandardGpuResources in faiss-rs
#[derive(Debug)]
pub struct StandardGpuResources {
inner: *mut FaissGpuResources,
}
// Deliberately _not_ Sync!
unsafe impl Send for StandardGpuResources {}
@Enet4
Enet4 / flat_index_use.rs
Created April 12, 2018 19:19
use of FlatIndex in faiss-rs
let mut index = FlatIndex::new(8, MetricType::L2)?;
let some_data = get_data();
index.add(some_data)?;
let my_query = get_query();
let result = index.assign(&my_query, 5)?;
let my_query = get_another_query();
// flat index can be used behind an immutable ref!
let result2 = (&index).assign(&my_query, 5)?;
@Enet4
Enet4 / index.rs
Created April 12, 2018 19:02
Index trait in faiss-rs
/// Interface for a Faiss index.
pub trait Index {
/// Whether the index is trained
fn is_trained(&self) -> bool;
/// The total number of vectors indexed
fn ntotal(&self) -> u64;
/// The dimensionality of the indexed vectors
fn d(&self) -> u32;
@Enet4
Enet4 / concurrent_index.rs
Created April 11, 2018 23:29
ConcurrentIndex in faiss-rs
/// Trait for a Faiss index that can be safely searched over multiple threads.
pub trait ConcurrentIndex: Index {
fn assign(&self, q: &[f32], k: usize) -> Result<AssignSearchResult>;
fn search(&self, q: &[f32], k: usize) -> Result<SearchResult>;
fn range_search(&self, q: &[f32], radius: f32) -> Result<RangeSearchResult>;
}
@Enet4
Enet4 / faiss_example.rs
Created April 11, 2018 23:23
faiss-rs example
use faiss::{Index, index_factory, MetricType};
// contiguous 8-dimensional vectors in a single slice
let my_data = &[
7.5_f32, -7.5, 7.5, -7.5, 7.5, 7.5, 7.5, 7.5,
-1., 1., 1., 1., 1., 1., 1., -1.,
0., 0., 0., 1., 1., 0., 0., -1.,
10., 10., 10., 10., -10., 10., 10.,
10., 12., 10., 10., 10.5, -10., 10., 10., 5.5,
];
@Enet4
Enet4 / gpu.rs
Created April 11, 2018 19:12
index/gpu.rs in faiss-rs
#[derive(Debug)]
pub struct GpuIndexImpl<'gpu, I> {
inner: *mut FaissGpuIndex,
/// retaining the GPU resources' lifetime,
/// plus the original index type `I`
phantom: PhantomData<(&'gpu (), I)>,
}
@Enet4
Enet4 / index.rs
Last active April 11, 2018 23:18
index.rs in faiss-rs
#[derive(Debug)]
pub struct IndexImpl {
inner: *mut FaissIndex,
}
unsafe impl Send for IndexImpl {}
unsafe impl Sync for IndexImpl {}
impl Drop for IndexImpl {
fn drop(&mut self) {
@Enet4
Enet4 / index_factory.rs
Last active April 11, 2018 19:00
index_factory at index/mod.rs in faiss-rs
/// Use the index factory to create a native instance of a Faiss index, for `d`-dimensional
/// vectors.
pub fn index_factory<D: AsRef<str>>(
d: u32,
description: D,
metric: MetricType,
) -> Result<IndexImpl> {
unsafe {
let metric = metric as c_uint;
let description = CString::new(description.as_ref()).map_err(|_| Error::IndexDescription)?;
@Enet4
Enet4 / faiss_try_macro.rs
Created April 11, 2018 18:52
faiss_try! macro at lib.rs in faiss-rs
macro_rules! faiss_try {
($e: expr) => {{
let c = $e;
if c != 0 {
return Err(::error::NativeError::from_last_error(c).into());
}
}}
}
@Enet4
Enet4 / error.rs
Last active April 11, 2018 22:46
error.rs at faiss-rs
#[derive(Debug, Clone, PartialEq)]
pub struct NativeError {
code: c_int,
msg: String,
}
impl NativeError {
pub fn from_last_error(code: c_int) -> Self {
unsafe {
let e: *const _ = faiss_get_last_error();