trt-runner 0.1.0

Minimal TensorRT 10 inference runner: deserialize an engine, bind I/O tensors, enqueue V3. No bindings generator, no runtime dependencies.
Documentation
use std::fmt;

use crate::ffi;

/// Everything this crate can fail at, carrying the TensorRT or CUDA diagnostic when there is one.
#[derive(Debug)]
pub enum Error {
    /// Reading the engine file failed.
    Io(std::io::Error),
    /// A CUDA runtime call returned an error code.
    Cuda {
        /// The CUDA function that failed.
        call: &'static str,
        /// Raw `cudaError_t` value.
        code: i32,
        /// Human readable form of that code.
        message: String,
    },
    /// A TensorRT call returned false, or the runtime refused the engine.
    TensorRt {
        /// The TensorRT method that failed.
        call: &'static str,
        /// Whatever the TensorRT logger said just before the failure.
        message: String,
    },
    /// No tensor with that name exists in the engine.
    UnknownTensor {
        /// The name that was asked for.
        name: String,
        /// Every name the engine does expose.
        available: Vec<String>,
    },
    /// The engine uses an element type this crate cannot compute a size for.
    UnsupportedDataType {
        /// The offending tensor.
        tensor: String,
        /// Raw `nvinfer1::DataType` value.
        code: i32,
    },
    /// A tensor has dynamic dimensions, so its size is unknown until a shape is set.
    DynamicShape {
        /// The tensor with a `-1` in its declared shape.
        tensor: String,
        /// The declared shape.
        shape: Vec<i64>,
    },
    /// A buffer and the tensor it was bound to hold different numbers of bytes.
    SizeMismatch {
        /// The tensor involved.
        tensor: String,
        /// What the engine expects.
        expected: usize,
        /// What was supplied.
        found: usize,
    },
    /// A shape has more dimensions than TensorRT allows.
    TooManyDimensions {
        /// Dimensions supplied.
        found: usize,
        /// The limit, [`crate::MAX_DIMS`].
        max: usize,
    },
    /// A tensor name contains an embedded NUL and cannot reach the C API.
    InvalidTensorName(String),
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Error::Io(source) => write!(f, "io: {source}"),
            Error::Cuda {
                call,
                code,
                message,
            } => write!(f, "cuda: {call} failed ({code}): {message}"),
            Error::TensorRt { call, message } => {
                if message.is_empty() {
                    write!(f, "tensorrt: {call} failed")
                } else {
                    write!(f, "tensorrt: {call} failed: {message}")
                }
            }
            Error::UnknownTensor { name, available } => {
                let names: Vec<&str> = available.iter().map(String::as_str).collect();
                write!(
                    f,
                    "no tensor named {name:?}; the engine has {}",
                    names.join(", ")
                )
            }
            Error::UnsupportedDataType { tensor, code } => {
                write!(
                    f,
                    "tensor {tensor:?} has data type code {code}, which this crate cannot size"
                )
            }
            Error::DynamicShape { tensor, shape } => {
                write!(
                    f,
                    "tensor {tensor:?} has dynamic dimensions {shape:?}; set an input shape first"
                )
            }
            Error::SizeMismatch {
                tensor,
                expected,
                found,
            } => {
                write!(
                    f,
                    "tensor {tensor:?} needs {expected} bytes, the buffer has {found}"
                )
            }
            Error::TooManyDimensions { found, max } => {
                write!(
                    f,
                    "shape has {found} dimensions, TensorRT allows at most {max}"
                )
            }
            Error::InvalidTensorName(name) => write!(f, "tensor name {name:?} contains a NUL byte"),
        }
    }
}

impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Error::Io(source) => Some(source),
            _ => None,
        }
    }
}

impl From<std::io::Error> for Error {
    fn from(source: std::io::Error) -> Self {
        Error::Io(source)
    }
}

/// The message recorded by the shim: the last CUDA error string, or whatever TensorRT passed to the
/// logger before the failing call returned.
pub(crate) fn last_message() -> String {
    let raw = unsafe { ffi::trt_last_error() };
    if raw.is_null() {
        return String::new();
    }
    unsafe { std::ffi::CStr::from_ptr(raw) }
        .to_string_lossy()
        .into_owned()
}

pub(crate) fn cuda_check(code: i32, call: &'static str) -> Result<(), Error> {
    if code == 0 {
        return Ok(());
    }
    let message = last_message();
    Err(Error::Cuda {
        call,
        code,
        message,
    })
}

pub(crate) fn trt_check(code: i32, call: &'static str) -> Result<(), Error> {
    if code == 0 {
        return Ok(());
    }
    Err(Error::TensorRt {
        call,
        message: last_message(),
    })
}