Skip to content
Closed
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
branches: [ master, "change/faiss-v1.8.0" ]

env:
CARGO_TERM_COLOR: always
Expand Down
2 changes: 1 addition & 1 deletion faiss-sys/faiss
Submodule faiss updated 555 files
10 changes: 5 additions & 5 deletions faiss-sys/gen_bindings.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@
# Ensure that the submodule is updated and checked out in the intended revision
if ! which bindgen > /dev/null; then
echo "ERROR: `bindgen` not found. Please install using cargo:"
echo " cargo install bindgen"
echo " cargo install bindgen-cli --version=^0.69"
exit 1
fi

repo_url=https://github.com/facebookresearch/faiss
repo_rev=v1.7.2
repo_rev=v1.8.0
cuda_root=/opt/cuda

if [ ! -d faiss ]; then
git clone "$repo_url" faiss --branch "$repo_rev" --depth 1
fi

bindgen_opt='--size_t-is-usize --whitelist-function faiss_.* --whitelist-type idx_t|Faiss.* --opaque-type FILE'
bindgen_opt='--allowlist-function faiss_.* --allowlist-type idx_t|Faiss.* --opaque-type FILE'

headers=`ls faiss/c_api/*_c.h faiss/c_api/impl/*_c.h faiss/c_api/utils/*_c.h`
echo '// Auto-generated, do not edit!' > c_api.h
for header in $headers; do
echo "#include \""$header"\"" >> c_api.h;
done

cmd="bindgen --rust-target 1.33 $bindgen_opt c_api.h -o src/bindings.rs"
cmd="bindgen --rust-target 1.59 $bindgen_opt c_api.h -o src/bindings.rs"
echo ${cmd}
${cmd}

Expand All @@ -33,7 +33,7 @@ for header in $headers; do
echo "#include \""$header"\"" >> c_api.h;
done

cmd="bindgen --rust-target 1.33 $bindgen_opt c_api.h -o src/bindings_gpu.rs -- -Ifaiss/c_api -I$cuda_root/include"
cmd="bindgen --rust-target 1.59 $bindgen_opt c_api.h -o src/bindings_gpu.rs -- -Ifaiss/c_api -I$cuda_root/include"
echo ${cmd}
${cmd}

Expand Down
1,494 changes: 789 additions & 705 deletions faiss-sys/src/bindings.rs

Large diffs are not rendered by default.

1,530 changes: 800 additions & 730 deletions faiss-sys/src/bindings_gpu.rs

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions faiss-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@ mod tests {
assert!(!last_error.is_null());
}
}
#[test]
fn flat_index_binary() {
const D: usize = 8;
unsafe {
let description =
CString::new::<&str>("BFlat".as_ref()).unwrap();
let mut index_ptr = ::std::ptr::null_mut();
let code = faiss_index_binary_factory(
&mut index_ptr,
(D & 0x7FFF_FFFF) as i32,
description.as_ptr(),
);
assert_eq!(code, 0);
}
}

#[test]
fn flat_index() {
Expand Down
78 changes: 77 additions & 1 deletion src/index/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

use crate::error::{Error, Result};
use crate::faiss_try;
use crate::index::{CpuIndex, FromInnerPtr, IndexImpl, NativeIndex};
use crate::index::{
CpuIndex, FromInnerPtr, IndexImpl, NativeIndex,
CpuIndexBinary, FromInnerPtrBinary, BinaryIndexImpl, NativeIndexBinary
};
use faiss_sys::*;
use std::ffi::CString;
use std::os::raw::c_int;
Expand Down Expand Up @@ -31,6 +34,28 @@ where
}
}


/// Write a binary index to a file.
///
/// # Error
///
/// This function returns an error if the description contains any byte with the value `\0` (since
/// it cannot be converted to a C string), or if the internal index writing operation fails.
pub fn write_index_binary<I, P>(index: &I, file_name: P) -> Result<()>
where
I: NativeIndexBinary,
I: CpuIndexBinary,
P: AsRef<str>,
{
unsafe {
let f = file_name.as_ref();
let f = CString::new(f).map_err(|_| Error::BadFilePath)?;

faiss_try(faiss_write_index_binary_fname(index.inner_ptr(), f.as_ptr()))?;
Ok(())
}
}

/// Read an index from a file.
///
/// # Error
Expand All @@ -54,6 +79,30 @@ where
}
}


/// Read a binary index from a file.
///
/// # Error
///
/// This function returns an error if the description contains any byte with the value `\0` (since
/// it cannot be converted to a C string), or if the internal index reading operation fails.
pub fn read_index_binary<P>(file_name: P) -> Result<BinaryIndexImpl>
where
P: AsRef<str>,
{
unsafe {
let f = file_name.as_ref();
let f = CString::new(f).map_err(|_| Error::BadFilePath)?;
let mut inner = ptr::null_mut();
faiss_try(faiss_read_index_binary_fname(
f.as_ptr(),
IoFlags::MEM_RESIDENT.into(),
&mut inner,
))?;
Ok(BinaryIndexImpl::from_inner_ptr(inner))
}
}

/// Read an index from a file with I/O flags.
///
/// You can memory map some index types with this.
Expand All @@ -79,6 +128,33 @@ where
}
}


/// Read a binary index from a file with I/O flags.
///
/// You can memory map some index types with this.
///
/// # Error
///
/// This function returns an error if the description contains any byte with the value `\0` (since
/// it cannot be converted to a C string), or if the internal index reading operation fails.
pub fn read_index_binary_with_flags<P>(file_name: P, io_flags: IoFlags) -> Result<BinaryIndexImpl>
where
P: AsRef<str>,
{
unsafe {
let f = file_name.as_ref();
let f = CString::new(f).map_err(|_| Error::BadFilePath)?;
let mut inner = ptr::null_mut();
faiss_try(faiss_read_index_binary_fname(
f.as_ptr(),
io_flags.0 as c_int,
&mut inner,
))?;
Ok(BinaryIndexImpl::from_inner_ptr(inner))
}
}


#[cfg(test)]
mod tests {
use super::*;
Expand Down
Loading