# Copyright (c) ONNX Project Contributors# SPDX-License-Identifier: Apache-2.0"""Utilities for traversing the IR graph."""from__future__importannotations__all__=["RecursiveGraphIterator",]fromcollections.abcimportIterator,ReversiblefromtypingimportCallable,Unionfromtyping_extensionsimportSelffromonnx_irimport_core,_enumsGraphLike=Union[_core.Graph,_core.Function,_core.GraphView]
[docs]def__init__(self,graph_like:GraphLike,*,recursive:Callable[[_core.Node],bool]|None=None,reverse:bool=False,):"""Iterate over the nodes in the graph, recursively visiting subgraphs. Args: graph_like: The graph to traverse. recursive: A callback that determines whether to recursively visit the subgraphs contained in a node. If not provided, all nodes in subgraphs are visited. reverse: Whether to iterate in reverse order. """self._graph=graph_likeself._recursive=recursiveself._reverse=reverseself._iterator=self._recursive_node_iter(graph_like)