Skip to content

Commit

Permalink
Make backend names in JSON reports match burnbench CLI
Browse files Browse the repository at this point in the history
- add `config_name`  to `Backend` trait
- add `backend_config_name` to  `Benchmark` trait
- fix documentation for JSON reports to use correct unit of time
  • Loading branch information
errordeveloper committed Mar 19, 2024
1 parent 8911093 commit 36f4364
Show file tree
Hide file tree
Showing 16 changed files with 107 additions and 6 deletions.
4 changes: 4 additions & 0 deletions backend-comparison/benches/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ impl<B: Backend, const D: usize> Benchmark for BinaryBenchmark<B, D> {
"binary".into()
}

fn backend_config_name(&self) -> Option<String> {
B::config_name(&self.device)
}

fn shapes(&self) -> Vec<Vec<usize>> {
vec![self.shape.dims.into()]
}
Expand Down
4 changes: 4 additions & 0 deletions backend-comparison/benches/custom_gelu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ impl<B: Backend, const D: usize> Benchmark for CustomGeluBenchmark<B, D> {
.into()
}

fn backend_config_name(&self) -> Option<String> {
B::config_name(&self.device)
}

fn options(&self) -> Option<String> {
Some(format!("{:?}", self.kind))
}
Expand Down
8 changes: 8 additions & 0 deletions backend-comparison/benches/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ impl<B: Backend, const D: usize> Benchmark for ToDataBenchmark<B, D> {
"to_data".into()
}

fn backend_config_name(&self) -> Option<String> {
B::config_name(&self.device)
}

fn shapes(&self) -> Vec<Vec<usize>> {
vec![self.shape.dims.into()]
}
Expand Down Expand Up @@ -46,6 +50,10 @@ impl<B: Backend, const D: usize> Benchmark for FromDataBenchmark<B, D> {
"from_data".into()
}

fn backend_config_name(&self) -> Option<String> {
B::config_name(&self.device)
}

fn shapes(&self) -> Vec<Vec<usize>> {
vec![self.shape.dims.into()]
}
Expand Down
4 changes: 4 additions & 0 deletions backend-comparison/benches/matmul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ impl<B: Backend, const D: usize> Benchmark for MatmulBenchmark<B, D> {
"matmul".into()
}

fn backend_config_name(&self) -> Option<String> {
B::config_name(&self.device)
}

fn shapes(&self) -> Vec<Vec<usize>> {
vec![self.shape_lhs.dims.into(), self.shape_rhs.dims.into()]
}
Expand Down
4 changes: 4 additions & 0 deletions backend-comparison/benches/max_pool2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ impl<B: Backend> Benchmark for MaxPool2dBenchmark<B> {
"max_pool2d".into()
}

fn backend_config_name(&self) -> Option<String> {
B::config_name(&self.device)
}

fn shapes(&self) -> Vec<Vec<usize>> {
vec![self.shape.dims.into()]
}
Expand Down
4 changes: 4 additions & 0 deletions backend-comparison/benches/unary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ impl<B: Backend, const D: usize> Benchmark for UnaryBenchmark<B, D> {
"unary".into()
}

fn backend_config_name(&self) -> Option<String> {
B::config_name(&self.device)
}

