onnx_runtime_ir/value.rs
1//! Graph values: the typed, shaped SSA edges between nodes.
2
3use crate::arena::ArenaKey;
4use crate::device::DeviceId;
5use crate::dtype::DataType;
6use crate::layout::TensorLayout;
7use crate::node::NodeId;
8use crate::shape::Shape;
9
10/// Unique identifier for a [`Value`] within a [`Graph`](crate::Graph).
11#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
12pub struct ValueId(pub u32);
13
14impl ArenaKey for ValueId {
15 fn from_raw(raw: u32) -> Self {
16 ValueId(raw)
17 }
18 fn to_raw(self) -> u32 {
19 self.0
20 }
21}
22
23/// A value flowing through the graph — the output of one node and the input of
24/// zero or more others (SSA form: at most one producer).
25///
26/// Carries a first-class [`TensorLayout`] and optional [`DeviceId`] placement,
27/// beyond ONNX's plain type+shape.
28#[derive(Clone, Debug, PartialEq)]
29pub struct Value {
30 pub id: ValueId,
31 /// Optional name (graph inputs/outputs and initializers are named; interior
32 /// SSA values may be anonymous).
33 pub name: Option<String>,
34 pub dtype: DataType,
35 pub shape: Shape,
36 pub layout: TensorLayout,
37 /// Device placement, filled in by the placement pass.
38 pub device: Option<DeviceId>,
39 /// The node that produces this value, or `None` for graph inputs and
40 /// initializers.
41 pub producer: Option<NodeId>,
42 /// Nodes that consume this value (one entry per consuming input slot).
43 pub consumers: Vec<NodeId>,
44}
45
46impl Value {
47 /// A new anonymous value with a contiguous default layout and no edges.
48 pub fn new(id: ValueId, dtype: DataType, shape: Shape) -> Self {
49 Self {
50 id,
51 name: None,
52 dtype,
53 shape,
54 layout: TensorLayout::contiguous(),
55 device: None,
56 producer: None,
57 consumers: Vec::new(),
58 }
59 }
60
61 /// Whether this value has no producing node (graph input / initializer).
62 pub fn is_source(&self) -> bool {
63 self.producer.is_none()
64 }
65
66 /// The static rank (number of dimensions).
67 pub fn rank(&self) -> usize {
68 self.shape.len()
69 }
70}