#[derive(Debug, Clone)]
pub struct TensorRTConfig {
fp16: Option<bool>,
int8: Option<bool>,
cuda_graph: Option<bool>,
builder_optimization_level: Option<u8>,
engine_cache: Option<bool>,
engine_cache_path: Option<String>,
timing_cache: Option<bool>,
timing_cache_path: Option<String>,
device_id: Option<i32>,
max_workspace_size: Option<usize>,
min_subgraph_size: Option<usize>,
layer_norm_fp32_fallback: Option<bool>,
}
impl Default for TensorRTConfig {
fn default() -> Self {
Self {
fp16: Some(true),
cuda_graph: Some(true),
engine_cache: Some(true),
timing_cache: Some(true),
builder_optimization_level: Some(3),
int8: None,
engine_cache_path: None,
timing_cache_path: None,
device_id: None,
max_workspace_size: None,
min_subgraph_size: None,
layer_norm_fp32_fallback: None,
}
}
}
impl TensorRTConfig {
#[must_use]
pub const fn new() -> Self {
Self {
fp16: Some(true),
cuda_graph: Some(true),
engine_cache: Some(true),
timing_cache: Some(true),
builder_optimization_level: Some(3),
int8: None,
engine_cache_path: None,
timing_cache_path: None,
device_id: None,
max_workspace_size: None,
min_subgraph_size: None,
layer_norm_fp32_fallback: None,
}
}
#[must_use]
pub const fn with_fp16(mut self, enable: bool) -> Self {
self.fp16 = Some(enable);
self
}
#[must_use]
pub const fn with_int8(mut self, enable: bool) -> Self {
self.int8 = Some(enable);
self
}
#[must_use]
pub const fn with_cuda_graph(mut self, enable: bool) -> Self {
self.cuda_graph = Some(enable);
self
}
#[must_use]
pub const fn with_builder_optimization_level(mut self, level: u8) -> Self {
self.builder_optimization_level = Some(level);
self
}
#[must_use]
pub const fn with_engine_cache(mut self, enable: bool) -> Self {
self.engine_cache = Some(enable);
self
}
#[must_use]
pub fn with_engine_cache_path(mut self, path: impl Into<String>) -> Self {
self.engine_cache_path = Some(path.into());
self
}
#[must_use]
pub const fn with_timing_cache(mut self, enable: bool) -> Self {
self.timing_cache = Some(enable);
self
}
#[must_use]
pub fn with_timing_cache_path(mut self, path: impl Into<String>) -> Self {
self.timing_cache_path = Some(path.into());
self
}
#[must_use]
pub const fn with_device_id(mut self, device_id: i32) -> Self {
self.device_id = Some(device_id);
self
}
#[must_use]
pub const fn with_max_workspace_size(mut self, max_size: usize) -> Self {
self.max_workspace_size = Some(max_size);
self
}
#[must_use]
pub const fn with_min_subgraph_size(mut self, min_size: usize) -> Self {
self.min_subgraph_size = Some(min_size);
self
}
#[must_use]
pub const fn with_layer_norm_fp32_fallback(mut self, enable: bool) -> Self {
self.layer_norm_fp32_fallback = Some(enable);
self
}
pub(crate) fn apply_to(
self,
provider: ort::execution_providers::TensorRTExecutionProvider,
) -> ort::execution_providers::TensorRTExecutionProvider {
let mut p = provider;
if let Some(v) = self.fp16 {
p = p.with_fp16(v);
}
if let Some(v) = self.int8 {
p = p.with_int8(v);
}
if let Some(v) = self.cuda_graph {
p = p.with_cuda_graph(v);
}
if let Some(v) = self.builder_optimization_level {
p = p.with_builder_optimization_level(v);
}
if let Some(v) = self.engine_cache {
p = p.with_engine_cache(v);
}
if let Some(path) = self.engine_cache_path {
p = p.with_engine_cache_path(path);
}
if let Some(v) = self.timing_cache {
p = p.with_timing_cache(v);
}
if let Some(path) = self.timing_cache_path {
p = p.with_timing_cache_path(path);
}
if let Some(id) = self.device_id {
p = p.with_device_id(id);
}
if let Some(size) = self.max_workspace_size {
p = p.with_max_workspace_size(size);
}
if let Some(size) = self.min_subgraph_size {
p = p.with_min_subgraph_size(size);
}
if let Some(v) = self.layer_norm_fp32_fallback {
p = p.with_layer_norm_fp32_fallback(v);
}
p
}
}
#[cfg(test)]
mod tests {
#![allow(clippy::disallowed_methods)]
use super::*;
#[test]
fn test_tensorrt_config_default() {
let config = TensorRTConfig::default();
assert_eq!(config.fp16, Some(true));
assert_eq!(config.cuda_graph, Some(true));
assert_eq!(config.engine_cache, Some(true));
assert_eq!(config.timing_cache, Some(true));
assert_eq!(config.builder_optimization_level, Some(3));
assert_eq!(config.int8, None);
assert_eq!(config.engine_cache_path, None);
}
#[test]
fn test_tensorrt_config_new() {
let config = TensorRTConfig::new();
assert_eq!(config.fp16, Some(true));
assert_eq!(config.cuda_graph, Some(true));
assert_eq!(config.engine_cache, Some(true));
assert_eq!(config.timing_cache, Some(true));
assert_eq!(config.builder_optimization_level, Some(3));
}
#[test]
fn test_tensorrt_config_builder_pattern() {
let config = TensorRTConfig::new()
.with_fp16(false)
.with_device_id(1)
.with_max_workspace_size(1_000_000_000);
assert_eq!(config.fp16, Some(false));
assert_eq!(config.device_id, Some(1));
assert_eq!(config.max_workspace_size, Some(1_000_000_000));
}
#[test]
fn test_tensorrt_config_disable_all_optimizations() {
let config = TensorRTConfig::new()
.with_fp16(false)
.with_cuda_graph(false)
.with_engine_cache(false)
.with_timing_cache(false);
assert_eq!(config.fp16, Some(false));
assert_eq!(config.cuda_graph, Some(false));
assert_eq!(config.engine_cache, Some(false));
assert_eq!(config.timing_cache, Some(false));
}
#[test]
fn test_tensorrt_config_cache_paths() {
let config = TensorRTConfig::new()
.with_engine_cache_path("/tmp/engines")
.with_timing_cache_path("/tmp/timing");
assert_eq!(config.engine_cache_path, Some("/tmp/engines".to_string()));
assert_eq!(config.timing_cache_path, Some("/tmp/timing".to_string()));
}
#[test]
fn test_tensorrt_config_optimization_levels() {
let config0 = TensorRTConfig::new().with_builder_optimization_level(0);
let config5 = TensorRTConfig::new().with_builder_optimization_level(5);
assert_eq!(config0.builder_optimization_level, Some(0));
assert_eq!(config5.builder_optimization_level, Some(5));
}
#[test]
fn test_tensorrt_config_int8() {
let config = TensorRTConfig::new().with_int8(true);
assert_eq!(config.int8, Some(true));
}
#[test]
fn test_tensorrt_config_layer_norm_fallback() {
let config = TensorRTConfig::new().with_layer_norm_fp32_fallback(true);
assert_eq!(config.layer_norm_fp32_fallback, Some(true));
}
#[test]
fn test_tensorrt_config_min_subgraph_size() {
let config = TensorRTConfig::new().with_min_subgraph_size(5);
assert_eq!(config.min_subgraph_size, Some(5));
}
}