Skip to main content

onnx_runtime_shape_inference/
error.rs

1//! Errors raised by shape inference.
2
3use onnx_runtime_ir::ValueId;
4
5/// An error produced while inferring shapes.
6///
7/// Inference is *permissive by default*: an unsupported operator or an
8/// under-specified input never errors — the affected outputs are simply left
9/// unresolved (see [`crate::InferenceReport`]). These variants are raised only
10/// for genuine contract violations: a malformed graph (cycle), an operator used
11/// with the wrong arity/rank, or — under [`MergePolicy::Strict`] — a concrete
12/// dimension conflict between an inferred and a declared shape.
13///
14/// [`MergePolicy::Strict`]: crate::MergePolicy::Strict
15#[derive(Debug, thiserror::Error, PartialEq, Eq)]
16#[non_exhaustive]
17pub enum ShapeInferError {
18    /// The graph could not be topologically ordered (it contains a cycle).
19    #[error("graph has a cycle; cannot order nodes for shape inference")]
20    CycleDetected,
21
22    /// An operator was invoked with the wrong number of inputs.
23    #[error("op `{op}`: expected {expected} inputs, found {found}")]
24    Arity {
25        op: String,
26        expected: String,
27        found: usize,
28    },
29
30    /// An input had a rank the operator cannot accept.
31    #[error("op `{op}`: input #{index} has invalid rank {rank} ({detail})")]
32    InvalidRank {
33        op: String,
34        index: usize,
35        rank: usize,
36        detail: String,
37    },
38
39    /// An attribute required by the operator was missing or the wrong type.
40    #[error("op `{op}`: attribute `{attr}` is missing or has the wrong type")]
41    MissingAttribute { op: String, attr: String },
42
43    /// A structural inconsistency detected while applying an op rule (e.g. a
44    /// contraction-dimension mismatch in `MatMul`, or an out-of-range axis).
45    #[error("op `{op}`: {detail}")]
46    Invalid { op: String, detail: String },
47
48    /// Under [`MergePolicy::Strict`](crate::MergePolicy::Strict), an inferred
49    /// dimension disagreed with the value's declared dimension. Only concrete
50    /// (static) disagreements are reported; symbolic differences are treated as
51    /// naming and never conflict (see [`crate::context::merge_shapes`]).
52    #[error(
53        "value {value:?}: inferred dim {inferred} conflicts with declared dim {declared} at axis {axis}"
54    )]
55    ShapeConflict {
56        value: ValueId,
57        axis: usize,
58        inferred: i64,
59        declared: i64,
60    },
61
62    /// Under [`MergePolicy::Strict`](crate::MergePolicy::Strict), an inferred
63    /// rank disagreed with the value's declared rank.
64    #[error("value {value:?}: inferred rank {inferred} conflicts with declared rank {declared}")]
65    RankConflict {
66        value: ValueId,
67        inferred: usize,
68        declared: usize,
69    },
70}