Skip to main content

trt_runner/
error.rs

1use std::fmt;
2
3use crate::ffi;
4
5/// Everything this crate can fail at, carrying the TensorRT or CUDA diagnostic when there is one.
6#[derive(Debug)]
7pub enum Error {
8    /// Reading the engine file failed.
9    Io(std::io::Error),
10    /// A CUDA runtime call returned an error code.
11    Cuda {
12        /// The CUDA function that failed.
13        call: &'static str,
14        /// Raw `cudaError_t` value.
15        code: i32,
16        /// Human readable form of that code.
17        message: String,
18    },
19    /// A TensorRT call returned false, or the runtime refused the engine.
20    TensorRt {
21        /// The TensorRT method that failed.
22        call: &'static str,
23        /// Whatever the TensorRT logger said just before the failure.
24        message: String,
25    },
26    /// No tensor with that name exists in the engine.
27    UnknownTensor {
28        /// The name that was asked for.
29        name: String,
30        /// Every name the engine does expose.
31        available: Vec<String>,
32    },
33    /// The engine uses an element type this crate cannot compute a size for.
34    UnsupportedDataType {
35        /// The offending tensor.
36        tensor: String,
37        /// Raw `nvinfer1::DataType` value.
38        code: i32,
39    },
40    /// A tensor has dynamic dimensions, so its size is unknown until a shape is set.
41    DynamicShape {
42        /// The tensor with a `-1` in its declared shape.
43        tensor: String,
44        /// The declared shape.
45        shape: Vec<i64>,
46    },
47    /// A buffer and the tensor it was bound to hold different numbers of bytes.
48    SizeMismatch {
49        /// The tensor involved.
50        tensor: String,
51        /// What the engine expects.
52        expected: usize,
53        /// What was supplied.
54        found: usize,
55    },
56    /// A shape has more dimensions than TensorRT allows.
57    TooManyDimensions {
58        /// Dimensions supplied.
59        found: usize,
60        /// The limit, [`crate::MAX_DIMS`].
61        max: usize,
62    },
63    /// A tensor name contains an embedded NUL and cannot reach the C API.
64    InvalidTensorName(String),
65}
66
67impl fmt::Display for Error {
68    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
69        match self {
70            Error::Io(source) => write!(f, "io: {source}"),
71            Error::Cuda {
72                call,
73                code,
74                message,
75            } => write!(f, "cuda: {call} failed ({code}): {message}"),
76            Error::TensorRt { call, message } => {
77                if message.is_empty() {
78                    write!(f, "tensorrt: {call} failed")
79                } else {
80                    write!(f, "tensorrt: {call} failed: {message}")
81                }
82            }
83            Error::UnknownTensor { name, available } => {
84                let names: Vec<&str> = available.iter().map(String::as_str).collect();
85                write!(
86                    f,
87                    "no tensor named {name:?}; the engine has {}",
88                    names.join(", ")
89                )
90            }
91            Error::UnsupportedDataType { tensor, code } => {
92                write!(
93                    f,
94                    "tensor {tensor:?} has data type code {code}, which this crate cannot size"
95                )
96            }
97            Error::DynamicShape { tensor, shape } => {
98                write!(
99                    f,
100                    "tensor {tensor:?} has dynamic dimensions {shape:?}; set an input shape first"
101                )
102            }
103            Error::SizeMismatch {
104                tensor,
105                expected,
106                found,
107            } => {
108                write!(
109                    f,
110                    "tensor {tensor:?} needs {expected} bytes, the buffer has {found}"
111                )
112            }
113            Error::TooManyDimensions { found, max } => {
114                write!(
115                    f,
116                    "shape has {found} dimensions, TensorRT allows at most {max}"
117                )
118            }
119            Error::InvalidTensorName(name) => write!(f, "tensor name {name:?} contains a NUL byte"),
120        }
121    }
122}
123
124impl std::error::Error for Error {
125    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
126        match self {
127            Error::Io(source) => Some(source),
128            _ => None,
129        }
130    }
131}
132
133impl From<std::io::Error> for Error {
134    fn from(source: std::io::Error) -> Self {
135        Error::Io(source)
136    }
137}
138
139/// The message recorded by the shim: the last CUDA error string, or whatever TensorRT passed to the
140/// logger before the failing call returned.
141pub(crate) fn last_message() -> String {
142    let raw = unsafe { ffi::trt_last_error() };
143    if raw.is_null() {
144        return String::new();
145    }
146    unsafe { std::ffi::CStr::from_ptr(raw) }
147        .to_string_lossy()
148        .into_owned()
149}
150
151pub(crate) fn cuda_check(code: i32, call: &'static str) -> Result<(), Error> {
152    if code == 0 {
153        return Ok(());
154    }
155    let message = last_message();
156    Err(Error::Cuda {
157        call,
158        code,
159        message,
160    })
161}
162
163pub(crate) fn trt_check(code: i32, call: &'static str) -> Result<(), Error> {
164    if code == 0 {
165        return Ok(());
166    }
167    Err(Error::TensorRt {
168        call,
169        message: last_message(),
170    })
171}