Skip to main content

quickstart/
quickstart.rs

1//! The README example, kept compilable: load an engine, bind every I/O tensor, run once, read the
2//! outputs back.
3//!
4//! ```text
5//! cargo run --release --example quickstart -- model_fp16.engine
6//! ```
7
8use trt_runner::{cuda, DeviceBuffer, Engine, IoMode, PinnedBuffer, Stream};
9
10fn main() -> trt_runner::Result<()> {
11    let path = std::env::args()
12        .nth(1)
13        .unwrap_or_else(|| "model_fp16.engine".to_string());
14
15    cuda::set_device(0)?;
16    let engine = Engine::from_file(&path)?;
17    let stream = Stream::new()?;
18    let mut context = engine.context()?;
19
20    // One device buffer per I/O tensor, sized from the engine's own metadata.
21    let mut buffers = Vec::new();
22    for info in engine.io() {
23        let bytes = info.size_bytes().expect("a static shape");
24        let mut buffer = DeviceBuffer::zeroed(bytes, &stream)?;
25        if info.mode == IoMode::Input {
26            let mut host = PinnedBuffer::new(bytes)?;
27            host.f32s_mut().fill(0.5);
28            buffer.copy_from_host(host.bytes(), &stream)?;
29        }
30        // SAFETY: `buffers` is alive until after the synchronised run below.
31        unsafe { context.bind_buffer(&info.name, &buffer)? };
32        buffers.push((info.name.clone(), info.mode, buffer));
33    }
34
35    context.run(&stream)?; // enqueue plus synchronize
36
37    for (name, mode, buffer) in &buffers {
38        if *mode == IoMode::Output {
39            let mut out = vec![0u8; buffer.size_bytes()];
40            // copy_to_host waits for the stream, so these bytes are real.
41            buffer.copy_to_host(&mut out, &stream)?;
42            println!("{name}: read back {} bytes", out.len());
43        }
44    }
45    Ok(())
46}