-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eb6985e
commit b094d09
Showing
4 changed files
with
56 additions
and
83 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -1,4 +1,8 @@ | ||
mod benchmarks; | ||
|
||
use criterion::criterion_main; | ||
criterion_main!(benchmarks::layer_norm::benches, benchmarks::conv::benches); | ||
criterion_main!( | ||
benchmarks::softmax::benches, | ||
benchmarks::layer_norm::benches, | ||
benchmarks::conv::benches | ||
); |
This file contains 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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
pub(crate) mod conv; | ||
pub(crate) mod layer_norm; | ||
pub(crate) mod softmax; | ||
|
||
use candle::{Device, Result}; | ||
|
||
|
This file contains 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,49 @@ | ||
use crate::benchmarks::{BenchDevice, BenchDeviceHandler}; | ||
use candle::{DType, Device, Tensor}; | ||
use candle_nn::ops::softmax_last_dim; | ||
use criterion::Throughput; | ||
use criterion::{black_box, criterion_group, Criterion}; | ||
use std::time::Instant; | ||
|
||
fn run(input: &Tensor) { | ||
let _ = softmax_last_dim(&input).unwrap(); | ||
} | ||
|
||
const B: usize = 1; | ||
const M: usize = 1024; | ||
const K: usize = 1024; | ||
|
||
fn run_softmax_benchmark(c: &mut Criterion, device: &Device, dtype: DType, name: &str) { | ||
let elements = B * M * K; | ||
|
||
let input = Tensor::rand(-1000.0f32, 1000.0f32, (B, M, K), &device) | ||
.unwrap() | ||
.to_dtype(dtype) | ||
.unwrap(); | ||
|
||
let flops = elements * dtype.size_in_bytes(); | ||
let mut group = c.benchmark_group(device.bench_name(name)); | ||
group.throughput(Throughput::Bytes(flops as u64)); | ||
group.bench_function("iter", move |b| { | ||
b.iter_custom(|iters| { | ||
let start = Instant::now(); | ||
for _i in 0..iters { | ||
run(black_box(&input)); | ||
} | ||
device.sync().unwrap(); | ||
start.elapsed() | ||
}) | ||
}); | ||
group.finish(); | ||
} | ||
|
||
fn criterion_benchmark(c: &mut Criterion) { | ||
let device = BenchDeviceHandler::new().unwrap(); | ||
for d in device.devices { | ||
run_softmax_benchmark(c, &d, DType::F32, "softmax_f32"); | ||
run_softmax_benchmark(c, &d, DType::BF16, "softmax_bf16"); | ||
run_softmax_benchmark(c, &d, DType::F16, "softmax_f16"); | ||
} | ||
} | ||
|
||
criterion_group!(benches, criterion_benchmark); |