Skip to content

Commit bc07a6e

Browse files
authored
Rust tensorrt warmup script (HULKs#2312)
* Add rust script to precompile onnx models * Run pepsi format * Fix image width
1 parent 3539cec commit bc07a6e

5 files changed

Lines changed: 85 additions & 6 deletions

File tree

Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ members = [
5757
"tools/mujoco-simulator/mujoco-rust-server",
5858
"tools/parameter_tester",
5959
"tools/pepsi",
60+
"tools/tensorrt-compile",
6061
"tools/twix",
6162
"tools/vista",
6263
"tools/widget_gallery",

tools/machine-learning/multi-task-yolo/pyproject.toml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ description = "Add your description here"
55
readme = "README.md"
66
requires-python = ">=3.13"
77
dependencies = [
8-
"click>=8.3.1",
9-
"onnxruntime>=1.24.3",
10-
"onnxscript>=0.6.2",
11-
"onnxslim>=0.1.87",
12-
"torch>=2.10.0",
13-
"ultralytics>=8.4.21",
8+
"click>=8.3.1",
9+
"onnxruntime>=1.24.3",
10+
"onnxscript>=0.6.2",
11+
"onnxslim>=0.1.87",
12+
"torch>=2.10.0",
13+
"ultralytics>=8.4.21",
1414
]

tools/tensorrt-compile/Cargo.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "tensorrt-compile"
3+
version.workspace = true
4+
edition.workspace = true
5+
license.workspace = true
6+
homepage.workspace = true
7+
8+
[dependencies]
9+
clap = { workspace = true }
10+
color-eyre = { workspace = true }
11+
ndarray = { workspace = true }
12+
ort = { workspace = true, features = ["cuda", "tensorrt"] }

tools/tensorrt-compile/src/main.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
use std::path::PathBuf;
2+
3+
use clap::Parser;
4+
use color_eyre::{Result, eyre::Context};
5+
use ndarray::Array3;
6+
use ort::{
7+
execution_providers::{CUDAExecutionProvider, TensorRTExecutionProvider},
8+
inputs,
9+
session::{Session, SessionOutputs, builder::GraphOptimizationLevel},
10+
value::TensorRef,
11+
};
12+
13+
#[derive(Debug, Parser)]
14+
struct CliArguments {
15+
/// Path to onnx model
16+
onnx_path: PathBuf,
17+
18+
/// Path to cache folder
19+
#[arg(long, default_value = "/home/booster/.cache/hulk/tensor-rt")]
20+
cache_path: PathBuf,
21+
}
22+
23+
fn main() -> Result<()> {
24+
const IMAGE_WIDTH: usize = 544;
25+
const IMAGE_HEIGHT: usize = 448;
26+
27+
let args = CliArguments::parse();
28+
color_eyre::install()?;
29+
std::fs::create_dir_all(&args.cache_path).wrap_err("failed to create cache path")?;
30+
31+
let tensor_rt = TensorRTExecutionProvider::default()
32+
.with_device_id(0)
33+
.with_fp16(true)
34+
.with_engine_cache(true)
35+
.with_engine_cache_path(args.cache_path.display())
36+
.build()
37+
.error_on_failure();
38+
let cuda = CUDAExecutionProvider::default().build();
39+
40+
let mut session = Session::builder()?
41+
.with_execution_providers([tensor_rt, cuda])?
42+
.with_optimization_level(GraphOptimizationLevel::Level3)?
43+
.with_intra_threads(2)?
44+
.commit_from_file(args.onnx_path)?;
45+
46+
let sample_image = Array3::<u8>::default([IMAGE_HEIGHT / 2, IMAGE_WIDTH / 2, 6]);
47+
let outputs: SessionOutputs = session
48+
.run(inputs!["raw_bytes_input" => TensorRef::from_array_view(sample_image.view())?])?;
49+
let _ = outputs["network_detections"]
50+
.try_extract_array::<f32>()?
51+
.t()
52+
.into_owned();
53+
eprintln!("object detection setup complete");
54+
55+
Ok(())
56+
}

0 commit comments

Comments
 (0)