Graph Class¶
The Forge Graph class wraps around the igraph.Graph
object that is initialized by the graphical encoding provided by the graph-core API.
forge.relay.graph.graph.Graph(relay_expr: Relay.Node.obj, params: Optional[Union[dict, ParamsDict]] = None, sink_name: str = 'Sink')
¶
Wrapper around igraph.Graph instances for graph-encoded Relay
This class provides a high-level API and context for working with Forge Nodes, enabling a 1-to-1 graphical representation of any Relay expression. It includes methods for traversal, transformation, and manipulation of the graph while maintaining Relay-specific semantics.
Initializes a Graph instance from a Relay expression.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
relay_expr
|
obj
|
The Relay expression to encode as a graph. |
required |
params
|
Optional[Union[dict, ParamsDict]]
|
Optional parameters to associate with the graph. |
None
|
sink_name
|
str
|
The name of the artificial sink vertex. Defaults to "Sink". |
'Sink'
|
params: ParamsDict = params
class-attribute
instance-attribute
¶
igraph: igraph.Graph = graph_core.from_relay(relay_expr, sink_name=sink_name, input_names=input_names)
class-attribute
instance-attribute
¶
calibration: Dict[str, CalibrationMetaData] = field(init=False, factory=dict, repr=False)
class-attribute
instance-attribute
¶
fingerprint: str
property
¶
Computes a unique fingerprint for the graph, a hash of structure and data
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
A string representing the graph's unique fingerprint. |
inputs: List[Node]
property
¶
Retrieves the input nodes of the graph.
Returns:
Type | Description |
---|---|
List[Node]
|
List[Node]: A list of input nodes. |
output: Node
property
¶
Retrieves the output node of the graph.
Returns:
Name | Type | Description |
---|---|---|
Node |
Node
|
The output node. |
Note
- The output node is the node that immediately precedes the sink nodes
sink: Node
property
¶
Retrieves the artificial sink node of the graph.
Returns:
Name | Type | Description |
---|---|---|
Node |
Node
|
The sink node. |
relay_expr_type: str
property
¶
Gets the type of the Relay expression represented by the graph.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The type of the Relay expression. |
mod: Relay.IRModule.obj
property
¶
Creates the Relay IRModule encoded by the graph.
Returns:
Type | Description |
---|---|
obj
|
Relay.IRModule.obj: The represented Relay IRModule. |
typed_mod: Relay.IRModule.obj
property
¶
Creates the Relay IRModule encoded by the graph with typing.
Returns:
Type | Description |
---|---|
obj
|
Relay.IRModule.obj: The typed Relay IRModule. |
Note
- This is can be used as a 'validation' of expression correctness. An error will be thrown for invalid expressions.
operators: Dict[str, int]
property
¶
Retrieves a dictionary of operators in the graph and their usage counts.
Returns:
Type | Description |
---|---|
Dict[str, int]
|
Dict[str, int]: A mapping of operator names to their usage counts. |
find(*args, **kwargs) -> Node
¶
Finds a single node in the graph using igraph.Graph.vs.find()
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*args
|
Positional arguments for |
()
|
|
**kwargs
|
Keyword arguments for |
{}
|
Returns:
Name | Type | Description |
---|---|---|
Node |
Node
|
The found node. |
Note
-
This method is direct access to the underlying igraph method, so API matches it directly, please reference the igraph documentation.
-
If using lambda functions, the functions are expected to interact with
igraph.Vertex
objects, not Node objects. -
This method returns a Forge Node instance
select(*args, **kwargs) -> List[Node]
¶
Selects multiple nodes in the graph using igraph.Graph.vs.select()
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*args
|
Positional arguments for |
()
|
|
**kwargs
|
Keyword arguments for |
{}
|
Returns:
Type | Description |
---|---|
List[Node]
|
List[Node]: The selected nodes. |
Note
-
This method is direct access to the underlying igraph method, so API matches it directly, please reference the igraph documentation.
-
If using lambda functions, the functions are expected to interact with
igraph.Vertex
objects, not Node objects. -
This method returns a Forge Node instance
copy() -> Graph
¶
Creates a deep copy of the graph.
Returns:
Name | Type | Description |
---|---|---|
Graph |
Graph
|
A deep copy of the current graph instance. |
get_inference_function(root: Optional[Union[str, Node, igraph.Vertex]] = None, target: str = 'llvm') -> Callable
¶
Generates an inference function for the graph.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
root
|
Optional[Union[str, Node, Vertex]]
|
The root node for inference. Defaults to None (i.e. the sink node). |
None
|
target
|
str
|
The target for inference (e.g., "llvm"). Defaults to "llvm". |
'llvm'
|
Returns:
Name | Type | Description |
---|---|---|
Callable |
Callable
|
A callable function for inference. |
graft_relay_expr(relay_expr: Relay.Node.obj, target_names: List[str], target_argidxs: List[int], *, root_name: Optional[str] = None) -> str
¶
Grafts a Relay expression onto the graph.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
relay_expr
|
obj
|
The Relay expression to graft. |
required |
target_names
|
List[str]
|
Names of the target nodes in the graph. |
required |
target_argidxs
|
List[int]
|
Argument indices for the target nodes. |
required |
root_name
|
Optional[str]
|
Optional custom name for the root of the grafted expression. If None, a random node name will be generated. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The name of the grafted root node. |
dfs_iter(root: Optional[Union[str, Node, igraph.Vertex]] = None, in_order: bool = True) -> Iterator
¶
Iterates over the graph using depth-first search (DFS) from some root node.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
root
|
Optional[Union[str, Node, Vertex]]
|
The root node to start the DFS from. If None, the sink node is used as the root. |
None
|
in_order
|
bool
|
Whether to perform an in-order DFS traversal. Defaults to True. |
True
|
Returns:
Name | Type | Description |
---|---|---|
Iterator |
Iterator
|
An iterator over the nodes in DFS order. |
get_relay_mod(root: Optional[Union[str, Node, igraph.Vertex]] = None, typed: bool = True, global_vars: Optional[Dict[str, Relay.GlobalVar.obj]] = None) -> Relay.IRModule.obj
¶
Converts the graph rooted at some node into a Relay IRModule.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
root
|
Optional[Union[str, Node, Vertex]]
|
The root node to start from. If None, the sink node is used. Defaults to None. |
None
|
typed
|
bool
|
Whether to produce a typed Relay IRModule. Defaults to True. |
True
|
global_vars
|
Optional[Dict[str, obj]]
|
A dictionary of global variables to include. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
obj
|
Relay.IRModule.obj: The corresponding Relay IRModule. |
get_relay_func(root: Optional[Union[str, Node, igraph.Vertex]] = None, typed: bool = True, global_vars: Optional[Dict[str, Relay.GlobalVar.obj]] = None) -> Relay.Function.obj
¶
Converts the graph rooted at some node into a Relay Function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
root
|
Optional[Union[str, Node, Vertex]]
|
The root node to start from. If None, the sink node is used. Defaults to None. |
None
|
typed
|
bool
|
Whether to produce a typed Relay Function. Defaults to True. |
True
|
global_vars
|
Optional[Dict[str, obj]]
|
A dictionary of global variables to include. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
obj
|
Relay.Function.obj: The corresponding Relay Function. |
get_relay_expr(root: Optional[Union[str, Node, igraph.Vertex]] = None, typed: bool = True, global_vars: Optional[Dict[str, Relay.GlobalVar.obj]] = None) -> Relay.Node.obj
¶
Converts the graph rooted at some node into a Relay expression.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
root
|
Optional[Union[str, Node, Vertex]]
|
The root node to start from. If None, the sink node is used. Defaults to None. |
None
|
typed
|
bool
|
Whether to produce a typed Relay expression. Defaults to True. |
True
|
global_vars
|
Optional[Dict[str, obj]]
|
A dictionary of global variables to include. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
obj
|
Relay.Node.obj: The corresponding Relay expression. |
get_unbound_var_nodes(root: Optional[Union[str, Node, igraph.Vertex]] = None, include_inputs: bool = True) -> List[Node]
¶
Retrieves unbound variable nodes in the graph.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
root
|
Optional[Union[str, Node, Vertex]]
|
The root node to start from. If None, the sink node is used. Defaults to None. |
None
|
include_inputs
|
bool
|
Whether to include input nodes in the result. Defaults to True. |
True
|
Returns:
Type | Description |
---|---|
List[Node]
|
List[Node]: A list of unbound variable nodes. |
get_bound_var_nodes(root: Optional[Union[str, Node, igraph.Vertex]] = None) -> List[Node]
¶
reload()
¶
Reloads the graph to restore its original state.
This function resets the graph to its initial state, restoring the original igraph bytes.
__iter__() -> Iterator
¶
Iterates over all nodes in the graph in DFS from the sink.
Returns:
Name | Type | Description |
---|---|---|
Iterator |
Iterator
|
An iterator over the nodes in the graph. |
__eq__(other) -> bool
¶
Checks structural equality between two Graph instances.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other
|
Graph
|
The other graph to compare with. |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the graphs are equal, False otherwise. |
__hash__() -> int
¶
Computes a structural hash value for the graph.
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
The hash value of the graph. |
__repr__() -> str
¶
Generates a string representation of the graph.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
A string describing the graph. |
__getstate__() -> dict
¶
Serializes the graph state for pickling.
Returns:
Name | Type | Description |
---|---|---|
dict |
dict
|
The serialized state of the graph. |
__setstate__(state) -> None
¶
Restores the graph state from a serialized representation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state
|
dict
|
The serialized state to restore. |
required |
_init_node(vertex: igraph.Vertex) -> Node
¶
Initializes a Node instance from an igraph.Vertex.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
vertex
|
Vertex
|
The vertex to initialize a Node from. |
required |
Returns:
Name | Type | Description |
---|---|---|
Node |
Node
|
The initialized Node. |
_init_nodes(vertices: Union[Iterable[igraph.Vertex], igraph.VertexSeq]) -> List[Node]
¶
Initializes multiple Node instances from igraph vertices.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
vertices
|
Union[Iterable[Vertex], VertexSeq]
|
The vertices to initialize Nodes from. |
required |
Returns:
Type | Description |
---|---|
List[Node]
|
List[Node]: A list of initialized Nodes. |
_get_ivertex(node: Union[str, Node, igraph.Vertex]) -> igraph.Vertex
¶
Retrieves the corresponding igraph.Vertex for a given node.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
node
|
Union[str, Node, Vertex]
|
The node to retrieve the vertex for. |
required |
Returns:
Type | Description |
---|---|
Vertex
|
igraph.Vertex: The corresponding igraph.Vertex. |