This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[derive(Debug)] | |
pub struct StandardGpuResources { | |
inner: *mut FaissGpuResources, | |
} | |
// Deliberately _not_ Sync! | |
unsafe impl Send for StandardGpuResources {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)?; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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>; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | |
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[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)>, | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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)?; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
macro_rules! faiss_try { | |
($e: expr) => {{ | |
let c = $e; | |
if c != 0 { | |
return Err(::error::NativeError::from_last_error(c).into()); | |
} | |
}} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[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(); |