Skip to main content

rknn_runtime/
error.rs

1//! Error types for RKNN operations.
2
3use std::fmt;
4
5/// Everything that can go wrong when using RKNN.
6///
7/// Most variants carry the RKNN error code (a negative `i32`).
8/// The RKNN SDK doesn't document specific codes, but common ones are:
9/// - `-1` - generic failure
10/// - `-2` - invalid parameter
11/// - `-3` - device not found
12///
13/// # Example
14///
15/// ```rust,no_run
16/// use rknn_runtime::{RknnModel, Error};
17///
18/// match RknnModel::load("model.rknn") {
19///     Ok(model) => println!("Loaded!"),
20///     Err(Error::LibraryNotFound(path)) => {
21///         eprintln!("RKNN library not found at: {}", path);
22///         eprintln!("Make sure librknnmrt.so is installed on the device.");
23///     }
24///     Err(e) => eprintln!("Error: {}", e),
25/// }
26/// ```
27#[derive(Debug)]
28pub enum Error {
29    /// `librknnmrt.so` could not be loaded. Contains the attempted path.
30    LibraryNotFound(String),
31    /// A required function was not found in the loaded library.
32    SymbolNotFound(String),
33    /// `rknn_init()` failed. The model file may be corrupt or incompatible.
34    InitFailed(i32),
35    /// `rknn_query()` failed when reading tensor metadata.
36    QueryFailed(i32),
37    /// `rknn_run()` failed during inference.
38    InferenceFailed(i32),
39    /// NPU memory allocation returned null.
40    MemAllocFailed,
41    /// `rknn_mem_sync()` failed. Cache sync between NPU and CPU did not complete.
42    MemSyncFailed(i32),
43    /// `rknn_set_io_mem()` failed when binding a memory buffer to a tensor.
44    SetIoMemFailed(i32),
45    /// The model data is empty or invalid.
46    InvalidModel,
47    /// Output index is out of range.
48    InvalidIndex {
49        /// The index that was requested.
50        requested: usize,
51        /// How many outputs the model actually has.
52        available: usize,
53    },
54    /// File I/O error (e.g. model file not found).
55    IoError(std::io::Error),
56}
57
58/// Just display the error message.
59impl fmt::Display for Error {
60    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
61        match self {
62            Error::LibraryNotFound(path) => {
63                write!(f, "failed to load library: {}", path)
64            }
65            Error::SymbolNotFound(name) => {
66                write!(f, "symbol not found: {}", name)
67            }
68            Error::InitFailed(code) => {
69                write!(f, "rknn_init failed (code {})", code)
70            }
71            Error::QueryFailed(code) => {
72                write!(f, "rknn_query failed (code {})", code)
73            }
74            Error::InferenceFailed(code) => {
75                write!(f, "rknn_run failed (code {})", code)
76            }
77            Error::MemAllocFailed => {
78                write!(f, "rknn_create_mem returned null")
79            }
80            Error::MemSyncFailed(code) => {
81                write!(f, "rknn_mem_sync failed (code {})", code)
82            }
83            Error::SetIoMemFailed(code) => {
84                write!(f, "rknn_set_io_mem failed (code {})", code)
85            }
86            Error::InvalidModel => write!(f, "invalid or empty model data"),
87            Error::InvalidIndex {
88                requested,
89                available,
90            } => {
91                write!(
92                    f,
93                    "output index {} out of range (have {})",
94                    requested, available
95                )
96            }
97            Error::IoError(e) => write!(f, "I/O error: {}", e),
98        }
99    }
100}
101
102/// Allow chaining
103impl std::error::Error for Error {
104    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
105        match self {
106            Error::IoError(e) => Some(e),
107            _ => None,
108        }
109    }
110}
111
112/// Convert std::io::Error into crate's Error type.
113impl From<std::io::Error> for Error {
114    fn from(e: std::io::Error) -> Self {
115        Error::IoError(e)
116    }
117}