Repository navigation
Closed
Description
When running below program with rustc 1.86.0 on x86_64 ubuntu 22.04 with TensorRT EP (Needs GPU).
Does not occur every time. Increase num_sessions to increase probability of occuring.
image_resize.onnx (Expires in 7 days).
use ndarray::Array;
use ort::{
execution_providers::TensorRTExecutionProvider,
inputs,
session::{builder::GraphOptimizationLevel, Session},
value::Value,
};
use std::{sync::Arc, thread};
pub fn main() {
let model_bytes = std::include_bytes!("image_resize.onnx");
let mut sessions = vec![];
let mut handles = vec![];
let num_sessions = 200;
for i in 0..num_sessions {
let session_builder = Session::builder()
.unwrap()
.with_optimization_level(GraphOptimizationLevel::Level3)
.unwrap()
.with_intra_threads(1)
.unwrap()
.with_execution_providers([TensorRTExecutionProvider::default()
.with_device_id(0)
.build()
.error_on_failure()])
.unwrap();
let session = Arc::new(
session_builder
.commit_from_memory(model_bytes)
.unwrap_or_else(|e| {
panic!("Failed to create session {}: {:?}", i, e);
}),
);
sessions.push(session.clone());
}
for (i, session) in sessions.iter().enumerate() {
let session = session.clone();
let input_tensor = Value::from_array(
Array::from_shape_vec(vec![1, 3, 12, 12], vec![1.0f32; 1 * 3 * 12 * 12]).unwrap(),
)
.unwrap();
let target_size_tensor =
Value::from_array(Array::from_shape_vec(vec![2], vec![6i64, 6]).unwrap()).unwrap();
let handle = thread::spawn(move || {
let result = session.run(
inputs!["image" => input_tensor, "target_size" => target_size_tensor].unwrap(),
);
match result {
Ok(_) => {
println!("Thread {} completed successfully.", i);
}
Err(e) => {
eprintln!("Thread {} session run failed: {:?}", i, e);
}
}
});
handles.push(handle);
}
println!(
"All {} threads spawned. Waiting for completion...",
num_sessions
);
for (idx, handle) in handles.into_iter().enumerate() {
match handle.join() {
Ok(_) => {}
Err(e) => {
eprintln!("Thread {} panicked: {:?}", idx, e);
}
}
}
println!("All session runs completed.");
}
Activity
maaft commentedon May 12, 2025
related (temporary fix):
This is my attempt for a temporary fix to above bug: Create and use sessions in the same thread. While doing this, another bug occured
When we guard session creation by a global mutex (see comments), this is also fixed.
Related upstream issue (but unfortunately just closed without explanation): https://github.com/microsoft/onnxruntime/issues/22663
fix(sys): update binaries with fix for #392
partially fix pykeio/ort#392