Skip to main content

onnx_runtime_ir/
error.rs

1//! Error types for the IR crate (see `docs/ORT2.md` §22 and §3.3).
2//!
3//! The IR uses two error types:
4//! * [`GraphError`] — a single structural defect found while validating or
5//!   ordering a [`Graph`](crate::Graph).
6//! * [`IrError`] — the crate-level error returned by fallible IR operations,
7//!   a subset of the runtime's top-level `Error` (§22) restricted to what the
8//!   IR itself can produce.
9
10use crate::node::NodeId;
11use crate::value::ValueId;
12
13/// A convenience `Result` alias for IR operations.
14pub type Result<T> = std::result::Result<T, IrError>;
15
16/// A single structural defect in a [`Graph`](crate::Graph).
17#[derive(Clone, Debug, PartialEq, Eq)]
18pub enum GraphError {
19    /// A node references a value id that is not live in the value arena.
20    DanglingValue(ValueId),
21    /// A referenced node id is not live in the node arena.
22    DanglingNode(NodeId),
23    /// The graph contains a cycle (no valid topological order).
24    CycleDetected,
25    /// A graph output (or interior value) has no producing node.
26    MissingProducer(ValueId),
27    /// The same value is produced as an output more than once (SSA violation).
28    DuplicateOutput(ValueId),
29    /// A graph input has a producer node (inputs must be sources).
30    InputHasProducer(ValueId),
31    /// A value's `producer` link disagrees with the node's `outputs`.
32    ProducerLinkMismatch(ValueId),
33    /// A value's consumer-use set disagrees with a node's `inputs`.
34    ConsumerLinkMismatch(ValueId),
35    /// An opset import is malformed.
36    InvalidOpsetImport { domain: String, version: u64 },
37}
38
39impl std::fmt::Display for GraphError {
40    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41        match self {
42            GraphError::DanglingValue(v) => write!(f, "dangling value id {v:?}"),
43            GraphError::DanglingNode(n) => write!(f, "dangling node id {n:?}"),
44            GraphError::CycleDetected => write!(f, "cycle detected in graph"),
45            GraphError::MissingProducer(v) => write!(f, "value {v:?} has no producer"),
46            GraphError::DuplicateOutput(v) => write!(f, "value {v:?} produced more than once"),
47            GraphError::InputHasProducer(v) => write!(f, "graph input {v:?} has a producer"),
48            GraphError::ProducerLinkMismatch(v) => {
49                write!(f, "producer link inconsistent for value {v:?}")
50            }
51            GraphError::ConsumerLinkMismatch(v) => {
52                write!(f, "consumer link inconsistent for value {v:?}")
53            }
54            GraphError::InvalidOpsetImport { domain, version } => {
55                write!(f, "invalid opset import: domain={domain} version={version}")
56            }
57        }
58    }
59}
60
61impl std::error::Error for GraphError {}
62
63/// Crate-level error for fallible IR operations.
64#[derive(Debug, thiserror::Error)]
65pub enum IrError {
66    /// One or more structural defects were found by
67    /// [`Graph::validate`](crate::Graph::validate).
68    #[error("graph validation failed: {0:?}")]
69    GraphInvalid(Vec<GraphError>),
70
71    /// A cycle was detected while computing a topological order.
72    #[error("cycle detected in graph")]
73    CycleDetected,
74
75    /// A referenced value id is not live in the graph.
76    #[error("unknown value id: {0:?}")]
77    UnknownValue(ValueId),
78
79    /// A referenced node id is not live in the graph.
80    #[error("unknown node id: {0:?}")]
81    UnknownNode(NodeId),
82
83    /// Two shapes that were required to match did not.
84    #[error("shape mismatch: expected {expected:?}, got {actual:?}")]
85    ShapeMismatch {
86        expected: Vec<usize>,
87        actual: Vec<usize>,
88    },
89
90    /// Two shapes could not be broadcast together.
91    #[error("broadcast incompatible: {a:?} vs {b:?}")]
92    BroadcastIncompatible { a: Vec<usize>, b: Vec<usize> },
93
94    /// An opset version the IR does not model.
95    #[error("unsupported opset: domain={domain}, version={version}")]
96    UnsupportedOpset { domain: String, version: u64 },
97}
98
99impl From<Vec<GraphError>> for IrError {
100    fn from(errors: Vec<GraphError>) -> Self {
101        IrError::GraphInvalid(errors)
102    }
103}