-
Notifications
You must be signed in to change notification settings - Fork 648
feat(storage): implement in memory hnsw builder #21648
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
Open
wenym1
wants to merge
7
commits into
yiming/hummock-flat-index
Choose a base branch
from
yiming/in-memory-hnsw-builder
base: yiming/hummock-flat-index
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+917
−24
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
128cac2
feat(storage): implement in memory hnsw builder
wenym1 7a2dac7
refine with trait
wenym1 9c8d1f9
bump commit
wenym1 b19f93e
install package when build
wenym1 8dbe305
include extra library in CI image and remove install in build.rs
wenym1 3fd04d2
refine
wenym1 78a5b4f
refine collect
wenym1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
BUILD_ENV_VERSION=v20250509-1 | ||
BUILD_ENV_VERSION=FAISS-v20250506 | ||
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,9 @@ | |
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use std::simd::Simd; | ||
use std::simd::num::SimdFloat; | ||
|
||
use crate::vector::{ | ||
MeasureDistance, MeasureDistanceBuilder, VectorDistance, VectorItem, VectorRef, | ||
}; | ||
|
@@ -155,14 +158,42 @@ impl MeasureDistanceBuilder for InnerProductDistance { | |
} | ||
} | ||
|
||
#[cfg_attr(not(test), expect(dead_code))] | ||
fn inner_product_trivial(first: VectorRef<'_>, second: VectorRef<'_>) -> VectorDistance { | ||
let len = first.0.len(); | ||
assert_eq!(len, second.0.len()); | ||
-(0..len) | ||
.map(|i| first.0[i] * second.0[i]) | ||
.sum::<VectorItem>() | ||
} | ||
|
||
#[cfg_attr(not(test), expect(dead_code))] | ||
fn inner_product_simd(first: VectorRef<'_>, second: VectorRef<'_>) -> VectorDistance { | ||
let len = first.0.len(); | ||
assert_eq!(len, second.0.len()); | ||
let mut sum = 0.0; | ||
< 9E7A td id="diff-40304c8bcc7f62c8853b3778acb8066f25948884e0015b09b3d7e0521c4e92faR175" data-line-number="175" class="blob-num blob-num-addition js-linkable-line-number js-blob-rnum"> | let mut start = 0; | |
let mut end = start + 32; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need a check to make sure the length of input vec can be divided by 32? |
||
while end <= len { | ||
let this = Simd::<VectorItem, 32>::from_slice(&first.0[start..end]); | ||
let target = Simd::<VectorItem, 32>::from_slice(&second.0[start..end]); | ||
sum += (this * target).reduce_sum(); | ||
start += 32; | ||
end += 32; | ||
} | ||
-((start..len) | ||
.map(|i| first.0[i] * second.0[i]) | ||
.sum::<VectorDistance>() | ||
+ sum) | ||
} | ||
|
||
fn inner_product_faiss(first: VectorRef<'_>, second: VectorRef<'_>) -> VectorDistance { | ||
-faiss::utils::fvec_inner_product(first.0, second.0) | ||
} | ||
|
||
impl<'a> MeasureDistance for InnerProductDistanceMeasure<'a> { | ||
fn measure(&self, other: VectorRef<'_>) -> VectorDistance { | ||
// TODO: use some library with simd support | ||
let len = self.0.0.len(); | ||
assert_eq!(len, other.0.len()); | ||
-(0..len) | ||
.map(|i| self.0.0[i] * other.0[i]) | ||
.sum::<VectorDistance>() | ||
inner_product_faiss(self.0, other) | ||
} | ||
} | ||
|
||
|
@@ -172,6 +203,7 @@ mod tests { | |
use expect_test::expect; | ||
|
||
use super::*; | ||
use crate::vector::test_utils::gen_vector; | ||
use crate::vector::{MeasureDistanceBuilder, Vector, VectorInner}; | ||
|
||
const VECTOR_LEN: usize = 10; | ||
|
@@ -186,6 +218,14 @@ mod tests { | |
0.22877127, 0.97690505, 0.44438475, | ||
]; | ||
|
||
const FLOAT_ALLOWED_BIAS: f32 = 1e-5; | ||
|
||
macro_rules! assert_eq_float { | ||
($first:expr, $second:expr) => { | ||
assert!(($first - $second) < FLOAT_ALLOWED_BIAS) | ||
}; | ||
} | ||
|
||
#[test] | ||
fn test_distance() { | ||
let first_vec = [0.238474, 0.578234]; | ||
|
@@ -223,6 +263,13 @@ mod tests { | |
InnerProductDistance::distance(first_vec, second_vec), | ||
-(v1_1 * v2_1 + v1_2 * v2_2) | ||
); | ||
{ | ||
let v1 = gen_vector(128); | ||
let v2 = gen_vector(128); | ||
let trivial = inner_product_trivial(v1.to_ref(), v2.to_ref()); | ||
assert_eq_float!(inner_product_simd(v1.to_ref(), v2.to_ref()), trivial); | ||
assert_eq_float!(inner_product_faiss(v1.to_ref(), v2.to_ref()), trivial); | ||
} | ||
} | ||
|
||
#[test] | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this expected? related to a new image with the new dependencies
libblas-dev liblapack-dev libomp-dev
for linear algebra ?