Skip to content

Commit

Permalink
benches & tests (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
Smirkey committed Nov 12, 2023
1 parent 1b6e6c8 commit 92841c5
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 23 deletions.
12 changes: 11 additions & 1 deletion .github/workflows/codespeed.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,21 @@ jobs:
- name: Install dependencies
run: pip install -r tests/requirements.txt

- name: Setup rust toolchain, cache and cargo-codspeed binary
uses: moonrepo/setup-rust@v0
with:
channel: stable
cache-target: release
bins: cargo-codspeed

- name: Install project
run: pip install .

- name: Build the benchmark target(s)
run: cargo codspeed build

- name: Run benchmarks
uses: CodSpeedHQ/action@v1
with:
token: ${{ secrets.CODSPEED_TOKEN }}
run: pytest tests/ --codspeed
run: pytest tests/ --codspeed && cargo codspeed run
40 changes: 40 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ readme = "README.md"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
name = "powerboxesrs"
crate-type = ["cdylib"]
crate-type = ["lib"]

[dependencies]
ndarray = { version = "0.15.6", features = ["rayon"] }
Expand All @@ -18,4 +18,9 @@ pyo3 = "0.20.0"
rayon = "1.8.0"

[dev-dependencies]
codspeed-criterion-compat = "2.3.1"
criterion = "0.5.1"

[[bench]]
name = "bench_iou"
harness = false
85 changes: 85 additions & 0 deletions benches/bench_iou.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
use codspeed_criterion_compat::{black_box, criterion_group, criterion_main, Criterion};
use ndarray::{arr2, Array2};
use powerboxesrs::giou::{giou_distance, parallel_giou_distance};
use powerboxesrs::iou::{iou_distance, parallel_iou_distance};

pub fn iou_distance_benchmark(c: &mut Criterion) {
let mut boxes1 = Array2::<f64>::zeros((100, 4));
for i in 0..100 {
for j in 0..4 {
if j < 2 {
boxes1[[i, j]] = 0.0;
} else {
boxes1[[i, j]] = 10.0;
}
}
}
let boxes2 = boxes1.clone();

c.bench_function("iou distance benchmark", |b| {
b.iter(|| iou_distance(black_box(&boxes1), black_box(&boxes2)))
});
}

pub fn parallel_iou_distance_benchmark(c: &mut Criterion) {
let mut boxes1 = Array2::<f64>::zeros((100, 4));
for i in 0..100 {
for j in 0..4 {
if j < 2 {
boxes1[[i, j]] = 0.0;
} else {
boxes1[[i, j]] = 10.0;
}
}
}
let boxes2 = boxes1.clone();

c.bench_function("parallel iou distance benchmark", |b| {
b.iter(|| parallel_iou_distance(black_box(&boxes1), black_box(&boxes2)))
});
}

pub fn giou_distance_benchmark(c: &mut Criterion) {
let mut boxes1 = Array2::<f64>::zeros((100, 4));
for i in 0..100 {
for j in 0..4 {
if j < 2 {
boxes1[[i, j]] = 0.0;
} else {
boxes1[[i, j]] = 10.0;
}
}
}
let boxes2 = boxes1.clone();

c.bench_function("giou distance benchmark", |b| {
b.iter(|| giou_distance(black_box(&boxes1), black_box(&boxes2)))
});
}

pub fn parallel_giou_distance_benchmark(c: &mut Criterion) {
let mut boxes1 = Array2::<f64>::zeros((100, 4));
for i in 0..100 {
for j in 0..4 {
if j < 2 {
boxes1[[i, j]] = 0.0;
} else {
boxes1[[i, j]] = 10.0;
}
}
}
let boxes2 = boxes1.clone();

c.bench_function("parallel giou distance benchmark", |b| {
b.iter(|| parallel_giou_distance(black_box(&boxes1), black_box(&boxes2)))
});
}

criterion_group!(
benches,
iou_distance_benchmark,
parallel_iou_distance_benchmark,
giou_distance_benchmark,
parallel_giou_distance_benchmark
);
criterion_main!(benches);
22 changes: 15 additions & 7 deletions src/boxes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub enum BoxFormat {
///
/// ```
/// use ndarray::array;
/// use powerboxes::boxes::box_areas;
/// use powerboxesrs::boxes::box_areas;
///
/// let boxes = array![[1., 2., 3., 4.], [0., 0., 10., 10.]];
///
Expand Down Expand Up @@ -60,7 +60,7 @@ pub fn box_areas(boxes: &Array2<f64>) -> Array1<f64> {
///
/// ```
/// use ndarray::array;
/// use powerboxes::boxes::parallel_box_areas;
/// use powerboxesrs::boxes::parallel_box_areas;
///
/// let boxes = array![[1., 2., 3., 4.], [0., 0., 10., 10.]];
///
Expand Down Expand Up @@ -99,13 +99,13 @@ pub fn parallel_box_areas(boxes: &Array2<f64>) -> Array1<f64> {
///
/// ```
/// use ndarray::array;
/// use powerboxes::boxes::remove_small_boxes;
/// use powerboxesrs::boxes::remove_small_boxes;
///
/// let boxes = array![[1., 2., 3., 4.], [0., 0., 10., 10.]];
/// let min_size = 10;
/// let min_size = 10.0;
/// let result = remove_small_boxes(&boxes, min_size);
///
/// assert_eq!(result, array![[0., 0., 10., 10.]]));
/// assert_eq!(result, array![[0., 0., 10., 10.]]);
/// ```
pub fn remove_small_boxes(boxes: &Array2<f64>, min_size: f64) -> Array2<f64> {
let areas = box_areas(boxes);
Expand Down Expand Up @@ -133,7 +133,7 @@ pub fn remove_small_boxes(boxes: &Array2<f64>, min_size: f64) -> Array2<f64> {
///
/// ```
/// use ndarray::arr2;
/// use powerboxes::{BoxFormat, box_convert};
/// use powerboxesrs::boxes::{BoxFormat, box_convert};
///
/// let boxes = arr2(&[
/// [10.0, 20.0, 30.0, 40.0],
Expand Down Expand Up @@ -241,7 +241,7 @@ pub fn box_convert(boxes: &Array2<f64>, in_fmt: &BoxFormat, out_fmt: &BoxFormat)
///
/// ```
/// use ndarray::arr2;
/// use powerboxes::{BoxFormat, parallel_box_convert};
/// use powerboxesrs::boxes::{BoxFormat, parallel_box_convert};
///
/// let boxes = arr2(&[
/// [10.0, 20.0, 30.0, 40.0],
Expand Down Expand Up @@ -505,4 +505,12 @@ mod tests {
assert_eq!(areas, array![9.]);
assert_eq!(parallel_areas, areas);
}

#[test]
fn test_remove_small_boxes() {
let boxes = array![[1., 2., 3., 4.], [0., 0., 10., 10.]];
let min_size = 10.;
let filtered_boxes = remove_small_boxes(&boxes, min_size);
assert_eq!(filtered_boxes, array![[0., 0., 10., 10.]]);
}
}
20 changes: 10 additions & 10 deletions src/giou.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use ndarray::{Array2, Zip};
///
/// ```
/// use ndarray::array;
/// use powerboxes::giou::giou_distance;
/// use powerboxesrs::giou::giou_distance;
///
/// let boxes1 = array![[0., 0., 10., 10.], [20., 20., 30., 30.]];
/// let boxes2 = array![[0., 0., 10., 10.], [15., 15., 25., 25.], [20., 20., 30., 30.]];
Expand All @@ -23,10 +23,10 @@ use ndarray::{Array2, Zip};
///
/// assert_eq!(giou.shape(), &[2, 3]);
/// assert_eq!(giou[[0, 0]], 0.);
/// assert_eq!(giou[[0, 1]], 0.25);
/// assert_eq!(giou[[0, 2]], 0.);
/// assert_eq!(giou[[1, 0]], 0.25);
/// assert_eq!(giou[[1, 1]], 0.);
/// assert_eq!(giou[[0, 1]], 1.5948840131957898);
/// assert_eq!(giou[[0, 2]], 1.3293605909992827);
/// assert_eq!(giou[[1, 0]], 1.3293605909992827);
/// assert_eq!(giou[[1, 1]], 1.020555218446602);
/// assert_eq!(giou[[1, 2]], 0.);
/// ```
pub fn giou_distance(boxes1: &Array2<f64>, boxes2: &Array2<f64>) -> Array2<f64> {
Expand Down Expand Up @@ -94,7 +94,7 @@ pub fn giou_distance(boxes1: &Array2<f64>, boxes2: &Array2<f64>) -> Array2<f64>
///
/// ```
/// use ndarray::array;
/// use powerboxes::giou::parallel_giou_distance;
/// use powerboxesrs::giou::parallel_giou_distance;
///
/// let boxes1 = array![[0., 0., 10., 10.], [20., 20., 30., 30.]];
/// let boxes2 = array![[0., 0., 10., 10.], [15., 15., 25., 25.], [20., 20., 30., 30.]];
Expand All @@ -103,10 +103,10 @@ pub fn giou_distance(boxes1: &Array2<f64>, boxes2: &Array2<f64>) -> Array2<f64>
///
/// assert_eq!(giou.shape(), &[2, 3]);
/// assert_eq!(giou[[0, 0]], 0.);
/// assert_eq!(giou[[0, 1]], 0.25);
/// assert_eq!(giou[[0, 2]], 0.);
/// assert_eq!(giou[[1, 0]], 0.25);
/// assert_eq!(giou[[1, 1]], 0.);
/// assert_eq!(giou[[0, 1]], 1.5948840131957898);
/// assert_eq!(giou[[0, 2]], 1.3293605909992827);
/// assert_eq!(giou[[1, 0]], 1.3293605909992827);
/// assert_eq!(giou[[1, 1]], 1.020555218446602);
/// assert_eq!(giou[[1, 2]], 0.);
/// ```
pub fn parallel_giou_distance(boxes1: &Array2<f64>, boxes2: &Array2<f64>) -> Array2<f64> {
Expand Down
8 changes: 4 additions & 4 deletions src/iou.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ use ndarray::{Array2, Zip};
///
/// ```
/// use ndarray::array;
/// use powerboxes::iou::iou_distance;
/// use powerboxesrs::iou::iou_distance;
///
/// let boxes1 = array![[0.0, 0.0, 1.0, 1.0], [2.0, 2.0, 3.0, 3.0]];
/// let boxes2 = array![[0.5, 0.5, 1.5, 1.5], [2.5, 2.5, 3.5, 3.5]];
/// let iou = iou_distance(&boxes1, &boxes2);
/// assert_eq!(iou, array![[0.25, 0.0], [0.0, 0.25]]);
/// assert_eq!(iou, array![[0.6086956521739131, 0.967741935483871],[0.967741935483871, 0.6086956521739131]]);
/// ```
pub fn iou_distance(boxes1: &Array2<f64>, boxes2: &Array2<f64>) -> Array2<f64> {
let num_boxes1 = boxes1.nrows();
Expand Down Expand Up @@ -75,12 +75,12 @@ pub fn iou_distance(boxes1: &Array2<f64>, boxes2: &Array2<f64>) -> Array2<f64> {
///
/// ```
/// use ndarray::array;
/// use powerboxes::iou::iou_distance;
/// use powerboxesrs::iou::iou_distance;
///
/// let boxes1 = array![[0.0, 0.0, 1.0, 1.0], [2.0, 2.0, 3.0, 3.0]];
/// let boxes2 = array![[0.5, 0.5, 1.5, 1.5], [2.5, 2.5, 3.5, 3.5]];
/// let iou = iou_distance(&boxes1, &boxes2);
/// assert_eq!(iou, array![[0.25, 0.0], [0.0, 0.25]]);
/// assert_eq!(iou, array![[0.6086956521739131, 0.967741935483871],[0.967741935483871, 0.6086956521739131]]);
/// ```
pub fn parallel_iou_distance(boxes1: &Array2<f64>, boxes2: &Array2<f64>) -> Array2<f64> {
let num_boxes1 = boxes1.nrows();
Expand Down

0 comments on commit 92841c5

Please sign in to comment.