pulsatrix
Loading...
Searching...
No Matches
pulsatrix::ComputationGraph Class Reference

Owns every Node in a computation graph and exposes read access for graph-walking code (autograd's backward pass, Phase 2+ explainers). More...

#include <computation_graph.hpp>

Public Member Functions

NodeId add_node (OpType op_type, Shape shape, std::optional< std::string > label=std::nullopt, std::vector< NodeId > parent_ids={})
 Adds a node to the graph and wires it to its parents.
 
const Node & node (NodeId id) const
 Looks up a node by id.
 
size_t node_count () const
 Number of nodes currently in the graph.
 
std::vector< NodeId > nodes_by_op_type (OpType op_type) const
 Finds every node with the given op type.
 
std::vector< NodeId > topological_order () const
 Returns every node id in a valid topological order (every node after all its parents).
 

Detailed Description

Owns every Node in a computation graph and exposes read access for graph-walking code (autograd's backward pass, Phase 2+ explainers).

Note
This class exposes no method that removes a node or clears the graph. That is deliberate, not an oversight – the charter's central Phase 0 requirement is that the graph survives past the backward pass (Mission 3 exercises this directly). Nothing in this codebase should be able to silently discard graph structure.

Member Function Documentation

◆ add_node()

NodeId pulsatrix::ComputationGraph::add_node ( OpType  op_type,
Shape  shape,
std::optional< std::string >  label = std::nullopt,
std::vector< NodeId >  parent_ids = {} 
)

Adds a node to the graph and wires it to its parents.

Parameters
op_typeThe operation category this node represents.
shapeThis node's output shape.
labelOptional human-readable label.
parent_idsIds of this node's parents. Every id must already exist in the graph (i.e. refer to a node added by an earlier call) – this constraint is what guarantees insertion order is already a valid topological order.
Returns
The new node's id.

◆ node()

const Node & pulsatrix::ComputationGraph::node ( NodeId  id) const

Looks up a node by id.

Parameters
idNode id, must be < node_count().
Returns
The node.

◆ node_count()

size_t pulsatrix::ComputationGraph::node_count ( ) const
inline

Number of nodes currently in the graph.

◆ nodes_by_op_type()

std::vector< NodeId > pulsatrix::ComputationGraph::nodes_by_op_type ( OpType  op_type) const

Finds every node with the given op type.

Parameters
op_typeOp type to match.
Returns
Ids of matching nodes, in insertion order. Empty if none match.
Note
This is the op-type-tagging query mechanism from charter Part 2 SS3 – e.g. Grad-CAM finding "the last conv layer" queries this, never by layer name.

◆ topological_order()

std::vector< NodeId > pulsatrix::ComputationGraph::topological_order ( ) const

Returns every node id in a valid topological order (every node after all its parents).

Returns
Node ids in topological order.
Note
Simply returns insertion order – add_node()'s constraint that parent_ids must already exist in the graph guarantees insertion order is already topological. If a future relaxation of that constraint is ever considered, this method's implementation would need to change to an explicit sort; it does not today.

The documentation for this class was generated from the following file: