-
Notifications
You must be signed in to change notification settings - Fork 24
[POP-2951] Slow-but-perfect KNN in plaintext #1676
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
b03a716
draft naive-knn
mcalancea b32449d
Release memory correctly add checkpoints and safeguards
mcalancea 9ea42f6
clippy
mcalancea 575b15f
Merge branch 'main' into mihai/ideal-neighborhoods-gen
mcalancea 39625c8
fixes
mcalancea a0073da
change floats to fractions
mcalancea efaf4a9
dev: deploy main
naure 3bcafc9
dev: Increase timeout to 30min to support batch_size=32
naure 7878a72
Merge branch 'main' into mihai/ideal-neighborhoods-gen
mcalancea 01d5824
Merge branch 'main' into dev
mcalancea d12a2b3
Merge branch 'dev' into mihai/ideal-neighborhoods-gen
mcalancea 55adf81
Merge branch 'main' into dev
mcalancea 9844546
[POP-2929] add graceful shutdown to the networking stack (#1685)
sdwoodbury 1cf5cbd
u32 instead of usize and stream.take
mcalancea e160325
Merge main -> dev (#1705)
naure bb22b9a
Merge branch 'dev' into mihai/ideal-neighborhoods-gen
mcalancea d90538b
clippy
mcalancea 9472803
Merge branch 'dev' into mihai/ideal-neighborhoods-gen
bgillesp 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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 |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| use std::{fs::File, io::BufReader, path::PathBuf}; | ||
|
|
||
| use clap::Parser; | ||
| use iris_mpc_common::iris_db::iris::IrisCode; | ||
| use iris_mpc_common::vector_id::SerialId; | ||
| use iris_mpc_cpu::{ | ||
| hawkers::naive_knn_plaintext::naive_knn, | ||
| py_bindings::{limited_iterator, plaintext_store::Base64IrisCode}, | ||
| }; | ||
| use metrics::IntoF64; | ||
| use serde_json::Deserializer; | ||
| use std::time::Instant; | ||
|
|
||
| #[derive(Clone, Debug, PartialEq, Eq, Hash)] | ||
| pub struct IrisCodeWithSerialId { | ||
| pub iris_code: IrisCode, | ||
| pub serial_id: SerialId, | ||
| } | ||
|
|
||
| #[derive(Parser, Debug)] | ||
| #[command(author, version, about, long_about = None)] | ||
| struct Args { | ||
| /// Number of irises to process | ||
| #[arg(long, default_value_t = 1000)] | ||
| num_irises: usize, | ||
|
|
||
| /// Number of threads to use | ||
| #[arg(long, default_value_t = 1)] | ||
| num_threads: usize, | ||
| } | ||
| #[tokio::main] | ||
| async fn main() { | ||
| let args = Args::parse(); | ||
| let n_existing_irises = 0; | ||
| let num_irises = args.num_irises; | ||
|
|
||
| let mut path_to_iris_codes = PathBuf::new(); | ||
| path_to_iris_codes.push("iris-mpc-cpu/data/store.ndjson".to_owned()); | ||
mcalancea marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| let file = File::open(path_to_iris_codes.as_path()).unwrap(); | ||
| let reader = BufReader::new(file); | ||
|
|
||
| let stream = Deserializer::from_reader(reader) | ||
| .into_iter::<Base64IrisCode>() | ||
| .skip(2 * n_existing_irises); | ||
|
|
||
| let mut irises: [Vec<IrisCode>; 2] = [Vec::new(), Vec::new()]; | ||
|
|
||
| let stream = limited_iterator(stream, Some(num_irises * 2)); | ||
| for (idx, json_pt) in stream.enumerate() { | ||
| let iris_code_query = (&json_pt.unwrap()).into(); | ||
| let _serial_id = ((idx / 2) + 1 + n_existing_irises) as u32; | ||
|
|
||
| let side = idx % 2; | ||
| irises[side].push(iris_code_query); | ||
| } | ||
mcalancea marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| let start = Instant::now(); | ||
| naive_knn(irises[0].clone(), args.num_threads); | ||
| let duration = start.elapsed(); | ||
| println!( | ||
| "naive_knn took {:?} (per number of pairs)", | ||
| duration.into_f64() / (num_irises as f64) / (num_irises as f64) | ||
| ); | ||
| } | ||
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 |
|---|---|---|
|
|
@@ -21,3 +21,5 @@ pub mod plaintext_store; | |
| pub mod shared_irises; | ||
|
|
||
| pub mod build_plaintext; | ||
|
|
||
| pub mod naive_knn_plaintext; | ||
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 |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| use iris_mpc_common::iris_db::iris::IrisCode; | ||
| use rayon::{ | ||
| iter::{IntoParallelIterator, ParallelIterator}, | ||
| ThreadPoolBuilder, | ||
| }; | ||
|
|
||
| pub fn naive_knn(irises: Vec<IrisCode>, num_threads: usize) { | ||
| let k = 320; | ||
mcalancea marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| let n = irises.len(); | ||
|
|
||
| let pool = ThreadPoolBuilder::new() | ||
| .num_threads(num_threads) | ||
| .build() | ||
| .unwrap(); | ||
|
|
||
| let _results = pool.install(|| { | ||
| (0..n) | ||
| .collect::<Vec<_>>() | ||
| .into_par_iter() | ||
| .map(|i| { | ||
| let current_iris = &irises[i]; | ||
| let mut distances = irises | ||
| .iter() | ||
| .enumerate() | ||
| .map(|(j, other_iris)| (j, current_iris.get_distance(other_iris))) | ||
mcalancea marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| .collect::<Vec<_>>(); | ||
| distances.select_nth_unstable_by(k - 1, |(_, d1), (_, d2)| d1.total_cmp(d2)); | ||
| distances.truncate(k); | ||
| distances.sort_by(|(_, d1), (_, d2)| d1.total_cmp(d2)); | ||
| distances | ||
| }) | ||
| .collect::<Vec<_>>() | ||
| }); | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.