pub struct Graph {
pub nodes: Vec<GraphNode>,
pub names: BTreeMap<usize, String>,
}Expand description
The traced computational graph: a list of GraphNodes plus optional node names.
Fields§
§nodes: Vec<GraphNode>The graph’s nodes, in the order they were recorded.
names: BTreeMap<usize, String>Node index → dotted name captured from [crate::name_scope] at recording time.
Implementations§
Source§impl Graph
impl Graph
Sourcepub fn add_node(
&mut self,
op: Op,
inputs: Vec<usize>,
dtype: DtypeRepr,
shape: Shape,
) -> usize
pub fn add_node( &mut self, op: Op, inputs: Vec<usize>, dtype: DtypeRepr, shape: Shape, ) -> usize
Appends a new node to the graph, returning its index.
Sourcepub fn topological_sort(&self) -> Vec<usize>
pub fn topological_sort(&self) -> Vec<usize>
Returns node indices in topological order (producers before consumers) using Kahn’s algorithm. Panics if the graph contains a cycle.
Sourcepub fn optimise(&self) -> Graph
pub fn optimise(&self) -> Graph
Rewrite the graph by fusing compatible op sequences into single fused ops.
Currently recognises: Conv2d(no bias) → BatchNorm2d → Silu
and replaces the triple with a single Conv2dBnSilu node.
Nodes that are absorbed into a fused node are removed from the output graph; remaining node indices are renumbered contiguously.
This does not include Graph::fuse_elementwise_chains — that pass
produces Op::Fused nodes, and folding it in here would silently change
what every existing optimise() caller lowers to. Call it separately once
the target lowering backend actually knows how to compile Op::Fused.
Sourcepub fn fuse_elementwise_chains(&self) -> Graph
pub fn fuse_elementwise_chains(&self) -> Graph
Rewrite the graph by fusing adjacent, single-input/single-output elementwise
chains (see is_fusable_elementwise) into Op::Fused nodes.
Separate from Graph::optimise on purpose: producing Op::Fused only helps
once a lowering backend knows how to compile it (concatenate each member’s
kernel source and synthesize an entry point that runs them in sequence — see
the Op::Fused doc comment). Call this explicitly once that backend support
exists; don’t fold it into optimise(), which every existing caller already
depends on producing today’s set of ops.
Iterates Graph::fuse_elementwise_chain_pass to a fixed point: each call
only merges one adjacent pair, so a chain longer than two nodes grows by one
member per iteration until nothing more merges.