pub struct Graph {
pub nodes: Arena<NodeId, Node>,
pub values: Arena<ValueId, Value>,
pub inputs: Vec<ValueId>,
pub outputs: Vec<ValueId>,
pub initializers: HashMap<ValueId, WeightRef>,
pub symbol_constraints: HashMap<SymbolId, SymbolConstraints>,
pub opset_imports: HashMap<String, u64>,
pub subgraphs: HashMap<(NodeId, String), Graph>,
/* private fields */
}Expand description
A computation graph in SSA form.
Nodes and values live in Arenas keyed by NodeId / ValueId. The
mutation API keeps producer/consumer edges consistent, so optimization
passes can rewrite the graph and then Graph::validate it.
Fields§
§nodes: Arena<NodeId, Node>§values: Arena<ValueId, Value>§inputs: Vec<ValueId>Graph inputs, in order. These have no producer.
outputs: Vec<ValueId>Graph outputs, in order.
initializers: HashMap<ValueId, WeightRef>Constant initializer weights, keyed by the value they populate.
symbol_constraints: HashMap<SymbolId, SymbolConstraints>Constraints on symbolic dimensions.
opset_imports: HashMap<String, u64>Imported opsets: domain → version.
subgraphs: HashMap<(NodeId, String), Graph>Subgraph bodies for control-flow ops, keyed by (node, attr_name).
Implementations§
Source§impl Graph
impl Graph
Sourcepub fn node(&self, id: NodeId) -> &Node
pub fn node(&self, id: NodeId) -> &Node
Borrow a node. Panics if id is not live; use
Graph::try_node for a checked lookup.
Sourcepub fn node_mut(&mut self, id: NodeId) -> &mut Node
pub fn node_mut(&mut self, id: NodeId) -> &mut Node
Mutably borrow a node. Panics if id is not live.
Sourcepub fn value(&self, id: ValueId) -> &Value
pub fn value(&self, id: ValueId) -> &Value
Borrow a value. Panics if id is not live; use
Graph::try_value for a checked lookup.
Sourcepub fn value_mut(&mut self, id: ValueId) -> &mut Value
pub fn value_mut(&mut self, id: ValueId) -> &mut Value
Mutably borrow a value. Panics if id is not live.
Sourcepub fn num_values(&self) -> usize
pub fn num_values(&self) -> usize
Number of live values.
Sourcepub fn create_symbol(&mut self, name: Option<String>) -> SymbolId
pub fn create_symbol(&mut self, name: Option<String>) -> SymbolId
Allocate a fresh symbolic dimension with an optional name (no dedup).
Sourcepub fn intern_symbol(&mut self, name: &str) -> SymbolId
pub fn intern_symbol(&mut self, name: &str) -> SymbolId
Intern a symbolic dimension by protobuf dim-param name: repeated names
resolve to the same SymbolId (graph-construction invariant §3.5.4).
Sourcepub fn create_value(&mut self, dtype: DataType, shape: Shape) -> ValueId
pub fn create_value(&mut self, dtype: DataType, shape: Shape) -> ValueId
Create a new anonymous value with a contiguous default layout.
Sourcepub fn create_named_value(
&mut self,
name: impl Into<String>,
dtype: DataType,
shape: Shape,
) -> ValueId
pub fn create_named_value( &mut self, name: impl Into<String>, dtype: DataType, shape: Shape, ) -> ValueId
Create a new named value.
Sourcepub fn add_input(&mut self, value: ValueId)
pub fn add_input(&mut self, value: ValueId)
Register value as a graph input (also clears any producer link).
Sourcepub fn add_output(&mut self, value: ValueId)
pub fn add_output(&mut self, value: ValueId)
Register value as a graph output.
Sourcepub fn set_initializer(&mut self, value: ValueId, weight: WeightRef)
pub fn set_initializer(&mut self, value: ValueId, weight: WeightRef)
Attach initializer weights to value.
Sourcepub fn predecessors(&self, node: NodeId) -> Vec<NodeId>
pub fn predecessors(&self, node: NodeId) -> Vec<NodeId>
Direct predecessors: nodes that produce this node’s inputs.
Sourcepub fn successors(&self, node: NodeId) -> Vec<NodeId>
pub fn successors(&self, node: NodeId) -> Vec<NodeId>
Direct successors: nodes that consume this node’s outputs.
Sourcepub fn nodes_between(
&self,
inputs: &[ValueId],
outputs: &[ValueId],
) -> Vec<NodeId>
pub fn nodes_between( &self, inputs: &[ValueId], outputs: &[ValueId], ) -> Vec<NodeId>
All nodes that lie on a path between inputs and outputs.
Walks backwards from outputs via producer edges, stopping at any value
in inputs. Used to extract subgraphs for EP capability claims (§3.4).
Sourcepub fn topological_order(&self) -> Result<Vec<NodeId>, GraphError>
pub fn topological_order(&self) -> Result<Vec<NodeId>, GraphError>
Topological order of nodes via Kahn’s algorithm.
Ties are broken by ascending NodeId for deterministic output.
Returns GraphError::CycleDetected if the graph has a cycle.
Sourcepub fn insert_node(&mut self, node: Node) -> NodeId
pub fn insert_node(&mut self, node: Node) -> NodeId
Insert a node, wiring its producer/consumer edges. The node’s id
field is overwritten with the freshly allocated NodeId.
Sourcepub fn remove_node(&mut self, id: NodeId)
pub fn remove_node(&mut self, id: NodeId)
Remove a node, disconnecting its edges. Output values left with no consumers (and not graph I/O or initializers) are deleted.
Sourcepub fn replace_node(&mut self, old: NodeId, new: Node) -> NodeId
pub fn replace_node(&mut self, old: NodeId, new: Node) -> NodeId
Replace node old in place with new, preserving the NodeId.
The old node’s edges are disconnected and the new node’s edges are
connected. Values that were outputs of old but not of new are left
in place (producer cleared); the caller may prune them.
Sourcepub fn insert_on_edge(&mut self, value: ValueId, new_node: Node) -> NodeId
pub fn insert_on_edge(&mut self, value: ValueId, new_node: Node) -> NodeId
Splice new_node onto the edge feeding out of value:
producer(value) → [new_node] → consumers(value).
new_node’s single input becomes value, and it produces a fresh value
that replaces value in all of value’s original consumers.
Sourcepub fn replace_all_uses(&mut self, old_value: ValueId, new_value: ValueId)
pub fn replace_all_uses(&mut self, old_value: ValueId, new_value: ValueId)
Replace every use of old_value with new_value in consumer nodes and
in the graph output list, moving consumer edges accordingly.