+
Skip to content

Adding some "benchmarking" scripts #483

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,16 @@ indicatif = "0.16.2"

[build-dependencies]
rustc_version = "0.4.0"
glob = "0.3.0"
glob = "0.3.0"

[[bench]]
name = "batchnorm2d"
harness = false

[[bench]]
name = "conv2d"
harness = false

[[bench]]
name = "sum"
harness = false
10 changes: 10 additions & 0 deletions benches/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Benchmarks

This folder contains a couple of simple utility scripts for running
forward and backward passes of core ops.

- `cargo bench --bench batchnorm2d`
- `cargo bench --bench sum`
- `cargo +nightly bench --bench conv2d`

Additionally you can pass `-F cuda` to use a Cuda.
38 changes: 38 additions & 0 deletions benches/batchnorm2d.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use std::time::Instant;

use dfdx::prelude::*;

#[cfg(feature = "cuda")]
type Dev = Cuda;

#[cfg(not(feature = "cuda"))]
type Dev = Cpu;

type Model = BatchNorm2D<512>;
type Dtype = f32;
type InputShape = Rank4<64, 512, 28, 28>;

fn main() {
println!("Benchmarking `BatchNorm2D`");
println!("Device {}", std::any::type_name::<Dev>());
println!("Dtype {}", std::any::type_name::<Dtype>());
println!("Input shape {}", std::any::type_name::<InputShape>());
println!();

let dev: Dev = Default::default();
let mut m = dev.build_module::<Model, Dtype>();

loop {
let img: Tensor<InputShape, Dtype, _> = dev.sample_normal();

let start = Instant::now();
let out = m.forward_mut(img.traced());
let loss = out.square().mean();
let fwd_dur = start.elapsed();

let start = Instant::now();
let _ = loss.backward();
let bwd_dur = start.elapsed();
println!("fwd={:?} bwd={:?}", fwd_dur, bwd_dur);
}
}
46 changes: 46 additions & 0 deletions benches/conv2d.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#![cfg_attr(feature = "nightly", feature(generic_const_exprs))]

#[cfg(feature = "nightly")]
fn main() {
use std::time::Instant;

use dfdx::prelude::*;

#[cfg(feature = "cuda")]
type Dev = Cuda;

#[cfg(not(feature = "cuda"))]
type Dev = Cpu;

type Model = Conv2D<128, 256, 4>;
type Dtype = f32;
type InputShape = Rank4<64, 128, 28, 28>;

println!("Benchmarking `Conv2D`");
println!("Device {}", std::any::type_name::<Dev>());
println!("Dtype {}", std::any::type_name::<Dtype>());
println!("Input shape {}", std::any::type_name::<InputShape>());
println!();

let dev: Dev = Default::default();
let mut m = dev.build_module::<Model, Dtype>();

loop {
let img: Tensor<InputShape, Dtype, _> = dev.sample_normal();

let start = Instant::now();
let out = m.forward_mut(img.traced());
let loss = out.square().mean();
let fwd_dur = start.elapsed();

let start = Instant::now();
let _ = loss.backward();
let bwd_dur = start.elapsed();
println!("fwd={:?} bwd={:?}", fwd_dur, bwd_dur);
}
}

#[cfg(not(feature = "nightly"))]
fn main() {
panic!("Run with `cargo +nightly run ...` to run this example.");
}
35 changes: 35 additions & 0 deletions benches/sum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use std::time::Instant;

use dfdx::prelude::*;

#[cfg(feature = "cuda")]
type Dev = Cuda;

#[cfg(not(feature = "cuda"))]
type Dev = Cpu;

type Dtype = f32;
type InputShape = Rank4<32, 64, 128, 256>;

fn main() {
println!("Benchmarking `sum`");
println!("Device {}", std::any::type_name::<Dev>());
println!("Dtype {}", std::any::type_name::<Dtype>());
println!("Input shape {}", std::any::type_name::<InputShape>());
println!();

let dev: Dev = Default::default();

loop {
let img: Tensor<InputShape, Dtype, _> = dev.sample_normal();

let start = Instant::now();
let y = img.traced().sum();
let fwd_dur = start.elapsed();

let start = Instant::now();
let _ = y.backward();
let bwd_dur = start.elapsed();
println!("fwd={:?} bwd={:?}", fwd_dur, bwd_dur);
}
}
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载