Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanielsimard committed Dec 18, 2024
1 parent f96c715 commit 947eb1d
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 11 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion backend-comparison/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ wgpu-spirv-fusion = ["wgpu-spirv", "burn/fusion"]
arboard = { workspace = true }
burn = { path = "../crates/burn", default-features = false }
burn-common = { path = "../crates/burn-common", version = "0.16.0" }
burn-wgpu = { path = "../crates/burn-wgpu", default-features = false, version = "0.16.0", optional = true }

clap = { workspace = true }
colored = { workspace = true }
cubecl = { workspace = true, features = ["wgpu"], default-features = true }
Expand Down Expand Up @@ -96,6 +96,11 @@ name = "conv3d"
harness = false
name = "matmul"

[[bench]]
harness = false
name = "matmul-fused"
path = "benches/matmul_fused.rs"

[[bench]]
harness = false
name = "data"
Expand Down
16 changes: 7 additions & 9 deletions backend-comparison/benches/matmul.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use backend_comparison::persistence::save;
use burn::tensor::{activation::relu, backend::Backend, Shape, Tensor};
use burn::tensor::{backend::Backend, Distribution, Shape, Tensor};
use burn_common::benchmark::{run_benchmark, Benchmark};
use derive_new::new;

Expand All @@ -11,7 +11,7 @@ struct MatmulBenchmark<B: Backend, const D: usize> {
}

impl<B: Backend, const D: usize> Benchmark for MatmulBenchmark<B, D> {
type Args = (Tensor<B, D>, Tensor<B, D>, Tensor<B, 1>);
type Args = (Tensor<B, D>, Tensor<B, D>);

fn name(&self) -> String {
"matmul".into()
Expand All @@ -21,17 +21,15 @@ impl<B: Backend, const D: usize> Benchmark for MatmulBenchmark<B, D> {
vec![self.shape_lhs.dims.clone(), self.shape_rhs.dims.clone()]
}

fn execute(&self, (lhs, rhs, bias): Self::Args) {
let bias = bias.unsqueeze();
relu(lhs.matmul(rhs) + bias);
fn execute(&self, (lhs, rhs): Self::Args) {
lhs.matmul(rhs);
}

fn prepare(&self) -> Self::Args {
let lhs = Tensor::zeros(self.shape_lhs.clone(), &self.device);
let rhs = Tensor::zeros(self.shape_rhs.clone(), &self.device);
let bias = Tensor::zeros([self.shape_rhs.dims[2]], &self.device);
let lhs = Tensor::random(self.shape_lhs.clone(), Distribution::Default, &self.device);
let rhs = Tensor::random(self.shape_rhs.clone(), Distribution::Default, &self.device);

(lhs, rhs, bias)
(lhs, rhs)
}

fn sync(&self) {
Expand Down
74 changes: 74 additions & 0 deletions backend-comparison/benches/matmul_fused.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use backend_comparison::persistence::save;
use burn::tensor::{activation::relu, backend::Backend, Distribution, Shape, Tensor};
use burn_common::benchmark::{run_benchmark, Benchmark};
use derive_new::new;

#[derive(new)]
struct MatmulBenchmark<B: Backend, const D: usize> {
shape_lhs: Shape,
shape_rhs: Shape,
device: B::Device,
}

impl<B: Backend, const D: usize> Benchmark for MatmulBenchmark<B, D> {
type Args = (Tensor<B, D>, Tensor<B, D>, Tensor<B, 1>);

fn name(&self) -> String {
"matmul_bias_relu".into()
}

fn shapes(&self) -> Vec<Vec<usize>> {
vec![self.shape_lhs.dims.clone(), self.shape_rhs.dims.clone()]
}

fn execute(&self, (lhs, rhs, bias): Self::Args) {
let bias = bias.unsqueeze();
relu(lhs.matmul(rhs) + bias);
}

fn prepare(&self) -> Self::Args {
let lhs = Tensor::random(self.shape_lhs.clone(), Distribution::Default, &self.device);
let rhs = Tensor::random(self.shape_rhs.clone(), Distribution::Default, &self.device);
let bias = Tensor::random(
[self.shape_rhs.dims[2]],
Distribution::Default,
&self.device,
);

(lhs, rhs, bias)
}

fn sync(&self) {
B::sync(&self.device)
}
}

#[allow(dead_code)]
fn bench<B: Backend>(
device: &B::Device,
feature_name: &str,
url: Option<&str>,
token: Option<&str>,
) {
let benchmarks = [
(2, 4096, 4096, 4096),
(32, 2048, 2048, 2048),
(256, 1024, 1024, 1024),
(1024, 256, 256, 256),
]
.into_iter()
.map(|(b, m, n, k)| {
let shape_lhs = [b, m, k].into();
let shape_rhs = [b, k, n].into();

MatmulBenchmark::<B, 3>::new(shape_lhs, shape_rhs, device.clone())
})
.map(run_benchmark)
.collect();

save::<B>(benchmarks, device, feature_name, url, token).unwrap();
}

fn main() {
backend_comparison::bench_on_backend!();
}
2 changes: 2 additions & 0 deletions backend-comparison/src/burnbenchapp/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ enum BenchmarkValues {
Data,
#[strum(to_string = "matmul")]
Matmul,
#[strum(to_string = "matmul-fused")]
MatmulFused,
#[strum(to_string = "unary")]
Unary,
#[strum(to_string = "max-pool2d")]
Expand Down

0 comments on commit 947eb1d

Please sign in to comment.