Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve RefineFlatIndex #60

Merged
merged 6 commits into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/index/flat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,14 @@ impl TryFromInnerPtr for FlatIndexImpl {

impl_native_index!(FlatIndex);

impl TryClone for FlatIndexImpl {}
impl TryClone for FlatIndexImpl {
fn try_clone(&self) -> Result<Self>
where
Self: Sized,
{
try_clone_from_inner_ptr(self)
}
}

impl_concurrent_index!(FlatIndexImpl);

Expand Down
11 changes: 9 additions & 2 deletions src/index/ivf_flat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::*;
use crate::error::Result;
use crate::faiss_try;
use std::mem;
use std::os::raw::{c_int, c_char};
use std::os::raw::{c_char, c_int};
use std::ptr;

/// Alias for the native implementation of a flat index.
Expand Down Expand Up @@ -142,7 +142,14 @@ impl FromInnerPtr for IVFFlatIndexImpl {

impl_native_index!(IVFFlatIndex);

impl TryClone for IVFFlatIndexImpl {}
impl TryClone for IVFFlatIndexImpl {
fn try_clone(&self) -> Result<Self>
where
Self: Sized,
{
try_clone_from_inner_ptr(self)
}
}

impl_concurrent_index!(IVFFlatIndexImpl);

Expand Down
13 changes: 10 additions & 3 deletions src/index/lsh.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! Interface and implementation to Locality-Sensitive Hashing (LSH) index type.

use super::{
AssignSearchResult, CpuIndex, FromInnerPtr, Idx, Index, IndexImpl, NativeIndex,
RangeSearchResult, SearchResult, TryClone, TryFromInnerPtr,
try_clone_from_inner_ptr, AssignSearchResult, CpuIndex, FromInnerPtr, Idx, Index, IndexImpl,
NativeIndex, RangeSearchResult, SearchResult, TryClone, TryFromInnerPtr,
};
use crate::error::{Error, Result};
use crate::faiss_try;
Expand Down Expand Up @@ -113,7 +113,14 @@ impl LshIndex {

impl_native_index!(LshIndex);

impl TryClone for LshIndex {}
impl TryClone for LshIndex {
fn try_clone(&self) -> Result<Self>
where
Self: Sized,
{
try_clone_from_inner_ptr(self)
}
}

impl IndexImpl {
/// Attempt a dynamic cast of an index to the LSH index type.
Expand Down
29 changes: 20 additions & 9 deletions src/index/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ pub trait TryFromInnerPtr: NativeIndex {
}

/// A trait which provides a Clone implementation to native index types.
pub trait TryClone: FromInnerPtr {
pub trait TryClone {
/// Create an independent clone of this index.
///
/// # Errors
Expand All @@ -345,13 +345,17 @@ pub trait TryClone: FromInnerPtr {
/// supported for the internal type of index.
fn try_clone(&self) -> Result<Self>
where
Self: Sized,
{
unsafe {
let mut new_index_ptr = ::std::ptr::null_mut();
faiss_try(faiss_clone_index(self.inner_ptr(), &mut new_index_ptr))?;
Ok(crate::index::FromInnerPtr::from_inner_ptr(new_index_ptr))
}
Self: Sized;
}

pub fn try_clone_from_inner_ptr<T>(val: &T) -> Result<T>
where
T: FromInnerPtr,
{
unsafe {
let mut new_index_ptr = ::std::ptr::null_mut();
faiss_try(faiss_clone_index(val.inner_ptr(), &mut new_index_ptr))?;
Ok(crate::index::FromInnerPtr::from_inner_ptr(new_index_ptr))
}
}

Expand Down Expand Up @@ -532,7 +536,14 @@ impl<NI: NativeIndex> UpcastIndex for NI {

impl_native_index!(IndexImpl);

impl TryClone for IndexImpl {}
impl TryClone for IndexImpl {
fn try_clone(&self) -> Result<Self>
where
Self: Sized,
{
try_clone_from_inner_ptr(self)
}
}

/// Use the index factory to create a native instance of a Faiss index, for `d`-dimensional
/// vectors. `description` should follow the exact guidelines as the native Faiss interface
Expand Down
18 changes: 16 additions & 2 deletions src/index/pretransform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl<I> NativeIndex for PreTransformIndexImpl<I> {
}
}

impl<I> FromInnerPtr for PreTransformIndexImpl<I> {
impl FromInnerPtr for PreTransformIndexImpl<IndexImpl> {
unsafe fn from_inner_ptr(inner_ptr: *mut FaissIndex) -> Self {
PreTransformIndexImpl {
inner: inner_ptr as *mut FaissIndexPreTransform,
Expand Down Expand Up @@ -218,7 +218,21 @@ impl<I> Index for PreTransformIndexImpl<I> {
}
}

impl<I: TryClone> TryClone for PreTransformIndexImpl<I> {}
impl<I> TryClone for PreTransformIndexImpl<I> {
fn try_clone(&self) -> Result<Self>
where
Self: Sized,
{
unsafe {
let mut new_index_ptr = ::std::ptr::null_mut();
faiss_try(faiss_clone_index(self.inner_ptr(), &mut new_index_ptr))?;
Ok(PreTransformIndexImpl {
inner: new_index_ptr as *mut FaissIndexPreTransform,
sub_index: PhantomData,
})
}
}
}

impl<I> ConcurrentIndex for PreTransformIndexImpl<I>
where
Expand Down
40 changes: 38 additions & 2 deletions src/index/refine_flat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl<BI> NativeIndex for RefineFlatIndexImpl<BI> {
}
}

impl<BI> FromInnerPtr for RefineFlatIndexImpl<BI> {
impl FromInnerPtr for RefineFlatIndexImpl<IndexImpl> {
unsafe fn from_inner_ptr(inner_ptr: *mut FaissIndex) -> Self {
RefineFlatIndexImpl {
inner: inner_ptr as *mut FaissIndexFlat,
Expand All @@ -80,6 +80,28 @@ impl<BI> FromInnerPtr for RefineFlatIndexImpl<BI> {
}
}

impl TryFromInnerPtr for RefineFlatIndexImpl<IndexImpl> {
unsafe fn try_from_inner_ptr(inner_ptr: *mut FaissIndex) -> Result<Self>
where
Self: Sized,
{
// safety: `inner_ptr` is documented to be a valid pointer to an index,
// so the dynamic cast should be safe.
#[allow(unused_unsafe)]
unsafe {
let new_inner = faiss_IndexRefineFlat_cast(inner_ptr);
if new_inner.is_null() {
Err(Error::BadCast)
} else {
Ok(RefineFlatIndexImpl {
inner: new_inner,
base_index: PhantomData,
})
}
}
}
}

impl<BI> Index for RefineFlatIndexImpl<BI> {
fn is_trained(&self) -> bool {
unsafe { faiss_Index_is_trained(self.inner_ptr()) != 0 }
Expand Down Expand Up @@ -200,7 +222,21 @@ impl<BI> Index for RefineFlatIndexImpl<BI> {
}
}

impl<BI: TryClone> TryClone for RefineFlatIndexImpl<BI> {}
impl<I> TryClone for RefineFlatIndexImpl<I> {
fn try_clone(&self) -> Result<Self>
where
Self: Sized,
{
unsafe {
let mut new_index_ptr = ::std::ptr::null_mut();
faiss_try(faiss_clone_index(self.inner_ptr(), &mut new_index_ptr))?;
Ok(RefineFlatIndexImpl {
inner: new_index_ptr as *mut FaissIndexFlat,
base_index: PhantomData,
})
}
}
}

impl<BI> ConcurrentIndex for RefineFlatIndexImpl<BI>
where
Expand Down
27 changes: 24 additions & 3 deletions src/index/scalar_quantizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,14 @@ impl FromInnerPtr for ScalarQuantizerIndexImpl {

impl_native_index!(ScalarQuantizerIndexImpl);

impl TryClone for ScalarQuantizerIndexImpl {}
impl TryClone for ScalarQuantizerIndexImpl {
fn try_clone(&self) -> Result<Self>
where
Self: Sized,
{
try_clone_from_inner_ptr(self)
}
}

impl IndexImpl {
/// Attempt a dynamic cast of an index to the Scalar Quantizer index type.
Expand Down Expand Up @@ -274,7 +281,7 @@ impl<Q> NativeIndex for IVFScalarQuantizerIndexImpl<Q> {
}
}

impl<Q> FromInnerPtr for IVFScalarQuantizerIndexImpl<Q> {
impl FromInnerPtr for IVFScalarQuantizerIndexImpl<IndexImpl> {
unsafe fn from_inner_ptr(inner_ptr: *mut FaissIndex) -> Self {
IVFScalarQuantizerIndexImpl {
inner: inner_ptr as *mut FaissIndexIVFScalarQuantizer,
Expand Down Expand Up @@ -403,7 +410,21 @@ impl<Q> Index for IVFScalarQuantizerIndexImpl<Q> {
}
}

impl<Q: TryClone> TryClone for IVFScalarQuantizerIndexImpl<Q> {}
impl<Q> TryClone for IVFScalarQuantizerIndexImpl<Q> {
fn try_clone(&self) -> Result<Self>
where
Self: Sized,
{
unsafe {
let mut new_index_ptr = ::std::ptr::null_mut();
faiss_try(faiss_clone_index(self.inner_ptr(), &mut new_index_ptr))?;
Ok(IVFScalarQuantizerIndexImpl {
inner: new_index_ptr as *mut FaissIndexIVFScalarQuantizer,
quantizer: PhantomData,
})
}
}
}

impl<Q> ConcurrentIndex for IVFScalarQuantizerIndexImpl<Q>
where
Expand Down