Skip to content

armgit / uzu

Public Imported from GitHub
0

A high-performance inference engine for AI models

Support benchmark gpu capture (#522)

CCYeha7f31d2eba72
diff --git a/crates/backend-uzu/tests/BENCHMARKS.md b/crates/backend-uzu/tests/BENCHMARKS.md
index 4f1ece3b8..e9f2f3990 100644
--- a/crates/backend-uzu/tests/BENCHMARKS.md
+++ b/crates/backend-uzu/tests/BENCHMARKS.md
@@ -53,6 +53,11 @@ CRITERION_HOME="$PWD/target/criterion/m2_max" cargo bench \
--save-baseline matmul_baseline_m2_max
```
+Set `UZU_CAPTURE_BENCH=1` to capture the first matching benchmark command
+buffer as a Metal `.gputrace`. `UZU_CAPTURE_BENCH_FILTER` is an optional
+benchmark path substring; `UZU_CAPTURE_BENCH_DIR` defaults to the current
+directory.
+
## Running on iPhone (via `cargo-dinghy`)
Run one benchmark group at a time to avoid the iOS watchdog killing the
diff --git a/crates/backend-uzu/tests/common/matmul/bench.rs b/crates/backend-uzu/tests/common/matmul/bench.rs
index 5626f8523..5fdf859a8 100644
--- a/crates/backend-uzu/tests/common/matmul/bench.rs
+++ b/crates/backend-uzu/tests/common/matmul/bench.rs
@@ -1,18 +1,74 @@
-use backend_uzu::backends::common::{Backend, Encoder};
+use std::{
+ env,
+ path::PathBuf,
+ sync::atomic::{AtomicBool, Ordering},
+ time::{SystemTime, UNIX_EPOCH},
+};
+
+use backend_uzu::backends::common::{Backend, Context, Encoder};
use criterion::Bencher;
+use crate::common::env_var_enabled;
+
+static CAPTURE_TAKEN: AtomicBool = AtomicBool::new(false);
+
+fn should_capture_benchmark(benchmark_path: &str) -> bool {
+ env_var_enabled("UZU_CAPTURE_BENCH")
+ && benchmark_path.starts_with("Metal/")
+ && env::var("UZU_CAPTURE_BENCH_FILTER").map_or(true, |filter| benchmark_path.contains(&filter))
+ && !CAPTURE_TAKEN.swap(true, Ordering::AcqRel)
+}
+
+fn benchmark_capture_path() -> PathBuf {
+ let timestamp = SystemTime::now().duration_since(UNIX_EPOCH).expect("system clock before Unix epoch").as_secs();
+ env::var("UZU_CAPTURE_BENCH_DIR")
+ .map(PathBuf::from)
+ .unwrap_or(env::current_dir().unwrap())
+ .join(format!("uzu_bench-{timestamp}.gputrace"))
+}
+
+fn start_benchmark_capture<B: Backend>(
+ context: &B::Context,
+ benchmark_path: &str,
+) -> bool {
+ if !should_capture_benchmark(benchmark_path) {
+ return false;
+ }
+
+ let path = benchmark_capture_path();
+ context.start_capture(&path).expect("failed to start benchmark GPU capture");
+ println!("GPU benchmark capture started for {benchmark_path}: {path:?}");
+ true
+}
+
pub fn iter_encode_loop<B: Backend, F>(
context: &B::Context,
bencher: &mut Bencher,
mut encode: F,
) where
F: FnMut(&mut Encoder<B>),
+{
+ iter_encode_loop_named(context, bencher, "unnamed_benchmark", |encoder| encode(encoder));
+}
+
+pub fn iter_encode_loop_named<B: Backend, F>(
+ context: &B::Context,
+ bencher: &mut Bencher,
+ benchmark_path: &str,
+ mut encode: F,
+) where
+ F: FnMut(&mut Encoder<B>),
{
bencher.iter_custom(|n_iters| {
+ let capture = start_benchmark_capture::<B>(context, benchmark_path);
let mut encoder = Encoder::<B>::new(context).unwrap();
for _ in 0..n_iters {
encode(&mut encoder);
}
- encoder.end_encoding().submit().wait_until_completed().unwrap().gpu_execution_time()
+ let completed = encoder.end_encoding().submit().wait_until_completed().unwrap();
+ if capture {
+ context.stop_capture().expect("failed to stop benchmark GPU capture");
+ }
+ completed.gpu_execution_time()
});
}
diff --git a/crates/backend-uzu/tests/common/matmul/mod.rs b/crates/backend-uzu/tests/common/matmul/mod.rs
index 2647d5809..81c30e3b5 100644
--- a/crates/backend-uzu/tests/common/matmul/mod.rs
+++ b/crates/backend-uzu/tests/common/matmul/mod.rs
@@ -3,7 +3,7 @@ pub mod harness;
pub mod quant;
pub mod shape;
-pub use bench::iter_encode_loop;
+pub use bench::{iter_encode_loop, iter_encode_loop_named};
#[cfg(metal_backend)]
pub use harness::run_metal;
pub use harness::{Case, cpu_reference, deterministic_input};
diff --git a/crates/backend-uzu/tests/common/mod.rs b/crates/backend-uzu/tests/common/mod.rs
index 312bc8c72..bf35d59a4 100644
--- a/crates/backend-uzu/tests/common/mod.rs
+++ b/crates/backend-uzu/tests/common/mod.rs
@@ -15,6 +15,18 @@ pub fn type_short_name<T>() -> &'static str {
std::any::type_name::<T>().rsplit("::").next().unwrap()
}
+pub fn env_var_enabled(name: &str) -> bool {
+ std::env::var(name).is_ok_and(|v| v == "1" || v.eq_ignore_ascii_case("yes") || v.eq_ignore_ascii_case("true"))
+}
+
+pub fn enable_benchmark_gpu_capture_if_requested() {
+ if env_var_enabled("UZU_CAPTURE_BENCH") {
+ unsafe {
+ std::env::set_var("METAL_CAPTURE_ENABLED", "1");
+ }
+ }
+}
+
#[cfg(metal_backend)]
pub fn shared_metal_context() -> std::rc::Rc<backend_uzu::backends::metal::MetalContext> {
use backend_uzu::backends::{common::Context, metal::MetalContext};
diff --git a/crates/backend-uzu/tests/performance/bench_entry.rs b/crates/backend-uzu/tests/performance/bench_entry.rs
index 049f2e974..56068214d 100644
--- a/crates/backend-uzu/tests/performance/bench_entry.rs
+++ b/crates/backend-uzu/tests/performance/bench_entry.rs
@@ -4,6 +4,7 @@
fn bench_runner(benches: &[&dyn Fn()]) {
#[cfg(target_os = "ios")]
crate::common::path::ios_set_current_dir();
+ crate::common::enable_benchmark_gpu_capture_if_requested();
criterion::runner(benches);
}
diff --git a/crates/backend-uzu/tests/performance/kernel/bench_entry.rs b/crates/backend-uzu/tests/performance/kernel/bench_entry.rs
index 3d58c86ea..11bea57e2 100644
--- a/crates/backend-uzu/tests/performance/kernel/bench_entry.rs
+++ b/crates/backend-uzu/tests/performance/kernel/bench_entry.rs
@@ -4,6 +4,7 @@
fn bench_runner(benches: &[&dyn Fn()]) {
#[cfg(target_os = "ios")]
crate::common::path::ios_set_current_dir();
+ crate::common::enable_benchmark_gpu_capture_if_requested();
criterion::runner(benches);
}
diff --git a/crates/backend-uzu/tests/performance/kernel/matmul/quant_gemv_bench.rs b/crates/backend-uzu/tests/performance/kernel/matmul/quant_gemv_bench.rs
index 109ff2d45..eb241abc8 100644
--- a/crates/backend-uzu/tests/performance/kernel/matmul/quant_gemv_bench.rs
+++ b/crates/backend-uzu/tests/performance/kernel/matmul/quant_gemv_bench.rs
@@ -12,7 +12,7 @@ use num_traits::Float;
use crate::{
common::{
- matmul::{QuantBuffers, QuantInput, bench_quant_gemv_shapes, iter_encode_loop, quant_arguments},
+ matmul::{QuantBuffers, QuantInput, bench_quant_gemv_shapes, iter_encode_loop_named, quant_arguments},
type_short_name,
},
uzu_bench,
@@ -26,7 +26,8 @@ fn bench_gemv_typed<B: Backend, T: ArrayElement + Float>(
bits: u32,
quant_method: QuantizationMethod,
) {
- let mut group = c.benchmark_group(format!("{}/Kernel/Gemv/{}", type_short_name::<B>(), label));
+ let group_path = format!("{}/Kernel/Gemv/{}", type_short_name::<B>(), label);
+ let mut group = c.benchmark_group(group_path.clone());
for shape in bench_quant_gemv_shapes(bits) {
let (m, k, n) = (shape.m, shape.k, shape.n);
@@ -42,7 +43,8 @@ fn bench_gemv_typed<B: Backend, T: ArrayElement + Float>(
group.throughput(Throughput::Elements((m * n * k) as u64));
group.bench_function(BenchmarkId::from_parameter(shape.to_string()), |b| {
- iter_encode_loop::<B, _>(context, b, |encoder| {
+ let benchmark_path = format!("{group_path}/{shape}");
+ iter_encode_loop_named::<B, _>(context, b, &benchmark_path, |encoder| {
let args = quant_arguments(&mut buffers, &input);
matmul.encode(args, encoder).expect("encode failed");
});