-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.rs
More file actions
40 lines (34 loc) · 1.25 KB
/
benchmark.rs
File metadata and controls
40 lines (34 loc) · 1.25 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
use std::path::Path;
use criterion::Criterion;
use miette::{miette, Context, IntoDiagnostic, Result};
use openvino::CompiledModel;
fn load_model(path: impl AsRef<Path>) -> Result<CompiledModel> {
let mut core = openvino::Core::new().into_diagnostic()?;
let onnx_path = path
.as_ref()
.to_str()
.ok_or_else(|| miette!("Model file path is not valid UTF-8"))?;
let model = core.read_model_from_file(onnx_path, "").into_diagnostic()?;
core.compile_model(&model, openvino::DeviceType::CPU)
.into_diagnostic()
}
/// Run a benchmark on an ONNX model.
pub fn run_benchmark_onnx(path: impl AsRef<Path>, c: &mut Criterion) -> Result<()> {
let mut model = load_model(&path).context("failed to load model")?;
let onnx_name = path
.as_ref()
.file_name()
.and_then(|s| s.to_str())
.ok_or_else(|| miette!("model file name is not valid UTF-8"))?;
c.bench_function(onnx_name, |b| {
b.iter(|| {
let request = model
.create_infer_request()
.expect("failed to create inference request");
std::hint::black_box(request)
.infer()
.expect("failed to complete inference");
});
});
Ok(())
}