-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbench_vball_cpu.rs
More file actions
72 lines (61 loc) · 2.46 KB
/
Copy pathbench_vball_cpu.rs
File metadata and controls
72 lines (61 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//! Benchmark: VballNetGrid CPU inference timing.
//!
//! Usage:
//! cargo run --release --example bench_vball_cpu -- /path/to/model.onnx [iterations]
use std::collections::HashMap;
use std::time::Instant;
use yscv_onnx::{load_onnx_model_from_file, profile_onnx_model_cpu, run_onnx_model};
use yscv_tensor::Tensor;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let args: Vec<String> = std::env::args().skip(1).collect();
if args.is_empty() {
eprintln!("Usage: bench_vball_cpu <model.onnx> [iterations]");
std::process::exit(1);
}
let model_path = &args[0];
let iterations: usize = args.get(1).and_then(|s| s.parse().ok()).unwrap_or(10);
println!("Loading model: {model_path}");
let model = load_onnx_model_from_file(model_path)?;
let input_name = model.inputs.first().cloned().unwrap();
let output_name = model.outputs.first().cloned().unwrap();
println!("Input: {input_name}, Output: {output_name}");
// VballNetGridV1b: input [1, 9, 432, 768]
let input = Tensor::zeros(vec![1, 9, 432, 768])?;
// Profile
if std::env::var("PROFILE").is_ok() {
println!("Profiling...");
let mut pinputs = HashMap::new();
pinputs.insert(input_name.clone(), input.clone());
let _ = profile_onnx_model_cpu(&model, pinputs);
}
// Warmup
println!("Warmup...");
let mut inputs = HashMap::new();
inputs.insert(input_name.clone(), input.clone());
let _ = run_onnx_model(&model, inputs)?;
// Benchmark
println!("Running {iterations} iterations...");
let mut times = Vec::with_capacity(iterations);
for i in 0..iterations {
let mut inputs = HashMap::new();
inputs.insert(input_name.clone(), input.clone());
let t0 = Instant::now();
let _ = run_onnx_model(&model, inputs)?;
let elapsed = t0.elapsed();
times.push(elapsed.as_secs_f64() * 1000.0);
println!(" iter {}: {:.1} ms", i + 1, times[i]);
}
times.sort_by(|a, b| a.partial_cmp(b).unwrap());
let mean = times.iter().sum::<f64>() / times.len() as f64;
let median = times[times.len() / 2];
let min = times[0];
let max = times[times.len() - 1];
println!("\n--- VballNetGrid CPU Inference ---");
println!("Iterations: {iterations}");
println!("Mean: {mean:.1} ms");
println!("Median: {median:.1} ms");
println!("Min: {min:.1} ms");
println!("Max: {max:.1} ms");
println!("FPS: {:.1}", 1000.0 / mean);
Ok(())
}