2024-01-13 00:15:00 +08:00
|
|
|
use backend_comparison::persistence::save;
|
2023-09-28 21:38:21 +08:00
|
|
|
use burn::tensor::{backend::Backend, Distribution, Shape, Tensor};
|
2024-06-13 21:56:08 +08:00
|
|
|
use burn_common::{
|
|
|
|
benchmark::{run_benchmark, Benchmark},
|
|
|
|
sync_type::SyncType,
|
|
|
|
};
|
2023-09-28 21:38:21 +08:00
|
|
|
use derive_new::new;
|
|
|
|
|
|
|
|
#[derive(new)]
|
|
|
|
struct UnaryBenchmark<B: Backend, const D: usize> {
|
|
|
|
shape: Shape<D>,
|
2023-10-30 04:44:59 +08:00
|
|
|
device: B::Device,
|
2023-09-28 21:38:21 +08:00
|
|
|
}
|
|
|
|
|
2023-10-30 04:44:59 +08:00
|
|
|
impl<B: Backend, const D: usize> Benchmark for UnaryBenchmark<B, D> {
|
2023-09-28 21:38:21 +08:00
|
|
|
type Args = Tensor<B, D>;
|
|
|
|
|
|
|
|
fn name(&self) -> String {
|
2024-01-13 00:15:00 +08:00
|
|
|
"unary".into()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn shapes(&self) -> Vec<Vec<usize>> {
|
|
|
|
vec![self.shape.dims.into()]
|
2023-09-28 21:38:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn execute(&self, args: Self::Args) {
|
2024-01-13 00:15:00 +08:00
|
|
|
// Choice of tanh is arbitrary
|
2024-07-08 22:16:58 +08:00
|
|
|
B::float_tanh(args.clone().into_primitive().tensor());
|
2023-09-28 21:38:21 +08:00
|
|
|
}
|
|
|
|
|
2023-10-30 04:44:59 +08:00
|
|
|
fn prepare(&self) -> Self::Args {
|
2023-12-21 06:49:59 +08:00
|
|
|
Tensor::random(self.shape.clone(), Distribution::Default, &self.device)
|
2023-10-30 04:44:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn sync(&self) {
|
2024-06-13 21:56:08 +08:00
|
|
|
B::sync(&self.device, SyncType::Wait)
|
2023-09-28 21:38:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
2024-04-01 21:48:44 +08:00
|
|
|
fn bench<B: Backend>(
|
|
|
|
device: &B::Device,
|
|
|
|
feature_name: &str,
|
|
|
|
url: Option<&str>,
|
|
|
|
token: Option<&str>,
|
|
|
|
) {
|
2023-09-28 21:38:21 +08:00
|
|
|
const D: usize = 3;
|
|
|
|
let shape: Shape<D> = [32, 512, 1024].into();
|
|
|
|
|
2024-01-13 00:15:00 +08:00
|
|
|
let benchmark = UnaryBenchmark::<B, D>::new(shape, device.clone());
|
2023-09-28 21:38:21 +08:00
|
|
|
|
2024-04-01 21:48:44 +08:00
|
|
|
save::<B>(
|
|
|
|
vec![run_benchmark(benchmark)],
|
|
|
|
device,
|
|
|
|
feature_name,
|
|
|
|
url,
|
|
|
|
token,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2023-09-28 21:38:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
backend_comparison::bench_on_backend!();
|
|
|
|
}
|