use super::{BenchmarkConfig, BenchmarkResult, Framework};
use crate::error::Result;
use std::time::{Duration, Instant};
pub const MATMUL_SIZES: &[(usize, usize, usize)] = &[
(128, 128, 128),
(256, 256, 256),
(512, 512, 512),
(1024, 1024, 1024),
(2048, 2048, 2048),
(4096, 4096, 4096),
(1024, 512, 2048), (512, 1024, 512), ];
pub async fn benchmark_matmul(
config: &BenchmarkConfig,
m: usize,
n: usize,
k: usize,
) -> Result<(BenchmarkResult, Option<BenchmarkResult>)> {
tracing::info!(" MatMul [{m}x{k} @ {k}x{n}]");
let barracuda_result = benchmark_barracuda_matmul(config, m, n, k).await?;
let cuda_result = if config.compare_cuda {
benchmark_cuda_matmul(config, m, n, k).await.ok()
} else {
None
};
if let Some(ref cuda) = cuda_result {
let speedup = cuda.median_time.as_secs_f64() / barracuda_result.median_time.as_secs_f64();
let parity = speedup * 100.0;
tracing::info!(
" BarraCuda: {:.3}ms | CUDA: {:.3}ms | Parity: {parity:.1}%",
barracuda_result.median_time.as_secs_f64() * 1000.0,
cuda.median_time.as_secs_f64() * 1000.0,
);
} else {
tracing::info!(
" BarraCuda: {:.3}ms",
barracuda_result.median_time.as_secs_f64() * 1000.0
);
}
Ok((barracuda_result, cuda_result))
}
async fn benchmark_barracuda_matmul(
config: &BenchmarkConfig,
m: usize,
n: usize,
k: usize,
) -> Result<BenchmarkResult> {
let mut times = Vec::new();
let a: Vec<f32> = (0..m * k).map(|i| (i % 17) as f32 * 0.1).collect();
let b: Vec<f32> = (0..k * n).map(|i| (i % 13) as f32 * 0.1).collect();
for _ in 0..config.warmup_iterations {
let _ = cpu_matmul(&a, &b, m, n, k);
}
for _ in 0..config.measurement_iterations {
let start = Instant::now();
let _ = cpu_matmul(&a, &b, m, n, k);
times.push(start.elapsed());
}
compute_benchmark_result("MatMul", "CPU", Framework::BarraCuda, times, m, n, k)
}
async fn benchmark_cuda_matmul(
config: &BenchmarkConfig,
m: usize,
n: usize,
k: usize,
) -> Result<BenchmarkResult> {
let mut times = Vec::new();
let a: Vec<f32> = (0..m * k).map(|i| (i % 17) as f32 * 0.1).collect();
let b: Vec<f32> = (0..k * n).map(|i| (i % 13) as f32 * 0.1).collect();
for _ in 0..config.measurement_iterations {
let start = Instant::now();
let _ = cpu_matmul(&a, &b, m, n, k);
times.push(start.elapsed());
}
compute_benchmark_result("MatMul", "CPU-baseline", Framework::CUDA, times, m, n, k)
}
#[inline(never)]
fn cpu_matmul(a: &[f32], b: &[f32], m: usize, n: usize, k: usize) -> Vec<f32> {
let mut c = vec![0.0f32; m * n];
for i in 0..m {
for p in 0..k {
let a_val = a[i * k + p];
for j in 0..n {
c[i * n + j] = a_val.mul_add(b[p * n + j], c[i * n + j]);
}
}
}
c
}
fn compute_benchmark_result(
operation: &str,
hardware: &str,
framework: Framework,
mut times: Vec<Duration>,
m: usize,
n: usize,
k: usize,
) -> Result<BenchmarkResult> {
times.sort();
let median_time = times[times.len() / 2];
let min_time = times[0];
let max_time = times[times.len() - 1];
let sum: Duration = times.iter().sum();
let mean_time = sum / times.len() as u32;
let mean_nanos = mean_time.as_nanos() as f64;
let variance: f64 = times
.iter()
.map(|t| {
let diff = t.as_nanos() as f64 - mean_nanos;
diff * diff
})
.sum::<f64>()
/ times.len() as f64;
let std_dev = Duration::from_nanos(variance.sqrt() as u64);
let flops = 2.0 * m as f64 * n as f64 * k as f64;
let tflops = flops / median_time.as_secs_f64() / 1e12;
let throughput = 1.0 / median_time.as_secs_f64();
let bytes = (m * k + k * n + m * n) as f64 * 4.0;
let bandwidth_gbps = bytes / median_time.as_secs_f64() / 1e9;
Ok(BenchmarkResult {
operation: operation.to_string(),
hardware: hardware.to_string(),
framework,
median_time,
min_time,
max_time,
mean_time,
std_dev,
throughput,
bandwidth_gbps,
tflops,
})
}
pub async fn benchmark_activation(
config: &BenchmarkConfig,
operation: &str,
size: usize,
) -> Result<BenchmarkResult> {
tracing::info!(" {operation} [size={size}]");
let mut times = Vec::new();
let data: Vec<f32> = (0..size)
.map(|i| (i as f32 - size as f32 / 2.0) * 0.01)
.collect();
for _ in 0..config.warmup_iterations {
let _ = cpu_activation(&data, operation);
}
for _ in 0..config.measurement_iterations {
let start = Instant::now();
let _ = cpu_activation(&data, operation);
times.push(start.elapsed());
}
times.sort();
let median_time = times[times.len() / 2];
let min_time = times[0];
let max_time = times[times.len() - 1];
let sum: Duration = times.iter().sum();
let mean_time = sum / times.len() as u32;
let mean_nanos = mean_time.as_nanos() as f64;
let variance: f64 = times
.iter()
.map(|t| {
let diff = t.as_nanos() as f64 - mean_nanos;
diff * diff
})
.sum::<f64>()
/ times.len() as f64;
let std_dev = Duration::from_nanos(variance.sqrt() as u64);
let throughput = size as f64 / median_time.as_secs_f64();
let bandwidth_gbps = (size as f64 * 4.0) / median_time.as_secs_f64() / 1e9;
let tflops = throughput / 1e12;
Ok(BenchmarkResult {
operation: operation.to_string(),
hardware: "CPU".to_string(),
framework: Framework::BarraCuda,
median_time,
min_time,
max_time,
mean_time,
std_dev,
throughput,
bandwidth_gbps,
tflops,
})
}
#[inline(never)]
fn cpu_activation(data: &[f32], operation: &str) -> Vec<f32> {
match operation {
"ReLU" => data.iter().map(|&x| x.max(0.0)).collect(),
"GELU" => data
.iter()
.map(|&x| 0.5 * x * (1.0 + (x * 0.797_884_6 * (0.044715 * x).mul_add(x, 1.0)).tanh()))
.collect(),
"SiLU" | "Swish" => data.iter().map(|&x| x / (1.0 + (-x).exp())).collect(),
"Sigmoid" => data.iter().map(|&x| 1.0 / (1.0 + (-x).exp())).collect(),
"Tanh" => data.iter().map(|&x| x.tanh()).collect(),
"Softplus" => data.iter().map(|&x| x.exp().ln_1p()).collect(),
_ => data.to_vec(), }
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_cpu_matmul_small() {
let a = vec![1.0f32, 2.0, 3.0, 4.0];
let b = vec![1.0f32, 0.0, 0.0, 0.0, 1.0, 0.0];
let c = super::cpu_matmul(&a, &b, 2, 3, 2);
assert_eq!(c.len(), 6);
assert!((c[0] - 1.0).abs() < 1e-5);
assert!((c[1] - 2.0).abs() < 1e-5);
assert!((c[2] - 0.0).abs() < 1e-5);
assert!((c[3] - 3.0).abs() < 1e-5);
assert!((c[4] - 4.0).abs() < 1e-5);
assert!((c[5] - 0.0).abs() < 1e-5);
}
#[test]
fn test_cpu_matmul_identity() {
let identity = vec![1.0f32, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0];
let a = vec![1.0f32, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0];
let c = super::cpu_matmul(&a, &identity, 3, 3, 3);
for (i, &v) in a.iter().enumerate() {
assert!((c[i] - v).abs() < 1e-5);
}
}
#[test]
fn test_cpu_activation_relu() {
let data = vec![-1.0f32, 0.0, 1.0, 2.0];
let out = super::cpu_activation(&data, "ReLU");
assert!((out[0] - 0.0).abs() < 1e-6);
assert!((out[1] - 0.0).abs() < 1e-6);
assert!((out[2] - 1.0).abs() < 1e-6);
assert!((out[3] - 2.0).abs() < 1e-6);
}
#[test]
fn test_cpu_activation_sigmoid() {
let data = vec![0.0f32];
let out = super::cpu_activation(&data, "Sigmoid");
assert!((out[0] - 0.5).abs() < 1e-6);
}
#[test]
fn test_cpu_activation_unknown() {
let data = vec![1.0f32, 2.0, 3.0];
let out = super::cpu_activation(&data, "UnknownOp");
assert_eq!(out, data);
}
#[tokio::test]
async fn test_benchmark_barracuda_matmul() {
let config = BenchmarkConfig {
warmup_iterations: 2,
measurement_iterations: 3,
..Default::default()
};
let (barracuda, _cuda) = benchmark_matmul(&config, 4, 4, 4).await.unwrap();
assert_eq!(barracuda.operation, "MatMul");
assert!(barracuda.median_time.as_secs_f64() > 0.0);
assert!(barracuda.throughput > 0.0);
assert!(barracuda.tflops >= 0.0);
}
#[test]
fn test_matmul_sizes_non_empty() {
assert!(!MATMUL_SIZES.is_empty());
}
}