pub struct NirGraph {
pub nodes: IndexMap<String, NirNode>,
pub edges: Vec<(String, String)>,
pub metadata: HashMap<String, MetadataValue>,
pub version: Option<String>,
}Expand description
Directed NIR computation graph.
Node insertion order is preserved via IndexMap (stable iteration for
serialization and debugging). Edges are an ordered list of (src, dst)
name pairs.
Fields§
§nodes: IndexMap<String, NirNode>Named computational nodes (insertion-ordered).
edges: Vec<(String, String)>Directed edges as (source_name, destination_name).
metadata: HashMap<String, MetadataValue>Free-form graph metadata.
version: Option<String>Optional NIR version string (set when loading from HDF5 in v0.3).
Implementations§
Source§impl NirGraph
impl NirGraph
Sourcepub const MAX_NESTING_DEPTH: usize = 1024
pub const MAX_NESTING_DEPTH: usize = 1024
Maximum nesting depth of NirNode::Graph subgraphs that
Self::validate_structure will walk.
The root graph has depth 0. Each nested NirNode::Graph increments
the depth. A subgraph whose depth would exceed this limit is rejected
with NirError::InvalidGraph instead of growing the process stack.
This bound is independent of the HDF5 reader/writer nested-graph budget: it applies to adversarial in-memory graphs after deserialization succeeds; parsing depth is controlled by the serde format and deserializer.
Sourcepub fn add_edge(&mut self, from: impl Into<String>, to: impl Into<String>)
pub fn add_edge(&mut self, from: impl Into<String>, to: impl Into<String>)
Append a directed edge (from → to) without validating endpoints.
Call validate_structure to check that
endpoints exist and that the edge is not duplicated.
Sourcepub fn get_mut(&mut self, name: &str) -> Option<&mut NirNode>
pub fn get_mut(&mut self, name: &str) -> Option<&mut NirNode>
Mutably borrow the node named name, if present.
Sourcepub fn validate_structure(&self) -> Result<()>
pub fn validate_structure(&self) -> Result<()>
Validate structural integrity.
Checks:
- every edge endpoint names an existing node
- no duplicate directed edges
(src, dst) - nested
NirNode::Graphsubgraphs also validate, in node insertion order, depth-first
Nested subgraphs are walked with an explicit heap-allocated work list, so
process-stack usage does not grow with nesting depth. A chain deeper than
Self::MAX_NESTING_DEPTH fails with NirError::InvalidGraph.
Cycles are allowed. Type/shape inference is out of scope for v0.2.
Convolution and pooling parameter invariants are checked separately by
validate_parameters.
First-error ordering matches a recursive walk: missing endpoints, then duplicate edges, then nested subgraphs in insertion order. Nested failures are re-prefixed so the message names each enclosing subgraph.
§Errors
NirError::MissingNodeif an endpoint is unknownNirError::DuplicateEdgeif the same directed edge appears twiceNirError::InvalidGraphif a nested subgraph fails validation, or if nesting exceedsSelf::MAX_NESTING_DEPTH
Sourcepub fn validate_parameters(&self) -> Result<()>
pub fn validate_parameters(&self) -> Result<()>
Validate local convolution and pooling parameter invariants.
Complements validate_structure: structure
checks only edge endpoints and duplicate directed edges, while this
walks each node for weight rank, grouped-convolution divisibility,
stride/dilation/padding extents, bias length, and pooling windows.
HDF5 crate::io::read does not call this method. The default
writer also does not — invoke it explicitly after assembling or
importing a graph.
Nested crate::NirNode::Graph subgraphs are visited. Failures name
the node with a /-separated path ("encoder/conv").
§Errors
NirError::InvalidNodeParameters for the first node whose
convolution or pooling fields violate a local invariant.