use criterion::{criterion_group, criterion_main, Criterion};
use ndarray::prelude::*;
use ndarray_conv::*;
use ndarray_rand::{rand_distr::Uniform, RandomExt};
use ndarray_vision::processing::ConvolutionExt;
use num::Complex;
fn criterion_benchmark(c: &mut Criterion) {
let x = Array::random(5000, Uniform::new(0f32, 1.).unwrap());
let k = Array::random(31, Uniform::new(0f32, 1.).unwrap());
let x_crs = x.to_shape((1, 1, 5000)).unwrap().to_owned();
let k_crs = k.to_shape((1, 1, 1, 31)).unwrap().to_owned();
let tensor = tch::Tensor::from_slice(x.as_slice().unwrap())
.to_dtype(tch::Kind::Float, false, true)
.reshape([1, 1, 5000]);
let kernel = tch::Tensor::from_slice(k.as_slice().unwrap())
.to_dtype(tch::Kind::Float, false, true)
.reshape([1, 1, 31]);
let mut fft_processor = get_fft_processor();
c.bench_function("fft_1d", |b| {
b.iter(|| x.conv_fft(&k, ConvMode::Same, PaddingMode::Zeros))
});
c.bench_function("fft_with_processor_1d", |b| {
b.iter(|| {
x.conv_fft_with_processor(&k, ConvMode::Same, PaddingMode::Zeros, &mut fft_processor)
})
});
c.bench_function("torch_1d", |b| {
b.iter(|| tensor.conv1d_padding::<tch::Tensor>(&kernel, None, 1, "same", 1, 1))
});
let x = Array::random((200, 5000), Uniform::new(0f32, 1.).unwrap());
let k = Array::random((11, 31), Uniform::new(0f32, 1.).unwrap());
let x_crs = x.to_shape((1, 200, 5000)).unwrap().to_owned();
let k_crs = k.to_shape((1, 1, 11, 31)).unwrap().to_owned();
let x_nvs = x.to_shape((200, 5000, 1)).unwrap().to_owned();
let k_nvs = k.to_shape((11, 31, 1)).unwrap().to_owned();
let tensor = tch::Tensor::from_slice(x.as_slice().unwrap())
.to_dtype(tch::Kind::Float, false, true)
.reshape([1, 1, 200, 5000]);
let kernel = tch::Tensor::from_slice(k.as_slice().unwrap())
.to_dtype(tch::Kind::Float, false, true)
.reshape([1, 1, 11, 31]);
let mut fft_processor = get_fft_processor();
c.bench_function("fft_2d", |b| {
b.iter(|| x.conv_fft(&k, ConvMode::Same, PaddingMode::Zeros))
});
c.bench_function("fft_with_processor_2d", |b| {
b.iter(|| {
x.conv_fft_with_processor(&k, ConvMode::Same, PaddingMode::Zeros, &mut fft_processor)
})
});
c.bench_function("torch_2d", |b| {
b.iter(|| tensor.conv2d_padding::<tch::Tensor>(&kernel, None, 1, "same", 1, 1))
});
let x = Array::random((10, 100, 200), Uniform::new(0f32, 1.).unwrap());
let k = Array::random((5, 11, 31), Uniform::new(0f32, 1.).unwrap());
let x_crs = x.to_shape((10, 100, 200)).unwrap().to_owned();
let k_crs = k.to_shape((1, 5, 11, 31)).unwrap().to_owned();
let tensor = tch::Tensor::from_slice(x.as_slice().unwrap())
.to_dtype(tch::Kind::Float, false, true)
.reshape([1, 1, 10, 100, 200]);
let kernel = tch::Tensor::from_slice(k.as_slice().unwrap())
.to_dtype(tch::Kind::Float, false, true)
.reshape([1, 1, 5, 11, 31]);
let mut fft_processor = get_fft_processor();
c.bench_function("fft_3d", |b| {
b.iter(|| x.conv_fft(&k, ConvMode::Same, PaddingMode::Zeros))
});
c.bench_function("fft_with_processor_3d", |b| {
b.iter(|| {
x.conv_fft_with_processor(&k, ConvMode::Same, PaddingMode::Zeros, &mut fft_processor)
})
});
c.bench_function("torch_3d", |b| {
b.iter(|| tensor.conv3d_padding::<tch::Tensor>(&kernel, None, 1, "same", 1, 1))
});
}
criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);