Add benchmarks to instant-distance-py
This commit is contained in:
parent
d51b99beb3
commit
53fbb4507e
|
@ -53,7 +53,7 @@ jobs:
|
||||||
- name: Test Python bindings
|
- name: Test Python bindings
|
||||||
run: |
|
run: |
|
||||||
sudo apt-get install -y wamerican
|
sudo apt-get install -y wamerican
|
||||||
cp target/release/libinstant_distance.so instant-distance-py/test/instant_distance.so
|
cp target/release/libinstant_distance_py.so instant-distance-py/test/instant_distance.so
|
||||||
PYTHONPATH=instant-distance-py/test/ python3 -m test
|
PYTHONPATH=instant-distance-py/test/ python3 -m test
|
||||||
|
|
||||||
lint:
|
lint:
|
||||||
|
|
9
Makefile
9
Makefile
|
@ -1,6 +1,9 @@
|
||||||
test-python:
|
instant-distance-py/test/instant_distance.so: instant-distance-py/src/lib.rs
|
||||||
cargo build --release
|
RUSTFLAGS="-C target-cpu=native" cargo build --release
|
||||||
cp target/release/libinstant_distance.dylib instant-distance-py/test/instant_distance.so
|
([ -f target/release/libinstant_distance_py.dylib ] && cp target/release/libinstant_distance_py.dylib instant-distance-py/test/instant_distance.so) || \
|
||||||
|
([ -f target/release/libinstant_distance_py.so ] && cp target/release/libinstant_distance_py.so instant-distance-py/test/instant_distance.so)
|
||||||
|
|
||||||
|
test-python: instant-distance-py/test/instant_distance.so
|
||||||
PYTHONPATH=instant-distance-py/test/ python3 -m test
|
PYTHONPATH=instant-distance-py/test/ python3 -m test
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
|
|
|
@ -11,8 +11,11 @@ repository = "https://github.com/InstantDomain/instant-distance"
|
||||||
readme = "../README.md"
|
readme = "../README.md"
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
|
name = "instant_distance_py"
|
||||||
|
crate-type = ["cdylib", "lib"]
|
||||||
|
|
||||||
|
[package.metadata.maturin]
|
||||||
name = "instant_distance"
|
name = "instant_distance"
|
||||||
crate-type = ["cdylib"]
|
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
bincode = "1.3.1"
|
bincode = "1.3.1"
|
||||||
|
@ -20,3 +23,11 @@ instant-distance = { version = "0.6", path = "../instant-distance", features = [
|
||||||
pyo3 = { version = "0.18.0", features = ["extension-module"] }
|
pyo3 = { version = "0.18.0", features = ["extension-module"] }
|
||||||
serde = { version = "1", features = ["derive"] }
|
serde = { version = "1", features = ["derive"] }
|
||||||
serde-big-array = "0.5.0"
|
serde-big-array = "0.5.0"
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
bencher = "0.1.5"
|
||||||
|
rand = { version = "0.8", features = ["small_rng"] }
|
||||||
|
|
||||||
|
[[bench]]
|
||||||
|
name = "all"
|
||||||
|
harness = false
|
||||||
|
|
|
@ -0,0 +1,41 @@
|
||||||
|
use bencher::{benchmark_group, benchmark_main, Bencher};
|
||||||
|
|
||||||
|
use instant_distance::{Builder, Point, Search};
|
||||||
|
use instant_distance_py::FloatArray;
|
||||||
|
use rand::{rngs::StdRng, Rng, SeedableRng};
|
||||||
|
|
||||||
|
benchmark_main!(benches);
|
||||||
|
benchmark_group!(benches, distance, build, query);
|
||||||
|
|
||||||
|
fn distance(bench: &mut Bencher) {
|
||||||
|
let mut rng = StdRng::seed_from_u64(SEED);
|
||||||
|
let point_a = FloatArray([rng.gen(); 300]);
|
||||||
|
let point_b = FloatArray([rng.gen(); 300]);
|
||||||
|
|
||||||
|
bench.iter(|| point_a.distance(&point_b));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn build(bench: &mut Bencher) {
|
||||||
|
let mut rng = StdRng::seed_from_u64(SEED);
|
||||||
|
let points = (0..1024)
|
||||||
|
.map(|_| FloatArray([rng.gen(); 300]))
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
bench.iter(|| Builder::default().seed(SEED).build_hnsw(points.clone()));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn query(bench: &mut Bencher) {
|
||||||
|
let mut rng = StdRng::seed_from_u64(SEED);
|
||||||
|
let points = (0..1024)
|
||||||
|
.map(|_| FloatArray([rng.gen(); 300]))
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
let (hnsw, _) = Builder::default().seed(SEED).build_hnsw(points);
|
||||||
|
let point = FloatArray([rng.gen(); 300]);
|
||||||
|
|
||||||
|
bench.iter(|| {
|
||||||
|
let mut search = Search::default();
|
||||||
|
let _ = hnsw.search(&point, &mut search);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const SEED: u64 = 123456789;
|
|
@ -4,3 +4,6 @@ name = "instant-distance"
|
||||||
[build-system]
|
[build-system]
|
||||||
requires = ["maturin >= 0.14, < 0.15"]
|
requires = ["maturin >= 0.14, < 0.15"]
|
||||||
build-backend = "maturin"
|
build-backend = "maturin"
|
||||||
|
|
||||||
|
[tool.maturin]
|
||||||
|
python-source = "python"
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
from .instant_distance import *
|
||||||
|
|
||||||
|
__doc__ = instant_distance.__doc__
|
||||||
|
if hasattr(instant_distance, "__all__"):
|
||||||
|
__all__ = instant_distance.__all__
|
|
@ -348,7 +348,7 @@ impl Neighbor {
|
||||||
|
|
||||||
#[repr(align(32))]
|
#[repr(align(32))]
|
||||||
#[derive(Clone, Deserialize, Serialize)]
|
#[derive(Clone, Deserialize, Serialize)]
|
||||||
struct FloatArray(#[serde(with = "BigArray")] [f32; DIMENSIONS]);
|
pub struct FloatArray(#[serde(with = "BigArray")] pub [f32; DIMENSIONS]);
|
||||||
|
|
||||||
impl TryFrom<&PyAny> for FloatArray {
|
impl TryFrom<&PyAny> for FloatArray {
|
||||||
type Error = PyErr;
|
type Error = PyErr;
|
||||||
|
|
Loading…
Reference in New Issue