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
¶
Graph(
relay_expr: 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'
|
Methods:
Name | Description |
---|---|
find |
Finds a single node in the graph using |
select |
Selects multiple nodes in the graph using |
copy |
Creates a deep copy of the graph. |
get_inference_function |
Generates an inference function for the graph. |
graft_relay_expr |
Grafts a Relay expression onto the graph. |
dfs_iter |
Iterates over the graph using depth-first search (DFS) from some root node. |
get_relay_mod |
Converts the graph rooted at some node into a Relay IRModule. |
get_relay_func |
Converts the graph rooted at some node into a Relay Function. |
get_relay_expr |
Converts the graph rooted at some node into a Relay expression. |
get_unbound_var_nodes |
Retrieves unbound variable nodes in the graph. |
get_bound_var_nodes |
Retrieves bound variable nodes in the graph. |
reload |
Reloads the graph to restore its original state. |
__iter__ |
Iterates over all nodes in the graph in DFS from the sink. |
__eq__ |
Checks structural equality between two Graph instances. |
__hash__ |
Computes a structural hash value for the graph. |
__repr__ |
Generates a string representation of the graph. |
__getstate__ |
Serializes the graph state for pickling. |
__setstate__ |
Restores the graph state from a serialized representation. |
_init_node |
Initializes a Node instance from an igraph.Vertex. |
_init_nodes |
Initializes multiple Node instances from igraph vertices. |
_get_ivertex |
Retrieves the corresponding igraph.Vertex for a given node. |
Attributes:
Name | Type | Description |
---|---|---|
params |
ParamsDict
|
|
igraph |
Graph
|
|
calibration |
Dict[str, CalibrationMetaData]
|
|
fingerprint |
str
|
Computes a unique fingerprint for the graph, a hash of structure and data |
inputs |
List[Node]
|
Retrieves the input nodes of the graph. |
output |
Node
|
Retrieves the output node of the graph. |
sink |
Node
|
Retrieves the artificial sink node of the graph. |
relay_expr_type |
str
|
Gets the type of the Relay expression represented by the graph. |
mod |
obj
|
Creates the Relay IRModule encoded by the graph. |
typed_mod |
obj
|
Creates the Relay IRModule encoded by the graph with typing. |
operators |
Dict[str, int]
|
Retrieves a dictionary of operators in the graph and their usage counts. |
Attributes¶
igraph
class-attribute
instance-attribute
¶
igraph: Graph = from_relay(
relay_expr, sink_name=sink_name, input_names=input_names
)
calibration
class-attribute
instance-attribute
¶
calibration: Dict[str, CalibrationMetaData] = field(
init=False, factory=dict, repr=False
)
fingerprint
property
¶
fingerprint: str
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
property
¶
inputs: List[Node]
Retrieves the input nodes of the graph.
Returns:
Type | Description |
---|---|
List[Node]
|
List[Node]: A list of input nodes. |
output
property
¶
output: Node
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
property
¶
sink: Node
Retrieves the artificial sink node of the graph.
Returns:
Name | Type | Description |
---|---|---|
Node |
Node
|
The sink node. |
relay_expr_type
property
¶
relay_expr_type: str
Gets the type of the Relay expression represented by the graph.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The type of the Relay expression. |
mod
property
¶
mod: obj
Creates the Relay IRModule encoded by the graph.
Returns:
Type | Description |
---|---|
obj
|
Relay.IRModule.obj: The represented Relay IRModule. |
typed_mod
property
¶
typed_mod: obj
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
property
¶
operators: Dict[str, int]
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. |
Functions¶
find
¶
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
¶
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
¶
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
¶
get_inference_function(
root: Optional[Union[str, Node, 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
¶
graft_relay_expr(
relay_expr: 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
¶
dfs_iter(
root: Optional[Union[str, Node, 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
¶
get_relay_mod(
root: Optional[Union[str, Node, Vertex]] = None,
typed: bool = True,
global_vars: Optional[Dict[str, obj]] = None,
) -> 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
¶
get_relay_func(
root: Optional[Union[str, Node, Vertex]] = None,
typed: bool = True,
global_vars: Optional[Dict[str, obj]] = None,
) -> 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
¶
get_relay_expr(
root: Optional[Union[str, Node, Vertex]] = None,
typed: bool = True,
global_vars: Optional[Dict[str, obj]] = None,
) -> 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
¶
get_unbound_var_nodes(
root: Optional[Union[str, Node, 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
¶
reload
¶
reload()
Reloads the graph to restore its original state.
This function resets the graph to its initial state, restoring the original igraph bytes.
__iter__
¶
__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__
¶
__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__
¶
__hash__() -> int
Computes a structural hash value for the graph.
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
The hash value of the graph. |
__repr__
¶
__repr__() -> str
Generates a string representation of the graph.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
A string describing the graph. |
__getstate__
¶
__getstate__() -> dict
Serializes the graph state for pickling.
Returns:
Name | Type | Description |
---|---|---|
dict |
dict
|
The serialized state of the graph. |
__setstate__
¶
__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
¶
_init_node(vertex: 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
¶
_init_nodes(vertices: Union[Iterable[Vertex], 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
¶
_get_ivertex(node: Union[str, Node, Vertex]) -> 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. |