fn shapes(&self) -> Vec<Vec<usize>> {
vec![self.shape.dims.into()]
}
Expand Down
17 changes: 11 additions & 6 deletions backend-comparison/src/persistence/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,19 @@ pub struct BenchmarkRecord {
/// [
/// {
/// "backend": "backend name",
/// "backendConfigName": "backend config name as appers in burnbench flag",
/// "device": "device name",
/// "git_hash": "hash",
/// "name": "benchmark name",
/// "operation": "operation name",
/// "shapes": ["shape dimension", "shape dimension", ...],
/// "timestamp": "timestamp",
/// "numSamples": "number of samples",
/// "min": "duration in seconds",
/// "max": "duration in seconds",
/// "median": "duration in seconds",
/// "mean": "duration in seconds",
/// "variance": "duration in seconds"
/// "min": "duration in microseconds",
/// "max": "duration in microseconds",
/// "median": "duration in microseconds",
/// "mean": "duration in microseconds",
/// "variance": "duration in microseconds"
/// "rawDurations": ["duration 1", "duration 2", ...],
/// },
/// { ... }
Expand Down Expand Up @@ -64,7 +65,7 @@ pub fn save<B: Backend>(

let records: Vec<BenchmarkRecord> = benches
.into_iter()
.map(|bench| BenchmarkRecord {
.map(|bench: BenchmarkResult| BenchmarkRecord {
backend: B::name().to_string(),
device: format!("{:?}", device),
results: bench,
Expand Down Expand Up @@ -151,6 +152,7 @@ impl Serialize for BenchmarkRecord {
self,
("backend", &self.backend),
("device", &self.device),
("backendConfigName", &self.results.backend_config_name),
("gitHash", &self.results.git_hash),
("max", &self.results.computed.max.as_micros()),
("mean", &self.results.computed.mean.as_micros()),
Expand Down Expand Up @@ -183,6 +185,7 @@ impl<'de> Visitor<'de> for BenchmarkRecordVisitor {
match key.as_str() {
"backend" => br.backend = map.next_value::<String>()?,
"device" => br.device = map.next_value::<String>()?,
"backendConfigName" => br.results.backend_config_name = Some(map.next_value::<String>()?),
"gitHash" => br.results.git_hash = map.next_value::<String>()?,
"name" => br.results.name = map.next_value::<String>()?,
"max" => {
Expand Down Expand Up @@ -262,6 +265,7 @@ mod tests {
let sample_result = r#"{
"backend": "candle",
"device": "Cuda(0)",
"backendConfigName": "candle-cuda",
"gitHash": "02d37011ab4dc773286e5983c09cde61f95ba4b5",
"name": "unary",
"max": 8858,
Expand Down Expand Up @@ -325,6 +329,7 @@ mod tests {
let record = serde_json::from_str::<BenchmarkRecord>(sample_result).unwrap();
assert!(record.backend == "candle");
assert!(record.device == "Cuda(0)");
assert!(record.results.backend_config_name == "candle-cuda");
assert!(record.results.git_hash == "02d37011ab4dc773286e5983c09cde61f95ba4b5");
assert!(record.results.name == "unary");
assert!(record.results.computed.max.as_micros() == 8858);
Expand Down
7 changes: 7 additions & 0 deletions crates/burn-autodiff/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ impl<B: Backend, C: CheckpointStrategy> Backend for Autodiff<B, C> {
format!("autodiff<{}>", B::name())
}

fn config_name(device: &Self::Device) -> Option<String> {
match B::config_name(device) {
Some(name) => Some(format!("{}-autodiff", name)),
None => None,
}
}

fn seed(seed: u64) {
B::seed(seed)
}
Expand Down
7 changes: 7 additions & 0 deletions crates/burn-candle/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ impl<F: FloatCandleElement, I: IntCandleElement> Backend for Candle<F, I> {
"candle".to_string()
}

fn config_name(device: &Self::Device) -> Option<String> {
match device {
CandleDevice::Cpu => Some(String::from("candle-cpu")),
CandleDevice::Cuda(_) | CandleDevice::Metal(_) => Some(String::from("candle-gpu")),
}
}

fn seed(seed: u64) {
// TODO submit an issue at Candle
panic!("Manual seed not supported by Candle. ")
Expand Down
7 changes: 7 additions & 0 deletions crates/burn-common/src/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ pub trait Benchmark {
/// Name of the benchmark, should be short and it should match the name
/// defined in the crate Cargo.toml
fn name(&self) -> String;

/// Name of the backend as appears in burnbench CLI
fn backend_config_name(&self) -> Option<String>;

/// The options passed to the benchmark.
fn options(&self) -> Option<String> {
None
Expand Down Expand Up @@ -185,6 +189,8 @@ pub struct BenchmarkResult {
pub git_hash: String,
/// Name of the benchmark
pub name: String,
/// Name of the backend as appears in burnbench CLI
pub backend_config_name: Option<String>,
/// Options passed to the benchmark
pub options: Option<String>,
/// Shape dimensions
Expand Down Expand Up @@ -230,6 +236,7 @@ where
computed: BenchmarkComputations::new(&durations),
git_hash,
name: benchmark.name(),
backend_config_name: benchmark.backend_config_name(),
options: benchmark.options(),
shapes: benchmark.shapes(),
timestamp,
Expand Down
4 changes: 4 additions & 0 deletions crates/burn-compute/src/tune/tune_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ impl<S: ComputeServer, C: ComputeChannel<S>> Benchmark for TuneBenchmark<S, C> {
"autotune".to_string()
}

fn backend_config_name(&self) -> Option<String> {
None
}

fn sync(&self) {
self.client.sync();
}
Expand Down
7 changes: 7 additions & 0 deletions crates/burn-fusion/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ impl<B: FusionBackend> Backend for Fusion<B> {
format!("fusion<{}>", B::name())
}

fn config_name(device: &Self::Device) -> Option<String> {
match B::config_name(device) {
Some(name) => Some(format!("{}-fusion", name)),
None => None,
}
}

fn seed(seed: u64) {
B::seed(seed);
}
Expand Down
8 changes: 8 additions & 0 deletions crates/burn-jit/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ impl<R: Runtime> Backend for JitBackend<R> {
format!("jit<{}>", R::name())
}

fn config_name(_device: &Self::Device) -> Option<String> {
// match R::config_name(device) {
// Some(name) => Some(format!("{}-jit", name)),
// None => None,
// }
None
}

fn seed(seed: u64) {
let rng = StdRng::seed_from_u64(seed);
let mut seed = SEED.lock().unwrap();
Expand Down
16 changes: 16 additions & 0 deletions crates/burn-ndarray/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ impl Default for NdArrayDevice {
}
}

fn config_name() -> String {
if cfg!(feature = "blas-netlib") {
String::from("ndarray-blas-netlib")
} else if cfg!(feature = "blas-openblas") {
String::from("ndarray-blas-openblas")
} else if cfg!(feature = "blas-openblas-system") {
String::from("ndarray-blas-openblas-system")
} else {
String::from("ndarray")
}
}

/// Tensor backend that uses the [ndarray](ndarray) crate for executing tensor operations.
///
/// This backend is compatible with CPUs and can be compiled for almost any platform, including
Expand Down Expand Up @@ -51,6 +63,10 @@ impl<E: FloatNdArrayElement> Backend for NdArray<E> {
String::from("ndarray")
}

fn config_name(_: &Self::Device) -> Option<String> {
Some(config_name())
}

fn seed(seed: u64) {
let rng = StdRng::seed_from_u64(seed);
let mut seed = SEED.lock().unwrap();
Expand Down
9 changes: 9 additions & 0 deletions crates/burn-tch/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,15 @@ impl<E: TchElement> Backend for LibTorch<E> {
"tch".to_string()
}

fn config_name(device: &Self::Device) -> Option<String> {
match device {
LibTorchDevice::Cpu => Some(String::from("tch-cpu")),
LibTorchDevice::Cuda(_) | LibTorchDevice::Mps | LibTorchDevice::Vulkan => {
Some(String::from("tch-gpu"))
}
}
}

fn sync(device: &Self::Device) {
if let LibTorchDevice::Cuda(index) = device {
tch::Cuda::synchronize(*index as i64);
Expand Down
3 changes: 3 additions & 0 deletions crates/burn-tensor/src/tensor/backend/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ pub trait Backend:
/// Name of the backend.
fn name() -> String;

/// Name of the backend configuration (i.e. as used in burnbench CLI).
fn config_name(device: &Self::Device) -> Option<String>;

/// Seed the backend.
fn seed(seed: u64);

Expand Down

0 comments on commit 36f4364

Please sign in to comment.