Python IrGraph. Beneath it is a core.Graph, which is used for creating a c++ Ir Pass Graph. An IrGraph is just a graph view of a Program. In an IrGraph, both Variables and Operators are graph nodes.
| 5581 | |
| 5582 | |
| 5583 | class IrGraph: |
| 5584 | """ |
| 5585 | Python IrGraph. Beneath it is a core.Graph, which is used for |
| 5586 | creating a c++ Ir Pass Graph. An IrGraph is just a graph view of |
| 5587 | a Program. In an IrGraph, both Variables and Operators are graph |
| 5588 | nodes. |
| 5589 | """ |
| 5590 | |
| 5591 | def __init__(self, graph, for_test=False): |
| 5592 | """ |
| 5593 | Construct an IrGraph using core.Graph. |
| 5594 | |
| 5595 | Args: |
| 5596 | graph(core.Graph): C++ Graph. |
| 5597 | for_test(bool): True for the test graph and false for the train graph. |
| 5598 | """ |
| 5599 | assert isinstance(graph, core.Graph), ( |
| 5600 | "graph must be the instance of core.Graph." |
| 5601 | ) |
| 5602 | self.graph = graph |
| 5603 | self._for_test = for_test |
| 5604 | |
| 5605 | def clone(self): |
| 5606 | """ |
| 5607 | Create a new and duplicated IrGraph. |
| 5608 | |
| 5609 | Warns: |
| 5610 | The method only clones the graph structure, not its attributes. |
| 5611 | |
| 5612 | Returns: |
| 5613 | IrGraph: A new and duplicated graph. |
| 5614 | """ |
| 5615 | g = self.graph.clone() |
| 5616 | return IrGraph(g, self._for_test) |
| 5617 | |
| 5618 | def is_test(self): |
| 5619 | """ |
| 5620 | If the graph is used for testing, the function returns true. Otherwise, returns false. |
| 5621 | """ |
| 5622 | return self._for_test |
| 5623 | |
| 5624 | def all_nodes(self): |
| 5625 | """ |
| 5626 | Return all nodes included in the graph as a set. |
| 5627 | """ |
| 5628 | return {IrNode(node) for node in self.graph.nodes()} |
| 5629 | |
| 5630 | def all_var_nodes(self): |
| 5631 | """ |
| 5632 | Return all variable nodes included in the graph as a set. |
| 5633 | """ |
| 5634 | return {IrVarNode(node) for node in self.graph.nodes() if node.is_var()} |
| 5635 | |
| 5636 | def all_persistable_nodes(self): |
| 5637 | """ |
| 5638 | Return all persistable variable nodes included in the graph as a set. |
| 5639 | """ |
| 5640 | persistable_nodes = set() |
no outgoing calls