forked from mlomb/ort-batcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.rs
72 lines (60 loc) · 2.14 KB
/
example.rs
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
use ndarray::{ArrayD, Axis};
use ort::{CUDAExecutionProvider, Environment, SessionBuilder, Value};
use ort_batcher::batcher::Batcher;
use std::time::Duration;
fn main() -> ort::Result<()> {
tracing_subscriber::fmt::init();
let environment = Environment::builder()
.with_execution_providers([CUDAExecutionProvider::default().build()])
.build()?
.into_arc();
let session = SessionBuilder::new(&environment)?
.with_intra_threads(1)?
.with_model_from_memory(include_bytes!("../tests/model.onnx"))?;
{
let start = std::time::Instant::now();
// 128 threads
// 256 inferences each
// sequential
std::thread::scope(|s| {
for _ in 0..128 {
let session = &session;
let input = ArrayD::<f32>::zeros(vec![7, 8, 9]);
s.spawn(move || {
for _ in 0..256 {
let value = Value::from_array(input.clone().insert_axis(Axis(0))).unwrap();
let _output = session.run([value]).unwrap()[0]
.extract_tensor::<f32>()
.unwrap()
.view()
.index_axis(Axis(0), 0)
.to_owned();
}
});
}
});
println!("sequential: {:?}", start.elapsed());
}
let max_batch_size = 32;
let max_wait_time = Duration::from_millis(10);
let batcher = Batcher::spawn(session, max_batch_size, max_wait_time);
{
let start = std::time::Instant::now();
// 128 threads
// 256 inferences each
// batched
std::thread::scope(|s| {
for _ in 0..128 {
let batcher = &batcher;
let input = ArrayD::<f32>::zeros(vec![7, 8, 9]);
s.spawn(move || {
for _ in 0..256 {
let _output = batcher.run(vec![input.clone()]).unwrap();
}
});
}
});
println!("batched: {:?}", start.elapsed());
}
Ok(())
}