use serde::{Deserialize, Serialize};
use std::time::{Duration, Instant};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BenchResult {
pub kernel_name: String,
pub elem_type: String,
pub shape: Vec<i64>,
pub execution_time_ms: f64,
pub throughput_gflops: f64,
pub memory_bandwidth_gbps: f64,
pub gpu_utilization_pct: f64,
}
#[derive(Debug, Clone)]
pub struct BenchConfig {
pub max_duration: Duration,
pub warmup_iterations: u32,
pub bench_iterations: u32,
pub quick: bool,
}
impl Default for BenchConfig {
fn default() -> Self {
BenchConfig {
max_duration: Duration::from_secs(300),
warmup_iterations: 10,
bench_iterations: 100,
quick: false,
}
}
}
impl BenchConfig {
pub fn quick() -> Self {
BenchConfig {
max_duration: Duration::from_secs(30),
warmup_iterations: 2,
bench_iterations: 5,
quick: true,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BenchKernelEntry {
pub kernel: String,
pub elem: String,
pub shape: Vec<i64>,
pub execution_time_ms: f64,
pub gflops: f64,
pub bandwidth_gbps: f64,
pub baseline_time_ms: f64,
pub efficiency_pct: f64,
pub is_regression: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BenchReport {
pub schema_version: String,
pub timestamp: String,
pub host: String,
pub kernels: Vec<BenchKernelEntry>,
pub summary: BenchSummary,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BenchSummary {
pub total_kernels: usize,
pub regression_count: usize,
pub best_gflops: f64,
pub best_bandwidth_gbps: f64,
pub avg_efficiency_pct: f64,
}
pub struct BenchRunner {
config: BenchConfig,
results: Vec<BenchResult>,
}
impl BenchRunner {
pub fn new(config: BenchConfig) -> Self {
BenchRunner {
config,
results: Vec::new(),
}
}
pub fn results(&self) -> &[BenchResult] {
&self.results
}
pub fn run_kernel(&mut self, kernel_name: &str, elem_type: &str, shape: &[i64]) -> BenchResult {
for _ in 0..self.config.warmup_iterations {
std::thread::sleep(Duration::from_millis(1));
}
let start = Instant::now();
let mut total_time = Duration::new(0, 0);
for _ in 0..self.config.bench_iterations {
let iter_start = Instant::now();
std::thread::sleep(Duration::from_millis(1));
total_time += iter_start.elapsed();
}
let _ = start.elapsed();
let avg_time_ms = total_time.as_secs_f64() * 1000.0 / self.config.bench_iterations as f64;
let num_elements: i64 = shape.iter().product();
let throughput_gflops = if avg_time_ms > 0.0 {
num_elements as f64 / (avg_time_ms / 1000.0) / 1e9
} else {
0.0
};
let bytes_per_elem = elem_type_bytes(elem_type);
let total_bytes = num_elements as f64 * bytes_per_elem * 2.0;
let bandwidth_gbps = if avg_time_ms > 0.0 {
total_bytes / (avg_time_ms / 1000.0) / 1e9
} else {
0.0
};
let result = BenchResult {
kernel_name: kernel_name.to_string(),
elem_type: elem_type.to_string(),
shape: shape.to_vec(),
execution_time_ms: avg_time_ms,
throughput_gflops,
memory_bandwidth_gbps: bandwidth_gbps,
gpu_utilization_pct: 0.0,
};
self.results.push(result.clone());
result
}
pub fn run_all(&mut self, kernels: &[(String, String, Vec<i64>)]) -> BenchReport {
let mut entries = Vec::new();
for (kernel, elem, shape) in kernels {
let result = self.run_kernel(kernel, elem, shape);
let entry = build_entry(result);
entries.push(entry);
}
let summary = build_summary(&entries);
BenchReport {
schema_version: "1.0".to_string(),
timestamp: iso8601_now(),
host: host_triple(),
kernels: entries,
summary,
}
}
}
pub fn elem_type_bytes(elem: &str) -> f64 {
match elem {
"f32" | "i32" => 4.0,
"f16" | "bf16" | "i16" => 2.0,
"f64" | "i64" => 8.0,
"i8" => 1.0,
_ => 4.0,
}
}
pub fn build_entry(result: BenchResult) -> BenchKernelEntry {
let baseline_ms = baseline_ms(&result.kernel_name);
let efficiency_pct = if result.execution_time_ms > 0.0 {
(baseline_ms / result.execution_time_ms) * 100.0
} else {
0.0
};
let is_regression = result.execution_time_ms > baseline_ms * 1.05;
BenchKernelEntry {
kernel: result.kernel_name,
elem: result.elem_type,
shape: result.shape,
execution_time_ms: result.execution_time_ms,
gflops: result.throughput_gflops,
bandwidth_gbps: result.memory_bandwidth_gbps,
baseline_time_ms: baseline_ms,
efficiency_pct,
is_regression,
}
}
pub fn build_summary(entries: &[BenchKernelEntry]) -> BenchSummary {
let total = entries.len();
let regression_count = entries.iter().filter(|e| e.is_regression).count();
let best_gflops = entries.iter().map(|e| e.gflops).fold(0.0, f64::max);
let best_bandwidth = entries.iter().map(|e| e.bandwidth_gbps).fold(0.0, f64::max);
let avg_efficiency = if total > 0 {
entries.iter().map(|e| e.efficiency_pct).sum::<f64>() / total as f64
} else {
0.0
};
BenchSummary {
total_kernels: total,
regression_count,
best_gflops,
best_bandwidth_gbps: best_bandwidth,
avg_efficiency_pct: avg_efficiency,
}
}
pub fn report_to_json(report: &BenchReport) -> String {
serde_json::to_string_pretty(report).unwrap_or_else(|_| "{}".to_string())
}
pub fn report_from_json(json: &str) -> Result<BenchReport, String> {
serde_json::from_str(json).map_err(|e| format!("Failed to parse report JSON: {e}"))
}
pub fn compare_reports(baseline: &BenchReport, current: &BenchReport) -> Vec<EfficiencyDelta> {
let mut deltas = Vec::new();
for (b, c) in baseline.kernels.iter().zip(current.kernels.iter()) {
let delta_pct = if b.execution_time_ms > 0.0 {
((c.execution_time_ms - b.execution_time_ms) / b.execution_time_ms) * 100.0
} else {
0.0
};
deltas.push(EfficiencyDelta {
kernel_name: b.kernel.clone(),
baseline_time_ms: b.execution_time_ms,
current_time_ms: c.execution_time_ms,
delta_pct,
is_regression: delta_pct > 5.0,
});
}
deltas
}
fn host_triple() -> String {
let arch = std::env::consts::ARCH;
let os = std::env::consts::OS;
let env = std::env::consts::FAMILY;
format!("{arch}-{os}-{env}")
}
fn iso8601_now() -> String {
use std::time::{SystemTime, UNIX_EPOCH};
let secs = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_secs();
let days = secs / 86400;
let rem = secs % 86400;
let h = rem / 3600;
let m = (rem % 3600) / 60;
let s = rem % 60;
let (year, month, day) = days_to_date(days);
format!("{year:04}-{month:02}-{day:02}T{h:02}:{m:02}:{s:02}Z")
}
fn days_to_date(days: u64) -> (u32, u32, u32) {
let mut year: u32 = 1970;
let mut remaining = days;
loop {
let days_in_year = if is_leap_year(year) { 366 } else { 365 };
if remaining < days_in_year {
break;
}
remaining -= days_in_year;
year += 1;
}
let mut month: u32 = 1;
loop {
let dim = days_in_month(year, month);
if remaining < dim {
break;
}
remaining -= dim;
month += 1;
}
(year, month, remaining as u32 + 1)
}
fn is_leap_year(year: u32) -> bool {
(year.is_multiple_of(4) && !year.is_multiple_of(100)) || year.is_multiple_of(400)
}
fn days_in_month(year: u32, month: u32) -> u64 {
match month {
1 | 3 | 5 | 7 | 8 | 10 | 12 => 31,
4 | 6 | 9 | 11 => 30,
2 => {
if is_leap_year(year) {
29
} else {
28
}
}
_ => 30,
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EfficiencyDelta {
pub kernel_name: String,
pub baseline_time_ms: f64,
pub current_time_ms: f64,
pub delta_pct: f64,
pub is_regression: bool,
}
pub fn compare_results(baseline: &[BenchResult], current: &[BenchResult]) -> Vec<EfficiencyDelta> {
let mut deltas = Vec::new();
for (b, c) in baseline.iter().zip(current.iter()) {
let delta_pct = if b.execution_time_ms > 0.0 {
((c.execution_time_ms - b.execution_time_ms) / b.execution_time_ms) * 100.0
} else {
0.0
};
deltas.push(EfficiencyDelta {
kernel_name: b.kernel_name.clone(),
baseline_time_ms: b.execution_time_ms,
current_time_ms: c.execution_time_ms,
delta_pct,
is_regression: delta_pct > 5.0,
});
}
deltas
}
pub fn baseline_ms(kernel: &str) -> f64 {
if let Ok(raw) = std::fs::read_to_string("tuning/dispatch_table.json") {
if let Ok(parsed) = serde_json::from_str::<serde_json::Value>(&raw) {
let key = format!("{}_f32_1024", kernel);
if let Some(ms) = parsed
.pointer(&format!("/entries/{}/execution_time_ms", key))
.and_then(|v| v.as_f64())
{
return ms;
}
}
}
match kernel {
"vector_add" => 0.5,
"matmul" => 2.0,
"softmax" => 0.8,
_ => 1.0,
}
}
pub fn format_delta(delta: &EfficiencyDelta) -> String {
let sign = if delta.delta_pct >= 0.0 { "+" } else { "" };
let status = if delta.is_regression {
"REGRESSION"
} else {
"OK"
};
format!(
"{}: {}{:.2}% ({:.3}ms -> {:.3}ms) [{}]",
delta.kernel_name,
sign,
delta.delta_pct,
delta.baseline_time_ms,
delta.current_time_ms,
status
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_bench_config_quick() {
let config = BenchConfig::quick();
assert_eq!(config.max_duration, Duration::from_secs(30));
assert!(config.quick);
}
#[test]
fn test_compare_results() {
let baseline = vec![BenchResult {
kernel_name: "vector_add".to_string(),
elem_type: "f32".to_string(),
shape: vec![1024],
execution_time_ms: 1.0,
throughput_gflops: 100.0,
memory_bandwidth_gbps: 0.0,
gpu_utilization_pct: 0.0,
}];
let current = vec![BenchResult {
kernel_name: "vector_add".to_string(),
elem_type: "f32".to_string(),
shape: vec![1024],
execution_time_ms: 1.1,
throughput_gflops: 90.0,
memory_bandwidth_gbps: 0.0,
gpu_utilization_pct: 0.0,
}];
let deltas = compare_results(&baseline, ¤t);
assert_eq!(deltas.len(), 1);
assert!(deltas[0].delta_pct > 0.0);
}
#[test]
fn test_build_entry_efficiency() {
let result = BenchResult {
kernel_name: "vector_add".to_string(),
elem_type: "f32".to_string(),
shape: vec![1024],
execution_time_ms: 0.5,
throughput_gflops: 2.0,
memory_bandwidth_gbps: 8.0,
gpu_utilization_pct: 0.0,
};
let entry = build_entry(result);
assert_eq!(entry.kernel, "vector_add");
assert_eq!(entry.baseline_time_ms, 0.5);
assert!((entry.efficiency_pct - 100.0).abs() < 0.01);
assert!(!entry.is_regression);
}
#[test]
fn test_build_entry_regression() {
let result = BenchResult {
kernel_name: "vector_add".to_string(),
elem_type: "f32".to_string(),
shape: vec![1024],
execution_time_ms: 0.6,
throughput_gflops: 1.67,
memory_bandwidth_gbps: 6.67,
gpu_utilization_pct: 0.0,
};
let entry = build_entry(result);
assert!(entry.is_regression);
assert!(entry.efficiency_pct < 100.0);
}
#[test]
fn test_report_roundtrip() {
let report = BenchReport {
schema_version: "1.0".to_string(),
timestamp: "2026-06-28T12:00:00Z".to_string(),
host: "x86_64-pc-windows-msvc".to_string(),
kernels: vec![BenchKernelEntry {
kernel: "vector_add".to_string(),
elem: "f32".to_string(),
shape: vec![1024],
execution_time_ms: 0.5,
gflops: 2.048,
bandwidth_gbps: 8.192,
baseline_time_ms: 0.5,
efficiency_pct: 100.0,
is_regression: false,
}],
summary: BenchSummary {
total_kernels: 1,
regression_count: 0,
best_gflops: 2.048,
best_bandwidth_gbps: 8.192,
avg_efficiency_pct: 100.0,
},
};
let json = report_to_json(&report);
let parsed = report_from_json(&json).unwrap();
assert_eq!(parsed.schema_version, "1.0");
assert_eq!(parsed.kernels.len(), 1);
assert_eq!(parsed.kernels[0].kernel, "vector_add");
assert_eq!(parsed.summary.total_kernels, 1);
}
#[test]
fn test_elem_type_bytes() {
assert_eq!(elem_type_bytes("f32"), 4.0);
assert_eq!(elem_type_bytes("f16"), 2.0);
assert_eq!(elem_type_bytes("f64"), 8.0);
assert_eq!(elem_type_bytes("i32"), 4.0);
assert_eq!(elem_type_bytes("i8"), 1.0);
assert_eq!(elem_type_bytes("unknown"), 4.0);
}
}